Skip to content

Commit

Permalink
feat(filters): add support for the \model.field IN array\ filter co…
Browse files Browse the repository at this point in the history
…ndition (#719)
  • Loading branch information
larcin authored May 11, 2021
1 parent 5297355 commit 5f58457
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/services/filters-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
33 changes: 33 additions & 0 deletions test/databases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion test/services/filters-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}%` });
Expand All @@ -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', () => {
Expand Down

0 comments on commit 5f58457

Please sign in to comment.