@pi-r/mysql 0.10.3 → 0.11.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
@@ -97,13 +97,16 @@ function setCredential(item) {
97
97
  }
98
98
  const config = this.getPoolConfig("mysql", password);
99
99
  if (config) {
100
- const { max, idle, queue_max, timeout } = config;
100
+ const { max, idle, idle_max, queue_max, timeout } = config;
101
101
  if (max > 0) {
102
102
  credential.connectionLimit ??= max;
103
103
  }
104
104
  if (idle >= 0) {
105
105
  credential.idleTimeout ??= idle;
106
106
  }
107
+ if (idle_max >= 0) {
108
+ credential.maxIdle ??= idle_max;
109
+ }
107
110
  if (queue_max >= 0) {
108
111
  credential.queueLimit ??= queue_max;
109
112
  }
@@ -122,7 +125,7 @@ function setCredential(item) {
122
125
  .parent = POOL_STATE;
123
126
  }
124
127
  async function executeQuery(item, options) {
125
- return (await executeBatchQuery.call(this, [item], options))[0] || [];
128
+ return (await executeBatchQuery.call(this, [item], options)).at(0) || [];
126
129
  }
127
130
  async function executeBatchQuery(batch, options = '', outResult) {
128
131
  const length = batch.length;
@@ -173,16 +176,11 @@ async function executeBatchQuery(batch, options = '', outResult) {
173
176
  }
174
177
  catch (err) {
175
178
  let close;
176
- switch (err instanceof Error && err.code) {
177
- case 'ER_ACCESS_DENIED_ERROR':
178
- case 'ER_DBACCESS_DENIED_ERROR':
179
- case 'ER_SPECIFIC_ACCESS_DENIED_ERROR':
180
- case 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR':
181
- close = 1;
182
- break;
183
- default:
184
- close = pool.closeable;
185
- break;
179
+ if ((0, types_1.isErrorCode)(err, 'ER_ACCESS_DENIED_ERROR', 'ER_DBACCESS_DENIED_ERROR', 'ER_SPECIFIC_ACCESS_DENIED_ERROR', 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR')) {
180
+ close = 1;
181
+ }
182
+ else {
183
+ close = pool.closeable;
186
184
  }
187
185
  if (close) {
188
186
  await pool.detach(true);
@@ -208,10 +206,30 @@ async function executeBatchQuery(batch, options = '', outResult) {
208
206
  if (this.host?.username) {
209
207
  this.applyCommand(...batch);
210
208
  }
211
- for (let i = 0, mysql2Client, mysql2Credential; i < length; ++i) {
209
+ for (let i = 0, mysql2Client, mysql2Credential, statement; i < length; ++i) {
212
210
  const item = batch[i];
213
- const { source, query, params, ignoreCache } = item;
211
+ const { source, params, ignoreCache } = item;
214
212
  const streamRow = typeof item.streamRow === 'string' ? this.hasCoerce("mysql", 'options', mysql2Credential || item.credential) && (0, types_1.asFunction)(item.streamRow) : item.streamRow;
213
+ let query = item.query, prepared = false;
214
+ if (typeof item.preparedStatement === 'boolean') {
215
+ if (item.preparedStatement) {
216
+ if (query) {
217
+ statement = query;
218
+ }
219
+ else {
220
+ query = statement;
221
+ }
222
+ prepared = true;
223
+ }
224
+ else if (statement) {
225
+ mysqlClient?.unprepare(statement);
226
+ statement = undefined;
227
+ }
228
+ }
229
+ else if (!query && statement) {
230
+ query = statement;
231
+ prepared = true;
232
+ }
215
233
  let credential, error;
216
234
  if (streamRow) {
217
235
  if (mysql2Credential) {
@@ -241,7 +259,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
241
259
  item.transactionState = 1;
242
260
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
243
261
  let queryString = '';
244
- if (caching && ignoreCache !== true && !streamRow) {
262
+ if (caching && ignoreCache !== true && !prepared && !streamRow) {
245
263
  queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
246
264
  if (ignoreCache !== 1) {
247
265
  const result = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue);
@@ -285,8 +303,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
285
303
  mysql2Credential = credential;
286
304
  }
287
305
  commandType = this.commandType.SELECT;
288
- client
289
- .query(query, params)
306
+ client[prepared && connectOnce && !parallel ? 'execute' : 'query'](query, params)
290
307
  .on('result', row => {
291
308
  if (processRow) {
292
309
  const err = processRow(row);
@@ -317,7 +334,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
317
334
  mysqlClient = undefined;
318
335
  }
319
336
  commandType = this.commandType.SELECT;
320
- const [rows] = await client.query(query, params);
337
+ const [rows] = await client[prepared && connectOnce && !parallel ? 'execute' : 'query'](query, params);
321
338
  this.add(item, 4);
322
339
  if ((0, types_1.isArray)(rows)) {
323
340
  const [row1, row2] = rows;
package/client/pool.js CHANGED
@@ -4,7 +4,7 @@ const util_1 = require("@e-mc/db/util");
4
4
  const DbPool = require('@e-mc/db/pool');
5
5
  const POOL_ACTIVE = new WeakSet();
6
6
  class MySQLPool extends DbPool {
7
- static CACHE_UNUSED = ['connectTimeout', 'queueLimit', 'waitForConnections', 'connectionLimit', 'maxIdle', 'idleTimeout', 'enableKeepAlive', 'keepAliveInitialDelay', 'Promise'];
7
+ static CACHE_UNUSED = ['connectTimeout', 'queueLimit', 'waitForConnections', 'connectionLimit', 'maxIdle', 'idleTimeout', 'enableKeepAlive', 'keepAliveInitialDelay', 'Promise', 'gracefulEnd'];
8
8
  static CACHE_IGNORE = ['authPlugins', 'authSwitchHandler', 'stream', 'infileStreamFactory'];
9
9
  static asString(credential) {
10
10
  if ((credential.host || credential.socketPath) && credential.user) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mysql",
3
- "version": "0.10.3",
3
+ "version": "0.11.1",
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.12.7",
24
- "@e-mc/types": "^0.12.7",
25
- "mysql2": "^3.14.5"
23
+ "@e-mc/db": "^0.13.5",
24
+ "@e-mc/types": "^0.13.5",
25
+ "mysql2": "^3.16.0"
26
26
  }
27
27
  }
package/types/index.d.ts CHANGED
@@ -7,6 +7,7 @@ import type { PoolOptions, QueryOptions } from 'mysql2/promise';
7
7
 
8
8
  export interface MySQLDataSource<T = unknown> extends DbDataSource<string | QueryOptions, unknown, unknown, MySQLCredential, string>, ExecuteAction<T> {
9
9
  source: "mysql";
10
+ preparedStatement?: boolean;
10
11
  }
11
12
 
12
13
  export type MySQLCredential = PoolOptions & ServerAuth;