Skip to content

Refactor Filters #1894

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

Merged
merged 10 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 19 additions & 22 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ import {
import {
Bound,
Direction,
FieldFilter,
Filter,
Operator,
OrderBy,
Query as InternalQuery,
RelationFilter,
RelationOp
Query as InternalQuery
} from '../core/query';
import { Transaction as InternalTransaction } from '../core/transaction';
import { ChangeType, ViewSnapshot } from '../core/view_snapshot';
Expand Down Expand Up @@ -1430,16 +1430,16 @@ export class Query implements firestore.Query {

let fieldValue;
const fieldPath = fieldPathFromArgument('Query.where', field);
const relationOp = RelationOp.fromString(opStr);
const operator = Operator.fromString(opStr);
if (fieldPath.isKeyField()) {
if (
relationOp === RelationOp.ARRAY_CONTAINS ||
relationOp === RelationOp.ARRAY_CONTAINS_ANY ||
relationOp === RelationOp.IN
operator === Operator.ARRAY_CONTAINS ||
operator === Operator.ARRAY_CONTAINS_ANY ||
operator === Operator.IN
) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
`Invalid Query. You can't perform '${relationOp.toString()}' ` +
`Invalid Query. You can't perform '${operator.toString()}' ` +
'queries on FieldPath.documentId().'
);
}
Expand Down Expand Up @@ -1492,20 +1492,20 @@ export class Query implements firestore.Query {
}
} else {
if (
relationOp === RelationOp.IN ||
relationOp === RelationOp.ARRAY_CONTAINS_ANY
operator === Operator.IN ||
operator === Operator.ARRAY_CONTAINS_ANY
) {
if (!Array.isArray(value) || value.length === 0) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid Query. A non-empty array is required for ' +
`'${relationOp.toString()}' filters.`
`'${operator.toString()}' filters.`
);
}
if (value.length > 10) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
`Invalid Query. '${relationOp.toString()}' filters support a ` +
`Invalid Query. '${operator.toString()}' filters support a ` +
'maximum of 10 elements in the value array.'
);
}
Expand All @@ -1515,7 +1515,7 @@ export class Query implements firestore.Query {
value
);
}
const filter = Filter.create(fieldPath, relationOp, fieldValue);
const filter = Filter.create(fieldPath, operator, fieldValue);
this.validateNewFilter(filter);
return new Query(this._query.addFilter(filter), this.firestore);
}
Expand Down Expand Up @@ -1947,12 +1947,9 @@ export class Query implements firestore.Query {
}

private validateNewFilter(filter: Filter): void {
if (filter instanceof RelationFilter) {
const arrayOps = [
RelationOp.ARRAY_CONTAINS,
RelationOp.ARRAY_CONTAINS_ANY
];
const disjunctiveOps = [RelationOp.IN, RelationOp.ARRAY_CONTAINS_ANY];
if (filter instanceof FieldFilter) {
const arrayOps = [Operator.ARRAY_CONTAINS, Operator.ARRAY_CONTAINS_ANY];
const disjunctiveOps = [Operator.IN, Operator.ARRAY_CONTAINS_ANY];
const isArrayOp = arrayOps.indexOf(filter.op) >= 0;
const isDisjunctiveOp = disjunctiveOps.indexOf(filter.op) >= 0;

Expand All @@ -1978,12 +1975,12 @@ export class Query implements firestore.Query {
} else if (isDisjunctiveOp || isArrayOp) {
// You can have at most 1 disjunctive filter and 1 array filter. Check if
// the new filter conflicts with an existing one.
let conflictingOp: RelationOp | null = null;
let conflictingOp: Operator | null = null;
if (isDisjunctiveOp) {
conflictingOp = this._query.findRelationOpFilter(disjunctiveOps);
conflictingOp = this._query.findFilterOperator(disjunctiveOps);
}
if (conflictingOp === null && isArrayOp) {
conflictingOp = this._query.findRelationOpFilter(arrayOps);
conflictingOp = this._query.findFilterOperator(arrayOps);
}
if (conflictingOp != null) {
// We special case when it's a duplicate op to give a slightly clearer error message.
Expand Down
Loading