collection-group.d.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as firestore from '@google-cloud/firestore';
  2. import { QueryPartition } from './query-partition';
  3. import { Query } from './reference/query';
  4. import { Firestore } from './index';
  5. /**
  6. * A `CollectionGroup` refers to all documents that are contained in a
  7. * collection or subcollection with a specific collection ID.
  8. *
  9. * @class CollectionGroup
  10. */
  11. export declare class CollectionGroup<AppModelType = firestore.DocumentData, DbModelType extends firestore.DocumentData = firestore.DocumentData> extends Query<AppModelType, DbModelType> implements firestore.CollectionGroup<AppModelType, DbModelType> {
  12. /** @private */
  13. constructor(firestore: Firestore, collectionId: string, converter: firestore.FirestoreDataConverter<AppModelType, DbModelType> | undefined);
  14. /**
  15. * Partitions a query by returning partition cursors that can be used to run
  16. * the query in parallel. The returned cursors are split points that can be
  17. * used as starting and end points for individual query invocations.
  18. *
  19. * @example
  20. * ```
  21. * const query = firestore.collectionGroup('collectionId');
  22. * for await (const partition of query.getPartitions(42)) {
  23. * const partitionedQuery = partition.toQuery();
  24. * const querySnapshot = await partitionedQuery.get();
  25. * console.log(`Partition contained ${querySnapshot.length} documents`);
  26. * }
  27. *
  28. * ```
  29. * @param {number} desiredPartitionCount The desired maximum number of
  30. * partition points. The number must be strictly positive. The actual number
  31. * of partitions returned may be fewer.
  32. * @return {AsyncIterable<QueryPartition>} An AsyncIterable of
  33. * `QueryPartition`s.
  34. */
  35. getPartitions(desiredPartitionCount: number): AsyncIterable<QueryPartition<AppModelType, DbModelType>>;
  36. /**
  37. * Applies a custom data converter to this `CollectionGroup`, allowing you
  38. * to use your own custom model objects with Firestore. When you call get()
  39. * on the returned `CollectionGroup`, the provided converter will convert
  40. * between Firestore data of type `NewDbModelType` and your custom type
  41. * `NewAppModelType`.
  42. *
  43. * Using the converter allows you to specify generic type arguments when
  44. * storing and retrieving objects from Firestore.
  45. *
  46. * Passing in `null` as the converter parameter removes the current
  47. * converter.
  48. *
  49. * @example
  50. * ```
  51. * class Post {
  52. * constructor(readonly title: string, readonly author: string) {}
  53. *
  54. * toString(): string {
  55. * return this.title + ', by ' + this.author;
  56. * }
  57. * }
  58. *
  59. * const postConverter = {
  60. * toFirestore(post: Post): FirebaseFirestore.DocumentData {
  61. * return {title: post.title, author: post.author};
  62. * },
  63. * fromFirestore(
  64. * snapshot: FirebaseFirestore.QueryDocumentSnapshot
  65. * ): Post {
  66. * const data = snapshot.data();
  67. * return new Post(data.title, data.author);
  68. * }
  69. * };
  70. *
  71. * const querySnapshot = await Firestore()
  72. * .collectionGroup('posts')
  73. * .withConverter(postConverter)
  74. * .get();
  75. * for (const doc of querySnapshot.docs) {
  76. * const post = doc.data();
  77. * post.title; // string
  78. * post.toString(); // Should be defined
  79. * post.someNonExistentProperty; // TS error
  80. * }
  81. *
  82. * ```
  83. * @param {FirestoreDataConverter | null} converter Converts objects to and
  84. * from Firestore. Passing in `null` removes the current converter.
  85. * @return {CollectionGroup} A `CollectionGroup` that uses the provided
  86. * converter.
  87. */
  88. withConverter(converter: null): CollectionGroup;
  89. withConverter<NewAppModelType, NewDbModelType extends firestore.DocumentData = firestore.DocumentData>(converter: firestore.FirestoreDataConverter<NewAppModelType, NewDbModelType>): CollectionGroup<NewAppModelType, NewDbModelType>;
  90. }