Functioning locale

This commit is contained in:
Elliot DeNolf
2019-03-16 14:00:41 -04:00
parent 436e95550a
commit 75b20ad907
3 changed files with 33 additions and 24 deletions

View File

@@ -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);
}

View File

@@ -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;