@pi-r/mysql 0.11.0 → 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
@@ -125,7 +125,7 @@ function setCredential(item) {
125
125
  .parent = POOL_STATE;
126
126
  }
127
127
  async function executeQuery(item, options) {
128
- return (await executeBatchQuery.call(this, [item], options))[0] || [];
128
+ return (await executeBatchQuery.call(this, [item], options)).at(0) || [];
129
129
  }
130
130
  async function executeBatchQuery(batch, options = '', outResult) {
131
131
  const length = batch.length;
@@ -206,10 +206,30 @@ async function executeBatchQuery(batch, options = '', outResult) {
206
206
  if (this.host?.username) {
207
207
  this.applyCommand(...batch);
208
208
  }
209
- for (let i = 0, mysql2Client, mysql2Credential; i < length; ++i) {
209
+ for (let i = 0, mysql2Client, mysql2Credential, statement; i < length; ++i) {
210
210
  const item = batch[i];
211
- const { source, query, params, ignoreCache } = item;
211
+ const { source, params, ignoreCache } = item;
212
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
+ }
213
233
  let credential, error;
214
234
  if (streamRow) {
215
235
  if (mysql2Credential) {
@@ -239,7 +259,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
239
259
  item.transactionState = 1;
240
260
  const cacheValue = ignoreCache === undefined ? sessionKey : Array.isArray(ignoreCache) ? { sessionKey, exclusiveOf: ignoreCache } : { sessionKey, renewCache: ignoreCache === 0 };
241
261
  let queryString = '';
242
- if (caching && ignoreCache !== true && !streamRow) {
262
+ if (caching && ignoreCache !== true && !prepared && !streamRow) {
243
263
  queryString = Db.asString(query, true) + '_' + Db.asString(params, true);
244
264
  if (ignoreCache !== 1) {
245
265
  const result = this.getQueryResult(source, DbPool.sanitize(credential), queryString, cacheValue);
@@ -283,8 +303,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
283
303
  mysql2Credential = credential;
284
304
  }
285
305
  commandType = this.commandType.SELECT;
286
- client
287
- .query(query, params)
306
+ client[prepared && connectOnce && !parallel ? 'execute' : 'query'](query, params)
288
307
  .on('result', row => {
289
308
  if (processRow) {
290
309
  const err = processRow(row);
@@ -315,7 +334,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
315
334
  mysqlClient = undefined;
316
335
  }
317
336
  commandType = this.commandType.SELECT;
318
- const [rows] = await client.query(query, params);
337
+ const [rows] = await client[prepared && connectOnce && !parallel ? 'execute' : 'query'](query, params);
319
338
  this.add(item, 4);
320
339
  if ((0, types_1.isArray)(rows)) {
321
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.11.0",
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.13.0",
24
- "@e-mc/types": "^0.13.0",
25
- "mysql2": "^3.15.2"
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;