Execute more than 500 operations at once in Firestore Database

前端 未结 1 1689
鱼传尺愫
鱼传尺愫 2020-12-17 04:03

I\'m trying to create a WriteBatch to keep control of one of my dynamic references in my database. My app have a simple User-Follow-Post-Feed model

相关标签:
1条回答
  • 2020-12-17 04:50

    Finally the problem was caused because I was trying to realize this batch operations inside a transaction, which also acts like a batch in the end. That's why even I was generating batches for each 400 references, these instances were created inside a transaction and it counts like a single big transaction which surpass the 500 limit.

    I made a few changes and implemented on a repository on my GitHub.

    //Generate the right amount of batches
        const batches = _.chunk(updateReferences, MAX_BATCH_SIZE)
            .map(dataRefs => {
                const writeBatch = firestoreInstance.batch();
                dataRefs.forEach(ref => {
                    writeBatch.update(ref, 'author', newAuthor);
                });
                return writeBatch.commit();
            });
    

    It is writed on typescript, but you will understand it for sure: https://github.com/FrangSierra/firestore-cloud-functions-typescript/blob/master/functions/src/atomic-operations/index.ts

    0 讨论(0)
提交回复
热议问题