Skip to content

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

Closed

Description

@JanOschii

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.

Activity

  1. jamesdaniels commented on Apr 19, 2018

    @jamesdaniels
    Contributor

    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.

  2. fnhipster commented on Apr 30, 2018

    @fnhipster

    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.

  3. fnhipster commented on Apr 30, 2018

    @fnhipster

    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();
    
  4. swftvsn commented on May 2, 2018

    @swftvsn

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

  5. JanOschii commented on May 3, 2018

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

  6. swftvsn commented on May 3, 2018

    @swftvsn

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

  7. jamesdaniels commented on May 4, 2018

    @jamesdaniels
    Contributor

    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.

  8. texano00 commented on Jun 18, 2018

    @texano00

    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

  9. florent1933 commented on Jul 11, 2018

    @florent1933

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

    Does anyone had the same problem?

  10. fnhipster commented on Jul 11, 2018

    @fnhipster

    @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

  11. florent1933 commented on Jul 11, 2018

    @florent1933

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions