Mehdi
Mehdi

Reputation: 2398

Trigger function on batch create with firebase

In my app, I have two ways of creating users. One is a singular add which triggers a cloud function onCreate to send email and does some other logic. The other one is by batch which ultimately triggers the same function for each added document.

Question is how can I trigger a different function when users are added by a batch ?

I looked into firebase documentation and it doesn't seem to have this feature. Am I wrong ?

This will greatly help reducing the number of reads and I can bulk send emails to added users instead of sending them one by one.

Upvotes: 2

Views: 448

Answers (1)

Soni Sol
Soni Sol

Reputation: 2612

The trigger on Cloud functions for document creation is only one.

What you can do is to have two different functions with the same trigger and incode differentiate between both creation methods.

This can be something like adding to the document two more values:

  • creation_method
  • batch

with creation method you can evaluate its value on each document to verify if the execution continues or it finishes at that point.

batch can be used in the batch created to identify the whole batch.

for creation_method I recommend there different values:

  • singular
  • batch_normal
  • batch_final

on Batch just having a batchID

For the function for singular creation verify that is singular and thats it.

For the batch function make that it only continue on batch_final status and get all the values that have the same batchId.

This approach will not reduce the reads as the reads are billed for each document read so unless you depend on additional documents the number of reads will be the same.

As a work around if you want to reduce the amount you are billed per reads you can change to Realtime Database the triggers you mentioned also exist and it has the advantage that it doesn't bill for reads.

Upvotes: 2

Related Questions