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

Add option to get named firestore instance for v2 firestore functions #1550

Merged
merged 2 commits into from Apr 12, 2024
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
@@ -0,0 +1 @@
- Add option to get named firestore instance for v2 firestore functions (#1550).
27 changes: 19 additions & 8 deletions src/common/providers/firestore.ts
Expand Up @@ -52,9 +52,9 @@ function _getValueProto(data: any, resource: string, valueFieldName: string) {
}

/** @internal */
export function createSnapshotFromProtobuf(data: Uint8Array, path: string) {
export function createSnapshotFromProtobuf(data: Uint8Array, path: string, databaseId: string) {
if (!firestoreInstance) {
firestoreInstance = firestore.getFirestore(getApp());
firestoreInstance = firestore.getFirestore(databaseId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why aren't you conditioning on firestoreInstance here like in createBeforeSnapshotFromBeforeProtobuf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean conditioning on databaseId right?

Because these two *fromProtobuf functions are only called from the V2 handler and V2 events always supply the databaseId

}
try {
const dataBuffer = Buffer.from(data);
Expand All @@ -68,9 +68,13 @@ export function createSnapshotFromProtobuf(data: Uint8Array, path: string) {
}

/** @internal */
export function createBeforeSnapshotFromProtobuf(data: Uint8Array, path: string) {
export function createBeforeSnapshotFromProtobuf(
data: Uint8Array,
path: string,
databaseId: string
) {
if (!firestoreInstance) {
firestoreInstance = firestore.getFirestore(getApp());
firestoreInstance = firestore.getFirestore(databaseId);
}
try {
const dataBuffer = Buffer.from(data);
Expand All @@ -88,10 +92,13 @@ export function createSnapshotFromJson(
data: any,
source: string,
createTime: string | undefined,
updateTime: string | undefined
updateTime: string | undefined,
databaseId?: string
) {
if (!firestoreInstance) {
firestoreInstance = firestore.getFirestore(getApp());
firestoreInstance = databaseId
? firestore.getFirestore(databaseId)
: firestore.getFirestore(getApp());
}
const valueProto = _getValueProto(data, source, "value");
let timeString = createTime || updateTime;
Expand All @@ -110,11 +117,15 @@ export function createBeforeSnapshotFromJson(
data: any,
source: string,
createTime: string | undefined,
updateTime: string | undefined
updateTime: string | undefined,
databaseId?: string
) {
if (!firestoreInstance) {
firestoreInstance = firestore.getFirestore(getApp());
firestoreInstance = databaseId
? firestore.getFirestore(databaseId)
: firestore.getFirestore(getApp());
}

const oldValueProto = _getValueProto(data, source, "oldValue");
const oldReadTime = dateToTimestampProto(createTime || updateTime);
return firestoreInstance.snapshot_(oldValueProto, oldReadTime, "json");
Expand Down
14 changes: 10 additions & 4 deletions src/v2/providers/firestore.ts
Expand Up @@ -532,13 +532,14 @@ function getPath(event: RawFirestoreEvent): string {
/** @internal */
export function createSnapshot(event: RawFirestoreEvent): QueryDocumentSnapshot {
if (event.datacontenttype?.includes("application/protobuf") || Buffer.isBuffer(event.data)) {
return createSnapshotFromProtobuf(event.data as Uint8Array, getPath(event));
return createSnapshotFromProtobuf(event.data as Uint8Array, getPath(event), event.database);
} else if (event.datacontenttype?.includes("application/json")) {
return createSnapshotFromJson(
event.data,
event.source,
(event.data as RawFirestoreData).value?.createTime,
(event.data as RawFirestoreData).value?.updateTime
(event.data as RawFirestoreData).value?.updateTime,
event.database
);
} else {
logger.error(
Expand All @@ -551,13 +552,18 @@ export function createSnapshot(event: RawFirestoreEvent): QueryDocumentSnapshot
/** @internal */
export function createBeforeSnapshot(event: RawFirestoreEvent): QueryDocumentSnapshot {
if (event.datacontenttype?.includes("application/protobuf") || Buffer.isBuffer(event.data)) {
return createBeforeSnapshotFromProtobuf(event.data as Uint8Array, getPath(event));
return createBeforeSnapshotFromProtobuf(
event.data as Uint8Array,
getPath(event),
event.database
);
} else if (event.datacontenttype?.includes("application/json")) {
return createBeforeSnapshotFromJson(
event.data,
event.source,
(event.data as RawFirestoreData).oldValue?.createTime,
(event.data as RawFirestoreData).oldValue?.updateTime
(event.data as RawFirestoreData).oldValue?.updateTime,
event.database
);
} else {
logger.error(
Expand Down