popurelate
Version:
Easily populate and filter documents using data from related collections.
90 lines (89 loc) • 3.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPipelineType = exports.defaultPipelineIs = exports.PipeLineIsHelper = void 0;
class PipeLineIsHelper {
constructor() {
this.limit = (pipeline) => pipeline.limit !== undefined;
this.skip = (pipeline) => pipeline.skip !== undefined;
this.sort = (pipeline) => pipeline.sort !== undefined;
this.project = (pipeline) => pipeline.project !== undefined;
this.addFields = (pipeline) => pipeline.addFields !== undefined;
this.query = (pipeline) => pipeline.query !== undefined;
this.rawPipeline = (pipeline) => pipeline.rawPipeline !== undefined;
this.populate = (pipeline) => pipeline.populate !== undefined;
this.count = (pipeline) => pipeline.count === true;
this.withCount = (pipeline) => pipeline.withCount === true;
this.countLike = (pipeline) => this.count(pipeline) || this.withCount(pipeline);
this.singleRequiredPopulation = (pipeline) => {
if (!this.populate(pipeline))
return false;
if (pipeline.populate.required && !pipeline.populate.matchesMany) {
return true;
}
if (pipeline.populate.children) {
for (const key in pipeline.populate.children) {
const child = pipeline.populate.children[key];
if (this.singleRequiredPopulation(child))
return true;
}
}
return false;
};
this.changingDocCount = (pipeline) => {
if (this.query(pipeline) ||
this.limit(pipeline) ||
this.skip(pipeline) ||
this.countLike(pipeline) ||
this.singleRequiredPopulation(pipeline) ||
(this.rawPipeline(pipeline) && this.rawPipelineChangingDocCount(pipeline))) {
return true;
}
return false;
};
this.changingDocOrder = (pipeline) => this.sort(pipeline) ||
(this.rawPipeline(pipeline) && this.rawPipelineChangingDocOrder(pipeline));
this.changingCountOrOrder = (pipeline) => this.changingDocOrder(pipeline) || this.changingDocCount(pipeline);
this.orderAndCountImportant = (pipeline) => {
return (this.limit(pipeline) ||
this.skip(pipeline) ||
this.countLike(pipeline) ||
(this.rawPipeline(pipeline) &&
this.orderAndCountImportantForRawPipeline(pipeline)));
};
this.orderAndCountImportantForRawPipeline = (pipeline) => {
return true;
};
this.rawPipelineChangingDocOrder = (pipeline) => {
return true;
};
this.rawPipelineChangingDocCount = (pipeline) => {
return true;
};
}
}
exports.PipeLineIsHelper = PipeLineIsHelper;
exports.defaultPipelineIs = new PipeLineIsHelper();
const getPipelineType = (pipeline, is = exports.defaultPipelineIs) => {
if (is.query(pipeline))
return "query";
if (is.project(pipeline))
return "project";
if (is.addFields(pipeline))
return "addFields";
if (is.sort(pipeline))
return "sort";
if (is.populate(pipeline))
return "populate";
if (is.limit(pipeline))
return "limit";
if (is.skip(pipeline))
return "skip";
if (is.withCount(pipeline))
return "withCount";
if (is.count(pipeline))
return "count";
if (is.rawPipeline(pipeline))
return "rawPipeline";
return null;
};
exports.getPipelineType = getPipelineType;