@firebase-function-kits/firestore-bigquery-export 0.0.2-rc.3 → 0.0.2-rc.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firebase-function-kits/firestore-bigquery-export",
3
- "version": "0.0.2-rc.3",
3
+ "version": "0.0.2-rc.5",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/firebase/extensions.git",
@@ -37,10 +37,5 @@
37
37
  "firebase-functions": "^7.3.2",
38
38
  "generate-schema": "^2.6.0",
39
39
  "lodash": "^4.17.14"
40
- },
41
- "devDependencies": {
42
- "@types/lodash": "^4.17.0",
43
- "typescript": "^5.9.3",
44
- "vitest": "^3.2.4"
45
40
  }
46
41
  }
package/src/config.ts CHANGED
@@ -125,6 +125,63 @@ const params = {
125
125
  default: "(default)",
126
126
  input: { text: { example: "(default)" } },
127
127
  }),
128
+ // Declared so the CLI prompts for the value and persists it to `.env`; the
129
+ // function region option cannot be a param expression, so the entry point
130
+ // reads the same variable from `process.env` at module load instead.
131
+ databaseRegion: defineString("DATABASE_REGION", {
132
+ label: "Firestore Instance Location",
133
+ description:
134
+ "Where is the Firestore database located? You can check your current database location at https://console.cloud.google.com/firestore/databases. The functions in this kit deploy to the Cloud Run region closest to this location.",
135
+
136
+ input: select({
137
+ "Multi-region (Europe - Belgium and Netherlands)": "eur3",
138
+ "Multi-region (United States)": "nam5",
139
+ "Multi-region (Iowa, North Virginia, and Oklahoma)": "nam7",
140
+ "Iowa (us-central1)": "us-central1",
141
+ "Oregon (us-west1)": "us-west1",
142
+ "Los Angeles (us-west2)": "us-west2",
143
+ "Salt Lake City (us-west3)": "us-west3",
144
+ "Las Vegas (us-west4)": "us-west4",
145
+ "South Carolina (us-east1)": "us-east1",
146
+ "Northern Virginia (us-east4)": "us-east4",
147
+ "Columbus (us-east5)": "us-east5",
148
+ "Dallas (us-south1)": "us-south1",
149
+ "Montreal (northamerica-northeast1)": "northamerica-northeast1",
150
+ "Toronto (northamerica-northeast2)": "northamerica-northeast2",
151
+ "Queretaro (northamerica-south1)": "northamerica-south1",
152
+ "Sao Paulo (southamerica-east1)": "southamerica-east1",
153
+ "Santiago (southamerica-west1)": "southamerica-west1",
154
+ "Belgium (europe-west1)": "europe-west1",
155
+ "London (europe-west2)": "europe-west2",
156
+ "Frankfurt (europe-west3)": "europe-west3",
157
+ "Netherlands (europe-west4)": "europe-west4",
158
+ "Zurich (europe-west6)": "europe-west6",
159
+ "Milan (europe-west8)": "europe-west8",
160
+ "Paris (europe-west9)": "europe-west9",
161
+ "Berlin (europe-west10)": "europe-west10",
162
+ "Turin (europe-west12)": "europe-west12",
163
+ "Madrid (europe-southwest1)": "europe-southwest1",
164
+ "Finland (europe-north1)": "europe-north1",
165
+ "Stockholm (europe-north2)": "europe-north2",
166
+ "Warsaw (europe-central2)": "europe-central2",
167
+ "Doha (me-central1)": "me-central1",
168
+ "Dammam (me-central2)": "me-central2",
169
+ "Tel Aviv (me-west1)": "me-west1",
170
+ "Mumbai (asia-south1)": "asia-south1",
171
+ "Delhi (asia-south2)": "asia-south2",
172
+ "Singapore (asia-southeast1)": "asia-southeast1",
173
+ "Jakarta (asia-southeast2)": "asia-southeast2",
174
+ "Kuala Lumpur (asia-southeast3)": "asia-southeast3",
175
+ "Taiwan (asia-east1)": "asia-east1",
176
+ "Hong Kong (asia-east2)": "asia-east2",
177
+ "Tokyo (asia-northeast1)": "asia-northeast1",
178
+ "Osaka (asia-northeast2)": "asia-northeast2",
179
+ "Seoul (asia-northeast3)": "asia-northeast3",
180
+ "Sydney (australia-southeast1)": "australia-southeast1",
181
+ "Melbourne (australia-southeast2)": "australia-southeast2",
182
+ "Johannesburg (africa-south1)": "africa-south1",
183
+ }),
184
+ }),
128
185
  collectionPath: defineString("COLLECTION_PATH", {
129
186
  label: "Collection path",
130
187
  description:
@@ -69,7 +69,10 @@ export interface ExportConfig {
69
69
  partitioning?: ChangeTrackerConfig["partitioning"];
70
70
  /** Clustering columns (max 4). */
71
71
  clustering?: string[] | null;
72
- /** Materialized-view max staleness interval, e.g. `0-0 0 4:0:0`. */
72
+ /**
73
+ * Materialized-view max staleness interval, e.g.
74
+ * `INTERVAL "8:0:0" HOUR TO SECOND`.
75
+ */
73
76
  maxStaleness?: ConfigValue<string>;
74
77
  /** Incremental materialized-view refresh interval in minutes. */
75
78
  refreshIntervalMinutes?: ConfigValue<number>;
package/src/index.ts CHANGED
@@ -44,6 +44,7 @@ import { resolveExportConfig, toTrackerConfig } from "./export-config";
44
44
  import { type HandlerContext, handleDocumentWrite } from "./handlers";
45
45
  import { createEnsureInitialized } from "./init";
46
46
  import * as logs from "./logs";
47
+ import { firestoreLocationToFunctionRegion } from "./region";
47
48
 
48
49
  // Re-export the side-effect-free library surface (handlers and config types).
49
50
  export * from "./lib";
@@ -117,12 +118,16 @@ function getHandlerContext(): HandlerContext {
117
118
  }
118
119
 
119
120
  /*
120
- * None of the exported functions set `region`. Firestore locations such as
121
- * `nam5` are not Cloud Run regions, so the database location must never become
122
- * a function region. The Firebase CLI resolves each function's deploy region
123
- * itself and pins the Firestore trigger to the database's own region via the
124
- * `database` option.
121
+ * Read at module load: the CLI populates `.env` values into the discovery
122
+ * process env (firebase-tools >= 15.28.0), and the region option cannot be a
123
+ * param expression. When unset, no function declares a region and the CLI
124
+ * falls back to its default. The Eventarc trigger region needs no handling:
125
+ * the CLI pins it to the database's own region regardless of where the
126
+ * function runs.
125
127
  */
128
+ const functionRegion = firestoreLocationToFunctionRegion(
129
+ process.env.DATABASE_REGION
130
+ );
126
131
 
127
132
  /**
128
133
  * Firestore trigger: streams document writes on the watched collection into the
@@ -131,6 +136,7 @@ function getHandlerContext(): HandlerContext {
131
136
  */
132
137
  export const fsexportbigquery = onDocumentWritten(
133
138
  {
139
+ ...(functionRegion ? { region: functionRegion } : {}),
134
140
  document: expr`${CONFIG_EXPRESSIONS.collectionPath}/{documentId}`,
135
141
  database: CONFIG_EXPRESSIONS.database,
136
142
  retry: true,
@@ -158,6 +164,7 @@ async function handleBigQuerySyncInitialization(): Promise<void> {
158
164
  */
159
165
  export const initBigQuerySync = onTaskDispatched(
160
166
  {
167
+ ...(functionRegion ? { region: functionRegion } : {}),
161
168
  retryConfig: LIFECYCLE_RETRY_CONFIG,
162
169
  },
163
170
  handleBigQuerySyncInitialization
@@ -169,6 +176,7 @@ export const initBigQuerySync = onTaskDispatched(
169
176
  */
170
177
  export const setupBigQuerySync = onTaskDispatched(
171
178
  {
179
+ ...(functionRegion ? { region: functionRegion } : {}),
172
180
  retryConfig: LIFECYCLE_RETRY_CONFIG,
173
181
  },
174
182
  handleBigQuerySyncInitialization
package/src/region.ts ADDED
@@ -0,0 +1,37 @@
1
+ /*
2
+ * Copyright 2019 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ // Firestore multi-region locations are not Cloud Run regions; deploying a
18
+ // function to one hard-fails, so they map to a region inside the multi-region.
19
+ const MULTI_REGION_TO_FUNCTION_REGION: Record<string, string> = {
20
+ nam5: "us-central1",
21
+ nam7: "us-central1",
22
+ eur3: "europe-west1",
23
+ };
24
+
25
+ /**
26
+ * Maps a Firestore database location to the Cloud Run region the functions
27
+ * should deploy to. Regional locations pass through unchanged; an unset or
28
+ * empty location returns `undefined`, meaning the functions declare no region.
29
+ */
30
+ export function firestoreLocationToFunctionRegion(
31
+ location: string | undefined
32
+ ): string | undefined {
33
+ if (!location) {
34
+ return undefined;
35
+ }
36
+ return MULTI_REGION_TO_FUNCTION_REGION[location] ?? location;
37
+ }
@@ -14,7 +14,7 @@
14
14
  * limitations under the License.
15
15
  */
16
16
 
17
- import { describe, expect, test, vi } from "vitest";
17
+ import { afterEach, describe, expect, test, vi } from "vitest";
18
18
 
19
19
  vi.mock("firebase-functions/firestore", () => ({
20
20
  onDocumentWritten: vi.fn(() => ({})),
@@ -31,38 +31,96 @@ vi.mock("firebase-functions/v2/lifecycle", () => ({
31
31
  afterRedeploy: vi.fn(),
32
32
  }));
33
33
 
34
- import { onDocumentWritten } from "firebase-functions/firestore";
35
- import { onTaskDispatched } from "firebase-functions/tasks";
36
- import "../src/index";
34
+ type FunctionOptions = Record<string, unknown>;
37
35
 
38
- function triggerOptions(): Record<string, unknown> {
39
- const call = vi.mocked(onDocumentWritten).mock.calls[0];
40
- return call[0] as unknown as Record<string, unknown>;
36
+ interface ExportedOptions {
37
+ trigger: FunctionOptions;
38
+ tasks: FunctionOptions[];
41
39
  }
42
40
 
43
- function taskOptions(): Array<Record<string, unknown>> {
44
- return vi
45
- .mocked(onTaskDispatched)
46
- .mock.calls.map((call) => call[0] as unknown as Record<string, unknown>);
41
+ const originalDatabaseRegion = process.env.DATABASE_REGION;
42
+
43
+ afterEach(() => {
44
+ if (originalDatabaseRegion === undefined) {
45
+ delete process.env.DATABASE_REGION;
46
+ } else {
47
+ process.env.DATABASE_REGION = originalDatabaseRegion;
48
+ }
49
+ });
50
+
51
+ async function loadExportedOptions(
52
+ databaseRegion?: string
53
+ ): Promise<ExportedOptions> {
54
+ vi.resetModules();
55
+ if (databaseRegion === undefined) {
56
+ delete process.env.DATABASE_REGION;
57
+ } else {
58
+ process.env.DATABASE_REGION = databaseRegion;
59
+ }
60
+
61
+ await import("../src/index");
62
+ const { onDocumentWritten } = await import("firebase-functions/firestore");
63
+ const { onTaskDispatched } = await import("firebase-functions/tasks");
64
+
65
+ const triggerCalls = vi.mocked(onDocumentWritten).mock.calls;
66
+ const taskCalls = vi.mocked(onTaskDispatched).mock.calls;
67
+ const trigger = triggerCalls[triggerCalls.length - 1][0] as FunctionOptions;
68
+ const tasks = taskCalls
69
+ .slice(-2)
70
+ .map((call) => call[0] as unknown as FunctionOptions);
71
+
72
+ expect(tasks).toHaveLength(2);
73
+ return { trigger, tasks };
74
+ }
75
+
76
+ function allOptions({ trigger, tasks }: ExportedOptions): FunctionOptions[] {
77
+ return [trigger, ...tasks];
47
78
  }
48
79
 
49
80
  describe("exported function options", () => {
50
- test("no function sets a region (DATABASE_REGION values like nam5 are Firestore locations, not Cloud Run regions)", () => {
51
- expect(triggerOptions()).not.toHaveProperty("region");
81
+ test.each([
82
+ ["nam5", "us-central1"],
83
+ ["nam7", "us-central1"],
84
+ ["eur3", "europe-west1"],
85
+ ])(
86
+ "multi-region DATABASE_REGION %s deploys every function to %s",
87
+ async (databaseRegion, expectedRegion) => {
88
+ const options = await loadExportedOptions(databaseRegion);
89
+ for (const opts of allOptions(options)) {
90
+ expect(opts.region).toBe(expectedRegion);
91
+ }
92
+ }
93
+ );
94
+
95
+ test("regional DATABASE_REGION passes through to every function", async () => {
96
+ const options = await loadExportedOptions("europe-west1");
97
+ for (const opts of allOptions(options)) {
98
+ expect(opts.region).toBe("europe-west1");
99
+ }
100
+ });
101
+
102
+ test("unset DATABASE_REGION leaves every function without a region", async () => {
103
+ const options = await loadExportedOptions();
104
+ for (const opts of allOptions(options)) {
105
+ expect(opts).not.toHaveProperty("region");
106
+ }
107
+ });
52
108
 
53
- const tasks = taskOptions();
54
- expect(tasks).toHaveLength(2);
55
- for (const opts of tasks) {
109
+ test("empty DATABASE_REGION leaves every function without a region", async () => {
110
+ const options = await loadExportedOptions("");
111
+ for (const opts of allOptions(options)) {
56
112
  expect(opts).not.toHaveProperty("region");
57
113
  }
58
114
  });
59
115
 
60
- test("the trigger binds to the configured database instance", () => {
61
- expect(String(triggerOptions().database)).toBe("params.DATABASE");
116
+ test("the trigger binds to the configured database instance", async () => {
117
+ const { trigger } = await loadExportedOptions();
118
+ expect(String(trigger.database)).toBe("params.DATABASE");
62
119
  });
63
120
 
64
- test("the trigger watches the configured collection path", () => {
65
- const document = triggerOptions().document as { toCEL(): string };
121
+ test("the trigger watches the configured collection path", async () => {
122
+ const { trigger } = await loadExportedOptions();
123
+ const document = trigger.document as { toCEL(): string };
66
124
  expect(document.toCEL()).toContain("params.COLLECTION_PATH");
67
125
  });
68
126
  });
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright 2026 Google LLC
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ import { describe, expect, test } from "vitest";
18
+
19
+ import { firestoreLocationToFunctionRegion } from "../src/region";
20
+
21
+ describe("firestoreLocationToFunctionRegion", () => {
22
+ test.each([
23
+ ["nam5", "us-central1"],
24
+ ["nam7", "us-central1"],
25
+ ["eur3", "europe-west1"],
26
+ ])("maps the multi-region location %s to %s", (location, region) => {
27
+ expect(firestoreLocationToFunctionRegion(location)).toBe(region);
28
+ });
29
+
30
+ test.each(["us-central1", "europe-west1", "asia-northeast1", "us-east1"])(
31
+ "passes the regional location %s through unchanged",
32
+ (location) => {
33
+ expect(firestoreLocationToFunctionRegion(location)).toBe(location);
34
+ }
35
+ );
36
+
37
+ test("returns undefined for an unset location", () => {
38
+ expect(firestoreLocationToFunctionRegion(undefined)).toBeUndefined();
39
+ });
40
+
41
+ test("returns undefined for an empty location", () => {
42
+ expect(firestoreLocationToFunctionRegion("")).toBeUndefined();
43
+ });
44
+ });
@@ -1 +1 @@
1
- {"root":["./src/config.ts","./src/events.ts","./src/export-config.ts","./src/handlers.ts","./src/index.ts","./src/init.ts","./src/lib.ts","./src/logs.ts","./src/util.ts"],"version":"5.9.3"}
1
+ {"root":["./src/config.ts","./src/events.ts","./src/export-config.ts","./src/handlers.ts","./src/index.ts","./src/init.ts","./src/lib.ts","./src/logs.ts","./src/region.ts","./src/util.ts"],"version":"5.9.3"}