|
2 | 2 | // [SNIPPETS_SEPARATION enabled] |
3 | 3 |
|
4 | 4 | const { expect } = require('chai'); |
| 5 | +import { or } from "firebase/firestore"; |
5 | 6 |
|
6 | 7 | // [START city_custom_object] |
7 | 8 | class City { |
@@ -892,7 +893,7 @@ describe("firestore", () => { |
892 | 893 | const { query, where } = require("firebase/firestore"); |
893 | 894 |
|
894 | 895 | const q = query(citiesRef, |
895 | | - where('regions', 'array-contains-any', ['west_coast', 'east_coast'])); |
| 896 | + where('regions', 'array-contains-any', [['west_coast'], ['east_coast']])); |
896 | 897 | // [END array_contains_any_filter] |
897 | 898 | }); |
898 | 899 |
|
@@ -1102,6 +1103,77 @@ describe("firestore", () => { |
1102 | 1103 | limit(25)); |
1103 | 1104 | // [END paginate] |
1104 | 1105 | }); |
| 1106 | + |
| 1107 | + it("should handle OR queries", async () => { |
| 1108 | + const { collection, query, where, and } = require("firebase/firestore"); |
| 1109 | + // [START or_query] |
| 1110 | + const q = query(collection(db, "cities"), and( |
| 1111 | + where('state', '==', 'CA'), |
| 1112 | + or( |
| 1113 | + where('capital', '==', true), |
| 1114 | + where('population', '>=', 1000000) |
| 1115 | + ) |
| 1116 | + )); |
| 1117 | + // [END or_query] |
| 1118 | + }); |
| 1119 | + |
| 1120 | + it("should allow for 30 or fewer disjunctions", async () => { |
| 1121 | + const { collection, query, where, and } = require("firebase/firestore"); |
| 1122 | + const collectionRef = collection(db, "cities"); |
| 1123 | + // [START one_disjunction] |
| 1124 | + query(collectionRef, where("a", "==", 1)); |
| 1125 | + // [END one_disjunction] |
| 1126 | + |
| 1127 | + // [START two_disjunctions] |
| 1128 | + query(collectionRef, or( where("a", "==", 1), where("b", "==", 2) )); |
| 1129 | + // [END two_disjunctions] |
| 1130 | + |
| 1131 | + // [START four_disjunctions] |
| 1132 | + query(collectionRef, |
| 1133 | + or( and( where("a", "==", 1), where("c", "==", 3) ), |
| 1134 | + and( where("a", "==", 1), where("d", "==", 4) ), |
| 1135 | + and( where("b", "==", 2), where("c", "==", 3) ), |
| 1136 | + and( where("b", "==", 2), where("d", "==", 4) ) |
| 1137 | + ) |
| 1138 | + ); |
| 1139 | + // [END four_disjunctions] |
| 1140 | + |
| 1141 | + // [START four_disjunctions_compact] |
| 1142 | + query(collectionRef, |
| 1143 | + and( or( where("a", "==", 1), where("b", "==", 2) ), |
| 1144 | + or( where("c", "==", 3), where("d", "==", 4) ) |
| 1145 | + ) |
| 1146 | + ); |
| 1147 | + // [END four_disjunctions_compact] |
| 1148 | + |
| 1149 | + expect(() => { |
| 1150 | + // [START 50_disjunctions] |
| 1151 | + query(collectionRef, |
| 1152 | + and( where("a", "in", [1, 2, 3, 4, 5]), |
| 1153 | + where("b", "in", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 1154 | + ) |
| 1155 | + ); |
| 1156 | + // [END 50_disjunctions] |
| 1157 | + }).to.throw; |
| 1158 | + |
| 1159 | + // [START 20_disjunctions] |
| 1160 | + query(collectionRef, |
| 1161 | + or( where("a", "in", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), |
| 1162 | + where("b", "in", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) |
| 1163 | + ) |
| 1164 | + ); |
| 1165 | + // [END 20_disjunctions] |
| 1166 | + |
| 1167 | + // [START 10_disjunctions] |
| 1168 | + query(collectionRef, |
| 1169 | + and( where("a", "in", [1, 2, 3, 4, 5]), |
| 1170 | + or( where("b", "==", 2), |
| 1171 | + where("c", "==", 3) |
| 1172 | + ) |
| 1173 | + ) |
| 1174 | + ); |
| 1175 | + // [END 10_disjunctions] |
| 1176 | + }); |
1105 | 1177 | }); |
1106 | 1178 |
|
1107 | 1179 | describe('collectionGroup(landmarks)', () => { |
|
0 commit comments