import { batchExecutor , isLikeDocument , isRootOfDatabase , safelyGetCollectionsSnapshot , safelyGetDocumentReferences , } from './firestore-helpers' ; import * as admin from 'firebase-admin' ; import DocumentReference = FirebaseFirestore . DocumentReference ; const clearData = async ( startingRef , logs = false ) => { if ( isLikeDocument ( startingRef ) ) { const promises = [ clearCollections ( startingRef , logs ) ] ; if ( ! isRootOfDatabase ( startingRef ) ) { promises . push ( startingRef . delete ( ) as Promise < any > ) ; } return Promise . all ( promises ) ; } else { return clearDocuments ( < FirebaseFirestore . CollectionReference > startingRef , logs ) ; } } ; const clearCollections = async ( startingRef , logs = false ) => { const collectionPromises = [ ] ; const collectionsSnapshot = await safelyGetCollectionsSnapshot ( startingRef , logs ) ; collectionsSnapshot . map ( ( collectionRef ) => { collectionPromises . push ( clearDocuments ( collectionRef , logs ) ) ; } ) ; return batchExecutor ( collectionPromises ) ; } ; const clearDocuments = async ( collectionRef , logs = false ) => { logs && console . log ( `Retrieving documents from ${ collectionRef . path }` ) ; const allDocuments = await safelyGetDocumentReferences ( collectionRef , logs ) ; const documentPromises = [ ] ; allDocuments . forEach ( ( docRef ) => { documentPromises . push ( clearCollections ( docRef , logs ) ) ; documentPromises . push ( docRef . delete ( ) ) ; } ) ; return batchExecutor ( documentPromises ) ; } ; export default clearData ;