@pi-r/mysql 0.11.2 → 0.12.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 +33 -32
- package/client/pool.js +9 -7
- package/package.json +4 -4
- package/types/index.d.ts +2 -2
package/client/index.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
const mysql = require("mysql2/promise");
|
|
10
|
-
const Db = require("@e-mc/db");
|
|
11
|
-
const types_1 = require("@e-mc/types");
|
|
12
|
-
const util_1 = require("@e-mc/db/util");
|
|
13
|
-
const DbPool = require("@pi-r/mysql/client/pool");
|
|
2
|
+
|
|
3
|
+
const main = require('mysql2');
|
|
4
|
+
const mysql = require('mysql2/promise');
|
|
5
|
+
const Db = require('@e-mc/db');
|
|
6
|
+
const { DB_TYPE, asFunction, errorMessage, isArray, isError, isErrorCode, isPlainObject, isString } = require('@e-mc/types');
|
|
7
|
+
const { parseConnectionString, parseServerAuth } = require('@e-mc/db/util');
|
|
8
|
+
const DbPool = require('@pi-r/mysql/client/pool');
|
|
14
9
|
const POOL_STATE = {};
|
|
15
10
|
function isResultSetHeader(value) {
|
|
16
11
|
const result = Array.isArray(value) ? value[0] : value;
|
|
@@ -24,11 +19,11 @@ function isResultSetHeader(value) {
|
|
|
24
19
|
return false;
|
|
25
20
|
}
|
|
26
21
|
function setAuthentication(credential) {
|
|
27
|
-
const auth =
|
|
22
|
+
const auth = parseServerAuth(credential);
|
|
28
23
|
credential.host ||= auth.hostname;
|
|
29
24
|
credential.user ||= auth.username;
|
|
30
25
|
credential.port ||= 3306;
|
|
31
|
-
if (
|
|
26
|
+
if (isPlainObject(credential.ssl)) {
|
|
32
27
|
this.readTLSConfig(credential.ssl);
|
|
33
28
|
}
|
|
34
29
|
}
|
|
@@ -43,15 +38,16 @@ function setCredential(item) {
|
|
|
43
38
|
}
|
|
44
39
|
let host;
|
|
45
40
|
if (!credential.uri) {
|
|
41
|
+
host = credential.host;
|
|
46
42
|
const errors = [];
|
|
47
|
-
if (!
|
|
43
|
+
if (!host && !credential.socketPath) {
|
|
48
44
|
errors.push('host');
|
|
49
45
|
}
|
|
50
46
|
if (!credential.user) {
|
|
51
47
|
errors.push('user');
|
|
52
48
|
}
|
|
53
49
|
if (errors.length > 0) {
|
|
54
|
-
throw
|
|
50
|
+
throw errorMessage("mysql", "Invalid credentials", errors.join(', '));
|
|
55
51
|
}
|
|
56
52
|
}
|
|
57
53
|
else {
|
|
@@ -64,7 +60,7 @@ function setCredential(item) {
|
|
|
64
60
|
if (host?.endsWith('.rds.amazonaws.com')) {
|
|
65
61
|
try {
|
|
66
62
|
const ssl = credential.ssl ||= {};
|
|
67
|
-
if (
|
|
63
|
+
if (isPlainObject(ssl) && !ssl.ca) {
|
|
68
64
|
const bundle = require('aws-ssl-profiles');
|
|
69
65
|
ssl.ca = bundle.ca;
|
|
70
66
|
}
|
|
@@ -81,8 +77,8 @@ function setCredential(item) {
|
|
|
81
77
|
return;
|
|
82
78
|
}
|
|
83
79
|
let username, password, pool;
|
|
84
|
-
if (
|
|
85
|
-
username = credential.user || credential.uri &&
|
|
80
|
+
if (isString(usePool)) {
|
|
81
|
+
username = credential.user || credential.uri && parseConnectionString(credential.uri)?.username;
|
|
86
82
|
if (username) {
|
|
87
83
|
[password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
|
|
88
84
|
if (pool) {
|
|
@@ -133,7 +129,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
133
129
|
return [];
|
|
134
130
|
}
|
|
135
131
|
let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
|
|
136
|
-
if (
|
|
132
|
+
if (isPlainObject(options)) {
|
|
137
133
|
({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
|
|
138
134
|
}
|
|
139
135
|
else {
|
|
@@ -176,15 +172,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
176
172
|
}
|
|
177
173
|
catch (err) {
|
|
178
174
|
let close;
|
|
179
|
-
if (
|
|
180
|
-
close = 1;
|
|
181
|
-
}
|
|
182
|
-
else {
|
|
175
|
+
if (!isErrorCode(err, 'ER_ACCESS_DENIED_ERROR', 'ER_DBACCESS_DENIED_ERROR', 'ER_SPECIFIC_ACCESS_DENIED_ERROR', 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR')) {
|
|
183
176
|
close = pool.closeable;
|
|
184
177
|
}
|
|
185
|
-
if (close) {
|
|
178
|
+
if (close !== false) {
|
|
186
179
|
await pool.detach(true);
|
|
187
|
-
if (close
|
|
180
|
+
if (!close) {
|
|
188
181
|
throw err;
|
|
189
182
|
}
|
|
190
183
|
}
|
|
@@ -209,7 +202,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
209
202
|
for (let i = 0, mysql2Client, mysql2Credential, statement; i < length; ++i) {
|
|
210
203
|
const item = batch[i];
|
|
211
204
|
const { source, params, ignoreCache } = item;
|
|
212
|
-
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mysql", 'options', mysql2Credential || item.credential) &&
|
|
205
|
+
const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mysql", 'options', mysql2Credential || item.credential) && asFunction(item.streamRow) : item.streamRow;
|
|
213
206
|
let query = item.query, prepared = false;
|
|
214
207
|
if (typeof item.preparedStatement === 'boolean') {
|
|
215
208
|
if (item.preparedStatement) {
|
|
@@ -240,7 +233,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
240
233
|
else if (credential = mysqlCredential || onceCredential) {
|
|
241
234
|
error = null;
|
|
242
235
|
}
|
|
243
|
-
if (error !== null && !
|
|
236
|
+
if (error !== null && !isPlainObject(credential = item.credential) && (error = errorMessage(source, "Invalid credentials")) || !query && (error = errorMessage(source, "Missing database query"))) {
|
|
244
237
|
if (this.handleFail(error, item, { errorQuery })) {
|
|
245
238
|
if (!parallel) {
|
|
246
239
|
tasks.length = 0;
|
|
@@ -310,7 +303,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
310
303
|
if (err === false) {
|
|
311
304
|
return;
|
|
312
305
|
}
|
|
313
|
-
if (err
|
|
306
|
+
if (isError(err)) {
|
|
314
307
|
error = err;
|
|
315
308
|
return;
|
|
316
309
|
}
|
|
@@ -336,7 +329,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
336
329
|
commandType = this.commandType.SELECT;
|
|
337
330
|
const [rows] = await (prepared && connectOnce && !parallel ? client.execute(query, params) : client.query(query, params));
|
|
338
331
|
this.add(item, 4);
|
|
339
|
-
if (
|
|
332
|
+
if (isArray(rows)) {
|
|
340
333
|
const [row1, row2] = rows;
|
|
341
334
|
if (rows.length === 2 && Array.isArray(row1) && !Array.isArray(row2) && isResultSetHeader(row2)) {
|
|
342
335
|
resolve(row1);
|
|
@@ -388,5 +381,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
|
|
|
388
381
|
async function checkTimeout(value, limit = 0) {
|
|
389
382
|
return DbPool.checkTimeout(POOL_STATE, value, limit);
|
|
390
383
|
}
|
|
391
|
-
|
|
392
|
-
|
|
384
|
+
const DB_SOURCE_CLIENT = true;
|
|
385
|
+
const DB_SOURCE_TYPE = DB_TYPE.SQL;
|
|
386
|
+
|
|
387
|
+
exports.DB_SOURCE_CLIENT = DB_SOURCE_CLIENT;
|
|
388
|
+
exports.DB_SOURCE_TYPE = DB_SOURCE_TYPE;
|
|
389
|
+
exports.checkTimeout = checkTimeout;
|
|
390
|
+
exports.executeBatchQuery = executeBatchQuery;
|
|
391
|
+
exports.executeQuery = executeQuery;
|
|
392
|
+
exports.setAuthentication = setAuthentication;
|
|
393
|
+
exports.setCredential = setCredential;
|
package/client/pool.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
const
|
|
2
|
+
|
|
3
|
+
const { isArray, isObject, isString } = require('@e-mc/types');
|
|
4
|
+
const { checkEmpty } = require('@e-mc/db/util');
|
|
4
5
|
const DbPool = require('@e-mc/db/pool');
|
|
5
6
|
const POOL_ACTIVE = new WeakSet();
|
|
6
7
|
class MySQLPool extends DbPool {
|
|
7
|
-
static CACHE_UNUSED = ['connectTimeout', 'queueLimit', 'waitForConnections', 'connectionLimit', 'maxIdle', 'idleTimeout', 'enableKeepAlive', 'keepAliveInitialDelay', 'Promise', 'gracefulEnd'];
|
|
8
|
+
static CACHE_UNUSED = ['connectTimeout', 'queueLimit', 'resetOnRelease', 'waitForConnections', 'connectionLimit', 'maxIdle', 'idleTimeout', 'enableKeepAlive', 'keepAliveInitialDelay', 'Promise', 'gracefulEnd'];
|
|
8
9
|
static CACHE_IGNORE = ['authPlugins', 'authSwitchHandler', 'stream', 'infileStreamFactory'];
|
|
9
10
|
static asString(credential) {
|
|
10
11
|
if ((credential.host || credential.socketPath) && credential.user) {
|
|
@@ -21,18 +22,18 @@ class MySQLPool extends DbPool {
|
|
|
21
22
|
}
|
|
22
23
|
const ssl = credential.ssl;
|
|
23
24
|
let result = super.sanitize(credential);
|
|
24
|
-
if (
|
|
25
|
+
if (isObject(ssl)) {
|
|
25
26
|
if (result === credential) {
|
|
26
27
|
result = { ...credential };
|
|
27
28
|
}
|
|
28
29
|
const cert = ssl.cert;
|
|
29
|
-
if (
|
|
30
|
+
if (isString(cert)) {
|
|
30
31
|
result.ssl = cert;
|
|
31
32
|
}
|
|
32
33
|
else if (Buffer.isBuffer(cert)) {
|
|
33
34
|
result.ssl = cert.toString('utf8');
|
|
34
35
|
}
|
|
35
|
-
else if (
|
|
36
|
+
else if (isArray(cert)) {
|
|
36
37
|
result.ssl = cert.reduce((a, b) => a + (Buffer.isBuffer(b) ? b.toString('utf8') : b), '');
|
|
37
38
|
}
|
|
38
39
|
else {
|
|
@@ -52,10 +53,11 @@ class MySQLPool extends DbPool {
|
|
|
52
53
|
}
|
|
53
54
|
isEmpty() {
|
|
54
55
|
const pool = this.client.pool;
|
|
55
|
-
return this.closed ||
|
|
56
|
+
return this.closed || checkEmpty(pool._allConnections) && checkEmpty(pool._connectionQueue);
|
|
56
57
|
}
|
|
57
58
|
get closed() {
|
|
58
59
|
return !!this.client.pool._closed;
|
|
59
60
|
}
|
|
60
61
|
}
|
|
62
|
+
|
|
61
63
|
module.exports = MySQLPool;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pi-r/mysql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "MySQL client driver for E-mc.",
|
|
5
5
|
"main": "client/index.js",
|
|
6
6
|
"types": "client/index.d.ts",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"license": "MIT",
|
|
21
21
|
"homepage": "https://github.com/anpham6/pi-r#readme",
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@e-mc/db": "^0.
|
|
24
|
-
"@e-mc/types": "^0.
|
|
25
|
-
"mysql2": "^3.
|
|
23
|
+
"@e-mc/db": "^0.14.0",
|
|
24
|
+
"@e-mc/types": "^0.14.0",
|
|
25
|
+
"mysql2": "^3.22.3"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -3,9 +3,9 @@ import type { DbDataSource } from '@e-mc/types/lib/squared';
|
|
|
3
3
|
import type { IdentifierAction } from '@e-mc/types/lib/core';
|
|
4
4
|
import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
|
|
5
5
|
|
|
6
|
-
import type { PoolOptions, QueryOptions } from 'mysql2/promise';
|
|
6
|
+
import type { ExecuteValues, PoolOptions, QueryOptions, QueryValues } from 'mysql2/promise';
|
|
7
7
|
|
|
8
|
-
export interface MySQLDataSource<T =
|
|
8
|
+
export interface MySQLDataSource<T = ExecuteValues | QueryValues> extends DbDataSource<string | QueryOptions, unknown, unknown, MySQLCredential, string>, ExecuteAction<T> {
|
|
9
9
|
source: "mysql";
|
|
10
10
|
preparedStatement?: boolean;
|
|
11
11
|
}
|