@klapeks/micro-db-utils 0.0.6 → 0.0.8
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/dist/connections/kafka/kafka.consumer.d.ts +2 -2
- package/dist/connections/kafka/kafka.consumer.js +19 -5
- package/dist/connections/nosql/mongo.d.ts +1 -1
- package/dist/connections/nosql/mongo.js +6 -2
- package/dist/connections/nosql/redis.d.ts +6 -0
- package/dist/connections/nosql/redis.js +32 -0
- package/dist/selectors/typeorm.select.handler.js +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Consumer, ConsumerConfig, EachMessagePayload } from "kafkajs";
|
|
1
|
+
import type { Consumer, ConsumerConfig, ConsumerRunConfig, EachMessagePayload } from "kafkajs";
|
|
2
2
|
import { KafkaConnection } from "./kafka";
|
|
3
3
|
export type KafkaConnectionHandler = (message: any, payload: EachMessagePayload) => any;
|
|
4
4
|
export declare class KafkaConsumer {
|
|
@@ -8,6 +8,6 @@ export declare class KafkaConsumer {
|
|
|
8
8
|
constructor(connection: KafkaConnection, config: ConsumerConfig);
|
|
9
9
|
private _needConnection;
|
|
10
10
|
connect(): Promise<void>;
|
|
11
|
-
subscribe(topic: string, handler: KafkaConnectionHandler): Promise<void>;
|
|
11
|
+
subscribe(topic: string, handler: KafkaConnectionHandler, consumerOptions?: Omit<ConsumerRunConfig, "eachMessage">): Promise<void>;
|
|
12
12
|
disconnect(): Promise<void>;
|
|
13
13
|
}
|
|
@@ -1,4 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
2
13
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
14
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
15
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -62,7 +73,7 @@ var KafkaConsumer = /** @class */ (function () {
|
|
|
62
73
|
});
|
|
63
74
|
});
|
|
64
75
|
};
|
|
65
|
-
KafkaConsumer.prototype.subscribe = function (topic, handler) {
|
|
76
|
+
KafkaConsumer.prototype.subscribe = function (topic, handler, consumerOptions) {
|
|
66
77
|
return __awaiter(this, void 0, void 0, function () {
|
|
67
78
|
var _this = this;
|
|
68
79
|
return __generator(this, function (_a) {
|
|
@@ -79,9 +90,13 @@ var KafkaConsumer = /** @class */ (function () {
|
|
|
79
90
|
})];
|
|
80
91
|
case 3:
|
|
81
92
|
_a.sent();
|
|
93
|
+
if (!consumerOptions)
|
|
94
|
+
consumerOptions = {};
|
|
95
|
+
if (!consumerOptions.partitionsConsumedConcurrently) {
|
|
96
|
+
consumerOptions.partitionsConsumedConcurrently = 10;
|
|
97
|
+
}
|
|
82
98
|
logger.log("Pre run:", topic);
|
|
83
|
-
return [4 /*yield*/, this.consumer.run({
|
|
84
|
-
eachMessage: function (payload) { return __awaiter(_this, void 0, void 0, function () {
|
|
99
|
+
return [4 /*yield*/, this.consumer.run(__assign(__assign({}, consumerOptions), { eachMessage: function (payload) { return __awaiter(_this, void 0, void 0, function () {
|
|
85
100
|
var msg;
|
|
86
101
|
var _a;
|
|
87
102
|
return __generator(this, function (_b) {
|
|
@@ -100,8 +115,7 @@ var KafkaConsumer = /** @class */ (function () {
|
|
|
100
115
|
return [2 /*return*/];
|
|
101
116
|
}
|
|
102
117
|
});
|
|
103
|
-
}); }
|
|
104
|
-
})];
|
|
118
|
+
}); } }))];
|
|
105
119
|
case 4:
|
|
106
120
|
_a.sent();
|
|
107
121
|
logger.log("Listening:", topic);
|
|
@@ -43,7 +43,7 @@ export declare class MongoDBConnection {
|
|
|
43
43
|
readonly set: (model: T, value: number) => Promise<any>;
|
|
44
44
|
};
|
|
45
45
|
static getOptions(): MongoDBConnectOptions;
|
|
46
|
-
static init(databaseName: string): Promise<void>;
|
|
46
|
+
static init(databaseName: string, replicaSet?: string): Promise<void>;
|
|
47
47
|
}
|
|
48
48
|
export declare const MongoGeoPoint: MongooseSchema<Point, import("mongoose").Model<Point, any, any, any, import("mongoose").Document<unknown, any, Point, any> & Point & {
|
|
49
49
|
_id: import("mongoose").Types.ObjectId;
|
|
@@ -139,7 +139,7 @@ var MongoDBConnection = /** @class */ (function () {
|
|
|
139
139
|
database: ""
|
|
140
140
|
};
|
|
141
141
|
};
|
|
142
|
-
MongoDBConnection.init = function (databaseName) {
|
|
142
|
+
MongoDBConnection.init = function (databaseName, replicaSet) {
|
|
143
143
|
return __awaiter(this, void 0, void 0, function () {
|
|
144
144
|
var _a, host, port, password, username, mongoURI;
|
|
145
145
|
return __generator(this, function (_b) {
|
|
@@ -147,8 +147,12 @@ var MongoDBConnection = /** @class */ (function () {
|
|
|
147
147
|
case 0:
|
|
148
148
|
if (_databaseName)
|
|
149
149
|
return [2 /*return*/];
|
|
150
|
+
if (!replicaSet && process.env.MONGO_DATABASE_REPLICA_SET) {
|
|
151
|
+
replicaSet = process.env.MONGO_DATABASE_REPLICA_SET;
|
|
152
|
+
}
|
|
150
153
|
_a = this.getOptions(), host = _a.host, port = _a.port, password = _a.password, username = _a.username;
|
|
151
|
-
mongoURI = 'mongodb://' + host + ':' + port + '/' + databaseName
|
|
154
|
+
mongoURI = 'mongodb://' + host + ':' + port + '/' + databaseName
|
|
155
|
+
+ (replicaSet ? '?replicaSet=' + replicaSet : undefined);
|
|
152
156
|
// logger.log(mongoURI);
|
|
153
157
|
if (!mongooseModule)
|
|
154
158
|
throw new Error("No mongoose module");
|
|
@@ -18,4 +18,10 @@ export declare class RedisConnection {
|
|
|
18
18
|
/** in seconds */
|
|
19
19
|
expiredInSeconds?: number;
|
|
20
20
|
}): Promise<void>;
|
|
21
|
+
setLock(key: string, options?: {
|
|
22
|
+
expiredInSeconds?: number;
|
|
23
|
+
}): Promise<{
|
|
24
|
+
isAlreadyLocked: boolean;
|
|
25
|
+
}>;
|
|
26
|
+
unlock(key: string): Promise<boolean>;
|
|
21
27
|
}
|
|
@@ -129,6 +129,38 @@ var RedisConnection = /** @class */ (function () {
|
|
|
129
129
|
});
|
|
130
130
|
});
|
|
131
131
|
};
|
|
132
|
+
RedisConnection.prototype.setLock = function (key, options) {
|
|
133
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
134
|
+
var result, err_2;
|
|
135
|
+
return __generator(this, function (_a) {
|
|
136
|
+
switch (_a.label) {
|
|
137
|
+
case 0:
|
|
138
|
+
_a.trys.push([0, 2, , 3]);
|
|
139
|
+
return [4 /*yield*/, this.redisClient.set(this.keyPrefix + key, "1", {
|
|
140
|
+
NX: true,
|
|
141
|
+
EX: (options === null || options === void 0 ? void 0 : options.expiredInSeconds) || 60
|
|
142
|
+
})];
|
|
143
|
+
case 1:
|
|
144
|
+
result = _a.sent();
|
|
145
|
+
return [2 /*return*/, {
|
|
146
|
+
isAlreadyLocked: !result
|
|
147
|
+
}];
|
|
148
|
+
case 2:
|
|
149
|
+
err_2 = _a.sent();
|
|
150
|
+
logger.error("Error locking redis:", (0, utils_1.shortErrorParser)(err_2));
|
|
151
|
+
throw err_2;
|
|
152
|
+
case 3: return [2 /*return*/];
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
RedisConnection.prototype.unlock = function (key) {
|
|
158
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
+
return __generator(this, function (_a) {
|
|
160
|
+
return [2 /*return*/, this.remove(key)];
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
};
|
|
132
164
|
return RedisConnection;
|
|
133
165
|
}());
|
|
134
166
|
exports.RedisConnection = RedisConnection;
|
|
@@ -136,7 +136,7 @@ var SelectEntityHandler = /** @class */ (function (_super) {
|
|
|
136
136
|
for (var _i = 0, _c = Object.keys(where); _i < _c.length; _i++) {
|
|
137
137
|
var key = _c[_i];
|
|
138
138
|
var placeholderKey = key + "_" + i;
|
|
139
|
-
if (typeof where[key]
|
|
139
|
+
if (typeof where[key] === 'undefined')
|
|
140
140
|
continue;
|
|
141
141
|
if (where[key] == null) {
|
|
142
142
|
qb2 = qb2.andWhere('key is NULL');
|