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_subcollection' ; interface User { id : string ; name : string ; } interface UserFriend { id : string ; name : string ; created : Date ; } const userNames = [ 'alice' , 'bob' , 'john' ] ; const main = async ( ) => { const firestoreSimple = new FirestoreSimple ( firestore ) ; const userDao = firestoreSimple . collection < User > ( { path : `${ ROOT_PATH }/user` } ) ; await userDao . bulkSet ( userNames . map ( ( name ) => { return { id : name , name } ; } ) ) ; const userFriendFactory = firestoreSimple . collectionFactory < UserFriend > ( { decode : ( doc ) => { return { id : doc . id , name : doc . name , created : doc . created . toDate ( ) } ; } } ) ; const users = await userDao . fetchAll ( ) ; for ( const user of users ) { const userFriendDao = userFriendFactory . create ( `${ ROOT_PATH }/user/${ user . id }/friend` ) ; const friendNames = userNames . filter ( ( name ) => name !== user . name ) ; await userFriendDao . bulkSet ( friendNames . map ( ( name ) => { return { id : name , name , created : new Date ( ) } ; } ) ) ; const friends = await userFriendDao . fetchAll ( ) ; console . log ( user . id , friends ) ; } for ( const user of users ) { const userFriendDao = userFriendFactory . create ( `${ ROOT_PATH }/user/${ user . id }/friend` ) ; const friends = await userFriendDao . fetchAll ( ) ; await userDao . bulkDelete ( friends . map ( ( friend ) => friend . id ) ) ; } await userDao . bulkDelete ( users . map ( ( user ) => user . id ) ) ; } ; main ( ) ;