recursive-delete.d.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. * Copyright 2021 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import * as firestore from '@google-cloud/firestore';
  17. import Firestore, { BulkWriter } from '.';
  18. /*!
  19. * Datastore allowed numeric IDs where Firestore only allows strings. Numeric
  20. * IDs are exposed to Firestore as __idNUM__, so this is the lowest possible
  21. * negative numeric value expressed in that format.
  22. *
  23. * This constant is used to specify startAt/endAt values when querying for all
  24. * descendants in a single collection.
  25. */
  26. export declare const REFERENCE_NAME_MIN_ID = "__id-9223372036854775808__";
  27. /*!
  28. * The query limit used for recursive deletes when fetching all descendants of
  29. * the specified reference to delete. This is done to prevent the query stream
  30. * from streaming documents faster than Firestore can delete.
  31. */
  32. export declare const RECURSIVE_DELETE_MAX_PENDING_OPS = 5000;
  33. /*!
  34. * The number of pending BulkWriter operations at which RecursiveDelete
  35. * starts the next limit query to fetch descendants. By starting the query
  36. * while there are pending operations, Firestore can improve BulkWriter
  37. * throughput. This helps prevent BulkWriter from idling while Firestore
  38. * fetches the next query.
  39. */
  40. export declare const RECURSIVE_DELETE_MIN_PENDING_OPS = 1000;
  41. /**
  42. * Class used to store state required for running a recursive delete operation.
  43. * Each recursive delete call should use a new instance of the class.
  44. * @private
  45. * @internal
  46. */
  47. export declare class RecursiveDelete {
  48. private readonly firestore;
  49. private readonly writer;
  50. private readonly ref;
  51. private readonly maxLimit;
  52. private readonly minLimit;
  53. /**
  54. * The number of deletes that failed with a permanent error.
  55. * @private
  56. * @internal
  57. */
  58. private errorCount;
  59. /**
  60. * The most recently thrown error. Used to populate the developer-facing
  61. * error message when the recursive delete operation completes.
  62. * @private
  63. * @internal
  64. */
  65. private lastError;
  66. /**
  67. * Whether there are still documents to delete that still need to be fetched.
  68. * @private
  69. * @internal
  70. */
  71. private documentsPending;
  72. /**
  73. * Whether run() has been called.
  74. * @private
  75. * @internal
  76. */
  77. private started;
  78. /**
  79. * Query limit to use when fetching all descendants.
  80. * @private
  81. * @internal
  82. */
  83. private readonly maxPendingOps;
  84. /**
  85. * The number of pending BulkWriter operations at which RecursiveDelete
  86. * starts the next limit query to fetch descendants.
  87. * @private
  88. * @internal
  89. */
  90. private readonly minPendingOps;
  91. /**
  92. * A deferred promise that resolves when the recursive delete operation
  93. * is completed.
  94. * @private
  95. * @internal
  96. */
  97. private readonly completionDeferred;
  98. /**
  99. * Whether a query stream is currently in progress. Only one stream can be
  100. * run at a time.
  101. * @private
  102. * @internal
  103. */
  104. private streamInProgress;
  105. /**
  106. * The last document snapshot returned by the stream. Used to set the
  107. * startAfter() field in the subsequent stream.
  108. * @private
  109. * @internal
  110. */
  111. private lastDocumentSnap;
  112. /**
  113. * The number of pending BulkWriter operations. Used to determine when the
  114. * next query can be run.
  115. * @private
  116. * @internal
  117. */
  118. private pendingOpsCount;
  119. private errorStack;
  120. /**
  121. *
  122. * @param firestore The Firestore instance to use.
  123. * @param writer The BulkWriter instance to use for delete operations.
  124. * @param ref The document or collection reference to recursively delete.
  125. * @param maxLimit The query limit to use when fetching descendants
  126. * @param minLimit The number of pending BulkWriter operations at which
  127. * RecursiveDelete starts the next limit query to fetch descendants.
  128. */
  129. constructor(firestore: Firestore, writer: BulkWriter, ref: firestore.CollectionReference<unknown> | firestore.DocumentReference<unknown>, maxLimit: number, minLimit: number);
  130. /**
  131. * Recursively deletes the reference provided in the class constructor.
  132. * Returns a promise that resolves when all descendants have been deleted, or
  133. * if an error occurs.
  134. */
  135. run(): Promise<void>;
  136. /**
  137. * Creates a query stream and attaches event handlers to it.
  138. * @private
  139. * @internal
  140. */
  141. private setupStream;
  142. /**
  143. * Retrieves all descendant documents nested under the provided reference.
  144. * @param ref The reference to fetch all descendants for.
  145. * @private
  146. * @internal
  147. * @return {Stream<QueryDocumentSnapshot>} Stream of descendant documents.
  148. */
  149. private getAllDescendants;
  150. /**
  151. * Called when all descendants of the provided reference have been streamed
  152. * or if a permanent error occurs during the stream. Deletes the developer
  153. * provided reference and wraps any errors that occurred.
  154. * @private
  155. * @internal
  156. */
  157. private onQueryEnd;
  158. /**
  159. * Deletes the provided reference and starts the next stream if conditions
  160. * are met.
  161. * @private
  162. * @internal
  163. */
  164. private deleteRef;
  165. private incrementErrorCount;
  166. }