Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OR query snippets #326

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions firestore-next/test.firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,77 @@ describe("firestore", () => {
limit(25));
// [END paginate]
});

it("should handle OR queries", async () => {
const { collection, query, where, and } = require("firebase/firestore");
// [START or_query]
const query = query(collection(db, "cities"), and(
morganchen12 marked this conversation as resolved.
Show resolved Hide resolved
where('name', '>', 'L'),
or(
where('capital', '==', true),
where('population', '>=', 1000000)
)
));
// [END or_query]
});

it("should allow for 30 or fewer disjunctions", async () => {
const { collection, query, where, and } = require("firebase/firestore");
const collectionRef = collection(db, "cities");
// [START one_disjunction]
query(collectionRef, where("a", "==", 1));
// [END one_disjunction]

// [START two_disjunctions]
query(collectionRef, or( where("a", "==", 1), where("b", "==", 2) ));
// [END two_disjunctions]

// [START four_disjunctions]
query(collectionRef,
or( and( where("a", "==", 1), where("c", "==", 3) ),
and( where("a", "==", 1), where("d", "==", 4) ),
and( where("b", "==", 2), where("c", "==", 3) ),
and( where("b", "==", 2), where("d", "==", 4) )
)
);
// [END four_disjunctions]

// [START four_disjunctions_compact]
query(collectionRef,
and( or( where("a", "==", 1), where("b", "==", 2) ),
or( where("c", "==", 3), where("d", "==", 4) )
)
);
// [END four_disjunctions_compact]

expect(() => {
// [START 50_disjunctions]
query(collectionRef,
and( where("a", "in", [1, 2, 3, 4, 5]),
where("b", "in", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)
);
// [END 50_disjunctions]
}).to.throw;

// [START 20_disjunctions]
query(collectionRef,
or( where("a", "in", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
where("b", "in", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)
);
// [END 20_disjunctions]

// [START 10_disjunctions]
query(collectionRef,
and( where("a", "in", [1, 2, 3, 4, 5]),
or( where("b", "==", 2),
where("c", "==", 3)
)
)
);
// [END 10_disjunctions]
});
});

describe('collectionGroup(landmarks)', () => {
Expand Down
15 changes: 15 additions & 0 deletions snippets/firestore-next/test-firestore/four_disjunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START four_disjunctions_modular]
query(collectionRef,
or( and( where("a", "==", 1), where("c", "==", 3) ),
and( where("a", "==", 1), where("d", "==", 4) ),
and( where("b", "==", 2), where("c", "==", 3) ),
and( where("b", "==", 2), where("d", "==", 4) )
)
);
// [END four_disjunctions_modular]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START four_disjunctions_compact_modular]
query(collectionRef,
and( or( where("a", "==", 1), where("b", "==", 2) ),
or( where("c", "==", 3), where("d", "==", 4) )
)
);
// [END four_disjunctions_compact_modular]
9 changes: 9 additions & 0 deletions snippets/firestore-next/test-firestore/one_disjunction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START one_disjunction_modular]
query(collectionRef, where("a", "==", 1));
// [END one_disjunction_modular]
15 changes: 15 additions & 0 deletions snippets/firestore-next/test-firestore/or_query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START or_query_modular]
const query = query(collection(db, "cities"), and(
where('name', '>', 'L'),
or(
where('capital', '==', true),
where('population', '>=', 1000000)
)
));
// [END or_query_modular]
9 changes: 9 additions & 0 deletions snippets/firestore-next/test-firestore/two_disjunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// This snippet file was generated by processing the source file:
// ./firestore-next/test.firestore.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.

// [START two_disjunctions_modular]
query(collectionRef, or( where("a", "==", 1), where("b", "==", 2) ));
// [END two_disjunctions_modular]