@e-mc/db 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/LICENSE +11 -0
- package/README.md +5 -0
- package/index.d.ts +5 -0
- package/index.js +288 -0
- package/mongodb/index.d.ts +19 -0
- package/mongodb/index.js +679 -0
- package/mssql/index.d.ts +7 -0
- package/mssql/index.js +505 -0
- package/mysql/index.d.ts +10 -0
- package/mysql/index.js +332 -0
- package/oracle/index.d.ts +7 -0
- package/oracle/index.js +358 -0
- package/package.json +26 -0
- package/pool.d.ts +5 -0
- package/pool.js +144 -0
- package/postgres/index.d.ts +7 -0
- package/postgres/index.js +272 -0
- package/redis/index.d.ts +7 -0
- package/redis/index.js +550 -0
- package/types.d.ts +14 -0
- package/util.d.ts +17 -0
- package/util.js +106 -0
package/oracle/index.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = void 0;
|
|
4
|
+
const util_1 = require("../util");
|
|
5
|
+
const types_1 = require("../../types");
|
|
6
|
+
const index_1 = require("../index");
|
|
7
|
+
const pool_1 = require("../pool");
|
|
8
|
+
class OraclePool extends pool_1.default {
|
|
9
|
+
getConnection() {
|
|
10
|
+
return this.client.getConnection();
|
|
11
|
+
}
|
|
12
|
+
close() {
|
|
13
|
+
return this.client.close();
|
|
14
|
+
}
|
|
15
|
+
isEmpty() {
|
|
16
|
+
const { connectionsInUse, currentQueueLength } = this.client.getStatistics();
|
|
17
|
+
return this.closed || connectionsInUse === 0 && currentQueueLength === 0;
|
|
18
|
+
}
|
|
19
|
+
get closeable() {
|
|
20
|
+
return super.closeable || this.client.status === 6001 /* POOL_STATUS.DRAINING */;
|
|
21
|
+
}
|
|
22
|
+
get closed() {
|
|
23
|
+
return this.client.status === 6002 /* POOL_STATUS.CLOSED */;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
const POOL_STATE = {};
|
|
27
|
+
function removePoolProperties(options) {
|
|
28
|
+
if (!options.uuidKey && ('poolMax' in options || 'poolMin' in options || 'poolIncrement' in options || 'poolTimeout' in options || 'poolPingInterval' in options || 'poolMaxPerShard' in options || 'queueMax' in options || 'queueRequests' in options || 'queueTimeout' in options)) {
|
|
29
|
+
return { ...options, poolMax: undefined, poolMin: undefined, poolIncrement: undefined, poolTimeout: undefined, poolPingInterval: undefined, poolMaxPerShard: undefined, queueMax: undefined, queueRequests: undefined, queueTimeout: undefined };
|
|
30
|
+
}
|
|
31
|
+
return options;
|
|
32
|
+
}
|
|
33
|
+
function getPoolKey(credential) {
|
|
34
|
+
if (credential.connectString && (credential.user && credential.password || credential.externalAuth)) {
|
|
35
|
+
return credential.connectString + '_' + (credential.externalAuth ? 'external' : credential.user + '_' + credential.password) + '_' + (credential.edition || '') + '_' + (credential.tag ? credential.tag + '#' + (credential.matchAny ? '1' : '0') : '') + '_' + (credential.privilege ?? '') + '_' + index_1.default.asString(credential.shardingKey) + '_' + index_1.default.asString(credential.superShardingKey);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async function setCredential(item) {
|
|
39
|
+
let credential = this.getCredential(item), hostname, port, username, database;
|
|
40
|
+
if (credential) {
|
|
41
|
+
({ hostname, port, username } = (0, util_1.parseServerAuth)(credential));
|
|
42
|
+
credential.user || (credential.user = username);
|
|
43
|
+
if ('database' in credential) {
|
|
44
|
+
database = credential.database;
|
|
45
|
+
delete credential.database;
|
|
46
|
+
}
|
|
47
|
+
if ('connectionString' in credential) {
|
|
48
|
+
credential.connectString || (credential.connectString = credential.connectionString);
|
|
49
|
+
delete credential.connectionString;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
const uri = item.uri;
|
|
54
|
+
credential = {};
|
|
55
|
+
if (uri) {
|
|
56
|
+
const connection = (0, util_1.parseConnectionString)(uri);
|
|
57
|
+
if (connection) {
|
|
58
|
+
({ hostname, port, username: credential.user, password: credential.password, database } = connection);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
credential.poolAlias = uri;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
item.credential = credential;
|
|
65
|
+
}
|
|
66
|
+
if (hostname) {
|
|
67
|
+
credential.connectString || (credential.connectString = hostname + (port ? ':' + port : '') + (database ? '/' + database : ''));
|
|
68
|
+
}
|
|
69
|
+
if (credential.externalAuth) {
|
|
70
|
+
delete credential.user;
|
|
71
|
+
delete credential.password;
|
|
72
|
+
}
|
|
73
|
+
if (!credential.poolAlias) {
|
|
74
|
+
const errors = [];
|
|
75
|
+
if (!credential.externalAuth) {
|
|
76
|
+
if (!credential.user) {
|
|
77
|
+
errors.push('user');
|
|
78
|
+
}
|
|
79
|
+
if (!credential.password) {
|
|
80
|
+
errors.push('password');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!credential.connectString) {
|
|
84
|
+
errors.push('connectString');
|
|
85
|
+
}
|
|
86
|
+
if (errors.length) {
|
|
87
|
+
throw (0, types_1.errorMessage)("oracle" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const usePool = item.usePool;
|
|
91
|
+
if (usePool) {
|
|
92
|
+
const poolAlias = credential.poolAlias;
|
|
93
|
+
let password, pool;
|
|
94
|
+
if (typeof usePool === 'string' && (username = credential.user || poolAlias)) {
|
|
95
|
+
[password, pool] = pool_1.default.validateKey(POOL_STATE, username, usePool);
|
|
96
|
+
if (pool) {
|
|
97
|
+
pool.add(item, password);
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (poolAlias) {
|
|
102
|
+
for (const poolKey in POOL_STATE) {
|
|
103
|
+
const current = POOL_STATE[poolKey];
|
|
104
|
+
if (current.client.poolAlias === poolAlias) {
|
|
105
|
+
if (!current.closed) {
|
|
106
|
+
current.add(item);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
current.remove();
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const poolKey = getPoolKey(credential);
|
|
115
|
+
if (poolKey) {
|
|
116
|
+
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
117
|
+
const { createPool } = require("oracledb" /* STRINGS.PACKAGE_NAME */);
|
|
118
|
+
const config = this.getPoolConfig("oracle" /* STRINGS.MODULE_NAME */, password);
|
|
119
|
+
if (config) {
|
|
120
|
+
const { min, max, idle, queue_max, queue_idle } = config;
|
|
121
|
+
if (min >= 0) {
|
|
122
|
+
credential.poolMin ?? (credential.poolMin = min);
|
|
123
|
+
}
|
|
124
|
+
if (max > 0) {
|
|
125
|
+
credential.poolMax ?? (credential.poolMax = max);
|
|
126
|
+
}
|
|
127
|
+
if (idle >= 0) {
|
|
128
|
+
credential.poolTimeout ?? (credential.poolTimeout = idle / 1000);
|
|
129
|
+
}
|
|
130
|
+
if (queue_max >= 0) {
|
|
131
|
+
credential.queueMax ?? (credential.queueMax = queue_max);
|
|
132
|
+
}
|
|
133
|
+
if (queue_idle >= 0) {
|
|
134
|
+
credential.queueTimeout ?? (credential.queueTimeout = queue_idle);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
new OraclePool(await createPool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
pool.add(item);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
item.usePool = false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
exports.setCredential = setCredential;
|
|
149
|
+
async function executeQuery(item, options) {
|
|
150
|
+
return (await executeBatchQuery.call(this, [item], options))[0] || [];
|
|
151
|
+
}
|
|
152
|
+
exports.executeQuery = executeQuery;
|
|
153
|
+
async function executeBatchQuery(batch, options = '', outResult) {
|
|
154
|
+
const length = batch.length;
|
|
155
|
+
if (length === 0) {
|
|
156
|
+
return [];
|
|
157
|
+
}
|
|
158
|
+
const db = require("oracledb" /* STRINGS.PACKAGE_NAME */);
|
|
159
|
+
let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
160
|
+
if ((0, types_1.isPlainObject)(options)) {
|
|
161
|
+
({ parallel, connectOnce, errorQuery, sessionKey } = options);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
if (typeof options === 'string') {
|
|
165
|
+
sessionKey = options;
|
|
166
|
+
}
|
|
167
|
+
options = undefined;
|
|
168
|
+
}
|
|
169
|
+
if (length === 1) {
|
|
170
|
+
connectOnce = false;
|
|
171
|
+
parallel = false;
|
|
172
|
+
}
|
|
173
|
+
else if (parallel === undefined) {
|
|
174
|
+
parallel = !batch.some(item => item.parallel === false);
|
|
175
|
+
}
|
|
176
|
+
if (!parallel) {
|
|
177
|
+
outResult || (outResult = new Array(length));
|
|
178
|
+
}
|
|
179
|
+
const caching = this.hasCache("oracle" /* STRINGS.MODULE_NAME */, sessionKey);
|
|
180
|
+
const tasks = new Array(length);
|
|
181
|
+
const clients = [];
|
|
182
|
+
let oraclePool, oracleClient, oracleCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
|
|
183
|
+
const getConnection = async (item, credential) => {
|
|
184
|
+
item.transactionState = 64 /* TRANSACTION.AUTH */;
|
|
185
|
+
let client;
|
|
186
|
+
if (oraclePool) {
|
|
187
|
+
clients.push(client = await oraclePool.getConnection());
|
|
188
|
+
item.transactionState &= ~64 /* TRANSACTION.AUTH */;
|
|
189
|
+
return client;
|
|
190
|
+
}
|
|
191
|
+
const pool = item.usePool && pool_1.default.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
|
|
192
|
+
if (pool) {
|
|
193
|
+
try {
|
|
194
|
+
client = await pool.getConnection();
|
|
195
|
+
if (connectOnce) {
|
|
196
|
+
oraclePool = pool;
|
|
197
|
+
}
|
|
198
|
+
pool.connected = true;
|
|
199
|
+
}
|
|
200
|
+
catch (err) {
|
|
201
|
+
let close;
|
|
202
|
+
switch (err instanceof Error && err.code) {
|
|
203
|
+
case 'ORA-01017':
|
|
204
|
+
close = 1;
|
|
205
|
+
break;
|
|
206
|
+
default:
|
|
207
|
+
close = pool.closeable;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
if (close) {
|
|
211
|
+
await pool.detach(true);
|
|
212
|
+
if (close === 1) {
|
|
213
|
+
throw err;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
pool.connected = false;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
clients.push(client || (client = await db.getConnection((oracleCredential || credential))));
|
|
220
|
+
if (connectOnce) {
|
|
221
|
+
if (!parallel) {
|
|
222
|
+
oracleClient = client;
|
|
223
|
+
}
|
|
224
|
+
oracleCredential = credential;
|
|
225
|
+
}
|
|
226
|
+
item.transactionState &= ~64 /* TRANSACTION.AUTH */;
|
|
227
|
+
return client;
|
|
228
|
+
};
|
|
229
|
+
for (let i = 0; i < length; ++i) {
|
|
230
|
+
const item = batch[i];
|
|
231
|
+
const { source, query, params, ignoreCache, options: clientOptions } = item;
|
|
232
|
+
let credential = oracleCredential || onceCredential, error;
|
|
233
|
+
if (!credential && !(0, types_1.isPlainObject)(credential = item.credential) && (error = (0, types_1.errorMessage)(source, "Invalid credentials" /* ERR_DB.CREDENTIALS */)) || !query && (error = (0, types_1.errorMessage)(source, "Missing database query" /* ERR_DB.QUERY */))) {
|
|
234
|
+
if (this.handleFail(error, item, { errorQuery })) {
|
|
235
|
+
if (!parallel) {
|
|
236
|
+
tasks.length = 0;
|
|
237
|
+
break;
|
|
238
|
+
}
|
|
239
|
+
tasks[i] = Promise.reject(error);
|
|
240
|
+
}
|
|
241
|
+
else if (parallel) {
|
|
242
|
+
tasks[i] = Promise.resolve([]);
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
outResult[i] = [];
|
|
246
|
+
}
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
item.transactionState = 1 /* TRANSACTION.ACTIVE */;
|
|
250
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("oracle" /* STRINGS.MODULE_NAME */, 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
|
|
251
|
+
const renewCache = ignoreCache === 0;
|
|
252
|
+
const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
|
|
253
|
+
let queryString = '';
|
|
254
|
+
if ((caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) && !streamRow) {
|
|
255
|
+
queryString = index_1.default.asString(query, true) + '_' + index_1.default.asString(params, true) + index_1.default.asString(clientOptions, true);
|
|
256
|
+
if (ignoreCache !== 1) {
|
|
257
|
+
const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
|
|
258
|
+
if (result) {
|
|
259
|
+
if (parallel) {
|
|
260
|
+
tasks[i] = Promise.resolve(result);
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
outResult[i] = result;
|
|
264
|
+
}
|
|
265
|
+
this.add(item, 4 /* TRANSACTION.COMMIT */ | 128 /* TRANSACTION.CACHE */);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (!ignoreCache && outCacheMiss) {
|
|
269
|
+
outCacheMiss.push(source);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (onceCredential && parallel) {
|
|
274
|
+
try {
|
|
275
|
+
oracleClient = await getConnection(item, onceCredential);
|
|
276
|
+
}
|
|
277
|
+
catch {
|
|
278
|
+
connectOnce = false;
|
|
279
|
+
parallel = false;
|
|
280
|
+
}
|
|
281
|
+
onceCredential = undefined;
|
|
282
|
+
}
|
|
283
|
+
tasks[i] = new Promise(async (resolve, reject) => {
|
|
284
|
+
let commandType;
|
|
285
|
+
try {
|
|
286
|
+
const client = oracleClient || await getConnection(item, credential);
|
|
287
|
+
if (oracleClient && parallel) {
|
|
288
|
+
oracleClient = undefined;
|
|
289
|
+
}
|
|
290
|
+
const executeOptions = { outFormat: db.OUT_FORMAT_OBJECT, ...clientOptions, resultSet: false, autoCommit: true };
|
|
291
|
+
commandType = this.commandType.SELECT;
|
|
292
|
+
if (streamRow) {
|
|
293
|
+
const rows = [];
|
|
294
|
+
const processRow = typeof streamRow === 'function' && streamRow;
|
|
295
|
+
const stream = client.queryStream(query, params || [], executeOptions);
|
|
296
|
+
stream
|
|
297
|
+
.on('data', row => {
|
|
298
|
+
if (processRow) {
|
|
299
|
+
const err = processRow(row);
|
|
300
|
+
if (err === false) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (err instanceof Error) {
|
|
304
|
+
error = err;
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
rows.push(row);
|
|
309
|
+
})
|
|
310
|
+
.on('error', err => error || (error = err))
|
|
311
|
+
.on('close', () => {
|
|
312
|
+
if (error && this.handleFail(error, item, { errorQuery, commandType })) {
|
|
313
|
+
reject(error);
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
this.add(item, 4 /* TRANSACTION.COMMIT */);
|
|
317
|
+
resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows);
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
.on('end', () => stream.destroy());
|
|
321
|
+
}
|
|
322
|
+
else {
|
|
323
|
+
const { rows } = await client.execute(query, params || [], executeOptions);
|
|
324
|
+
this.add(item, 4 /* TRANSACTION.COMMIT */);
|
|
325
|
+
resolve(rows ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
catch (err) {
|
|
329
|
+
if (this.handleFail(err, item, { errorQuery, commandType })) {
|
|
330
|
+
reject(err);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
333
|
+
resolve([]);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
if (!parallel) {
|
|
338
|
+
try {
|
|
339
|
+
outResult[i] = await tasks[i];
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
tasks.length = 0;
|
|
343
|
+
break;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return this.processRows(batch, tasks, {
|
|
348
|
+
disconnect: () => clients.forEach(item => item.close(err => err && this.writeFail(["Unable to close connnection" /* ERR_DB.CONNECTION_CLOSE */, "oracle" /* STRINGS.MODULE_NAME */], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false }))),
|
|
349
|
+
parallel
|
|
350
|
+
}, outResult);
|
|
351
|
+
}
|
|
352
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
353
|
+
function checkTimeout(value, limit = 0) {
|
|
354
|
+
return pool_1.default.checkTimeout(POOL_STATE, value, limit);
|
|
355
|
+
}
|
|
356
|
+
exports.checkTimeout = checkTimeout;
|
|
357
|
+
exports.DB_SOURCE_CLIENT = true;
|
|
358
|
+
exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@e-mc/db",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "DB modules for e-mc.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/anpham6/e-mc.git",
|
|
13
|
+
"directory": "src/db"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"squared",
|
|
17
|
+
"squared-functions"
|
|
18
|
+
],
|
|
19
|
+
"author": "An Pham <anpham6@gmail.com>",
|
|
20
|
+
"license": "BSD 3-Clause",
|
|
21
|
+
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@e-mc/core": "0.0.1",
|
|
24
|
+
"@e-mc/request": "0.0.1"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/pool.d.ts
ADDED
package/pool.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a, _b, _c;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const util_1 = require("./util");
|
|
6
|
+
const kItems = Symbol('items');
|
|
7
|
+
const kParent = Symbol('parent');
|
|
8
|
+
const kIdlePrevious = Symbol('idlePrevious');
|
|
9
|
+
class DbPool {
|
|
10
|
+
static findKey(pools, uuidKey, poolKey, ...items) {
|
|
11
|
+
if (uuidKey) {
|
|
12
|
+
let pool;
|
|
13
|
+
if (typeof uuidKey === 'string') {
|
|
14
|
+
for (const key in pools) {
|
|
15
|
+
const result = pools[key];
|
|
16
|
+
if (result.uuidKey === uuidKey) {
|
|
17
|
+
pool = result;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (pool || poolKey && (pool = pools[poolKey])) {
|
|
23
|
+
if (!pool.closed) {
|
|
24
|
+
return items.length && !items.some(item => pool.has(item)) ? null : pool;
|
|
25
|
+
}
|
|
26
|
+
pool.remove();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
static validateKey(pools, username, uuidKey) {
|
|
32
|
+
if ((0, types_1.validateUUID)(uuidKey)) {
|
|
33
|
+
if ((0, types_1.isString)(username)) {
|
|
34
|
+
for (const key in pools) {
|
|
35
|
+
const pool = pools[key];
|
|
36
|
+
const auth = pool.uuidKey;
|
|
37
|
+
if (auth?.username === username && auth.password === uuidKey) {
|
|
38
|
+
if (!pool.closed) {
|
|
39
|
+
return [uuidKey, pool];
|
|
40
|
+
}
|
|
41
|
+
delete pools[key];
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return [uuidKey, null];
|
|
47
|
+
}
|
|
48
|
+
return ['', null];
|
|
49
|
+
}
|
|
50
|
+
static async checkTimeout(pools, value, limit = 0) {
|
|
51
|
+
let result = 0;
|
|
52
|
+
for (const key in pools) {
|
|
53
|
+
const pool = pools[key];
|
|
54
|
+
if (pool.isIdle(value)) {
|
|
55
|
+
await pool.detach(true);
|
|
56
|
+
if (++result === limit) {
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
}
|
|
63
|
+
constructor(client, poolKey, uuidKey = null) {
|
|
64
|
+
this.client = client;
|
|
65
|
+
this.poolKey = poolKey;
|
|
66
|
+
this.uuidKey = uuidKey;
|
|
67
|
+
this.success = 0;
|
|
68
|
+
this.failed = 0;
|
|
69
|
+
this.lastAccessed = 0;
|
|
70
|
+
this[_a] = new WeakSet();
|
|
71
|
+
this[_b] = null;
|
|
72
|
+
this[_c] = { success: -1, failed: -1, count: 0, error: 0 };
|
|
73
|
+
}
|
|
74
|
+
add(item, uuidKey) {
|
|
75
|
+
this[kItems].add(item);
|
|
76
|
+
if (uuidKey || (uuidKey = this.uuidKey?.password)) {
|
|
77
|
+
(0, util_1.setUUIDKey)(item, uuidKey);
|
|
78
|
+
}
|
|
79
|
+
this.lastAccessed = Date.now();
|
|
80
|
+
return this;
|
|
81
|
+
}
|
|
82
|
+
has(item) {
|
|
83
|
+
return this[kItems].has(item);
|
|
84
|
+
}
|
|
85
|
+
remove() {
|
|
86
|
+
const parent = this[kParent];
|
|
87
|
+
if (parent) {
|
|
88
|
+
delete parent[this.poolKey];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
detach(force) {
|
|
92
|
+
this.remove();
|
|
93
|
+
return this.closed ? Promise.resolve() : force ? this.close().catch(() => { }) : this.close();
|
|
94
|
+
}
|
|
95
|
+
isIdle(timeout) {
|
|
96
|
+
if (this.closed) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
const { success, failed } = this;
|
|
100
|
+
const previous = this[kIdlePrevious];
|
|
101
|
+
let count = 0, error = 0;
|
|
102
|
+
if (success === previous.success && failed >= previous.failed) {
|
|
103
|
+
count = ++previous.count;
|
|
104
|
+
if (failed > previous.failed) {
|
|
105
|
+
error = ++previous.error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
previous.count = 0;
|
|
110
|
+
if (failed === previous.failed) {
|
|
111
|
+
previous.error = 0;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
previous.success = success;
|
|
115
|
+
previous.failed = failed;
|
|
116
|
+
return Date.now() - timeout >= this.lastAccessed && (this.closeable || count >= (this.uuidKey ? 10 /* IDLE_THRESHOLD.UUID */ : 5 /* IDLE_THRESHOLD.CONFIG */)) || error >= (this.uuidKey ? 3 /* IDLE_THRESHOLD.UUID_ERROR */ : 2 /* IDLE_THRESHOLD.CONFIG_ERROR */);
|
|
117
|
+
}
|
|
118
|
+
get persist() {
|
|
119
|
+
return this.uuidKey ? this.success > this.failed : false;
|
|
120
|
+
}
|
|
121
|
+
get closeable() {
|
|
122
|
+
return this.success === 0 && this.failed > 0 || !this.persist && this.isEmpty();
|
|
123
|
+
}
|
|
124
|
+
set connected(value) {
|
|
125
|
+
if (value) {
|
|
126
|
+
++this.success;
|
|
127
|
+
this.lastAccessed = Date.now();
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
++this.failed;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
set parent(value) {
|
|
134
|
+
value[this.poolKey] = this;
|
|
135
|
+
this[kParent] = value;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
_a = kItems, _b = kParent, _c = kIdlePrevious;
|
|
139
|
+
exports.default = DbPool;
|
|
140
|
+
|
|
141
|
+
if (exports.default) {
|
|
142
|
+
module.exports = exports.default;
|
|
143
|
+
module.exports.default = exports.default;
|
|
144
|
+
}
|