@pi-r/mysql 0.3.4 → 0.3.6

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,7 +1,7 @@
1
- Copyright 2024 An Pham
2
-
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
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
1
+ Copyright 2023 An Pham
2
+
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
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
7
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1,7 +1,5 @@
1
- # @pi-r/mysql
2
-
3
- PEP 402 - Forever
4
-
5
- ## LICENSE
6
-
1
+ # @pi-r/mysql
2
+
3
+ ## LICENSE
4
+
7
5
  MIT
package/client/index.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import type { IDb } from '@e-mc/types/lib';
2
-
3
- import type { IDbSourceClient } from '@e-mc/db/types';
4
-
5
- import type { MySQLCredential, MySQLDataSource } from '../types';
6
-
7
- declare const MySQL: IDbSourceClient<MySQLDataSource> & {
8
- setAuthentication(this: IDb, credential: MySQLCredential): void;
9
- };
10
-
1
+ import type { IDb } from '@e-mc/types/lib';
2
+
3
+ import type { IDbSourceClient } from '@e-mc/db/types';
4
+
5
+ import type { MySQLCredential, MySQLDataSource } from '../types';
6
+
7
+ declare const MySQL: IDbSourceClient<MySQLDataSource> & {
8
+ setAuthentication(this: IDb, credential: MySQLCredential): void;
9
+ };
10
+
11
11
  export = MySQL;
package/client/index.js CHANGED
@@ -1,8 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DB_SOURCE_TYPE = exports.DB_SOURCE_CLIENT = exports.checkTimeout = exports.executeBatchQuery = exports.executeQuery = exports.setCredential = exports.setAuthentication = void 0;
4
- const main = require("mysql2");
5
- const mysql = require("mysql2/promise");
6
4
  const util_1 = require("@e-mc/db/util");
7
5
  const types_1 = require("@e-mc/types");
8
6
  const Db = require("@e-mc/db");
@@ -107,6 +105,7 @@ function setCredential(item) {
107
105
  }
108
106
  const poolKey = getPoolKey(credential);
109
107
  if (!(pool = POOL_STATE[poolKey]) || pool.closed) {
108
+ const { createPool } = require("mysql2/promise");
110
109
  const config = this.getPoolConfig("mysql", password);
111
110
  if (config) {
112
111
  const { max, idle, queue_max } = config;
@@ -120,7 +119,7 @@ function setCredential(item) {
120
119
  (_d = credential.queueLimit) !== null && _d !== void 0 ? _d : (credential.queueLimit = queue_max);
121
120
  }
122
121
  }
123
- new MySQLPool(mysql.createPool(removeUUIDKey(credential)), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
122
+ new MySQLPool(createPool(removeUUIDKey(credential)), poolKey, username && password ? { username, password } : undefined).add(item).parent = POOL_STATE;
124
123
  }
125
124
  else {
126
125
  pool.add(item);
@@ -137,6 +136,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
137
136
  if (length === 0) {
138
137
  return [];
139
138
  }
139
+ const db = require("mysql2/promise");
140
140
  let parallel, connectOnce, errorQuery, sessionKey, outCacheMiss;
141
141
  if ((0, types_1.isPlainObject)(options)) {
142
142
  ({ parallel, connectOnce, errorQuery, sessionKey, outCacheMiss } = options);
@@ -202,7 +202,7 @@ async function executeBatchQuery(batch, options = '', outResult) {
202
202
  }
203
203
  }
204
204
  if (!client) {
205
- clients.push(client = await mysql.createConnection(removeUUIDKey(mysqlCredential || credential)));
205
+ clients.push(client = await db.createConnection(removeUUIDKey(mysqlCredential || credential)));
206
206
  }
207
207
  if (connectOnce) {
208
208
  if (!parallel) {
@@ -282,7 +282,8 @@ async function executeBatchQuery(batch, options = '', outResult) {
282
282
  if (streamRow) {
283
283
  const rows = [];
284
284
  const processRow = typeof streamRow === 'function' && streamRow;
285
- const client = mysql2Client || main.createConnection(removeUUIDKey(mysql2Credential || credential));
285
+ const { createConnection } = require("mysql2");
286
+ const client = mysql2Client || createConnection(removeUUIDKey(mysql2Credential || credential));
286
287
  clients.push(client);
287
288
  if (connectOnce) {
288
289
  if (!parallel) {
package/client/pool.js ADDED
@@ -0,0 +1,61 @@
1
+ "use strict";
2
+ const types_1 = require("@e-mc/types");
3
+ const util_1 = require("@e-mc/db/util");
4
+ const DbPool = require('@e-mc/db/pool');
5
+ const POOL_ACTIVE = new WeakSet();
6
+ class MySQLPool extends DbPool {
7
+ static CACHE_UNUSED = ['connectTimeout', 'queueLimit', 'waitForConnections', 'connectionLimit', 'maxIdle', 'idleTimeout', 'enableKeepAlive', 'keepAliveInitialDelay', 'Promise'];
8
+ static CACHE_IGNORE = ['authPlugins', 'authSwitchHandler', 'stream', 'infileStreamFactory'];
9
+ static asString(credential) {
10
+ if ((credential.host || credential.socketPath) && credential.user) {
11
+ return JSON.stringify(this.removeUUIDKey(credential));
12
+ }
13
+ return super.asString(credential);
14
+ }
15
+ static sanitize(credential) {
16
+ if (!this.canCache(credential)) {
17
+ return;
18
+ }
19
+ if (!POOL_ACTIVE.has(credential) || credential.uuidKey) {
20
+ return credential;
21
+ }
22
+ const ssl = credential.ssl;
23
+ let result = super.sanitize(credential);
24
+ if ((0, types_1.isObject)(ssl)) {
25
+ if (result === credential) {
26
+ result = { ...credential };
27
+ }
28
+ const cert = ssl.cert;
29
+ if ((0, types_1.isString)(cert)) {
30
+ result.ssl = cert;
31
+ }
32
+ else if (Buffer.isBuffer(cert)) {
33
+ result.ssl = cert.toString('utf8');
34
+ }
35
+ else if ((0, types_1.isArray)(cert)) {
36
+ result.ssl = cert.reduce((a, b) => a + (Buffer.isBuffer(b) ? b.toString('utf8') : b), '');
37
+ }
38
+ else {
39
+ result.ssl = "Unknown";
40
+ }
41
+ }
42
+ return result;
43
+ }
44
+ async getConnection(credential) {
45
+ if (credential) {
46
+ POOL_ACTIVE.add(credential);
47
+ }
48
+ return this.client.getConnection();
49
+ }
50
+ async close() {
51
+ return this.client.end();
52
+ }
53
+ isEmpty() {
54
+ const pool = this.client.pool;
55
+ return this.closed || (0, util_1.checkEmpty)(pool._allConnections) && (0, util_1.checkEmpty)(pool._connectionQueue);
56
+ }
57
+ get closed() {
58
+ return !!this.client.pool._closed;
59
+ }
60
+ }
61
+ module.exports = MySQLPool;
package/package.json CHANGED
@@ -1,28 +1,28 @@
1
- {
2
- "name": "@pi-r/mysql",
3
- "version": "0.3.4",
4
- "description": "MySQL client driver for E-mc.",
5
- "main": "client/index.js",
6
- "types": "client/index.d.ts",
7
- "publishConfig": {
8
- "access": "public"
9
- },
10
- "repository": {
11
- "type": "git",
12
- "url": "git+https://github.com/anpham6/pi-r.git",
13
- "directory": "src/db/mysql"
14
- },
15
- "keywords": [
16
- "squared",
17
- "e-mc",
18
- "squared-functions"
19
- ],
20
- "author": "An Pham <anpham6@gmail.com>",
21
- "license": "MIT",
22
- "homepage": "https://github.com/anpham6/pi-r#readme",
23
- "dependencies": {
24
- "@e-mc/db": "^0.6.3",
25
- "@e-mc/types": "^0.6.3",
26
- "mysql2": "^3.9.3"
27
- }
28
- }
1
+ {
2
+ "name": "@pi-r/mysql",
3
+ "version": "0.3.6",
4
+ "description": "MySQL client driver for E-mc.",
5
+ "main": "client/index.js",
6
+ "types": "client/index.d.ts",
7
+ "publishConfig": {
8
+ "access": "public"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/anpham6/pi-r.git",
13
+ "directory": "src/db/mysql"
14
+ },
15
+ "keywords": [
16
+ "squared",
17
+ "e-mc",
18
+ "squared-functions"
19
+ ],
20
+ "author": "An Pham <anpham6@gmail.com>",
21
+ "license": "MIT",
22
+ "homepage": "https://github.com/anpham6/pi-r#readme",
23
+ "dependencies": {
24
+ "@e-mc/db": "^0.6.16",
25
+ "@e-mc/types": "^0.6.16",
26
+ "mysql2": "^3.10.3"
27
+ }
28
+ }
package/types/index.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- import type { DbDataSource } from '@e-mc/types/lib/squared';
2
-
3
- import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
4
-
5
- import type { SecureContextOptions } from 'tls';
6
-
7
- import type { PoolOptions, QueryOptions } from 'mysql2/promise';
8
-
9
- export interface MySQLDataSource<T = unknown> extends DbDataSource<string | QueryOptions, unknown, unknown, string | MySQLCredential, string>, ExecuteAction<T> {
10
- source: "mysql";
11
- }
12
-
13
- export interface MySQLCredential extends ServerAuth, Omit<PoolOptions, "ssl"> {
14
- ssl?: boolean | string | SecureContextOptions & { rejectUnauthorized?: boolean };
1
+ import type { DbDataSource } from '@e-mc/types/lib/squared';
2
+
3
+ import type { ExecuteAction, ServerAuth } from '@e-mc/types/lib/db';
4
+
5
+ import type { SecureContextOptions } from 'tls';
6
+
7
+ import type { PoolOptions, QueryOptions } from 'mysql2/promise';
8
+
9
+ export interface MySQLDataSource<T = unknown> extends DbDataSource<string | QueryOptions, unknown, unknown, string | MySQLCredential, string>, ExecuteAction<T> {
10
+ source: "mysql";
11
+ }
12
+
13
+ export interface MySQLCredential extends ServerAuth, Omit<PoolOptions, "ssl"> {
14
+ ssl?: boolean | string | SecureContextOptions & { rejectUnauthorized?: boolean };
15
15
  }