@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/mysql/index.js DELETED
@@ -1,332 +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 = exports.setAuthentication = 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 MySQLPool extends pool_1.default {
9
- getConnection() {
10
- return this.client.getConnection();
11
- }
12
- close() {
13
- return this.client.end();
14
- }
15
- isEmpty() {
16
- const pool = this.client.pool;
17
- return this.closed || (0, util_1.checkEmpty)(pool._allConnections) && (0, util_1.checkEmpty)(pool._connectionQueue);
18
- }
19
- get closed() {
20
- return !!this.client.pool._closed;
21
- }
22
- }
23
- const POOL_STATE = {};
24
- function removePoolProperties(credential) {
25
- if (!credential.uuidKey && ('ssl' in credential || 'connectionLimit' in credential || 'queueLimit' in credential || 'waitForConnections' in credential || 'enableKeepAlive' in credential || 'keepAliveInitialDelay' in credential || 'connectTimeout' in credential)) {
26
- return { ...credential, ssl: (0, types_1.isObject)(credential.ssl) ? true : undefined, connectionLimit: undefined, queueLimit: undefined, waitForConnections: undefined, enableKeepAlive: undefined, keepAliveInitialDelay: undefined, connectTimeout: undefined };
27
- }
28
- return credential;
29
- }
30
- function removeUUIDKey(credential) {
31
- if ('uuidKey' in credential) {
32
- credential = { ...credential };
33
- delete credential.uuidKey;
34
- return credential;
35
- }
36
- return credential;
37
- }
38
- const getPoolKey = (credential) => (credential.host || '') + '_' + (credential.port || '3306') + (credential.user || '') + '_' + (credential.password || '') + '_' + (credential.database || '') + '_' + (credential.uri || '') + '_' + (credential.rowsAsArray ? '1' : '0');
39
- function setAuthentication(credential) {
40
- const auth = (0, util_1.parseServerAuth)(credential);
41
- credential.host || (credential.host = auth.hostname);
42
- credential.user || (credential.user = auth.username);
43
- if ((0, types_1.isPlainObject)(credential.ssl)) {
44
- this.readTLSConfig(credential.ssl);
45
- }
46
- }
47
- exports.setAuthentication = setAuthentication;
48
- function setCredential(item) {
49
- let credential = this.getCredential(item);
50
- if (credential) {
51
- setAuthentication.call(this, credential);
52
- }
53
- else {
54
- credential = { uri: item.uri };
55
- item.credential = credential;
56
- }
57
- if (!credential.uri) {
58
- const errors = [];
59
- if (!credential.host) {
60
- errors.push('host');
61
- }
62
- if (!credential.user) {
63
- errors.push('user');
64
- }
65
- if (errors.length) {
66
- throw (0, types_1.errorMessage)("mysql" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
67
- }
68
- }
69
- const usePool = item.usePool;
70
- if (usePool) {
71
- let username, password, pool;
72
- if (typeof usePool === 'string' && (username = credential.user || credential.uri && (0, util_1.parseConnectionString)(credential.uri)?.username)) {
73
- [password, pool] = pool_1.default.validateKey(POOL_STATE, username, usePool);
74
- if (pool) {
75
- pool.add(item, password);
76
- return;
77
- }
78
- }
79
- const poolKey = getPoolKey(credential);
80
- if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
81
- const { createPool } = require("mysql2/promise" /* STRINGS.PACKAGE_NAME */);
82
- const config = this.getPoolConfig("mysql" /* STRINGS.MODULE_NAME */, password);
83
- if (config) {
84
- const { max, queue_max } = config;
85
- if (max > 0) {
86
- credential.connectionLimit ?? (credential.connectionLimit = max);
87
- }
88
- if (queue_max >= 0) {
89
- credential.queueLimit ?? (credential.queueLimit = queue_max);
90
- }
91
- }
92
- new MySQLPool(createPool(removeUUIDKey(credential)), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
93
- }
94
- else {
95
- pool.add(item);
96
- }
97
- }
98
- }
99
- exports.setCredential = setCredential;
100
- async function executeQuery(item, options) {
101
- return (await executeBatchQuery.call(this, [item], options))[0] || [];
102
- }
103
- exports.executeQuery = executeQuery;
104
- async function executeBatchQuery(batch, options = '', outResult) {
105
- const length = batch.length;
106
- if (length === 0) {
107
- return [];
108
- }
109
- const db = require("mysql2/promise" /* STRINGS.PACKAGE_NAME */);
110
- let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
111
- if ((0, types_1.isPlainObject)(options)) {
112
- ({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
113
- }
114
- else {
115
- if (typeof options === 'string') {
116
- sessionKey = options;
117
- }
118
- options = undefined;
119
- }
120
- if (length === 1) {
121
- connectOnce = false;
122
- parallel = false;
123
- }
124
- else if (parallel === undefined) {
125
- parallel = !batch.some(item => item.parallel === false);
126
- }
127
- if (!parallel) {
128
- outResult || (outResult = new Array(length));
129
- }
130
- const caching = this.hasCache("mysql" /* STRINGS.MODULE_NAME */, sessionKey);
131
- const tasks = new Array(length);
132
- const clients = [];
133
- const pools = [];
134
- let mysqlPool, mysqlClient, mysqlCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
135
- const getConnection = async (item, credential) => {
136
- item.transactionState = 64 /* TRANSACTION_DB.AUTH */;
137
- let client;
138
- if (mysqlPool) {
139
- pools.push(client = await mysqlPool.getConnection());
140
- item.transactionState &= ~64 /* TRANSACTION_DB.AUTH */;
141
- return client;
142
- }
143
- const pool = item.usePool && pool_1.default.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
144
- if (pool) {
145
- try {
146
- pools.push(client = await pool.getConnection());
147
- if (connectOnce) {
148
- mysqlPool = pool;
149
- }
150
- pool.connected = true;
151
- }
152
- catch (err) {
153
- let close;
154
- switch (err instanceof Error && err.code) {
155
- case 'ER_ACCESS_DENIED_ERROR':
156
- close = 1;
157
- break;
158
- default:
159
- close = pool.closeable;
160
- break;
161
- }
162
- if (close) {
163
- await pool.detach(true);
164
- if (close === 1) {
165
- throw err;
166
- }
167
- }
168
- pool.connected = false;
169
- }
170
- }
171
- if (!client) {
172
- clients.push(client = await db.createConnection(removeUUIDKey(mysqlCredential || credential)));
173
- }
174
- if (connectOnce) {
175
- if (!parallel) {
176
- mysqlClient = client;
177
- }
178
- mysqlCredential = credential;
179
- }
180
- item.transactionState &= ~64 /* TRANSACTION_DB.AUTH */;
181
- return client;
182
- };
183
- for (let i = 0, mysql2Client, mysql2Credential; i < length; ++i) {
184
- const item = batch[i];
185
- const { source, query, params, ignoreCache } = item;
186
- const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mysql" /* STRINGS.MODULE_NAME */, 'options', null, mysql2Credential || item.credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
187
- let credential, error;
188
- if (streamRow) {
189
- if (mysql2Credential) {
190
- credential = mysql2Credential;
191
- error = null;
192
- }
193
- }
194
- else if (credential = mysqlCredential || onceCredential) {
195
- error = null;
196
- }
197
- if (error !== null && !(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 */))) {
198
- if (this.handleFail(error, item, { errorQuery })) {
199
- if (!parallel) {
200
- tasks.length = 0;
201
- break;
202
- }
203
- tasks[i] = Promise.reject(error);
204
- }
205
- else if (parallel) {
206
- tasks[i] = Promise.resolve([]);
207
- }
208
- else {
209
- outResult[i] = [];
210
- }
211
- continue;
212
- }
213
- item.transactionState = 1 /* TRANSACTION_DB.ACTIVE */;
214
- const renewCache = ignoreCache === 0;
215
- const cacheValue = renewCache ? { sessionKey, renewCache } : sessionKey;
216
- let queryString = '';
217
- if ((caching && ignoreCache !== true || ignoreCache === false || ignoreCache === 1 || renewCache) && !streamRow) {
218
- queryString = index_1.default.asString(query, true) + '_' + index_1.default.asString(params, true);
219
- const result = this.getQueryResult(source, removePoolProperties(credential), queryString, cacheValue);
220
- if (ignoreCache !== 1) {
221
- if (result) {
222
- if (parallel) {
223
- tasks[i] = Promise.resolve(result);
224
- }
225
- else {
226
- outResult[i] = result;
227
- }
228
- this.add(item, 4 /* TRANSACTION_DB.COMMIT */ | 128 /* TRANSACTION_DB.CACHE */);
229
- continue;
230
- }
231
- if (!ignoreCache && outCacheMiss) {
232
- outCacheMiss.push(source);
233
- }
234
- }
235
- }
236
- if (onceCredential && parallel && !streamRow) {
237
- try {
238
- mysqlClient = await getConnection(item, onceCredential);
239
- }
240
- catch {
241
- connectOnce = false;
242
- parallel = false;
243
- }
244
- onceCredential = undefined;
245
- }
246
- tasks[i] = new Promise(async (resolve, reject) => {
247
- let commandType;
248
- try {
249
- if (streamRow) {
250
- const rows = [];
251
- const processRow = typeof streamRow === 'function' && streamRow;
252
- const { createConnection } = require("mysql2" /* STRINGS.PACKAGE_STREAM */);
253
- const client = mysql2Client || createConnection((mysql2Credential || credential));
254
- clients.push(client);
255
- if (connectOnce) {
256
- if (!parallel) {
257
- mysql2Client = client;
258
- }
259
- mysql2Credential = credential;
260
- }
261
- commandType = this.commandType.SELECT;
262
- client
263
- .query(query, params)
264
- .on('error', err => error || (error = err))
265
- .on('result', row => {
266
- if (processRow) {
267
- const err = processRow(row);
268
- if (err === false) {
269
- return;
270
- }
271
- if (err instanceof Error) {
272
- error = err;
273
- return;
274
- }
275
- }
276
- rows.push(row);
277
- })
278
- .on('end', () => {
279
- if (error && this.handleFail(error, item, { errorQuery, commandType })) {
280
- reject(error);
281
- }
282
- else {
283
- this.add(item, 4 /* TRANSACTION_DB.COMMIT */);
284
- resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows);
285
- }
286
- });
287
- }
288
- else {
289
- const client = mysqlClient || await getConnection(item, credential);
290
- if (mysqlClient && parallel) {
291
- mysqlClient = undefined;
292
- }
293
- commandType = this.commandType.SELECT;
294
- const [rows] = await client.query(query, params);
295
- this.add(item, 4 /* TRANSACTION_DB.COMMIT */);
296
- resolve(Array.isArray(rows) && !rows.some(row => row.constructor.name === 'OkPacket') ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : null);
297
- }
298
- }
299
- catch (err) {
300
- if (this.handleFail(err, item, { errorQuery, commandType })) {
301
- reject(err);
302
- }
303
- else {
304
- resolve([]);
305
- }
306
- }
307
- });
308
- if (!parallel) {
309
- try {
310
- outResult[i] = await tasks[i];
311
- }
312
- catch {
313
- tasks.length = 0;
314
- break;
315
- }
316
- }
317
- }
318
- return this.processRows(batch, tasks, {
319
- disconnect: () => {
320
- clients.forEach(item => item.end(err => err && this.writeFail(["Unable to close connnection" /* ERR_DB.CONNECTION_CLOSE */, "mysql" /* STRINGS.MODULE_NAME */], err, { type: 65536 /* LOG_TYPE.DB */, fatal: false })));
321
- pools.forEach(item => item.release());
322
- },
323
- parallel
324
- }, outResult);
325
- }
326
- exports.executeBatchQuery = executeBatchQuery;
327
- function checkTimeout(value, limit = 0) {
328
- return pool_1.default.checkTimeout(POOL_STATE, value, limit);
329
- }
330
- exports.checkTimeout = checkTimeout;
331
- exports.DB_SOURCE_CLIENT = true;
332
- exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
package/oracle/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import type { OracleDataSource } from '../../types/lib/db';
2
-
3
- import type { IDbSourceClient } from '../types';
4
-
5
- declare const Oracle: IDbSourceClient<OracleDataSource>;
6
-
7
- export = Oracle;