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_encode_decode' ; class User { constructor ( public id , public name , public created , public updated ? ) { } } const main = async ( ) => { const firestoreSimple = new FirestoreSimple ( firestore ) ; const dao = firestoreSimple . collection < User > ( { path : `${ ROOT_PATH }/user` , encode : ( user ) => { return { name : user . name , created : user . created , updated : admin . firestore . FieldValue . serverTimestamp ( ) } ; } , decode : ( doc ) => { return new User ( doc . id , doc . name , doc . created . toDate ( ) , doc . updated . toDate ( ) ) ; } } ) ; let user ; const userId = await dao . add ( { name : 'bob' , created : new Date ( ) } ) ; user = await dao . fetch ( userId ) ; console . log ( user ) ; if ( ! user ) return ; user . name = 'alice' ; await dao . set ( user ) ; user = await dao . fetch ( userId ) ; console . log ( user ) ; if ( ! user ) return ; await dao . delete ( userId ) ; } ; main ( ) ;