I have a Node.js app with @google-cloud/firestore that has two collections, A and B.
Collection A documents have a field called 'name' and one called 'collectionBId'. Collection B documents just have a field called 'name'. Name's in collection A and B are unique.
// Collection A
{ id: 1, name: 'example1', nameB: 'example2' }
// Collection B
{ id: 1, name: 'example2' }
There an endpoint in Node.js that does:
const result = await firestore.runTransaction(async transaction => {
const refA = firestore.collection('A').where('name', '==', reqParam1)
const {docs: docsA} = await transaction.get(refA)
const refB = firestore.collection('B').where('name', '==', docsA[0].data().name).where('name', '==', reqParam2)
const {docs: docsB} = await transaction.get(refB)
return docsB[0].data()
})
This is significantly faster than (there is one less round trip):
const {docs: docsA} = await firestore.collection('A').where('name', '==', reqParam1).get()
const {docs: docsB} = await firestore.collection('B').where('name', '==', docsA[0].data().name).where('name', '==', reqParam2).get()
const result = docsB[0].data()
The transactions run in pessimistic locks in Node.js but is not clear if multiple users can use this transaction concurrently (eg. 100 users).
On the docs, it is mentioned that when a write is performed there is a lock in place that prevent a concurrent write will that prevent reads also? Also, if a read transaction is used a lot, would that block writes?
That leads to the following two questions:
- Do Firestore read transactions block other read transactions in Node.js?
- Do Firestore read transactions block write transactions in Node.js?