Twitter LogoFacebook Logo
Batch Operations in Firebase Cloud Firestore
[object Object]
By: King

Before you get started, you must add the FirebaseTS library to your project. Follow this tutorial to learn how to add FirebaseTS.

Once you have added FirebaseTS, you are set to go!

Batch Operations

In this section, we will be executing multiple Cloud Firestore operations using the all() method from the FirebaseTSFirestore Class.

Here is the definition of the all() method:

public all(
        params: {
            operations: BatchOperation<any>[],
            onComplete?: () => void,
            onFail?: (err: any) => void
        }
): Promise<void>;

The operations property is an array that stores a collection of operation objects call BatchOperation.

class BatchOperation <DT>{
    constructor(
          operation: "create" | "update", 
          path: string[], 
          data: DT);
    constructor(
          operation: "delete", 
          path: string[]);
}

It has 2 constructors, one with 2 parameters and another with 3 parameters.

To create and update a document, we will use the one with 3 parameters and to delete a document, we will use the one with 2 parameters.

Create BatchOperation

new BatchOperation (

   "create",
   ["COLLECTION", "DOC_ID"],
   {
       name : "",
       age : 10,
       hobbies: [
           "Soccer"
       ]
   }
);

Update BatchOperation

new BatchOperation (

   "update",
   ["COLLECTION", "DOC_ID"],
   {
       name : "Joe",
   }
);

Delete BatchOperation

new BatchOperation (

   "delete",
   ["COLLECTION", "DOC_ID"]
);

How to Use

1. Import the FirebaseTSFirestore class.

import FirebaseTSFirestore from 'firebasets/firebasetsFirestore/firebaseTSFirestore';

2. Declare and initialize a FirebaseTSFirestore object.

private firestoreFirebaseTSFirestore;

constructor(){
  this.firestore new FirebaseTSFirestore();
}

3. Call the all() method and add all the batch operations.

this.firestore.all(

   {
       operations: [
           new BatchOperation (
               "create",
               ["COLLECTION", "DOC_ID"],
               {
                   name : "",
                   age : 10,
                   hobbies: [
                       "Soccer"
                   ]
               }),
           new BatchOperation (
               "update",
               ["COLLECTION", "DOC_ID"],
               {
                   name : "Joe"
               }),
           new BatchOperation (
               "delete",
               ["COLLECTION", "DOC_ID"]
               )
       ]
   }
);

Handling code on complete or fail

Sometimes you may want to do something when you have successfully or unsuccessfully execute all the operations, like a notification message. You can add the optional parameters onComplete and onFail.

this.firestore.all(
 {
   operations: [ ... ],
   onComplete: () => {
      // Code gets executed when it was successful.
      alert("Data deleted!");
   },
   onFail: err => {
      // Code gets executed when it fails.
      alert(err.message);
   }
 }
);


Sign In