UNPKG

@omegasolutions/vallora-log-sdk

Version:

A lightweight SDK for publishing transaction log events to Google Pub/Sub

30 lines (29 loc) 1.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LogClient = void 0; const pubsub_1 = require("@google-cloud/pubsub"); class LogClient { constructor() { this.topicName = process.env.TOPIC_LOGS_NAME || 'transaction-log-topic'; const projectId = process.env.GOOGLE_PROJECT_ID; if (!projectId) { throw new Error('Environment variable GOOGLE_PROJECT_ID is not defined. Please set it before using LogClient.'); } this.pubsub = new pubsub_1.PubSub({ projectId }); } /** * Publishes a log message to the transaction-log-topic */ async publishLog(payload) { const message = { ...payload, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), }; const buffer = Buffer.from(JSON.stringify(message)); const messageId = await this.pubsub.topic(this.topicName).publish(buffer); console.log(`Published message ${messageId} to topic "${this.topicName}" in project ${process.env.GOOGLE_PROJECT_ID}`); return messageId; } } exports.LogClient = LogClient;