Supporintg IN and NOT IN as a filter type

This commit is contained in:
Sajjad Shirazy 2019-02-03 15:08:36 +03:30
parent 71c279aee6
commit a9606b6eae
12 changed files with 56 additions and 8 deletions

View File

@ -27,6 +27,8 @@ Easily filter results according to fields values.
- `_in`: Include in array
- `_contains`: Contains
- `_containss`: Contains case sensitive
- `_in`: Matches any value in the array of values
- `_nin`: Doesn't match any value in the array of values
#### Examples

View File

@ -197,6 +197,8 @@ You can also apply different parameters to the query to make more complex querie
- `<field>_gte`: Greater than or equal to.
- `<field>_contains`: Contains.
- `<field>_containss`: Contains sensitive.
- `<field>_in`: Matches any value in the array of values.
- `<field>_nin`: Doesn't match any value in the array of values.
Return the second decade of users which have an email that contains `@strapi.io` ordered by username.
@ -207,6 +209,10 @@ query {
}) {
username
email
},
books(limit: 10, where: { _id_nin: ["5c4dad1a8f3845222ca88a56", "5c4dad1a8f3845222ca88a57"] }) {
_id,
title
}
}
```

View File

@ -26,6 +26,8 @@ Easily filter results according to fields values.
- `_gte`: Greater than or equal to
- `_contains`: Contains
- `_containss`: Contains case sensitive
- `_in`: Matches any value in the array of values
- `_nin`: Doesn't match any value in the array of values
#### Examples

View File

@ -31,7 +31,7 @@ module.exports = {
return <%= globalID %>.query(function(qb) {
_.forEach(filters.where, (where, key) => {
if (_.isArray(where.value) && where.symbol !== 'IN') {
if (_.isArray(where.value) && where.symbol !== 'IN' && where.symbol !== 'NOT IN') {
for (const value in where.value) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value])
}

View File

@ -1107,14 +1107,14 @@ module.exports = function(strapi) {
result.key = `where.${key}`;
result.value = {
symbol: 'IN',
value,
value: _.isArray(value) ? value : [value],
};
break;
break;
case '_nin':
result.key = `where.${key}`;
result.value = {
symbol: 'NOT IN',
value,
value: _.isArray(value) ? value : [value],
};
break;
default:

View File

@ -37,6 +37,14 @@ const getFilters = (type) => {
id: 'content-manager.components.FilterOptions.FILTER_TYPES._containss',
value: '_containss',
},
{
id: 'content-manager.components.FilterOptions.FILTER_TYPES._in',
value: '_in',
},
{
id: 'content-manager.components.FilterOptions.FILTER_TYPES._nin',
value: '_nin',
}
];
case 'integer':
case 'float':
@ -67,6 +75,14 @@ const getFilters = (type) => {
id: 'content-manager.components.FilterOptions.FILTER_TYPES._gte',
value: '_gte',
},
{
id: 'content-manager.components.FilterOptions.FILTER_TYPES._in',
value: '_in',
},
{
id: 'content-manager.components.FilterOptions.FILTER_TYPES._nin',
value: '_nin',
},
];
default:
return [

View File

@ -13,6 +13,8 @@
"components.FilterOptions.FILTER_TYPES._lt": "is lower than",
"components.FilterOptions.FILTER_TYPES._lte": "is lower than or equal to",
"components.FilterOptions.FILTER_TYPES._ne": "is not",
"components.FilterOptions.FILTER_TYPES._in": "matches any value in the array of values",
"components.FilterOptions.FILTER_TYPES._nin": "doesn't match any value in the array of values",
"components.FilterOptions.button.apply": "Apply",
"components.FiltersPickWrapper.PluginHeader.actions.apply": "Apply",
"components.FiltersPickWrapper.PluginHeader.actions.clearAll": "Clear all",

View File

@ -4,7 +4,7 @@ module.exports = {
find: async function (params, populate, raw = false) {
return this.query(function(qb) {
_.forEach(params.where, (where, key) => {
if (_.isArray(where.value) && where.symbol !== 'IN') {
if (_.isArray(where.value) && where.symbol !== 'IN' && where.symbol !== 'NOT IN') {
for (const value in where.value) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
}

View File

@ -108,5 +108,25 @@
"type": "string"
},
"deprecated": false
},
{
"name": "_in",
"in": "query",
"required": false,
"description": "Get records that matches any value in the array of values",
"schema": {
"type": "array"
},
"deprecated": false
},
{
"name": "_nin",
"in": "query",
"required": false,
"description": "Get records that doesn't match any value in the array of values",
"schema": {
"type": "array"
},
"deprecated": false
}
]

View File

@ -6,7 +6,7 @@ module.exports = {
Object.keys(params.where).forEach((key) => {
const where = params.where[key];
if (Array.isArray(where.value)) {
if (Array.isArray(where.value) && where.symbol !== 'IN' && where.symbol !== 'NOT IN') {
for (const value in where.value) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
}

View File

@ -5,7 +5,7 @@ module.exports = {
find: async function (params = {}, populate) {
const records = await this.query(function(qb) {
_.forEach(params.where, (where, key) => {
if (_.isArray(where.value)) {
if (_.isArray(where.value) && where.symbol !== 'IN' && where.symbol !== 'NOT IN') {
for (const value in where.value) {
qb[value ? 'where' : 'orWhere'](key, where.symbol, where.value[value]);
}

View File

@ -491,7 +491,7 @@ module.exports = {
let type;
if (_.includes(['ne', 'lt', 'gt', 'lte', 'gte', 'contains', 'containss', 'in'], _.last(suffix))) {
if (_.includes(['ne', 'lt', 'gt', 'lte', 'gte', 'contains', 'containss', 'in', 'nin'], _.last(suffix))) {
type = `_${_.last(suffix)}`;
key = _.dropRight(suffix).join('_');
} else {