Realtime Database Triggers with Firebase Functions
Cloud Functions for Firebase is truly a great way to add quick database triggered events to your project with minimal effort and deploy time.
Warning: May cause addiction.
If you still do not have Firebase Functions setup, please refer to the documentation found here to get started with this amazing tool.
Event types:
- Create: triggered the first time you write to a certain path
- Delete: triggered when you delete a certain path
- Update: triggered if you change the content of a certain path
- Write: all the above
1. Create:
Let’s say you want to update your coworkers/clients each time a new entry is added to /publishedDrafts a path that contains all the articles written by your users.
2. Delete:
Keep a backup of a deleted article just in case if the user regrets it later.
3. Update:
Compare the previous value with the new value of the published article.
4. Write:
This can handle all of the above cases.
5. Bonus:
You can check who wrote to a certain path:
context.authType === 'ADMIN'
context.authType === 'USER' -> context.auth.uid will contain the user Id
Don’t forget to deploy:
firebase deploy --only functions