import { Component , NgZone } from "@angular/core" ; import { firestore } from "nativescript-plugin-firebase" ; import { Observable } from "rxjs" ; import { City } from "~/model/City" ; const firebase = require ( "nativescript-plugin-firebase/app" ) ; @ Component ( { selector : "firestore" , moduleId : module . id , templateUrl : "./firestore.component.html" , } ) export class FirestoreComponent { private listenerUnsubscribe : ( ) => void ; myCity$ : Observable < City > ; myCities$ : Observable < Array < City > > ; private city : City ; private cities : Array < City > = [ ] ; constructor ( private zone ) { } issue854 ( ) { const helloRef = firebase . firestore ( ) . collection ( "users" ) . doc ( firebase . auth ( ) . currentUser . uid ) . collection ( "availability" ) . doc ( "hello" ) ; helloRef . get ( { source : "cache" } ) . then ( snapshot => console . log ( snapshot . data ( ) ) ) . catch ( err => console . log ( "issue854 err: " + err ) ) ; } loginAnonymously ( ) { firebase . auth ( ) . signInAnonymously ( ) . then ( ( ) => { const user = firebase . auth ( ) . currentUser ; firebase . firestore ( ) . collection ( "users" ) . doc ( user . uid ) . set ( user ) ; } ) . catch ( err => console . log ( "Login error: " + JSON . stringify ( err ) ) ) ; } firestoreAdd ( ) { firebase . firestore ( ) . collection ( "dogs" ) . add ( { name : "Fido" } ) . then ( ( docRef ) => { console . log ( "Fido added, ref: " + docRef . id ) ; } ) . catch ( err => console . log ( "Adding Fido failed, error: " + err ) ) ; } firestoreSet ( ) { firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" ) . set ( { name : "Woofie" , last : "lastofwoofie" , lastKnownLocation : firebase . firestore ( ) . GeoPoint ( 5.34 , 6.67 ) , lastKnownLocation2 : firestore . GeoPoint ( 5.34 , 6.67 ) } , { merge : true } ) . then ( ( ) => console . log ( "Woofie set" ) ) . catch ( err => console . log ( "Setting Woofie failed, error: " + err ) ) ; const citiesCollection = firebase . firestore ( ) . collection ( "cities" ) ; citiesCollection . doc ( "SF" ) . set ( { name : "San Francisco" , state : "CA" , country : "USA" , capital : false , population : 860000 } ) ; citiesCollection . doc ( "LA" ) . set ( { name : "Los Angeles" , state : "CA" , country : "USA" , capital : false , population : 3900000 , landmarks : [ "lake" , "mountain" ] } ) ; citiesCollection . doc ( "SAC" ) . set ( { name : "Sacramento" , state : "CA" , country : "USA" , capital : true , population : 500000 , landmarks : [ "mountain" ] } ) ; citiesCollection . doc ( "DC" ) . set ( { name : "Washington, D.C." , state : "WA" , country : "USA" , capital : true , population : 680000 , landmarks : [ "airport" , "lake" ] } ) ; citiesCollection . doc ( "TOK" ) . set ( { name : "Tokyo" , state : null , country : "Japan" , capital : true , population : 9000000 } ) ; citiesCollection . doc ( "BJ" ) . set ( { name : "Beijing" , state : null , country : "China" , capital : true , population : 21500000 } ) ; } firestoreSetByAutoID ( ) { firebase . firestore ( ) . collection ( "dogs" ) . doc ( ) . set ( { name : "Woofie" , last : "lastofwoofie" , date : new Date ( ) } ) . then ( ( ) => console . log ( "Woofie set" ) ) . catch ( err => console . log ( "Setting Woofie failed, error: " + err ) ) ; } firestoreUpdate ( ) { const sfDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ; firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" ) . update ( { name : "Woofieupdate" , last : "updatedwoofie!" , nested : { nestedFoo : "bar" , nestedUpdateTs : firestore . FieldValue . serverTimestamp ( ) , nestedLastKnownLocation : firebase . firestore ( ) . GeoPoint ( 4.34 , 5.67 ) , nestedLastKnownLocation2 : firestore . GeoPoint ( 4.34 , 5.67 ) } , city : sfDocRef , updateTs : firestore . FieldValue . serverTimestamp ( ) , updateTsAlt : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) , lastKnownLocation : firebase . firestore ( ) . GeoPoint ( 4.34 , 5.67 ) , lastKnownLocation2 : firestore . GeoPoint ( 4.34 , 5.67 ) } ) . then ( ( ) => console . log ( "Woofie updated" ) ) . catch ( err => console . log ( "Updating Woofie failed, error: " + JSON . stringify ( err ) ) ) ; } firestoreGet ( ) { const collectionRef = firebase . firestore ( ) . collection ( "dogs" ) ; console . log ( ">> collectionRef.parent: " + collectionRef . parent ) ; collectionRef . get ( { source : "server" } ) . then ( ( querySnapshot ) => { querySnapshot . forEach ( doc => console . log ( `${ doc . id } => ${ JSON . stringify ( doc . data ( ) ) }` ) ) ; } ) . catch ( err => console . log ( "Get failed, error: " + err ) ) ; const bjDistrictsRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "BJ" ) . collection ( "districts" ) ; console . log ( ">> bjDistrictsRef.parent.id: " + bjDistrictsRef . parent . id ) ; const docRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "BJ" ) ; console . log ( ">> docRef.parent.id: " + docRef . parent . id ) ; docRef . get ( ) . then ( ( doc ) => { if ( doc . exists ) { console . log ( "Document data:" , JSON . stringify ( doc . data ( ) ) ) ; const docRef = doc . data ( ) . referenceToCitiesDC ; console . log ( ">> docRef2.parent.id: " + docRef . parent . id ) ; docRef . get ( ) . then ( res => console . log ( "docref.get: " + JSON . stringify ( res . data ( ) ) ) ) . catch ( err => console . log ( "docref.get error: " + err ) ) ; } else { console . log ( "No such document!" ) ; } } ) . catch ( error => console . log ( "Error getting document:" , error ) ) ; } firestoreGetNested ( ) { const mainStreetInSFDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) . collection ( "streets" ) . doc ( "QZNrg22tkN8W71YC3qCb" ) ; mainStreetInSFDocRef . get ( ) . then ( ( doc ) => { if ( doc . exists ) { console . log ( "Document data:" , JSON . stringify ( doc . data ( ) ) ) ; } else { console . log ( "No such document!" ) ; } } ) . catch ( error => console . log ( "Error getting document:" , error ) ) ; } deleteFields ( ) { const faveDog = firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" ) ; faveDog . update ( { field1ToDelete : "foo" , field2ToDelete : "bar" , updateTs : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) } ) . then ( ( ) => { setTimeout ( ( ) => { faveDog . update ( { last : "Updated From 'delete'" , field1ToDelete : firestore . FieldValue . delete ( ) , field2ToDelete : firestore . FieldValue . delete ( ) , updateTs : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) } ) . then ( ( ) => console . log ( "Woofie updated from 'delete'" ) ) . catch ( err => console . log ( "Updating Woofie from 'delete' failed, error: " + JSON . stringify ( err ) ) ) ; } , 2000 ) ; } ) . catch ( err => console . log ( "deleteFields error: " + err ) ) ; } arrayUnion ( ) { firestore . collection ( "dogs" ) . doc ( "fave" ) . update ( { last : "Updated From 'arrayUnion'" , fieldToDelete : firestore . FieldValue . delete ( ) , updateTs : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) , colors : firestore . FieldValue . arrayUnion ( "red" , "blue" ) , ageInt : firebase . firestore ( ) . FieldValue ( ) . increment ( 2 ) , messages : firebase . firestore ( ) . FieldValue ( ) . arrayUnion ( { message : "Test 1" , source : "central" , time : Date . now ( ) } ) } ) . then ( ( ) => console . log ( "Woofie updated from 'arrayUnion'" ) ) . catch ( err => console . log ( "Updating Woofie from 'arrayUnion' failed, error: " + JSON . stringify ( err ) ) ) ; } arrayRemove ( ) { firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" ) . update ( { last : "Updated From 'arrayRemove'" , updateTs : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) , colors : firestore . FieldValue . arrayUnion ( "red" ) , ageDouble : firebase . firestore ( ) . FieldValue ( ) . increment ( 1.1 ) , messages : firebase . firestore ( ) . FieldValue ( ) . arrayRemove ( { message : "Test 1" , source : "central" , time : Date . now ( ) } ) } ) . then ( ( ) => console . log ( "Woofie updated from 'arrayRemove'" ) ) . catch ( err => console . log ( "Updating Woofie from 'arrayRemove' failed, error: " + JSON . stringify ( err ) ) ) ; } firestoreDocumentObservable ( ) { this . myCity$ = new Observable ( subscriber => { const docRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ; docRef . onSnapshot ( { includeMetadataChanges : true } , ( doc ) => { const source = doc . metadata . fromCache ? "local cache" : "server" ; console . log ( "Data came from " + source ) ; console . log ( "Has pending writes? " + doc . metadata . hasPendingWrites ) ; this . zone . run ( ( ) => { this . city = < City > doc . data ( ) ; console . log ( "City name: " + this . city . name ) ; subscriber . next ( this . city ) ; } ) ; } ) ; } ) ; } firestoreCollectionObservable ( ) { this . myCities$ = new Observable ( subscriber => { const colRef = firebase . firestore ( ) . collection ( "cities" ) ; colRef . onSnapshot ( { includeMetadataChanges : true } , ( snapshot ) => { const source = snapshot . metadata . fromCache ? "local cache" : "server" ; console . log ( "Data came from " + source ) ; console . log ( "Has pending writes? " + snapshot . metadata . hasPendingWrites ) ; this . zone . run ( ( ) => { this . cities = [ ] ; snapshot . forEach ( docSnap => this . cities . push ( < City > docSnap . data ( ) ) ) ; subscriber . next ( this . cities ) ; } ) ; } ) ; } ) ; } firestoreListen ( ) { if ( this . listenerUnsubscribe !== undefined ) { console . log ( "Already listening ;)" ) ; return ; } const docRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ; this . listenerUnsubscribe = docRef . onSnapshot ( ( doc ) => { if ( doc . exists ) { console . log ( "Document data:" , JSON . stringify ( doc . data ( ) ) ) ; } else { console . log ( "No such document!" ) ; } } ) ; } firestoreStopListening ( ) { if ( this . listenerUnsubscribe === undefined ) { console . log ( "Please start listening first ;)" ) ; return ; } this . listenerUnsubscribe ( ) ; this . listenerUnsubscribe = undefined ; } firestoreWhere ( ) { const cityDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ; firebase . firestore ( ) . collection ( "dogs" ) . where ( "city" , "==" , cityDocRef ) . get ( ) . then ( ( querySnapshot ) => { querySnapshot . forEach ( doc => { console . log ( `Relatively small Californian city: ${ doc . id } => ${ JSON . stringify ( doc . data ( ) ) }` ) ; } ) ; } ) . catch ( err => console . log ( "Where-get failed, error: " + err ) ) ; } firestoreWhereOrderLimit ( ) { const query = firebase . firestore ( ) . collection ( "cities" ) . where ( "state" , "in" , [ "CA" , "WA" ] ) . where ( "population" , "<" , 99999999 ) . orderBy ( "population" , "desc" ) . limit ( 4 ) ; query . get ( ) . then ( ( querySnapshot ) => { querySnapshot . forEach ( doc => { console . log ( `Large CA/WA city: ${ doc . id } => ${ JSON . stringify ( doc . data ( ) ) }` ) ; } ) ; } ) . catch ( err => console . log ( "firestoreWhereOrderLimit failed, error: " + err ) ) ; } firestoreWhereCityHasALakeAndOrMountain ( ) { const query = firebase . firestore ( ) . collection ( "cities" ) . where ( "landmarks" , "array-contains-any" , [ "mountain" , "lake" ] ) ; query . get ( ) . then ( ( querySnapshot ) => { querySnapshot . forEach ( doc => { console . log ( `city with a lake: ${ doc . id } => ${ JSON . stringify ( doc . data ( ) ) }` ) ; } ) ; } ) . catch ( err => console . log ( "firestoreWhereCityHasALake failed, error: " + err ) ) ; } firestoreDelete ( ) { firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" ) . delete ( ) . then ( ( ) => { console . log ( "Woofie deleted" ) ; } ) . catch ( err => console . log ( "Delete failed, error: " + err ) ) ; } firestoreCollectionGroupQuery ( ) { firebase . firestore ( ) . collectionGroup ( "cities" ) . where ( "population" , ">=" , 1000000 ) . get ( ) . then ( querySnapshot => { querySnapshot . forEach ( doc => { console . log ( `City with >= 1M population: ${ doc . id } => ${ JSON . stringify ( doc . data ( ) ) }` ) ; } ) ; } ) . catch ( err => console . log ( "Querying collection group failed, error: " + err ) ) ; } doWebGetValueForCompanies ( ) { const path = "/companies" ; firebase . database ( ) . ref ( path ) . once ( "value" ) . then ( result => console . log ( `${ result . key } => ${ JSON . stringify ( result . val ( ) ) }` ) ) . catch ( error => console . log ( "doWebGetValueForCompanies error: " + error ) ) ; } writeBatch ( ) { const sfDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ; const sacDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SAC" ) ; firebase . firestore ( ) . batch ( ) . set ( sfDocRef , { capital : false } , { merge : true } ) . update ( sfDocRef , { population : 860100 } ) . update ( sacDocRef , { population : 6500100 } ) . commit ( ) . then ( ( ) => console . log ( `Batch successfully committed` ) ) . catch ( error => console . log ( "Batch error: " + error ) ) ; } transactionalUpdate ( ) { const sfDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ; firebase . firestore ( ) . runTransaction ( transaction => { const sfDoc = transaction . get ( sfDocRef ) ; if ( ! sfDoc . exists ) { console . log ( "City SF doesn't exist" ) ; } else { const newPopulation = sfDoc . data ( ) . population + 1 ; console . log ( `Updating city 'SF' to a new population of: ${ newPopulation }, and flipping the 'capital' state to ${ sfDoc . data ( ) . capital }.` ) ; transaction . set ( sfDocRef , { capital : ! sfDoc . data ( ) . capital } , { merge : true } ) . update ( sfDocRef , { population : newPopulation } ) ; } return null ; } ) . then ( ( ) => console . log ( `Transaction successfully committed` ) ) . catch ( error => console . log ( "doTransaction error: " + error ) ) ; } firestoreStartAt ( ) { firebase . firestore ( ) . collection ( 'cities' ) . doc ( 'LA' ) . get ( ) . then ( doc => { firebase . firestore ( ) . collection ( 'cities' ) . orderBy ( 'name' , 'asc' ) . startAt ( doc ) . get ( ) . then ( snap => snap . forEach ( doc => console . log ( doc . id , doc . data ( ) ) ) ) ; } ) ; } firestoreStartAfter ( ) { firebase . firestore ( ) . collection ( 'cities' ) . doc ( 'LA' ) . get ( ) . then ( doc => { firebase . firestore ( ) . collection ( 'cities' ) . orderBy ( 'name' , 'asc' ) . startAfter ( doc ) . get ( ) . then ( snap => snap . forEach ( doc => console . log ( doc . id , doc . data ( ) ) ) ) ; } ) ; } }