0
votes

With Google Firebase Realtime Database, it appears there is no way to run a cloud function that performs queries and updates on the database data using promises to synchronize data access. Specifically I am trying to write a timed function that runs periodically and updates some of the data elements. The Youtube tutorial set at https://www.youtube.com/playlist?list=PLl-K7zZEsYLkPZHe41m4jfAxUi0JjLgSM  uses Firestore (not Realtime Database which is an older product) for the examples. Is something like this possible with RealTime database?

1
What do you mean by "using promises to synchronize data access"? Realtime Database update operations using the Firebase Admin SDK definitely return a promise that indicates when the write is complete, so I don't see what the problem is.Doug Stevenson
So I am not updating directly. I have an algorithm that needs to search my database looking for the top 10 teams in a game and then based on that to do something to those teams. So I need to collect up that data first. I have a loop through my data looking for these teams. Problem I have is each database read (for each team) is an asynchronous call, which means I can't continue with my algorithm once I have ten teams collected. Is there a way to block all calls and make them sequential in a realtime database server side function?David
Database reads using once() also return a promise. You will need to use the promises from these calls. There are no blocking calls, and that is very much the norm for JavaScript APIs. You will need to learn the JS conventions for async programming with promises, and that is a whole lot easier if you know async/await syntax. The video tutorials you linked to were made by me, and they will help you understand how to work with promises regardless. The last three videos works with Realtime Database exclusively.Doug Stevenson

1 Answers

0
votes

You can use a Cloud Function to trigger a function - similar to a cron job - that will use Google Scheduler to run the update in the database, you can perform the updates as you would like.

As mentioned on the below tutorials, in case you are using the module NodeJS 2.3.0 of the firebase-functions, with 6.7.0 of the Firebase CLI, the syntax for these functions are pretty simple.

The below, it's an example using cron syntax and the second one is using English description, making the job even easier. :)

functions.pubsub.schedule('5 11 * * *').onRun((context) => {
    console.log('This will be run every day at 11:05 AM UTC!');
});
functions.pubsub.schedule('every 5 minutes').onRun((context) => {
    console.log('This will be run every 5 minutes!');
});

In the below tutorials, there are more information on how to configure and using the Cloud Functions with Google Scheduler.

Let me know if this helped you!