From 75b20ad90729326b89f322685170e9c83bbcb067 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Sat, 16 Mar 2019 14:00:41 -0400 Subject: [PATCH] Functioning locale --- demo/Page/Page.model.js | 2 +- src/plugins/buildQuery.js | 38 +++++++++++++++++++++--------------- src/requestHandlers/query.js | 17 +++++++++------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/demo/Page/Page.model.js b/demo/Page/Page.model.js index 601f2752ae..20ee15e980 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 fe995877a9..b0ab856ab0 100644 --- a/src/plugins/buildQuery.js +++ b/src/plugins/buildQuery.js @@ -2,18 +2,25 @@ export default function buildQuery(schema) { - schema.statics.apiQuery = function (rawParams) { + schema.statics.apiQuery = function (rawParams, cb) { const model = this; const params = paramParser(this, rawParams); + console.log('parsed params', params); // Create the Mongoose Query object. let query = model .find(params.searchParams); + console.log('after query find'); + if (params.sort) query = query.sort(params.sort); - return query; + if (cb) { + query.exec(cb); + } else { + return query; + } }; } @@ -76,17 +83,17 @@ function parseParam(key, val, model, query) { } else if (lcKey === 'include') { if (val.match(',')) { let orArray = []; - val.split(',').map(id => orArray.push({_id: id})); + val.split(',').map(id => orArray.push({ _id: id })); query = addSearchParam(query, '$or', orArray); } else query.searchParams['_id'] = val; } else if (lcKey === 'exclude') { if (val.match(',')) { let andArray = []; - val.split(',').map(id => andArray.push({_id: {$ne: id}})); + val.split(',').map(id => andArray.push({ _id: { $ne: id } })); query = addSearchParam(query, '$and', andArray); } else - query.searchParams['_id'] = {$ne: val}; + query.searchParams['_id'] = { $ne: val }; } else { query = parseSchemaForKey(model.schema, query, '', lcKey, val, operator); } @@ -103,8 +110,7 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator) { if (schema.paths[matches[1]].constructor.name === 'DocumentArray' || schema.paths[matches[1]].constructor.name === 'Mixed') { parseSchemaForKey(schema.paths[matches[1]].schema, `${matches[1]}.`, matches[2], val, operator) - } - else if (schema.paths[matches[1]].constructor.name === 'SchemaType'){ + } else if (schema.paths[matches[1]].constructor.name === 'SchemaType') { // This wasn't handled in the original package but seems to work paramType = schema.paths[matches[1]].schema.paths.name.instance; } @@ -129,13 +135,13 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator) { } else if (paramType === 'Number') { if (val.match(/([0-9]+,?)/) && val.match(',')) { if (operator === 'all') { - query = addSearchParam(query, key, {$all: val.split(',')}); + query = addSearchParam(query, key, { $all: val.split(',') }); } else if (operator === 'nin') { - query = addSearchParam(query, key, {$nin: val.split(',')}); + query = addSearchParam(query, key, { $nin: val.split(',') }); } else if (operator === 'mod') { - query = addSearchParam(query, key, {$mod: [val.split(',')[0], val.split(',')[1]]}); + query = addSearchParam(query, key, { $mod: [val.split(',')[0], val.split(',')[1]] }); } else { - query = addSearchParam(query, key, {$in: val.split(',')}); + query = addSearchParam(query, key, { $in: val.split(',') }); } } else if (val.match(/([0-9]+)/)) { if (operator === 'gt' || @@ -155,11 +161,11 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator) { const options = val.split(',').map(str => new RegExp(str, 'i')); if (operator === 'all') { - query = addSearchParam(query, key, {$all: options}); + query = addSearchParam(query, key, { $all: options }); } else if (operator === 'nin') { - query = addSearchParam(query, key, {$nin: options}); + query = addSearchParam(query, key, { $nin: options }); } else { - query = addSearchParam(query, key, {$in: options}); + query = addSearchParam(query, key, { $in: options }); } } else if (val.match(/([0-9]+)/)) { if (operator === 'gt' || @@ -174,9 +180,9 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator) { } } else if (operator === 'ne' || operator === 'not') { const neregex = new RegExp(val, 'i'); - query = addSearchParam(query, key, {'$not': neregex}); + query = addSearchParam(query, key, { '$not': neregex }); } else if (operator === 'like') { - query = addSearchParam(query, key, {$regex: val, $options: '-i'}); + query = addSearchParam(query, key, { $regex: val, $options: '-i' }); } else { query = addSearchParam(query, key, val); } diff --git a/src/requestHandlers/query.js b/src/requestHandlers/query.js index 0164a81507..193b02c38e 100644 --- a/src/requestHandlers/query.js +++ b/src/requestHandlers/query.js @@ -1,22 +1,25 @@ import httpStatus from 'http-status'; const query = (req, res) => { - req.model.paginate(req.model.apiQuery(req.query), req.query, (err, result) => { + console.log('inside query'); + + req.model.apiQuery(req.query, (err, result) => { if (err) { + console.log('api query error', err); return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err }); } - - return res.json({ - ...result, - docs: result.docs.map(doc => { + console.log('no error inside api query'); + return res.json( + result.map(doc => { //TODO: 'result.docs' will need to be used for the pagination plugin if (req.locale) { doc.setLocale(req.locale, req.query['fallback-locale']); } return doc.toJSON({ virtuals: true }) }) - }); - }) + + ); + }); }; export default query;