Transaction class

A reference to a transaction.

TheTransaction object passed to a transaction'supdateFunction provides the methods to read and write data within the transaction context. SeerunTransaction().

Signature:

exportdeclareclassTransaction

Methods

MethodModifiersDescription
delete(documentRef)Deletes the document referred to by the providedDocumentReference.
get(documentRef)Reads the document referenced by the providedDocumentReference.
set(documentRef, data)Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created.
set(documentRef, data, options)Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created. If you providemerge ormergeFields, the provided data can be merged into an existing document.
update(documentRef, data)Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document that does not exist.
update(documentRef, field, value, moreFieldsAndValues)Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document that does not exist.Nested fields can be updated by providing dot-separated field path strings or by providingFieldPath objects.

Transaction.delete()

Deletes the document referred to by the providedDocumentReference.

Signature:

delete<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>):this;

Parameters

ParameterTypeDescription
documentRefDocumentReference<AppModelType, DbModelType>A reference to the document to be deleted.

Returns:

this

ThisTransaction instance. Used for chaining method calls.

Transaction.get()

Reads the document referenced by the providedDocumentReference.

Signature:

get<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>):Promise<DocumentSnapshot<AppModelType,DbModelType>>;

Parameters

ParameterTypeDescription
documentRefDocumentReference<AppModelType, DbModelType>A reference to the document to be read.

Returns:

Promise<DocumentSnapshot<AppModelType, DbModelType>>

ADocumentSnapshot with the read data.

Transaction.set()

Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created.

Signature:

set<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,data:WithFieldValue<AppModelType>):this;

Parameters

ParameterTypeDescription
documentRefDocumentReference<AppModelType, DbModelType>A reference to the document to be set.
dataWithFieldValue<AppModelType>An object of the fields and values for the document.

Returns:

this

ThisTransaction instance. Used for chaining method calls.

Exceptions

Error - If the provided input is not a valid Firestore document.

Transaction.set()

Writes to the document referred to by the providedDocumentReference. If the document does not exist yet, it will be created. If you providemerge ormergeFields, the provided data can be merged into an existing document.

Signature:

set<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,data:PartialWithFieldValue<AppModelType>,options:SetOptions):this;

Parameters

ParameterTypeDescription
documentRefDocumentReference<AppModelType, DbModelType>A reference to the document to be set.
dataPartialWithFieldValue<AppModelType>An object of the fields and values for the document.
optionsSetOptionsAn object to configure the set behavior.

Returns:

this

ThisTransaction instance. Used for chaining method calls.

Exceptions

Error - If the provided input is not a valid Firestore document.

Transaction.update()

Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document that does not exist.

Signature:

update<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,data:UpdateData<DbModelType>):this;

Parameters

ParameterTypeDescription
documentRefDocumentReference<AppModelType, DbModelType>A reference to the document to be updated.
dataUpdateData<DbModelType>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:

this

ThisTransaction instance. Used for chaining method calls.

Exceptions

Error - If the provided input is not valid Firestore data.

Transaction.update()

Updates fields in the document referred to by the providedDocumentReference. The update will fail if applied to a document that does not exist.

Nested fields can be updated by providing dot-separated field path strings or by providingFieldPath objects.

Signature:

update<AppModelType,DbModelTypeextendsDocumentData>(documentRef:DocumentReference<AppModelType,DbModelType>,field:string|FieldPath,value:unknown,...moreFieldsAndValues:unknown[]):this;

Parameters

ParameterTypeDescription
documentRefDocumentReference<AppModelType, DbModelType>A reference to the document to be updated.
fieldstring |FieldPathThe first field to update.
valueunknownThe first value.
moreFieldsAndValuesunknown[]Additional key/value pairs.

Returns:

this

ThisTransaction instance. Used for chaining method calls.

Exceptions

Error - If the provided input is not valid Firestore data.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-01-19 UTC.