import admin from 'firebase-admin' ; import { FirestoreSimple } from '../src' ; process . env . GCLOUD_PROJECT = 'firestore-simple-test' ; process . env . FIRESTORE_EMULATOR_HOST = 'localhost:8080' ; admin . initializeApp ( { } ) ; const firestore = admin . firestore ( ) ; const ROOT_PATH = 'example/admin_batch' ; interface User { id : string ; name : string ; rank : number ; } const userNames = [ 'bob' , 'alice' , 'john' , 'meary' , 'king' ] ; const main = async ( ) : Promise < void > => { const firestoreSimple = new FirestoreSimple ( firestore ) ; const userDao = firestoreSimple . collection < User > ( { path : `${ ROOT_PATH }/user` } ) ; await firestoreSimple . runBatch ( async ( _batch ) => { let rank = 1 ; for ( const name of userNames ) { await userDao . add ( { name , rank } ) ; rank += 1 ; } console . dir ( await userDao . fetchAll ( ) ) ; } ) ; let users = await userDao . orderBy ( 'rank' ) . fetch ( ) ; console . dir ( users ) ; await firestoreSimple . runBatch ( async ( _batch ) => { let rank = 0 ; for ( const user of users ) { if ( user . rank < 4 ) { await userDao . update ( { id : user . id , rank } ) ; } else { await userDao . delete ( user . id ) ; } rank += 1 ; } } ) ; users = await userDao . orderBy ( 'rank' ) . fetch ( ) ; console . dir ( users ) ; await userDao . bulkDelete ( users . map ( ( user ) => user . id ) ) ; } ; main ( ) ;