Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't we Write multiple files in Firebase Cloud Functions

How can we implement Micro-service architecture using Firebase Cloud Functions can we write multiple .js files unlike writing all functions into index.js so that we need not re-deploy all functions for change in single function

like image 611
Mahi Tej Gvp Avatar asked Dec 14 '22 21:12

Mahi Tej Gvp


2 Answers

I'm importing other .js files with firebase functions. Just think of the functions folder as root, I was mistakenly trying to import files from /functions parent folder.

index.js

var paymentFunctions = require('./payment_functions');

along with something like:

exports.paymentMethodTask = functions.database.ref('/newPaymentMethodTask/{taskId}').onWrite(event => {
    return paymentFunctions.processPaymentMethodTask(event);
});

With the folder structure:

/myProject/functions/index.js
/myProject/functions/payment_functions.js

Then export your functions in payment_functions.js as normal:

module.exports = {
    processPaymentMethodTask: function test(event) {
        //do something here with the event
    }
};

https://medium.com/step-up-labs/our-experience-with-cloud-functions-for-firebase-d206448a8850

like image 151
viral 9966 Avatar answered Feb 12 '23 20:02

viral 9966


All your Cloud Functions for Firebase will have to be defined in the index.js file. But that doesn't mean you have to implement all functionality in a single file.

I often implement the bulk of each function in a separate file. For example, if I'm using the Google Cloud Vision API to extra text from images, I'll have an ocr.js. I give this file a main section, so that I can run the script from a local terminal using node ocr.js. Then in my index.js I need little more code than an import of ocr.js and wiring that up to Cloud Functions.

Also see:

  • my answer here for the code of the above example.
  • this video from I/O
like image 37
Frank van Puffelen Avatar answered Feb 12 '23 20:02

Frank van Puffelen