message-brokers 1.0.12 → 1.1.1

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
@@ -1,17 +1,13 @@
1
- const {
2
- colorLogger
3
- } = require("../utils/log.util");
4
- const {
5
- MongoClient
6
- } = require("mongodb");
1
+ // const { colorLogger } = require("../utils/log.util");
2
+ // const { MongoClient } = require("mongodb");
3
+
7
4
  let db = null;
8
- async function mongoDB(MONGO_URL, MONGO_DB) {
5
+ async function mongoDB(mongoDbConnection) {
9
6
  if (db) return db;
10
7
  try {
11
- const client = new MongoClient(MONGO_URL);
12
- await client.connect();
13
- db = client.db(MONGO_DB);
14
- await colorLogger("2F3C7E", "00539C", "EEA47F", `Connected to mongoDB successfully!`);
8
+ db = mongoDbConnection;
9
+
10
+ // await colorLogger("2F3C7E", "00539C", "EEA47F", `Connected to mongoDB successfully!`);
15
11
  return db;
16
12
  } catch (error) {
17
13
  console.error("Error connecting to mongoDB:", error);
@@ -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(mongoDbCredentials) {
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(mongoDbCredentials, true);
12
+ package_loader(mongoConnectionHolder, true);
13
13
  } catch (error) {
14
- package_loader(mongoDbCredentials, false); // disable rabbitmq
14
+ package_loader(mongoConnectionHolder, false); // disable rabbitmq
15
15
  }
16
16
  }
17
17
 
@@ -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 (mongoDbCredentials, 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 (mongoDbCredentials.mongoDbUrl) {
21
- await mongoDB(mongoDbCredentials.mongoDbUrl, mongoDbCredentials?.mongoDbName);
20
+ if (mongoConnectionHolder.connection) {
21
+ await mongoDB(mongoConnectionHolder.connection);
22
22
  }
23
23
  return;
24
24
  } catch (error) {
@@ -1,16 +1,48 @@
1
1
  declare module "message-brokers" {
2
+ interface MongoDbCredentials {
3
+ /**
4
+ * The connection string for MongoDB, including protocol, host, and port.
5
+ */
6
+ mongoDbUrl: string;
7
+
8
+ /**
9
+ * The name of the MongoDB database to connect to.
10
+ */
11
+ mongoDbName?: string;
12
+ }
13
+
2
14
  interface PublishResponse {
3
15
  success: boolean;
4
16
  message: string;
5
17
  data: any;
6
18
  }
7
19
 
20
+ interface MessageBrokersConfig {
21
+ /**
22
+ * The RabbitMQ connection URL.
23
+ */
24
+ rabbitmqUrl?: string;
25
+
26
+ /**
27
+ * The name of the RabbitMQ exchange.
28
+ */
29
+ exchangeName?: string;
30
+
31
+ /**
32
+ * MongoDB credentials for database connection.
33
+ */
34
+ mongoDbCredentials?: MongoDbCredentials;
35
+ }
36
+
8
37
  class MessageBrokers {
9
38
  /**
10
39
  * Initializes the MessageBrokers instance.
11
- * Throws an error if required configurations are missing.
40
+ * Throws an error if required configurations (like RabbitMQ URL) are missing.
41
+ *
42
+ * @param mongoDbCredentials - MongoDB credentials for connecting to the database.
43
+ * @param config - Optional configurations for the message broker (e.g., RabbitMQ URL, exchange name).
12
44
  */
13
- constructor();
45
+ constructor(mongoDbCredentials: MongoDbCredentials, config?: MessageBrokersConfig);
14
46
 
15
47
  /**
16
48
  * Publishes a HAR object into the exchange.
@@ -1,22 +1,14 @@
1
1
  declare module "message-brokers" {
2
- interface MongoDbCredentials {
3
- /**
4
- * The connection string for MongoDB, including protocol, host, and port.
5
- */
6
- mongoDbUrl: string;
7
-
8
- /**
9
- * The name of the MongoDB database to connect to.
10
- */
11
- mongoDbName?: string;
12
- }
13
-
14
2
  interface PublishResponse {
15
3
  success: boolean;
16
4
  message: string;
17
5
  data: any;
18
6
  }
19
7
 
8
+ interface MongoConnectionHolder {
9
+ connection: Function;
10
+ }
11
+
20
12
  interface MessageBrokersConfig {
21
13
  /**
22
14
  * The RabbitMQ connection URL.
@@ -31,7 +23,7 @@ declare module "message-brokers" {
31
23
  /**
32
24
  * MongoDB credentials for database connection.
33
25
  */
34
- mongoDbCredentials?: MongoDbCredentials;
26
+ mongoConnectionHolder?: MongoConnectionHolder;
35
27
  }
36
28
 
37
29
  class MessageBrokers {
@@ -39,10 +31,13 @@ declare module "message-brokers" {
39
31
  * Initializes the MessageBrokers instance.
40
32
  * Throws an error if required configurations (like RabbitMQ URL) are missing.
41
33
  *
42
- * @param mongoDbCredentials - MongoDB credentials for connecting to the database.
34
+ * @param mongoConnectionHolder - MongoDB credentials for connecting to the database.
43
35
  * @param config - Optional configurations for the message broker (e.g., RabbitMQ URL, exchange name).
44
36
  */
45
- constructor(mongoDbCredentials: MongoDbCredentials, config?: MessageBrokersConfig);
37
+ constructor(
38
+ mongoConnectionHolder: MongoConnectionHolder,
39
+ config?: MessageBrokersConfig
40
+ );
46
41
 
47
42
  /**
48
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.0.12",
3
+ "version": "1.1.1",
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",
@@ -21,7 +21,6 @@
21
21
  "amqplib": "^0.10.5",
22
22
  "chalk": "^5.4.1",
23
23
  "dotenv": "^16.4.7",
24
- "mongodb": "^5.9.2",
25
24
  "redis": "^4.7.0"
26
25
  },
27
26
  "devDependencies": {