Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize firestore.getDatabase API calls when deploying Firestore functions #6583

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
20 changes: 19 additions & 1 deletion src/deploy/functions/services/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,32 @@
import * as firestore from "../../../gcp/firestore";
import { FirebaseError } from "../../../error";

const dbCache = new Map<string, firestore.Database>();

/**
* 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<firestore.Database> {
const key = `${project}/${databaseId}`;
if (dbCache.has(key)) {
return dbCache.get(key)!;

Check warning on line 16 in src/deploy/functions/services/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Forbidden non-null assertion
}
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
*/
export async function ensureFirestoreTriggerRegion(
endpoint: backend.Endpoint & backend.EventTriggered
): Promise<void> {
const db = await firestore.getDatabase(
const db = await getDatabase(
endpoint.project,
endpoint.eventTrigger.eventFilters?.database || "(default)"
);
Expand Down
2 changes: 1 addition & 1 deletion src/gcp/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
urlPrefix: firestoreOrigin,
});

interface Database {
export interface Database {
name: string;
uid: string;
createTime: string;
Expand Down Expand Up @@ -57,7 +57,7 @@
pageSize: 2147483647,
};

return apiClient.post<any, { collectionIds?: string[] }>(url, data).then((res) => {

Check warning on line 60 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
return res.body.collectionIds || [];
});
}
Expand All @@ -71,8 +71,8 @@
* @param {object} doc a Document object to delete.
* @return {Promise} a promise for the delete operation.
*/
export async function deleteDocument(doc: any): Promise<any> {

Check warning on line 74 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type

Check warning on line 74 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
return apiClient.delete(doc.name);

Check warning on line 75 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check warning on line 75 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .name on an `any` value
}

/**
Expand All @@ -85,14 +85,14 @@
* @param {object[]} docs an array of Document objects to delete.
* @return {Promise<number>} a promise for the number of deleted documents.
*/
export async function deleteDocuments(project: string, docs: any[]): Promise<number> {

Check warning on line 88 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
const url = "projects/" + project + "/databases/(default)/documents:commit";

const writes = docs.map((doc) => {
return { delete: doc.name };

Check warning on line 92 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

Check warning on line 92 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .name on an `any` value
});
const data = { writes };

const res = await apiClient.post<any, { writeResults: any[] }>(url, data);

Check warning on line 96 in src/gcp/firestore.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
return res.body.writeResults.length;
}