From 5f5845758d8e93f493d82cf796ba1de9b058b9ac Mon Sep 17 00:00:00 2001 From: Valentin Lamatte Date: Tue, 11 May 2021 11:15:31 +0200 Subject: [PATCH] feat(filters): add support for the \`model.field IN array\` filter condition (#719) --- src/services/filters-parser.js | 2 ++ test/databases.test.js | 33 ++++++++++++++++++++++++++++ test/services/filters-parser.test.js | 3 ++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/services/filters-parser.js b/src/services/filters-parser.js index 4683dd91c..2011b7af6 100644 --- a/src/services/filters-parser.js +++ b/src/services/filters-parser.js @@ -93,6 +93,8 @@ function FiltersParser(modelSchema, timezone, options) { return { [this.OPERATORS.EQ]: value }; case 'includes_all': return { [this.OPERATORS.CONTAINS]: value }; + case 'in': + return { [this.OPERATORS.IN]: value }; default: throw new NoMatchingOperatorError(); } diff --git a/test/databases.test.js b/test/databases.test.js index 0d31f5a0d..2444843e7 100644 --- a/test/databases.test.js +++ b/test/databases.test.js @@ -2588,6 +2588,39 @@ const HasManyDissociator = require('../src/services/has-many-dissociator'); }); }); + describe('with a "in" condition on a number field', () => { + it('should generate a valid SQL query', async () => { + expect.assertions(1); + + const { models, sequelizeOptions } = initializeSequelize(); + const params = _.clone(paramsBaseList); + params.filters = JSON.stringify({ + field: 'id', + operator: 'in', + value: [100, 101, 102], + }); + const result = await new ResourcesGetter(models.user, sequelizeOptions, params) + .perform(); + // Only users with ids 100 and 102 exist in fixtures + expect(result[0]).toHaveLength(2); + }); + + it('should return the records result', async () => { + expect.assertions(1); + + const { models, sequelizeOptions } = initializeSequelize(); + const params = _.clone(paramsBaseCount); + params.filters = JSON.stringify({ + field: 'id', + operator: 'in', + value: [100, 101, 102], + }); + const count = await new ResourcesGetter(models.user, sequelizeOptions, params).count(); + // Only users with ids 100 and 102 exist in fixtures + expect(count).toStrictEqual(2); + }); + }); + describe('with complex filters conditions', () => { const filters = JSON.stringify({ aggregator: 'and', diff --git a/test/services/filters-parser.test.js b/test/services/filters-parser.test.js index b9973dbd7..05dfae04b 100644 --- a/test/services/filters-parser.test.js +++ b/test/services/filters-parser.test.js @@ -92,7 +92,7 @@ describe('services > filters-parser', () => { values.forEach((value) => { it(`should return the appropriate value (${typeof value})`, () => { - expect.assertions(14); + expect.assertions(15); expect(defaultFiltersParser.formatOperatorValue('starts_with', value)).toStrictEqual({ [OPERATORS.LIKE]: `${value}%` }); expect(defaultFiltersParser.formatOperatorValue('ends_with', value)).toStrictEqual({ [OPERATORS.LIKE]: `%${value}` }); expect(defaultFiltersParser.formatOperatorValue('contains', value)).toStrictEqual({ [OPERATORS.LIKE]: `%${value}%` }); @@ -113,6 +113,7 @@ describe('services > filters-parser', () => { [OPERATORS.EQ]: '', }], }); + expect(defaultFiltersParser.formatOperatorValue('in', value)).toStrictEqual({ [OPERATORS.IN]: value }); }); it('should raise an error on unknown operator', () => {