From 826bfafb386597e6cdf202f7d564e525834b0a76 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Sat, 16 Mar 2019 15:37:31 -0400 Subject: [PATCH] Properly integrate pagination plugin --- .idea/codeStyles/codeStyleConfig.xml | 5 +++ .idea/encodings.xml | 4 ++ .idea/inspectionProfiles/Project_Default.xml | 11 +++++ .idea/jsLibraryMappings.xml | 7 ++++ .idea/misc.xml | 9 +++++ .idea/modules.xml | 8 ++++ .idea/payload.iml | 16 ++++++++ .idea/vcs.xml | 6 +++ demo/Page/Page.model.js | 2 +- src/plugins/buildQuery.js | 19 ++++----- src/plugins/paginate.js | 42 ++++++++++---------- src/requestHandlers/query.js | 12 +++--- src/tests/troubleshoot.spec.js | 7 ++++ toggle_mongo.cmd | 8 ++++ 14 files changed, 117 insertions(+), 39 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/jsLibraryMappings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/payload.iml create mode 100644 .idea/vcs.xml create mode 100644 src/tests/troubleshoot.spec.js create mode 100644 toggle_mongo.cmd diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 000000000..a55e7a179 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 000000000..15a15b218 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 000000000..062dde0ea --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,11 @@ + + + + \ No newline at end of file diff --git a/.idea/jsLibraryMappings.xml b/.idea/jsLibraryMappings.xml new file mode 100644 index 000000000..5f2a5036f --- /dev/null +++ b/.idea/jsLibraryMappings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 000000000..92e28271b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 000000000..b5a69e057 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/payload.iml b/.idea/payload.iml new file mode 100644 index 000000000..1a2f4465a --- /dev/null +++ b/.idea/payload.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 000000000..94a25f7f4 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/demo/Page/Page.model.js b/demo/Page/Page.model.js index 20ee15e98..601f2752a 100644 --- a/demo/Page/Page.model.js +++ b/demo/Page/Page.model.js @@ -16,7 +16,7 @@ const PageSchema = new mongoose.Schema({ { timestamps: true } ); -// PageSchema.plugin(paginate); +PageSchema.plugin(paginate); PageSchema.plugin(buildQuery); PageSchema.plugin(internationalization, payloadConfig.localization); diff --git a/src/plugins/buildQuery.js b/src/plugins/buildQuery.js index 386477853..b8c2cefd9 100644 --- a/src/plugins/buildQuery.js +++ b/src/plugins/buildQuery.js @@ -6,18 +6,14 @@ export default function buildQuery(schema) { const model = this; const params = paramParser(this, rawParams, locale); - // Create the Mongoose Query object. - let query = model - .find(params.searchParams); - - if (params.sort) - query = query.sort(params.sort); if (cb) { - query.exec(cb); - } else { - return query; + model + .find(params.searchParams) + .exec(cb); } + + return params.searchParams; }; } @@ -93,8 +89,7 @@ function parseParam(key, val, model, query, locale) { query.searchParams['_id'] = { $ne: val }; } else if (lcKey === 'locale') { // Do nothing - } - else { + } else { query = parseSchemaForKey(model.schema, query, '', lcKey, val, operator, locale); } return query; @@ -114,7 +109,7 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, local // This wasn't handled in the original package but seems to work paramType = schema.paths[matches[1]].schema.paths.name.instance; } - } else if (typeof schema === 'object' ) { + } else if (typeof schema === 'object') { if (schema.obj[lcKey].intl) { key = `${key}.${locale}`; paramType = 'String' diff --git a/src/plugins/paginate.js b/src/plugins/paginate.js index bb4a3deb9..9b3645378 100644 --- a/src/plugins/paginate.js +++ b/src/plugins/paginate.js @@ -22,28 +22,28 @@ function paginate(query, options, callback) { options = Object.assign({}, paginate.options, options); options.customLabels = options.customLabels ? options.customLabels : {}; - var defaultLimit = 10; + let defaultLimit = 10; - var select = options.select; - var sort = options.sort; - var collation = options.collation || {}; - var populate = options.populate; - var lean = options.lean || false; - var leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true; - var limit = options.hasOwnProperty('limit') ? parseInt(options.limit) : defaultLimit; - var skip; - var offset; - var page; + let select = options.select; + let sort = options.sort; + let collation = options.collation || {}; + let populate = options.populate; + let lean = options.lean || false; + let leanWithId = options.hasOwnProperty('leanWithId') ? options.leanWithId : true; + let limit = options.hasOwnProperty('limit') ? parseInt(options.limit) : defaultLimit; + let skip; + let offset; + let page; // Custom Labels - var labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs'; - var labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit'; - var labelPage = options.customLabels.page ? options.customLabels.page : 'page'; - var labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages'; - var labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs'; - var labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage'; - var labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage'; - var labelPagingCounter = options.customLabels.pagingCounter ? options.customLabels.pagingCounter : 'pagingCounter'; + let labelTotal = options.customLabels.totalDocs ? options.customLabels.totalDocs : 'totalDocs'; + let labelLimit = options.customLabels.limit ? options.customLabels.limit : 'limit'; + let labelPage = options.customLabels.page ? options.customLabels.page : 'page'; + let labelTotalPages = options.customLabels.totalPages ? options.customLabels.totalPages : 'totalPages'; + let labelDocs = options.customLabels.docs ? options.customLabels.docs : 'docs'; + let labelNextPage = options.customLabels.nextPage ? options.customLabels.nextPage : 'nextPage'; + let labelPrevPage = options.customLabels.prevPage ? options.customLabels.prevPage : 'prevPage'; + let labelPagingCounter = options.customLabels.pagingCounter ? options.customLabels.pagingCounter : 'pagingCounter'; if (options.hasOwnProperty('offset')) { offset = parseInt(options.offset); @@ -78,7 +78,7 @@ function paginate(query, options, callback) { model.populate(populate); } - var docs = model.exec(); + let docs = model.exec(); if (lean && leanWithId) { docs = docs.then(function (docs) { @@ -92,7 +92,7 @@ function paginate(query, options, callback) { return Promise.all([count, docs]) .then(function (values) { - var result = { + let result = { [labelDocs]: values[1], [labelTotal]: values[0], [labelLimit]: limit diff --git a/src/requestHandlers/query.js b/src/requestHandlers/query.js index 3ce45e5aa..e682203cf 100644 --- a/src/requestHandlers/query.js +++ b/src/requestHandlers/query.js @@ -1,20 +1,22 @@ import httpStatus from 'http-status'; const query = (req, res) => { - req.model.apiQuery(req.query, req.locale, (err, result) => { + + 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.map(doc => { //TODO: 'result.docs' will need to be used for the pagination plugin + return res.json({ + ...result, + docs: result.docs.map(doc => { if (req.locale) { doc.setLocale(req.locale, req.query['fallback-locale']); } return doc.toJSON({ virtuals: true }) }) - ); - }); + }); + }) }; export default query; diff --git a/src/tests/troubleshoot.spec.js b/src/tests/troubleshoot.spec.js new file mode 100644 index 000000000..a64c8f41f --- /dev/null +++ b/src/tests/troubleshoot.spec.js @@ -0,0 +1,7 @@ +describe('troubleshooting', () => { + describe('plugins', () => { + it('should return all records', () => { + + }); + }); +}); diff --git a/toggle_mongo.cmd b/toggle_mongo.cmd new file mode 100644 index 000000000..aabf2c441 --- /dev/null +++ b/toggle_mongo.cmd @@ -0,0 +1,8 @@ +@ECHO OFF + +sc query "MongoDB" | findstr "RUNNING" +if errorlevel 1 ( + net start "MongoDB Server" +) else ( + net stop "MongoDB Server" +)