diff --git a/spec/v2/providers/database.spec.ts b/spec/v2/providers/database.spec.ts index 4435faeea..9d87bed70 100644 --- a/spec/v2/providers/database.spec.ts +++ b/spec/v2/providers/database.spec.ts @@ -26,6 +26,7 @@ import * as database from "../../../src/v2/providers/database"; import { expectType } from "../../common/metaprogramming"; import { MINIMAL_V2_ENDPOINT } from "../../fixtures"; import { CloudEvent, onInit } from "../../../src/v2/core"; +import * as params from "../../../src/params"; const RAW_RTDB_EVENT: database.RawRTDBCloudEvent = { data: { @@ -46,6 +47,9 @@ const RAW_RTDB_EVENT: database.RawRTDBCloudEvent = { authtype: "unauthenticated", }; +const TEST_RTDB_INSTANCE_ENV_VAR = "TEST_RTDB_INSTANCE_FOR_GETOPTS"; +const RESOLVED_RTDB_INSTANCE = "resolved-instance"; + describe("database", () => { describe("makeParams", () => { it("should make params with basic path", () => { @@ -146,6 +150,21 @@ describe("database", () => { }, }); }); + + it("should resolve instance Expression to runtime string", () => { + const prev = process.env[TEST_RTDB_INSTANCE_ENV_VAR]; + process.env[TEST_RTDB_INSTANCE_ENV_VAR] = RESOLVED_RTDB_INSTANCE; + try { + const p = params.defineString(TEST_RTDB_INSTANCE_ENV_VAR); + expect(database.getOpts({ ref: "/foo", instance: p })).to.deep.include({ + path: "foo", + instance: RESOLVED_RTDB_INSTANCE, + }); + } finally { + if (prev === undefined) delete process.env[TEST_RTDB_INSTANCE_ENV_VAR]; + else process.env[TEST_RTDB_INSTANCE_ENV_VAR] = prev; + } + }); }); describe("makeEndpoint", () => { diff --git a/src/v2/providers/database.ts b/src/v2/providers/database.ts index 6c5f5e83c..16b3b0dc0 100644 --- a/src/v2/providers/database.ts +++ b/src/v2/providers/database.ts @@ -117,7 +117,7 @@ export interface ReferenceOptions extends options.E * Examples: 'my-instance-1', 'my-instance-*' * Note: The capture syntax cannot be used for 'instance'. */ - instance?: string; + instance?: string | Expression; /** * If true, do not deploy or emulate this function. @@ -488,7 +488,10 @@ export function getOpts(referenceOrOpts: string | ReferenceOptions) { opts = {}; } else { path = normalizePath(referenceOrOpts.ref); - instance = referenceOrOpts.instance || "*"; + instance = + referenceOrOpts.instance instanceof Expression + ? referenceOrOpts.instance.value() + : referenceOrOpts.instance || "*"; opts = { ...referenceOrOpts }; delete (opts as any).ref; delete (opts as any).instance;