@@ -41,18 +41,48 @@ function firestoreQueryParser(collectionRef, queryParams) {
41
41
} ;
42
42
43
43
function emptyCollection ( db , collection , callback ) {
44
+ console . log ( 'Emptying firestore collection' , collection ) ;
45
+ var batchSize = 300 ;
44
46
var collectionRef = db . collection ( collection ) ;
45
- var query = collectionRef . get ( ) . then ( function ( querySnapshot ) {
46
- var writeBatch = db . batch ( ) ;
47
- querySnapshot . forEach ( function ( documentSnapshot ) {
48
- var documentPath = collection + '/' + documentSnapshot . id ;
49
- var documentRef = db . doc ( documentPath ) ;
50
- writeBatch . delete ( documentRef ) ;
51
- } ) ;
52
- writeBatch . commit ( ) . then ( function ( ) {
53
- if ( callback ) callback ( null ) ;
47
+ var query = collectionRef . limit ( batchSize ) ;
48
+
49
+ console . log ( 'Calling deleteQueryBatch for collection' ) ;
50
+ return deleteQueryBatch ( db , query , batchSize , callback ) ;
51
+ }
52
+
53
+ function deleteQueryBatch ( db , query , batchSize , callback ) {
54
+ console . log ( 'In deleteQueryBatch. Get query results' ) ;
55
+ query . get ( )
56
+ . then ( ( snapshot ) => {
57
+ console . log ( 'Snapshot returned' ) ;
58
+ console . log ( 'Size' , snapshot . size ) ;
59
+ if ( snapshot . size == 0 ) {
60
+ console . log ( 'Resolving because size is 0' ) ;
61
+ return new Promise ( ( resolve ) => resolve ( 0 ) ) ;
62
+ }
63
+
64
+ console . log ( 'Setting up batch for deletion' ) ;
65
+ var batch = db . batch ( ) ;
66
+ console . log ( 'Setting up docs for deletion' ) ;
67
+ snapshot . docs . forEach ( ( doc ) => batch . delete ( doc . ref ) ) ;
68
+ console . log ( 'Starting deletion commit' ) ;
69
+ return batch . commit ( ) . then ( ( ) => {
70
+ console . log ( 'Batch committed. Returning size' , snapshot . size ) ;
71
+ return snapshot . size ;
72
+ } ) . then ( ( numDeleted ) => {
73
+ console . log ( 'Num deleted' , numDeleted ) ;
74
+ if ( numDeleted == 0 ) {
75
+ console . log ( 'Call callback as numDeleted == 0' ) ;
76
+ return callback ( ) ;
77
+ }
78
+
79
+ console . log ( 'Schedule next batch for deletion on next tick' ) ;
80
+ process . nextTick ( ( ) => {
81
+ console . log ( 'TICK! Execute next batch' ) ;
82
+ deleteQueryBatch ( db , query , batchSize , callback ) ;
83
+ } ) ;
84
+ } ) . catch ( ( e ) => { console . error ( 'Error on get' , e ) ; callback ( e ) } ) ;
54
85
} ) ;
55
- } ) ;
56
86
} ;
57
87
58
88
function getPrecondition ( vm ) {
@@ -263,14 +293,18 @@ _.extend(Firestore.prototype, {
263
293
} ,
264
294
265
295
clear : function ( callback ) {
296
+ console . log ( 'Entered clear' ) ;
266
297
this . checkConnection ( ) ;
267
298
268
299
var self = this ;
269
300
if ( ! this . collection ) {
301
+ console . log ( 'No collection set, just callback' ) ;
270
302
if ( callback ) callback ( null ) ;
271
303
return ;
272
304
}
273
305
306
+ console . log ( 'Calling emptyCollection for' , this . collection ) ;
307
+
274
308
emptyCollection ( this . db , this . collection , callback ) ;
275
309
} ,
276
310
0 commit comments