fix 1st level deep filter

This commit is contained in:
Kamal Bennani 2018-11-19 00:10:48 +01:00
parent 85eeb4aef2
commit 511ca55461
No known key found for this signature in database
GPG Key ID: 4513063CDB1A1C25
6 changed files with 28 additions and 12 deletions

View File

@ -37,6 +37,16 @@ module.exports = {
generateMatchStage: function (qb) {
return (strapiModel, filters) => {
if (!filters) {
return undefined;
}
// 1st level deep filter
if (filters.where) {
this.generateMatchStage(qb)(strapiModel, { relations: filters.where });
}
// 2nd+ level deep filter
_.forEach(filters.relations, (value, key) => {
if (key !== 'relations') {
const association = strapiModel.associations.find(a => a.alias === key);

View File

@ -101,6 +101,18 @@ module.exports = {
let acc = [];
// 1st level deep filter
if (filters.where) {
acc.push(
...generateMatchStage(
strapiModel,
{ relations: filters.where },
{ prefixPath }
)
);
}
// 2nd+ level deep filter
_.forEach(filters.relations, (value, key) => {
if (key !== 'relations') {
const nextPrefixedPath = `${prefixPath}${key}.`;

View File

@ -32,7 +32,7 @@ module.exports = {
if (_.isPlainObject(value)) {
const flatObject = this.convertToQuery(value);
_.forEach (flatObject, (_value, _key) => {
result[key + '.' + _key] = _value;
result[`${key}.${_key}`] = _value;
});
} else {
result[key] = value;

View File

@ -398,7 +398,7 @@ module.exports = {
queryOpts.skip = convertedParams.start;
switch (association.nature) {
case "manyToMany":
case "manyToMany": {
const arrayOfIds = (obj[association.alias] || []).map(
related => {
return related[ref.primaryKey] || related;
@ -413,7 +413,7 @@ module.exports = {
...where.where,
}).where;
break;
// falls through
}
default:
// Where.
queryOpts.query = strapi.utils.models.convertParams(name, {

View File

@ -18,10 +18,9 @@ module.exports = {
}
}
})
.fetchAll({
withRelated: populate || _.keys(_.groupBy(_.reject(this.associations, { autoPopulate: false }), 'alias'))
});
.fetchAll({
withRelated: populate || _.keys(_.groupBy(_.reject(this.associations, { autoPopulate: false }), 'alias'))
});
return records ? records.toJSON() : records;
},

View File

@ -11,11 +11,6 @@ const path = require('path');
const _ = require('lodash');
const pluralize = require('pluralize');
// Following this discussion https://stackoverflow.com/questions/18082/validate-decimal-numbers-in-javascript-isnumeric this function is the best implem to determine if a value is a valid number candidate
const isNumeric = (value) => {
return !_.isObject(value) && !isNaN(parseFloat(value)) && isFinite(value);
};
// Constants
const ORDERS = ['ASC', 'DESC'];