Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase One-time Functions

I'm sure these are common scenarios, but after researching some hours, I couldn't really find what the common practice is. Maybe someone with more experience in Firebase can point me to the right direction.

I have two particular scenarios:

1. Code that runs once

Example 1: adding new data to all users in firestore, which is needed for a new feature
Example 2: start duplicating data into existing documents

I currently write the code in a cloud function, and run it on a firestore event (onUpdate of a "hidden" document) and then I immediately delete the function if everything goes well.

I also increase the timeout and memory for this function, as the idea is to potentially update millions of documents.

2. Manually trigger a function from the firebase console (or command line)

Example: Give a user admin privileges in the app (function that sets custom claims and firestore data). We don't have time to implement a back-office, so doing this from the firebase web portal/console would be ideal, specifying the user id.

My current solution is to use a https function, and run it from the GCP portal (on the function's "Testing" tab, being able to pass a json). BUT the function can be triggered publicly, which I don't really like...

What are the common practices for these scenarios?

like image 849
ernewston Avatar asked Mar 05 '20 23:03

ernewston


People also ask

Can I use Firebase functions for free?

Cloud Functions for Firebase does have a free tier for usage up to a certain point, as outlined on our pricing page. However, because Cloud Functions uses some aspects of Google Cloud's paid infrastructure, we need to enable billing to use Cloud Functions.

Is Firebase serverless?

Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.

Why use Firebase Cloud Functions?

You should use Cloud Functions for Firebase if you're a developer building a mobile app or mobile web app. Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database.


2 Answers

To expand on my comment: if you want to create a node script to run one-off code, you just write your JS code like for any cloud function but simply run it immediately. Something like this.

const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

db.collection('users')
  .where('myField', '==', true)
  .get()
  .then(querySnapshot => {
    querySnapshot.docs.forEach(doc => {
      // update doc
    });
  });

If you save this as script.js and execute it with node script.js you’ll be pointed towards downloading a JSON file with credentials. Follow the instructions and you can then run the script again and now you’re running your own code on Firestore, from the command line.

like image 180
Kevin Renskers Avatar answered Sep 25 '22 22:09

Kevin Renskers


For administrative type operations, you're better off just running them on your desktop or some other server you control. Cloud Functions is not well suited for long running operations, or things that must just happen once on demand.

Case 1 really should be managed by a standalone program or script that you can monitor by running it on your desktop.

Case 2 can be done a number of ways, such as building your own admin web site. But you might find it easiest to mirror the contents of a document to custom claims using a Firestore trigger. Read this: https://medium.com/firebase-developers/patterns-for-security-with-firebase-supercharged-custom-claims-with-firestore-and-cloud-functions-bb8f46b24e11

like image 40
Doug Stevenson Avatar answered Sep 22 '22 22:09

Doug Stevenson