Skip to content

Commit

Permalink
feat: allow null value in query filter list
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Nov 27, 2022
1 parent a0b24a2 commit 949124c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"dependencies": {
"@faker-js/faker": "^7.6.0",
"locter": "^0.3.2",
"rapiq": "^0.4.1",
"rapiq": "^0.5.0",
"reflect-metadata": "^0.1.13",
"yargs": "^17.6.2"
},
Expand Down
23 changes: 23 additions & 0 deletions src/query/parameter/filters/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ export function transformParsedFilters<T extends ObjectLiteral = ObjectLiteral>(
statement.push('IN');

statement.push(`(:...${bindingKey})`);

if (Array.isArray(filter.value)) {
const nullIndex = (filter.value as unknown[]).indexOf(null);
if (nullIndex !== -1) {
filter.value.splice(nullIndex, 1);

statement.unshift('(');
if (filter.operator === FilterComparisonOperator.NOT_IN) {
statement.push('AND');
} else {
statement.push('OR');
}
statement.push(fullKey);
statement.push('IS');

if (filter.operator === FilterComparisonOperator.NOT_IN) {
statement.push('NOT');
}

statement.push('NULL');
statement.push(')');
}
}
break;
}
case FilterComparisonOperator.LESS_THAN:
Expand Down
12 changes: 12 additions & 0 deletions test/unit/query/filters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,24 @@ describe('src/api/filters.ts', () => {
{statement: 'id IN (:...filter_id)', binding: {'filter_id': [1,2,3]}}
] as QueryFiltersOutput);

// in operator with null
data = parseAndTransformFilters({id: 'null,1,2,3'}, {allowed: ['id']});
expect(data).toEqual([
{statement: '( id IN (:...filter_id) OR id IS NULL )', binding: {'filter_id': [1,2,3]}}
] as QueryFiltersOutput);

// negation with in operator
data = parseAndTransformFilters({id: '!1,2,3'}, {allowed: ['id']});
expect(data).toEqual([
{statement: 'id NOT IN (:...filter_id)', binding: {'filter_id': [1,2,3]}}
] as QueryFiltersOutput);

// negation with in operator and null
data = parseAndTransformFilters({id: '!null,1,2,3'}, {allowed: ['id']});
expect(data).toEqual([
{statement: '( id NOT IN (:...filter_id) AND id IS NOT NULL )', binding: {'filter_id': [1,2,3]}}
] as QueryFiltersOutput);

// like operator
data = parseAndTransformFilters({name: '~name'}, {allowed: ['name']});
expect(data).toEqual([
Expand Down

0 comments on commit 949124c

Please sign in to comment.