Transaction
A reference to a transaction. The Transaction
object passed to a transaction's updateFunction provides the methods to
read and write data within the transaction context. See Firestore.runTransaction()
.
A transaction consists of any number of get()
operations followed by any number of write operations such as set(),
update()
, or delete()
. In the case of a concurrent edit, Cloud Firestore runs the entire transaction again. For example,
if a transaction reads documents and another client modifies any of those documents, Cloud Firestore retries the transaction.
This feature ensures that the transaction runs on up-to-date and consistent data.
Transactions never partially apply writes. All writes execute at the end of a successful transaction.
When using transactions, note that:
- Read operations must come before write operations.
- A function calling a transaction (transaction function) might run more than once if a concurrent edit affects a document that the transaction reads.
- Transaction functions should not directly modify application state (return a value from the
updateFunction
). - Transactions will fail when the client is offline.
Methods
delete
</>Deletes the document referred to by the provided DocumentReference
.
delete(documentRef: DocumentReference): Transaction;
get
</>Reads the document referenced by the provided DocumentReference
.
get(documentRef: DocumentReference): Promise<DocumentSnapshot>;
set
</>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.
set(documentRef: DocumentReference, data: { [key: string]: value }, options?: SetOptions): Transaction;
update
</>Signature 1
</>Updates fields in the document referred to by the provided DocumentReference
. The update will fail if applied
to a document that does not exist.
update(documentRef: DocumentReference, data: { [key: string]: value }): Transaction;
Signature 2
</>Updates fields in the document referred to by the provided DocumentReference. The update will fail if applied to a document that does not exist.
update(documentRef: DocumentReference, field: string | FieldPath, value: any, moreFieldsAndValues: any[]): Transaction;