Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to split Firebase functions in multiple files

I'm working with firebase functions and arrived to hundreds of functions, and now it is very hard to manage it in single index.js file as shown in their lots of examples

I tried to split that functions in multiple files like:

--firebase.json
--functions
  --node_modules
  --index.js
  --package.json
  --app
    --groupFunctions.js
    --authFunctions.js
    --storageFunctions.js

In this structure i divide my functions in three categories and put in that three files groupFunctions.js, authFunctions.js, and storageFunctions.js. I then tried to import thise files in index.js, but I don't know why it is not working for me.

Here is groupFunctions.js

var functions = require('firebase-functions');
module.exports = function(){
    exports.onGroupCreate = functions.database.ref('/groups/{groupId}')
        .onWrite(event => {
            console.log(`A group is created in database named:${event.params.groupId}.`);
            // some logic...
            //...
        })
}

Here is index.js file:

var functions = require('firebase-functions');
module.exports = require("./app/groupFunctions")();

My editor not giving any warning in this code. But when I deploy this code with firebase deploy --only functions, it does not deploy function. If some functions already exist on firebase console, it remove all functions on deploy.

here is deployment logs: enter image description here

question is also asked on github

like image 866
Inzamam Malik Avatar asked Jun 08 '17 09:06

Inzamam Malik


2 Answers

Working code example:

file structure:

--firebase.json
--functions
  --node_modules
  --index.js
  --package.json
  --src
    --groupFunctions.js
    --authFunctions.js
    --storageFunctions.js

index.js file:

require('./src/groupFunctions.js')(exports);
require('./src/authFunctions.js')(exports);
require('./src/storageFunctions.js')(exports);

groupFunctions.js file:

var functions = require('firebase-functions');

module.exports = function (e) {
    e.onGroupCreate = functions.database.ref('/groups/{groupId}')
        .onWrite(event => {
            console.log(`A group is created in database named:${event.params.groupId}.`);
            // some logic...
            //...
        })
}

UPDATE: now I have better solution

The full working code is located at https://github.com/malikasinger1/firebase-functions-with-typescript and it's written with cutting edge tech like typescript and webpack. You may use this as a boilerplate/starter.

like image 67
Inzamam Malik Avatar answered Nov 17 '22 01:11

Inzamam Malik


You can pass the exports object to the function in groupFunctions.js, like this:

var functions = require('firebase-functions');

module.exports = function (e) {
    e.onGroupCreate = functions.database.ref('/groups/{groupId}')
        .onWrite(event => {
            console.log(`A group is created in database named:${event.params.groupId}.`);
            // some logic...
            //...
        })
}

Now, in index.js:

var functions = require('firebase-functions');

require("./app/groupFunctions")(module.exports);

The way it works is that modules.exports is a regular JavaScript object, so you can add new properties to that from wherever you want.

like image 5
mhaligowski Avatar answered Nov 17 '22 00:11

mhaligowski