mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-23 20:10:05 +08:00
feat(firestore): array-contains, array-contains-any & in filters (#2868)
* feat(firestore) Add IN query support (JS/Android) * feat(firestore) in query validation * feat(firestore) in query ios support / tests * docs(firestore): update reference docs to include in query support
This commit is contained in:
@@ -25,6 +25,8 @@ const OPERATORS = {
|
||||
'<': 'LESS_THAN',
|
||||
'<=': 'LESS_THAN_OR_EQUAL',
|
||||
'array-contains': 'ARRAY_CONTAINS',
|
||||
'array-contains-any': 'ARRAY_CONTAINS_ANY',
|
||||
in: 'IN',
|
||||
};
|
||||
|
||||
const INEQUALITY = {
|
||||
@@ -150,6 +152,10 @@ export default class FirestoreQueryModifiers {
|
||||
return OPERATORS[operator] === 'EQUAL';
|
||||
}
|
||||
|
||||
isInOperator(operator) {
|
||||
return OPERATORS[operator] === 'IN' || OPERATORS[operator] === 'ARRAY_CONTAINS_ANY';
|
||||
}
|
||||
|
||||
where(fieldPath, opStr, value) {
|
||||
const filter = {
|
||||
fieldPath: fieldPath._toPath(),
|
||||
@@ -190,20 +196,48 @@ export default class FirestoreQueryModifiers {
|
||||
}
|
||||
|
||||
let hasArrayContains;
|
||||
let hasArrayContainsAny;
|
||||
let hasIn;
|
||||
|
||||
for (let i = 0; i < this._filters.length; i++) {
|
||||
const filter = this._filters[i];
|
||||
// Skip if no array-contains
|
||||
if (filter.operator !== OPERATORS['array-contains']) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!hasArrayContains) {
|
||||
if (filter.operator === OPERATORS['array-contains']) {
|
||||
if (hasArrayContains) {
|
||||
throw new Error('Invalid query. Queries only support a single array-contains filter.');
|
||||
}
|
||||
hasArrayContains = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
throw new Error('Invalid query. Queries only support a single array-contains filter.');
|
||||
if (filter.operator === OPERATORS['array-contains-any']) {
|
||||
if (hasArrayContainsAny) {
|
||||
throw new Error(
|
||||
"Invalid query. You cannot use more than one 'array-contains-any' filter.",
|
||||
);
|
||||
}
|
||||
|
||||
if (hasIn) {
|
||||
throw new Error(
|
||||
"Invalid query. You cannot use 'array-contains-any' filters with 'in' filters.",
|
||||
);
|
||||
}
|
||||
|
||||
hasArrayContainsAny = true;
|
||||
}
|
||||
|
||||
if (filter.operator === OPERATORS.in) {
|
||||
if (hasIn) {
|
||||
throw new Error("Invalid query. You cannot use more than one 'in' filter.");
|
||||
}
|
||||
|
||||
if (hasArrayContainsAny) {
|
||||
throw new Error(
|
||||
"Invalid query. You cannot use 'in' filters with 'array-contains-any' filters.",
|
||||
);
|
||||
}
|
||||
|
||||
hasIn = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user