collection-group.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. /*
  3. * Copyright 2020 Google LLC
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.CollectionGroup = void 0;
  19. const query_partition_1 = require("./query-partition");
  20. const util_1 = require("./util");
  21. const logger_1 = require("./logger");
  22. const query_1 = require("./reference/query");
  23. const query_options_1 = require("./reference/query-options");
  24. const path_1 = require("./path");
  25. const validate_1 = require("./validate");
  26. const types_1 = require("./types");
  27. const order_1 = require("./order");
  28. const trace_util_1 = require("./telemetry/trace-util");
  29. /**
  30. * A `CollectionGroup` refers to all documents that are contained in a
  31. * collection or subcollection with a specific collection ID.
  32. *
  33. * @class CollectionGroup
  34. */
  35. class CollectionGroup extends query_1.Query {
  36. /** @private */
  37. constructor(firestore, collectionId, converter) {
  38. super(firestore, query_options_1.QueryOptions.forCollectionGroupQuery(collectionId, converter));
  39. }
  40. /**
  41. * Partitions a query by returning partition cursors that can be used to run
  42. * the query in parallel. The returned cursors are split points that can be
  43. * used as starting and end points for individual query invocations.
  44. *
  45. * @example
  46. * ```
  47. * const query = firestore.collectionGroup('collectionId');
  48. * for await (const partition of query.getPartitions(42)) {
  49. * const partitionedQuery = partition.toQuery();
  50. * const querySnapshot = await partitionedQuery.get();
  51. * console.log(`Partition contained ${querySnapshot.length} documents`);
  52. * }
  53. *
  54. * ```
  55. * @param {number} desiredPartitionCount The desired maximum number of
  56. * partition points. The number must be strictly positive. The actual number
  57. * of partitions returned may be fewer.
  58. * @return {AsyncIterable<QueryPartition>} An AsyncIterable of
  59. * `QueryPartition`s.
  60. */
  61. async *getPartitions(desiredPartitionCount) {
  62. const partitions = [];
  63. await this._firestore._traceUtil.startActiveSpan(trace_util_1.SPAN_NAME_PARTITION_QUERY, async () => {
  64. var _a;
  65. (0, validate_1.validateInteger)('desiredPartitionCount', desiredPartitionCount, {
  66. minValue: 1,
  67. });
  68. const tag = (0, util_1.requestTag)();
  69. await this.firestore.initializeIfNeeded(tag);
  70. if (desiredPartitionCount > 1) {
  71. // Partition queries require explicit ordering by __name__.
  72. const queryWithDefaultOrder = this.orderBy(path_1.FieldPath.documentId());
  73. const request = queryWithDefaultOrder.toProto();
  74. // Since we are always returning an extra partition (with an empty endBefore
  75. // cursor), we reduce the desired partition count by one.
  76. request.partitionCount = desiredPartitionCount - 1;
  77. const stream = await this.firestore.requestStream('partitionQueryStream',
  78. /* bidirectional= */ false, request, tag);
  79. stream.resume();
  80. for await (const currentCursor of stream) {
  81. partitions.push((_a = currentCursor.values) !== null && _a !== void 0 ? _a : []);
  82. }
  83. }
  84. (0, logger_1.logger)('Firestore.getPartitions', tag, 'Received %d partitions', partitions.length);
  85. // Sort the partitions as they may not be ordered if responses are paged.
  86. partitions.sort((l, r) => (0, order_1.compareArrays)(l, r));
  87. });
  88. for (let i = 0; i < partitions.length; ++i) {
  89. yield new query_partition_1.QueryPartition(this._firestore, this._queryOptions.collectionId, this._queryOptions.converter, i > 0 ? partitions[i - 1] : undefined, partitions[i]);
  90. }
  91. // Return the extra partition with the empty cursor.
  92. yield new query_partition_1.QueryPartition(this._firestore, this._queryOptions.collectionId, this._queryOptions.converter, partitions.pop(), undefined);
  93. }
  94. withConverter(converter) {
  95. return new CollectionGroup(this.firestore, this._queryOptions.collectionId, converter !== null && converter !== void 0 ? converter : (0, types_1.defaultConverter)());
  96. }
  97. }
  98. exports.CollectionGroup = CollectionGroup;
  99. //# sourceMappingURL=collection-group.js.map