I would like to access multiple realtime databases within the same firebase app project.
As you can find in the documentation "Scale with Multiple Databases > Connect your app to multiple database instances":
// Get the default database instance for an app
var database = firebase.database();
// Get a secondary database instance by URL
var database = firebase.database('https://testapp-1234.firebaseio.com');
Would be nice, to have this in angularfire2 too.
You should be able to do this with DI in the @next release using the RealtimeDatabaseURL injection token. I've been meaning to document this. Give it a shot.
Hi @jamesdaniels, I've been needing to change to different databases into the same project. You mentioned RealtimeDatabaseURL injection token on the @next release. Could you elaborate a little bit more on that? Is there any documentation? Sample? Specific files to glance at?
Any help is highly appreciated.
I found a workaround that works with current @5.0.0-rc.6.0.
constructor(private _rtdb: AngularFireDatabase) { }
const dbRef = this._rtdb.database.app
.database(`https://<ANOTHER_DB_IN_PROJECT>.firebaseio.com/`)
.ref('/');
this._rtdb.object(dbRef).valueChanges();
@jamesdaniels I need to access data from both databases in the same component. How can I achieve that?
Hmm. Can the injection token be defined per @Injectable()? That way I could wrap all my database access to services and be done with it. Is it possible?
@swftvsn If @carlosacabrera code is running, I wouldn't call it a workaround.
It is the way I would like to use angularfire with multiple databases.
@jamesdaniels Do you see any problem with this implementation?
Yeah @JanOschii, I need to experiment more, I've tried to solve this earlier, for context see #1240
There's the RealtimeDatabaseURL injection token as of rc7. The individual modules no longer pull the angularfire app module directly, instead use the factory each time. So just provide it in your module declaration.
See here https://github.com/angular/angularfire2/blob/master/src/database/database.ts#L19
I'll work on docs for this.
If you need multiple databases in the same component building the ref as @carlosacabrera is the correct way to do this; you could also get fancy and make your own module for AngularFireDatabase to use both, but IMO not worth it unless you were referencing two separate Firebase applications in the same module.
Hi @carlosacabrera
I've tried your solution but the method database seems to not accept any params.
[ts] Expected 0 arguments, but got 1.
I've also tried to disable tslint but the param of database method is not considered.
I've tried it with @5.0.0-rc.10 and @5.0.0-rc.6.0
@texano00 , I have the same problem. Did you resolve this?
Does anyone had the same problem?
@texano00, @florent1933
The solution works but it does throw that typing error.
You could fix it by adding instance?: string to /node_modules/firebase/index.d.ts line 122
declare namespace firebase.app {
interface App {
auth(): firebase.auth.Auth;
database(instance?: string): firebase.database.Database;
delete(): Promise<any>;
messaging(): firebase.messaging.Messaging;
name: string;
options: Object;
storage(url?: string): firebase.storage.Storage;
firestore(): firebase.firestore.Firestore;
functions(): firebase.functions.Functions;
}
}
This is an issue with the firebase npm package. Please refer to this PR https://github.com/firebase/firebase-js-sdk/pull/1001
Thanks for your answer @carlosacabrera, it's very helpful.
Most helpful comment
I found a workaround that works with current
@5.0.0-rc.6.0.