@joktec/mongo
Version:
JokTec - Mongo Service
111 lines • 4.38 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransformPlugin = void 0;
const utils_1 = require("@joktec/utils");
const dot_object_1 = __importDefault(require("dot-object"));
const lodash_1 = require("lodash");
const helpers_1 = require("../helpers");
function combinePopulateMatch(populates, virtualMatch) {
return (0, utils_1.toArray)(populates).map(populate => {
if (typeof populate === 'string') {
populate = { path: populate, match: {} };
}
populate.match = Object.assign({}, populate.match, virtualMatch);
return populate;
});
}
function cleanUpDocument(doc, paths) {
const dotDoc = dot_object_1.default.dot(doc);
for (const key in dotDoc) {
if (!paths.includes(key)) {
delete doc[key];
}
}
}
const TransformPlugin = (schema) => {
schema.pre('save', function (next) {
['_id', '__v', 'createdAt', 'updatedAt', '__t'].map(path => {
if (this[path])
delete this[path];
});
next();
});
schema.pre([
'find',
'findOne',
'findOneAndUpdate',
'countDocuments',
'estimatedDocumentCount',
'updateMany',
'updateOne',
'deleteOne',
'findOneAndDelete',
'deleteMany',
], { document: false, query: true }, function (next) {
if (this.getOptions()) {
const options = this.getOptions();
if (options.sort)
options.sort = helpers_1.MongoHelper.parseSort(options.sort);
if (options.projection)
options.projection = helpers_1.MongoHelper.parseProjection(options.projection);
this.setOptions(options);
}
if (this.getFilter()) {
const newFilter = helpers_1.MongoHelper.parseFilter(this.getFilter());
this.setQuery(newFilter);
}
if (this.getUpdate()) {
const omitKeys = ['_id', '__v', 'createdAt', 'updatedAt', '__t'];
const newUpdate = helpers_1.MongoHelper.flatten(this.getUpdate(), omitKeys);
this.setUpdate(newUpdate);
}
const populatedPaths = this.getPopulatedPaths();
if (populatedPaths.length) {
populatedPaths.forEach(path => {
const virtual = this.model.schema.virtuals[path];
const virtualMatch = Object.assign({}, (0, lodash_1.get)(virtual, 'options.match'), (0, lodash_1.get)(virtual, 'options.options.match'));
const populateOptions = this.mongooseOptions().populate[path];
const populates = combinePopulateMatch(populateOptions, virtualMatch);
this.populate(populates);
});
}
next();
});
schema.pre('aggregate', function (next, opts) {
const version = opts?.version || this.options?.version || '5.0.0';
const mongoVersion = version.split('.').map(Number);
const pipelines = [];
while (this.pipeline().length)
pipelines.push(this.pipeline().shift());
pipelines.map(pipeline => {
if ('$lookup' in pipeline) {
if (mongoVersion[0] < 5) {
const lookupStage = pipeline['$lookup'];
if (lookupStage.localField && lookupStage.foreignField) {
pipeline = {
$lookup: {
from: lookupStage.from,
let: { localFieldVar: `$${lookupStage.localField}` },
pipeline: [{ $match: { $expr: { $eq: [`$${lookupStage.foreignField}`, '$$localFieldVar'] } } }],
as: lookupStage.as,
},
};
}
}
if (!pipeline.$lookup.pipeline?.length) {
delete pipeline.$lookup.pipeline;
}
}
this.pipeline().push(pipeline);
});
next();
});
schema.post(/^find/, function (res, next) {
next();
});
};
exports.TransformPlugin = TransformPlugin;
//# sourceMappingURL=transform.plugin.js.map