Implementation of Idempotent counter

From the docs:

It can also lead to multiple calls for a single event, so for the highest quality functions, make sure the functions are written as idempotent.

So, if Firestore does not offer a way to count the number of child documents in a collection, I need to make a cloud function to aggregate this information on a node, say /counters/{type}/count.

If I create a record trigger and increase the value, there is a chance that my counter will not reflect the actual document count, right?

How can I write a function to fully count documents in a collection (without being too expensive - suppose I don’t want to read the entire collection with every record)?

+4
source share
2 answers

The answer to this question will depend on various aspects of how you use the collection, as well as what “great calculates” means.

Preamble

To begin with, since Cloud Function calls are asynchronous with recording, this will cause the counter to lag slightly behind the true count of the collection. I assume that everything is in order.

Even if you counted the collection by reading each document, the counter may still be outdated, as the documents could have been inserted or deleted during the counting.

Expenses

" ". , , . , / . 3 , , 4 , . - , , .

, . +1 , ( ) - ? , , ? , ?

, .

-

- (txid) . txid , , .

, , txid. +1. , , (, ).

, , . , , . , "X", , .

, . , 1000 5000 5 ( , 5 ).

" "

-

, .

, : ,

-

. , (, Auto ID), . , , / , .

, , - Cloud Function . , , .

, , , , , , . , , , , .

, , , , , . , . .

, , ( ). , , .

+4

- Cloud Firestore + Cloud Function, 100% , , - , .

, ( ).

, , counted boolean .

, , :

  • . counted == true,
  • .
  • counted .

Cloud Firestore . : https://firebase.google.com/docs/firestore/manage-data/transactions

+1

Source: https://habr.com/ru/post/1687643/


All Articles