@bolt.tech/kafka-utils 1.0.7 → 1.0.9

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/build/index.js CHANGED
@@ -16,6 +16,10 @@ let currentNotificationType;
16
16
  function initialiseNotification(paramStorePath, nodeEnvLocal, serviceName, notificationType) {
17
17
  return __awaiter(this, void 0, void 0, function* () {
18
18
  try {
19
+ registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
20
+ console.log('🔵 Connecting to Schema Registry...');
21
+ registry_util_1.SchemaRegistryS.getInstance();
22
+ console.log('✅ Schema Registry connected.');
19
23
  currentNotificationType = notificationType;
20
24
  const KafkaServer = require('./kafkaServer');
21
25
  const kafkaInstance = new KafkaServer();
@@ -36,13 +40,16 @@ const publishToKafka = require('./utils/kafka/publisher.kafka');
36
40
  function sendNotification(userId, message) {
37
41
  return __awaiter(this, void 0, void 0, function* () {
38
42
  try {
43
+ if (!registry_util_1.SchemaRegistryS.isConnected()) {
44
+ console.log('🔵 Connecting to Schema Registry...');
45
+ registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
46
+ registry_util_1.SchemaRegistryS.getInstance();
47
+ console.log('✅ Schema Registry connected successfully.');
48
+ }
39
49
  const instance = notificationSchema_registry_1.NotificationSchema.getInstance();
40
50
  if (!instance) {
41
51
  throw new Error("Notification schema is not initialised or Schema Registry is not connected.");
42
52
  }
43
- if (!registry_util_1.SchemaRegistryS.isConnected()) {
44
- yield registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
45
- }
46
53
  if (!notificationSchema_registry_1.NotificationSchema) {
47
54
  throw new Error("Notification schema is not initialised");
48
55
  }
@@ -50,6 +57,7 @@ function sendNotification(userId, message) {
50
57
  let key = userId;
51
58
  const schemaId = yield instance.getSchemaId();
52
59
  yield publishToKafka(topic, schemaId, { key, value: message });
60
+ console.log(`✅ Notification sent to user: ${userId}`);
53
61
  }
54
62
  catch (e) {
55
63
  console.log("🔴 kafka-utils: sendNotification", e);
@@ -64,11 +64,11 @@ class KafkaServer {
64
64
  const kafka = config_kafka_1.KafkaConfig.getConfig(kafkaConnectionDetails, serviceName);
65
65
  // Connect to Kafka
66
66
  yield producer_kafka_1.KafkaProducer.connect(kafka);
67
- // Connect to Schema Registry
68
- registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
67
+ // Connect to Schema Registry and wait for completion
68
+ console.log('🔵 Connecting to Schema Registry...');
69
+ yield registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
69
70
  this.schemaRegistry = registry_util_1.SchemaRegistryS.getInstance();
70
- console.log(`Schema url:- ${process.env.SCHEMA_REGISTRY_URL}`);
71
- console.log(this.schemaRegistry = registry_util_1.SchemaRegistryS.getInstance());
71
+ console.log('✅ Schema Registry connected.');
72
72
  // Register Notification schema
73
73
  this.notificationSchema = notificationSchema_registry_1.NotificationSchema.getInstance();
74
74
  yield this.notificationSchema.register(notificationType);
@@ -14,6 +14,8 @@ const producer_kafka_1 = require("./producer.kafka");
14
14
  function publishToKafka(topic, schemaId, message) {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
16
  try {
17
+ registry_util_1.SchemaRegistryS.connect(process.env.SCHEMA_REGISTRY_URL);
18
+ registry_util_1.SchemaRegistryS.getInstance();
17
19
  const encodedMessage = yield registry_util_1.SchemaRegistryS.getInstance().encode(schemaId, message.value);
18
20
  const payload = { topic, messages: [{ key: message.key, value: encodedMessage }] };
19
21
  const producer = yield producer_kafka_1.KafkaProducer.getProducer();
@@ -3,10 +3,11 @@ export declare class NotificationSchema {
3
3
  private static instance;
4
4
  private registry;
5
5
  private registeredSchemaId;
6
+ private notificationType;
6
7
  private constructor();
7
8
  static getInstance(): NotificationSchema;
8
9
  register(notificationType: notificationType): Promise<void>;
9
- getSchemaId(): number;
10
+ getSchemaId(): Promise<number>;
10
11
  /**
11
12
  * Reads schema from protobuf file and stringifies it. Will throw an error in case proto file has multiple message
12
13
  * @returns Stringified schema
@@ -46,7 +46,11 @@ class NotificationSchema {
46
46
  });
47
47
  }
48
48
  getSchemaId() {
49
- return this.registeredSchemaId;
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ if (!this.registeredSchemaId)
51
+ yield this.register(this.notificationType);
52
+ return this.registeredSchemaId;
53
+ });
50
54
  }
51
55
  /**
52
56
  * Reads schema from protobuf file and stringifies it. Will throw an error in case proto file has multiple message
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bolt.tech/kafka-utils",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",