Running 2 firebase databases in the same ionic project


#1

I need to add an other database to my existing one in my ionic 3 project. I’m using angularfire 2. I have an envirement.ts an other envirement2.ts.
these files contains 2 different firebase configuration (API key, authDomain …) .

this is a part of my app.module.ts :

import { AngularFireModule } from ‘angularfire2’;
import { firebaseConfig } from ‘…/environment’;
import { AngularFireDatabaseModule } from ‘angularfire2/database’;

@NgModule({
declarations: [
MyApp
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
AngularFireModule.initializeApp(firebaseConfig),
AngularFireDatabaseModule,

That’s why I am trying to add my firebase database as a provider instead of inside my component app.module.ts.
since I can only put one single config in app.module.ts :slight_smile:

@Injectable()
export class FireProvider {
constructor( public db: AngularFireDatabase) {

}

How can 2 configurations files be initiated in the same app ?


#2

Hi, I am stuck with the same. Can someone shed some light here?


#3

I’ve never done it, but the idea is that you create two different Firebase projects, and create a database inside each project. Then you sign into both projects. There’s probably documentation on this, because it’s a recommended way to keep # of connections down. For example, you create two copies of your database, and flip a coin when your user wants some informtion, and on heads read from database 1, or tails read from database 2. So I’d suggest you dig around the advanced Firebase docs.

Edit: I got curious and looked for it myself. You want “web”

https://firebase.google.com/docs/configure/


#4

Thanks for the info. I have looked at this before asking here. Implementation is harder than the imagination :wink:


#5

Maybe post code then, along with the error message you’re getting. It looks straightforward to me.


#6

Page1 and Page2 with two different configs :
AngularFireModule.initializeApp(FIREBASE_CONFIG)
AngularFireModule.initializeApp(FIREBASE_PROFILE_CONFIG)
in .module.ts

Page1 calls an AuthService provider to createUserWithEmailAndPassword(email, password).
Page2 calls ProfileService provider to saveProfile(uid, firstname).

But the saveProfile method is always saving to database that the AuthService authenticated for.
How to create another instance of AngularFireModule - authenticate to that - and therefore save data to that project?

Not getting any errors - just not able to save to different databases. Data is saved to whichever project the AngularFireAuth instance is authenticated for.


#7

That isn’t consistent with the docs, and it isn’t the question you first asked. To follow the docs I linked, you don’t use AngularFire. You create a database provider and then have two variables:

let primaryDatabase = primaryConfig.database()
let secondaryDatabase = secondaryConfig.database()

So those exist inside the database provider, you don’t need to do anything in app.module.ts.

Edit to add: If you want two instances of AF you could always create two providers, and import one instance of AF into each provider.


#8

I already submitted an issue on github https://github.com/angular/angularfire2/issues/1026