/
resetDatabase.js
44 lines (37 loc) · 1.04 KB
/
resetDatabase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { db } from './src/firebase'
function deleteCollection(collectionName) {
let collectionRef = db.collection(collectionName);
let query = collectionRef.orderBy('__name__');
return new Promise((resolve, reject) => {
deleteQueryBatch(db, query, resolve, reject);
});
}
function deleteQueryBatch(db, query, resolve, reject) {
query.get()
.then((snapshot) => {
// When there are no documents left, we are done
if (snapshot.size === 0) {
return 0;
}
// Delete documents in a batch
let batch = db.batch();
snapshot.docs.forEach((doc) => {
batch.delete(doc.ref);
});
return batch.commit().then(() => {
return snapshot.size;
});
}).then((numDeleted) => {
if (numDeleted === 0) {
resolve();
return;
}
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(db, query, resolve, reject);
});
})
.catch(reject);
}
export default deleteCollection