Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initialize firebase app in multiple files with nodejs

I want to Initialize Firebase App in multiple files to organize my methods properly, but I not sure which is the best way to do so.

Below is the file system structure:

/functions
 |-keys
 |-methods
 |  |-email.js
 |  |-logger.js
 |-node_modules
 |-index.js
 |-package.json
 |-package-lock.json

In my index.js, I initialize 2 projects where 1 is for production and another is for OTE.:

const functions = require('firebase-functions');
var firebaseAdmin = require('firebase-admin');

var productionServiceAccount = require('./keys/production-key.json');
var oteServiceAccount = require("./keys/ote-key.json");

var prodServer = firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(productionServiceAccount),
    databaseURL: 'https://production-panel.firebaseio.com'
}, "prod");
var oteServer = firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(oteServiceAccount),
    databaseURL: "https://ote-panel.firebaseio.com"
}, "ote");

console.log("prodServer: ", prodServer.name, "oteServer: ", oteServer.name)

var mailer = require('./methods/email.js') //import code from method folder
var logger = require('./methods/logger.js') //import code from method folder

Below is how i handle the request whether use prod or OTE project:

let admin = req.headers.env == 'prod' ? prodServer : oteServer

Now the problem is my ./methods/logger.js want to read/write log into DB as well, but I don't know how/what to do.

Below is the `logger.js` code:
var exports = {}

exports.log = function(item, ref, env) {
    let admin = env == 'prod' ? prodServer : oteServer //<--problem here

    return admin.database().ref(ref).push(item)
}

module.exports = exports

Should I initialize firebase project again or import it from index.js?

-If initialize firebase project again it will say the project "name" has been used.

-If import it from index.js, then I have to export it from index.js, which when I deploy it to Firebase Function, it will become an onCall Methods..?

-If I move the initialize to another file (./method/initFirebase.js) and import it to index.js when I deploy it to Firebase Function, will it automatically initialize the firebase app?

Please advise. Thank you.

like image 261
Jerry Avatar asked Nov 07 '22 00:11

Jerry


1 Answers

You can create one additional file like you said initFirebase.js and put your initilization and export code there.

const prodServer = firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(productionServiceAccount),
    databaseURL: 'https://production-panel.firebaseio.com',
}, 'prod');
const oteServer = firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(oteServiceAccount),
    databaseURL: 'https://ote-panel.firebaseio.com',
}, 'ote');
module.exports = {
    firebaseApp: env === 'prod' ? prodServer : oteServer,
};

And from all other file import firebase app

const firebaseApp = require('../YOUR_initFirebase.js')

So you dont need to worry about environment in each of the files and it is working for me on google cloud functions.

like image 144
Ridham Tarpara Avatar answered Nov 14 '22 23:11

Ridham Tarpara