Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions spec/v2/providers/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
7 changes: 5 additions & 2 deletions src/v2/providers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export interface ReferenceOptions<Ref extends string = string> extends options.E
* Examples: 'my-instance-1', 'my-instance-*'
* Note: The capture syntax cannot be used for 'instance'.
*/
instance?: string;
instance?: string | Expression<string>;

/**
* If true, do not deploy or emulate this function.
Expand Down Expand Up @@ -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;
Expand Down
Loading