5

I know this was already asked here, BUT it does not answer my question. My question is how can we break apart index.js for Cloud Functions including the onWrite calls and such.

I realize you can use "require" and bring in outside code. It still leaves a bit of code ( in Franks OCR example, for example) , in index.js.

Ideally I'd love to be able to move an entire onWrite event trigger to another file.

EXAMPLE in index.js:

exports.sysQueueUserNew = functions.database.ref("/sys/queue/user_new/{queueId}").onWrite((event) => {
      // do something
})

How can I move the entire function event watch/call to another js file, and call it from index.js?

My index.js has grown quite large, and reading it for organizational purposes has become a PAIN.

Ideally I'd like my index.js to be very organized such as:

--in  index.js --
    /// User cleanup
    userHelpers()    

    /// SYS Logs
    sysLogs()

--and in userHelpers.js have the onWrite trigger for example---
   functions.database.ref("/sys/queue/user_new/{queueId}").onWrite((event) => {
      // create user
   })

etc....

Is this possible without having to have code written like so (a' la Franks OCR example):

var test = require('./test')

exports.sysQueueUserNew = functions.database.ref("/sys/queue/user_new/{queueId}").onWrite((event) => {
      // do something
      test.doCleanup()
})

Thanks in advance....

Community
  • 1
  • 1
paulyfl
  • 96
  • 3
  • Why don't you just write another file and add another script in your html? . Once all JS files are loaded, every function is accessible from any other js file – Kody R. Apr 11 '17 at 19:41
  • Because these are Firebase cloud functions that run on their server. The index file in question gets deployed to their server, and the DB uses it for trigger events. – paulyfl Apr 11 '17 at 19:52

2 Answers2

8

You can easily spread your functions across multiple files. Here's an example:

////////////// index.js

exports.sysQueueUserNew = require('./sys-queue-user-new');
exports.userCleanup = require('./user-cleanup');

///////////// sys-queue-user-new.js

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

module.exports = functions.database
  .ref("/sys/queue/user_new/{queueId}")
  .onWrite(e => {
    // do something
  });

///////////// user-cleanup.js

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

module.exports = functions.auth.user().onDelete(e => {
  // do something
});
Michael Bleigh
  • 25,334
  • 2
  • 79
  • 85
  • Thank you very much for a nice example. I must have been doing something wrong, as I could swear I tried that pattern with no luck. I feel like an idiot! Thank you! – paulyfl Apr 12 '17 at 11:18
  • @Michael, With this setup, suppose you initialize `firebase-admin` in `index.js` like so `const admin = require('firebase-admin')`, how would you pass it to `user-cleanup.js`? Because admin can only be initialized once, but you need to use it in `user-cleanup.js` – eyeezzi May 26 '17 at 10:22
  • Once initialized in `index.js` you can just `const admin = require('firebase-admin')` in other files and it will already be initialized. – Michael Bleigh May 26 '17 at 17:43
  • Thanks Michael, I posted what I ended up doing as a variant of your answer. – eyeezzi May 28 '17 at 07:12
8

As a variant of Michael's answer, I found this organization very neat and kind of object-oriented.

// index.js
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

const user = require('./user')(admin)
exports.sendWelcomeEmail = user.sendWelcomeEmail

// user.js
const functions = require('firebase-functions')

module.exports = function(admin) {

    let sendWelcomeEmail = functions.auth.user().onCreate(event => {
        // return admin.database().ref().update({})
    })

    return {
        sendWelcomeEmail: sendWelcomeEmail
    }
}
eyeezzi
  • 635
  • 7
  • 18