import { createQueryBuilder, OTHER_MANGO_ATTRIBUTES, OTHER_MANGO_OPERATORS } from 'nxdb-old/src/plugins/query-builder/mquery/nosql-query-builder'; import type { RxPlugin, RxQuery } from 'nxdb-old/src/types'; import { createRxQuery } from 'nxdb-old/src/rx-query'; import { clone } from 'nxdb-old/src/plugins/utils'; // if the query-builder plugin is used, we have to save its last path const RXQUERY_OTHER_FLAG = 'queryBuilderPath'; export function runBuildingStep( rxQuery: RxQuery, functionName: string, value: any ): RxQuery { const queryBuilder = createQueryBuilder(clone(rxQuery.mangoQuery), rxQuery.other[RXQUERY_OTHER_FLAG]); (queryBuilder as any)[functionName](value); // run const queryBuilderJson = queryBuilder.toJSON(); return createRxQuery( rxQuery.op, queryBuilderJson.query, rxQuery.collection, { ...rxQuery.other, [RXQUERY_OTHER_FLAG]: queryBuilderJson.path } ) as RxQuery; } export function applyBuildingStep( proto: any, functionName: string ): void { proto[functionName] = function (this: RxQuery, value: any) { return runBuildingStep(this, functionName, value); }; } export * from 'nxdb-old/src/plugins/query-builder/mquery/nosql-query-builder'; export const NxDBQueryBuilderPlugin: RxPlugin = { name: 'query-builder', nxdb: true, prototypes: { RxQuery(proto: any) { [ 'where', 'equals', 'eq', 'or', 'nor', 'and', 'mod', 'exists', 'elemMatch', 'sort' ].forEach(attribute => { applyBuildingStep(proto, attribute); }); OTHER_MANGO_ATTRIBUTES.forEach(attribute => { applyBuildingStep(proto, attribute); }); OTHER_MANGO_OPERATORS.forEach(operator => { applyBuildingStep(proto, operator); }); } } };