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.
I organize my event handlers by provider and resource in a folder called
triggers
. E.g. whereauth
is the provider anduser
is the resource; the folderfunctions/triggers/auth/user
contains anonCreate.js
andonDelete.js
, which welcomes and cleans up a user respectively.You can export a particular trigger by using the
require
function: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.You could use something like
/functions/index.jsexport { functionName } from './file'
at your index.js file.