-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (43 loc) · 1.88 KB
/
index.js
File metadata and controls
52 lines (43 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import functions from "@google-cloud/functions-framework";
import protobuf from "protobufjs";
import { streamDelete, streamUpdatesToBuffer, streamBoxesUpdatesToBuffer, syncBatchedUpdatesToTables } from "./utils.js";
import { tableNameArray } from "./settings.js";
const collectionNameSet = new Set(tableNameArray);
let DocumentEventData = null;
let isProtosLoaded = false;
const loadProtos = async () => {
const root = await protobuf.load("data.proto");
DocumentEventData = root.lookupType("google.events.cloud.firestore.v1.DocumentEventData");
isProtosLoaded = true;
};
functions.cloudEvent("streamFirestoreUpdates", async (cloudEvent) => {
if (!isProtosLoaded) {
await loadProtos();
}
const decodedData = DocumentEventData.toObject(DocumentEventData.decode(cloudEvent.data), { longs: Number });
const name = decodedData.value?.name || decodedData.oldValue.name;
const [tableName, docId] = name.split("/").slice(-2);
if (!collectionNameSet.has(tableName)) {
console.log(`Doc changes in "${tableName}" collection are not streamed to BigQuery.`);
return;
}
if (!decodedData.value) {
console.log(`Deleting row (docId: ${docId}) from ${tableName} table in BigQuery...`);
await streamDelete(tableName, docId);
return;
}
const timestamp = new Date().toISOString();
console.time(`Time for streaming ${tableName} doc ${docId}(${timestamp})`);
if (tableName === "boxes") {
await streamBoxesUpdatesToBuffer(tableName, docId, decodedData);
} else {
await streamUpdatesToBuffer(tableName, docId, decodedData);
}
console.timeEnd(`Time for streaming ${tableName} doc ${docId}(${timestamp})`);
});
functions.http("syncBatchedUpdatesToTables", async (req, res) => {
console.time(`Time for syncing updates to all tables`);
await syncBatchedUpdatesToTables();
console.timeEnd(`Time for syncing updates to all tables`);
return res.status(200).end();
});