Support special operators on intl string fields
This commit is contained in:
@@ -5,14 +5,11 @@ export default function buildQuery(schema) {
|
||||
schema.statics.apiQuery = function (rawParams, locale, cb) {
|
||||
const model = this;
|
||||
const params = paramParser(this, rawParams, locale);
|
||||
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);
|
||||
|
||||
@@ -40,7 +37,7 @@ function paramParser(model, rawParams, locale) {
|
||||
query = parseParam(key, rawParams[key], model, query, locale);
|
||||
} else {
|
||||
for (let i = 0; i < separatedParams.length; ++i) {
|
||||
query = parseParam(key, separatedParams[i], model, query);
|
||||
query = parseParam(key, separatedParams[i], model, query, locale);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -105,7 +102,7 @@ function parseParam(key, val, model, query, locale) {
|
||||
|
||||
function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, locale) {
|
||||
let paramType;
|
||||
const key = keyPrefix + lcKey;
|
||||
let key = keyPrefix + lcKey;
|
||||
|
||||
let matches = lcKey.match(/(.+)\.(.+)/);
|
||||
if (matches) {
|
||||
@@ -119,7 +116,8 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, local
|
||||
}
|
||||
} else if (typeof schema === 'object' ) {
|
||||
if (schema.obj[lcKey].intl) {
|
||||
query = addSearchParam(query, `${key}.${locale}`, val);
|
||||
key = `${key}.${locale}`;
|
||||
paramType = 'String'
|
||||
}
|
||||
} else if (typeof schema === 'undefined') {
|
||||
paramType = 'String';
|
||||
|
||||
@@ -1,14 +1,10 @@
|
||||
import httpStatus from 'http-status';
|
||||
|
||||
const query = (req, res) => {
|
||||
console.log('inside query');
|
||||
|
||||
req.model.apiQuery(req.query, req.locale, (err, result) => {
|
||||
if (err) {
|
||||
console.log('api query error', err);
|
||||
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
|
||||
}
|
||||
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) {
|
||||
@@ -17,7 +13,6 @@ const query = (req, res) => {
|
||||
|
||||
return doc.toJSON({ virtuals: true })
|
||||
})
|
||||
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user