Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angularfire2 transactions and batch writes in firestore

How can I do batch writes/run transactions with firestore in an angular2 app?

https://firebase.google.com/docs/firestore/manage-data/transactions

If it's possible, how can I convert the JS code to TS code in an angular2 app.

like image 608
BOOnZ Avatar asked Nov 13 '17 15:11

BOOnZ


People also ask

What is the maximum documents that are write by per transaction or batch of write in cloud firestore?

Each transaction or batch of writes can write to a maximum of 500 documents.

What is transactional data in Firebase?

Transactional data is used when you need to return some data from the database then make some calculation with it and store it back. Let us say we have one player inside our player list. We want to retrieve property, add one year of age and return it back to Firebase.


1 Answers

It's simple:

constructor(private db: AngularFirestore){}
    
inserting(){
        //--create batch-- 
        let batch = this.db.firestore.batch();
        
        //--create a reference-- 
        const userRef = this.db.collection('users').doc('women').ref;
        batch.set(userRef , {
          name: "Maria"
        });
        
        //--create a reference-- 
        const userRef = this.db.collection('users').doc('men').ref;
        batch.set(userRef , {
          name: "Diego"
        });
        
        //--Others more 54 batch's--
        
        //--finally--
        return batch.commit();
    }
like image 165
Diego Venâncio Avatar answered Sep 23 '22 07:09

Diego Venâncio