From 2342ecbe37729de810a3d127aa3e5cfc270b56a7 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 23 Dec 2019 16:52:27 -0500 Subject: [PATCH] restores custom paginate and populate scripts --- demo/collections/Category.js | 8 +++++++ package.json | 3 ++- src/mongoose/autopopulate.js | 31 +++++++++++++-------------- src/mongoose/requestHandlers/query.js | 9 ++++---- 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/demo/collections/Category.js b/demo/collections/Category.js index 792ecc7a1e..562444da3c 100644 --- a/demo/collections/Category.js +++ b/demo/collections/Category.js @@ -37,6 +37,14 @@ module.exports = { required: true, localized: true, }, + { + name: 'page', + label: 'Page', + type: 'relationship', + relationTo: 'pages', + localized: false, + hasMany: false, + }, ], timestamps: true, }; diff --git a/package.json b/package.json index 2a489b6ae5..96e1d95239 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "body-parser": "^1.19.0", "connect-history-api-fallback": "^1.6.0", "dotenv": "^6.0.0", + "express": "^4.17.1", "express-fileupload": "^1.1.5", "express-graphql": "^0.7.1", "express-validation": "^1.0.3", @@ -40,11 +41,11 @@ "joi": "^14.3.1", "jsonwebtoken": "^8.5.1", "merge-graphql-schemas": "^1.7.0", - "express": "^4.17.1", "method-override": "^3.0.0", "mkdirp": "^0.5.1", "mongoose": "^5.7.1", "mongoose-autopopulate": "^0.9.1", + "mongoose-paginate": "^5.0.3", "nodemailer": "^6.3.0", "passport": "^0.4.0", "passport-jwt": "^4.0.0", diff --git a/src/mongoose/autopopulate.js b/src/mongoose/autopopulate.js index abb125d8bf..1463d58c42 100644 --- a/src/mongoose/autopopulate.js +++ b/src/mongoose/autopopulate.js @@ -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) { diff --git a/src/mongoose/requestHandlers/query.js b/src/mongoose/requestHandlers/query.js index 68b262ae16..979565de39 100644 --- a/src/mongoose/requestHandlers/query.js +++ b/src/mongoose/requestHandlers/query.js @@ -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;