@pi-r/mariadb 0.7.2 → 0.8.0
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/client/index.js +45 -84
- package/client/pool.d.ts +9 -0
- package/client/pool.js +56 -0
- package/package.json +3 -3
- package/types/index.d.ts +3 -5
package/client/index.js
CHANGED
|
@@ -1,65 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT =
|
|
2
|
+
exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = void 0;
|
|
3
|
+
exports.setAuthentication = setAuthentication;
|
|
4
|
+
exports.setCredential = setCredential;
|
|
5
|
+
exports.executeQuery = executeQuery;
|
|
6
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
7
|
+
exports.checkTimeout = checkTimeout;
|
|
3
8
|
const mariadb = require("mariadb");
|
|
9
|
+
const Db = require("@e-mc/db");
|
|
4
10
|
const util_1 = require("@e-mc/db/util");
|
|
5
11
|
const types_1 = require("@e-mc/types");
|
|
6
|
-
const
|
|
7
|
-
const DbPool = require('@e-mc/db/pool');
|
|
8
|
-
class MariaDBPool extends DbPool {
|
|
9
|
-
async getConnection() {
|
|
10
|
-
return this.client.getConnection();
|
|
11
|
-
}
|
|
12
|
-
async close() {
|
|
13
|
-
return this.client.end();
|
|
14
|
-
}
|
|
15
|
-
isEmpty() {
|
|
16
|
-
const client = this.client;
|
|
17
|
-
return this.closed || client.activeConnections() === 0 && client.taskQueueSize() === 0;
|
|
18
|
-
}
|
|
19
|
-
get closed() {
|
|
20
|
-
return this.client.closed;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
12
|
+
const DbPool = require("@pi-r/mariadb/client/pool");
|
|
23
13
|
const POOL_STATE = {};
|
|
24
|
-
const POOL_UNUSED = ['ssl', 'connectTimeout', 'socketTimeout', 'compress', 'keepAliveDelay', 'acquireTimeout', 'connectionLimit', 'idleTimeout', 'initializationTimeout', 'minDelayValidation', 'minimumIdle', 'resetAfterUse', 'noControlAfterUse', 'leakDetectionTimeout'];
|
|
25
|
-
const POOL_ACTIVE = new WeakSet();
|
|
26
|
-
function removePoolProperties(credential) {
|
|
27
|
-
if (!credential.uuidKey && POOL_ACTIVE.has(credential)) {
|
|
28
|
-
let result;
|
|
29
|
-
for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
|
|
30
|
-
const attr = POOL_UNUSED[i];
|
|
31
|
-
if (attr in credential) {
|
|
32
|
-
result ||= { ...credential };
|
|
33
|
-
if (attr !== 'ssl') {
|
|
34
|
-
delete result[attr];
|
|
35
|
-
}
|
|
36
|
-
else if ((0, types_1.isObject)(result.ssl)) {
|
|
37
|
-
result[attr] = true;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
if (result) {
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return credential;
|
|
46
|
-
}
|
|
47
|
-
function removeUUIDKey(credential) {
|
|
48
|
-
if ('uuidKey' in credential) {
|
|
49
|
-
credential = { ...credential };
|
|
50
|
-
delete credential.uuidKey;
|
|
51
|
-
}
|
|
52
|
-
return credential;
|
|
53
|
-
}
|
|
54
14
|
function isOkPacket(value) {
|
|
55
15
|
const result = Array.isArray(value) ? value[0] : value;
|
|
56
16
|
return !!result && !Array.isArray(result) && result.constructor.name === 'OkPacket';
|
|
57
17
|
}
|
|
58
|
-
function getPoolKey(credential) {
|
|
59
|
-
if ((credential.host || credential.socketPath) && credential.user) {
|
|
60
|
-
return JSON.stringify(removeUUIDKey(credential));
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
18
|
function setAuthentication(credential) {
|
|
64
19
|
const auth = (0, util_1.parseServerAuth)(credential);
|
|
65
20
|
credential.host ||= auth.hostname;
|
|
@@ -68,17 +23,15 @@ function setAuthentication(credential) {
|
|
|
68
23
|
this.readTLSConfig(credential.ssl);
|
|
69
24
|
}
|
|
70
25
|
}
|
|
71
|
-
exports.setAuthentication = setAuthentication;
|
|
72
26
|
function setCredential(item) {
|
|
73
27
|
let credential = this.getCredential(item);
|
|
74
28
|
if (credential) {
|
|
75
29
|
setAuthentication.call(this, credential);
|
|
76
30
|
}
|
|
77
31
|
else {
|
|
78
|
-
const uri = item.uri;
|
|
79
32
|
credential = {};
|
|
80
|
-
if (uri) {
|
|
81
|
-
const connection = (0, util_1.parseConnectionString)(uri);
|
|
33
|
+
if (item.uri) {
|
|
34
|
+
const connection = (0, util_1.parseConnectionString)(item.uri);
|
|
82
35
|
if (connection) {
|
|
83
36
|
const { username, password, hostname, port, database } = connection;
|
|
84
37
|
credential.host = hostname;
|
|
@@ -99,9 +52,20 @@ function setCredential(item) {
|
|
|
99
52
|
if (!credential.user) {
|
|
100
53
|
errors.push('user');
|
|
101
54
|
}
|
|
102
|
-
if (errors.length) {
|
|
55
|
+
if (errors.length > 0) {
|
|
103
56
|
throw (0, types_1.errorMessage)("mariadb", 'Not defined - ' + errors.join(' | '));
|
|
104
57
|
}
|
|
58
|
+
if (credential.host?.endsWith('.rds.amazonaws.com')) {
|
|
59
|
+
try {
|
|
60
|
+
const ssl = credential.ssl ||= {};
|
|
61
|
+
if ((0, types_1.isPlainObject)(ssl) && !ssl.ca) {
|
|
62
|
+
const bundle = require('aws-ssl-profiles');
|
|
63
|
+
ssl.ca = bundle.ca;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
}
|
|
68
|
+
}
|
|
105
69
|
const usePool = item.usePool;
|
|
106
70
|
if (!usePool) {
|
|
107
71
|
return;
|
|
@@ -142,18 +106,19 @@ function setCredential(item) {
|
|
|
142
106
|
credential.socketTimeout ??= socket_timeout;
|
|
143
107
|
}
|
|
144
108
|
}
|
|
145
|
-
const poolKey =
|
|
109
|
+
const poolKey = DbPool.asString(credential);
|
|
146
110
|
if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
|
|
147
111
|
pool.add(item);
|
|
148
112
|
return;
|
|
149
113
|
}
|
|
150
|
-
|
|
114
|
+
const client = mariadb.createPool(DbPool.removeUUIDKey(credential));
|
|
115
|
+
new DbPool(client, poolKey, username && password ? { username, password } : undefined)
|
|
116
|
+
.add(item)
|
|
117
|
+
.parent = POOL_STATE;
|
|
151
118
|
}
|
|
152
|
-
exports.setCredential = setCredential;
|
|
153
119
|
async function executeQuery(item, options) {
|
|
154
120
|
return (await executeBatchQuery.call(this, [item], options))[0] || [];
|
|
155
121
|
}
|
|
156
|
-
exports.executeQuery = executeQuery;
|
|
157
122
|
async function executeBatchQuery(batch, options = '', outResult) {
|
|
158
123
|
const length = batch.length;
|
|
159
124
|
if (length === 0) {
|
|
@@ -188,20 +153,18 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
188
153
|
item.transactionState = 64;
|
|
189
154
|
let client;
|
|
190
155
|
if (mariaDBPool) {
|
|
191
|
-
pools.push(client = await mariaDBPool.getConnection());
|
|
156
|
+
pools.push(client = await mariaDBPool.getConnection(credential));
|
|
192
157
|
item.transactionState &= ~64;
|
|
193
|
-
POOL_ACTIVE.add(credential);
|
|
194
158
|
return client;
|
|
195
159
|
}
|
|
196
|
-
const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool,
|
|
160
|
+
const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, DbPool.asString(credential), ...connectOnce ? [item, batch[0]] : [item]);
|
|
197
161
|
if (pool) {
|
|
198
162
|
try {
|
|
199
|
-
pools.push(client = await pool.getConnection());
|
|
163
|
+
pools.push(client = await pool.getConnection(credential));
|
|
200
164
|
if (connectOnce) {
|
|
201
165
|
mariaDBPool = pool;
|
|
202
166
|
}
|
|
203
167
|
pool.connected = true;
|
|
204
|
-
POOL_ACTIVE.add(credential);
|
|
205
168
|
}
|
|
206
169
|
catch (err) {
|
|
207
170
|
let close;
|
|
@@ -226,7 +189,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
226
189
|
}
|
|
227
190
|
}
|
|
228
191
|
if (!client) {
|
|
229
|
-
clients.push(client = await mariadb.createConnection(removeUUIDKey(mariaDBCredential || credential)));
|
|
192
|
+
clients.push(client = await mariadb.createConnection(DbPool.removeUUIDKey(mariaDBCredential || credential)));
|
|
230
193
|
}
|
|
231
194
|
if (connectOnce) {
|
|
232
195
|
if (!parallel) {
|
|
@@ -267,7 +230,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
267
230
|
if (caching && ignoreCache !== true && !streamRow) {
|
|
268
231
|
queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
|
|
269
232
|
if (ignoreCache !== 1) {
|
|
270
|
-
const result = this.getQueryResult(source,
|
|
233
|
+
const result = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue);
|
|
271
234
|
if (result) {
|
|
272
235
|
if (parallel) {
|
|
273
236
|
tasks[i] = Promise.resolve(result);
|
|
@@ -296,14 +259,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
296
259
|
tasks[i] = new Promise(async (resolve, reject) => {
|
|
297
260
|
let commandType;
|
|
298
261
|
try {
|
|
299
|
-
const client = mariaDBClient || await getConnection(item, removeUUIDKey(credential));
|
|
262
|
+
const client = mariaDBClient || await getConnection(item, DbPool.removeUUIDKey(credential));
|
|
300
263
|
if (mariaDBClient && parallel) {
|
|
301
264
|
mariaDBClient = undefined;
|
|
302
265
|
}
|
|
303
266
|
commandType = this.commandType.SELECT;
|
|
304
267
|
if (streamRow) {
|
|
305
268
|
const rows = [];
|
|
306
|
-
const processRow = typeof streamRow === 'function'
|
|
269
|
+
const processRow = typeof streamRow === 'function' ? streamRow : null;
|
|
307
270
|
try {
|
|
308
271
|
for await (const row of client.queryStream(query, params)) {
|
|
309
272
|
if (processRow) {
|
|
@@ -327,7 +290,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
327
290
|
}
|
|
328
291
|
else {
|
|
329
292
|
this.add(item, 4);
|
|
330
|
-
resolve(!error ? this.setQueryResult(source,
|
|
293
|
+
resolve(!error ? this.setQueryResult(source, DbPool.sanitize(credential), queryString, rows, cacheValue) : rows.length > 0 ? rows : null);
|
|
331
294
|
}
|
|
332
295
|
}
|
|
333
296
|
else {
|
|
@@ -339,7 +302,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
339
302
|
resolve(row1);
|
|
340
303
|
}
|
|
341
304
|
else {
|
|
342
|
-
resolve(!isOkPacket(row1) ? this.setQueryResult(source,
|
|
305
|
+
resolve(!isOkPacket(row1) ? this.setQueryResult(source, DbPool.sanitize(credential), queryString, rows, cacheValue) : null);
|
|
343
306
|
}
|
|
344
307
|
}
|
|
345
308
|
else {
|
|
@@ -367,21 +330,19 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
367
330
|
}
|
|
368
331
|
}
|
|
369
332
|
return this.processRows(batch, tasks, {
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
333
|
+
parallel,
|
|
334
|
+
disconnect() {
|
|
335
|
+
for (const item of clients) {
|
|
336
|
+
void item.end();
|
|
337
|
+
}
|
|
338
|
+
for (const item of pools) {
|
|
339
|
+
void item.release();
|
|
340
|
+
}
|
|
341
|
+
}
|
|
379
342
|
}, outResult);
|
|
380
343
|
}
|
|
381
|
-
exports.executeBatchQuery = executeBatchQuery;
|
|
382
344
|
async function checkTimeout(value, limit = 0) {
|
|
383
345
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
384
346
|
}
|
|
385
|
-
exports.checkTimeout = checkTimeout;
|
|
386
347
|
exports.DB_SOURCE_CLIENT = true;
|
|
387
348
|
exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
|
package/client/pool.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { DbPoolConstructor } from '@e-mc/types/lib/db';
|
|
2
|
+
|
|
3
|
+
import type { DbPoolCredential, MariaDBDataSource } from '../types';
|
|
4
|
+
|
|
5
|
+
import type { Pool, PoolConnection } from 'mariadb';
|
|
6
|
+
|
|
7
|
+
declare const MariaDBPool: DbPoolConstructor<MariaDBDataSource, Pool, PoolConnection, DbPoolCredential>;
|
|
8
|
+
|
|
9
|
+
export = MariaDBPool;
|
package/client/pool.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const types_1 = require("@e-mc/types");
|
|
3
|
+
const DbPool = require('@e-mc/db/pool');
|
|
4
|
+
const POOL_ACTIVE = new WeakSet();
|
|
5
|
+
class MariaDBPool extends DbPool {
|
|
6
|
+
static CACHE_UNUSED = ['connectTimeout', 'socketTimeout', 'compress', 'keepAliveDelay', 'acquireTimeout', 'connectionLimit', 'idleTimeout', 'initializationTimeout', 'minDelayValidation', 'minimumIdle', 'resetAfterUse', 'noControlAfterUse', 'leakDetectionTimeout'];
|
|
7
|
+
static asString(credential) {
|
|
8
|
+
if ((credential.host || credential.socketPath) && credential.user) {
|
|
9
|
+
return JSON.stringify(this.removeUUIDKey(credential));
|
|
10
|
+
}
|
|
11
|
+
return super.asString(credential);
|
|
12
|
+
}
|
|
13
|
+
static sanitize(credential) {
|
|
14
|
+
if (!POOL_ACTIVE.has(credential) || credential.uuidKey) {
|
|
15
|
+
return credential;
|
|
16
|
+
}
|
|
17
|
+
const ssl = credential.ssl;
|
|
18
|
+
let result = super.sanitize(credential);
|
|
19
|
+
if ((0, types_1.isObject)(ssl)) {
|
|
20
|
+
if (result === credential) {
|
|
21
|
+
result = { ...credential };
|
|
22
|
+
}
|
|
23
|
+
const cert = ssl.cert;
|
|
24
|
+
if ((0, types_1.isString)(cert)) {
|
|
25
|
+
result.ssl = { cert };
|
|
26
|
+
}
|
|
27
|
+
else if (Buffer.isBuffer(cert)) {
|
|
28
|
+
result.ssl = { cert: cert.toString('utf-8') };
|
|
29
|
+
}
|
|
30
|
+
else if ((0, types_1.isArray)(cert)) {
|
|
31
|
+
result.ssl = { cert: cert.reduce((a, b) => a + (Buffer.isBuffer(b) ? b.toString('utf-8') : b), '') };
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
result.ssl = { cert: "Unknown" };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
async getConnection(credential) {
|
|
40
|
+
if (credential) {
|
|
41
|
+
POOL_ACTIVE.add(credential);
|
|
42
|
+
}
|
|
43
|
+
return this.client.getConnection();
|
|
44
|
+
}
|
|
45
|
+
async close() {
|
|
46
|
+
return this.client.end();
|
|
47
|
+
}
|
|
48
|
+
isEmpty() {
|
|
49
|
+
const client = this.client;
|
|
50
|
+
return this.closed || client.activeConnections() === 0 && client.taskQueueSize() === 0;
|
|
51
|
+
}
|
|
52
|
+
get closed() {
|
|
53
|
+
return this.client.closed;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
module.exports = MariaDBPool;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/mariadb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "MariaDB client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"license": "MIT",
|
|
22
22
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@e-mc/db": "^0.
|
|
25
|
-
"@e-mc/types": "^0.
|
|
24
|
+
"@e-mc/db": "^0.10.0",
|
|
25
|
+
"@e-mc/types": "^0.10.0",
|
|
26
26
|
"mariadb": "^3.3.1"
|
|
27
27
|
}
|
|
28
28
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
2
2
|
|
|
3
|
+
import type { IdentifierAction } from '@e-mc/types/lib/core';
|
|
3
4
|
import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
4
5
|
|
|
5
|
-
import type { SecureContextOptions } from 'tls';
|
|
6
|
-
|
|
7
6
|
import type { PoolConfig, QueryConfig } from 'mariadb';
|
|
8
7
|
|
|
9
8
|
export interface MariaDBDataSource<T = unknown> extends DbDataSource<string | QueryConfig, unknown, unknown, MariaDBCredential, string>, ExecuteAction<T> {
|
|
10
9
|
source: "mariadb";
|
|
11
10
|
}
|
|
12
11
|
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
}
|
|
12
|
+
export type MariaDBCredential = PoolConfig & ServerAuth;
|
|
13
|
+
export type DbPoolCredential = PoolConfig & IdentifierAction;
|