mingo
Version:
MongoDB query language for in-memory objects
220 lines (219 loc) • 8.96 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var project_exports = {};
__export(project_exports, {
$project: () => $project
});
module.exports = __toCommonJS(project_exports);
var import_internal = require("../../core/_internal");
var import_internal2 = require("../../util/_internal");
const $project = (collection, expr, options) => {
if ((0, import_internal2.isEmpty)(expr)) return collection;
checkExpression(expr, options);
return collection.map(createHandler(expr, import_internal.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 ((0, import_internal2.isNumber)(subExpr) || (0, import_internal2.isBoolean)(subExpr)) {
if (subExpr) {
if (isRoot && key.endsWith(".$")) {
const condition = options?.local?.condition;
(0, import_internal2.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 ((0, import_internal2.isArray)(subExpr)) {
handlers[key] = (o) => subExpr.map(
(v) => (0, import_internal.computeValue)(o, v, null, options.update({ root: o })) ?? null
);
} else if ((0, import_internal2.isObject)(subExpr)) {
const subExprKeys = Object.keys(subExpr);
const operator = subExprKeys.length == 1 ? subExprKeys[0] : "";
const projectFn = options.context.getOperator(
import_internal.OpType.PROJECTION,
operator
);
if (projectFn) {
const foundSlice = operator === "$slice";
if (foundSlice && !(0, import_internal2.ensureArray)(subExpr[operator]).every(import_internal2.isNumber)) {
handlers[key] = (o) => (0, import_internal.computeValue)(o, subExpr, key, options.update({ root: o }));
} else {
handlers[key] = (o) => projectFn(o, subExpr[operator], key, options.update({ root: o }));
}
} else if ((0, import_internal2.isOperator)(operator)) {
handlers[key] = (o) => (0, import_internal.computeValue)(o, subExpr[operator], operator, options);
} else {
checkExpression(subExpr, options);
(0, import_internal2.assert)(subExprKeys.length > 0, `Invalid empty sub-projection: ${key}`);
handlers[key] = (o) => {
if (isRoot) options.update({ root: o });
const target = (0, import_internal2.resolve)(o, key);
const fn = createHandler(subExpr, options, false);
if ((0, import_internal2.isArray)(target)) return target.map(fn);
if ((0, import_internal2.isObject)(target)) return fn(target);
const res = fn(o);
if ((0, import_internal2.has)(o, key)) return res;
return !(0, import_internal2.isObject)(res) || Object.keys(res).length ? res : void 0;
};
}
} else {
handlers[key] = (0, import_internal2.isString)(subExpr) && subExpr[0] === "$" ? (o) => (0, import_internal.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 = (0, import_internal2.resolveGraph)(o, k, opts) ?? {};
(0, import_internal2.merge)(newObj, pathObj);
if ((0, import_internal2.has)(positional, k)) {
positional[k](newObj);
}
}
if (includedKeys.length) (0, import_internal2.filterMissing)(newObj);
for (const k of handlerKeys) {
const value = handlers[k](o);
if (value === void 0) {
(0, import_internal2.removeValue)(newObj, k, { descendArray: true });
} else {
(0, import_internal2.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) {
(0, import_internal2.removeValue)(newObj, k, { descendArray: true });
}
if (idKeyImplicit && (0, import_internal2.has)(o, idKey)) {
newObj[idKey] = (0, import_internal2.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)) {
(0, import_internal2.assert)(!k.startsWith("$"), "Field names may not start with '$'.");
if (k.endsWith(".$")) {
(0, import_internal2.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;
}
(0, import_internal2.assert)(
!(exclusions && inclusions),
"Projection cannot have a mix of inclusion and exclusion."
);
}
}
const findMatches = (o, key, leaf, pred) => {
let arr = (0, import_internal2.resolve)(o, key);
if (!(0, import_internal2.isArray)(arr)) arr = (0, import_internal2.resolve)(arr, leaf);
(0, import_internal2.assert)((0, import_internal2.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((0, import_internal2.normalize)(val)).pop();
const fn = options.context.getOperator(
import_internal.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 ((0, import_internal2.isOperator)(key)) {
(0, import_internal2.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((0, import_internal2.unique)(orMatches));
}
const i = (0, import_internal2.intersection)(matches).sort()[0];
let first = (0, import_internal2.resolve)(o, field)[i];
if (parent != leaf && !(0, import_internal2.isObject)(first)) {
first = { [leaf]: first };
}
(0, import_internal2.setValue)(o, parent, [first]);
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
$project
});