message-brokers 1.1.0 → 1.1.2

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/README.md CHANGED
@@ -49,6 +49,58 @@ import MessageBrokers from "message-brokers";
49
49
  const messageBrokers = new MessageBrokers();
50
50
  ```
51
51
 
52
+ ### Initialization with MongoDB for deadqueue
53
+
54
+ #### 1. Mongoose initialization
55
+
56
+ ```javascript
57
+ const MessageBrokers = require("message-brokers");
58
+ const mongoose = require("mongoose");
59
+
60
+ const mongoConnectionHolder = { connection: null };
61
+
62
+ mongoose.connect("mongodb://localhost:27017/db");
63
+
64
+ const db = mongoose.connection;
65
+
66
+ db.on("connected", () => {
67
+ console.log("✅ Mongoose connected!");
68
+ mongoConnectionHolder.connection = db;
69
+ });
70
+
71
+ db.on("error", (err) => {
72
+ console.error("❌ Mongoose connection error:", err);
73
+ });
74
+
75
+ const messageBrokers = new MessageBrokers(mongoConnectionHolder);
76
+ ```
77
+
78
+ #### 2. Native Mongodb driver initialization
79
+
80
+ ```javascript
81
+ const MessageBrokers = require("message-brokers");
82
+ const { MongoClient } = require("mongodb");
83
+
84
+ const mongoUrl = "mongodb://localhost:27017";
85
+ const dbName = "db";
86
+
87
+ const client = new MongoClient(mongoUrl, {
88
+ useNewUrlParser: true,
89
+ useUnifiedTopology: true,
90
+ });
91
+
92
+ const mongoConnectionHolder = { connection: null };
93
+
94
+ client.connect()
95
+ .then(() => {
96
+ console.log("✅ MongoDB connected!");
97
+ mongoConnectionHolder.connection = client.db(dbName);
98
+ })
99
+ .catch(error => console.error("❌ MongoDB connection failed:", error));
100
+
101
+ const messageBrokers = new MessageBrokers(mongoConnectionHolder);
102
+ ```
103
+
52
104
  ## Usage
53
105
 
54
106
  ```javascript
@@ -10,7 +10,7 @@ const mqConfigs = [{
10
10
  priorityQueue: false
11
11
  }, {
12
12
  queueName: "datacloak.queue",
13
- routingKeyList: ["har", "har_meta", "lab_api_response", "purchase_details", "lab_location_data", "input_object"],
13
+ routingKeyList: ["har", "har_meta", "lab_api_response", "purchase_details", "lab_location_data", "input_object", "trending_data"],
14
14
  priorityQueue: false
15
15
  }
16
16
  // Example for priority queue.
@@ -4,14 +4,14 @@ const AmqpController = require("./controllers/amqp.controller");
4
4
  const mbConfigs = require("./configs/mb.configs");
5
5
  const hv_exchange = "hv.exchange";
6
6
  class MessageBrokers {
7
- constructor(mongoDbConnection) {
7
+ constructor(mongoConnectionHolder) {
8
8
  try {
9
9
  if (!mbConfigs.rabbitmqUrl) {
10
10
  throw new Error("RABBITMQ_URL is required for message brokers initialization.");
11
11
  }
12
- package_loader(mongoDbConnection, true);
12
+ package_loader(mongoConnectionHolder, true);
13
13
  } catch (error) {
14
- package_loader(mongoDbConnection, false); // disable rabbitmq
14
+ package_loader(mongoConnectionHolder, false); // disable rabbitmq
15
15
  }
16
16
  }
17
17
 
@@ -94,6 +94,16 @@ class MessageBrokers {
94
94
  const response = await AmqpController.publishMessage(hv_exchange, "har_stub", har_stub);
95
95
  return response;
96
96
  }
97
+
98
+ /**
99
+ * This function is used publish trending_data into the exchange.
100
+ * @param {Object} trending_data
101
+ * @returns
102
+ */
103
+ async publishTrendingData(trending_data) {
104
+ const response = await AmqpController.publishMessage(hv_exchange, "trending_data", trending_data);
105
+ return response;
106
+ }
97
107
  async publishPendingMessageInCache() {
98
108
  return await AmqpController.publishPendingMessageInCache();
99
109
  }
@@ -10,15 +10,15 @@ const MessageBroker = require("./helpers/message-brokers.helper");
10
10
  * configs and other requirements which are needed before the server
11
11
  * is started/listening.
12
12
  */
13
- const package_loader = async (mongoDbConnection, rabbitMqEnable = true) => {
13
+ const package_loader = async (mongoConnectionHolder, rabbitMqEnable = true) => {
14
14
  try {
15
15
  process.env.CACHING_ENABLED === "true" && (await redisDB()); // enable caching only if the env is set
16
16
  if (rabbitMqEnable) {
17
17
  await MessageBroker.connect();
18
18
  await MessageBroker.initializePubSub();
19
19
  }
20
- if (mongoDbConnection) {
21
- await mongoDB(mongoDbConnection);
20
+ if (mongoConnectionHolder.connection) {
21
+ await mongoDB(mongoConnectionHolder.connection);
22
22
  }
23
23
  return;
24
24
  } catch (error) {
@@ -5,6 +5,10 @@ declare module "message-brokers" {
5
5
  data: any;
6
6
  }
7
7
 
8
+ interface MongoConnectionHolder {
9
+ connection: Function;
10
+ }
11
+
8
12
  interface MessageBrokersConfig {
9
13
  /**
10
14
  * The RabbitMQ connection URL.
@@ -19,7 +23,7 @@ declare module "message-brokers" {
19
23
  /**
20
24
  * MongoDB credentials for database connection.
21
25
  */
22
- mongoDbConnection?: Function;
26
+ mongoConnectionHolder?: MongoConnectionHolder;
23
27
  }
24
28
 
25
29
  class MessageBrokers {
@@ -27,10 +31,13 @@ declare module "message-brokers" {
27
31
  * Initializes the MessageBrokers instance.
28
32
  * Throws an error if required configurations (like RabbitMQ URL) are missing.
29
33
  *
30
- * @param mongoDbConnection - MongoDB credentials for connecting to the database.
34
+ * @param mongoConnectionHolder - MongoDB credentials for connecting to the database.
31
35
  * @param config - Optional configurations for the message broker (e.g., RabbitMQ URL, exchange name).
32
36
  */
33
- constructor(mongoDbConnection: Function, config?: MessageBrokersConfig);
37
+ constructor(
38
+ mongoConnectionHolder: MongoConnectionHolder,
39
+ config?: MessageBrokersConfig
40
+ );
34
41
 
35
42
  /**
36
43
  * Publishes a HAR object into the exchange.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "message-brokers",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "This package contains functions to publish the messages into the message brokers like rabbitmq.",
5
5
  "main": "lib/message-brokers.js",
6
6
  "types": "lib/types/message-brokers.d.ts",