Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I implement beforeCreate trigger on Firebase Functions

On the Firebase docs they mention 4 types of triggers:

  • onCreate
  • onDelete
  • onUpdate
  • onWrite

Is there a way to listen to added row in the Cloud Functions and modify fields of an added row before the "child_added" listeners are triggered? Is there a way to implement BeforeCreate?

Desired BeforeCreate cycle (in Cloud Functions):

  • Request to add a new message
  • Change the message fields
  • Add a new message with modified fields
  • Clients receive a "child_added" event
like image 658
Guy Avatar asked Feb 05 '18 13:02

Guy


People also ask

Does Firebase have Webhooks?

Firebase Webhooks automates the workflow for companies and Developers by triggering the defined events via URL. Firebase Webhooks Integration is the simplest and most efficient way to communicate between app and Firebase.

What is the difference between onCall http callable and onRequest HTTP request Functions?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.


1 Answers

All events for the Realtime Database in Cloud Functions trigger asynchronously after the write has been committed. For this reason, other users may already have seen the data before your function can change it.

To solve this problem you'll want to ensure the data only gets written to the location everyone sees after it's been validated/modified.

To validate/modify the new data before listeners to that data can see it, you have two options:

  1. Use a HTTP triggered function for writing the data. The application code calls the HTTP function, which does the data manipulation you want, and then writes the result to the database.

  2. Have the applications write to a "moderation queue", which is just a separate location in the database. The Cloud Function triggers fro this queue, validates/modifies the data, writes it to the actual location, and then deletes it from the queue.

With both of these approaches you lose parts of the transparent offline behavior of the Firebase Realtime Database though, so you'll have to choose.

like image 84
Frank van Puffelen Avatar answered Sep 29 '22 21:09

Frank van Puffelen