In firestore document (https://firebase.google.com/docs/firestore/manage-data/transactions#transactions) it says that: "Read operations must come before write operations". What's the rationale for this rule enforced in the nodejs sdk?
Here's what I know:
- Read operations are associated with the transaction id; write operations will be submitted with the same transaction as batch writes; all reads and writes within the transaction take place exactly at the commit time.
- Firestore server sdk cache write operations in a write batch and commit at the end; if the write batch is not empty, read operations fail: https://github.com/googleapis/nodejs-firestore/blob/master/dev/src/transaction.ts#L123
- Firestore SDKs are based on Google gRPC APIs: https://github.com/googleapis/googleapis/blob/master/google/firestore/v1/firestore.proto
What if we remove this empty write batch check? The server sdk will cache all write operations and commit them at the end. What am I missing?
What I'm trying to do is: manage collections in different modules and they need to reuse the same transaction to perform an atomic operation (e.g. create an order and reduce the inventory and subtract user balance); each module need to read from some collections and make some changes.