diff --git a/CHANGELOG.md b/CHANGELOG.md index 80b2f725d68..ad44b19d754 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,4 @@ +- Fix bug where deploying Firestore function resulted in redudant API calls to the Firestore API (#6583). - Fix an issue preventing Vite applications from being emulated on Windows. (#6411) - Addressed an issue preventing Astro applications from being deployed from Windows. (#5709) - Fixed an issue preventing Angular apps using ng-deploy from being emulated or deployed. (#6584) diff --git a/src/deploy/functions/services/firestore.ts b/src/deploy/functions/services/firestore.ts index d4eb39b45d5..dade0774b8e 100644 --- a/src/deploy/functions/services/firestore.ts +++ b/src/deploy/functions/services/firestore.ts @@ -2,6 +2,24 @@ import * as backend from "../backend"; import * as firestore from "../../../gcp/firestore"; import { FirebaseError } from "../../../error"; +const dbCache = new Map(); + +/** + * A memoized version of firestore.getDatabase that avoids repeated calls to the API. + * + * @param project the project ID + * @param databaseId the database ID or "(default)" + */ +async function getDatabase(project: string, databaseId: string): Promise { + const key = `${project}/${databaseId}`; + if (dbCache.has(key)) { + return dbCache.get(key)!; + } + const db = await firestore.getDatabase(project, databaseId); + dbCache.set(key, db); + return db; +} + /** * Sets a firestore event trigger's region to the firestore database region. * @param endpoint the firestore endpoint @@ -9,7 +27,7 @@ import { FirebaseError } from "../../../error"; export async function ensureFirestoreTriggerRegion( endpoint: backend.Endpoint & backend.EventTriggered ): Promise { - const db = await firestore.getDatabase( + const db = await getDatabase( endpoint.project, endpoint.eventTrigger.eventFilters?.database || "(default)" ); diff --git a/src/gcp/firestore.ts b/src/gcp/firestore.ts index e5897c75f66..c870fa95485 100644 --- a/src/gcp/firestore.ts +++ b/src/gcp/firestore.ts @@ -8,7 +8,7 @@ const apiClient = new Client({ urlPrefix: firestoreOrigin, }); -interface Database { +export interface Database { name: string; uid: string; createTime: string;