@e-mc/db 0.0.2 → 0.0.4
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/index.js +21 -13
- package/package.json +4 -4
- package/mongodb/index.d.ts +0 -12
- package/mongodb/index.js +0 -679
- package/mssql/index.d.ts +0 -7
- package/mssql/index.js +0 -505
- package/mysql/index.d.ts +0 -10
- package/mysql/index.js +0 -332
- package/oracle/index.d.ts +0 -7
- package/oracle/index.js +0 -358
- package/postgres/index.d.ts +0 -7
- package/postgres/index.js +0 -272
- package/redis/index.d.ts +0 -7
- package/redis/index.js +0 -550
package/oracle/index.js
DELETED
|
@@ -1,358 +0,0 @@
|
|
|
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_DB.AUTH */;
|
|
185
|
-
let client;
|
|
186
|
-
if (oraclePool) {
|
|
187
|
-
clients.push(client = await oraclePool.getConnection());
|
|
188
|
-
item.transactionState &= ~64 /* TRANSACTION_DB.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_DB.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_DB.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_DB.COMMIT */ | 128 /* TRANSACTION_DB.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_DB.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_DB.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/postgres/index.d.ts
DELETED
package/postgres/index.js
DELETED
|
@@ -1,272 +0,0 @@
|
|
|
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 mysql_1 = require("../mysql");
|
|
6
|
-
const types_1 = require("../../types");
|
|
7
|
-
const index_1 = require("../index");
|
|
8
|
-
const pool_1 = require("../pool");
|
|
9
|
-
class PostgresPool extends pool_1.default {
|
|
10
|
-
getConnection() {
|
|
11
|
-
return this.client.connect();
|
|
12
|
-
}
|
|
13
|
-
close() {
|
|
14
|
-
return this.client.end();
|
|
15
|
-
}
|
|
16
|
-
isEmpty() {
|
|
17
|
-
return this.closed || this.client.totalCount === 0 && this.client.waitingCount === 0;
|
|
18
|
-
}
|
|
19
|
-
get closed() {
|
|
20
|
-
return this.client.ended;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
const POOL_STATE = {};
|
|
24
|
-
function removePoolProperties(options) {
|
|
25
|
-
if (!options.uuidKey && ('ssl' in options || 'max' in options || 'min' in options || 'idleTimeoutMillis' in options || 'log' in options || 'allowExitOnIdle' in options || 'maxUses' in options)) {
|
|
26
|
-
return { ...options, ssl: (0, types_1.isObject)(options.ssl) ? true : undefined, max: undefined, min: undefined, idleTimeoutMillis: undefined, log: undefined, allowExitOnIdle: undefined, maxUses: undefined };
|
|
27
|
-
}
|
|
28
|
-
return options;
|
|
29
|
-
}
|
|
30
|
-
const getPoolKey = (credential) => (credential.host || '') + '_' + (credential.port || '5432') + (credential.user || '') + '_' + (credential.password || '') + '_' + (credential.database || '') + '_' + (credential.connectionString || '');
|
|
31
|
-
function setCredential(item) {
|
|
32
|
-
let credential = this.getCredential(item);
|
|
33
|
-
if (credential) {
|
|
34
|
-
mysql_1.setAuthentication.call(this, credential);
|
|
35
|
-
}
|
|
36
|
-
else {
|
|
37
|
-
credential = { connectionString: item.uri };
|
|
38
|
-
item.credential = credential;
|
|
39
|
-
}
|
|
40
|
-
if (!credential.connectionString) {
|
|
41
|
-
const errors = [];
|
|
42
|
-
if (!credential.host) {
|
|
43
|
-
errors.push('host');
|
|
44
|
-
}
|
|
45
|
-
if (!credential.user) {
|
|
46
|
-
errors.push('user');
|
|
47
|
-
}
|
|
48
|
-
if (errors.length) {
|
|
49
|
-
throw (0, types_1.errorMessage)("postgres" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
const usePool = item.usePool;
|
|
53
|
-
if (item.usePool && typeof credential.password !== 'function') {
|
|
54
|
-
let username, password, pool;
|
|
55
|
-
if (typeof usePool === 'string' && (username = credential.user || credential.connectionString && (0, util_1.parseConnectionString)(credential.connectionString)?.username)) {
|
|
56
|
-
[password, pool] = pool_1.default.validateKey(POOL_STATE, username, usePool);
|
|
57
|
-
if (pool) {
|
|
58
|
-
pool.add(item, password);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
const poolKey = getPoolKey(credential);
|
|
63
|
-
if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
|
|
64
|
-
const { Pool } = require("pg" /* STRINGS.PACKAGE_NAME */);
|
|
65
|
-
const config = this.getPoolConfig("postgres" /* STRINGS.MODULE_NAME */, password);
|
|
66
|
-
if (config) {
|
|
67
|
-
const { min, max, idle } = config;
|
|
68
|
-
if (min >= 0) {
|
|
69
|
-
credential.min ?? (credential.min = min);
|
|
70
|
-
}
|
|
71
|
-
if (max > 0) {
|
|
72
|
-
credential.max ?? (credential.max = max);
|
|
73
|
-
}
|
|
74
|
-
if (idle > 0) {
|
|
75
|
-
credential.idleTimeoutMillis ?? (credential.idleTimeoutMillis = idle);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
new PostgresPool(new Pool(credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
pool.add(item);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
else if ('usePool' in item) {
|
|
85
|
-
item.usePool = false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
exports.setCredential = setCredential;
|
|
89
|
-
async function executeQuery(item, options) {
|
|
90
|
-
return (await executeBatchQuery.call(this, [item], options))[0] || [];
|
|
91
|
-
}
|
|
92
|
-
exports.executeQuery = executeQuery;
|
|
93
|
-
async function executeBatchQuery(batch, options = '', outResult) {
|
|
94
|
-
const length = batch.length;
|
|
95
|
-
if (length === 0) {
|
|
96
|
-
return [];
|
|
97
|
-
}
|
|
98
|
-
const db = require("pg" /* STRINGS.PACKAGE_NAME */);
|
|
99
|
-
let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
100
|
-
if ((0, types_1.isPlainObject)(options)) {
|
|
101
|
-
({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
102
|
-
}
|
|
103
|
-
else {
|
|
104
|
-
if (typeof options === 'string') {
|
|
105
|
-
sessionKey = options;
|
|
106
|
-
}
|
|
107
|
-
options = undefined;
|
|
108
|
-
}
|
|
109
|
-
if (length === 1) {
|
|
110
|
-
connectOnce = false;
|
|
111
|
-
parallel = false;
|
|
112
|
-
}
|
|
113
|
-
else if (parallel === undefined) {
|
|
114
|
-
parallel = !batch.some(item => item.parallel === false);
|
|
115
|
-
}
|
|
116
|
-
if (!parallel) {
|
|
117
|
-
outResult || (outResult = new Array(length));
|
|
118
|
-
}
|
|
119
|
-
const caching = this.hasCache("postgres" /* STRINGS.MODULE_NAME */, sessionKey);
|
|
120
|
-
const tasks = new Array(length);
|
|
121
|
-
const clients = [];
|
|
122
|
-
const pools = [];
|
|
123
|
-
let postgresPool, postgresClient, postgresCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
|
|
124
|
-
const getConnection = async (item, credential) => {
|
|
125
|
-
item.transactionState = 64 /* TRANSACTION_DB.AUTH */;
|
|
126
|
-
let client;
|
|
127
|
-
if (postgresPool) {
|
|
128
|
-
pools.push(client = await postgresPool.getConnection());
|
|
129
|
-
item.transactionState &= ~64 /* TRANSACTION_DB.AUTH */;
|
|
130
|
-
return client;
|
|
131
|
-
}
|
|
132
|
-
const pool = item.usePool && pool_1.default.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
|
|
133
|
-
if (pool) {
|
|
134
|
-
try {
|
|
135
|
-
pools.push(client = await pool.getConnection());
|
|
136
|
-
if (connectOnce) {
|
|
137
|
-
postgresPool = pool;
|
|
138
|
-
}
|
|
139
|
-
pool.connected = true;
|
|
140
|
-
}
|
|
141
|
-
catch (err) {
|
|
142
|
-
let close;
|
|
143
|
-
switch (err instanceof Error && err.code) {
|
|
144
|
-
case '28000':
|
|
145
|
-
case '28P01':
|
|
146
|
-
close = 1;
|
|
147
|
-
break;
|
|
148
|
-
default:
|
|
149
|
-
close = pool.closeable;
|
|
150
|
-
break;
|
|
151
|
-
}
|
|
152
|
-
if (close) {
|
|
153
|
-
await pool.detach(true);
|
|
154
|
-
if (close === 1) {
|
|
155
|
-
throw err;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
pool.connected = false;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
if (!client) {
|
|
162
|
-
clients.push(client = new db.Client((postgresCredential || credential)));
|
|
163
|
-
await client.connect();
|
|
164
|
-
}
|
|
165
|
-
if (connectOnce) {
|
|
166
|
-
if (!parallel) {
|
|
167
|
-
postgresClient = client;
|
|
168
|
-
}
|
|
169
|
-
postgresCredential = credential;
|
|
170
|
-
}
|
|
171
|
-
item.transactionState &= ~64 /* TRANSACTION_DB.AUTH */;
|
|
172
|
-
return client;
|
|
173
|
-
};
|
|
174
|
-
for (let i = 0; i < length; ++i) {
|
|
175
|
-
const item = batch[i];
|
|
176
|
-
const { source, query, params, ignoreCache } = item;
|
|
177
|
-
let credential = postgresCredential || onceCredential, error;
|
|
178
|
-
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 */))) {
|
|
179
|
-
if (this.handleFail(error, item, { errorQuery })) {
|
|
180
|
-
if (!parallel) {
|
|
181
|
-
tasks.length = 0;
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
184
|
-
tasks[i] = Promise.reject(error);
|
|
185
|
-
}
|
|
186
|
-
else if (parallel) {
|
|
187
|
-
tasks[i] = Promise.resolve([]);
|
|
188
|
-
}
|
|
189
|
-
else {
|
|
190
|
-
outResult[i] = [];
|
|
191
|
-
}
|
|
192
|
-
continue;
|
|
193
|
-
}
|
|
194
|
-
item.transactionState = 1 /* TRANSACTION_DB.ACTIVE */;
|
|
195
|
-
const renewCache = ignoreCache === 0;
|
|
196
|
-
const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
|
|
197
|
-
let queryString = '';
|
|
198
|
-
if (caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) {
|
|
199
|
-
queryString = index_1.default.asString(query, true) + '_' + index_1.default.asString(params, true);
|
|
200
|
-
if (ignoreCache !== 1) {
|
|
201
|
-
const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
|
|
202
|
-
if (result) {
|
|
203
|
-
if (parallel) {
|
|
204
|
-
tasks[i] = Promise.resolve(result);
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
outResult[i] = result;
|
|
208
|
-
}
|
|
209
|
-
this.add(item, 4 /* TRANSACTION_DB.COMMIT */ | 128 /* TRANSACTION_DB.CACHE */);
|
|
210
|
-
continue;
|
|
211
|
-
}
|
|
212
|
-
if (!ignoreCache && outCacheMiss) {
|
|
213
|
-
outCacheMiss.push(source);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
if (onceCredential && parallel) {
|
|
218
|
-
try {
|
|
219
|
-
postgresClient = await getConnection(item, onceCredential);
|
|
220
|
-
}
|
|
221
|
-
catch {
|
|
222
|
-
connectOnce = false;
|
|
223
|
-
parallel = false;
|
|
224
|
-
}
|
|
225
|
-
onceCredential = undefined;
|
|
226
|
-
}
|
|
227
|
-
tasks[i] = new Promise(async (resolve, reject) => {
|
|
228
|
-
let commandType;
|
|
229
|
-
try {
|
|
230
|
-
const client = postgresClient || await getConnection(item, credential);
|
|
231
|
-
if (postgresClient && parallel) {
|
|
232
|
-
postgresClient = undefined;
|
|
233
|
-
}
|
|
234
|
-
commandType = this.commandType.SELECT;
|
|
235
|
-
const { rows, command } = await client.query(query, params);
|
|
236
|
-
this.add(item, 4 /* TRANSACTION_DB.COMMIT */);
|
|
237
|
-
resolve(rows.length || command === 'SELECT' ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
|
|
238
|
-
}
|
|
239
|
-
catch (err) {
|
|
240
|
-
if (this.handleFail(err, item, { errorQuery, commandType })) {
|
|
241
|
-
reject(err);
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
resolve([]);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
});
|
|
248
|
-
if (!parallel) {
|
|
249
|
-
try {
|
|
250
|
-
outResult[i] = await tasks[i];
|
|
251
|
-
}
|
|
252
|
-
catch {
|
|
253
|
-
tasks.length = 0;
|
|
254
|
-
break;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return this.processRows(batch, tasks, {
|
|
259
|
-
disconnect: () => {
|
|
260
|
-
clients.forEach(item => item.end(err => err && this.writeFail(["Unable to close connnection" /* ERR_DB.CONNECTION_CLOSE */, "postgres" /* STRINGS.MODULE_NAME */], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false })));
|
|
261
|
-
pools.forEach(item => item.release());
|
|
262
|
-
},
|
|
263
|
-
parallel
|
|
264
|
-
}, outResult);
|
|
265
|
-
}
|
|
266
|
-
exports.executeBatchQuery = executeBatchQuery;
|
|
267
|
-
function checkTimeout(value, limit = 0) {
|
|
268
|
-
return pool_1.default.checkTimeout(POOL_STATE, value, limit);
|
|
269
|
-
}
|
|
270
|
-
exports.checkTimeout = checkTimeout;
|
|
271
|
-
exports.DB_SOURCE_CLIENT = true;
|
|
272
|
-
exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
|