Last active
November 15, 2019 15:42
-
-
Save koptionalsoftware/f554634707058e6315e6efd5d0226fa2 to your computer and use it in GitHub Desktop.
Clears everything recursively in your Firestore database. Useful in the cleanUp routine of any tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const clearCollection = db => async collections => { | |
await Promise.all( | |
collections.map(async collection => { | |
// get documents | |
const documents = await collection.listDocuments(); | |
await Promise.all( | |
documents.map(async doc => { | |
const collections = await db.doc(`${doc.path}`).listCollections(); | |
// If document has a sub collection delete that first | |
if (collections.length > 0) { | |
await clearCollection(db)(collections); | |
} | |
await doc.delete(); | |
}) | |
); | |
}) | |
); | |
}; | |
const clearDB = async db => { | |
// get root collections | |
const collections = await db.listCollections(); | |
// delete root collections | |
return clearCollection(db)(collections); | |
}; | |
module.exports = {clearDB, clearCollection}; | |
// clearDB(admin.firestore()) will delete everything in your database | |
// clearCollection(admin.firestore())(collectionRef) will delete everything within a collection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment