From a9606b6eae403fe744ea55373a2247042c2746ca Mon Sep 17 00:00:00 2001 From: Sajjad Shirazy Date: Sun, 3 Feb 2019 15:08:36 +0330 Subject: [PATCH] Supporintg `IN` and `NOT IN` as a filter type --- docs/3.x.x/guides/filters.md | 2 ++ docs/3.x.x/guides/graphql.md | 6 ++++++ docs/3.x.x/guides/restapi.md | 2 ++ .../templates/bookshelf/service.template | 2 +- packages/strapi-hook-bookshelf/lib/index.js | 6 +++--- .../components/FilterOptions/filterTypes.js | 16 +++++++++++++++ .../admin/src/translations/en.json | 2 ++ .../config/queries/bookshelf.js | 2 +- .../services/utils/parametersOptions.json | 20 +++++++++++++++++++ .../config/queries/bookshelf.js | 2 +- .../config/queries/bookshelf.js | 2 +- packages/strapi-utils/lib/models.js | 2 +- 12 files changed, 56 insertions(+), 8 deletions(-) diff --git a/docs/3.x.x/guides/filters.md b/docs/3.x.x/guides/filters.md index c4760cfc01..6303f82e15 100644 --- a/docs/3.x.x/guides/filters.md +++ b/docs/3.x.x/guides/filters.md @@ -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 diff --git a/docs/3.x.x/guides/graphql.md b/docs/3.x.x/guides/graphql.md index 48dfa1752f..5f2f47e03f 100644 --- a/docs/3.x.x/guides/graphql.md +++ b/docs/3.x.x/guides/graphql.md @@ -197,6 +197,8 @@ You can also apply different parameters to the query to make more complex querie - `_gte`: Greater than or equal to. - `_contains`: Contains. - `_containss`: Contains sensitive. + - `_in`: Matches any value in the array of values. + - `_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 } } ``` diff --git a/docs/3.x.x/guides/restapi.md b/docs/3.x.x/guides/restapi.md index 27232a711c..5b9efaeea1 100644 --- a/docs/3.x.x/guides/restapi.md +++ b/docs/3.x.x/guides/restapi.md @@ -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 diff --git a/packages/strapi-generate-api/templates/bookshelf/service.template b/packages/strapi-generate-api/templates/bookshelf/service.template index 37b3b58cd2..f20d20582f 100644 --- a/packages/strapi-generate-api/templates/bookshelf/service.template +++ b/packages/strapi-generate-api/templates/bookshelf/service.template @@ -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]) } diff --git a/packages/strapi-hook-bookshelf/lib/index.js b/packages/strapi-hook-bookshelf/lib/index.js index a34f198d4c..a52d9570b7 100644 --- a/packages/strapi-hook-bookshelf/lib/index.js +++ b/packages/strapi-hook-bookshelf/lib/index.js @@ -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: diff --git a/packages/strapi-plugin-content-manager/admin/src/components/FilterOptions/filterTypes.js b/packages/strapi-plugin-content-manager/admin/src/components/FilterOptions/filterTypes.js index 7b1403d777..78571fe26f 100644 --- a/packages/strapi-plugin-content-manager/admin/src/components/FilterOptions/filterTypes.js +++ b/packages/strapi-plugin-content-manager/admin/src/components/FilterOptions/filterTypes.js @@ -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 [ diff --git a/packages/strapi-plugin-content-manager/admin/src/translations/en.json b/packages/strapi-plugin-content-manager/admin/src/translations/en.json index 76479a2cf8..708d6d9427 100644 --- a/packages/strapi-plugin-content-manager/admin/src/translations/en.json +++ b/packages/strapi-plugin-content-manager/admin/src/translations/en.json @@ -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", diff --git a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js index 81e0c29ee4..9ee33ec021 100644 --- a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js +++ b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js @@ -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]); } diff --git a/packages/strapi-plugin-documentation/services/utils/parametersOptions.json b/packages/strapi-plugin-documentation/services/utils/parametersOptions.json index bedb55afed..01e003b90f 100755 --- a/packages/strapi-plugin-documentation/services/utils/parametersOptions.json +++ b/packages/strapi-plugin-documentation/services/utils/parametersOptions.json @@ -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 } ] \ No newline at end of file diff --git a/packages/strapi-plugin-upload/config/queries/bookshelf.js b/packages/strapi-plugin-upload/config/queries/bookshelf.js index f5efcaabd4..5ba8021768 100644 --- a/packages/strapi-plugin-upload/config/queries/bookshelf.js +++ b/packages/strapi-plugin-upload/config/queries/bookshelf.js @@ -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]); } diff --git a/packages/strapi-plugin-users-permissions/config/queries/bookshelf.js b/packages/strapi-plugin-users-permissions/config/queries/bookshelf.js index 7ca7e422cd..0ada8ded60 100644 --- a/packages/strapi-plugin-users-permissions/config/queries/bookshelf.js +++ b/packages/strapi-plugin-users-permissions/config/queries/bookshelf.js @@ -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]); } diff --git a/packages/strapi-utils/lib/models.js b/packages/strapi-utils/lib/models.js index 349628459d..c8e25f6d66 100644 --- a/packages/strapi-utils/lib/models.js +++ b/packages/strapi-utils/lib/models.js @@ -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 {