@paralect/hive 0.0.20 → 0.0.22
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@paralect/hive",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.22",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"aws-sdk": "2.1080.0",
|
|
24
24
|
"axios": "^1.7.2",
|
|
25
25
|
"bcryptjs": "2.4.3",
|
|
26
|
+
"bullmq": "^5.10.3",
|
|
26
27
|
"commander": "^12.1.0",
|
|
27
28
|
"dotenv": "16.0.0",
|
|
28
29
|
"eslint-config-prettier": "8.5.0",
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const config = require('app-config');
|
|
2
|
+
|
|
3
|
+
const { Queue, Worker } = require('bullmq');
|
|
4
|
+
|
|
5
|
+
let connectionOpts = {
|
|
6
|
+
host: config.redis.url.split('@')[1].split(':')[0],
|
|
7
|
+
port: config.redis.url.split('@')[1].split(':')[1],
|
|
8
|
+
username: 'default',
|
|
9
|
+
password: config.redis.url.split(':')[2].split('@')[0],
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
Queue(queueName) {
|
|
14
|
+
return new Queue(queueName, {
|
|
15
|
+
connection: connectionOpts,
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
Worker(queueName, workerFn, options = {}) {
|
|
19
|
+
return new Worker(queueName, workerFn, {
|
|
20
|
+
...options,
|
|
21
|
+
connection: connectionOpts,
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const config = require('app-config');
|
|
2
|
+
const bullMq = require('./bullMq');
|
|
3
|
+
const queue = bullMq.Queue(`database-${config.env}`);
|
|
4
|
+
|
|
5
|
+
let handlers = {};
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
on(eventName, handler) {
|
|
9
|
+
console.log('registering', eventName);
|
|
10
|
+
|
|
11
|
+
handlers[eventName] = handlers[eventName] || [];
|
|
12
|
+
handlers[eventName].push(handler);
|
|
13
|
+
},
|
|
14
|
+
emit(eventName, data) {
|
|
15
|
+
queue.add(eventName, data);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
bullMq.Worker(`database-${config.env}`, async ({ name: eventName, data }) => {
|
|
20
|
+
await Promise.all((handlers[eventName] || []).map(h => h(data)));
|
|
21
|
+
});
|
package/starter/src/db.js
CHANGED
|
@@ -6,6 +6,9 @@ const getSchemas = require("helpers/getSchemas");
|
|
|
6
6
|
const getResources = require("helpers/getResources");
|
|
7
7
|
|
|
8
8
|
const config = require("app-config");
|
|
9
|
+
|
|
10
|
+
const bullMqBus = require("./bullMqBus");
|
|
11
|
+
|
|
9
12
|
const db = require("lib/node-mongo").connect(config.mongoUri);
|
|
10
13
|
|
|
11
14
|
db.services = {};
|
|
@@ -34,6 +37,7 @@ db.init = async () => {
|
|
|
34
37
|
|
|
35
38
|
db.services[schemaName] = db.createService(`${resourceName}`, {
|
|
36
39
|
validate: (obj) => schema.validate(obj, { allowUnknown: true }),
|
|
40
|
+
emitter: bullMqBus
|
|
37
41
|
});
|
|
38
42
|
}
|
|
39
43
|
);
|
|
@@ -18,7 +18,7 @@ const connect = (connectionString, settings) => {
|
|
|
18
18
|
});
|
|
19
19
|
|
|
20
20
|
db.on("open", () => {
|
|
21
|
-
logger.info(`Connected to mongodb
|
|
21
|
+
logger.info(`Connected to mongodb`);
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
db.on("close", (err) => {
|
|
@@ -33,7 +33,7 @@ const connect = (connectionString, settings) => {
|
|
|
33
33
|
if (err) {
|
|
34
34
|
logger.error(err);
|
|
35
35
|
} else {
|
|
36
|
-
logger.info(`Connected to mongodb
|
|
36
|
+
logger.info(`Connected to mongodb`);
|
|
37
37
|
}
|
|
38
38
|
});
|
|
39
39
|
|
|
@@ -18,7 +18,6 @@ const defaultOptions = {
|
|
|
18
18
|
class MongoService extends MongoQueryService {
|
|
19
19
|
constructor(collection, options = {}) {
|
|
20
20
|
super(collection, options);
|
|
21
|
-
|
|
22
21
|
_.defaults(this._options, defaultOptions);
|
|
23
22
|
|
|
24
23
|
this._bus = this._options.emitter || new EventEmitter();
|
|
@@ -102,25 +101,34 @@ class MongoService extends MongoQueryService {
|
|
|
102
101
|
}
|
|
103
102
|
|
|
104
103
|
emit(eventName, event) {
|
|
105
|
-
return this._bus.emit(eventName
|
|
104
|
+
return this._bus.emit(`${this._collection.name}:${eventName}`, event);
|
|
106
105
|
}
|
|
107
106
|
|
|
108
107
|
once(eventName, handler) {
|
|
109
|
-
return this._bus.once(eventName
|
|
108
|
+
return this._bus.once(`${this._collection.name}:${eventName}`, handler);
|
|
110
109
|
}
|
|
111
110
|
|
|
112
111
|
on(eventName, handler) {
|
|
113
|
-
|
|
112
|
+
console.log("this._bus.on(`${this._collection.name}:${eventName}`, handler);", `${this._collection.name}:${eventName}`);
|
|
113
|
+
return this._bus.on(`${this._collection.name}:${eventName}`, handler);
|
|
114
114
|
}
|
|
115
115
|
|
|
116
|
-
onPropertiesUpdated(
|
|
117
|
-
return this.on(
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
)
|
|
123
|
-
|
|
116
|
+
onPropertiesUpdated(fieldNames, handler) {
|
|
117
|
+
return this.on(`${this._collection.name}:${eventName}`, (event) => {
|
|
118
|
+
const { doc, prevDoc } = event;
|
|
119
|
+
|
|
120
|
+
let isFieldChanged = false;
|
|
121
|
+
|
|
122
|
+
_.forEach(fieldNames, (fieldName) => {
|
|
123
|
+
if (!_.isEqual(doc[fieldName], prevDoc[fieldName])) {
|
|
124
|
+
isFieldChanged = true;
|
|
125
|
+
return false; // break loop
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return true;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
if (isFieldChanged) handler(event);
|
|
124
132
|
});
|
|
125
133
|
}
|
|
126
134
|
|
|
@@ -147,7 +155,7 @@ class MongoService extends MongoQueryService {
|
|
|
147
155
|
await this._collection.insert(created, options);
|
|
148
156
|
|
|
149
157
|
created.forEach((doc) => {
|
|
150
|
-
this._bus.emit(
|
|
158
|
+
this._bus.emit(`${this._collection.name}:created`, {
|
|
151
159
|
doc,
|
|
152
160
|
});
|
|
153
161
|
});
|
|
@@ -186,7 +194,7 @@ class MongoService extends MongoQueryService {
|
|
|
186
194
|
options
|
|
187
195
|
);
|
|
188
196
|
|
|
189
|
-
this._bus.emit(
|
|
197
|
+
this._bus.emit(`${this._collection.name}:updated`, {
|
|
190
198
|
doc: updated,
|
|
191
199
|
prevDoc: doc,
|
|
192
200
|
});
|
|
@@ -247,7 +255,7 @@ class MongoService extends MongoQueryService {
|
|
|
247
255
|
await this._collection.remove(query, options);
|
|
248
256
|
|
|
249
257
|
removed.results.forEach((doc) => {
|
|
250
|
-
this._bus.emit(
|
|
258
|
+
this._bus.emit(`${this._collection.name}:removed`, {
|
|
251
259
|
doc,
|
|
252
260
|
});
|
|
253
261
|
});
|