angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

Use multiple firestore databases

Open rubenheymans opened this issue 1 year ago • 8 comments

Version info

Angular: 16.2.9

Firebase: 9.23.0

AngularFire: 7.6.1

We can now create multiple firestore databases. The documentation mentions we can access a named database with a client library, how do we do this in AngularFire? https://cloud.google.com/firestore/docs/manage-databases#access_a_named_database_with_a_client_library

Can we add a new option to firebaseConfig?

 production: false,
  environment: 'dev',
  firebaseConfig: {
    apiKey: '',
    authDomain: '',
    databaseURL: '',
    projectId: '',
    storageBucket: '',
    messagingSenderId: '',
    appId: '',
    extraOptionHere??
  },
  useEmulators: true,
  origin: undefined,

rubenheymans avatar Oct 26 '23 08:10 rubenheymans

This issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot avatar Oct 26 '23 08:10 google-oss-bot

In theory there are three APIs that should allow this in the new API:

  1. You should be able to inject FirestoreInstances into your component to get an array of the firestore instances. They would have had to have been initialized before your component
  2. There's an observable, firestoreInstance$ which you can import from @angular/fire/firestore, you can filter the observable emissions to grab the database you want.
  3. You can use the same API as you would the JS SDK to get access (getFirestore and initializeFirestore, see the databaseId arg), just import from @angular/fire/firestore rather than firebase/firestore so you get Zone.js fixes

That said, we only unblocked use against Firebase JS SDK v10 the other day with the release of AngularFire v16. So I've not had a chance to experiment here yet.

jamesdaniels avatar Oct 27 '23 23:10 jamesdaniels

@jamesdaniels is there any way of doing it by importing modules? Like shown here https://stackoverflow.com/questions/77377149/how-to-access-non-default-firestore-database-in-angularfire

chumberjosh avatar Oct 27 '23 23:10 chumberjosh

@chumberjosh I don't believe there's a compat API to be able to access these. You will need to get/initializeFirestore with the modular API to setup access. That said you should be able to mix and match the modular and compat APIs as needed, just means a little work to migrate your code.

jamesdaniels avatar Oct 27 '23 23:10 jamesdaniels

Hey @jamesdaniels Could you show a code example please?

I'm on v7.5

omar-dev-amrk avatar Nov 15 '23 08:11 omar-dev-amrk

I have this code so far:

import { FirebaseApp } from "@angular/fire/compat";
import {
  AngularFirestore
} from "@angular/fire/compat//firestore";

import {
  initializeFirestore
} from "@angular/fire/firestore";

import { firestoreInstance$ } from "@angular/fire/firestore";

export class AppModule {
  constructor(private firestore: AngularFirestore) {
    const firebaseApp: FirebaseApp = this.firestore.firestore.app;

    initializeFirestore(firebaseApp, {}, secondDbName);

    firestoreInstance$.subscribe((instance) => {
      console.log("instance:", instance);
    });
  }
}

And both the default and second instances are getting logged. But instance field does not provide Firestore fields like collection and others:

image

What should I do to surface them?

omar-dev-amrk avatar Nov 15 '23 10:11 omar-dev-amrk

Eventually, I rewrote all queries I have to use the modular syntax, and refactored away from all things compat. Here's how you make your code connected to any database in the same project:


import { getApp, initializeApp, provideFirebaseApp } from "@angular/fire/app";
import { initializeFirestore, provideFirestore } from "@angular/fire/firestore";

@NgModule({
  imports: [
    provideFirebaseApp(() => initializeApp(config)),
    provideFirestore(() => {
      const app = getApp();
      const dbName = "db-name";
      const providedFirestore = initializeFirestore(app, {}, dbName);
      return providedFirestore;
    }),
  ]
})

Then in your services:

import { inject } from "@angular/core";
import { Firestore, getDocs } from "@angular/fire/firestore";
import {
  collection,
  query,
  where,
  orderBy,
} from "@angular/fire/firestore";

@Injectable({
  providedIn: "root",
})
export class MyService {
  private firestore: Firestore = inject(Firestore);

  getUsers() {
    return getDocs(
      query(
        collection(this.firestore, "Users"),
        where("some_id", "==", "id"),
        orderBy("created_at", "asc")
      )
    );
  }
}

omar-dev-amrk avatar Nov 20 '23 13:11 omar-dev-amrk

how i would reference a differente db ? i think im missing something, because in getUsers() how i would tell that i want form a specific db?

maev-mcbo avatar Apr 08 '24 01:04 maev-mcbo