@e-mc/db 0.3.3 → 0.4.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 +10 -10
- package/index.d.ts +4 -4
- package/index.js +291 -291
- package/package.json +4 -4
- package/pool.d.ts +4 -4
- package/pool.js +139 -139
- package/util.js +106 -106
package/LICENSE
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
Copyright 2023 An Pham
|
|
2
|
-
|
|
3
|
-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
-
|
|
5
|
-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
-
|
|
7
|
-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
-
|
|
9
|
-
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
-
|
|
1
|
+
Copyright 2023 An Pham
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
11
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DbConstructor } from '../types/lib';
|
|
2
|
-
|
|
3
|
-
declare const Db: DbConstructor;
|
|
4
|
-
|
|
1
|
+
import type { DbConstructor } from '../types/lib';
|
|
2
|
+
|
|
3
|
+
declare const Db: DbConstructor;
|
|
4
|
+
|
|
5
5
|
export = Db;
|
package/index.js
CHANGED
|
@@ -1,294 +1,294 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const path = require("path");
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const core_1 = require("../core");
|
|
6
|
-
const request_1 = require("../request");
|
|
7
|
-
const util_1 = require("./util");
|
|
8
|
-
const DB_CLIENT = new Map();
|
|
9
|
-
const POOL_CONFIG = new Map();
|
|
10
|
-
function sanitizePoolConfig(value) {
|
|
11
|
-
value.min ?? (value.min = -1);
|
|
12
|
-
value.max ?? (value.max = -1);
|
|
13
|
-
value.idle ?? (value.idle = -1);
|
|
14
|
-
value.queue_max ?? (value.queue_max = -1);
|
|
15
|
-
value.queue_idle ?? (value.queue_idle = -1);
|
|
16
|
-
value.purge ?? (value.purge = 0);
|
|
17
|
-
return value;
|
|
18
|
-
}
|
|
19
|
-
function setCert(items, cache) {
|
|
20
|
-
if (Array.isArray(items)) {
|
|
21
|
-
return items.map(cert => (0, types_1.isString)(cert) ? this.readTLSCert(cert, cache) : Buffer.isBuffer(cert) ? cert : null).filter(cert => cert);
|
|
22
|
-
}
|
|
23
|
-
if ((0, types_1.isString)(items)) {
|
|
24
|
-
return this.readTLSCert(items, cache);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
class Db extends core_1.ClientDb {
|
|
28
|
-
constructor() {
|
|
29
|
-
super(...arguments);
|
|
30
|
-
this._moduleName = 'db';
|
|
31
|
-
this._threadable = true;
|
|
32
|
-
}
|
|
33
|
-
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
34
|
-
let result = 0;
|
|
35
|
-
if (percent > 0 && percent <= 1) {
|
|
36
|
-
for (const [name, client] of DB_CLIENT) {
|
|
37
|
-
const config = POOL_CONFIG.get(name);
|
|
38
|
-
if (config && config.purge > 0) {
|
|
39
|
-
const checkTimeout = client.checkTimeout?.bind(client);
|
|
40
|
-
if (checkTimeout) {
|
|
41
|
-
try {
|
|
42
|
-
result += await checkTimeout(config.purge, limit);
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return result + (parent ? await super.purgeMemory(typeof parent === 'number' && parent > 0 ? parent : percent, 0, true) : 0);
|
|
51
|
-
}
|
|
52
|
-
static setPoolConfig(value) {
|
|
53
|
-
for (const name in value) {
|
|
54
|
-
const source = value[name];
|
|
55
|
-
if ((0, types_1.isPlainObject)(source)) {
|
|
56
|
-
POOL_CONFIG.set(name, sanitizePoolConfig(source));
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
static getPoolConfig(source) {
|
|
61
|
-
return POOL_CONFIG.get(source);
|
|
62
|
-
}
|
|
63
|
-
setCredential(item) {
|
|
64
|
-
try {
|
|
65
|
-
return this.getClient(item.source).setCredential.call(this, item);
|
|
66
|
-
}
|
|
67
|
-
catch (err) {
|
|
68
|
-
return Promise.reject(err instanceof Error ? err : new Error(Db.asString(err) || "Invalid credentials" /* ERR_DB.CREDENTIALS */));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
getCredential(item) {
|
|
72
|
-
let credential = item.credential, stored;
|
|
73
|
-
if (typeof credential === 'string') {
|
|
74
|
-
credential = this.module[item.source]?.[credential];
|
|
75
|
-
stored = true;
|
|
76
|
-
}
|
|
77
|
-
if ((0, types_1.isPlainObject)(credential)) {
|
|
78
|
-
if (this.settingsOf(item.source, 'coerce', 'credential') === true) {
|
|
79
|
-
(0, types_1.coerceObject)(credential, stored);
|
|
80
|
-
}
|
|
81
|
-
return stored ? item.credential = { ...credential } : credential;
|
|
82
|
-
}
|
|
83
|
-
return item.credential = undefined;
|
|
84
|
-
}
|
|
85
|
-
hasSource(source, ...type) {
|
|
86
|
-
try {
|
|
87
|
-
const client = this.getClient(source);
|
|
88
|
-
return type.length === 0 || type.every(value => (client.DB_SOURCE_TYPE & value) === value);
|
|
89
|
-
}
|
|
90
|
-
catch {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
executeQuery(item, options) {
|
|
95
|
-
if (this.aborted) {
|
|
96
|
-
return Promise.reject((0, types_1.createAbortError)());
|
|
97
|
-
}
|
|
98
|
-
try {
|
|
99
|
-
return this.getClient(item.source).executeQuery.call(this, item, options);
|
|
100
|
-
}
|
|
101
|
-
catch (err) {
|
|
102
|
-
return Promise.reject(err);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
executeBatchQuery(batch, options, outResult) {
|
|
106
|
-
if (this.aborted) {
|
|
107
|
-
return Promise.reject((0, types_1.createAbortError)());
|
|
108
|
-
}
|
|
109
|
-
try {
|
|
110
|
-
return this.getClient(batch[0].source).executeBatchQuery.call(this, batch, options, outResult);
|
|
111
|
-
}
|
|
112
|
-
catch (err) {
|
|
113
|
-
return Promise.reject(err);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
processRows(batch, tasks, parallel, outResult) {
|
|
117
|
-
let disconnect;
|
|
118
|
-
if ((0, types_1.isObject)(parallel)) {
|
|
119
|
-
({ disconnect, parallel } = parallel);
|
|
120
|
-
}
|
|
121
|
-
const terminate = () => {
|
|
122
|
-
this.applyState(batch, 8 /* DB_TRANSACTION.TERMINATE */);
|
|
123
|
-
if (typeof disconnect === 'function') {
|
|
124
|
-
try {
|
|
125
|
-
disconnect();
|
|
126
|
-
}
|
|
127
|
-
catch {
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
};
|
|
131
|
-
const cleanup = () => {
|
|
132
|
-
this.applyState(batch, 16 /* DB_TRANSACTION.ABORT */);
|
|
133
|
-
terminate();
|
|
134
|
-
if (outResult) {
|
|
135
|
-
for (let i = 0, length = outResult.length; i < length; ++i) {
|
|
136
|
-
outResult[i] = null;
|
|
137
|
-
}
|
|
138
|
-
return outResult;
|
|
139
|
-
}
|
|
140
|
-
return [];
|
|
141
|
-
};
|
|
142
|
-
if (tasks.length === 0) {
|
|
143
|
-
return Promise.resolve(cleanup());
|
|
144
|
-
}
|
|
145
|
-
if (!parallel && outResult) {
|
|
146
|
-
terminate();
|
|
147
|
-
return Promise.resolve(outResult);
|
|
148
|
-
}
|
|
149
|
-
return Promise.all(tasks)
|
|
150
|
-
.then(result => {
|
|
151
|
-
terminate();
|
|
152
|
-
if (outResult) {
|
|
153
|
-
for (let i = 0, length = outResult.length; i < length; ++i) {
|
|
154
|
-
outResult[i] = result[i] || null;
|
|
155
|
-
}
|
|
156
|
-
return outResult;
|
|
157
|
-
}
|
|
158
|
-
return result;
|
|
159
|
-
})
|
|
160
|
-
.catch(() => cleanup());
|
|
161
|
-
}
|
|
162
|
-
handleFail(err, item, options) {
|
|
163
|
-
this.add(item, 32 /* DB_TRANSACTION.FAIL */);
|
|
164
|
-
item.transactionFail = true;
|
|
165
|
-
if (options && typeof options.errorQuery === 'function') {
|
|
166
|
-
if (options.errorQuery(err, item, options.commandType)) {
|
|
167
|
-
if (item.willAbort) {
|
|
168
|
-
this.abort();
|
|
169
|
-
}
|
|
170
|
-
return true;
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
if (item.willAbort) {
|
|
175
|
-
this.abort();
|
|
176
|
-
}
|
|
177
|
-
this.writeFail(["Unable to execute query" /* ERR_DB.EXEC_QUERY */, item.source], err, 65536 /* LOG_TYPE.DB */);
|
|
178
|
-
}
|
|
179
|
-
return false;
|
|
180
|
-
}
|
|
181
|
-
commit(items) {
|
|
182
|
-
if (this.aborted) {
|
|
183
|
-
return Promise.reject((0, types_1.createAbortError)());
|
|
184
|
-
}
|
|
185
|
-
const tasks = (items || this.pending).map(data => {
|
|
186
|
-
data.ignoreCache ?? (data.ignoreCache = true);
|
|
187
|
-
return this.executeQuery(data).catch(() => {
|
|
188
|
-
this.applyState([data], 16 /* DB_TRANSACTION.ABORT */);
|
|
189
|
-
return [];
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
return tasks.length === 0 ? Promise.resolve(false) : this.allSettled(tasks, ["Execute unassigned queries" /* VAL_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
|
|
193
|
-
}
|
|
194
|
-
readTLSCert(value, cache) {
|
|
195
|
-
if ((0, types_1.isString)(value)) {
|
|
196
|
-
let pathname = value.trim();
|
|
197
|
-
if (request_1.default.isCert(pathname)) {
|
|
198
|
-
return pathname;
|
|
199
|
-
}
|
|
200
|
-
if (Db.isPath(pathname = path.resolve(pathname)) && this.canRead(pathname, { ownPermissionOnly: true })) {
|
|
201
|
-
return request_1.default.readCACert(pathname, cache);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
return '';
|
|
205
|
-
}
|
|
206
|
-
readTLSConfig(options, cache = true) {
|
|
207
|
-
if ((0, types_1.isPlainObject)(options)) {
|
|
208
|
-
const { ca, cert, key } = options;
|
|
209
|
-
if (ca) {
|
|
210
|
-
options.ca = setCert.call(this, ca);
|
|
211
|
-
}
|
|
212
|
-
if (cert) {
|
|
213
|
-
options.cert = setCert.call(this, cert);
|
|
214
|
-
}
|
|
215
|
-
if (key) {
|
|
216
|
-
if ((0, types_1.isString)(key)) {
|
|
217
|
-
options.key = this.readTLSCert(key, cache);
|
|
218
|
-
}
|
|
219
|
-
else if ((0, types_1.isArray)(key)) {
|
|
220
|
-
options.key = key.map(item => (0, types_1.isString)(item) ? { pem: this.readTLSCert(item, cache) } : Buffer.isBuffer(item) ? { pem: item.toString('utf-8') } : item).filter(item => item.pem);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
resolveSource(source, folder) {
|
|
226
|
-
let result;
|
|
227
|
-
if (source[0] !== '@') {
|
|
228
|
-
switch (source) {
|
|
229
|
-
case 'mongodb':
|
|
230
|
-
case 'mssql':
|
|
231
|
-
case 'mysql':
|
|
232
|
-
case 'oracle':
|
|
233
|
-
case 'postgres':
|
|
234
|
-
case 'redis':
|
|
235
|
-
result = '@
|
|
236
|
-
break;
|
|
237
|
-
default:
|
|
238
|
-
result = source;
|
|
239
|
-
break;
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
else if ((result = source).indexOf('/') === -1) {
|
|
243
|
-
folder || (folder = 'client');
|
|
244
|
-
}
|
|
245
|
-
return result + (folder ? '/' + folder : '');
|
|
246
|
-
}
|
|
247
|
-
getPoolConfig(source, uuidKey) {
|
|
248
|
-
const config = Db.getPoolConfig(source);
|
|
249
|
-
const result = uuidKey && this.settingsKey(uuidKey, 'pool');
|
|
250
|
-
if (result) {
|
|
251
|
-
if (config) {
|
|
252
|
-
result.min ?? (result.min = config.min);
|
|
253
|
-
result.max ?? (result.max = config.max);
|
|
254
|
-
result.idle ?? (result.idle = config.idle);
|
|
255
|
-
result.queue_max ?? (result.queue_max = config.queue_max);
|
|
256
|
-
result.queue_idle ?? (result.queue_idle = config.queue_idle);
|
|
257
|
-
}
|
|
258
|
-
else {
|
|
259
|
-
sanitizePoolConfig(result);
|
|
260
|
-
}
|
|
261
|
-
return result;
|
|
262
|
-
}
|
|
263
|
-
return config;
|
|
264
|
-
}
|
|
265
|
-
get sourceType() {
|
|
266
|
-
return types_1.DB_TYPE;
|
|
267
|
-
}
|
|
268
|
-
get commandType() {
|
|
269
|
-
return util_1.SQL_COMMAND;
|
|
270
|
-
}
|
|
271
|
-
getClient(source) {
|
|
272
|
-
let client = DB_CLIENT.get(source);
|
|
273
|
-
if (client) {
|
|
274
|
-
return client;
|
|
275
|
-
}
|
|
276
|
-
try {
|
|
277
|
-
client = require(this.resolveSource(source));
|
|
278
|
-
if (client?.DB_SOURCE_CLIENT) {
|
|
279
|
-
client.DB_SOURCE_NAME = source;
|
|
280
|
-
DB_CLIENT.set(source, client);
|
|
281
|
-
return client;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
catch {
|
|
285
|
-
}
|
|
286
|
-
throw (0, types_1.errorMessage)(source, "Database provider not found" /* ERR_DB.PROVIDER_NOTFOUND */);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
Object.freeze(types_1.DB_TYPE);
|
|
290
|
-
Object.freeze(util_1.SQL_COMMAND);
|
|
291
|
-
exports.default = Db;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path = require("path");
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const core_1 = require("../core");
|
|
6
|
+
const request_1 = require("../request");
|
|
7
|
+
const util_1 = require("./util");
|
|
8
|
+
const DB_CLIENT = new Map();
|
|
9
|
+
const POOL_CONFIG = new Map();
|
|
10
|
+
function sanitizePoolConfig(value) {
|
|
11
|
+
value.min ?? (value.min = -1);
|
|
12
|
+
value.max ?? (value.max = -1);
|
|
13
|
+
value.idle ?? (value.idle = -1);
|
|
14
|
+
value.queue_max ?? (value.queue_max = -1);
|
|
15
|
+
value.queue_idle ?? (value.queue_idle = -1);
|
|
16
|
+
value.purge ?? (value.purge = 0);
|
|
17
|
+
return value;
|
|
18
|
+
}
|
|
19
|
+
function setCert(items, cache) {
|
|
20
|
+
if (Array.isArray(items)) {
|
|
21
|
+
return items.map(cert => (0, types_1.isString)(cert) ? this.readTLSCert(cert, cache) : Buffer.isBuffer(cert) ? cert : null).filter(cert => cert);
|
|
22
|
+
}
|
|
23
|
+
if ((0, types_1.isString)(items)) {
|
|
24
|
+
return this.readTLSCert(items, cache);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class Db extends core_1.ClientDb {
|
|
28
|
+
constructor() {
|
|
29
|
+
super(...arguments);
|
|
30
|
+
this._moduleName = 'db';
|
|
31
|
+
this._threadable = true;
|
|
32
|
+
}
|
|
33
|
+
static async purgeMemory(percent = 1, limit = 0, parent) {
|
|
34
|
+
let result = 0;
|
|
35
|
+
if (percent > 0 && percent <= 1) {
|
|
36
|
+
for (const [name, client] of DB_CLIENT) {
|
|
37
|
+
const config = POOL_CONFIG.get(name);
|
|
38
|
+
if (config && config.purge > 0) {
|
|
39
|
+
const checkTimeout = client.checkTimeout?.bind(client);
|
|
40
|
+
if (checkTimeout) {
|
|
41
|
+
try {
|
|
42
|
+
result += await checkTimeout(config.purge, limit);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return result + (parent ? await super.purgeMemory(typeof parent === 'number' && parent > 0 ? parent : percent, 0, true) : 0);
|
|
51
|
+
}
|
|
52
|
+
static setPoolConfig(value) {
|
|
53
|
+
for (const name in value) {
|
|
54
|
+
const source = value[name];
|
|
55
|
+
if ((0, types_1.isPlainObject)(source)) {
|
|
56
|
+
POOL_CONFIG.set(name, sanitizePoolConfig(source));
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
static getPoolConfig(source) {
|
|
61
|
+
return POOL_CONFIG.get(source);
|
|
62
|
+
}
|
|
63
|
+
setCredential(item) {
|
|
64
|
+
try {
|
|
65
|
+
return this.getClient(item.source).setCredential.call(this, item);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
return Promise.reject(err instanceof Error ? err : new Error(Db.asString(err) || "Invalid credentials" /* ERR_DB.CREDENTIALS */));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
getCredential(item) {
|
|
72
|
+
let credential = item.credential, stored;
|
|
73
|
+
if (typeof credential === 'string') {
|
|
74
|
+
credential = this.module[item.source]?.[credential];
|
|
75
|
+
stored = true;
|
|
76
|
+
}
|
|
77
|
+
if ((0, types_1.isPlainObject)(credential)) {
|
|
78
|
+
if (this.settingsOf(item.source, 'coerce', 'credential') === true) {
|
|
79
|
+
(0, types_1.coerceObject)(credential, stored);
|
|
80
|
+
}
|
|
81
|
+
return stored ? item.credential = { ...credential } : credential;
|
|
82
|
+
}
|
|
83
|
+
return item.credential = undefined;
|
|
84
|
+
}
|
|
85
|
+
hasSource(source, ...type) {
|
|
86
|
+
try {
|
|
87
|
+
const client = this.getClient(source);
|
|
88
|
+
return type.length === 0 || type.every(value => (client.DB_SOURCE_TYPE & value) === value);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
executeQuery(item, options) {
|
|
95
|
+
if (this.aborted) {
|
|
96
|
+
return Promise.reject((0, types_1.createAbortError)());
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
return this.getClient(item.source).executeQuery.call(this, item, options);
|
|
100
|
+
}
|
|
101
|
+
catch (err) {
|
|
102
|
+
return Promise.reject(err);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
executeBatchQuery(batch, options, outResult) {
|
|
106
|
+
if (this.aborted) {
|
|
107
|
+
return Promise.reject((0, types_1.createAbortError)());
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
return this.getClient(batch[0].source).executeBatchQuery.call(this, batch, options, outResult);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
return Promise.reject(err);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
processRows(batch, tasks, parallel, outResult) {
|
|
117
|
+
let disconnect;
|
|
118
|
+
if ((0, types_1.isObject)(parallel)) {
|
|
119
|
+
({ disconnect, parallel } = parallel);
|
|
120
|
+
}
|
|
121
|
+
const terminate = () => {
|
|
122
|
+
this.applyState(batch, 8 /* DB_TRANSACTION.TERMINATE */);
|
|
123
|
+
if (typeof disconnect === 'function') {
|
|
124
|
+
try {
|
|
125
|
+
disconnect();
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
const cleanup = () => {
|
|
132
|
+
this.applyState(batch, 16 /* DB_TRANSACTION.ABORT */);
|
|
133
|
+
terminate();
|
|
134
|
+
if (outResult) {
|
|
135
|
+
for (let i = 0, length = outResult.length; i < length; ++i) {
|
|
136
|
+
outResult[i] = null;
|
|
137
|
+
}
|
|
138
|
+
return outResult;
|
|
139
|
+
}
|
|
140
|
+
return [];
|
|
141
|
+
};
|
|
142
|
+
if (tasks.length === 0) {
|
|
143
|
+
return Promise.resolve(cleanup());
|
|
144
|
+
}
|
|
145
|
+
if (!parallel && outResult) {
|
|
146
|
+
terminate();
|
|
147
|
+
return Promise.resolve(outResult);
|
|
148
|
+
}
|
|
149
|
+
return Promise.all(tasks)
|
|
150
|
+
.then(result => {
|
|
151
|
+
terminate();
|
|
152
|
+
if (outResult) {
|
|
153
|
+
for (let i = 0, length = outResult.length; i < length; ++i) {
|
|
154
|
+
outResult[i] = result[i] || null;
|
|
155
|
+
}
|
|
156
|
+
return outResult;
|
|
157
|
+
}
|
|
158
|
+
return result;
|
|
159
|
+
})
|
|
160
|
+
.catch(() => cleanup());
|
|
161
|
+
}
|
|
162
|
+
handleFail(err, item, options) {
|
|
163
|
+
this.add(item, 32 /* DB_TRANSACTION.FAIL */);
|
|
164
|
+
item.transactionFail = true;
|
|
165
|
+
if (options && typeof options.errorQuery === 'function') {
|
|
166
|
+
if (options.errorQuery(err, item, options.commandType)) {
|
|
167
|
+
if (item.willAbort) {
|
|
168
|
+
this.abort();
|
|
169
|
+
}
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
if (item.willAbort) {
|
|
175
|
+
this.abort();
|
|
176
|
+
}
|
|
177
|
+
this.writeFail(["Unable to execute query" /* ERR_DB.EXEC_QUERY */, item.source], err, 65536 /* LOG_TYPE.DB */);
|
|
178
|
+
}
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
commit(items) {
|
|
182
|
+
if (this.aborted) {
|
|
183
|
+
return Promise.reject((0, types_1.createAbortError)());
|
|
184
|
+
}
|
|
185
|
+
const tasks = (items || this.pending).map(data => {
|
|
186
|
+
data.ignoreCache ?? (data.ignoreCache = true);
|
|
187
|
+
return this.executeQuery(data).catch(() => {
|
|
188
|
+
this.applyState([data], 16 /* DB_TRANSACTION.ABORT */);
|
|
189
|
+
return [];
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
return tasks.length === 0 ? Promise.resolve(false) : this.allSettled(tasks, ["Execute unassigned queries" /* VAL_DB.EXEC_QUERYUNASSIGNED */, this.moduleName]).then(result => result.length > 0).catch(() => false);
|
|
193
|
+
}
|
|
194
|
+
readTLSCert(value, cache) {
|
|
195
|
+
if ((0, types_1.isString)(value)) {
|
|
196
|
+
let pathname = value.trim();
|
|
197
|
+
if (request_1.default.isCert(pathname)) {
|
|
198
|
+
return pathname;
|
|
199
|
+
}
|
|
200
|
+
if (Db.isPath(pathname = path.resolve(pathname)) && this.canRead(pathname, { ownPermissionOnly: true })) {
|
|
201
|
+
return request_1.default.readCACert(pathname, cache);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return '';
|
|
205
|
+
}
|
|
206
|
+
readTLSConfig(options, cache = true) {
|
|
207
|
+
if ((0, types_1.isPlainObject)(options)) {
|
|
208
|
+
const { ca, cert, key } = options;
|
|
209
|
+
if (ca) {
|
|
210
|
+
options.ca = setCert.call(this, ca);
|
|
211
|
+
}
|
|
212
|
+
if (cert) {
|
|
213
|
+
options.cert = setCert.call(this, cert);
|
|
214
|
+
}
|
|
215
|
+
if (key) {
|
|
216
|
+
if ((0, types_1.isString)(key)) {
|
|
217
|
+
options.key = this.readTLSCert(key, cache);
|
|
218
|
+
}
|
|
219
|
+
else if ((0, types_1.isArray)(key)) {
|
|
220
|
+
options.key = key.map(item => (0, types_1.isString)(item) ? { pem: this.readTLSCert(item, cache) } : Buffer.isBuffer(item) ? { pem: item.toString('utf-8') } : item).filter(item => item.pem);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
resolveSource(source, folder) {
|
|
226
|
+
let result;
|
|
227
|
+
if (source[0] !== '@') {
|
|
228
|
+
switch (source) {
|
|
229
|
+
case 'mongodb':
|
|
230
|
+
case 'mssql':
|
|
231
|
+
case 'mysql':
|
|
232
|
+
case 'oracle':
|
|
233
|
+
case 'postgres':
|
|
234
|
+
case 'redis':
|
|
235
|
+
result = '@pi-r/' + source;
|
|
236
|
+
break;
|
|
237
|
+
default:
|
|
238
|
+
result = source;
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
else if ((result = source).indexOf('/') === -1) {
|
|
243
|
+
folder || (folder = 'client');
|
|
244
|
+
}
|
|
245
|
+
return result + (folder ? '/' + folder : '');
|
|
246
|
+
}
|
|
247
|
+
getPoolConfig(source, uuidKey) {
|
|
248
|
+
const config = Db.getPoolConfig(source);
|
|
249
|
+
const result = uuidKey && this.settingsKey(uuidKey, 'pool');
|
|
250
|
+
if (result) {
|
|
251
|
+
if (config) {
|
|
252
|
+
result.min ?? (result.min = config.min);
|
|
253
|
+
result.max ?? (result.max = config.max);
|
|
254
|
+
result.idle ?? (result.idle = config.idle);
|
|
255
|
+
result.queue_max ?? (result.queue_max = config.queue_max);
|
|
256
|
+
result.queue_idle ?? (result.queue_idle = config.queue_idle);
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
sanitizePoolConfig(result);
|
|
260
|
+
}
|
|
261
|
+
return result;
|
|
262
|
+
}
|
|
263
|
+
return config;
|
|
264
|
+
}
|
|
265
|
+
get sourceType() {
|
|
266
|
+
return types_1.DB_TYPE;
|
|
267
|
+
}
|
|
268
|
+
get commandType() {
|
|
269
|
+
return util_1.SQL_COMMAND;
|
|
270
|
+
}
|
|
271
|
+
getClient(source) {
|
|
272
|
+
let client = DB_CLIENT.get(source);
|
|
273
|
+
if (client) {
|
|
274
|
+
return client;
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
client = require(this.resolveSource(source));
|
|
278
|
+
if (client?.DB_SOURCE_CLIENT) {
|
|
279
|
+
client.DB_SOURCE_NAME = source;
|
|
280
|
+
DB_CLIENT.set(source, client);
|
|
281
|
+
return client;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
}
|
|
286
|
+
throw (0, types_1.errorMessage)(source, "Database provider not found" /* ERR_DB.PROVIDER_NOTFOUND */);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
Object.freeze(types_1.DB_TYPE);
|
|
290
|
+
Object.freeze(util_1.SQL_COMMAND);
|
|
291
|
+
exports.default = Db;
|
|
292
292
|
|
|
293
293
|
if (exports.default) {
|
|
294
294
|
module.exports = exports.default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e-mc/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "DB modules for E-mc.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "BSD 3-Clause",
|
|
21
21
|
"homepage": "https://github.com/anpham6/e-mc#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/core": "0.
|
|
24
|
-
"@e-mc/request": "0.
|
|
25
|
-
"@e-mc/types": "0.
|
|
23
|
+
"@e-mc/core": "0.4.1",
|
|
24
|
+
"@e-mc/request": "0.4.1",
|
|
25
|
+
"@e-mc/types": "0.4.1"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/pool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DbPoolConstructor } from '../types/lib/db';
|
|
2
|
-
|
|
3
|
-
declare const DbPool: DbPoolConstructor;
|
|
4
|
-
|
|
1
|
+
import type { DbPoolConstructor } from '../types/lib/db';
|
|
2
|
+
|
|
3
|
+
declare const DbPool: DbPoolConstructor;
|
|
4
|
+
|
|
5
5
|
export = DbPool;
|
package/pool.js
CHANGED
|
@@ -1,142 +1,142 @@
|
|
|
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;
|
|
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
140
|
|
|
141
141
|
if (exports.default) {
|
|
142
142
|
module.exports = exports.default;
|
package/util.js
CHANGED
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasBasicAuth = exports.getBasicAuth = exports.setUUIDKey = exports.checkEmpty = exports.parseConnectionString = exports.parseServerAuth = exports.SQL_COMMAND = void 0;
|
|
4
|
-
const types_1 = require("../types");
|
|
5
|
-
const util_1 = require("../request/util");
|
|
6
|
-
Object.defineProperty(exports, "getBasicAuth", { enumerable: true, get: function () { return util_1.getBasicAuth; } });
|
|
7
|
-
Object.defineProperty(exports, "hasBasicAuth", { enumerable: true, get: function () { return util_1.hasBasicAuth; } });
|
|
8
|
-
var SQL_COMMAND;
|
|
9
|
-
(function (SQL_COMMAND) {
|
|
10
|
-
SQL_COMMAND[SQL_COMMAND["SELECT"] = 1] = "SELECT";
|
|
11
|
-
SQL_COMMAND[SQL_COMMAND["INSERT"] = 2] = "INSERT";
|
|
12
|
-
SQL_COMMAND[SQL_COMMAND["UPDATE"] = 3] = "UPDATE";
|
|
13
|
-
SQL_COMMAND[SQL_COMMAND["DELETE"] = 4] = "DELETE";
|
|
14
|
-
})(SQL_COMMAND = exports.SQL_COMMAND || (exports.SQL_COMMAND = {}));
|
|
15
|
-
function parseServerAuth(credential, port, all) {
|
|
16
|
-
if (typeof port === 'boolean') {
|
|
17
|
-
all = port;
|
|
18
|
-
port = undefined;
|
|
19
|
-
}
|
|
20
|
-
let protocol, server, hostname, username, password, database;
|
|
21
|
-
if ('protocol' in credential) {
|
|
22
|
-
protocol = credential.protocol;
|
|
23
|
-
delete credential.protocol;
|
|
24
|
-
}
|
|
25
|
-
if ('server' in credential) {
|
|
26
|
-
server = credential.server;
|
|
27
|
-
delete credential.server;
|
|
28
|
-
}
|
|
29
|
-
if ('hostname' in credential) {
|
|
30
|
-
hostname = credential.hostname;
|
|
31
|
-
delete credential.hostname;
|
|
32
|
-
}
|
|
33
|
-
if ('port' in credential) {
|
|
34
|
-
const value = +credential.port;
|
|
35
|
-
if (value > 0) {
|
|
36
|
-
port = value;
|
|
37
|
-
credential.port = port;
|
|
38
|
-
if (all) {
|
|
39
|
-
delete credential.port;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
delete credential.port;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if ('username' in credential) {
|
|
47
|
-
username = credential.username;
|
|
48
|
-
delete credential.username;
|
|
49
|
-
}
|
|
50
|
-
if ('password' in credential) {
|
|
51
|
-
password = credential.password;
|
|
52
|
-
if (all) {
|
|
53
|
-
delete credential.password;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
if ('database' in credential) {
|
|
57
|
-
database = credential.database;
|
|
58
|
-
if (all) {
|
|
59
|
-
delete credential.database;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
if (!server) {
|
|
63
|
-
if (hostname) {
|
|
64
|
-
server = hostname + (port ? ':' + port : '');
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
else if (!hostname) {
|
|
68
|
-
const match = /^\s*([^:]+)(?::(\d+))?\s*$/.exec(server);
|
|
69
|
-
if (match) {
|
|
70
|
-
hostname = match[1];
|
|
71
|
-
if (match[2]) {
|
|
72
|
-
port = +match[2];
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return { protocol, server, hostname, port, username, password, database };
|
|
77
|
-
}
|
|
78
|
-
exports.parseServerAuth = parseServerAuth;
|
|
79
|
-
function parseConnectionString(value, scheme = 'http') {
|
|
80
|
-
if (!/^[^:]+:\/\//.test(value)) {
|
|
81
|
-
value = scheme + '://' + value;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
const { protocol, username, password, hostname, pathname, port, search } = new URL(value);
|
|
85
|
-
const database = pathname.substring(1);
|
|
86
|
-
return { protocol, username: decodeURIComponent(username), password: decodeURIComponent(password), hostname, port, database: decodeURIComponent(/^([^?/#]+)/.exec(database)?.[1] || database), pathname, search };
|
|
87
|
-
}
|
|
88
|
-
catch {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
exports.parseConnectionString = parseConnectionString;
|
|
93
|
-
function checkEmpty(value) {
|
|
94
|
-
return value === undefined || value === null ? false : value === 0 || (value instanceof Set ? value.size === 0 : (Array.isArray(value) || (0, types_1.isObject)(value)) && value.length === 0);
|
|
95
|
-
}
|
|
96
|
-
exports.checkEmpty = checkEmpty;
|
|
97
|
-
function setUUIDKey(item, value) {
|
|
98
|
-
var _a;
|
|
99
|
-
if ((0, types_1.isString)(value)) {
|
|
100
|
-
if (!(0, types_1.isPlainObject)(item.credential)) {
|
|
101
|
-
item.credential = {};
|
|
102
|
-
}
|
|
103
|
-
(_a = item.credential).uuidKey || (_a.uuidKey = value);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
exports.setUUIDKey = setUUIDKey;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.hasBasicAuth = exports.getBasicAuth = exports.setUUIDKey = exports.checkEmpty = exports.parseConnectionString = exports.parseServerAuth = exports.SQL_COMMAND = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const util_1 = require("../request/util");
|
|
6
|
+
Object.defineProperty(exports, "getBasicAuth", { enumerable: true, get: function () { return util_1.getBasicAuth; } });
|
|
7
|
+
Object.defineProperty(exports, "hasBasicAuth", { enumerable: true, get: function () { return util_1.hasBasicAuth; } });
|
|
8
|
+
var SQL_COMMAND;
|
|
9
|
+
(function (SQL_COMMAND) {
|
|
10
|
+
SQL_COMMAND[SQL_COMMAND["SELECT"] = 1] = "SELECT";
|
|
11
|
+
SQL_COMMAND[SQL_COMMAND["INSERT"] = 2] = "INSERT";
|
|
12
|
+
SQL_COMMAND[SQL_COMMAND["UPDATE"] = 3] = "UPDATE";
|
|
13
|
+
SQL_COMMAND[SQL_COMMAND["DELETE"] = 4] = "DELETE";
|
|
14
|
+
})(SQL_COMMAND = exports.SQL_COMMAND || (exports.SQL_COMMAND = {}));
|
|
15
|
+
function parseServerAuth(credential, port, all) {
|
|
16
|
+
if (typeof port === 'boolean') {
|
|
17
|
+
all = port;
|
|
18
|
+
port = undefined;
|
|
19
|
+
}
|
|
20
|
+
let protocol, server, hostname, username, password, database;
|
|
21
|
+
if ('protocol' in credential) {
|
|
22
|
+
protocol = credential.protocol;
|
|
23
|
+
delete credential.protocol;
|
|
24
|
+
}
|
|
25
|
+
if ('server' in credential) {
|
|
26
|
+
server = credential.server;
|
|
27
|
+
delete credential.server;
|
|
28
|
+
}
|
|
29
|
+
if ('hostname' in credential) {
|
|
30
|
+
hostname = credential.hostname;
|
|
31
|
+
delete credential.hostname;
|
|
32
|
+
}
|
|
33
|
+
if ('port' in credential) {
|
|
34
|
+
const value = +credential.port;
|
|
35
|
+
if (value > 0) {
|
|
36
|
+
port = value;
|
|
37
|
+
credential.port = port;
|
|
38
|
+
if (all) {
|
|
39
|
+
delete credential.port;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
delete credential.port;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if ('username' in credential) {
|
|
47
|
+
username = credential.username;
|
|
48
|
+
delete credential.username;
|
|
49
|
+
}
|
|
50
|
+
if ('password' in credential) {
|
|
51
|
+
password = credential.password;
|
|
52
|
+
if (all) {
|
|
53
|
+
delete credential.password;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if ('database' in credential) {
|
|
57
|
+
database = credential.database;
|
|
58
|
+
if (all) {
|
|
59
|
+
delete credential.database;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!server) {
|
|
63
|
+
if (hostname) {
|
|
64
|
+
server = hostname + (port ? ':' + port : '');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (!hostname) {
|
|
68
|
+
const match = /^\s*([^:]+)(?::(\d+))?\s*$/.exec(server);
|
|
69
|
+
if (match) {
|
|
70
|
+
hostname = match[1];
|
|
71
|
+
if (match[2]) {
|
|
72
|
+
port = +match[2];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return { protocol, server, hostname, port, username, password, database };
|
|
77
|
+
}
|
|
78
|
+
exports.parseServerAuth = parseServerAuth;
|
|
79
|
+
function parseConnectionString(value, scheme = 'http') {
|
|
80
|
+
if (!/^[^:]+:\/\//.test(value)) {
|
|
81
|
+
value = scheme + '://' + value;
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
const { protocol, username, password, hostname, pathname, port, search } = new URL(value);
|
|
85
|
+
const database = pathname.substring(1);
|
|
86
|
+
return { protocol, username: decodeURIComponent(username), password: decodeURIComponent(password), hostname, port, database: decodeURIComponent(/^([^?/#]+)/.exec(database)?.[1] || database), pathname, search };
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
exports.parseConnectionString = parseConnectionString;
|
|
93
|
+
function checkEmpty(value) {
|
|
94
|
+
return value === undefined || value === null ? false : value === 0 || (value instanceof Set ? value.size === 0 : (Array.isArray(value) || (0, types_1.isObject)(value)) && value.length === 0);
|
|
95
|
+
}
|
|
96
|
+
exports.checkEmpty = checkEmpty;
|
|
97
|
+
function setUUIDKey(item, value) {
|
|
98
|
+
var _a;
|
|
99
|
+
if ((0, types_1.isString)(value)) {
|
|
100
|
+
if (!(0, types_1.isPlainObject)(item.credential)) {
|
|
101
|
+
item.credential = {};
|
|
102
|
+
}
|
|
103
|
+
(_a = item.credential).uuidKey || (_a.uuidKey = value);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
exports.setUUIDKey = setUUIDKey;
|