message-brokers 1.1.0 → 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 +52 -0
- package/lib/message-brokers.js +3 -3
- package/lib/package-loader.js +3 -3
- package/lib/types/types/message-brokers.d.ts +10 -3
- package/package.json +1 -1
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
|
package/lib/message-brokers.js
CHANGED
|
@@ -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(
|
|
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(
|
|
12
|
+
package_loader(mongoConnectionHolder, true);
|
|
13
13
|
} catch (error) {
|
|
14
|
-
package_loader(
|
|
14
|
+
package_loader(mongoConnectionHolder, false); // disable rabbitmq
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
package/lib/package-loader.js
CHANGED
|
@@ -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 (
|
|
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 (
|
|
21
|
-
await mongoDB(
|
|
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
|
-
|
|
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
|
|
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(
|
|
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.
|
|
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",
|