@@ -16,8 +16,8 @@ export class FirestoreComponent {
1616
1717 private listenerUnsubscribe : ( ) => void ;
1818
19- public myCity$ : Observable < City > ;
20- public myCities$ : Observable < Array < City > > ;
19+ myCity$ : Observable < City > ;
20+ myCities$ : Observable < Array < City > > ;
2121
2222 private city : City ;
2323 private cities : Array < City > = [ ] ;
@@ -26,7 +26,7 @@ export class FirestoreComponent {
2626 // AngularFireModule.initializeApp({});
2727 }
2828
29- public issue854 ( ) : void {
29+ issue854 ( ) : void {
3030 const helloRef : firestore . DocumentReference =
3131 firebase . firestore ( )
3232 . collection ( "users" )
@@ -37,7 +37,7 @@ export class FirestoreComponent {
3737 helloRef . get ( ) . then ( snapshot => console . log ( snapshot . data ( ) ) )
3838 }
3939
40- public loginAnonymously ( ) : void {
40+ loginAnonymously ( ) : void {
4141 firebase . auth ( ) . signInAnonymously ( )
4242 . then ( ( ) => {
4343 const user = firebase . auth ( ) . currentUser ;
@@ -46,15 +46,15 @@ export class FirestoreComponent {
4646 . catch ( err => console . log ( "Login error: " + JSON . stringify ( err ) ) ) ;
4747 }
4848
49- public firestoreAdd ( ) : void {
49+ firestoreAdd ( ) : void {
5050 firebase . firestore ( ) . collection ( "dogs" ) . add ( { name : "Fido" } )
5151 . then ( ( docRef : firestore . DocumentReference ) => {
5252 console . log ( "Fido added, ref: " + docRef . id ) ;
5353 } )
5454 . catch ( err => console . log ( "Adding Fido failed, error: " + err ) ) ;
5555 }
5656
57- public firestoreSet ( ) : void {
57+ firestoreSet ( ) : void {
5858 firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
5959 . set ( {
6060 name : "Woofie" ,
@@ -125,14 +125,14 @@ export class FirestoreComponent {
125125 } ) ;
126126 }
127127
128- public firestoreSetByAutoID ( ) : void {
128+ firestoreSetByAutoID ( ) : void {
129129 firebase . firestore ( ) . collection ( "dogs" ) . doc ( )
130130 . set ( { name : "Woofie" , last : "lastofwoofie" , date : new Date ( ) } )
131131 . then ( ( ) => console . log ( "Woofie set" ) )
132132 . catch ( err => console . log ( "Setting Woofie failed, error: " + err ) ) ;
133133 }
134134
135- public firestoreUpdate ( ) : void {
135+ firestoreUpdate ( ) : void {
136136 // get a document reference so we can add a city reference to our favourite dog
137137 const sfDocRef : firestore . DocumentReference = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ;
138138
@@ -149,7 +149,7 @@ export class FirestoreComponent {
149149 . catch ( err => console . log ( "Updating Woofie failed, error: " + JSON . stringify ( err ) ) ) ;
150150 }
151151
152- public firestoreGet ( ) : void {
152+ firestoreGet ( ) : void {
153153 const collectionRef : firestore . CollectionReference = firebase . firestore ( ) . collection ( "dogs" ) ;
154154 collectionRef . get ( )
155155 . then ( ( querySnapshot : firestore . QuerySnapshot ) => {
@@ -174,7 +174,7 @@ export class FirestoreComponent {
174174 } ) ;
175175 }
176176
177- public firestoreGetNested ( ) : void {
177+ firestoreGetNested ( ) : void {
178178 const mainStreetInSFDocRef : firestore . DocumentReference =
179179 firebase . firestore ( )
180180 . collection ( "cities" )
@@ -194,6 +194,28 @@ export class FirestoreComponent {
194194 } ) ;
195195 }
196196
197+ arrayUnion ( ) : void {
198+ firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
199+ . update ( {
200+ last : "Updated From arrayUnion" ,
201+ updateTs : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) ,
202+ colors : firebase . firestore ( ) . FieldValue ( ) . arrayUnion ( [ "red" , "blue" ] )
203+ } )
204+ . then ( ( ) => console . log ( "Woofie updated from arrayUnion" ) )
205+ . catch ( err => console . log ( "Updating Woofie from arrayUnion failed, error: " + JSON . stringify ( err ) ) ) ;
206+ }
207+
208+ arrayRemove ( ) : void {
209+ firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
210+ . update ( {
211+ last : "Updated From arrayRemove" ,
212+ updateTs : firebase . firestore ( ) . FieldValue ( ) . serverTimestamp ( ) ,
213+ colors : firebase . firestore ( ) . FieldValue ( ) . arrayRemove ( [ "red" ] )
214+ } )
215+ . then ( ( ) => console . log ( "Woofie updated from arrayRemove" ) )
216+ . catch ( err => console . log ( "Updating Woofie from arrayRemove failed, error: " + JSON . stringify ( err ) ) ) ;
217+ }
218+
197219 firestoreDocumentObservable ( ) : void {
198220 this . myCity$ = Observable . create ( subscriber => {
199221 const docRef : firestore . DocumentReference = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ;
@@ -219,7 +241,7 @@ export class FirestoreComponent {
219241 } ) ;
220242 }
221243
222- public firestoreListen ( ) : void {
244+ firestoreListen ( ) : void {
223245 if ( this . listenerUnsubscribe !== undefined ) {
224246 console . log ( "Already listening ;)" ) ;
225247 return ;
@@ -236,7 +258,7 @@ export class FirestoreComponent {
236258 } ) ;
237259 }
238260
239- public firestoreStopListening ( ) : void {
261+ firestoreStopListening ( ) : void {
240262 if ( this . listenerUnsubscribe === undefined ) {
241263 console . log ( "Please start listening first ;)" ) ;
242264 return ;
@@ -246,7 +268,7 @@ export class FirestoreComponent {
246268 this . listenerUnsubscribe = undefined ;
247269 }
248270
249- public firestoreWhere ( ) : void {
271+ firestoreWhere ( ) : void {
250272 const cityDocRef = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ;
251273
252274 firebase . firestore ( ) . collection ( "dogs" )
@@ -260,7 +282,7 @@ export class FirestoreComponent {
260282 . catch ( err => console . log ( "Where-get failed, error: " + err ) ) ;
261283 }
262284
263- public firestoreWhereOrderLimit ( ) : void {
285+ firestoreWhereOrderLimit ( ) : void {
264286 const query : firestore . Query = firebase . firestore ( ) . collection ( "cities" )
265287 . where ( "state" , "==" , "CA" )
266288 . where ( "population" , "<" , 99999999 )
@@ -277,7 +299,7 @@ export class FirestoreComponent {
277299 . catch ( err => console . log ( "firestoreWhereOrderLimit failed, error: " + err ) ) ;
278300 }
279301
280- public firestoreWhereCityHasALake ( ) : void {
302+ firestoreWhereCityHasALake ( ) : void {
281303 const query : firestore . Query = firebase . firestore ( ) . collection ( "cities" )
282304 . where ( "landmarks" , "array-contains" , "lake" ) ;
283305
@@ -291,7 +313,7 @@ export class FirestoreComponent {
291313 . catch ( err => console . log ( "firestoreWhereCityHasALake failed, error: " + err ) ) ;
292314 }
293315
294- public firestoreDelete ( ) : void {
316+ firestoreDelete ( ) : void {
295317 firebase . firestore ( ) . collection ( "dogs" ) . doc ( "fave" )
296318 . delete ( )
297319 . then ( ( ) => {
@@ -300,15 +322,15 @@ export class FirestoreComponent {
300322 . catch ( err => console . log ( "Delete failed, error: " + err ) ) ;
301323 }
302324
303- public doWebGetValueForCompanies ( ) : void {
325+ doWebGetValueForCompanies ( ) : void {
304326 const path = "/companies" ;
305327 firebase . database ( ) . ref ( path )
306328 . once ( "value" )
307329 . then ( result => console . log ( `${ result . key } => ${ JSON . stringify ( result . val ( ) ) } ` ) )
308330 . catch ( error => console . log ( "doWebGetValueForCompanies error: " + error ) ) ;
309331 }
310332
311- public writeBatch ( ) : void {
333+ writeBatch ( ) : void {
312334 // one batch can update multiple docs
313335 const sfDocRef : firestore . DocumentReference = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ;
314336 const sacDocRef : firestore . DocumentReference = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SAC" ) ;
@@ -323,7 +345,7 @@ export class FirestoreComponent {
323345 . catch ( error => console . log ( "Batch error: " + error ) ) ;
324346 }
325347
326- public transactionalUpdate ( ) : void {
348+ transactionalUpdate ( ) : void {
327349 const sfDocRef : firestore . DocumentReference = firebase . firestore ( ) . collection ( "cities" ) . doc ( "SF" ) ;
328350
329351 firebase . firestore ( ) . runTransaction ( transaction => {
@@ -345,29 +367,29 @@ export class FirestoreComponent {
345367 . catch ( error => console . log ( "doTransaction error: " + error ) ) ;
346368 }
347369
348- public firestoreStartAt ( ) : void {
370+ firestoreStartAt ( ) : void {
349371 firebase . firestore ( ) . collection ( 'cities' )
350- . doc ( 'LA' )
351- . get ( )
352- . then ( doc => {
353- firebase . firestore ( ) . collection ( 'cities' )
354- . orderBy ( 'name' , 'asc' )
355- . startAt ( doc )
356- . get ( )
357- . then ( snap => snap . forEach ( doc => console . log ( doc . id , doc . data ( ) ) ) ) ;
358- } ) ;
372+ . doc ( 'LA' )
373+ . get ( )
374+ . then ( doc => {
375+ firebase . firestore ( ) . collection ( 'cities' )
376+ . orderBy ( 'name' , 'asc' )
377+ . startAt ( doc )
378+ . get ( )
379+ . then ( snap => snap . forEach ( doc => console . log ( doc . id , doc . data ( ) ) ) ) ;
380+ } ) ;
359381 }
360382
361- public firestoreStartAfter ( ) : void {
383+ firestoreStartAfter ( ) : void {
362384 firebase . firestore ( ) . collection ( 'cities' )
363- . doc ( 'LA' )
364- . get ( )
365- . then ( doc => {
366- firebase . firestore ( ) . collection ( 'cities' )
367- . orderBy ( 'name' , 'asc' )
368- . startAfter ( doc )
369- . get ( )
370- . then ( snap => snap . forEach ( doc => console . log ( doc . id , doc . data ( ) ) ) ) ;
371- } ) ;
385+ . doc ( 'LA' )
386+ . get ( )
387+ . then ( doc => {
388+ firebase . firestore ( ) . collection ( 'cities' )
389+ . orderBy ( 'name' , 'asc' )
390+ . startAfter ( doc )
391+ . get ( )
392+ . then ( snap => snap . forEach ( doc => console . log ( doc . id , doc . data ( ) ) ) ) ;
393+ } ) ;
372394 }
373395}
0 commit comments