Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url": "github.com/firebase/extensions.git",
"directory": "firestore-bigquery-export/firestore-bigquery-change-tracker"
},
"version": "1.1.2",
"version": "1.1.3",
"description": "Core change-tracker library for Cloud Firestore Collection BigQuery Exports",
"main": "./lib/index.js",
"scripts": {
Expand All @@ -28,7 +28,8 @@
"generate-schema": "^2.6.0",
"inquirer": "^6.4.0",
"lodash": "^4.17.14",
"sql-formatter": "^2.3.3"
"sql-formatter": "^2.3.3",
"traverse": "^0.6.6"
},
"devDependencies": {
"typescript": "^3.4.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/

import * as bigquery from "@google-cloud/bigquery";
import * as firebase from "firebase-admin";
import * as traverse from "traverse";
import { RawChangelogSchema, RawChangelogViewSchema } from "./schema";
import { latestConsistentSnapshotView } from "./snapshot";

Expand Down Expand Up @@ -52,6 +54,7 @@ export class FirestoreBigQueryEventHistoryTracker

async record(events: FirestoreDocumentChangeEvent[]) {
await this.initialize();

const rows = events.map((event) => {
return {
insertId: event.eventId,
Expand All @@ -60,20 +63,36 @@ export class FirestoreBigQueryEventHistoryTracker
event_id: event.eventId,
document_name: event.documentName,
operation: ChangeType[event.operation],
data: JSON.stringify(event.data),
data: JSON.stringify(this.serializeData(event.data)),
},
};
});
await this.insertData(rows);
}

serializeData(eventData: any) {
if (typeof eventData === "undefined") {
return undefined;
}

const data = traverse(eventData).map((property) => {
if (property instanceof firebase.firestore.DocumentReference) {
return property.path;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌 Great job on finding the issue quickly @russellwheatley 🙏

}

return property;
});

return data;
}

/**
* Inserts rows of data into the BigQuery raw change log table.
*/
private async insertData(rows: bigquery.RowMetadata[]) {
const options = {
skipInvalidRows: false,
ignoreUnkownValues: false,
ignoreUnknownValues: false,
raw: true,
};
try {
Expand Down
2 changes: 1 addition & 1 deletion firestore-bigquery-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"author": "Jan Wyszynski <[email protected]>",
"license": "Apache-2.0",
"dependencies": {
"@firebaseextensions/firestore-bigquery-change-tracker": "^1.1.2",
"@firebaseextensions/firestore-bigquery-change-tracker": "^1.1.3",
"@google-cloud/bigquery": "^2.1.0",
"@types/chai": "^4.1.6",
"chai": "^4.2.0",
Expand Down