Transactions
Transactions allow you to safely perform operations on document data without worrying about simultaneous document updates.
Typesaurus improves the Firestore’s transaction API by splitting the transaction into two parts: get
and set
:
In this example, we fetch the user document and increment its rating. If the rating changes during the transaction, Firestore will retry the transaction.
In the read
part, you get
any number of documents. Whatever you return, will be accessible in the write
part as $.result
:
The write
part allows you to perform any write operations on any collections and the documents from the result:
In this example, we update the user rating, level up the subscription if the rating exceeds ten, and update the leaderboard. We’ll always get up-to-date stats even if the user documents get updated.