UNPKG

@liqd-js/mongodb-model

Version:
104 lines (103 loc) 4.54 kB
import { Document, Filter as MongoFilter, FindOptions, Sort, UpdateFilter } from "mongodb"; import { AbstractModelConverter, SyncComputedPropertyMethod } from "../../types"; type Filter = Record<string, any>; export declare const toBase64: (str: string) => string; export declare const fromBase64: (str: string) => string; export declare function convert<DBE extends Document>(model: object, converter: AbstractModelConverter<DBE>, dbe: DBE, conversion: string | number | symbol): Promise<unknown>; export declare function reverseSort(sort: Sort): Sort; export declare function sortProjection(sort: Sort, id: string): Record<string, 1>; export declare function resolveBSONValue(value: any): any; export declare function resolveBSONObject(obj: Record<string, any>): Record<string, any>; export declare function addPrefixToFilter(filter: Filter, prefix: string, prefixKeys?: boolean): Filter; export declare function addPrefixToPipeline(pipeline: Document[], prefix: string): Document[]; export declare function addPrefixToUpdate<RootDBE, DBE>(update: Partial<DBE> | UpdateFilter<DBE>, prefix: string): Partial<RootDBE> | UpdateFilter<RootDBE>; export declare function isExclusionProjection<DBE extends Document>(projection: FindOptions<DBE>['projection']): boolean; export declare function projectionToReplace<DBE extends Document>(projection?: FindOptions<DBE>['projection'], prefix?: string): Record<string, unknown>; export declare function projectionToProject<DBE extends Document>(projection?: FindOptions<DBE>['projection'], prefix?: string, prefixKeys?: boolean): Record<string, unknown>; export declare function bsonValue(value: any): any; export declare function isUpdateOperator(update: object): boolean; export declare function toUpdateOperations<T>(update: Partial<T> | UpdateFilter<T>): UpdateFilter<T>; export declare function getCursor(dbe: Document, sort: Sort): string; export declare function generateCursorCondition(cursor: string, sort: Sort): Filter; export declare function collectAddedFields(pipeline: any[]): string[]; /** * Optimizes match filter by merging conditions and removing unnecessary operators * @param obj - filter to optimize * @returns optimized filter */ export declare function optimizeMatch(obj: MongoFilter<any>): MongoFilter<any> | undefined; /** * Builds update parameters for property model update * @param paths * @param parentID // * @param operation - create or update - determines if filtering should be done on parentID or _id */ export declare function propertyModelUpdateParams(paths: { path: string; array: boolean; }[], parentID: any): { parentIDPath: string; updatePath: string; arrayFilters: Document[]; }; export declare function mergeComputedProperties(...objects: { [path: string]: Awaited<ReturnType<SyncComputedPropertyMethod>>; }[]): { [path: string]: Awaited<ReturnType<SyncComputedPropertyMethod>>; }; /** * Merges properties of multiple objects into one object - helper for optimizeMatch * @param objects - objects to merge * @returns merged object or false if there are conflicting properties */ export declare function mergeProperties(...objects: object[]): object | false; /** * Extracts fields used in the pipeline * @param pipeline */ export declare function getUsedFields(pipeline: Document[]): { used: string[]; ignored: string[]; }; /** * Splits filter into stages to be put between unwinds based on the path * @param filter * @param paths * @returns {MongoFilter[]} - array of optimized filters for each stage */ export declare function splitFilterToStages<DBE>(filter: MongoFilter<DBE>, paths: { path: string; array: boolean; }[]): MongoFilter<DBE>[]; /** * Create subPaths for unwinds * a[].b.c[].d[].e.f => a, b.c, d, e.f * @param paths */ export declare function getSubPaths(paths: { path: string; array: boolean; }[]): string[]; /** * Extracts properties from filter that are relevant for the given stage * @param filter * @param stage * @param nextStage * @param paths */ export declare function subfilter(filter: MongoFilter<any>, stage: string, nextStage: string, paths: { path: string; array: boolean; }[]): MongoFilter<any>; export declare function transformToElemMatch(key: string, value: any[], operator: '$in' | '$nin', paths: { path: string; array: boolean; }[]): { key: string; value: { $elemMatch: object; }; } | false; export declare const isSet: (value: any) => boolean; export declare const Arr: (value: any) => any[]; export {};