Skip to content

Commit a7622d4

Browse files
Markduckworth/or queries 4274 (#6896)
* Updated the canonical ID of flat conjunctions to match the canonical ID of old style implicit flat conjunctions. * Updated documentation of or() and and() to match the latest API docs in the Android implementation.
1 parent 20e5ef7 commit a7622d4

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

.changeset/quick-radios-obey.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@firebase/firestore": patch
3+
---
4+
5+
Update canonifyFilter to compute the canonization for flat conjunctions the same as implicit AND queries.

packages/firestore/src/core/filter.ts

+8
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,14 @@ export function canonifyFilter(filter: Filter): string {
325325
filter.op.toString() +
326326
canonicalId(filter.value)
327327
);
328+
} else if (compositeFilterIsFlatConjunction(filter)) {
329+
// Older SDK versions use an implicit AND operation between their filters.
330+
// In the new SDK versions, the developer may use an explicit AND filter.
331+
// To stay consistent with the old usages, we add a special case to ensure
332+
// the canonical ID for these two are the same. For example:
333+
// `col.whereEquals("a", 1).whereEquals("b", 2)` should have the same
334+
// canonical ID as `col.where(and(equals("a",1), equals("b",2)))`.
335+
return filter.filters.map(filter => canonifyFilter(filter)).join(',');
328336
} else {
329337
// filter instanceof CompositeFilter
330338
const canonicalIdsString = filter.filters

packages/firestore/src/lite-api/query.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,14 @@ export type QueryFilterConstraint =
364364
| QueryCompositeFilterConstraint;
365365

366366
/**
367-
* Creates a {@link QueryCompositeFilterConstraint} that performs a logical OR
368-
* of all the provided {@link QueryFilterConstraint}s.
367+
* Creates a new {@link QueryCompositeFilterConstraint} that is a disjunction of
368+
* the given filter constraints. A disjunction filter includes a document if it
369+
* satisfies any of the given filters.
369370
*
370-
* @param queryConstraints - Optional. The {@link QueryFilterConstraint}s
371-
* for OR operation. These must be created with calls to {@link where},
372-
* {@link or}, or {@link and}.
373-
* @returns The created {@link QueryCompositeFilterConstraint}.
371+
* @param queryConstraints - Optional. The list of
372+
* {@link QueryFilterConstraint}s to perform a disjunction for. These must be
373+
* created with calls to {@link where}, {@link or}, or {@link and}.
374+
* @returns The newly created {@link QueryCompositeFilterConstraint}.
374375
* @internal TODO remove this internal tag with OR Query support in the server
375376
*/
376377
export function or(
@@ -388,13 +389,14 @@ export function or(
388389
}
389390

390391
/**
391-
* Creates a {@link QueryCompositeFilterConstraint} that performs a logical AND
392-
* of all the provided {@link QueryFilterConstraint}s.
392+
* Creates a new {@link QueryCompositeFilterConstraint} that is a conjunction of
393+
* the given filter constraints. A conjunction filter includes a document if it
394+
* satisfies all of the given filters.
393395
*
394-
* @param queryConstraints - Optional. The {@link QueryFilterConstraint}s
395-
* for AND operation. These must be created with calls to {@link where},
396-
* {@link or}, or {@link and}.
397-
* @returns The created {@link QueryCompositeFilterConstraint}.
396+
* @param queryConstraints - Optional. The list of
397+
* {@link QueryFilterConstraint}s to perform a conjunction for. These must be
398+
* created with calls to {@link where}, {@link or}, or {@link and}.
399+
* @returns The newly created {@link QueryCompositeFilterConstraint}.
398400
* @internal TODO remove this internal tag with OR Query support in the server
399401
*/
400402
export function and(

packages/firestore/test/unit/core/filter.test.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import {
2525
FieldFilter,
2626
Operator
2727
} from '../../../src/core/filter';
28-
import { andFilter, filter, orFilter } from '../../util/helpers';
28+
import { queryToTarget } from '../../../src/core/query';
29+
import { canonifyTarget } from '../../../src/core/target';
30+
import { andFilter, filter, orFilter, query } from '../../util/helpers';
2931

3032
describe('FieldFilter', () => {
3133
it('exposes field filter members', () => {
@@ -93,4 +95,14 @@ describe('CompositeFilter', () => {
9395
expect(compositeFilterIsFlat(orFilter2)).false;
9496
expect(compositeFilterIsFlatConjunction(orFilter2)).false;
9597
});
98+
99+
it('computes canonical id of flat conjunctions', () => {
100+
const target1 = query('col', a, b, c);
101+
102+
const target2 = query('col', andFilter(a, b, c));
103+
104+
expect(canonifyTarget(queryToTarget(target1))).to.equal(
105+
canonifyTarget(queryToTarget(target2))
106+
);
107+
});
96108
});

0 commit comments

Comments
 (0)