This is a snippet that uses firebase's firebase-admin
to initialize multiple firebase projects in one admin application.
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;
import { first, second } from '../path/to/the/file/above'
first.database();
second.database();
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
}