A write batch, used to perform multiple writes as a single atomic unit.

A WriteBatch object can be acquired by calling Firestore.batch(). It provides methods for adding writes to the write batch. None of the writes will be committed (or visible locally) until WriteBatch.commit() is called.

Unlike transactions, write batches are persisted offline and therefore are preferable when you don't need to condition your writes on read data.

Index

Constructors

Methods

Constructors

Private constructor

Methods

commit

  • commit ( ) : Promise < void >
  • Commits all of the writes in this write batch as a single atomic unit.

    Returns Promise<void>

    A Promise resolved once all of the writes in the batch have been successfully written to the backend as an atomic unit. Note that it won't resolve while you're offline.

delete

  • delete ( documentRef DocumentReference < any > ) : WriteBatch
  • Deletes the document referred to by the provided DocumentReference.

    Parameters

    Returns WriteBatch

    This WriteBatch instance. Used for chaining method calls.

set

  • set < T > ( documentRef DocumentReference < T > ,  data Partial < T > ,  options SetOptions ) : WriteBatch
  • Writes to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created. If you pass SetOptions, the provided data can be merged into the existing document.

    Type parameters

    • T

    Parameters

    • documentRef: DocumentReference<T>

      A reference to the document to be set.

    • data: Partial<T>

      An object of the fields and values for the document.

    • options: SetOptions

      An object to configure the set behavior.

    Returns WriteBatch

    This WriteBatch instance. Used for chaining method calls.

  • set < T > ( documentRef DocumentReference < T > ,  data T ) : WriteBatch
  • Writes to the document referred to by the provided DocumentReference. If the document does not exist yet, it will be created. If you pass SetOptions, the provided data can be merged into the existing document.

    Type parameters

    • T

    Parameters

    • documentRef: DocumentReference<T>

      A reference to the document to be set.

    • data: T

      An object of the fields and values for the document.

    Returns WriteBatch

    This WriteBatch instance. Used for chaining method calls.

update

  • update ( documentRef DocumentReference < any > ,  data UpdateData ) : WriteBatch
  • Updates fields in the document referred to by the provided DocumentReference. The update will fail if applied to a document that does not exist.

    Parameters

    • documentRef: DocumentReference<any>

      A reference to the document to be updated.

    • data: UpdateData

      An object containing the fields and values with which to update the document. Fields can contain dots to reference nested fields within the document.

    Returns WriteBatch

    This WriteBatch instance. Used for chaining method calls.

  • update ( documentRef DocumentReference < any > ,  field string | FieldPath ,  value any... moreFieldsAndValues any [] ) : WriteBatch
  • Updates fields in the document referred to by this DocumentReference. The update will fail if applied to a document that does not exist.

    Nested fields can be update by providing dot-separated field path strings or by providing FieldPath objects.

    Parameters

    • documentRef: DocumentReference<any>

      A reference to the document to be updated.

    • field: string | FieldPath

      The first field to update.

    • value: any

      The first value.

    • Rest ...moreFieldsAndValues: any[]

      Additional key value pairs.

    Returns WriteBatch

    A Promise resolved once the data has been successfully written to the backend (Note that it won't resolve while you're offline).