@firebase/firestore
Version:
The Cloud Firestore component of the Firebase JS SDK.
204 lines (203 loc) • 9.84 kB
TypeScript
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DatabaseId } from '../core/database_info';
import { Query as InternalQuery } from '../core/query';
import { Bound, Direction, FieldFilter, Operator, OrderBy } from '../core/target';
import { Document } from '../model/document';
import { FieldPath as InternalFieldPath } from '../model/path';
import { FieldPath } from './field_path';
import { Query } from './reference';
import { DocumentSnapshot } from './snapshot';
import { UserDataReader } from './user_data_reader';
export declare function validateHasExplicitOrderByForLimitToLast(query: InternalQuery): void;
/** Describes the different query constraints available in this SDK. */
export declare type QueryConstraintType = 'where' | 'orderBy' | 'limit' | 'limitToLast' | 'startAt' | 'startAfter' | 'endAt' | 'endBefore';
/**
* A `QueryConstraint` is used to narrow the set of documents returned by a
* Firestore query. `QueryConstraint`s are created by invoking {@link where},
* {@link orderBy}, {@link (startAt:1)}, {@link (startAfter:1)}, {@link
* endBefore:1}, {@link (endAt:1)}, {@link limit} or {@link limitToLast} and
* can then be passed to {@link query} to create a new query instance that
* also contains this `QueryConstraint`.
*/
export declare abstract class QueryConstraint {
/** The type of this query constraints */
abstract readonly type: QueryConstraintType;
/**
* Takes the provided `Query` and returns a copy of the `Query` with this
* `QueryConstraint` applied.
*/
abstract _apply<T>(query: Query<T>): Query<T>;
}
/**
* Creates a new immutable instance of `Query` that is extended to also include
* additional query constraints.
*
* @param query - The Query instance to use as a base for the new constraints.
* @param queryConstraints - The list of `QueryConstraint`s to apply.
* @throws if any of the provided query constraints cannot be combined with the
* existing or new constraints.
*/
export declare function query<T>(query: Query<T>, ...queryConstraints: QueryConstraint[]): Query<T>;
/**
* Filter conditions in a {@link where} clause are specified using the
* strings '<', '<=', '==', '!=', '>=', '>', 'array-contains', 'in',
* 'array-contains-any', and 'not-in'.
*/
export declare type WhereFilterOp = '<' | '<=' | '==' | '!=' | '>=' | '>' | 'array-contains' | 'in' | 'array-contains-any' | 'not-in';
/**
* Creates a `QueryConstraint` that enforces that documents must contain the
* specified field and that the value should satisfy the relation constraint
* provided.
*
* @param fieldPath - The path to compare
* @param opStr - The operation string (e.g "<", "<=", "==", "<",
* "<=", "!=").
* @param value - The value for comparison
* @returns The created `Query`.
*/
export declare function where(fieldPath: string | FieldPath, opStr: WhereFilterOp, value: unknown): QueryConstraint;
/**
* The direction of a {@link orderBy} clause is specified as 'desc' or 'asc'
* (descending or ascending).
*/
export declare type OrderByDirection = 'desc' | 'asc';
/**
* Creates a `QueryConstraint` that sorts the query result by the
* specified field, optionally in descending order instead of ascending.
*
* @param fieldPath - The field to sort by.
* @param directionStr - Optional direction to sort by ('asc' or 'desc'). If
* not specified, order will be ascending.
* @returns The created `Query`.
*/
export declare function orderBy(fieldPath: string | FieldPath, directionStr?: OrderByDirection): QueryConstraint;
/**
* Creates a `QueryConstraint` that only returns the first matching documents.
*
* @param limit - The maximum number of items to return.
* @returns The created `Query`.
*/
export declare function limit(limit: number): QueryConstraint;
/**
* Creates a `QueryConstraint` that only returns the last matching documents.
*
* You must specify at least one `orderBy` clause for `limitToLast` queries,
* otherwise an exception will be thrown during execution.
*
* @param limit - The maximum number of items to return.
* @returns The created `Query`.
*/
export declare function limitToLast(limit: number): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to start at the
* provided document (inclusive). The starting position is relative to the order
* of the query. The document must contain all of the fields provided in the
* `orderBy` of this query.
*
* @param snapshot - The snapshot of the document to start at.
* @returns A `QueryConstraint` to pass to `query()`.
*/
export declare function startAt(snapshot: DocumentSnapshot<unknown>): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to start at the
* provided fields relative to the order of the query. The order of the field
* values must match the order of the order by clauses of the query.
*
* @param fieldValues - The field values to start this query at, in order
* of the query's order by.
* @returns A `QueryConstraint` to pass to `query()`.
*/
export declare function startAt(...fieldValues: unknown[]): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to start after the
* provided document (exclusive). The starting position is relative to the order
* of the query. The document must contain all of the fields provided in the
* orderBy of the query.
*
* @param snapshot - The snapshot of the document to start after.
* @returns A `QueryConstraint` to pass to `query()`
*/
export declare function startAfter(snapshot: DocumentSnapshot<unknown>): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to start after the
* provided fields relative to the order of the query. The order of the field
* values must match the order of the order by clauses of the query.
*
* @param fieldValues - The field values to start this query after, in order
* of the query's order by.
* @returns A `QueryConstraint` to pass to `query()`
*/
export declare function startAfter(...fieldValues: unknown[]): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to end before the
* provided document (exclusive). The end position is relative to the order of
* the query. The document must contain all of the fields provided in the
* orderBy of the query.
*
* @param snapshot - The snapshot of the document to end before.
* @returns A `QueryConstraint` to pass to `query()`
*/
export declare function endBefore(snapshot: DocumentSnapshot<unknown>): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to end before the
* provided fields relative to the order of the query. The order of the field
* values must match the order of the order by clauses of the query.
*
* @param fieldValues - The field values to end this query before, in order
* of the query's order by.
* @returns A `QueryConstraint` to pass to `query()`
*/
export declare function endBefore(...fieldValues: unknown[]): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to end at the
* provided document (inclusive). The end position is relative to the order of
* the query. The document must contain all of the fields provided in the
* orderBy of the query.
*
* @param snapshot - The snapshot of the document to end at.
* @returns A `QueryConstraint` to pass to `query()`
*/
export declare function endAt(snapshot: DocumentSnapshot<unknown>): QueryConstraint;
/**
* Creates a `QueryConstraint` that modifies the result set to end at the
* provided fields relative to the order of the query. The order of the field
* values must match the order of the order by clauses of the query.
*
* @param fieldValues - The field values to end this query at, in order
* of the query's order by.
* @returns A `QueryConstraint` to pass to `query()`
*/
export declare function endAt(...fieldValues: unknown[]): QueryConstraint;
export declare function newQueryFilter(query: InternalQuery, methodName: string, dataReader: UserDataReader, databaseId: DatabaseId, fieldPath: InternalFieldPath, op: Operator, value: unknown): FieldFilter;
export declare function newQueryOrderBy(query: InternalQuery, fieldPath: InternalFieldPath, direction: Direction): OrderBy;
/**
* Create a Bound from a query and a document.
*
* Note that the Bound will always include the key of the document
* and so only the provided document will compare equal to the returned
* position.
*
* Will throw if the document does not contain all fields of the order by
* of the query or if any of the fields in the order by are an uncommitted
* server timestamp.
*/
export declare function newQueryBoundFromDocument(query: InternalQuery, databaseId: DatabaseId, methodName: string, doc: Document | null, before: boolean): Bound;
/**
* Converts a list of field values to a Bound for the given query.
*/
export declare function newQueryBoundFromFields(query: InternalQuery, databaseId: DatabaseId, dataReader: UserDataReader, methodName: string, values: unknown[], before: boolean): Bound;