UNPKG

mingo

Version:

MongoDB query language for in-memory objects

217 lines (216 loc) 7.17 kB
import { ComputeOptions, computeValue, OpType } from "../../core/_internal"; import { assert, ensureArray, filterMissing, has, intersection, isArray, isBoolean, isEmpty, isNumber, isObject, isOperator, isString, merge, normalize, removeValue, resolve, resolveGraph, setValue, unique } from "../../util/_internal"; const $project = (collection, expr, options) => { if (isEmpty(expr)) return collection; checkExpression(expr, options); return collection.map(createHandler(expr, ComputeOptions.init(options))); }; function createHandler(expr, options, isRoot = true) { const idKey = options.idKey; const expressionKeys = Object.keys(expr); const excludedKeys = new Array(); const includedKeys = new Array(); const handlers = {}; const positional = {}; for (const key of expressionKeys) { const subExpr = expr[key]; if (isNumber(subExpr) || isBoolean(subExpr)) { if (subExpr) { if (isRoot && key.endsWith(".$")) { const condition = options?.local?.condition; assert( condition, "positional operator '.$' couldn't find matching element in the array." ); const field = key.slice(0, -2); positional[field] = getPositionalFilter(field, condition, options); includedKeys.push(field); } else { includedKeys.push(key); } } else { excludedKeys.push(key); } } else if (isArray(subExpr)) { handlers[key] = (o) => subExpr.map( (v) => computeValue(o, v, null, options.update({ root: o })) ?? null ); } else if (isObject(subExpr)) { const subExprKeys = Object.keys(subExpr); const operator = subExprKeys.length == 1 ? subExprKeys[0] : ""; const projectFn = options.context.getOperator( OpType.PROJECTION, operator ); if (projectFn) { const foundSlice = operator === "$slice"; if (foundSlice && !ensureArray(subExpr[operator]).every(isNumber)) { handlers[key] = (o) => computeValue(o, subExpr, key, options.update({ root: o })); } else { handlers[key] = (o) => projectFn(o, subExpr[operator], key, options.update({ root: o })); } } else if (isOperator(operator)) { handlers[key] = (o) => computeValue(o, subExpr[operator], operator, options); } else { checkExpression(subExpr, options); assert(subExprKeys.length > 0, `Invalid empty sub-projection: ${key}`); handlers[key] = (o) => { if (isRoot) options.update({ root: o }); const target = resolve(o, key); const fn = createHandler(subExpr, options, false); if (isArray(target)) return target.map(fn); if (isObject(target)) return fn(target); const res = fn(o); if (has(o, key)) return res; return !isObject(res) || Object.keys(res).length ? res : void 0; }; } } else { handlers[key] = isString(subExpr) && subExpr[0] === "$" ? (o) => computeValue(o, subExpr, key, options) : (_) => subExpr; } } const handlerKeys = Object.keys(handlers); const idKeyExcluded = excludedKeys.includes(idKey); const idKeyImplicit = isRoot && !idKeyExcluded && !includedKeys.includes(idKey); const opts = { preserveMissing: true }; return (o) => { const newObj = {}; for (const k of includedKeys) { const pathObj = resolveGraph(o, k, opts) ?? {}; merge(newObj, pathObj); if (has(positional, k)) { positional[k](newObj); } } if (includedKeys.length) filterMissing(newObj); for (const k of handlerKeys) { const value = handlers[k](o); if (value === void 0) { removeValue(newObj, k, { descendArray: true }); } else { setValue(newObj, k, value); } } if (excludedKeys.length === 1 && idKeyExcluded) { if (Object.keys(newObj).length === 0) Object.assign(newObj, o); } else if (excludedKeys.length) { Object.assign(newObj, { ...o, ...newObj }); } for (const k of excludedKeys) { removeValue(newObj, k, { descendArray: true }); } if (idKeyImplicit && has(o, idKey)) { newObj[idKey] = resolve(o, idKey); } return newObj; }; } function checkExpression(expr, options) { let exclusions = false; let inclusions = false; let positional = 0; for (const [k, v] of Object.entries(expr)) { assert(!k.startsWith("$"), "Field names may not start with '$'."); if (k.endsWith(".$")) { assert( ++positional < 2, "Cannot specify more than one positional projection per query." ); } if (k === options?.idKey) continue; if (v === 0 || v === false) { exclusions = true; } else if (v === 1 || v === true) { inclusions = true; } assert( !(exclusions && inclusions), "Projection cannot have a mix of inclusion and exclusion." ); } } const findMatches = (o, key, leaf, pred) => { let arr = resolve(o, key); if (!isArray(arr)) arr = resolve(arr, leaf); assert(isArray(arr), "must resolve to array"); const matches = []; arr.forEach((e, i) => pred({ [leaf]: [e] }) && matches.push(i)); return matches; }; const complement = (p) => (e) => !p(e); const COMPOUND_OPS = { $and: 1, $or: 1, $nor: 1 }; function getPositionalFilter(field, condition, options) { const stack = Object.entries(condition).slice(); const selectors = { $and: [], $or: [] }; for (let i = 0; i < stack.length; i++) { const [key, val, op] = stack[i]; if (key === field || key.startsWith(field + ".")) { const [operator, expr] = Object.entries(normalize(val)).pop(); const fn = options.context.getOperator( OpType.QUERY, operator ); const leaf2 = key.substring(key.lastIndexOf(".") + 1); const pred = fn(leaf2, expr, options); if (!op || op === "$and") { selectors["$and"].push([key, pred, leaf2]); } else if (op === "$nor") { selectors["$and"].push([key, complement(pred), leaf2]); } else if (op === "$or") { selectors["$or"].push([key, pred, leaf2]); } } else if (isOperator(key)) { assert(COMPOUND_OPS[key], `${key} is not allowed in this context`); for (const item of val) { Object.entries(item).forEach(([k, v]) => stack.push([k, v, key])); } } } const sep = field.lastIndexOf("."); const parent = field.substring(0, sep) || field; const leaf = field.substring(sep + 1); return (o) => { const matches = []; for (const [key, pred, leaf2] of selectors["$and"]) { matches.push(findMatches(o, key, leaf2, pred)); } if (selectors["$or"].length) { const orMatches = []; for (const [key, pred, leaf2] of selectors["$or"]) { orMatches.push(...findMatches(o, key, leaf2, pred)); } matches.push(unique(orMatches)); } const i = intersection(matches).sort()[0]; let first = resolve(o, field)[i]; if (parent != leaf && !isObject(first)) { first = { [leaf]: first }; } setValue(o, parent, [first]); }; } export { $project };