UNPKG

@tinybirdco/mockingbird

Version:
40 lines (39 loc) 1.33 kB
import { Spanner } from "@google-cloud/spanner"; import { GoogleAuth } from "google-auth-library"; import { z } from "zod"; import { BaseGenerator, baseConfigSchema } from "./BaseGenerator"; const GoogleSpannerConfigSchema = baseConfigSchema.merge(z.object({ projectId: z.string(), instanceId: z.string(), databaseId: z.string(), table: z.string(), keyFilename: z.string(), })); export class GoogleSpannerGenerator extends BaseGenerator { client; instance; database; table; constructor(config) { super(GoogleSpannerConfigSchema.parse(config)); const auth = new GoogleAuth({ keyFilename: this.config.keyFilename, }); this.client = new Spanner({ projectId: this.config.projectId, auth, }); this.instance = this.client.instance(this.config.instanceId); this.database = this.instance.database(this.config.databaseId); this.table = this.database.table(this.config.table); } async sendData(rows) { try { const response = await this.table.insert(rows); this.log("info", `Google Spanner Response: ${JSON.stringify(response)}`); } catch (err) { this.log("error", `Google Spanner Error: ${JSON.stringify(err)}`); } } }