Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable connection to multiple database instances of the same project #1567

Closed
JanOschii opened this issue Apr 19, 2018 · 11 comments
Closed

Enable connection to multiple database instances of the same project #1567

JanOschii opened this issue Apr 19, 2018 · 11 comments

Comments

@JanOschii
Copy link

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.

@jamesdaniels
Copy link
Member

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.

@fnhipster
Copy link

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.

@fnhipster
Copy link

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();

@swftvsn
Copy link

swftvsn commented May 2, 2018

@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?

@JanOschii
Copy link
Author

@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?

@swftvsn
Copy link

swftvsn commented May 3, 2018

Yeah @JanOschii, I need to experiment more, I've tried to solve this earlier, for context see #1240

@jamesdaniels
Copy link
Member

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.

@texano00
Copy link

texano00 commented Jun 18, 2018

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

@florent1933
Copy link

florent1933 commented Jul 11, 2018

@texano00 , I have the same problem. Did you resolve this?

Does anyone had the same problem?

@fnhipster
Copy link

fnhipster commented Jul 11, 2018

@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 firebase/firebase-js-sdk#1001

@florent1933
Copy link

Thanks for your answer @CarlosACabrera, it's very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants