Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize Cloud Functions for Firebase

What is the best practice to organize all our Cloud Functions for Firebase?

I see from the sample GitHub repository that all functions reside in a single index.js file.

I guess for bigger project that there is a better approach to organize Cloud Functions for Firebase in different files/directory.

like image 944
François MARTIN Avatar asked Mar 22 '17 17:03

François MARTIN


People also ask

How do you organize a Firebase function?

Our solution for handling Firebase Cloud Functions automatically name all functions from their location. save your functions in a folder structure that is like your realtime-database structure. name your files according to the trigger you use ( onWrite, onCreate, onUpdate , onChange and onDelete)

What type of Cloud Functions exist in Firebase?

Distinctly there are 2 types of Cloud functions: HTTP functions and Event-driven functions.

Does Firebase support Nodejs?

Important: Node. js versions 10, 12, 14, and 16 are supported.

What is cold start Firebase?

Functions are stateless, and the execution environment is often initialized from scratch, which is called a cold start. Cold starts can take significant amounts of time to complete.


2 Answers

I organize my event handlers by provider and resource in a folder called triggers. E.g. where auth is the provider and user is the resource; the folder functions/triggers/auth/user contains an onCreate.js and onDelete.js, which welcomes and cleans up a user respectively.

+--/auth |  +--/user |     +--/onCreate.js |     +--/onDelete.js +--/database +--/storage 

You can export a particular trigger by using the require function:

exports.onCreateAuthUser = require('./triggers/auth/user/onCreate');     exports.onDeleteAuthUser = require('./triggers/auth/user/onDelete'); 

I went a step further and created a script that automatically exports the functions for me. I change the extension of the files to f.js and search recursively the triggers directory. For each file found, the function name is concocted by breaking down the directory and file path.

This structure was inspired by inspecting the internals of the firebase-functions npm package.

like image 118
Callam Avatar answered Sep 18 '22 06:09

Callam


This is a great question and something I have recently been looking for. I found this great strategy from Tarik Huber: Organizing your Firebase Cloud Functions. It's his take on his own thoughts and a few other contributors in this area.

He organizes his functions based on their usage and type (i.e. trigger, Http, etc) into a folder structure. The index.js code iterates thorugh the structure and imports the functions in a very structured and succinct way. It not only allows the developers to simply add new functions in a well understood structure but they don't have to manually manipulate the index.js file, and it deploys the function names in Firebase according to the structure as well.

Definitely check it out.

like image 32
Matt Penner Avatar answered Sep 19 '22 06:09

Matt Penner