I am developing mobile app with flutter framework in which I am using Firestore database and Firebase cloud functions. I have successfully implemented both in my project. However, I have some difficulties understanding how to run functions that are independent from my app. For example, if I have a Firestore database of 100 people and want to calculate average user age for my own analysis. I know that I can call the function calculateAverageAge() from my app, however is there a way to call this function without adding it in my app's code? Should I create a separate project (possibly in different language) and call those functions from there or I can somehow trigger this function from firebase website? Not sure what is the the right approach here.
Hopefully that makes sense.
Thanks!
CodePudding user response:
In general there are three common ways to trigger a function:
- Explicitly call it from your client-side application code, which applies to either HTTP functions or Callable functions.
- Run them periodically on a schedule.
- Run them when something in the data (or in another Firebase feature, or EventArc for v2) changes with background functions.
For your example of calculating the average age of your users, this means that you could combine the last two types and:
- Recalculate the average whenever a new user gets added to the database, or a user gets removed from the database.
- And then also run a scheduled function daily to update the average for any birthdays.