@drip_sync/drip
Version:
Scalable incremental sync for MongoDB aggregation pipelines
27 lines (26 loc) • 780 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.invertPipeline = invertPipeline;
function invertStage(stage) {
if (stage.type === "match") {
return {
type: "match",
filter: { $nor: [stage.filter] },
};
}
return stage;
}
function invertPipeline(pipeline) {
const numMatches = pipeline.filter((s) => s.type === "match").length;
if (numMatches === 0) {
return [];
}
if (numMatches === 1) {
return [pipeline.map(invertStage)];
}
// Inverting a pipeline with multiple $match stages requires
// it to be split up into multiple pipelines, which we don't
// bother doing, so we return a single sub-pipeline that
// matches all.
return [[]];
}