@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/mssql/index.js ADDED
@@ -0,0 +1,505 @@
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 types_1 = require("../../types");
5
+ const util_1 = require("../util");
6
+ const index_1 = require("../index");
7
+ const pool_1 = require("../pool");
8
+ class MSSQLPool extends pool_1.default {
9
+ getConnection() {
10
+ return new Promise((resolve, reject) => {
11
+ this.client.acquire((err, connection) => {
12
+ if (err) {
13
+ reject(err);
14
+ }
15
+ else {
16
+ resolve(connection);
17
+ }
18
+ });
19
+ });
20
+ }
21
+ close() {
22
+ return new Promise((resolve, reject) => {
23
+ try {
24
+ this.client.drain();
25
+ resolve();
26
+ }
27
+ catch (err) {
28
+ reject(err);
29
+ }
30
+ });
31
+ }
32
+ isEmpty() {
33
+ return this.closed || (0, util_1.checkEmpty)(this.client.connections) && (0, util_1.checkEmpty)(this.client.waiting);
34
+ }
35
+ get closed() {
36
+ return !!this.client.drained;
37
+ }
38
+ }
39
+ const POOL_STATE = {};
40
+ function convertParameters(sql, params, output) {
41
+ if ((0, types_1.isPlainObject)(params)) {
42
+ const items = [];
43
+ for (const name in params) {
44
+ items.push({ ...params[name], name });
45
+ }
46
+ params = items;
47
+ }
48
+ if (Array.isArray(params)) {
49
+ for (const param of params) {
50
+ const { name, value, options = {} } = param;
51
+ if (name) {
52
+ const type = convertType(sql, param.type, value, options);
53
+ if (type.name === 'TVP') {
54
+ if (output || !(0, types_1.isPlainObject)(value)) {
55
+ continue;
56
+ }
57
+ const columns = value.columns;
58
+ if ((0, types_1.isArray)(columns)) {
59
+ columns.forEach(col => col.type = convertType(sql, col.type, col.value, col.options || (col.options = {})));
60
+ }
61
+ }
62
+ this[output ? 'addOutputParameter' : 'addParameter'](name, type, value, options);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ function convertType(sql, type, value, options) {
68
+ let result;
69
+ if (typeof type === 'string') {
70
+ result = sql[type];
71
+ }
72
+ else if ((0, types_1.isObject)(type) && type.name) {
73
+ result = sql[type.name];
74
+ }
75
+ if (result) {
76
+ return result;
77
+ }
78
+ switch (typeof value) {
79
+ case 'boolean':
80
+ return sql.Bit;
81
+ case 'bigint':
82
+ return sql.BigInt;
83
+ case 'number':
84
+ if (value !== Math.floor(value)) {
85
+ return sql.Decimal;
86
+ }
87
+ if (value >= 0 && value <= 255) {
88
+ return sql.TinyInt;
89
+ }
90
+ if (value >= -32768 && value <= 32767) {
91
+ return sql.SmallInt;
92
+ }
93
+ if (value >= -2147483648 && value <= 2147483647) {
94
+ return sql.Int;
95
+ }
96
+ return sql.BigInt;
97
+ case 'string':
98
+ return sql.VarChar;
99
+ case 'object':
100
+ if (value instanceof Date) {
101
+ return sql.DateTime2;
102
+ }
103
+ if (Buffer.isBuffer(value)) {
104
+ options.length = Infinity;
105
+ return sql.VarBinary;
106
+ }
107
+ default:
108
+ return sql.Null;
109
+ }
110
+ }
111
+ function getPoolKey(credential) {
112
+ const tls = credential.options?.cryptoCredentialsDetails;
113
+ if (tls) {
114
+ if (!(0, types_1.isObject)(tls)) {
115
+ delete credential.options.cryptoCredentialsDetails;
116
+ }
117
+ else if (tls.ca || tls.cert || tls.key) {
118
+ credential = { ...credential };
119
+ credential.options = { ...credential.options };
120
+ delete credential.options.cryptoCredentialsDetails;
121
+ return JSON.stringify(credential) + '_tls';
122
+ }
123
+ }
124
+ return JSON.stringify(credential);
125
+ }
126
+ function isOutputParameters(params) {
127
+ return (0, types_1.isPlainObject)(params) && Object.keys(params).length === 2 && 'input' in params && 'output' in params;
128
+ }
129
+ function setCredential(item) {
130
+ let credential = this.getCredential(item), hostname, username, password, port, database;
131
+ if (credential) {
132
+ ({ username, password, hostname, port, database } = (0, util_1.parseServerAuth)(credential, true));
133
+ }
134
+ else {
135
+ credential = {};
136
+ if (item.uri) {
137
+ const connection = (0, util_1.parseConnectionString)(item.uri);
138
+ if (connection) {
139
+ ({ username, password, hostname, port, database } = connection);
140
+ }
141
+ }
142
+ item.credential = credential;
143
+ }
144
+ const errors = [];
145
+ if (hostname) {
146
+ credential.server || (credential.server = hostname);
147
+ }
148
+ if (!credential.server) {
149
+ errors.push('server');
150
+ }
151
+ const auth = credential.authentication || (credential.authentication = { options: {} });
152
+ const authOptions = auth.options || (auth.options = {});
153
+ auth.type || (auth.type = 'default');
154
+ if (database || port) {
155
+ const options = credential.options || (credential.options = {});
156
+ options.database || (options.database = database);
157
+ if (port) {
158
+ options.port || (options.port = +port);
159
+ }
160
+ }
161
+ if (username || password) {
162
+ authOptions.userName || (authOptions.userName = username);
163
+ authOptions.password || (authOptions.password = password);
164
+ }
165
+ if (!authOptions.userName) {
166
+ errors.push('authentication.options.userName');
167
+ }
168
+ if (!authOptions.password) {
169
+ errors.push('authentication.options.password');
170
+ }
171
+ if (errors.length) {
172
+ throw (0, types_1.errorMessage)("mssql" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
173
+ }
174
+ const options = credential.options;
175
+ if (options) {
176
+ if (options.cryptoCredentialsDetails) {
177
+ this.readTLSConfig(options.cryptoCredentialsDetails);
178
+ }
179
+ if ('rowCollectionOnDone' in options) {
180
+ delete options.rowCollectionOnDone;
181
+ }
182
+ if ('rowCollectionOnRequestCompletion' in options) {
183
+ delete options.rowCollectionOnRequestCompletion;
184
+ }
185
+ if ('useColumnNames' in options) {
186
+ delete options.useColumnNames;
187
+ }
188
+ }
189
+ const usePool = item.usePool;
190
+ if (usePool) {
191
+ let pool;
192
+ password = undefined;
193
+ if (typeof usePool === 'string' && (username = authOptions.userName)) {
194
+ [password, pool] = pool_1.default.validateKey(POOL_STATE, username, usePool);
195
+ if (pool) {
196
+ pool.add(item, password);
197
+ return;
198
+ }
199
+ }
200
+ const poolKey = getPoolKey(credential);
201
+ if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
202
+ const Pool = require("tedious-connection-pool2" /* STRINGS.PACKAGE_POOL */);
203
+ const config = this.getPoolConfig("mssql" /* STRINGS.MODULE_NAME */, password);
204
+ const poolConfig = (0, types_1.isPlainObject)(item.usePool) ? item.usePool : {};
205
+ if (config) {
206
+ const { min, max, idle, queue_idle } = config;
207
+ if (min >= 0) {
208
+ poolConfig.min ?? (poolConfig.min = min);
209
+ }
210
+ if (max > 0) {
211
+ poolConfig.max ?? (poolConfig.max = max);
212
+ }
213
+ if (idle >= 0) {
214
+ poolConfig.idleTimeout ?? (poolConfig.idleTimeout = idle);
215
+ }
216
+ if (queue_idle >= 0) {
217
+ poolConfig.acquireTimeout ?? (poolConfig.acquireTimeout = queue_idle);
218
+ }
219
+ }
220
+ new MSSQLPool(new Pool(poolConfig, credential), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
221
+ }
222
+ else {
223
+ pool.add(item);
224
+ }
225
+ }
226
+ }
227
+ exports.setCredential = setCredential;
228
+ async function executeQuery(item, options) {
229
+ return (await executeBatchQuery.call(this, [item], options))[0] || [];
230
+ }
231
+ exports.executeQuery = executeQuery;
232
+ async function executeBatchQuery(batch, options = '', outResult) {
233
+ const length = batch.length;
234
+ if (length === 0) {
235
+ return [];
236
+ }
237
+ const db = require("tedious" /* STRINGS.PACKAGE_NAME */);
238
+ let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
239
+ if ((0, types_1.isPlainObject)(options)) {
240
+ ({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
241
+ }
242
+ else {
243
+ if (typeof options === 'string') {
244
+ sessionKey = options;
245
+ }
246
+ options = undefined;
247
+ }
248
+ if (length === 1) {
249
+ connectOnce = false;
250
+ parallel = false;
251
+ }
252
+ else if (parallel === undefined) {
253
+ parallel = !batch.some(item => item.parallel === false);
254
+ }
255
+ if (!parallel) {
256
+ outResult || (outResult = new Array(length));
257
+ }
258
+ const caching = this.hasCache("mssql" /* STRINGS.MODULE_NAME */, sessionKey);
259
+ const tasks = new Array(length);
260
+ const clients = [];
261
+ const pools = [];
262
+ let mssqlPool, mssqlClient, mssqlCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
263
+ const getConnection = async (item, credential) => {
264
+ item.transactionState = 64 /* TRANSACTION.AUTH */;
265
+ let client;
266
+ if (mssqlPool) {
267
+ pools.push(client = await mssqlPool.getConnection());
268
+ item.transactionState &= ~64 /* TRANSACTION.AUTH */;
269
+ return client;
270
+ }
271
+ const pool = item.usePool && pool_1.default.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
272
+ if (pool) {
273
+ try {
274
+ client = await pool.getConnection();
275
+ if (connectOnce) {
276
+ mssqlPool = pool;
277
+ }
278
+ pool.connected = true;
279
+ }
280
+ catch (err) {
281
+ let close;
282
+ switch (err instanceof Error && err.code) {
283
+ case 'ELOGIN':
284
+ case 'EDRIVER':
285
+ case 'EINSTLOOKUP':
286
+ close = 1;
287
+ break;
288
+ default:
289
+ close = pool.closeable;
290
+ break;
291
+ }
292
+ if (close) {
293
+ await pool.detach(true);
294
+ if (close === 1) {
295
+ throw err;
296
+ }
297
+ }
298
+ pool.connected = false;
299
+ }
300
+ }
301
+ if (!client) {
302
+ clients.push(client = await new Promise((resolve, reject) => {
303
+ const sql = new db.Connection(mssqlCredential || credential);
304
+ sql.connect(err => {
305
+ if (err) {
306
+ reject(err);
307
+ }
308
+ else {
309
+ resolve(sql);
310
+ }
311
+ });
312
+ }));
313
+ }
314
+ if (connectOnce) {
315
+ if (!parallel) {
316
+ mssqlClient = client;
317
+ }
318
+ mssqlCredential = credential;
319
+ }
320
+ item.transactionState &= ~64 /* TRANSACTION.AUTH */;
321
+ return client;
322
+ };
323
+ for (let i = 0; i < length; ++i) {
324
+ const item = batch[i];
325
+ const { source, query, ignoreCache } = item;
326
+ let credential = mssqlCredential || onceCredential, error;
327
+ 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 */))) {
328
+ if (this.handleFail(error, item, { errorQuery })) {
329
+ if (!parallel) {
330
+ tasks.length = 0;
331
+ break;
332
+ }
333
+ tasks[i] = Promise.reject(error);
334
+ }
335
+ else if (parallel) {
336
+ tasks[i] = Promise.resolve([]);
337
+ }
338
+ else {
339
+ outResult[i] = [];
340
+ }
341
+ continue;
342
+ }
343
+ item.transactionState = 1 /* TRANSACTION.ACTIVE */;
344
+ const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mssql" /* STRINGS.MODULE_NAME */, 'options', null, credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
345
+ const renewCache = ignoreCache === 0;
346
+ const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
347
+ let params = item.params, queryString = '';
348
+ if ((caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) && !streamRow) {
349
+ queryString = index_1.default.asString(query, true) + '_' + index_1.default.asString(params, true);
350
+ if (ignoreCache !== 1) {
351
+ const result = this.getQueryResult(source, credential, queryString, cacheValue);
352
+ if (result) {
353
+ if (parallel) {
354
+ tasks[i] = Promise.resolve(result);
355
+ }
356
+ else {
357
+ outResult[i] = result;
358
+ }
359
+ this.add(item, 4 /* TRANSACTION.COMMIT */ | 128 /* TRANSACTION.CACHE */);
360
+ continue;
361
+ }
362
+ if (!ignoreCache && outCacheMiss) {
363
+ outCacheMiss.push(source);
364
+ }
365
+ }
366
+ }
367
+ if (onceCredential && parallel) {
368
+ try {
369
+ mssqlClient = await getConnection(item, onceCredential);
370
+ }
371
+ catch {
372
+ connectOnce = false;
373
+ parallel = false;
374
+ }
375
+ onceCredential = undefined;
376
+ }
377
+ tasks[i] = new Promise(async (resolve, reject) => {
378
+ let commandType;
379
+ try {
380
+ const client = mssqlClient || await getConnection(item, credential);
381
+ if (mssqlClient && parallel) {
382
+ mssqlClient = undefined;
383
+ }
384
+ commandType = this.commandType.SELECT;
385
+ const processRow = typeof streamRow === 'function' && streamRow;
386
+ const rows = [];
387
+ let recordset = 1, resume, output;
388
+ const request = new db.Request(query, () => {
389
+ if (error && this.handleFail(error, item, { errorQuery, commandType })) {
390
+ reject(error);
391
+ }
392
+ else {
393
+ this.add(item, 4 /* TRANSACTION.COMMIT */);
394
+ if (output) {
395
+ output['__recordset__'] = 0;
396
+ output['__returnvalue__'] = true;
397
+ rows.push([output]);
398
+ }
399
+ resolve(!error ? this.setQueryResult(source, credential, queryString, rows, cacheValue) : rows);
400
+ }
401
+ });
402
+ if (params) {
403
+ let paramsOutput;
404
+ if (isOutputParameters(params)) {
405
+ ({ input: params, output: paramsOutput } = params);
406
+ }
407
+ convertParameters.call(request, db.TYPES, params, false);
408
+ if (paramsOutput) {
409
+ convertParameters.call(request, db.TYPES, paramsOutput, true);
410
+ }
411
+ }
412
+ request
413
+ .on('row', columns => {
414
+ resume = true;
415
+ const result = { '__recordset__': recordset };
416
+ for (let j = 0, q = columns.length; j < q; ++j) {
417
+ const data = columns[j];
418
+ const name = data.metadata.colName;
419
+ if (name === 'JSON_F52E2B61-18A1-11d1-B105-00805F49916B') {
420
+ try {
421
+ const items = JSON.parse(data.value);
422
+ if ((0, types_1.isArray)(items)) {
423
+ items.forEach((col) => col['__recordset__'] = recordset);
424
+ rows.push(...items);
425
+ }
426
+ }
427
+ catch {
428
+ }
429
+ return;
430
+ }
431
+ result[name] = data.value;
432
+ }
433
+ if (processRow) {
434
+ const err = processRow(result);
435
+ if (err === false) {
436
+ return;
437
+ }
438
+ if (err instanceof Error) {
439
+ error = err;
440
+ return;
441
+ }
442
+ }
443
+ rows.push(result);
444
+ })
445
+ .on('returnValue', (name, value) => (output || (output = {}))[name] = value)
446
+ .on('done', rowCount => {
447
+ if (resume && rowCount >= 0) {
448
+ ++recordset;
449
+ }
450
+ resume = false;
451
+ })
452
+ .on('doneProc', rowCount => {
453
+ if (resume && rowCount >= 0) {
454
+ ++recordset;
455
+ }
456
+ resume = false;
457
+ })
458
+ .on('doneInProc', rowCount => {
459
+ if (resume && rowCount >= 0) {
460
+ ++recordset;
461
+ }
462
+ resume = false;
463
+ })
464
+ .on('error', err => error || (error = err));
465
+ if (item.storedProc) {
466
+ client.callProcedure(request);
467
+ }
468
+ else {
469
+ client.execSql(request);
470
+ }
471
+ }
472
+ catch (err) {
473
+ if (this.handleFail(err, item, { errorQuery, commandType })) {
474
+ reject(err);
475
+ }
476
+ else {
477
+ resolve([]);
478
+ }
479
+ }
480
+ });
481
+ if (!parallel) {
482
+ try {
483
+ outResult[i] = await tasks[i];
484
+ }
485
+ catch {
486
+ tasks.length = 0;
487
+ break;
488
+ }
489
+ }
490
+ }
491
+ return this.processRows(batch, tasks, {
492
+ disconnect: () => {
493
+ clients.forEach(item => item.close());
494
+ pools.forEach(item => item.release());
495
+ },
496
+ parallel
497
+ }, outResult);
498
+ }
499
+ exports.executeBatchQuery = executeBatchQuery;
500
+ function checkTimeout(value, limit = 0) {
501
+ return pool_1.default.checkTimeout(POOL_STATE, value, limit);
502
+ }
503
+ exports.checkTimeout = checkTimeout;
504
+ exports.DB_SOURCE_CLIENT = true;
505
+ exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
@@ -0,0 +1,10 @@
1
+ import type { IDb } from '../../types/lib';
2
+ import type { MySQLCredential, MySQLDataSource } from '../../types/lib/db';
3
+
4
+ import type { IDbSourceClient } from '../types';
5
+
6
+ declare const MySQL: IDbSourceClient<MySQLDataSource> & {
7
+ setAuthentication: (this: IDb, credential: MySQLCredential) => void;
8
+ };
9
+
10
+ export = MySQL;