Skip to content

Commit

Permalink
Merge pull request #273 from amilosmanli/lazy-loading-filter-fix
Browse files Browse the repository at this point in the history
Better filtering for string filters
  • Loading branch information
benwinding authored Apr 19, 2023
2 parents e3b1c01 + 6a36816 commit ac0fd05
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/providers/lazy-loading/paramsToQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ export function getFiltersConstraints(filters: {
}): QueryConstraint[] {
return Object.entries(filters).flatMap(([fieldName, fieldValue]) => {
if (Array.isArray(fieldValue)) {
return [where(fieldName, 'in', fieldValue)];
return [where(fieldName, 'array-contains-any', fieldValue)];
} else if (Object.keys(filters).length === 1 && isNaN(fieldValue)) {
return [
where(fieldName, '>=', fieldValue),
where(fieldName, '<', fieldValue + 'z'),
];
} else {
return [where(fieldName, '==', fieldValue)];
}
Expand Down
56 changes: 56 additions & 0 deletions tests/providers/lazy-loading/paramsToQuery.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { getFiltersConstraints } from '../../../src/providers/lazy-loading/paramsToQuery';
import { QueryConstraintType } from '@firebase/firestore';

describe('getFiltersConstraints', () => {
it('should return where filter with array-contains-any operator when filter value is array', () => {
const filters = { fieldA: ['valueA'] };

const result = getFiltersConstraints(filters);

expect(result.length).toEqual(1);
const queryConstraint = result[0];
expect(queryConstraint.type).toEqual('where' as QueryConstraintType);
// @ts-ignore
expect(queryConstraint['_op']).toEqual('array-contains-any');
});

it('should return two where filters when filter value is string', () => {
const filters = { fieldA: 'valueA' };

const result = getFiltersConstraints(filters);

expect(result.length).toEqual(2);
const queryConstraintGte = result[0];
const queryConstraintLt = result[1];
expect(queryConstraintGte.type).toEqual('where' as QueryConstraintType);
expect(queryConstraintLt.type).toEqual('where' as QueryConstraintType);
// @ts-ignore
expect(queryConstraintGte['_op']).toEqual('>=');
// @ts-ignore
expect(queryConstraintLt['_op']).toEqual('<');
});

it('should return where filter with == operator when field value is number', () => {
const filters = { fieldA: 1 };

const result = getFiltersConstraints(filters);

expect(result.length).toEqual(1);
const queryConstraint = result[0];
expect(queryConstraint.type).toEqual('where' as QueryConstraintType);
// @ts-ignore
expect(queryConstraint['_op']).toEqual('==');
});

it('should return where filter with == operator when field value is boolean', () => {
const filters = { fieldA: false };

const result = getFiltersConstraints(filters);

expect(result.length).toEqual(1);
const queryConstraint = result[0];
expect(queryConstraint.type).toEqual('where' as QueryConstraintType);
// @ts-ignore
expect(queryConstraint['_op']).toEqual('==');
});
});

0 comments on commit ac0fd05

Please sign in to comment.