@pi-r/mysql 0.6.3 → 0.6.5

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/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2023 An Pham
1
+ Copyright 2024 An Pham
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
4
 
package/README.md CHANGED
@@ -1,7 +1,10 @@
1
- ### @pi-r/mysql
1
+ # @pi-r/mysql
2
2
 
3
- https://e-mc.readthedocs.io/en/latest/db/mysql.html
3
+ ## Documentation
4
4
 
5
- ### LICENSE
5
+ - [E-mc](https://e-mc.readthedocs.io/en/latest/db/mysql.html)
6
+ - [squared](https://squared.readthedocs.io)
7
+
8
+ ## LICENSE
6
9
 
7
10
  MIT
package/client/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
1
  import type { IDb } from '@e-mc/types/lib';
2
- import type { MySQLCredential, MySQLDataSource } from '@e-mc/types/lib/db';
3
2
 
4
3
  import type { IDbSourceClient } from '@e-mc/db/types';
5
4
 
5
+ import type { MySQLCredential, MySQLDataSource } from '../types';
6
+
6
7
  declare const MySQL: IDbSourceClient<MySQLDataSource> & {
7
8
  setAuthentication(this: IDb, credential: MySQLCredential): void;
8
9
  };
package/client/index.js CHANGED
@@ -24,8 +24,9 @@ class MySQLPool extends DbPool {
24
24
  }
25
25
  const POOL_STATE = {};
26
26
  const POOL_UNUSED = ['ssl', 'connectTimeout', 'queueLimit', 'waitForConnections', 'connectionLimit', 'maxIdle', 'idleTimeout', 'enableKeepAlive', 'keepAliveInitialDelay', 'Promise'];
27
+ const POOL_ACTIVE = new WeakSet();
27
28
  function removePoolProperties(credential) {
28
- if (!credential.uuidKey) {
29
+ if (!credential.uuidKey && POOL_ACTIVE.has(credential)) {
29
30
  let result;
30
31
  for (let i = 0, length = POOL_UNUSED.length; i < length; ++i) {
31
32
  const attr = POOL_UNUSED[i];
@@ -95,7 +96,7 @@ function setCredential(item) {
95
96
  errors.push('user');
96
97
  }
97
98
  if (errors.length) {
98
- throw (0, types_1.errorMessage)("mysql" /* STRINGS.MODULE_NAME */, 'Not defined - ' + errors.join(' | '));
99
+ throw (0, types_1.errorMessage)("mysql", 'Not defined - ' + errors.join(' | '));
99
100
  }
100
101
  }
101
102
  const usePool = item.usePool;
@@ -116,7 +117,7 @@ function setCredential(item) {
116
117
  return;
117
118
  }
118
119
  }
119
- const config = this.getPoolConfig("mysql" /* STRINGS.MODULE_NAME */, password);
120
+ const config = this.getPoolConfig("mysql", password);
120
121
  if (config) {
121
122
  const { max, idle, queue_max, timeout } = config;
122
123
  if (max > 0) {
@@ -169,17 +170,18 @@ async function executeBatchQuery(batch, options = '', outResult) {
169
170
  if (!parallel) {
170
171
  outResult || (outResult = new Array(length));
171
172
  }
172
- const caching = this.hasCache("mysql" /* STRINGS.MODULE_NAME */, sessionKey);
173
+ const caching = this.hasCache("mysql", sessionKey);
173
174
  const tasks = new Array(length);
174
175
  const clients = [];
175
176
  const pools = [];
176
177
  let mysqlPool, mysqlClient, mysqlCredential, onceCredential = connectOnce ? batch[0].credential : undefined;
177
178
  const getConnection = async (item, credential) => {
178
- item.transactionState = 64 /* DB_TRANSACTION.AUTH */;
179
+ item.transactionState = 64;
179
180
  let client;
180
181
  if (mysqlPool) {
181
182
  pools.push(client = await mysqlPool.getConnection());
182
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
183
+ item.transactionState &= ~64;
184
+ POOL_ACTIVE.add(credential);
183
185
  return client;
184
186
  }
185
187
  const pool = item.usePool && DbPool.findKey(POOL_STATE, item.usePool, getPoolKey(credential), ...connectOnce ? [item, batch[0]] : [item]);
@@ -190,6 +192,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
190
192
  mysqlPool = pool;
191
193
  }
192
194
  pool.connected = true;
195
+ POOL_ACTIVE.add(credential);
193
196
  }
194
197
  catch (err) {
195
198
  let close;
@@ -222,7 +225,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
222
225
  }
223
226
  mysqlCredential = credential;
224
227
  }
225
- item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
228
+ item.transactionState &= ~64;
226
229
  return client;
227
230
  };
228
231
  if (this.host?.username) {
@@ -231,7 +234,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
231
234
  for (let i = 0, mysql2Client, mysql2Credential; i < length; ++i) {
232
235
  const item = batch[i];
233
236
  const { source, query, params, ignoreCache } = item;
234
- 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;
237
+ const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mysql", 'options', null, mysql2Credential || item.credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
235
238
  let credential, error;
236
239
  if (streamRow) {
237
240
  if (mysql2Credential) {
@@ -242,7 +245,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
242
245
  else if (credential = mysqlCredential || onceCredential) {
243
246
  error = null;
244
247
  }
245
- 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 */))) {
248
+ if (error !== null && !(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"))) {
246
249
  if (this.handleFail(error, item, { errorQuery })) {
247
250
  if (!parallel) {
248
251
  tasks.length = 0;
@@ -258,7 +261,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
258
261
  }
259
262
  continue;
260
263
  }
261
- item.transactionState = 1 /* DB_TRANSACTION.ACTIVE */;
264
+ item.transactionState = 1;
262
265
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
263
266
  let queryString = '';
264
267
  if (caching && ignoreCache !== true && !streamRow) {
@@ -272,7 +275,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
272
275
  else {
273
276
  outResult[i] = result;
274
277
  }
275
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */ | 128 /* DB_TRANSACTION.CACHE */);
278
+ this.add(item, 4 | 128);
276
279
  continue;
277
280
  }
278
281
  if (!ignoreCache && outCacheMiss) {
@@ -326,7 +329,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
326
329
  reject(error);
327
330
  }
328
331
  else {
329
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
332
+ this.add(item, 4);
330
333
  resolve(!error ? this.setQueryResult(source, removePoolProperties(credential), queryString, rows, cacheValue) : rows.length ? rows : null);
331
334
  }
332
335
  });
@@ -338,7 +341,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
338
341
  }
339
342
  commandType = this.commandType.SELECT;
340
343
  const [rows] = await client.query(query, params);
341
- this.add(item, 4 /* DB_TRANSACTION.COMMIT */);
344
+ this.add(item, 4);
342
345
  if ((0, types_1.isArray)(rows)) {
343
346
  const [row1, row2] = rows;
344
347
  if (rows.length === 2 && Array.isArray(row1) && !Array.isArray(row2) && isResultSetHeader(row2)) {
@@ -374,7 +377,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
374
377
  }
375
378
  return this.processRows(batch, tasks, {
376
379
  disconnect: () => {
377
- clients.forEach(async (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 })));
380
+ clients.forEach(async (item) => item.end(err => err && this.writeFail(["Unable to close connnection", "mysql"], err, { type: 65536, fatal: false })));
378
381
  pools.forEach(item => item.release());
379
382
  },
380
383
  parallel
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mysql",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
4
4
  "description": "MySQL 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.8.3",
25
- "@e-mc/types": "^0.8.3",
26
- "mysql2": "^3.9.1"
24
+ "@e-mc/db": "^0.8.6",
25
+ "@e-mc/types": "^0.8.6",
26
+ "mysql2": "^3.9.2"
27
27
  }
28
28
  }