@klapeks/micro-db-utils 0.0.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/dist/backups/mysql.backup.d.ts +7 -0
- package/dist/backups/mysql.backup.js +156 -0
- package/dist/connections/cloud/s3.d.ts +23 -0
- package/dist/connections/cloud/s3.js +169 -0
- package/dist/connections/index.d.ts +5 -0
- package/dist/connections/index.js +25 -0
- package/dist/connections/kafka/index.d.ts +3 -0
- package/dist/connections/kafka/index.js +21 -0
- package/dist/connections/kafka/kafka.consumer.d.ts +13 -0
- package/dist/connections/kafka/kafka.consumer.js +128 -0
- package/dist/connections/kafka/kafka.d.ts +16 -0
- package/dist/connections/kafka/kafka.js +205 -0
- package/dist/connections/kafka/kafka.producer.d.ts +11 -0
- package/dist/connections/kafka/kafka.producer.js +101 -0
- package/dist/connections/mysql.connection.d.ts +15 -0
- package/dist/connections/mysql.connection.js +165 -0
- package/dist/connections/nosql/mongo.d.ts +56 -0
- package/dist/connections/nosql/mongo.js +192 -0
- package/dist/connections/nosql/redis.d.ts +310 -0
- package/dist/connections/nosql/redis.js +129 -0
- package/dist/connections/quiet.require.d.ts +1 -0
- package/dist/connections/quiet.require.js +12 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +20 -0
- package/dist/migrations/index.d.ts +1 -0
- package/dist/migrations/index.js +17 -0
- package/dist/migrations/mysql.migrations.d.ts +6 -0
- package/dist/migrations/mysql.migrations.js +220 -0
- package/dist/selectors/abstract.select.handler.d.ts +26 -0
- package/dist/selectors/abstract.select.handler.js +202 -0
- package/dist/selectors/index.d.ts +3 -0
- package/dist/selectors/index.js +19 -0
- package/dist/selectors/raw-mysql.select.handler.d.ts +17 -0
- package/dist/selectors/raw-mysql.select.handler.js +149 -0
- package/dist/selectors/typeorm.select.handler.d.ts +21 -0
- package/dist/selectors/typeorm.select.handler.js +187 -0
- package/dist/utils/columns.types.d.ts +100 -0
- package/dist/utils/columns.types.js +29 -0
- package/dist/utils/index.d.ts +3 -0
- package/dist/utils/index.js +21 -0
- package/dist/utils/iso.date.time.d.ts +2 -0
- package/dist/utils/iso.date.time.js +27 -0
- package/dist/utils/sqlite3.utils.d.ts +4 -0
- package/dist/utils/sqlite3.utils.js +60 -0
- package/dist/utils/where.utils.d.ts +3 -0
- package/dist/utils/where.utils.js +22 -0
- package/index.d.ts +1 -0
- package/index.js +2 -0
- package/package.json +37 -0
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.KafkaConnection = exports.instances = void 0;
|
|
40
|
+
var utils_1 = require("@klapeks/utils");
|
|
41
|
+
var kafka_producer_1 = require("./kafka.producer");
|
|
42
|
+
var kafka_consumer_1 = require("./kafka.consumer");
|
|
43
|
+
var quiet_require_1 = require("../quiet.require");
|
|
44
|
+
var kafkajsModule = (0, quiet_require_1.quietRequire)('kafkajs');
|
|
45
|
+
var logger = new utils_1.Logger("Kafka");
|
|
46
|
+
exports.instances = {
|
|
47
|
+
producers: [],
|
|
48
|
+
consumers: [],
|
|
49
|
+
};
|
|
50
|
+
var KafkaConnection = /** @class */ (function () {
|
|
51
|
+
function KafkaConnection(clientId) {
|
|
52
|
+
this.clientId = clientId;
|
|
53
|
+
if (!kafkajsModule)
|
|
54
|
+
throw "No kafkajs module installed";
|
|
55
|
+
this.kafka = new kafkajsModule.Kafka({
|
|
56
|
+
clientId: clientId,
|
|
57
|
+
brokers: ['localhost:9092'],
|
|
58
|
+
logCreator: function () { return function () { }; }
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
KafkaConnection.prototype.createTopic = function (topic, partitions, replicas) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
+
var admin, topics, result;
|
|
64
|
+
return __generator(this, function (_a) {
|
|
65
|
+
switch (_a.label) {
|
|
66
|
+
case 0:
|
|
67
|
+
admin = this.kafka.admin();
|
|
68
|
+
return [4 /*yield*/, admin.connect()];
|
|
69
|
+
case 1:
|
|
70
|
+
_a.sent();
|
|
71
|
+
return [4 /*yield*/, admin.fetchTopicMetadata({ topics: [topic] }).catch(function (err) { return null; })];
|
|
72
|
+
case 2:
|
|
73
|
+
topics = _a.sent();
|
|
74
|
+
logger.log("Topic infos:", topics === null || topics === void 0 ? void 0 : topics.topics);
|
|
75
|
+
if (!!(topics === null || topics === void 0 ? void 0 : topics.topics.length)) return [3 /*break*/, 4];
|
|
76
|
+
return [4 /*yield*/, admin.createTopics({
|
|
77
|
+
topics: [{
|
|
78
|
+
topic: topic,
|
|
79
|
+
numPartitions: partitions,
|
|
80
|
+
replicationFactor: replicas
|
|
81
|
+
}],
|
|
82
|
+
})];
|
|
83
|
+
case 3:
|
|
84
|
+
_a.sent();
|
|
85
|
+
logger.log("New topic created:", topic, '|', partitions, '|', replicas);
|
|
86
|
+
return [3 /*break*/, 6];
|
|
87
|
+
case 4:
|
|
88
|
+
if (!(topics.topics[0].partitions.length < partitions)) return [3 /*break*/, 6];
|
|
89
|
+
logger.log("Malo partitions:", topics.topics[0].partitions.length);
|
|
90
|
+
// Увеличиваем число партиций
|
|
91
|
+
return [4 /*yield*/, admin.createPartitions({
|
|
92
|
+
topicPartitions: [{
|
|
93
|
+
topic: topic,
|
|
94
|
+
count: partitions, // новое общее количество партиций
|
|
95
|
+
}],
|
|
96
|
+
})];
|
|
97
|
+
case 5:
|
|
98
|
+
// Увеличиваем число партиций
|
|
99
|
+
_a.sent();
|
|
100
|
+
logger.log("Topic partitions change:", topic, '|', partitions);
|
|
101
|
+
_a.label = 6;
|
|
102
|
+
case 6: return [4 /*yield*/, admin.alterConfigs({
|
|
103
|
+
validateOnly: false,
|
|
104
|
+
resources: [{
|
|
105
|
+
type: kafkajsModule.ConfigResourceTypes.TOPIC,
|
|
106
|
+
name: topic,
|
|
107
|
+
configEntries: [{
|
|
108
|
+
name: 'retention.ms',
|
|
109
|
+
value: (24 * 60 * 60 * 1000).toString() // 86400000
|
|
110
|
+
}]
|
|
111
|
+
}]
|
|
112
|
+
})];
|
|
113
|
+
case 7:
|
|
114
|
+
result = _a.sent();
|
|
115
|
+
logger.log("Alter config result:", result);
|
|
116
|
+
return [4 /*yield*/, admin.disconnect()];
|
|
117
|
+
case 8:
|
|
118
|
+
_a.sent();
|
|
119
|
+
return [2 /*return*/];
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
KafkaConnection.prototype.createProducer = function (config) {
|
|
125
|
+
return new kafka_producer_1.KafkaProducer(this, config);
|
|
126
|
+
};
|
|
127
|
+
KafkaConnection.prototype.createConsumer = function (config) {
|
|
128
|
+
return new kafka_consumer_1.KafkaConsumer(this, config);
|
|
129
|
+
};
|
|
130
|
+
KafkaConnection.cleanExit = function () {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
132
|
+
var consumers, producers, _i, consumers_1, c, _a, producers_1, p;
|
|
133
|
+
return __generator(this, function (_b) {
|
|
134
|
+
switch (_b.label) {
|
|
135
|
+
case 0:
|
|
136
|
+
consumers = exports.instances.consumers;
|
|
137
|
+
producers = exports.instances.producers;
|
|
138
|
+
exports.instances.producers = [];
|
|
139
|
+
exports.instances.consumers = [];
|
|
140
|
+
_i = 0, consumers_1 = consumers;
|
|
141
|
+
_b.label = 1;
|
|
142
|
+
case 1:
|
|
143
|
+
if (!(_i < consumers_1.length)) return [3 /*break*/, 4];
|
|
144
|
+
c = consumers_1[_i];
|
|
145
|
+
return [4 /*yield*/, c.disconnect().catch(function (err) { return logger.error(err); })];
|
|
146
|
+
case 2:
|
|
147
|
+
_b.sent();
|
|
148
|
+
_b.label = 3;
|
|
149
|
+
case 3:
|
|
150
|
+
_i++;
|
|
151
|
+
return [3 /*break*/, 1];
|
|
152
|
+
case 4:
|
|
153
|
+
_a = 0, producers_1 = producers;
|
|
154
|
+
_b.label = 5;
|
|
155
|
+
case 5:
|
|
156
|
+
if (!(_a < producers_1.length)) return [3 /*break*/, 8];
|
|
157
|
+
p = producers_1[_a];
|
|
158
|
+
return [4 /*yield*/, p.disconnect().catch(function (err) { return logger.error(err); })];
|
|
159
|
+
case 6:
|
|
160
|
+
_b.sent();
|
|
161
|
+
_b.label = 7;
|
|
162
|
+
case 7:
|
|
163
|
+
_a++;
|
|
164
|
+
return [3 /*break*/, 5];
|
|
165
|
+
case 8: return [2 /*return*/];
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
return KafkaConnection;
|
|
171
|
+
}());
|
|
172
|
+
exports.KafkaConnection = KafkaConnection;
|
|
173
|
+
function cleanExit() {
|
|
174
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
175
|
+
return __generator(this, function (_a) {
|
|
176
|
+
switch (_a.label) {
|
|
177
|
+
case 0: return [4 /*yield*/, KafkaConnection.cleanExit()];
|
|
178
|
+
case 1:
|
|
179
|
+
_a.sent();
|
|
180
|
+
process.exit(0);
|
|
181
|
+
return [2 /*return*/];
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
process.on('SIGTERM', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
187
|
+
return __generator(this, function (_a) {
|
|
188
|
+
switch (_a.label) {
|
|
189
|
+
case 0: return [4 /*yield*/, cleanExit()];
|
|
190
|
+
case 1:
|
|
191
|
+
_a.sent();
|
|
192
|
+
return [2 /*return*/];
|
|
193
|
+
}
|
|
194
|
+
});
|
|
195
|
+
}); });
|
|
196
|
+
process.on('SIGINT', function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
197
|
+
return __generator(this, function (_a) {
|
|
198
|
+
switch (_a.label) {
|
|
199
|
+
case 0: return [4 /*yield*/, cleanExit()];
|
|
200
|
+
case 1:
|
|
201
|
+
_a.sent();
|
|
202
|
+
return [2 /*return*/];
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
}); });
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Message as KafkaMessage, Producer, ProducerConfig } from "kafkajs";
|
|
2
|
+
import { KafkaConnection } from "./kafka";
|
|
3
|
+
export declare class KafkaProducer {
|
|
4
|
+
readonly connection: KafkaConnection;
|
|
5
|
+
readonly producer: Producer;
|
|
6
|
+
constructor(connection: KafkaConnection, config?: ProducerConfig);
|
|
7
|
+
private _needConnection;
|
|
8
|
+
connect(): Promise<void>;
|
|
9
|
+
sendMessage(topic: string, data: any, options?: Omit<KafkaMessage, "value">): Promise<void>;
|
|
10
|
+
disconnect(): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.KafkaProducer = void 0;
|
|
40
|
+
var utils_1 = require("@klapeks/utils");
|
|
41
|
+
var logger = new utils_1.Logger("Kafka Producer");
|
|
42
|
+
var KafkaProducer = /** @class */ (function () {
|
|
43
|
+
function KafkaProducer(connection, config) {
|
|
44
|
+
this._needConnection = true;
|
|
45
|
+
this.connection = connection;
|
|
46
|
+
this.producer = connection.kafka.producer(config);
|
|
47
|
+
}
|
|
48
|
+
KafkaProducer.prototype.connect = function () {
|
|
49
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
50
|
+
return __generator(this, function (_a) {
|
|
51
|
+
switch (_a.label) {
|
|
52
|
+
case 0:
|
|
53
|
+
if (!this._needConnection)
|
|
54
|
+
return [2 /*return*/];
|
|
55
|
+
return [4 /*yield*/, this.producer.connect()];
|
|
56
|
+
case 1:
|
|
57
|
+
_a.sent();
|
|
58
|
+
this._needConnection = false;
|
|
59
|
+
return [2 /*return*/];
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
KafkaProducer.prototype.sendMessage = function (topic, data, options) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var message;
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0:
|
|
70
|
+
if (!this._needConnection) return [3 /*break*/, 2];
|
|
71
|
+
return [4 /*yield*/, this.connect()];
|
|
72
|
+
case 1:
|
|
73
|
+
_a.sent();
|
|
74
|
+
_a.label = 2;
|
|
75
|
+
case 2:
|
|
76
|
+
message = (options || {});
|
|
77
|
+
message.value = JSON.stringify(data);
|
|
78
|
+
return [4 /*yield*/, this.producer.send({ topic: topic, messages: [message] })];
|
|
79
|
+
case 3:
|
|
80
|
+
_a.sent();
|
|
81
|
+
return [2 /*return*/];
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
KafkaProducer.prototype.disconnect = function () {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
88
|
+
return __generator(this, function (_a) {
|
|
89
|
+
switch (_a.label) {
|
|
90
|
+
case 0: return [4 /*yield*/, this.producer.disconnect()];
|
|
91
|
+
case 1:
|
|
92
|
+
_a.sent();
|
|
93
|
+
logger.log("Consumer disconnected:", this.connection.clientId);
|
|
94
|
+
return [2 /*return*/];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
return KafkaProducer;
|
|
100
|
+
}());
|
|
101
|
+
exports.KafkaProducer = KafkaProducer;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import mysql2 from 'mysql2';
|
|
2
|
+
import { DatabaseOptions } from '@klapeks/utils';
|
|
3
|
+
export declare class RawMySQLConnection {
|
|
4
|
+
private _pool;
|
|
5
|
+
private _poolOptions;
|
|
6
|
+
constructor(options: DatabaseOptions & {
|
|
7
|
+
type: "mysql";
|
|
8
|
+
});
|
|
9
|
+
get pool(): mysql2.Pool | undefined;
|
|
10
|
+
takePool(): Promise<mysql2.Pool>;
|
|
11
|
+
destroyConnection(): Promise<void>;
|
|
12
|
+
getConnection(): Promise<mysql2.PoolConnection>;
|
|
13
|
+
runSQL<T = any>(query: string, params?: any[]): Promise<T[]>;
|
|
14
|
+
runSQL_One<T = any>(query: string, params?: any[]): Promise<T | null>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
|
+
};
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.RawMySQLConnection = void 0;
|
|
43
|
+
var mysql2_1 = __importDefault(require("mysql2"));
|
|
44
|
+
var utils_1 = require("@klapeks/utils");
|
|
45
|
+
var logger = new utils_1.Logger('MySQL');
|
|
46
|
+
var RawMySQLConnection = /** @class */ (function () {
|
|
47
|
+
function RawMySQLConnection(options) {
|
|
48
|
+
this._poolOptions = {
|
|
49
|
+
user: options.username,
|
|
50
|
+
password: options.password,
|
|
51
|
+
host: options.host,
|
|
52
|
+
port: options.port,
|
|
53
|
+
database: options.database,
|
|
54
|
+
charset: options.charset,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
Object.defineProperty(RawMySQLConnection.prototype, "pool", {
|
|
58
|
+
get: function () {
|
|
59
|
+
return this._pool;
|
|
60
|
+
},
|
|
61
|
+
enumerable: false,
|
|
62
|
+
configurable: true
|
|
63
|
+
});
|
|
64
|
+
RawMySQLConnection.prototype.takePool = function () {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var _this = this;
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
if (this._pool)
|
|
69
|
+
return [2 /*return*/, this._pool];
|
|
70
|
+
// if (this.options.type != 'mysql') throw "Not a mysql";
|
|
71
|
+
this._pool = mysql2_1.default.createPool(this._poolOptions);
|
|
72
|
+
this._pool.on('connection', function () {
|
|
73
|
+
logger.log('Database connected');
|
|
74
|
+
});
|
|
75
|
+
this._pool.on('close', function () {
|
|
76
|
+
var _a;
|
|
77
|
+
logger.log('Database disconnected');
|
|
78
|
+
(_a = _this._pool) === null || _a === void 0 ? void 0 : _a.end();
|
|
79
|
+
_this._pool = undefined;
|
|
80
|
+
});
|
|
81
|
+
return [2 /*return*/, this._pool];
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
};
|
|
85
|
+
RawMySQLConnection.prototype.destroyConnection = function () {
|
|
86
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
87
|
+
var _this = this;
|
|
88
|
+
return __generator(this, function (_a) {
|
|
89
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
90
|
+
if (!_this._pool)
|
|
91
|
+
return resolve();
|
|
92
|
+
_this._pool.end(function (err) {
|
|
93
|
+
_this._pool = undefined;
|
|
94
|
+
if (err)
|
|
95
|
+
reject(err);
|
|
96
|
+
else
|
|
97
|
+
resolve();
|
|
98
|
+
});
|
|
99
|
+
})];
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
};
|
|
103
|
+
RawMySQLConnection.prototype.getConnection = function () {
|
|
104
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
105
|
+
var _a;
|
|
106
|
+
var _this = this;
|
|
107
|
+
return __generator(this, function (_b) {
|
|
108
|
+
switch (_b.label) {
|
|
109
|
+
case 0:
|
|
110
|
+
if (!!this._pool) return [3 /*break*/, 2];
|
|
111
|
+
_a = this;
|
|
112
|
+
return [4 /*yield*/, this.takePool()];
|
|
113
|
+
case 1:
|
|
114
|
+
_a._pool = _b.sent();
|
|
115
|
+
_b.label = 2;
|
|
116
|
+
case 2: return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
117
|
+
if (!_this._pool)
|
|
118
|
+
return reject("Can't connect to (unknown) mysql");
|
|
119
|
+
_this._pool.getConnection(function (err, conn) {
|
|
120
|
+
if (err)
|
|
121
|
+
return reject(err);
|
|
122
|
+
resolve(conn);
|
|
123
|
+
});
|
|
124
|
+
})];
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
RawMySQLConnection.prototype.runSQL = function (query, params) {
|
|
130
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
131
|
+
var connection;
|
|
132
|
+
return __generator(this, function (_a) {
|
|
133
|
+
switch (_a.label) {
|
|
134
|
+
case 0: return [4 /*yield*/, this.getConnection()];
|
|
135
|
+
case 1:
|
|
136
|
+
connection = _a.sent();
|
|
137
|
+
return [2 /*return*/, new Promise(function (resolve, reject) {
|
|
138
|
+
// logger.debug("SQL query: ", query, '| params:', params||[])
|
|
139
|
+
connection.query(query, params || [], function (err, result) {
|
|
140
|
+
connection.release();
|
|
141
|
+
if (err)
|
|
142
|
+
return reject(err);
|
|
143
|
+
resolve(result);
|
|
144
|
+
});
|
|
145
|
+
})];
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
};
|
|
150
|
+
RawMySQLConnection.prototype.runSQL_One = function (query, params) {
|
|
151
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
152
|
+
var res;
|
|
153
|
+
return __generator(this, function (_a) {
|
|
154
|
+
switch (_a.label) {
|
|
155
|
+
case 0: return [4 /*yield*/, this.runSQL(query, params)];
|
|
156
|
+
case 1:
|
|
157
|
+
res = _a.sent();
|
|
158
|
+
return [2 /*return*/, res.length ? (res[0] || res) : null];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
return RawMySQLConnection;
|
|
164
|
+
}());
|
|
165
|
+
exports.RawMySQLConnection = RawMySQLConnection;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference types="mongoose/types/aggregate" />
|
|
2
|
+
/// <reference types="mongoose/types/callback" />
|
|
3
|
+
/// <reference types="mongoose/types/collection" />
|
|
4
|
+
/// <reference types="mongoose/types/connection" />
|
|
5
|
+
/// <reference types="mongoose/types/cursor" />
|
|
6
|
+
/// <reference types="mongoose/types/document" />
|
|
7
|
+
/// <reference types="mongoose/types/error" />
|
|
8
|
+
/// <reference types="mongoose/types/expressions" />
|
|
9
|
+
/// <reference types="mongoose/types/helpers" />
|
|
10
|
+
/// <reference types="mongoose/types/middlewares" />
|
|
11
|
+
/// <reference types="mongoose/types/indexes" />
|
|
12
|
+
/// <reference types="mongoose/types/models" />
|
|
13
|
+
/// <reference types="mongoose/types/mongooseoptions" />
|
|
14
|
+
/// <reference types="mongoose/types/pipelinestage" />
|
|
15
|
+
/// <reference types="mongoose/types/populate" />
|
|
16
|
+
/// <reference types="mongoose/types/query" />
|
|
17
|
+
/// <reference types="mongoose/types/schemaoptions" />
|
|
18
|
+
/// <reference types="mongoose/types/session" />
|
|
19
|
+
/// <reference types="mongoose/types/types" />
|
|
20
|
+
/// <reference types="mongoose/types/utility" />
|
|
21
|
+
/// <reference types="mongoose/types/validation" />
|
|
22
|
+
/// <reference types="mongoose/types/virtuals" />
|
|
23
|
+
/// <reference types="mongoose/types/schematypes" />
|
|
24
|
+
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
+
/// <reference types="mongoose/types/inferrawdoctype" />
|
|
26
|
+
import { DatabaseOptions, Flatten } from "@klapeks/utils";
|
|
27
|
+
import type { Schema as MongooseSchema, PipelineStage, QuerySelector } from "mongoose";
|
|
28
|
+
import { Point } from "typeorm";
|
|
29
|
+
export type MongoDBConnectOptions = Omit<DatabaseOptions & {
|
|
30
|
+
type: "mysql";
|
|
31
|
+
}, "type"> & {
|
|
32
|
+
type: "mongo";
|
|
33
|
+
};
|
|
34
|
+
export type MongoSearchQuery<T extends object> = {
|
|
35
|
+
[K in keyof Flatten<T>]?: Flatten<T>[K] | QuerySelector<Flatten<T>[K]>;
|
|
36
|
+
};
|
|
37
|
+
export declare function flattenForUpdate<T extends object>(object: T, prefix?: string): Partial<Flatten<T>>;
|
|
38
|
+
export declare function cursorOffsetAggregation<T extends object>(sortField: keyof Flatten<T>, lastValue: number | string, fieldSort: 'asc' | 'desc', idField: keyof Flatten<T>, lastId: number | string): PipelineStage;
|
|
39
|
+
export declare class MongoDBConnection {
|
|
40
|
+
static createCounter<T extends string>(name?: string): {
|
|
41
|
+
readonly model: any;
|
|
42
|
+
readonly increment: (model: T, increment?: number) => Promise<any>;
|
|
43
|
+
readonly set: (model: T, value: number) => Promise<any>;
|
|
44
|
+
};
|
|
45
|
+
static getOptions(): MongoDBConnectOptions;
|
|
46
|
+
static init(databaseName: string): Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
export declare const MongoGeoPoint: MongooseSchema<Point, import("mongoose").Model<Point, any, any, any, import("mongoose").Document<unknown, any, Point, any, {}> & Point & {
|
|
49
|
+
_id: import("mongoose").Types.ObjectId;
|
|
50
|
+
} & {
|
|
51
|
+
__v: number;
|
|
52
|
+
}, any>, {}, {}, {}, {}, import("mongoose").DefaultSchemaOptions, Point, import("mongoose").Document<unknown, {}, import("mongoose").FlatRecord<Point>, {}, import("mongoose").ResolveSchemaOptions<import("mongoose").DefaultSchemaOptions>> & import("mongoose").FlatRecord<Point> & {
|
|
53
|
+
_id: import("mongoose").Types.ObjectId;
|
|
54
|
+
} & {
|
|
55
|
+
__v: number;
|
|
56
|
+
}>;
|