Skip to content

Commit 40ccc0f

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 40ccc0f

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

lib/databases/firestore.js

+27-11
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,34 @@ function firestoreQueryParser(collectionRef, queryParams) {
4141
};
4242

4343
function emptyCollection(db, collection, callback) {
44+
var batchSize = 300;
4445
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);
54-
});
55-
});
46+
var query = collectionRef.limit(batchSize);
47+
48+
return deleteQueryBatch(db, query, batchSize, callback);
49+
}
50+
51+
function deleteQueryBatch(db, query, batchSize, callback) {
52+
query.get()
53+
.then((snapshot) => {
54+
if (snapshot.size == 0) {
55+
return 0;
56+
}
57+
58+
var batch = db.batch();
59+
snapshot.docs.forEach((doc) => batch.delete(doc.ref));
60+
return batch.commit().then(() => {
61+
return snapshot.size;
62+
});
63+
}).then((numDeleted) => {
64+
if (numDeleted == 0) {
65+
return callback();
66+
}
67+
68+
process.nextTick(() => {
69+
deleteQueryBatch(db, query, batchSize, callback);
70+
});
71+
}).catch(callback);
5672
};
5773

5874
function getPrecondition(vm) {

0 commit comments

Comments
 (0)