Skip to content

Commit bcf41c6

Browse files
authored
Fixes an emulator issue where snapshot.ref could not point to multiple databases (#1339)
* yank first condition * update changelog * add emulator instance creation * run prettier
1 parent dc4f917 commit bcf41c6

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixes an emulator issue where snapshot.ref could not point to multiple databases (#1339).

src/common/providers/database.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ export class DataSnapshot implements database.DataSnapshot {
5050
instance?: string
5151
) {
5252
const config = firebaseConfig();
53-
if (app?.options?.databaseURL?.startsWith("http:")) {
54-
// In this case we're dealing with an emulator
55-
this.instance = app.options.databaseURL;
56-
} else if (instance) {
53+
if (instance) {
5754
// SDK always supplies instance, but user's unit tests may not
5855
this.instance = instance;
5956
} else if (app) {

src/v2/providers/database.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ export function onChangedOperation<Ref extends string>(
465465
// wrap the handler
466466
const func = (raw: CloudEvent<unknown>) => {
467467
const event = raw as RawRTDBCloudEvent;
468-
const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
468+
const instanceUrl = getInstance(event);
469469
const params = makeParams(event, pathPattern, instancePattern) as unknown as ParamsOf<Ref>;
470470
const databaseEvent = makeChangedDatabaseEvent(event, instanceUrl, params);
471471
return wrapTraceContext(handler)(databaseEvent);
@@ -492,7 +492,7 @@ export function onOperation<Ref extends string>(
492492
// wrap the handler
493493
const func = (raw: CloudEvent<unknown>) => {
494494
const event = raw as RawRTDBCloudEvent;
495-
const instanceUrl = `https://${event.instance}.${event.firebasedatabasehost}`;
495+
const instanceUrl = getInstance(event);
496496
const params = makeParams(event, pathPattern, instancePattern) as unknown as ParamsOf<Ref>;
497497
const data = eventType === deletedEventType ? event.data.data : event.data.delta;
498498
const databaseEvent = makeDatabaseEvent(event, data, instanceUrl, params);
@@ -505,3 +505,10 @@ export function onOperation<Ref extends string>(
505505

506506
return func;
507507
}
508+
509+
function getInstance(event: RawRTDBCloudEvent) {
510+
const emuHost = process.env.FIREBASE_DATABASE_EMULATOR_HOST;
511+
return emuHost
512+
? `http://${emuHost}/?ns=${event.instance}`
513+
: `https://${event.instance}.${event.firebasedatabasehost}`;
514+
}

0 commit comments

Comments
 (0)