Skip to content

Commit 2308183

Browse files
committed
fix(firestore): a single or/and filter is allowed, 2+ not required
this is allowed in firebase-js-sdk
1 parent 95050c0 commit 2308183

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

packages/firestore/e2e/Query/where.and.filter.e2e.js

+40
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,46 @@ describe(' firestore().collection().where(AND Filters)', function () {
10561056
});
10571057
});
10581058

1059+
it('returns with single where "==" in or filter', async function () {
1060+
const { getFirestore, collection, addDoc, getDocs, query, where, and } = firestoreModular;
1061+
const colRef = collection(getFirestore(), `${COLLECTION}/filter/single-or`);
1062+
1063+
const expected = { foo: 'bar', baz: 'baz' };
1064+
const notExpected = { foo: 'bar', baz: 'something' };
1065+
await Promise.all([
1066+
addDoc(colRef, notExpected),
1067+
addDoc(colRef, expected),
1068+
addDoc(colRef, expected),
1069+
]);
1070+
1071+
const snapshot = await getDocs(query(colRef, and(where('baz', '==', 'baz'))));
1072+
1073+
snapshot.size.should.eql(2);
1074+
snapshot.forEach(s => {
1075+
s.data().should.eql(jet.contextify(expected));
1076+
});
1077+
});
1078+
1079+
it('returns with single where "==" in and filter', async function () {
1080+
const { getFirestore, collection, addDoc, getDocs, query, where, or } = firestoreModular;
1081+
const colRef = collection(getFirestore(), `${COLLECTION}/filter/single-and`);
1082+
1083+
const expected = { foo: 'bar', baz: 'baz' };
1084+
const notExpected = { foo: 'bar', baz: 'something' };
1085+
await Promise.all([
1086+
addDoc(colRef, notExpected),
1087+
addDoc(colRef, expected),
1088+
addDoc(colRef, expected),
1089+
]);
1090+
1091+
const snapshot = await getDocs(query(colRef, or(where('baz', '==', 'baz'))));
1092+
1093+
snapshot.size.should.eql(2);
1094+
snapshot.forEach(s => {
1095+
s.data().should.eql(jet.contextify(expected));
1096+
});
1097+
});
1098+
10591099
it('returns with where "==" & ">" filter', async function () {
10601100
const { getFirestore, collection, addDoc, getDocs, query, where, and } = firestoreModular;
10611101
const colRef = collection(getFirestore(), `${COLLECTION}/filter/equals`);

packages/firestore/lib/FirestoreFilter.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export function _Filter(fieldPath, operator, value, filterOperator, queries) {
4242
}
4343

4444
Filter.and = function and(...queries) {
45-
if (queries.length > 10 || queries.length < 2) {
46-
throw new Error(`Expected 2-10 instances of Filter, but got ${queries.length} Filters`);
45+
if (queries.length > 10 || queries.length < 1) {
46+
throw new Error(`Expected 1-10 instances of Filter, but got ${queries.length} Filters`);
4747
}
4848

4949
const validateFilters = queries.every(filter => filter instanceof _Filter);
@@ -60,8 +60,8 @@ function hasOrOperator(obj) {
6060
}
6161

6262
Filter.or = function or(...queries) {
63-
if (queries.length > 10 || queries.length < 2) {
64-
throw new Error(`Expected 2-10 instances of Filter, but got ${queries.length} Filters`);
63+
if (queries.length > 10 || queries.length < 1) {
64+
throw new Error(`Expected 1-10 instances of Filter, but got ${queries.length} Filters`);
6565
}
6666

6767
const validateFilters = queries.every(filter => filter instanceof _Filter);

0 commit comments

Comments
 (0)