@pi-r/mariadb 0.11.3 → 0.12.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/client/index.js CHANGED
@@ -1,26 +1,21 @@
1
1
  "use strict";
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;
8
- const mariadb = require("mariadb");
9
- const Db = require("@e-mc/db");
10
- const types_1 = require("@e-mc/types");
11
- const util_1 = require("@e-mc/db/util");
12
- const DbPool = require("@pi-r/mariadb/client/pool");
2
+
3
+ const mariadb = require('mariadb');
4
+ const Db = require('@e-mc/db');
5
+ const { DB_TYPE, asFunction, errorMessage, isArray, isError, isErrorCode, isPlainObject, isString } = require('@e-mc/types');
6
+ const { parseConnectionString, parseServerAuth } = require('@e-mc/db/util');
7
+ const DbPool = require('@pi-r/mariadb/client/pool');
13
8
  const POOL_STATE = {};
14
9
  function isOkPacket(value) {
15
10
  const result = Array.isArray(value) ? value[0] : value;
16
11
  return !!result && !Array.isArray(result) && result.constructor.name === 'OkPacket';
17
12
  }
18
13
  function setAuthentication(credential) {
19
- const auth = (0, util_1.parseServerAuth)(credential);
14
+ const auth = parseServerAuth(credential);
20
15
  credential.host ||= auth.hostname;
21
16
  credential.user ||= auth.username;
22
17
  credential.port ||= 3306;
23
- if ((0, types_1.isPlainObject)(credential.ssl)) {
18
+ if (isPlainObject(credential.ssl)) {
24
19
  this.readTLSConfig(credential.ssl);
25
20
  }
26
21
  }
@@ -32,7 +27,7 @@ function setCredential(item) {
32
27
  else {
33
28
  credential = {};
34
29
  if (item.uri) {
35
- const connection = (0, util_1.parseConnectionString)(item.uri);
30
+ const connection = parseConnectionString(item.uri);
36
31
  if (connection) {
37
32
  const { username, password, hostname, port, database } = connection;
38
33
  credential.host = hostname;
@@ -54,12 +49,12 @@ function setCredential(item) {
54
49
  errors.push('user');
55
50
  }
56
51
  if (errors.length > 0) {
57
- throw (0, types_1.errorMessage)("mariadb", "Invalid credentials", errors.join(', '));
52
+ throw errorMessage("mariadb", "Invalid credentials", errors.join(', '));
58
53
  }
59
54
  if (credential.host?.endsWith('.rds.amazonaws.com')) {
60
55
  try {
61
56
  const ssl = credential.ssl ||= {};
62
- if ((0, types_1.isPlainObject)(ssl) && !ssl.ca) {
57
+ if (isPlainObject(ssl) && !ssl.ca) {
63
58
  const bundle = require('aws-ssl-profiles');
64
59
  ssl.ca = bundle.ca;
65
60
  }
@@ -76,7 +71,7 @@ function setCredential(item) {
76
71
  return;
77
72
  }
78
73
  let username, password, pool;
79
- if ((0, types_1.isString)(usePool)) {
74
+ if (isString(usePool)) {
80
75
  username = credential.user;
81
76
  if (username) {
82
77
  [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
@@ -134,7 +129,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
134
129
  return [];
135
130
  }
136
131
  let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
137
- if ((0, types_1.isPlainObject)(options)) {
132
+ if (isPlainObject(options)) {
138
133
  ({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
139
134
  }
140
135
  else {
@@ -154,6 +149,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
154
149
  outResult ||= new Array(length);
155
150
  }
156
151
  const caching = this.hasCache("mariadb", sessionKey);
152
+ const sortKeys = this.settingsOf("mariadb", 'cache', 'sort_keys') === true;
157
153
  const tasks = new Array(length);
158
154
  const clients = [];
159
155
  const pools = [];
@@ -177,20 +173,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
177
173
  }
178
174
  catch (err) {
179
175
  let close;
180
- switch (err instanceof Error && err.code) {
181
- case 'ER_ACCESS_DENIED_ERROR':
182
- case 'ER_DBACCESS_DENIED_ERROR':
183
- case 'ER_SPECIFIC_ACCESS_DENIED_ERROR':
184
- case 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR':
185
- close = 1;
186
- break;
187
- default:
188
- close = pool.closeable;
189
- break;
176
+ if (!isErrorCode(err, 'ER_ACCESS_DENIED_ERROR', 'ER_DBACCESS_DENIED_ERROR', 'ER_SPECIFIC_ACCESS_DENIED_ERROR', 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR')) {
177
+ close = pool.closeable;
190
178
  }
191
- if (close) {
179
+ if (close !== false) {
192
180
  await pool.detach(true);
193
- if (close === 1) {
181
+ if (!close) {
194
182
  throw err;
195
183
  }
196
184
  }
@@ -215,7 +203,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
215
203
  for (let i = 0, statement; i < length; ++i) {
216
204
  const item = batch[i];
217
205
  const { source, params, ignoreCache } = item;
218
- const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mariadb", 'options', item.credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
206
+ const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mariadb", 'options', item.credential) && asFunction(item.streamRow) : item.streamRow;
219
207
  let credential = mariaDBCredential || onceCredential, query = item.query, prepared = false, error;
220
208
  if (typeof item.preparedStatement === 'boolean') {
221
209
  if (item.preparedStatement) {
@@ -235,7 +223,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
235
223
  query = statement;
236
224
  prepared = true;
237
225
  }
238
- if (!(0, types_1.isPlainObject)(credential = item.credential) && (error = (0, types_1.errorMessage)(source, "Invalid credentials")) || !query && (error = (0, types_1.errorMessage)(source, "Missing database query"))) {
226
+ if (!isPlainObject(credential = item.credential) && (error = errorMessage(source, "Invalid credentials")) || !query && (error = errorMessage(source, "Missing database query"))) {
239
227
  if (this.handleFail(error, item, { errorQuery })) {
240
228
  if (!parallel) {
241
229
  tasks.length = 0;
@@ -253,11 +241,12 @@ async function executeBatchQuery(batch, options = '', outResult) {
253
241
  }
254
242
  item.transactionState = 1;
255
243
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
256
- let queryString = '';
244
+ let queryString = '', cacheCredential;
257
245
  if (caching && ignoreCache !== true && !prepared && !streamRow) {
258
246
  queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
247
+ cacheCredential = DbPool.sanitize(credential, sortKeys);
259
248
  if (ignoreCache !== 1) {
260
- const result = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue);
249
+ const result = this.getQueryResult(source, cacheCredential, queryString, cacheValue);
261
250
  if (result) {
262
251
  if (parallel) {
263
252
  tasks[i] = Promise.resolve(result);
@@ -301,7 +290,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
301
290
  if (err === false) {
302
291
  continue;
303
292
  }
304
- if (err instanceof Error) {
293
+ if (isError(err)) {
305
294
  error = err;
306
295
  continue;
307
296
  }
@@ -317,19 +306,19 @@ async function executeBatchQuery(batch, options = '', outResult) {
317
306
  }
318
307
  else {
319
308
  this.add(item, 4);
320
- resolve(!error ? this.setQueryResult(source, DbPool.sanitize(credential), queryString, rows, cacheValue) : rows.length > 0 ? rows : null);
309
+ resolve(!error ? this.setQueryResult(source, cacheCredential, queryString, rows, cacheValue) : rows.length > 0 ? rows : null);
321
310
  }
322
311
  }
323
312
  else {
324
313
  const rows = await client[prepared && connectOnce && !parallel ? 'execute' : 'query'](query, params);
325
314
  this.add(item, 4);
326
- if ((0, types_1.isArray)(rows)) {
315
+ if (isArray(rows)) {
327
316
  const [row1, row2] = rows;
328
317
  if (rows.length === 2 && Array.isArray(row1) && !Array.isArray(row2) && isOkPacket(row2)) {
329
318
  resolve(row1);
330
319
  }
331
320
  else {
332
- resolve(!isOkPacket(row1) ? this.setQueryResult(source, DbPool.sanitize(credential), queryString, rows, cacheValue) : null);
321
+ resolve(!isOkPacket(row1) ? this.setQueryResult(source, cacheCredential, queryString, rows, cacheValue) : null);
333
322
  }
334
323
  }
335
324
  else {
@@ -371,5 +360,13 @@ async function executeBatchQuery(batch, options = '', outResult) {
371
360
  async function checkTimeout(value, limit = 0) {
372
361
  return DbPool.checkTimeout(POOL_STATE, value, limit);
373
362
  }
374
- exports.DB_SOURCE_CLIENT = true;
375
- exports.DB_SOURCE_TYPE = types_1.DB_TYPE.SQL;
363
+ const DB_SOURCE_CLIENT = true;
364
+ const DB_SOURCE_TYPE = DB_TYPE.SQL;
365
+
366
+ exports.DB_SOURCE_CLIENT = DB_SOURCE_CLIENT;
367
+ exports.DB_SOURCE_TYPE = DB_SOURCE_TYPE;
368
+ exports.checkTimeout = checkTimeout;
369
+ exports.executeBatchQuery = executeBatchQuery;
370
+ exports.executeQuery = executeQuery;
371
+ exports.setAuthentication = setAuthentication;
372
+ exports.setCredential = setCredential;
package/client/pool.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
- const types_1 = require("@e-mc/types");
2
+
3
+ const { isArray, isObject, isString } = require('@e-mc/types');
3
4
  const DbPool = require('@e-mc/db/pool');
4
5
  const POOL_ACTIVE = new WeakSet();
5
6
  class MariaDBPool extends DbPool {
@@ -11,7 +12,7 @@ class MariaDBPool extends DbPool {
11
12
  }
12
13
  return super.asString(credential);
13
14
  }
14
- static sanitize(credential) {
15
+ static sanitize(credential, sort) {
15
16
  if (!this.canCache(credential)) {
16
17
  return;
17
18
  }
@@ -19,19 +20,19 @@ class MariaDBPool extends DbPool {
19
20
  return credential;
20
21
  }
21
22
  const ssl = credential.ssl;
22
- let result = super.sanitize(credential);
23
- if ((0, types_1.isObject)(ssl)) {
23
+ let result = super.sanitize(credential, sort);
24
+ if (isObject(ssl)) {
24
25
  if (result === credential) {
25
26
  result = { ...credential };
26
27
  }
27
28
  const cert = ssl.cert;
28
- if ((0, types_1.isString)(cert)) {
29
+ if (isString(cert)) {
29
30
  result.ssl = { cert };
30
31
  }
31
32
  else if (Buffer.isBuffer(cert)) {
32
33
  result.ssl = { cert: cert.toString('utf8') };
33
34
  }
34
- else if ((0, types_1.isArray)(cert)) {
35
+ else if (isArray(cert)) {
35
36
  result.ssl = { cert: cert.reduce((a, b) => a + (Buffer.isBuffer(b) ? b.toString('utf8') : b), '') };
36
37
  }
37
38
  else {
@@ -57,4 +58,5 @@ class MariaDBPool extends DbPool {
57
58
  return this.client.closed;
58
59
  }
59
60
  }
61
+
60
62
  module.exports = MariaDBPool;
package/package.json CHANGED
@@ -1,12 +1,9 @@
1
1
  {
2
2
  "name": "@pi-r/mariadb",
3
- "version": "0.11.3",
3
+ "version": "0.12.1",
4
4
  "description": "MariaDB client driver for E-mc.",
5
5
  "main": "client/index.js",
6
6
  "types": "client/index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
7
  "repository": {
11
8
  "type": "git",
12
9
  "url": "git+https://github.com/anpham6/pi-r.git",
@@ -20,8 +17,8 @@
20
17
  "license": "MIT",
21
18
  "homepage": "https://github.com/anpham6/pi-r#readme",
22
19
  "dependencies": {
23
- "@e-mc/db": "^0.13.10",
24
- "@e-mc/types": "^0.13.10",
20
+ "@e-mc/db": "^0.14.1",
21
+ "@e-mc/types": "^0.14.1",
25
22
  "mariadb": "^3.5.2"
26
23
  }
27
24
  }