Firestore transaction with multiple get

前端 未结 3 1522
一个人的身影
一个人的身影 2021-02-13 05:13

I\'m trying to run a transaction with a variable number of read operations. I put the read () operations before than update ().

Reading the Firestore doc on https://clou

3条回答
  •  無奈伤痛
    2021-02-13 05:46

    I was facing the same problem and decided to use a combination of a batched write and "normal" reads. The decision was guided by the fact that I needed to make many reads that did not rely on each other. At first I used a method similar to the one proposed by Derrick above, but it proved not sustainable for may reads. The code dictates that every loop is blocking to the next one. What I did was to batch all the reads to run in parallel with Promise.all The disadvantage of this is that you dont take advantage of transaction features, but since the field I was iterested in was not changing, it made sense Here's my sample code

    const batch = firestore().batch()
     const readPromises = invoiceValues.map(val => {
                            return orderCollection(omcId).where(, '', ).get()
                        })
    
                        return Promise.all(readPromises).then(orderDocs => {
                      //Perform batch operations here
                            return batch.commit()
                         })
    

    This has proven to be more efficient for many reads, while remaining safe since the fields I'm interested in dont change

提交回复
热议问题