Skip to content

Commit f2e1e9b

Browse files
committed
Change clear collections to be a batch function
- Firebase only supports 500 changes as part of a batch, so this function needs to loop through larger collections.
1 parent adb9165 commit f2e1e9b

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

lib/databases/firestore.js

+44-10
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,48 @@ function firestoreQueryParser(collectionRef, queryParams) {
4141
};
4242

4343
function emptyCollection(db, collection, callback) {
44+
console.log('Emptying firestore collection', collection);
45+
var batchSize = 300;
4446
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)});
5485
});
55-
});
5686
};
5787

5888
function getPrecondition(vm) {
@@ -263,14 +293,18 @@ _.extend(Firestore.prototype, {
263293
},
264294

265295
clear: function (callback) {
296+
console.log('Entered clear');
266297
this.checkConnection();
267298

268299
var self = this;
269300
if (!this.collection) {
301+
console.log('No collection set, just callback');
270302
if (callback) callback(null);
271303
return;
272304
}
273305

306+
console.log('Calling emptyCollection for', this.collection);
307+
274308
emptyCollection(this.db, this.collection, callback);
275309
},
276310

0 commit comments

Comments
 (0)