@pi-r/mysql 0.5.0 → 0.6.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.
Files changed (2) hide show
  1. package/client/index.js +36 -33
  2. package/package.json +3 -3
package/client/index.js CHANGED
@@ -8,10 +8,10 @@ const types_1 = require("@e-mc/types");
8
8
  const Db = require("@e-mc/db");
9
9
  const DbPool = require('@e-mc/db/pool');
10
10
  class MySQLPool extends DbPool {
11
- getConnection() {
11
+ async getConnection() {
12
12
  return this.client.getConnection();
13
13
  }
14
- close() {
14
+ async close() {
15
15
  return this.client.end();
16
16
  }
17
17
  isEmpty() {
@@ -95,39 +95,39 @@ function setCredential(item) {
95
95
  }
96
96
  }
97
97
  const usePool = item.usePool;
98
- if (usePool) {
99
- let username, password, pool;
100
- if (typeof usePool === 'string' && (username = credential.user || credential.uri && (0, util_1.parseConnectionString)(credential.uri)?.username)) {
101
- [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
102
- if (pool) {
103
- pool.add(item, password);
104
- return;
105
- }
98
+ if (!usePool) {
99
+ return;
100
+ }
101
+ let username, password, pool;
102
+ if (typeof usePool === 'string' && (username = credential.user || credential.uri && (0, util_1.parseConnectionString)(credential.uri)?.username)) {
103
+ [password, pool] = DbPool.validateKey(POOL_STATE, username, usePool);
104
+ if (pool) {
105
+ pool.add(item, password);
106
+ return;
106
107
  }
107
- const poolKey = getPoolKey(credential);
108
- if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
109
- const config = this.getPoolConfig("mysql" /* STRINGS.MODULE_NAME */, password);
110
- if (config) {
111
- const { max, idle, queue_max, timeout } = config;
112
- if (max > 0) {
113
- credential.connectionLimit ?? (credential.connectionLimit = max);
114
- }
115
- if (idle >= 0) {
116
- credential.idleTimeout ?? (credential.idleTimeout = idle);
117
- }
118
- if (queue_max >= 0) {
119
- credential.queueLimit ?? (credential.queueLimit = queue_max);
120
- }
121
- if (timeout > 0) {
122
- credential.connectTimeout ?? (credential.connectTimeout = timeout);
123
- }
124
- }
125
- new MySQLPool(mysql.createPool(removeUUIDKey(credential)), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
108
+ }
109
+ const poolKey = getPoolKey(credential);
110
+ if ((pool = POOL_STATE[poolKey]) && !pool.closed) {
111
+ pool.add(item);
112
+ return;
113
+ }
114
+ const config = this.getPoolConfig("mysql" /* STRINGS.MODULE_NAME */, password);
115
+ if (config) {
116
+ const { max, idle, queue_max, timeout } = config;
117
+ if (max > 0) {
118
+ credential.connectionLimit ?? (credential.connectionLimit = max);
126
119
  }
127
- else {
128
- pool.add(item);
120
+ if (idle >= 0) {
121
+ credential.idleTimeout ?? (credential.idleTimeout = idle);
122
+ }
123
+ if (queue_max >= 0) {
124
+ credential.queueLimit ?? (credential.queueLimit = queue_max);
125
+ }
126
+ if (timeout > 0) {
127
+ credential.connectTimeout ?? (credential.connectTimeout = timeout);
129
128
  }
130
129
  }
130
+ new MySQLPool(mysql.createPool(removeUUIDKey(credential)), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
131
131
  }
132
132
  exports.setCredential = setCredential;
133
133
  async function executeQuery(item, options) {
@@ -215,6 +215,9 @@ async function executeBatchQuery(batch, options = '', outResult) {
215
215
  item.transactionState &= ~64 /* DB_TRANSACTION.AUTH */;
216
216
  return client;
217
217
  };
218
+ if (this.host?.username) {
219
+ this.applyCommand(...batch);
220
+ }
218
221
  for (let i = 0, mysql2Client, mysql2Credential; i < length; ++i) {
219
222
  const item = batch[i];
220
223
  const { source, query, params, ignoreCache } = item;
@@ -362,14 +365,14 @@ async function executeBatchQuery(batch, options = '', outResult) {
362
365
  }
363
366
  return this.processRows(batch, tasks, {
364
367
  disconnect: () => {
365
- clients.forEach(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 })));
368
+ 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 })));
366
369
  pools.forEach(item => item.release());
367
370
  },
368
371
  parallel
369
372
  }, outResult);
370
373
  }
371
374
  exports.executeBatchQuery = executeBatchQuery;
372
- function checkTimeout(value, limit = 0) {
375
+ async function checkTimeout(value, limit = 0) {
373
376
  return DbPool.checkTimeout(POOL_STATE, value, limit);
374
377
  }
375
378
  exports.checkTimeout = checkTimeout;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-r/mysql",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
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.7.0",
25
- "@e-mc/types": "^0.7.0",
24
+ "@e-mc/db": "^0.8.0",
25
+ "@e-mc/types": "^0.8.0",
26
26
  "mysql2": "^3.6.5"
27
27
  }
28
28
  }