@drip_sync/drip
Version:
Scalable incremental sync for MongoDB aggregation pipelines
36 lines (35 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.scopePathExpression = scopePathExpression;
const assert_1 = require("assert");
const invalid_expression_1 = require("./invalid_expression");
function scopePathExpression(e, path,
// A set of user-defined variable names to allow
vars) {
(0, assert_1.strict)(e.startsWith("$"));
const epath = e.split(".");
if (epath[0].startsWith("$$")) {
// Variable - see https://www.mongodb.com/docs/manual/reference/aggregation-variables/
const varName = epath[0].substring(2);
if (vars[varName]) {
// No need to scope user-defined variables
return e;
}
switch (varName) {
case "REMOVE":
case "DESCEND":
case "PRUNE":
case "KEEP":
// No need to scope
return e;
case "CURRENT":
case "ROOT":
// Need to scope
return [epath[0], path, ...epath.slice(1)].join(".");
default:
throw new invalid_expression_1.InvalidExpression(`Use of undefined variable: ${varName}`);
}
}
// Shorthand notation - scope accordingly
return ["$" + path, epath[0].substring(1), ...epath.slice(1)].join(".");
}