UNPKG

6.51 kBTypeScriptView Raw
1import { Iterator } from "./lazy";
2import { AnyVal, Callback, Collection, Predicate, RawObject } from "./types";
3import { HashFunction, resolve } from "./util";
4/**
5 * Resolves the given string to a Collection.
6 * This is useful for operators that require a second collection to use such as $lookup and $out.
7 * The collection is not cached and will be resolved each time it is used.
8 */
9export declare type CollectionResolver = (name: string) => Collection;
10/** Specification for collation options */
11export interface CollationSpec {
12 readonly locale: string;
13 readonly caseLevel?: boolean;
14 readonly caseFirst?: string;
15 readonly strength?: number;
16 readonly numericOrdering?: boolean;
17 readonly alternate?: string;
18 readonly maxVariable?: string;
19 readonly backwards?: boolean;
20}
21/**
22 * JSON schema validator
23 */
24export declare type JsonSchemaValidator = (schema: RawObject) => Predicate<RawObject>;
25/**
26 * This controls how input and output documents are processed to meet different application needs.
27 * Each mode has different trade offs for; immutability, reference sharing, and performance.
28 */
29export declare enum ProcessingMode {
30 /**
31 * Clone inputs prior to processing, and the outputs if some objects graphs may be shared.
32 * Use this option to keep input collection immutable and to get distinct output objects.
33 *
34 * Note: This option is expensive and reduces performance.
35 */
36 CLONE_ALL = "CLONE_ALL",
37 /**
38 * Clones inputs prior to processing.
39 * This option will return output objects with shared graphs in their path if specific operators are used.
40 * Use this option to keep the input collection immutable.
41 *
42 */
43 CLONE_INPUT = "CLONE_INPUT",
44 /**
45 * Clones the output to return distinct objects with no shared paths.
46 * This option modifies the input collection and during processing.
47 */
48 CLONE_OUTPUT = "CLONE_OUTPUT",
49 /**
50 * Turn off cloning and modifies the input collection as needed.
51 * This option will also return output objects with shared paths in their graph when specific operators are used.
52 *
53 * This option provides the greatest speedup for the biggest tradeoff. When using the aggregation pipeline, you can use
54 * the "$out" operator to collect immutable intermediate results.
55 *
56 * @default
57 */
58 CLONE_OFF = "CLONE_OFF"
59}
60/**
61 * Generic options interface passed down to all operators
62 */
63export interface Options extends RawObject {
64 /** The key string that is used to lookup the ID value of a document. */
65 readonly idKey?: string;
66 /** The collation specification for string operations. */
67 readonly collation?: CollationSpec;
68 /** Processing mode that determines how to treat inputs and outputs. */
69 readonly processingMode?: ProcessingMode;
70 /** Hash function to replace the somewhat weaker default implementation. */
71 readonly hashFunction?: HashFunction;
72 /** Function to resolve collections from an external source. */
73 readonly collectionResolver?: CollectionResolver;
74 /** JSON schema validator to use with the $jsonSchema operator. Required to use the operator. */
75 readonly jsonSchemaValidator?: JsonSchemaValidator;
76}
77interface ComputeOptions extends Options {
78 readonly root?: RawObject;
79}
80/**
81 * Creates an Option from another required keys are initialized
82 * @param options Options
83 */
84export declare function makeOptions(options?: Options): Options;
85/**
86 * The different groups of operators
87 */
88export declare enum OperatorType {
89 ACCUMULATOR = "accumulator",
90 EXPRESSION = "expression",
91 PIPELINE = "pipeline",
92 PROJECTION = "projection",
93 QUERY = "query"
94}
95export declare type AccumulatorOperator = (collection: Collection, expr: AnyVal, options?: Options) => AnyVal;
96export declare type ExpressionOperator = (obj: RawObject, expr: AnyVal, options?: Options) => AnyVal;
97export declare type PipelineOperator = (collection: Iterator, expr: AnyVal, options?: Options) => Iterator;
98export declare type ProjectionOperator = (obj: RawObject, expr: AnyVal, field: string, options?: Options) => AnyVal;
99export declare type QueryOperator = (selector: string, value: AnyVal, options?: Options) => (obj: RawObject) => boolean;
100/** Map of operator functions */
101export declare type OperatorMap = Record<string, AccumulatorOperator | ExpressionOperator | PipelineOperator | ProjectionOperator | QueryOperator>;
102/** Spec for Query and Project operators where the value of the selector is resolved. */
103declare type SelectorOperator<R> = (selector: string, lhs: AnyVal, rhs: AnyVal, options?: Options) => R;
104export declare type AddOperatorsMap = Record<string, AccumulatorOperator | ExpressionOperator | PipelineOperator | SelectorOperator<AnyVal>>;
105/**
106 * Register fully specified operators for the given operator class.
107 *
108 * @param cls Category of the operator
109 * @param operators Name of operator
110 */
111export declare function useOperators(cls: OperatorType, operators: OperatorMap): void;
112/**
113 * Returns the operator function or null if it is not found
114 * @param cls Category of the operator
115 * @param operator Name of the operator
116 */
117export declare function getOperator(cls: OperatorType, operator: string): Callback<AnyVal> | null;
118/** Context used for creating new operators */
119export interface OperatorContext {
120 readonly computeValue: typeof computeValue;
121 readonly resolve: typeof resolve;
122}
123/**
124 * Add new operators
125 *
126 * @param operatorType the operator class to extend
127 * @param operatorFactory a callback that accepts internal object state and returns an object of new operators.
128 */
129export declare function addOperators(operatorType: OperatorType, operatorFactory: (context: OperatorContext) => AddOperatorsMap): void;
130/**
131 * Computes the value of the expression on the object for the given operator
132 *
133 * @param obj the current object from the collection
134 * @param expr the expression for the given field
135 * @param operator the operator to resolve the field with
136 * @param options {Object} extra options
137 * @returns {*}
138 */
139export declare function computeValue(obj: AnyVal, expr: AnyVal, operator: string, options?: ComputeOptions): AnyVal;
140/**
141 * Redact an object
142 * @param {Object} obj The object to redact
143 * @param {*} expr The redact expression
144 * @param {*} options Options for value
145 * @return {*} returns the result of the redacted object
146 */
147export declare function redact(obj: RawObject, expr: AnyVal, options: ComputeOptions): AnyVal;
148export {};