import { RxQueryBase } from 'nxdb-old/src/rx-query'; import { StringKeys } from 'nxdb-old/src/types/util'; /** * Typed Mango Query Selector * @link https://github.com/mongodb/node-mongodb-native/blob/26bce4a8debb65df5a42dc8599e886c9c83de10d/src/mongo_types.ts * @link https://stackoverflow.com/a/58436959/3443137 */ type Join = K extends string | number ? P extends string | number ? `${K}${'' extends P ? '' : '.'}${P}` : never : never; export type Paths = [D] extends [never] ? never : T extends object ? { [K in keyof T]-?: K extends string | number ? `${K}` | (Paths extends infer R ? Join : never) : never }[keyof T] : ''; export type Leaves = [D] extends [never] ? never : T extends object ? { [K in keyof T]-?: Join> }[keyof T] : ''; type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]; export type PropertyType = string extends Property ? unknown : Property extends keyof Type ? Type[Property] : Property extends `${number}` ? Type extends ReadonlyArray ? ArrayType : unknown : Property extends `${infer Key}.${infer Rest}` ? Key extends `${number}` ? Type extends ReadonlyArray ? PropertyType : unknown : Key extends keyof Type ? Type[Key] extends Map ? MapType : PropertyType : unknown : unknown; /* * The MongoDB query library is huge and we do not need all the operators. * If you add an operator here, make sure that you properly add a test in * the file /test/unit/rx-storage-query-correctness.test.ts * * @link https://github.com/kofrasa/mingo#es6 */ export interface MangoQueryOperators { $eq?: PathValueType; $gt?: PathValueType; $gte?: PathValueType; $lt?: PathValueType; $lte?: PathValueType; $ne?: PathValueType; $in?: PathValueType[]; $nin?: PathValueType[]; $regex?: string | RegExp; $options?: string; $exists?: boolean; $type?: 'null' | 'boolean' | 'number' | 'string' | 'array' | 'object'; $mod?: number; $not?: PathValueType; $size?: number; $elemMatch?: MangoQuerySelector; } export type MangoQuerySelector = Partial<{ [Property in Paths]: MangoQueryOperators | PropertyType; }> & { $and?: MangoQuerySelector[]; $or?: MangoQuerySelector[]; $nor?: MangoQuerySelector[]; }; /** * Discussion was at: * @link https://github.com/nxpkg/nxdb/issues/1972 */ export type MangoQuerySortDirection = 'asc' | 'desc'; export type MangoQuerySortPart = { [k in StringKeys | string]: MangoQuerySortDirection; }; export type MangoQuerySelectorAndIndex = { /** * Selector is optional, * if not given, the query matches all documents * that are not _deleted=true. */ selector?: MangoQuerySelector; /** * By default, the RxStorage implementation * decides which index to use when running the query. * * For better performance, a different index might be defined * by setting it in the query. * How this improves performance and if the defined index is used, * depends on the RxStorage implementation. */ index?: string | string[]; }; export type MangoQueryNoLimit = MangoQuerySelectorAndIndex & { /** * Sorting of the results. * If no sort is set, NxDB will sort by the primary key. * Also if sort is set, NxDB will add primaryKey sorting * if the primaryKey was not in the sort parameters before. * This ensures that there is a deterministic sorting of the * results, not mather at which order the documents have been * inserted into the storage. */ sort?: MangoQuerySortPart[]; }; export type MangoQuery = MangoQueryNoLimit & { skip?: number; limit?: number; }; export type RxQueryOP = 'find' | 'findOne' | 'count' | 'findByIds'; export declare class RxQuery extends RxQueryBase { equals(queryObj: any): RxQuery; eq(queryObj: any): RxQuery; or(queryObj: keyof RxDocumentType | string | any[]): RxQuery; nor(queryObj: keyof RxDocumentType | string | any[]): RxQuery; and(queryObj: keyof RxDocumentType | string | any[]): RxQuery; gt(queryObj: any): RxQuery; gte(queryObj: any): RxQuery; lt(queryObj: any): RxQuery; lte(queryObj: any): RxQuery; ne(queryObj: any): RxQuery; in(queryObj: any[]): RxQuery; nin(queryObj: any[]): RxQuery; all(queryObj: any): RxQuery; regex(queryObj: RegExp): RxQuery; exists(queryObj: any): RxQuery; elemMatch(queryObj: any): RxQuery; mod(p1: any, p2: any, p3: any): RxQuery; }