Class QueryPartition (7.5.0)

A split point that can be used in a query as a starting and/or end point for the query results. The cursors returned by and can only be used in a query that matches the constraint of query that produced this partition.

QueryPartition

Package

@google-cloud/firestore

Constructors

(constructor)(_firestore, _collectionId, _converter, _startAt, _endBefore)

constructor(_firestore: Firestore, _collectionId: string, _converter: firestore.FirestoreDataConverter<AppModelType, DbModelType>, _startAt: api.IValue[] | undefined, _endBefore: api.IValue[] | undefined);

Constructs a new instance of the QueryPartition class

Parameters
NameDescription
_firestore Firestore
_collectionId string
_converter FirebaseFirestore.FirestoreDataConverter<AppModelType, DbModelType>
_startAt google.firestore.v1.IValue[] | undefined
_endBefore google.firestore.v1.IValue[] | undefined

Properties

endBefore

get endBefore(): unknown[] | undefined;

The cursor that defines the first result after this partition or undefined if this is the last partition. The cursor value must be destructured when passed to endBefore() (for example with query.endBefore(...queryPartition.endBefore)).

Example

const query = firestore.collectionGroup('collectionId');
for await (const partition of query.getPartitions(42)) {
  let partitionedQuery = query.orderBy(FieldPath.documentId());
  if (partition.startAt) {
    partitionedQuery = partitionedQuery.startAt(...partition.startAt);
  }
  if (partition.endBefore) {
    partitionedQuery = partitionedQuery.endBefore(...partition.endBefore);
  }
  const querySnapshot = await partitionedQuery.get();
  console.log(`Partition contained ${querySnapshot.length} documents`);
}

{Array<*>} {Array<*>} A cursor value that can be used with or undefined if this is the last partition.

startAt

get startAt(): unknown[] | undefined;

The cursor that defines the first result for this partition or undefined if this is the first partition. The cursor value must be destructured when passed to startAt() (for example with query.startAt(...queryPartition.startAt)).

Example

const query = firestore.collectionGroup('collectionId');
for await (const partition of query.getPartitions(42)) {
  let partitionedQuery = query.orderBy(FieldPath.documentId());
  if (partition.startAt) {
    partitionedQuery = partitionedQuery.startAt(...partition.startAt);
  }
  if (partition.endBefore) {
    partitionedQuery = partitionedQuery.endBefore(...partition.endBefore);
  }
  const querySnapshot = await partitionedQuery.get();
  console.log(`Partition contained ${querySnapshot.length} documents`);
}

{Array<*>} {Array<*>} A cursor value that can be used with or undefined if this is the first partition.

Methods

toQuery()

toQuery(): Query<AppModelType, DbModelType>;

Returns a query that only encapsulates the documents for this partition.

Returns
TypeDescription
Query<AppModelType, DbModelType>
Example

const query = firestore.collectionGroup('collectionId');
for await (const partition of query.getPartitions(42)) {
  const partitionedQuery = partition.toQuery();
  const querySnapshot = await partitionedQuery.get();
  console.log(`Partition contained ${querySnapshot.length} documents`);
}

{Query