restores custom paginate and populate scripts

This commit is contained in:
James
2019-12-23 16:52:27 -05:00
parent 8f0426fbc8
commit 2342ecbe37
4 changed files with 29 additions and 22 deletions

View File

@@ -8,16 +8,16 @@ module.exports = function (schema) {
pathsToPopulate.push({
options: defaultOptions(pathname, schemaType.options),
autopopulate: option
autopopulate: option,
});
} else if (schemaType.options &&
schemaType.options.type &&
schemaType.options.type[0] &&
schemaType.options.type[0].autopopulate) {
} else if (schemaType.options
&& schemaType.options.type
&& schemaType.options.type[0]
&& schemaType.options.type[0].autopopulate) {
option = schemaType.options.type[0].autopopulate;
pathsToPopulate.push({
options: defaultOptions(pathname, schemaType.options.type[0]),
autopopulate: option
autopopulate: option,
});
}
});
@@ -34,12 +34,11 @@ module.exports = function (schema) {
}
function autopopulateHandler() {
if (this._mongooseOptions &&
this._mongooseOptions.lean &&
if (this._mongooseOptions
&& this._mongooseOptions.lean
// If lean and user didn't explicitly do `lean({ autopulate: true })`,
// skip it. See gh-27, gh-14, gh-48
!this._mongooseOptions.lean.autopopulate) {
&& !this._mongooseOptions.lean.autopopulate) {
return;
}
@@ -48,7 +47,7 @@ module.exports = function (schema) {
return;
}
let maxDepth = options.maxDepth;
let { maxDepth } = options;
if (options.autopopulate && options.autopopulate.maxDepth) {
maxDepth = options.autopopulate.maxDepth;
@@ -66,17 +65,17 @@ module.exports = function (schema) {
pathsToPopulate[i].options.options = pathsToPopulate[i].options.options || {};
Object.assign(pathsToPopulate[i].options.options, {
...options,
_depth: depth + 1
_depth: depth + 1,
});
processOption.call(this,
pathsToPopulate[i].autopopulate, pathsToPopulate[i].options);
}
}
schema.
pre('find', autopopulateHandler).
pre('findOne', autopopulateHandler).
pre('findOneAndUpdate', autopopulateHandler);
schema
.pre('find', autopopulateHandler)
.pre('findOne', autopopulateHandler)
.pre('findOneAndUpdate', autopopulateHandler);
};
function defaultOptions(pathname, v) {

View File

@@ -1,22 +1,21 @@
import httpStatus from 'http-status';
const query = (req, res) => {
req.model.paginate(req.model.apiQuery(req.query, req.locale), { ...req.query }, (err, result) => {
if (err) {
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
}
return res.json({
...result,
docs: result.docs.map(doc => {
docs: result.docs.map((doc) => {
if (req.locale && doc.setLocale) {
doc.setLocale(req.locale, req.query['fallback-locale']);
}
return doc.toJSON({ virtuals: true })
})
return doc.toJSON({ virtuals: true });
}),
});
})
});
};
export default query;