22

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.

chainicko
  • 461
  • 2
  • 13
  • 3
    This is a matter of opinion and not well suited for Stack Overflow. However, you should know that you can use the require() function to pull in code from other files into your index.js. After that, organization is whatever you want. – Doug Stevenson Mar 22 '17 at 20:31
  • Thank you @DougStevenson, this answer my question. – François MARTIN Mar 22 '17 at 20:56
  • 1
    See my example here: http://stackoverflow.com/questions/42726310/how-to-test-firebase-functions-locally-on-pc/42729988#42729988 It's from a somewhat outdated version of Cloud Functions, but the concepts still work the same. – Frank van Puffelen Mar 23 '17 at 03:42
  • 1
    Really good question, I'm also looking for how to split firebase functions in multi files e.g: login functions in a separate file and group creation subgroup creation functions in separate files etc. i have a very large codebase with hundreds of functions – Inzamam Malik Jun 08 '17 at 09:21

4 Answers4

11

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.

Callam
  • 10,671
  • 2
  • 27
  • 31
1

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.

Matt Penner
  • 988
  • 9
  • 21
0

You could use something like export { functionName } from './file' at your index.js file.

/functions/index.js
// This is the main entry point for the app written in ES that is compatible with node lts
import * as functions from 'firebase-functions';

export { sendWelcomeEmail } from './userEmails';

exports.helloWorld = functions.https.onRequest((request, response) => {
  let helloMsg = `Hello!`;
  response.send(helloMsg);
});
Let Me Tink About It
  • 11,866
  • 13
  • 72
  • 169
0

I created a script to copy all of the javascript files present in a folder named js, which is located in the root directory of the firebase function folder to index.js.

Basically, the script just automates the copying of the file, After merging all the required files one can then deploy the functions as usual.

You can find the script here.

Note: Reading all the other posts, I found out that there are other ways to organize the functions in different files. I made this script to solve my problem and I'm using it in one of my projects as on now. Sharing with you guys, thinking if other techniques seems too complex, you can try this out.

Link: https://github.com/UnresolvedCold/firebase-merge