如何使用 Angular Fire 连接到第二个数据库?

How to connect to a second database using Angular Fire?

提问人:Omar Dulaimi 提问时间:11/16/2023 更新时间:11/16/2023 访问量:24

问:

我有这个代码:

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

并一直在尝试获取非默认数据库的适当 firestore 实例。我遵循了 repo 提到的建议:

从理论上讲,有三个 API 应该在新 API 中允许这样做:

  1. 您应该能够注入到组件中以获取 firestore 实例的数组。他们会的 必须在组件之前初始化。FirestoreInstances

  2. 有一个可观察的 firestoreInstance$,你可以从中导入,你可以过滤可观察的 排放来获取所需的数据库。@angular/fire/firestore

  3. 您可以使用与 JS SDK 相同的 API 来获取访问权限(getFirestore 和 initializeFirestore,请参阅 databaseId 参数),只需 从 @angular/fire/firestore 而不是 firebase/firestore 导入,因此 您将获得 Zone.js 修复。

也就是说,我们只解锁了针对 Firebase JS SDK v10 的使用 随着 AngularFire v16 的发布。所以我没有机会 在这里做实验吧。

instance上面被多次记录,其中之一是我需要的数据库。但是,物体的形状只有三个键:

apptoJSONtype

code preview

用: 角度 15 + 角度火 7.5

角度 谷歌-云-Firestore 角火2

评论


答:

1赞 Ale_Bianco 11/16/2023 #1

您可以使用该函数访问特定的 Firestore 实例。getFirestore

import { FirebaseApp } from "@angular/fire/compat";
import { AngularFirestore, getFirestore, Firestore } from "@angular/fire/compat/firestore";
import { firestoreInstance$ } from "@angular/fire/firestore";
import { Subscription } from "rxjs";

export class AppModule {
  private firestoreSubscription: Subscription | undefined;

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

    // Get access to the second Firestore instance
    const secondFirestore: Firestore = getFirestore(firebaseApp, { databaseId: 'your-second-db-name' });

    // Now you can use `secondFirestore` to interact with the second database

    // Subscribe to the firestoreInstance$ observable to get information about Firestore instances
    this.firestoreSubscription = firestoreInstance$.subscribe((instances) => {
      console.log("Firestore Instances:", instances);

      // Find the instance you need based on your criteria
      const secondInstance = instances.find((instance) => {
        // Adjust this condition based on the properties of your Firestore instance
        return instance.app.name === 'your-second-app-name';
      });

      if (secondInstance) {
        console.log("Second Firestore Instance:", secondInstance);
      }
    });
  }

  ngOnDestroy() {
    if (this.firestoreSubscription) {
      this.firestoreSubscription.unsubscribe();
    }
  }
}

评论

0赞 Omar Dulaimi 11/16/2023
getFirestore 说: 预期的 0-1 参数,但得到 2 个
0赞 Omar Dulaimi 11/16/2023
您使用的是什么版本?
1赞 Ale_Bianco 11/16/2023
我使用的是最新版本:export declare function getFirestore(app: FirebaseApp, databaseId: string): Firestore;
0赞 Ale_Bianco 11/16/2023
这是一个预览功能,但这是我发现的唯一方法
0赞 Omar Dulaimi 11/20/2023 #2

最终,我重写了所有必须使用模块化语法的查询,并重构了所有兼容的东西。下面介绍如何使代码连接到同一项目中的任何数据库:


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;
    }),
  ]
})

然后在您的服务中:

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

这还可以让您连接到多个数据库并根据需要注入它们。