@firebase-function-kits/firestore-bigquery-export 0.0.1 → 0.0.2-rc.0
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/CHANGELOG.md +1 -0
- package/README.md +234 -0
- package/lib/config.d.ts +33 -0
- package/lib/config.d.ts.map +1 -0
- package/lib/config.js +345 -0
- package/lib/config.js.map +1 -0
- package/lib/events.d.ts +45 -0
- package/lib/events.d.ts.map +1 -0
- package/lib/events.js +154 -0
- package/lib/events.js.map +1 -0
- package/lib/export-config.d.ts +96 -0
- package/lib/export-config.d.ts.map +1 -0
- package/lib/export-config.js +77 -0
- package/lib/export-config.js.map +1 -0
- package/lib/handlers.d.ts +29 -0
- package/lib/handlers.d.ts.map +1 -0
- package/lib/handlers.js +165 -0
- package/lib/handlers.js.map +1 -0
- package/lib/index.d.ts +23 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +179 -0
- package/lib/index.js.map +1 -0
- package/lib/init.d.ts +16 -0
- package/lib/init.d.ts.map +1 -0
- package/lib/init.js +44 -0
- package/lib/init.js.map +1 -0
- package/lib/lib.d.ts +20 -0
- package/lib/lib.d.ts.map +1 -0
- package/lib/lib.js +47 -0
- package/lib/lib.js.map +1 -0
- package/lib/logs.d.ts +39 -0
- package/lib/logs.d.ts.map +1 -0
- package/lib/logs.js +186 -0
- package/lib/logs.js.map +1 -0
- package/lib/util.d.ts +23 -0
- package/lib/util.d.ts.map +1 -0
- package/lib/util.js +66 -0
- package/lib/util.js.map +1 -0
- package/package.json +33 -3
- package/src/config.ts +420 -0
- package/src/events.ts +146 -0
- package/src/export-config.ts +205 -0
- package/src/handlers.ts +211 -0
- package/src/index.ts +174 -0
- package/src/init.ts +45 -0
- package/src/lib.ts +55 -0
- package/src/logs.ts +233 -0
- package/src/util.ts +64 -0
- package/tests/config.test.ts +182 -0
- package/tests/events.test.ts +84 -0
- package/tests/export-config.test.ts +114 -0
- package/tests/handlers.test.ts +220 -0
- package/tests/init.test.ts +76 -0
- package/tests/util.test.ts +102 -0
- package/tsconfig.json +18 -0
- package/tsconfig.tsbuildinfo +1 -0
package/src/logs.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
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
|
+
import {
|
|
18
|
+
type ChangeType,
|
|
19
|
+
Logger,
|
|
20
|
+
} from "@firebaseextensions/firestore-bigquery-change-tracker";
|
|
21
|
+
|
|
22
|
+
export const logger = new Logger();
|
|
23
|
+
|
|
24
|
+
export const arrayFieldInvalid = (fieldName: string) => {
|
|
25
|
+
logger.warn(`Array field '${fieldName}' does not contain an array, skipping`);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const bigQueryDatasetCreated = (datasetId: string) => {
|
|
29
|
+
logger.info(`Created BigQuery dataset: ${datasetId}`);
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const bigQueryDatasetCreating = (datasetId: string) => {
|
|
33
|
+
logger.debug(`Creating BigQuery dataset: ${datasetId}`);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const bigQueryDatasetExists = (datasetId: string) => {
|
|
37
|
+
logger.info(`BigQuery dataset already exists: ${datasetId}`);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const bigQueryErrorRecordingDocumentChange = (e: Error) => {
|
|
41
|
+
logger.error(`Error recording document changes.`, e);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const bigQueryLatestSnapshotViewQueryCreated = (query: string) => {
|
|
45
|
+
logger.info(`BigQuery latest snapshot view query:\n${query}`);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const bigQueryTableAlreadyExists = (
|
|
49
|
+
tableName: string,
|
|
50
|
+
datasetName: string
|
|
51
|
+
) => {
|
|
52
|
+
logger.info(
|
|
53
|
+
`BigQuery table with name ${tableName} already ` +
|
|
54
|
+
`exists in dataset ${datasetName}!`
|
|
55
|
+
);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export const bigQueryTableCreated = (tableName: string) => {
|
|
59
|
+
logger.info(`Created BigQuery table: ${tableName}`);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const bigQueryTableCreating = (tableName: string) => {
|
|
63
|
+
logger.debug(`Creating BigQuery table: ${tableName}`);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const bigQueryTableUpdated = (tableName: string) => {
|
|
67
|
+
logger.info(`Updated existing BigQuery table: ${tableName}`);
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export const bigQueryTableUpdating = (tableName: string) => {
|
|
71
|
+
logger.debug(`Updating existing BigQuery table: ${tableName}`);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export const bigQueryTableUpToDate = (tableName: string) => {
|
|
75
|
+
logger.info(`BigQuery table: ${tableName} is up to date`);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const bigQueryTableValidated = (tableName: string) => {
|
|
79
|
+
logger.info(`Validated existing BigQuery table: ${tableName}`);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const bigQueryTableValidating = (tableName: string) => {
|
|
83
|
+
logger.debug(`Validating existing BigQuery table: ${tableName}`);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const bigQueryUserDefinedFunctionCreating = (
|
|
87
|
+
functionDefinition: string
|
|
88
|
+
) => {
|
|
89
|
+
logger.debug(
|
|
90
|
+
`Creating BigQuery User-defined Function:\n${functionDefinition}`
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export const bigQueryUserDefinedFunctionCreated = (
|
|
95
|
+
functionDefinition: string
|
|
96
|
+
) => {
|
|
97
|
+
logger.info(`Created BigQuery User-defined Function:\n${functionDefinition}`);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
export const bigQueryViewCreated = (viewName: string) => {
|
|
101
|
+
logger.info(`Created BigQuery view: ${viewName}`);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const bigQueryViewCreating = (viewName: string) => {
|
|
105
|
+
logger.debug(`Creating BigQuery view: ${viewName}`);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export const bigQueryViewAlreadyExists = (
|
|
109
|
+
viewName: string,
|
|
110
|
+
datasetName: string
|
|
111
|
+
) => {
|
|
112
|
+
logger.info(
|
|
113
|
+
`View with id ${viewName} already exists in dataset ${datasetName}.`
|
|
114
|
+
);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
export const bigQueryViewUpdated = (viewName: string) => {
|
|
118
|
+
logger.info(`Updated existing BigQuery view: ${viewName}`);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const bigQueryViewUpdating = (viewName: string) => {
|
|
122
|
+
logger.debug(`Updating existing BigQuery view: ${viewName}`);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const bigQueryViewUpToDate = (viewName: string) => {
|
|
126
|
+
logger.info(`BigQuery view: ${viewName} is up to date`);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export const bigQueryViewValidated = (viewName: string) => {
|
|
130
|
+
logger.info(`Validated existing BigQuery view: ${viewName}`);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const bigQueryViewValidating = (viewName: string) => {
|
|
134
|
+
logger.debug(`Validating existing BigQuery view: ${viewName}`);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const complete = () => {
|
|
138
|
+
logger.info("Completed execution of extension");
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const dataInserted = (rowCount: number) => {
|
|
142
|
+
logger.debug(`Inserted ${rowCount} row(s) of data into BigQuery`);
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const dataInserting = (rowCount: number) => {
|
|
146
|
+
logger.debug(`Inserting ${rowCount} row(s) of data into BigQuery`);
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export const dataTypeInvalid = (
|
|
150
|
+
fieldName: string,
|
|
151
|
+
fieldType: string,
|
|
152
|
+
dataType: string
|
|
153
|
+
) => {
|
|
154
|
+
logger.warn(
|
|
155
|
+
`Field '${fieldName}' has invalid data. Expected: ${fieldType}, received: ${dataType}`
|
|
156
|
+
);
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export const error = (
|
|
160
|
+
includeEvent: boolean,
|
|
161
|
+
message: string,
|
|
162
|
+
err: Error,
|
|
163
|
+
event?: any, // Made optional, as it is not always required
|
|
164
|
+
eventTrackerConfig?: any // Made optional, as it is not always required
|
|
165
|
+
) => {
|
|
166
|
+
const logDetails: Record<string, any> = { error: err };
|
|
167
|
+
|
|
168
|
+
if (includeEvent && event) {
|
|
169
|
+
logDetails.event = event;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (includeEvent && eventTrackerConfig) {
|
|
173
|
+
logDetails.eventTrackerConfig = eventTrackerConfig;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
logger.error(`Error when mirroring data to BigQuery: ${message}`, logDetails);
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const init = (config?: unknown) => {
|
|
180
|
+
logger.info("Initializing with configuration", config);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const start = (config?: unknown) => {
|
|
184
|
+
logger.info("Started execution with configuration", config);
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
export const timestampMissingValue = (fieldName: string) => {
|
|
188
|
+
logger.warn(
|
|
189
|
+
`Missing value for timestamp field: ${fieldName}, using default timestamp instead.`
|
|
190
|
+
);
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const logEventAction = (
|
|
194
|
+
action: string,
|
|
195
|
+
document_name: string,
|
|
196
|
+
event_id: string,
|
|
197
|
+
operation: ChangeType
|
|
198
|
+
) => {
|
|
199
|
+
logger.info(action, {
|
|
200
|
+
document_name,
|
|
201
|
+
event_id,
|
|
202
|
+
operation,
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
export const logFailedEventAction = (
|
|
207
|
+
action: string,
|
|
208
|
+
document_name: string,
|
|
209
|
+
event_id: string,
|
|
210
|
+
operation: ChangeType,
|
|
211
|
+
error: Error
|
|
212
|
+
) => {
|
|
213
|
+
const changeTypeMap = {
|
|
214
|
+
0: "CREATE",
|
|
215
|
+
1: "DELETE",
|
|
216
|
+
2: "UPDATE",
|
|
217
|
+
3: "IMPORT",
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
logger.error(action, {
|
|
221
|
+
document_name,
|
|
222
|
+
event_id,
|
|
223
|
+
operation: changeTypeMap[operation],
|
|
224
|
+
error,
|
|
225
|
+
});
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
export const failedToWriteToBigQueryImmediately = (error: Error) => {
|
|
229
|
+
logger.warn(
|
|
230
|
+
"Failed to write event to BigQuery Immediately. Will attempt to Enqueue to Cloud Tasks.",
|
|
231
|
+
error
|
|
232
|
+
);
|
|
233
|
+
};
|
package/src/util.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
import { ChangeType } from "@firebaseextensions/firestore-bigquery-change-tracker";
|
|
18
|
+
import type { Change, DocumentSnapshot } from "firebase-functions/firestore";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Get the change type (CREATE, UPDATE, DELETE) from the Firestore change.
|
|
22
|
+
* @param change Firestore document change object.
|
|
23
|
+
* @returns {ChangeType} The type of change.
|
|
24
|
+
*/
|
|
25
|
+
export function getChangeType(change: Change<DocumentSnapshot>): ChangeType {
|
|
26
|
+
if (!change.after.exists) {
|
|
27
|
+
return ChangeType.DELETE;
|
|
28
|
+
}
|
|
29
|
+
if (!change.before.exists) {
|
|
30
|
+
return ChangeType.CREATE;
|
|
31
|
+
}
|
|
32
|
+
return ChangeType.UPDATE;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Get the document ID from the Firestore change.
|
|
37
|
+
* @param change Firestore document change object.
|
|
38
|
+
* @returns {string} The document ID.
|
|
39
|
+
*/
|
|
40
|
+
export function getDocumentId(change: Change<DocumentSnapshot>): string {
|
|
41
|
+
if (change.after.exists) {
|
|
42
|
+
return change.after.id;
|
|
43
|
+
}
|
|
44
|
+
return change.before.id;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
*
|
|
49
|
+
* @param template - eg, regions/{regionId}/countries
|
|
50
|
+
* @param text - eg, regions/asia/countries
|
|
51
|
+
*
|
|
52
|
+
* @return - eg, { regionId: "asia" }
|
|
53
|
+
*/
|
|
54
|
+
export const resolveWildcardIds = (template: string, text: string) => {
|
|
55
|
+
const textSegments = text.split("/");
|
|
56
|
+
return template
|
|
57
|
+
.split("/")
|
|
58
|
+
.reduce((previousValue, currentValue, currentIndex) => {
|
|
59
|
+
if (currentValue.startsWith("{") && currentValue.endsWith("}")) {
|
|
60
|
+
previousValue[currentValue.slice(1, -1)] = textSegments[currentIndex];
|
|
61
|
+
}
|
|
62
|
+
return previousValue;
|
|
63
|
+
}, {});
|
|
64
|
+
};
|
|
@@ -0,0 +1,182 @@
|
|
|
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, vi } from "vitest";
|
|
18
|
+
|
|
19
|
+
vi.mock("firebase-functions/params", () => ({
|
|
20
|
+
Expression: class Expression<T> {
|
|
21
|
+
value(): T {
|
|
22
|
+
return this.runtimeValue();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
runtimeValue(): T {
|
|
26
|
+
throw new Error("Not implemented");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
toCEL(): string {
|
|
30
|
+
return `{{ ${this.toString()} }}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
toJSON(): string {
|
|
34
|
+
return this.toString();
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
defineString: (
|
|
38
|
+
_name: string,
|
|
39
|
+
opts?: { default?: string | { value(): string } }
|
|
40
|
+
) => ({
|
|
41
|
+
value: () =>
|
|
42
|
+
typeof opts?.default === "string"
|
|
43
|
+
? opts.default
|
|
44
|
+
: opts?.default?.value() ?? "",
|
|
45
|
+
toString: () => `params.${_name}`,
|
|
46
|
+
}),
|
|
47
|
+
defineInt: (_name: string, opts?: { default?: number }) => ({
|
|
48
|
+
value: () => opts?.default ?? 0,
|
|
49
|
+
toString: () => `params.${_name}`,
|
|
50
|
+
}),
|
|
51
|
+
defineBoolean: (_name: string, opts?: { default?: boolean }) => ({
|
|
52
|
+
value: () => opts?.default ?? false,
|
|
53
|
+
toString: () => `params.${_name}`,
|
|
54
|
+
}),
|
|
55
|
+
select: (options: unknown) => ({ select: { options } }),
|
|
56
|
+
expr: (
|
|
57
|
+
strings: TemplateStringsArray,
|
|
58
|
+
...values: ReadonlyArray<{ toString: () => string }>
|
|
59
|
+
) => ({
|
|
60
|
+
value: () =>
|
|
61
|
+
strings.reduce(
|
|
62
|
+
(acc, part, index) =>
|
|
63
|
+
`${acc}${values[index - 1]?.toString() ?? ""}${part}`
|
|
64
|
+
),
|
|
65
|
+
toString: () =>
|
|
66
|
+
strings.reduce(
|
|
67
|
+
(acc, part, index) =>
|
|
68
|
+
`${acc}${values[index - 1]?.toString() ?? ""}${part}`
|
|
69
|
+
),
|
|
70
|
+
}),
|
|
71
|
+
projectID: { value: () => "test-project" },
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
import {
|
|
75
|
+
buildPartitioningConfig,
|
|
76
|
+
CONFIG_EXPRESSIONS,
|
|
77
|
+
clustering,
|
|
78
|
+
configFromEnv,
|
|
79
|
+
} from "../src/config";
|
|
80
|
+
import { resolveExportConfig } from "../src/export-config";
|
|
81
|
+
|
|
82
|
+
describe("clustering", () => {
|
|
83
|
+
test("splits a comma list", () => {
|
|
84
|
+
expect(clustering("a,b,c")).toEqual(["a", "b", "c"]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test("caps at four columns", () => {
|
|
88
|
+
expect(clustering("a,b,c,d,e")).toEqual(["a", "b", "c", "d"]);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
test("empty or undefined becomes null", () => {
|
|
92
|
+
expect(clustering("")).toBeNull();
|
|
93
|
+
expect(clustering(undefined)).toBeNull();
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("buildPartitioningConfig", () => {
|
|
98
|
+
const base = {
|
|
99
|
+
timePartitioningField: undefined,
|
|
100
|
+
timePartitioningFieldType: undefined,
|
|
101
|
+
timePartitioningFirestoreField: undefined,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
test("no granularity and no fields => NONE", () => {
|
|
105
|
+
expect(
|
|
106
|
+
buildPartitioningConfig({ ...base, timePartitioning: null })
|
|
107
|
+
).toEqual({ granularity: "NONE" });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("granularity only", () => {
|
|
111
|
+
expect(
|
|
112
|
+
buildPartitioningConfig({ ...base, timePartitioning: "DAY" })
|
|
113
|
+
).toEqual({ granularity: "DAY" });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("timestamp field shorthand", () => {
|
|
117
|
+
expect(
|
|
118
|
+
buildPartitioningConfig({
|
|
119
|
+
...base,
|
|
120
|
+
timePartitioning: "DAY",
|
|
121
|
+
timePartitioningField: "timestamp",
|
|
122
|
+
})
|
|
123
|
+
).toMatchObject({ granularity: "DAY", bigqueryColumnName: "timestamp" });
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test("full custom field", () => {
|
|
127
|
+
expect(
|
|
128
|
+
buildPartitioningConfig({
|
|
129
|
+
timePartitioning: "DAY",
|
|
130
|
+
timePartitioningField: "created",
|
|
131
|
+
timePartitioningFieldType: "TIMESTAMP",
|
|
132
|
+
timePartitioningFirestoreField: "createdAt",
|
|
133
|
+
})
|
|
134
|
+
).toEqual({
|
|
135
|
+
granularity: "DAY",
|
|
136
|
+
bigqueryColumnName: "created",
|
|
137
|
+
bigqueryColumnType: "TIMESTAMP",
|
|
138
|
+
firestoreFieldName: "createdAt",
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("throws when fields are set without a granularity", () => {
|
|
143
|
+
expect(() =>
|
|
144
|
+
buildPartitioningConfig({
|
|
145
|
+
...base,
|
|
146
|
+
timePartitioning: null,
|
|
147
|
+
timePartitioningField: "created",
|
|
148
|
+
})
|
|
149
|
+
).toThrow();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
test("throws on incomplete custom field", () => {
|
|
153
|
+
expect(() =>
|
|
154
|
+
buildPartitioningConfig({
|
|
155
|
+
timePartitioning: "DAY",
|
|
156
|
+
timePartitioningField: "created",
|
|
157
|
+
timePartitioningFieldType: undefined,
|
|
158
|
+
timePartitioningFirestoreField: "createdAt",
|
|
159
|
+
})
|
|
160
|
+
).toThrow();
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
describe("configFromEnv", () => {
|
|
165
|
+
test("maps params without inventing a region default", () => {
|
|
166
|
+
const config = configFromEnv();
|
|
167
|
+
expect(config.projectId).toBe("test-project");
|
|
168
|
+
expect(config.bqProjectId).toBe("test-project");
|
|
169
|
+
expect(config.databaseId).toBe("(default)");
|
|
170
|
+
expect(resolveExportConfig(config).location).toBe("");
|
|
171
|
+
expect(config.viewType).toBe("view");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("exposes deploy-time expressions for trigger metadata", () => {
|
|
175
|
+
expect(CONFIG_EXPRESSIONS.collectionPath.toString()).toBe(
|
|
176
|
+
"params.COLLECTION_PATH"
|
|
177
|
+
);
|
|
178
|
+
expect(CONFIG_EXPRESSIONS.location.toString()).toBe(
|
|
179
|
+
"params.DATABASE_REGION"
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
});
|
|
@@ -0,0 +1,84 @@
|
|
|
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 { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
18
|
+
|
|
19
|
+
const { publish, channel } = vi.hoisted(() => {
|
|
20
|
+
const publish = vi.fn().mockResolvedValue(undefined);
|
|
21
|
+
const channel = vi.fn(() => ({ publish }));
|
|
22
|
+
return { publish, channel };
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
vi.mock("firebase-admin/eventarc", () => ({
|
|
26
|
+
getEventarc: () => ({ channel }),
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
import {
|
|
30
|
+
recordCompletionEvent,
|
|
31
|
+
recordErrorEvent,
|
|
32
|
+
recordStartEvent,
|
|
33
|
+
recordSuccessEvent,
|
|
34
|
+
setupEventChannel,
|
|
35
|
+
} from "../src/events";
|
|
36
|
+
|
|
37
|
+
const ORIGINAL = process.env.EVENTARC_CHANNEL;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => vi.clearAllMocks());
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
if (ORIGINAL === undefined) delete process.env.EVENTARC_CHANNEL;
|
|
42
|
+
else process.env.EVENTARC_CHANNEL = ORIGINAL;
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("no channel configured", () => {
|
|
46
|
+
test("publishers are a no-op when EVENTARC_CHANNEL is unset", async () => {
|
|
47
|
+
delete process.env.EVENTARC_CHANNEL;
|
|
48
|
+
setupEventChannel();
|
|
49
|
+
|
|
50
|
+
await recordStartEvent({ a: 1 });
|
|
51
|
+
await recordCompletionEvent({ a: 1 });
|
|
52
|
+
expect(publish).not.toHaveBeenCalled();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe("channel configured", () => {
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
process.env.EVENTARC_CHANNEL = "projects/p/locations/l/channels/c";
|
|
59
|
+
setupEventChannel();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test("publishes the firestore-bigquery-export event type only", async () => {
|
|
63
|
+
await recordStartEvent({ a: 1 });
|
|
64
|
+
expect(publish).toHaveBeenCalledTimes(1);
|
|
65
|
+
expect(publish).toHaveBeenCalledWith(
|
|
66
|
+
expect.objectContaining({
|
|
67
|
+
type: "firebase.extensions.firestore-bigquery-export.v1.onStart",
|
|
68
|
+
})
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("error / success / completion map to their event types", async () => {
|
|
73
|
+
await recordErrorEvent(new Error("boom"));
|
|
74
|
+
await recordSuccessEvent({ subject: "doc1", data: { a: 1 } });
|
|
75
|
+
await recordCompletionEvent({ a: 1 });
|
|
76
|
+
|
|
77
|
+
const types = publish.mock.calls.map((c) => c[0].type);
|
|
78
|
+
expect(types).toEqual([
|
|
79
|
+
"firebase.extensions.firestore-bigquery-export.v1.onError",
|
|
80
|
+
"firebase.extensions.firestore-bigquery-export.v1.onSuccess",
|
|
81
|
+
"firebase.extensions.firestore-bigquery-export.v1.onCompletion",
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
import { resolveExportConfig, toTrackerConfig } from "../src/export-config";
|
|
19
|
+
|
|
20
|
+
describe("resolveExportConfig", () => {
|
|
21
|
+
const minimal = {
|
|
22
|
+
collectionPath: "users",
|
|
23
|
+
datasetId: "analytics",
|
|
24
|
+
tableId: "users",
|
|
25
|
+
location: "us-central1",
|
|
26
|
+
projectId: "test-project",
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
test("applies defaults for omitted fields", () => {
|
|
30
|
+
const resolved = resolveExportConfig(minimal);
|
|
31
|
+
|
|
32
|
+
expect(resolved.projectId).toBe("test-project");
|
|
33
|
+
expect(resolved.datasetLocation).toBe("us");
|
|
34
|
+
expect(resolved.databaseId).toBe("(default)");
|
|
35
|
+
expect(resolved.viewType).toBe("view");
|
|
36
|
+
expect(resolved.wildcardIds).toBe(false);
|
|
37
|
+
expect(resolved.excludeOldData).toBe(false);
|
|
38
|
+
expect(resolved.clustering).toBeNull();
|
|
39
|
+
expect(resolved.logLevel).toBe("info");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("keeps caller-supplied values", () => {
|
|
43
|
+
const resolved = resolveExportConfig({
|
|
44
|
+
...minimal,
|
|
45
|
+
location: "europe-west2",
|
|
46
|
+
databaseId: "secondary",
|
|
47
|
+
wildcardIds: true,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
expect(resolved.location).toBe("europe-west2");
|
|
51
|
+
expect(resolved.databaseId).toBe("secondary");
|
|
52
|
+
expect(resolved.wildcardIds).toBe(true);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("carries required fields through", () => {
|
|
56
|
+
const resolved = resolveExportConfig(minimal);
|
|
57
|
+
expect(resolved.collectionPath).toBe("users");
|
|
58
|
+
expect(resolved.datasetId).toBe("analytics");
|
|
59
|
+
expect(resolved.tableId).toBe("users");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("toTrackerConfig", () => {
|
|
64
|
+
const base = {
|
|
65
|
+
collectionPath: "users",
|
|
66
|
+
datasetId: "analytics",
|
|
67
|
+
tableId: "users",
|
|
68
|
+
location: "us-central1",
|
|
69
|
+
projectId: "test-project",
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
test("derives materialized view flags from viewType", () => {
|
|
73
|
+
const plain = toTrackerConfig(resolveExportConfig(base));
|
|
74
|
+
expect(plain.useMaterializedView).toBe(false);
|
|
75
|
+
expect(plain.useIncrementalMaterializedView).toBe(false);
|
|
76
|
+
|
|
77
|
+
const incremental = toTrackerConfig(
|
|
78
|
+
resolveExportConfig({ ...base, viewType: "materialized_incremental" })
|
|
79
|
+
);
|
|
80
|
+
expect(incremental.useMaterializedView).toBe(true);
|
|
81
|
+
expect(incremental.useIncrementalMaterializedView).toBe(true);
|
|
82
|
+
|
|
83
|
+
const nonIncremental = toTrackerConfig(
|
|
84
|
+
resolveExportConfig({ ...base, viewType: "materialized_non_incremental" })
|
|
85
|
+
);
|
|
86
|
+
expect(nonIncremental.useMaterializedView).toBe(true);
|
|
87
|
+
expect(nonIncremental.useIncrementalMaterializedView).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("always skips init (initialization is handled lazily)", () => {
|
|
91
|
+
const tracker = toTrackerConfig(resolveExportConfig(base));
|
|
92
|
+
expect(tracker.skipInit).toBe(true);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("maps databaseId onto both firestore fields", () => {
|
|
96
|
+
const tracker = toTrackerConfig(
|
|
97
|
+
resolveExportConfig({ ...base, databaseId: "secondary" })
|
|
98
|
+
);
|
|
99
|
+
expect(tracker.databaseId).toBe("secondary");
|
|
100
|
+
expect(tracker.firestoreInstanceId).toBe("secondary");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("uses projectId as the default BigQuery project", () => {
|
|
104
|
+
const tracker = toTrackerConfig(resolveExportConfig(base));
|
|
105
|
+
expect(tracker.bqProjectId).toBe("test-project");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test("keeps an explicitly configured BigQuery project", () => {
|
|
109
|
+
const tracker = toTrackerConfig(
|
|
110
|
+
resolveExportConfig({ ...base, bqProjectId: "analytics-project" })
|
|
111
|
+
);
|
|
112
|
+
expect(tracker.bqProjectId).toBe("analytics-project");
|
|
113
|
+
});
|
|
114
|
+
});
|