Skip to content

Instantly share code, notes, and snippets.

@jofftiquez
Last active March 12, 2024 01:29
Show Gist options
  • Save jofftiquez/6d4bb432c7b25fe9a89e8f5231ea8ce2 to your computer and use it in GitHub Desktop.
Save jofftiquez/6d4bb432c7b25fe9a89e8f5231ea8ce2 to your computer and use it in GitHub Desktop.
Firebase admin - how to initialise multiple applications in ES6 nodejs.

Firebase Admin Multi App Initialization - ES6

This is a snippet that uses firebase's firebase-admin to initialize multiple firebase projects in one admin application.

ES5 version

Using ES6

import 'firebase';
import * as admin from 'firebase-admin';
import firstServiceAccount from 'path/to/service-account-1';
import secondServiceAccount from 'path/to/service-account-2';

var _first = admin.initializeApp(
  {
    credential: admin.credential.cert(firstServiceAccount),
    databaseURL: 'https://<1st-db-name>.firebaseio.com'
  }, 
  'first' // this name will be used to retrieve firebase instance. E.g. first.database();
);

var _second = admin.initializeApp(
  {
    credential: admin.credential.cert(secondServiceAccount),
    databaseURL: 'https://<2nd-db-name>.firebaseio.com'
  }, 
  'second' // this name will be used to retrieve firebase instance. E.g. second.database();
);

export const first = _first;
export const second = _second;

Usage

import { first, second } from '../path/to/the/file/above'
 
first.database();
second.database();
@lonelydatum
Copy link

Thanks for the post. Is there a way to use first or second based on the projects in the .firebaserc.
For example I have 2 databases

{
"projects": {
"production": "mydb_production",
"staging": "mydb_staging"
}
}

Is there a way to get the value of this_project_in_use.

ex)
const useThis = null
if( the_project_in_use==="staging"){
useThis = first
}else if( the_project_in_use==="production"){
useThis = second
}

@mowamed
Copy link

mowamed commented Feb 3, 2020

thanks for the post it really helped me

@jofftiquez
Copy link
Author

@mowamed I'm glad it helped you.

@mouWorks
Copy link

googled and found this.
you saved me. thanks! :)

@jofftiquez
Copy link
Author

Cool! @mouWorks

@cbdeveloper
Copy link

Worked really well. Thanks!

@jofftiquez
Copy link
Author

Nice! @cbdeveloper

@spock123
Copy link

thanks

@jofftiquez
Copy link
Author

Welcome! @spock123

@NetanelRotem
Copy link

this is awesome!!!

@somersbmatthews
Copy link

Very helpful, thanks.

@IsroilovIbrohim
Copy link

IsroilovIbrohim commented Sep 13, 2022

You saved my day! Thanks!

@WableSanket
Copy link

thanks

@ajay2966
Copy link

how can i get databaseURl

@Mohamed-Shamil
Copy link

Mohamed-Shamil commented Feb 24, 2024

If I have more than two Firebase applications and I want to initialize only one at a time based on the variable I pass, is it possible to do that? is there any other way to initialize only one project without initializing both at same time?

@jofftiquez
Copy link
Author

Hi @Mohamed-Shamil yes, I think it's possible. You just have to adjust the logic. Wrap each initialization in their own function and call them based on your desired logic.

This is just a rough example, I did not test it. Just to show you the possible logic update

import 'firebase';
import * as admin from 'firebase-admin';
import firstServiceAccount from 'path/to/service-account-1';
import secondServiceAccount from 'path/to/service-account-2';

function _initFirst () {
  return admin.initializeApp(
    {
      credential: admin.credential.cert(firstServiceAccount),
      databaseURL: 'https://<1st-db-name>.firebaseio.com'
    }, 
    'first' // this name will be used to retrieve firebase instance. E.g. first.database();
  );

}

function _initSecond () {
  return admin.initializeApp(
    {
      credential: admin.credential.cert(secondServiceAccount),
      databaseURL: 'https://<2nd-db-name>.firebaseio.com'
    }, 
    'second' // this name will be used to retrieve firebase instance. E.g. second.database();
  );
}

export function init (order) {
  if (order === 'first') {
    return _initFirst();
  } else if (order === 'second') {
    return _initSecond();
  }
}

export const initFirst = _initFirst;
export const initSecond = _initSecond;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment