angular / angularfire

Angular + Firebase = ❤️
https://firebaseopensource.com/projects/angular/angularfire2
MIT License
7.69k stars 2.19k forks source link

Failed to connect to a new db under same project other than default #3425

Open chriskimgts opened 1 year ago

chriskimgts commented 1 year ago

Hi, do you support connecting to multiple dbs under same project? it doesn't look like it at the latest version. google forestore supports multiple dbs under a same project now. https://cloud.google.com/blog/products/databases/manage-multiple-firestore-databases-in-a-project

If planned to support it in future, when would that be happening? If I am wrong, please let me know. Thanks,

Chris

IMPORTANT! YOU MUST FOLLOW THESE INSTRUCTIONS OR YOUR ISSUE WILL BE CLOSED.

Thank you for contributing to the Angular and Firebase communities!

Have a usage question?

Yes

If the official documentation doesn't help, try asking through our officially supported channels:

Please avoid double posting across multiple channels!

Think you found a bug?

Yes

Have a feature request?

support connecting to multiple dbs under same project

Version info

What versions of the following libraries are you using? Note that your issue may already be fixed in the latest versions. Angular: 15.1.0 Firebase: 12.5.2 AngularFire: 7.6.1

Other (e.g. Ionic/Cordova, Node, browser, operating system): node: 16.15.0

How to reproduce these conditions

put a new database name into datbaseURL:

Failing test unit, Stackblitz demonstrating the problem

Provide a failing test unit, or create a minimal, complete, and verifiable example (http://stackoverflow.com/help/mcve) using StackBlitz (https://stackblitz.com/edit/angular-fire-start).

Steps to set up and reproduce enter a new database name into datbaseURL: Ex) databaseURL: 'https://test.firebaseio.com' (test is name of db)

Sample data and security rules

Expected behavior

What is the expected behavior? If dabaseURL is put such as databaseURL: 'https://test.firebaseio.com', it should connect to the test database.

Actual behavior

What is the actual behavior? it connects to default db currently.

google-oss-bot commented 1 year ago

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

davideast commented 1 year ago

Hey @chriskimgts! Can you please provide a reproducible example in StackBlitz? That would make it much easier for me to debug.

chriskimgts commented 1 year ago

Sorry I can't provide my app id and project id to StackBlitz. But I can explain since it is simple code.

I have two databases; default and test for an example under same project id called test-project. And I want to access test database. So I defined angularFireConfig such as below

angularFireConfig: {
    apiKey: 'yourapikey',
    projectId: 'test-project',
    databaseURL: 'https://test.firebaseio.com',
},

And added AngularFireModule.initializeApp(environment.angularFireConfig), to @NgModule({ imports: .... But when in my component I try to

     this.firestore
        .doc<{ version: string }>('settings/version')
        .valueChanges()
        .subscribe({
            next: ({ version }) => {
                console.log(version);
            },
            error: error => {
                console.error('Error fetching version data:', error);
            },
        });

It failed to access the test db. btw I was able to access default db with angularFireConfig: { apiKey: 'yourapikey', projectId: 'test-project', },

Please let us know if something is missing

Jeoxs commented 1 year ago

It seems that what you're trying to achieve would not be possible using angularFire. You see, It happens that I have the same issue: I want to create a dev database environment for my team's development. We're running a Blaze (paid plan) firebase project for this and I happened to find that article.

That article refers to create multiple Firestore databases in Google Cloud. Google Cloud project is not actually the same as the firebase Project. Yes, when you create a firebase project, it creates a google cloud project, but Firebase only knows about the default database:

Firebase Google Cloud
Default FS DB Default FS DB
Second FS DB

I'm not really sure if this is 100% true. So, please: Anyone with the knowledge of angularfire's code help us to clarify this!

If angularfire can only connect to firebase (and not google cloud), then you need to find a way to connect the angular project to the google cloud's firestore database natively

chriskimgts commented 1 year ago

Hi, @_davideast would you confirm if we can not access different dbs other than default db under same project yet? Thank you.

omar-dev-amrk commented 11 months ago

Here's how I managed to connect to a non-default database in the same project:

I rewrote all queries I have to use the modular syntax, and refactored away from all things compat.


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")
      )
    );
  }
}

You can also initialize more databases, and inject them as needed.