Skip to content

Instantly share code, notes, and snippets.

@lvabarajithan
Created April 26, 2020 11:32
Show Gist options
  • Save lvabarajithan/28892dc97efabfc6aa9e1c506f0807cb to your computer and use it in GitHub Desktop.
Save lvabarajithan/28892dc97efabfc6aa9e1c506f0807cb to your computer and use it in GitHub Desktop.
Structuring the Firebase Cloud functions and triggers to scale
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const glob = require('glob');
admin.initializeApp(functions.config().firebase);
/**
* Cloud triggers
*/
const files = glob.sync('./triggers/**/*.f.js', {cwd: __dirname});
for (let f=0, fl=files.length; f<fl; f++) {
const file = files[f];
const splits = file.slice(0, -5).split('/');
const functionName = splits[splits.length - 1];
exports[functionName] = require(file);
}
/**
* Cloud http function calls
*/
const httpFiles = glob.sync('./http_functions/**/*.f.js', {cwd: __dirname});
for (let f=0, fl=httpFiles.length; f<fl; f++) {
const file = httpFiles[f];
const splits = file.slice(0, -5).split('/');
const funName = splits[splits.length - 1];
exports[funName] = require(file);
}
/**
* File located at triggers/db/onUserSignIn.f.js
*/
const functions = require('firebase-functions');
const admin = require('firebase-admin');
exports = module.exports = functions.database.ref(...)
.onUpdate((_, context)=>{
var message = {...};
return admin.messaging().send(message)
.then((response)=>{
console.log("Notification sent!");
})
.catch((err)=>{
console.log("Error sending notification for signin", err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment