UNPKG

mingo

Version:

MongoDB query language for in-memory objects

239 lines (238 loc) 7.05 kB
import { assert, has, isArray, isNil, isObject, isOperator, isString, resolve } from "../util"; var ProcessingMode = /* @__PURE__ */ ((ProcessingMode2) => { ProcessingMode2[ProcessingMode2["CLONE_OFF"] = 0] = "CLONE_OFF"; ProcessingMode2[ProcessingMode2["CLONE_INPUT"] = 1] = "CLONE_INPUT"; ProcessingMode2[ProcessingMode2["CLONE_OUTPUT"] = 2] = "CLONE_OUTPUT"; ProcessingMode2[ProcessingMode2["CLONE_ALL"] = 3] = "CLONE_ALL"; return ProcessingMode2; })(ProcessingMode || {}); class ComputeOptions { constructor(options, locals) { this.options = options; this.#locals = locals ? { ...locals } : {}; } #locals; /** * Initializes a new instance of the `ComputeOptions` class with the provided options. * * @param options - A partial set of options to configure the `ComputeOptions` instance. * If an instance of `ComputeOptions` is provided, its internal options and locals are used. * @returns A new `ComputeOptions` instance configured with the provided options and root. */ static init(options) { return options instanceof ComputeOptions ? new ComputeOptions(options.options, options.#locals) : new ComputeOptions({ idKey: "_id", scriptEnabled: true, useStrictMode: true, processingMode: 0 /* CLONE_OFF */, ...options, context: options?.context ? Context.from(options?.context) : Context.init() }); } update(locals) { Object.assign(this.#locals, locals, { // DO NOT override timestamp timestamp: this.#locals.timestamp, // merge variables. variables: { ...this.#locals?.variables, ...locals?.variables } }); return this; } get local() { return this.#locals; } get now() { if (!this.#locals?.timestamp) Object.assign(this.#locals, { timestamp: Date.now() }); return new Date(this.#locals.timestamp); } get idKey() { return this.options.idKey; } get collation() { return this.options?.collation; } get processingMode() { return this.options?.processingMode; } get useStrictMode() { return this.options?.useStrictMode; } get scriptEnabled() { return this.options?.scriptEnabled; } get collectionResolver() { return this.options?.collectionResolver; } get jsonSchemaValidator() { return this.options?.jsonSchemaValidator; } get variables() { return this.options?.variables; } get context() { return this.options?.context; } } var OpType = /* @__PURE__ */ ((OpType2) => { OpType2["ACCUMULATOR"] = "accumulator"; OpType2["EXPRESSION"] = "expression"; OpType2["PIPELINE"] = "pipeline"; OpType2["PROJECTION"] = "projection"; OpType2["QUERY"] = "query"; OpType2["WINDOW"] = "window"; return OpType2; })(OpType || {}); class Context { #operators = new Map( Object.values(OpType).map((k) => [k, {}]) ); constructor() { } static init(ops = {}) { const ctx = new Context(); for (const [type, operators] of Object.entries(ops)) { if (ctx.#operators.has(type) && operators) { ctx.addOps(type, operators); } } return ctx; } static from(ctx) { return Context.init(ctx && Object.fromEntries(ctx.#operators) || {}); } static merge(first, second) { const ctx = Context.from(first); for (const type of Object.values(OpType)) { ctx.addOps(type, second.#operators.get(type)); } return ctx; } addOps(type, operators) { this.#operators.set( type, Object.assign({}, operators, this.#operators.get(type)) ); return this; } getOperator(type, name) { return this.#operators.get(type)[name] ?? null; } addAccumulatorOps(ops) { return this.addOps("accumulator" /* ACCUMULATOR */, ops); } addExpressionOps(ops) { return this.addOps("expression" /* EXPRESSION */, ops); } addQueryOps(ops) { return this.addOps("query" /* QUERY */, ops); } addPipelineOps(ops) { return this.addOps("pipeline" /* PIPELINE */, ops); } addProjectionOps(ops) { return this.addOps("projection" /* PROJECTION */, ops); } addWindowOps(ops) { return this.addOps("window" /* WINDOW */, ops); } } function computeValue(obj, expr, operator, options) { const copts = !(options instanceof ComputeOptions) || isNil(options.local.root) ? ComputeOptions.init(options).update({ root: obj }) : options; return isOperator(operator) ? computeOperator(obj, expr, operator, copts) : computeExpression(obj, expr, copts); } const SYSTEM_VARS = ["$$ROOT", "$$CURRENT", "$$REMOVE", "$$NOW"]; function computeExpression(obj, expr, options) { if (isString(expr) && expr.length > 0 && expr[0] === "$") { if (expr === "$$KEEP" || expr === "$$PRUNE" || expr === "$$DESCEND") return expr; let ctx = options.local.root; const arr = expr.split("."); if (SYSTEM_VARS.includes(arr[0])) { switch (arr[0]) { case "$$ROOT": break; case "$$CURRENT": ctx = obj; break; case "$$REMOVE": ctx = void 0; break; case "$$NOW": ctx = new Date(options.now); break; } expr = expr.slice(arr[0].length + 1); } else if (arr[0].slice(0, 2) === "$$") { ctx = Object.assign( {}, // global vars options.variables, // current item is added before local variables because the binding may be changed. { this: obj }, // local vars options?.local?.variables ); const name = arr[0].slice(2); assert(has(ctx, name), `Use of undefined variable: ${name}`); expr = expr.slice(2); } else { expr = expr.slice(1); } return expr === "" ? ctx : resolve(ctx, expr); } if (isArray(expr)) { return expr.map((item) => computeExpression(obj, item, options)); } if (isObject(expr)) { const result = {}; const elems = Object.entries(expr); for (const [key, val] of elems) { if (isOperator(key)) { assert( elems.length === 1, `Expression must contain a single operator. got [${Object.keys(expr).join(",")}]` ); return computeOperator(obj, val, key, options); } result[key] = computeExpression(obj, val, options); } return result; } return expr; } function computeOperator(obj, expr, operator, options) { const context = options.context; const callExpression = context.getOperator( "expression" /* EXPRESSION */, operator ); if (callExpression) return callExpression(obj, expr, options); const callAccumulator = context.getOperator( "accumulator" /* ACCUMULATOR */, operator ); assert(!!callAccumulator, `accumulator '${operator}' is not registered.`); if (!isArray(obj)) { obj = computeExpression(obj, expr, options); expr = null; } assert(isArray(obj), `arguments must resolve to array for ${operator}.`); return callAccumulator(obj, expr, options); } export { ComputeOptions, Context, OpType, ProcessingMode, computeValue };