Class WriteBatch (7.3.1)

A Firestore WriteBatch that can be used to atomically commit multiple write operations at once.

WriteBatch

Package

@google-cloud/firestore

Constructors

(constructor)(firestore)

  constructor 
 ( 
 firestore 
 : 
  
 Firestore 
 ); 
 

Constructs a new instance of the WriteBatch class

Parameter
Name
Description
firestore

Methods

commit()

  commit 
 () 
 : 
  
 Promise<WriteResult 
 []>; 
 

Atomically commits all pending operations to the database and verifies all preconditions. Fails the entire write if any precondition is not met.

Returns
Type
Description
Promise < WriteResult []>

{Promise.<Array.

Example
  let 
  
 writeBatch 
  
 = 
  
 firestore 
 . 
 batch 
 (); 
 let 
  
 documentRef 
  
 = 
  
 firestore 
 . 
 doc 
 ( 
 'col/doc' 
 ); 
 writeBatch 
 . 
 set 
 ( 
 documentRef 
 , 
  
 { 
 foo 
 : 
  
 'bar' 
 }); 
 writeBatch 
 . 
 commit 
 (). 
 then 
 (() 
  
 = 
>  
 { 
  
 console 
 . 
 log 
 ( 
 'Successfully executed batch.' 
 ); 
 }); 
 

create(documentRef, data)

  create<AppModelType 
 , 
  
 DbModelType 
  
 extends 
  
 firestore 
 . 
 DocumentData 
> ( 
 documentRef 
 : 
  
 firestore 
 . 
 DocumentReference<AppModelType 
 , 
  
 DbModelType 
> , 
  
 data 
 : 
  
 firestore 
 . 
 WithFieldValue<AppModelType> 
 ) 
 : 
  
 WriteBatch 
 ; 
 

Create a document with the provided object values. This will fail the batch if a document exists at its location.

Parameters
Name
Description
documentRef
FirebaseFirestore.DocumentReference <AppModelType, DbModelType>

A reference to the document to be created.

data
FirebaseFirestore.WithFieldValue <AppModelType>

The object to serialize as the document.

Returns
Type
Description

{WriteBatch} This WriteBatch instance. Used for chaining method calls.

Type Parameters
Name
Description
AppModelType
DbModelType
Example
  let 
  
 writeBatch 
  
 = 
  
 firestore 
 . 
 batch 
 (); 
 let 
  
 documentRef 
  
 = 
  
 firestore 
 . 
 collection 
 ( 
 'col' 
 ). 
 doc 
 (); 
 writeBatch 
 . 
 create 
 ( 
 documentRef 
 , 
  
 { 
 foo 
 : 
  
 'bar' 
 }); 
 writeBatch 
 . 
 commit 
 (). 
 then 
 (() 
  
 = 
>  
 { 
  
 console 
 . 
 log 
 ( 
 'Successfully executed batch.' 
 ); 
 }); 
 

delete(documentRef, precondition)

  delete 
 ( 
 documentRef 
 : 
  
 firestore 
 . 
 DocumentReference<any 
 , 
  
 any 
> , 
  
 precondition 
 ?: 
  
 firestore 
 . 
 Precondition 
 ) 
 : 
  
 WriteBatch 
 ; 
 

Deletes a document from the database.

Parameters
Name
Description
documentRef
FirebaseFirestore.DocumentReference <any, any>

A reference to the document to be deleted.

precondition
firestore.Precondition

A precondition to enforce for this delete.

Returns
Type
Description

{WriteBatch} This WriteBatch instance. Used for chaining method calls.

Example
  let 
  
 writeBatch 
  
 = 
  
 firestore 
 . 
 batch 
 (); 
 let 
  
 documentRef 
  
 = 
  
 firestore 
 . 
 doc 
 ( 
 'col/doc' 
 ); 
 writeBatch 
 . 
 delete 
 ( 
 documentRef 
 ); 
 writeBatch 
 . 
 commit 
 (). 
 then 
 (() 
  
 = 
>  
 { 
  
 console 
 . 
 log 
 ( 
 'Successfully executed batch.' 
 ); 
 }); 
 

set(documentRef, data, options)

  set<AppModelType 
 , 
  
 DbModelType 
  
 extends 
  
 firestore 
 . 
 DocumentData 
> ( 
 documentRef 
 : 
  
 firestore 
 . 
 DocumentReference<AppModelType 
 , 
  
 DbModelType 
> , 
  
 data 
 : 
  
 firestore 
 . 
 PartialWithFieldValue<AppModelType> 
 , 
  
 options 
 : 
  
 firestore 
 . 
 SetOptions 
 ) 
 : 
  
 WriteBatch 
 ; 
 
Parameters
Name
Description
documentRef
FirebaseFirestore.DocumentReference <AppModelType, DbModelType>
data
FirebaseFirestore.PartialWithFieldValue <AppModelType>
options
firestore.SetOptions
Returns
Type
Description
Type Parameters
Name
Description
AppModelType
DbModelType

set(documentRef, data)

  set<AppModelType 
 , 
  
 DbModelType 
  
 extends 
  
 firestore 
 . 
 DocumentData 
> ( 
 documentRef 
 : 
  
 firestore 
 . 
 DocumentReference<AppModelType 
 , 
  
 DbModelType 
> , 
  
 data 
 : 
  
 firestore 
 . 
 WithFieldValue<AppModelType> 
 ) 
 : 
  
 WriteBatch 
 ; 
 
Parameters
Name
Description
documentRef
FirebaseFirestore.DocumentReference <AppModelType, DbModelType>
data
FirebaseFirestore.WithFieldValue <AppModelType>
Returns
Type
Description
Type Parameters
Name
Description
AppModelType
DbModelType

update(documentRef, dataOrField, preconditionOrValues)

  update<AppModelType 
  
 = 
  
 firestore 
 . 
 DocumentData 
 , 
  
 DbModelType 
  
 extends 
  
 firestore 
 . 
 DocumentData 
  
 = 
  
 firestore 
 . 
 DocumentData 
> ( 
 documentRef 
 : 
  
 firestore 
 . 
 DocumentReference<AppModelType 
 , 
  
 DbModelType 
> , 
  
 dataOrField 
 : 
  
 firestore 
 . 
 UpdateData<DbModelType> 
  
 | 
  
 string 
  
 | 
  
 firestore 
 . 
 FieldPath 
 , 
  
 ... 
 preconditionOrValues 
 : 
  
 Array 
< { 
  
 lastUpdateTime 
 ?: 
  
 firestore 
 . 
 Timestamp 
 ; 
  
 } 
  
 | 
  
 unknown 
  
 | 
  
 string 
  
 | 
  
 firestore 
 . 
 FieldPath 
> ) 
 : 
  
 WriteBatch 
 ; 
 

Update fields of the document referred to by the provided [DocumentReference] DocumentReference . If the document doesn't yet exist, the update fails and the entire batch will be rejected.

The update() method accepts either an object with field paths encoded as keys and field values encoded as values, or a variable number of arguments that alternate between field paths and field values. Nested fields can be updated by providing dot-separated field path strings or by providing FieldPath objects.

A Precondition restricting this update can be specified as the last argument.

Parameters
Name
Description
documentRef
FirebaseFirestore.DocumentReference <AppModelType, DbModelType>

A reference to the document to be updated.

dataOrField
FirebaseFirestore.UpdateData <DbModelType> | string | FirebaseFirestore.FieldPath

An object containing the fields and values with which to update the document or the path of the first field to update.

preconditionOrValues
Array <{ lastUpdateTime?: FirebaseFirestore.Timestamp ; } | unknown | string | FirebaseFirestore.FieldPath >

An alternating list of field paths and values to update or a Precondition to restrict this update.

Returns
Type
Description

{WriteBatch} This WriteBatch instance. Used for chaining method calls.

Type Parameters
Name
Description
AppModelType
DbModelType
Example
  let 
  
 writeBatch 
  
 = 
  
 firestore 
 . 
 batch 
 (); 
 let 
  
 documentRef 
  
 = 
  
 firestore 
 . 
 doc 
 ( 
 'col/doc' 
 ); 
 writeBatch 
 . 
 update 
 ( 
 documentRef 
 , 
  
 { 
 foo 
 : 
  
 'bar' 
 }); 
 writeBatch 
 . 
 commit 
 (). 
 then 
 (() 
  
 = 
>  
 { 
  
 console 
 . 
 log 
 ( 
 'Successfully executed batch.' 
 ); 
 }); 
 
Create a Mobile Website
View Site in Mobile | Classic
Share by: