UNPKG

mingo-exp

Version:

extend mingo with any custom function and support async code in aggregations

111 lines (109 loc) 5.42 kB
import { ExpressionOperator, Options } from 'mingo/core'; import { RawObject } from 'mingo/types'; declare type ParseFunc = (...args: any[]) => any; declare type FuncVal = string | number | null | undefined; /** * alternative for generateCustomOperator, generate single operator that can execute custom functions by type and arguments * @augments additionalCustomFunctions object with parsing functions that can be accessed by their type(key in this object) and args, you can pass any function you want to use in aggregations, async functions are supported using AsyncAggregator * also gives useful default parsing functions * @returns default type-> date: (val) => string // ISOString using dayjs * @returns default type-> number: (val) => number * @returns default type-> text: (val) => string // trimmed string * @returns default type-> boolean: (val: string | boolean) => boolean // support for boolean or string representations: 'TRUE' 'False' 'true' false * @returns default type-> point: (longitude: number, latitude: number) => string // wkt string * @returns default type-> wkt: (wktString: string) => string // valid wkt string (without self intersections using '@turf/unkink-polygon') * @returns default type-> geoJson: (obj) => string // wkt string * @returns default type-> orDefault: (val, defaultVal) => unknown * @returns default type-> concat: (separator: string, ...args: string[]) => string * @example const { $customParse } = customParseExpression({ multAsync: async (num1: number, num2: number) => { await delay(1000); return num1*num2; }, lodashGet: _.get }); * @example useOperators(OperatorType.EXPRESSION, { $customParse }); //must do to be able to use in aggregations * @example { $project: { id: '$a.b', response: { type: 'multAsync', args: ['$a.b', 5] } // this will run $multAsync(doc.a.b, 5), arguments are passed by order in an array } },... * */ export declare function customParseExpression(additionalCustomFunctions?: Record<string, ParseFunc>): { customFunctions: { date: (val: FuncVal) => string; number: (val: FuncVal) => number; text: (val: FuncVal) => string; boolean: (val: FuncVal) => boolean | undefined; point: (longitude: number, latitude: number) => string; wkt: (wktString: string) => string; geoJson: (obj: any) => string; orDefault: (val: unknown, defaultVal: unknown) => unknown; concat: (separator: string, ...args: FuncVal[]) => string; }; $customParse: ExpressionOperator; }; /** * simplified operation custom generation * @augments customOperators object with keys that are names for custom operators and values are any function you want to use in aggregation, async functions are supported using AsyncAggregator * @example const { $multAsync, $lodashGet } = generateCustomOperator({ $multAsync: async (num1: number, num2: number) => { await delay(1000); return num1*num2; }, $lodashGet: _.get }); * @example useOperators(OperatorType.EXPRESSION, { $multAsync, $lodashGet }); //must do to be able to use in aggregations * @example { $project: { id: '$a.b', response: { $multAsync: ['$a.b', 5] } // this will run $multAsync(doc.a.b, 5), arguments are passed by order in an array } },... * */ export declare function generateCustomOperator<T extends Record<string, ParseFunc>>(customOperators: T): Record<string, ExpressionOperator>; /** * custom AsyncAggregator expression, caches request based on operation and its arguments, run it only once and returns the same response * no need to call useOperators(OperatorType.EXPRESSION, { $runOnce }) because AsyncAggregator calls it already * @example { $project: { id: '$a.b', response: { $runOnce: { $someAsyncCustomOperator: ['$ids'] } } // this will run someAsyncCustomOperator(ids) only once and use the response for every doc that called someAsyncCustomOperator(ids) with the same ids } },... * */ export declare function $runOnce(obj: unknown, args: unknown[], options?: Options): Promise<any>; /** * custom AsyncAggregator expression, collects values to an array * no need to call useOperators(OperatorType.EXPRESSION, { $collect }) because AsyncAggregator calls it already * @example { $project: { id: '$a.b', idsForBatch: { $collect: '$a.b' } // this will put in every doc an array named idsForBatch with every value of $a.b } },... * */ export declare function $collect(obj: unknown, args: unknown[], options?: Options): Promise<any>; /** * Provides functionality for the mongoDB aggregation pipeline * similar to Aggregator but with async support (awaits all promises after each step in the aggregation) * uses $collect and $runOnce custom expression operators by default * * @param pipeline an Array of pipeline operators * @param options An optional Options to pass the aggregator * @constructor */ export declare class AsyncAggregator { private readonly pipeline; private readonly options?; constructor(pipeline: Array<RawObject>, options?: Options | undefined); private addPipelineIdPipe; private unsetPipelineIdPipe; run(collection: Array<RawObject>): Promise<Array<RawObject>>; } export {};