From 986786e270d56aac3b7c2d46deaa6d966891f4a1 Mon Sep 17 00:00:00 2001 From: "raoha.rh" Date: Tue, 30 Jan 2024 13:05:35 +0800 Subject: [PATCH] feat: rename to rds and support mysql2 feat: add mysql@8 test case fix: concurrency test mysql 5,8 --- .github/workflows/nodejs.yml | 1 + CHANGELOG.md | 82 ++++++++++++++++++------------------ LICENSE | 2 +- README.md | 20 ++++----- package.json | 20 ++++----- src/PoolConfig.ts | 33 +++++++++++---- src/client.ts | 21 ++++----- src/operator.ts | 2 +- src/sqlstring.ts | 5 ++- src/types.ts | 8 ++-- test/PoolConfig.test.ts | 8 ++-- test/client.test.ts | 25 ++++++----- test/rds_init.sql | 2 +- test/sqlstring.test.ts | 2 +- tsconfig.json | 3 +- typings/mysql/index.d.ts | 12 ++++++ 16 files changed, 134 insertions(+), 112 deletions(-) create mode 100644 typings/mysql/index.d.ts diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index c6ac5fa..d7d876c 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -14,3 +14,4 @@ jobs: with: os: 'ubuntu-latest' version: '16.17.0, 16, 18, 20' + mysql_version: '5, 8' \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 70d605e..ecca429 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,34 +1,34 @@ # Changelog -## [6.3.0](https://github.com/ali-sdk/ali-rds/compare/v6.2.0...v6.3.0) (2023-07-31) +## [6.3.0](https://github.com/node-modules/myrds/v6.2.0...v6.3.0) (2023-07-31) ### Features -* stats 增加使用中的连接数 ([#115](https://github.com/ali-sdk/ali-rds/issues/115)) ([2b152a1](https://github.com/ali-sdk/ali-rds/commit/2b152a14b39291665c74910f2fe803d8318843db)) +* stats 增加使用中的连接数 ([#115](https://github.com/node-modules/myrds/issues/115)) ([2b152a1](https://github.com/node-modules/myrds/commit/2b152a14b39291665c74910f2fe803d8318843db)) -## [6.2.0](https://github.com/ali-sdk/ali-rds/compare/v6.1.0...v6.2.0) (2023-06-10) +## [6.2.0](https://github.com/node-modules/myrds/v6.1.0...v6.2.0) (2023-06-10) ### Features -* export connection and query diagnostics_channel ([#111](https://github.com/ali-sdk/ali-rds/issues/111)) ([64aa75d](https://github.com/ali-sdk/ali-rds/commit/64aa75d121d63ddae4fa4bd2a6e097773f280dfe)) +* export connection and query diagnostics_channel ([#111](https://github.com/node-modules/myrds/issues/111)) ([64aa75d](https://github.com/node-modules/myrds/commit/64aa75d121d63ddae4fa4bd2a6e097773f280dfe)) -## [6.1.0](https://github.com/ali-sdk/ali-rds/compare/v6.0.1...v6.1.0) (2023-06-09) +## [6.1.0](https://github.com/node-modules/myrds/v6.0.1...v6.1.0) (2023-06-09) ### Features -* dynamic retrieval of database connection configuration ([#110](https://github.com/ali-sdk/ali-rds/issues/110)) ([f437efb](https://github.com/ali-sdk/ali-rds/commit/f437efb439c1770561c1d4dc79318c632e7321d9)) +* dynamic retrieval of database connection configuration ([#110](https://github.com/node-modules/myrds/issues/110)) ([f437efb](https://github.com/node-modules/myrds/commit/f437efb439c1770561c1d4dc79318c632e7321d9)) -## [6.0.1](https://github.com/ali-sdk/ali-rds/compare/v6.0.0...v6.0.1) (2023-06-05) +## [6.0.1](https://github.com/node-modules/myrds/v6.0.0...v6.0.1) (2023-06-05) ### Bug Fixes -* mysql type not found ([#109](https://github.com/ali-sdk/ali-rds/issues/109)) ([6a9bc45](https://github.com/ali-sdk/ali-rds/commit/6a9bc452a8a73f9d697ee0e55f91b823ef153df4)) +* mysql type not found ([#109](https://github.com/node-modules/myrds/issues/109)) ([6a9bc45](https://github.com/node-modules/myrds/commit/6a9bc452a8a73f9d697ee0e55f91b823ef153df4)) -## [6.0.0](https://github.com/ali-sdk/ali-rds/compare/v5.1.2...v6.0.0) (2023-06-03) +## [6.0.0](https://github.com/node-modules/myrds/v5.1.2...v6.0.0) (2023-06-03) ### ⚠ BREAKING CHANGES @@ -37,35 +37,35 @@ ### Features -* use AsyncLocalStorage to refactor transaction, to make it more safe ([#108](https://github.com/ali-sdk/ali-rds/issues/108)) ([ae327fa](https://github.com/ali-sdk/ali-rds/commit/ae327fa5a350b48c4e1f56c2769524c5786e1152)) +* use AsyncLocalStorage to refactor transaction, to make it more safe ([#108](https://github.com/node-modules/myrds/issues/108)) ([ae327fa](https://github.com/node-modules/myrds/commit/ae327fa5a350b48c4e1f56c2769524c5786e1152)) ### Bug Fixes -* use master branch ([758877d](https://github.com/ali-sdk/ali-rds/commit/758877d9e01df74b9df12c65b7f625275996656b)) +* use master branch ([758877d](https://github.com/node-modules/myrds/commit/758877d9e01df74b9df12c65b7f625275996656b)) -## [5.1.2](https://github.com/ali-sdk/ali-rds/compare/v5.1.1...v5.1.2) (2023-03-06) +## [5.1.2](https://github.com/node-modules/myrds/v5.1.1...v5.1.2) (2023-03-06) ### Bug Fixes -* don't export protected methods ([#106](https://github.com/ali-sdk/ali-rds/issues/106)) ([b2757df](https://github.com/ali-sdk/ali-rds/commit/b2757dffdf76bb74e9fff8a89632d19704b03e4f)) +* don't export protected methods ([#106](https://github.com/node-modules/myrds/issues/106)) ([b2757df](https://github.com/node-modules/myrds/commit/b2757dffdf76bb74e9fff8a89632d19704b03e4f)) -## [5.1.1](https://github.com/ali-sdk/ali-rds/compare/v5.1.0...v5.1.1) (2023-03-05) +## [5.1.1](https://github.com/node-modules/myrds/v5.1.0...v5.1.1) (2023-03-05) ### Bug Fixes -* support multi lifecircle hooks ([#105](https://github.com/ali-sdk/ali-rds/issues/105)) ([53b0a70](https://github.com/ali-sdk/ali-rds/commit/53b0a7058e4f3e583dc4610b1d1338014b9f2c15)) +* support multi lifecircle hooks ([#105](https://github.com/node-modules/myrds/issues/105)) ([53b0a70](https://github.com/node-modules/myrds/commit/53b0a7058e4f3e583dc4610b1d1338014b9f2c15)) -## [5.1.0](https://github.com/ali-sdk/ali-rds/compare/v5.0.0...v5.1.0) (2023-03-05) +## [5.1.0](https://github.com/node-modules/myrds/v5.0.0...v5.1.0) (2023-03-05) ### Features -* support custom query lifecircle ([#104](https://github.com/ali-sdk/ali-rds/issues/104)) ([5941c69](https://github.com/ali-sdk/ali-rds/commit/5941c69b461ad581aa88c211ee6c60a88d4f5420)) +* support custom query lifecircle ([#104](https://github.com/node-modules/myrds/issues/104)) ([5941c69](https://github.com/node-modules/myrds/commit/5941c69b461ad581aa88c211ee6c60a88d4f5420)) -## [5.0.0](https://github.com/ali-sdk/ali-rds/compare/v4.1.0...v5.0.0) (2023-03-04) +## [5.0.0](https://github.com/node-modules/myrds/v4.1.0...v5.0.0) (2023-03-04) ### ⚠ BREAKING CHANGES @@ -74,29 +74,29 @@ ### Features -* impl with typescript ([#103](https://github.com/ali-sdk/ali-rds/issues/103)) ([1cf7814](https://github.com/ali-sdk/ali-rds/commit/1cf7814effb2876919e73d331547ecd14caf45f4)) +* impl with typescript ([#103](https://github.com/node-modules/myrds/issues/103)) ([1cf7814](https://github.com/node-modules/myrds/commit/1cf7814effb2876919e73d331547ecd14caf45f4)) -## [4.1.0](https://github.com/ali-sdk/ali-rds/compare/v4.0.3...v4.1.0) (2023-01-01) +## [4.1.0](https://github.com/node-modules/myrds/v4.0.3...v4.1.0) (2023-01-01) ### Features -* add unlock/lock tables ([#97](https://github.com/ali-sdk/ali-rds/issues/97)) ([4dc3452](https://github.com/ali-sdk/ali-rds/commit/4dc3452a375e0c242084e23c6e5f1cb76f1b647d)) -* add unlock/lock tables ([#97](https://github.com/ali-sdk/ali-rds/issues/97)) ([0a61be6](https://github.com/ali-sdk/ali-rds/commit/0a61be6048db4df298bfff33c6d23bdcc0119b9a)) +* add unlock/lock tables ([#97](https://github.com/node-modules/myrds/issues/97)) ([4dc3452](https://github.com/node-modules/myrds/commit/4dc3452a375e0c242084e23c6e5f1cb76f1b647d)) +* add unlock/lock tables ([#97](https://github.com/node-modules/myrds/issues/97)) ([0a61be6](https://github.com/node-modules/myrds/commit/0a61be6048db4df298bfff33c6d23bdcc0119b9a)) -## [4.0.3](https://github.com/ali-sdk/ali-rds/compare/v4.0.2...v4.0.3) (2022-12-22) +## [4.0.3](https://github.com/node-modules/myrds/v4.0.2...v4.0.3) (2022-12-22) ### Bug Fixes -* export pool getter from rds client ([#102](https://github.com/ali-sdk/ali-rds/issues/102)) ([4048807](https://github.com/ali-sdk/ali-rds/commit/40488070b8bbae853a75ebe7d82a6cff6c8d071d)) +* export pool getter from rds client ([#102](https://github.com/node-modules/myrds/issues/102)) ([4048807](https://github.com/node-modules/myrds/commit/40488070b8bbae853a75ebe7d82a6cff6c8d071d)) -## [4.0.2](https://github.com/ali-sdk/ali-rds/compare/v4.0.1...v4.0.2) (2022-12-22) +## [4.0.2](https://github.com/node-modules/myrds/v4.0.1...v4.0.2) (2022-12-22) ### Bug Fixes -* should export conn property ([#101](https://github.com/ali-sdk/ali-rds/issues/101)) ([37afa42](https://github.com/ali-sdk/ali-rds/commit/37afa420f3330cbc7a5e6e68da88086339a2a955)) +* should export conn property ([#101](https://github.com/node-modules/myrds/issues/101)) ([37afa42](https://github.com/node-modules/myrds/commit/37afa420f3330cbc7a5e6e68da88086339a2a955)) --- @@ -104,70 +104,70 @@ ================== **fixes** - * [[`add4669`](http://github.com/ali-sdk/ali-rds/commit/add466917422b15deddd434c25595b6f6082bb6b)] - 🐛 FIX: Export db pool stats (#95) (fengmk2 <>) + * [[`add4669`](http://github.com/node-modules/myrds/commit/add466917422b15deddd434c25595b6f6082bb6b)] - 🐛 FIX: Export db pool stats (#95) (fengmk2 <>) 4.0.0 / 2022-12-14 ================== **features** - * [[`6296b5b`](http://github.com/ali-sdk/ali-rds/commit/6296b5b1a0e08bf88097937a0b579a4c90b13a2d)] - 📦 NEW: [BREAKING] Refactor impl base on async/await (#94) (fengmk2 <>) + * [[`6296b5b`](http://github.com/node-modules/myrds/commit/6296b5b1a0e08bf88097937a0b579a4c90b13a2d)] - 📦 NEW: [BREAKING] Refactor impl base on async/await (#94) (fengmk2 <>) 3.4.1 / 2022-12-13 ================== **fixes** - * [[`d983478`](http://github.com/ali-sdk/ali-rds/commit/d983478d40203357c71187c94f44ef3afab0b604)] - fix: handle concurrent transaction (#85) (killa <>) + * [[`d983478`](http://github.com/node-modules/myrds/commit/d983478d40203357c71187c94f44ef3afab0b604)] - fix: handle concurrent transaction (#85) (killa <>) **others** - * [[`61e8e38`](http://github.com/ali-sdk/ali-rds/commit/61e8e38208acf4a9cc1780128063318f7f0e17ac)] - Create codeql.yml (fengmk2 <>) + * [[`61e8e38`](http://github.com/node-modules/myrds/commit/61e8e38208acf4a9cc1780128063318f7f0e17ac)] - Create codeql.yml (fengmk2 <>) 3.4.0 / 2020-07-16 ================== **features** - * [[`2e99ab8`](http://github.com/ali-sdk/ali-rds/commit/2e99ab8ce872b8482fe2b0a29af51a7a99aaff84)] - feat: export sqlstring method (#79) (Haoliang Gao <>) + * [[`2e99ab8`](http://github.com/node-modules/myrds/commit/2e99ab8ce872b8482fe2b0a29af51a7a99aaff84)] - feat: export sqlstring method (#79) (Haoliang Gao <>) 3.3.1 / 2019-04-24 ================== **fixes** - * [[`52147de`](git@github.com:ali-sdk/ali-rds/commit/52147de9d7405b02efcf84ef28a11a4097675972)] - fix: query parameters are not allowed to be included in where (#67) (Hoyt <>) + * [[`52147de`](git@github.com:node-modules/myrds/commit/52147de9d7405b02efcf84ef28a11a4097675972)] - fix: query parameters are not allowed to be included in where (#67) (Hoyt <>) **others** - * [[`0f9f23b`](git@github.com:ali-sdk/ali-rds/commit/0f9f23bbd935650a1440537b18aaa982a6db2d44)] - chore: remove node 4 in ci (dead-horse <>) + * [[`0f9f23b`](git@github.com:node-modules/myrds/commit/0f9f23bbd935650a1440537b18aaa982a6db2d44)] - chore: remove node 4 in ci (dead-horse <>) 3.3.0 / 2018-12-11 ================== **features** - * [[`0d4d4ab`](http://github.com/ali-sdk/ali-rds/commit/0d4d4ab99a7cd655180f22d4d95e3cfef8c8714b)] - feat: where condition support NULL value (#60) (fengmk2 <>) + * [[`0d4d4ab`](http://github.com/node-modules/myrds/commit/0d4d4ab99a7cd655180f22d4d95e3cfef8c8714b)] - feat: where condition support NULL value (#60) (fengmk2 <>) 3.2.0 / 2018-11-19 ================== **features** - * [[`b227bc1`](http://github.com/ali-sdk/ali-rds/commit/b227bc12e5c6252264d4761b72f915b73d53c688)] - feat: support doomed transaction scope on test cases (#58) (AngrySean <>) + * [[`b227bc1`](http://github.com/node-modules/myrds/commit/b227bc12e5c6252264d4761b72f915b73d53c688)] - feat: support doomed transaction scope on test cases (#58) (AngrySean <>) 3.1.0 / 2018-09-30 ================== **features** - * [[`859d818`](http://github.com/ali-sdk/ali-rds/commit/859d818d7e327d1ff590d363dfbf3135d8c90454)] - feat: update multiple rows (#55) (Hang Jiang <>) + * [[`859d818`](http://github.com/node-modules/myrds/commit/859d818d7e327d1ff590d363dfbf3135d8c90454)] - feat: update multiple rows (#55) (Hang Jiang <>) **fixes** - * [[`db6d596`](http://github.com/ali-sdk/ali-rds/commit/db6d59616f4b5083142bed554fb104c1b5a7c14e)] - fix: add default value now() of `gmt_modified` and `gmt_create` (#56) (Hang Jiang <>) + * [[`db6d596`](http://github.com/node-modules/myrds/commit/db6d59616f4b5083142bed554fb104c1b5a7c14e)] - fix: add default value now() of `gmt_modified` and `gmt_create` (#56) (Hang Jiang <>) **others** - * [[`db3524c`](http://github.com/ali-sdk/ali-rds/commit/db3524c8da7f8b32291695a2fc5497ea8fddd155)] - doc: add object arguments in readme (#52) (凯 方 <>) + * [[`db3524c`](http://github.com/node-modules/myrds/commit/db3524c8da7f8b32291695a2fc5497ea8fddd155)] - doc: add object arguments in readme (#52) (凯 方 <>) 3.0.1 / 2017-09-26 ================== **fixes** - * [[`5ca4489`](http://github.com/ali-sdk/ali-rds/commit/5ca4489b903923302c81a8c9c8ac94c0afbce819)] - fix: don't redefined sqlstring.escape (#39) (Yiyu He <>) + * [[`5ca4489`](http://github.com/node-modules/myrds/commit/5ca4489b903923302c81a8c9c8ac94c0afbce819)] - fix: don't redefined sqlstring.escape (#39) (Yiyu He <>) **others** - * [[`004713c`](http://github.com/ali-sdk/ali-rds/commit/004713cf9a4aa2da84a9f02348996b1a8ec82430)] - doc: fix typo 'Can\'t not' => 'Can not' (#34) (dreamswhite <>) + * [[`004713c`](http://github.com/node-modules/myrds/commit/004713cf9a4aa2da84a9f02348996b1a8ec82430)] - doc: fix typo 'Can\'t not' => 'Can not' (#34) (dreamswhite <>) 3.0.0 / 2017-04-02 ================== @@ -219,7 +219,7 @@ ================== * feat: add *beginTransactionScope(scope) - * docs: require ali-rds + * docs: require myrds 2.0.0 / 2015-06-08 ================== diff --git a/LICENSE b/LICENSE index 8f3addb..d4efbc1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ This software is licensed under the MIT License. -Copyright (c) 2015-present ali-sdk and other contributors +Copyright (c) 2024-present myrds and other contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 1f0dc05..da46dee 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,18 @@ -# ali-rds +# myrds [![NPM version][npm-image]][npm-url] -[![Node.js CI](https://github.com/ali-sdk/ali-rds/actions/workflows/nodejs.yml/badge.svg)](https://github.com/ali-sdk/ali-rds/actions/workflows/nodejs.yml) +[![Node.js CI](https://github.com/node-modules/myrds/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/myrds/actions/workflows/nodejs.yml) [![Test coverage][codecov-image]][codecov-url] [![npm download][download-image]][download-url] -[npm-image]: https://img.shields.io/npm/v/ali-rds.svg?style=flat-square -[npm-url]: https://npmjs.org/package/ali-rds -[codecov-image]: https://codecov.io/github/ali-sdk/ali-rds/coverage.svg?branch=master -[codecov-url]: https://codecov.io/github/ali-sdk/ali-rds?branch=master -[download-image]: https://img.shields.io/npm/dm/ali-rds.svg?style=flat-square -[download-url]: https://npmjs.org/package/ali-rds +[npm-image]: https://img.shields.io/npm/v/myrds.svg?style=flat-square +[npm-url]: https://npmjs.org/package/myrds +[codecov-image]: https://codecov.io/github/node-modules/myrds/coverage.svg?branch=master +[codecov-url]: https://codecov.io/github/node-modules/myrds?branch=master +[download-image]: https://img.shields.io/npm/dm/myrds.svg?style=flat-square +[download-url]: https://npmjs.org/package/myrds -Aliyun RDS client. +MY RDS client. RDS, Relational Database Service. Equal to well know Amazon [RDS](http://aws.amazon.com/rds/). Support `MySQL` protocol only. @@ -22,7 +22,7 @@ Support `MySQL` protocol only. ### Create RDS instance ```ts -import { RDSClient } from 'ali-rds'; +import { RDSClient } from 'myrds'; const db = new RDSClient({ host: 'your-rds-address.mysql.rds.aliyuncs.com', diff --git a/package.json b/package.json index f598fb5..0ad37a8 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "ali-rds", - "version": "6.3.0", - "description": "Aliyun RDS client", + "name": "myrds", + "version": "7.0.0", + "description": "My RDS Client", "main": "lib/client.js", "types": "lib/client.d.ts", "files": [ @@ -17,8 +17,7 @@ "prepublishOnly": "npm run clean && npm run build" }, "dependencies": { - "@types/mysql": "^2.15.21", - "mysql": "^2.18.1" + "mysql2": "^3.9.1" }, "devDependencies": { "@eggjs/tsconfig": "^1.3.2", @@ -31,17 +30,18 @@ "mm": "^3.3.0", "typescript": "^5.1.3" }, - "homepage": "https://github.com/ali-sdk/ali-rds", + "homepage": "https://github.com/node-modules/myrds", "repository": { "type": "git", - "url": "git://github.com/ali-sdk/ali-rds.git" + "url": "git://github.com/node-modules/myrds.git" }, "bugs": { - "url": "https://github.com/ali-sdk/ali-rds/issues" + "url": "https://github.com/node-modules/myrds/issues" }, "keywords": [ - "ali-rds", - "mysql" + "myrds", + "mysql", + "mysql2" ], "engines": { "node": ">= 16.17.0" diff --git a/src/PoolConfig.ts b/src/PoolConfig.ts index ece1549..cb9a335 100644 --- a/src/PoolConfig.ts +++ b/src/PoolConfig.ts @@ -1,19 +1,34 @@ -import MySQLPoolConfig from 'mysql/lib/PoolConfig'; -import type { PoolConfig, ConnectionConfig } from 'mysql'; +import { ConnectionConfig } from 'mysql2'; +import type { PoolOptions } from 'mysql2'; import type { GetConnectionConfig } from './types'; -export class RDSPoolConfig extends MySQLPoolConfig { +export class RDSPoolConfig { #getConnectionConfig: GetConnectionConfig; + #options: PoolOptions; - constructor(options: PoolConfig, getConnectionConfig: GetConnectionConfig) { - super(options); + waitForConnections: boolean; + connectionLimit: number; + maxIdle: number; + idleTimeout: number; + queueLimit: number; + + constructor(options: PoolOptions, getConnectionConfig: GetConnectionConfig) { + this.#options = options; this.#getConnectionConfig = getConnectionConfig; + + // mysql2 has not exports ConnectionConfig + // so impl it mauanlly + this.waitForConnections = options.waitForConnections === undefined ? true : Boolean(options.waitForConnections); + this.connectionLimit = isNaN(options.connectionLimit!) ? 10 : Number(options.connectionLimit); + this.maxIdle = isNaN(options.maxIdle!) ? this.connectionLimit : Number(options.maxIdle); + this.idleTimeout = isNaN(options.idleTimeout!) ? 60000 : Number(options.idleTimeout); + this.queueLimit = isNaN(options.queueLimit!) ? 0 : Number(options.queueLimit); } - newConnectionConfig(): ConnectionConfig { - return { - ...super.newConnectionConfig(), + get connectionConfig(): any { + return new ConnectionConfig({ + ...this.#options, ...this.#getConnectionConfig(), - }; + }); } } diff --git a/src/client.ts b/src/client.ts index 36a7155..e096c18 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,15 +1,15 @@ import { AsyncLocalStorage } from 'node:async_hooks'; import { promisify } from 'node:util'; -import mysql from 'mysql'; -import type { Pool } from 'mysql'; +import mysql, { Pool } from 'mysql2'; +import type { PoolOptions } from 'mysql2'; import type { PoolConnectionPromisify, RDSClientOptions, TransactionContext, TransactionScope } from './types'; import { Operator } from './operator'; import { RDSConnection } from './connection'; import { RDSTransaction } from './transaction'; -import { RDSPoolConfig } from './PoolConfig'; import literals from './literals'; import channels from './channels'; import type { ConnectionMessage, ConnectionEnqueueMessage } from './channels'; +import { RDSPoolConfig } from './PoolConfig'; interface PoolPromisify extends Omit { query(sql: string): Promise; @@ -19,6 +19,9 @@ interface PoolPromisify extends Omit { _allConnections: any[]; _freeConnections: any[]; _connectionQueue: any[]; + config: Pool['config'] & { + connectionConfig: PoolOptions; + }; } export class RDSClient extends Operator { @@ -40,13 +43,7 @@ export class RDSClient extends Operator { const { connectionStorage, connectionStorageKey, ...mysqlOptions } = options; // get connection options from getConnectionConfig method every time if (mysqlOptions.getConnectionConfig) { - // eslint-disable-next-line @typescript-eslint/no-var-requires - const MySQLPool = require('mysql/lib/Pool'); - this.#pool = new MySQLPool({ - config: new RDSPoolConfig(mysqlOptions, mysqlOptions.getConnectionConfig), - }); - // override _needsChangeUser to return false - (this.#pool as any)._needsChangeUser = () => false; + this.#pool = new Pool({ config: new RDSPoolConfig(mysqlOptions, mysqlOptions.getConnectionConfig) } as any) as unknown as PoolPromisify; } else { this.#pool = mysql.createPool(mysqlOptions) as unknown as PoolPromisify; } @@ -99,13 +96,11 @@ export class RDSClient extends Operator { } get stats() { - const acquiringConnections = this.#pool._acquiringConnections.length; const allConnections = this.#pool._allConnections.length; const freeConnections = this.#pool._freeConnections.length; const connectionQueue = this.#pool._connectionQueue.length; - const busyConnections = allConnections - freeConnections - acquiringConnections; + const busyConnections = allConnections - freeConnections; return { - acquiringConnections, allConnections, freeConnections, connectionQueue, diff --git a/src/operator.ts b/src/operator.ts index d86a5f3..95d1067 100644 --- a/src/operator.ts +++ b/src/operator.ts @@ -328,7 +328,7 @@ export abstract class Operator { * {id: 3, name: 'fengmk23', email: 'm@fengmk23.com'}, * ]) * - * UPDATE `ali-sdk-test-user` SET + * UPDATE `myrds-test-user` SET * `name` = * CASE * WHEN `id` = 1 THEN 'fengmk21' diff --git a/src/sqlstring.ts b/src/sqlstring.ts index b17522d..edc6659 100644 --- a/src/sqlstring.ts +++ b/src/sqlstring.ts @@ -1,4 +1,5 @@ -import SqlString from 'mysql/lib/protocol/SqlString'; +import SqlString from 'sqlstring'; +import mysql2 from 'mysql2'; import { Literal } from './literals'; const kEscape = Symbol('kEscape'); @@ -6,7 +7,7 @@ const kEscape = Symbol('kEscape'); if (!SqlString[kEscape]) { SqlString[kEscape] = SqlString.escape; - SqlString.escape = (val: any, stringifyObjects?: boolean, timeZone?: string) => { + SqlString.escape = mysql2.escape = (val: any, stringifyObjects?: boolean, timeZone?: string) => { if (val instanceof Literal) { return val.toString(); } diff --git a/src/types.ts b/src/types.ts index 06356af..ab4bfb0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,13 +1,13 @@ import { AsyncLocalStorage } from 'node:async_hooks'; -import type { PoolConnection, PoolConfig, ConnectionConfig } from 'mysql'; +import type { PoolConnection, PoolOptions, ConnectionConfig } from 'mysql2'; import { RDSTransaction } from './transaction'; -export type GetConnectionConfig = () => ConnectionConfig; +export type GetConnectionConfig = () => Partial; -export interface RDSClientOptions extends PoolConfig { +export interface RDSClientOptions extends PoolOptions { connectionStorageKey?: string; connectionStorage?: AsyncLocalStorage>; - getConnectionConfig?: GetConnectionConfig + getConnectionConfig?: GetConnectionConfig; } export interface PoolConnectionPromisify extends Omit { diff --git a/test/PoolConfig.test.ts b/test/PoolConfig.test.ts index f82e848..97da010 100644 --- a/test/PoolConfig.test.ts +++ b/test/PoolConfig.test.ts @@ -9,7 +9,7 @@ import type { ConnectionMessage, QueryEndMessage } from '../src/channels'; describe('test/PoolConfig.test.ts', () => { const prefix = 'prefix-PoolConfig' + process.version + '-'; - const table = 'ali-sdk-test-user'; + const table = 'myrds-test-user'; let db: RDSClient; let index = 0; let newConnectionCount = 0; @@ -78,10 +78,8 @@ describe('test/PoolConfig.test.ts', () => { }); describe('new RDSClient(options.getConnectionConfig)', () => { - it('should get connection config from newConnectionConfig()', async () => { - assert.equal(db.pool.config.connectionConfig.database, undefined); - assert.equal(index, 1); - assert.equal((db.pool.config as any).newConnectionConfig().database, config.database); + it('should get connection config from connectionConfig', async () => { + assert.equal(db.pool.config.database, undefined); assert.equal(index, 2); }); diff --git a/test/client.test.ts b/test/client.test.ts index e1160f1..b2a67ec 100644 --- a/test/client.test.ts +++ b/test/client.test.ts @@ -17,7 +17,7 @@ const mmSpy = any>(target: T) => target as MockMa describe('test/client.test.ts', () => { const prefix = 'prefix-' + process.version + '-'; - const table = 'ali-sdk-test-user'; + const table = 'myrds-test-user'; let db: RDSClient; before(async () => { db = new RDSClient(config); @@ -157,14 +157,14 @@ describe('test/client.test.ts', () => { }, new Error('No lock_type provided while trying to lock table `xxxx`')); }); it('should unlock tables', async () => { - await db.lockOne('ali-sdk-test-user', 'READ', 't'); + await db.lockOne('myrds-test-user', 'READ', 't'); // error thrown: when table locked with alias, you can only query with the alias. await assert.rejects(async () => { - await db.query('select * from `ali-sdk-test-user` limit 1;'); + await db.query('select * from `myrds-test-user` limit 1;'); }); await db.unlock(); // recovered after unlock. - await db.query('select * from `ali-sdk-test-user` limit 1;'); + await db.query('select * from `myrds-test-user` limit 1;'); }); }); @@ -289,14 +289,14 @@ describe('test/client.test.ts', () => { const conn = await db.getConnection(); try { await conn.beginTransaction(); - await conn.lockOne('ali-sdk-test-user', 'READ', 't'); + await conn.lockOne('myrds-test-user', 'READ', 't'); // error thrown: when table locked with alias, you can only query with the alias. await assert.rejects(async () => { - await conn.query('select * from `ali-sdk-test-user` limit 1;'); + await conn.query('select * from `myrds-test-user` limit 1;'); }); await conn.unlock(); // recovered after unlock. - await conn.query('select * from `ali-sdk-test-user` limit 1;'); + await conn.query('select * from `myrds-test-user` limit 1;'); } catch (err) { conn.release(); throw err; @@ -1017,7 +1017,7 @@ describe('test/client.test.ts', () => { }); throw new Error('should not run this'); } catch (error) { - assert(error.message.includes("ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: 'now()' for column")); + assert(error.message.includes("Incorrect datetime value: 'now()' for column")); } result = await db.update(table, { @@ -1340,7 +1340,6 @@ describe('test/client.test.ts', () => { describe('get stats()', () => { it('should get client stats', async () => { const stats = db.stats; - assert.equal(typeof stats.acquiringConnections, 'number'); assert.equal(typeof stats.allConnections, 'number'); assert.equal(typeof stats.freeConnections, 'number'); assert.equal(typeof stats.connectionQueue, 'number'); @@ -1413,7 +1412,7 @@ describe('test/client.test.ts', () => { counter2After++; }); await db.query('select * from ?? limit 10', [ table ]); - assert.equal(lastSql, 'select * from `ali-sdk-test-user` limit 10'); + assert.equal(lastSql, 'select * from `myrds-test-user` limit 10'); assert.equal(lastArgs[0], lastSql); assert.equal(Array.isArray(lastArgs[1]), true); assert.equal(count, 1); @@ -1426,7 +1425,7 @@ describe('test/client.test.ts', () => { values(?, ?, now(), now())`, [ table, prefix + 'beginTransactionScope1', prefix + 'm@beginTransactionScope1.com' ]); }); - assert.equal(lastSql, 'insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified)\n' + + assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' + ` values('${prefix}beginTransactionScope1', '${prefix}m@beginTransactionScope1.com', now(), now())`); assert.equal(lastArgs[0], lastSql); assert.equal(lastArgs[1].affectedRows, 1); @@ -1437,7 +1436,7 @@ describe('test/client.test.ts', () => { values(?, ?, now(), now())`, [ table, prefix + 'beginDoomedTransactionScope1', prefix + 'm@beginDoomedTransactionScope1.com' ]); }); - assert.equal(lastSql, 'insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified)\n' + + assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' + ` values('${prefix}beginDoomedTransactionScope1', '${prefix}m@beginDoomedTransactionScope1.com', now(), now())`); assert.equal(lastArgs[0], lastSql); assert.equal(lastArgs[1].affectedRows, 1); @@ -1449,7 +1448,7 @@ describe('test/client.test.ts', () => { values(?, ?, now(), now())`, [ table, prefix + 'transaction1', prefix + 'm@transaction1.com' ]); await conn.commit(); - assert.equal(lastSql, 'insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified)\n' + + assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' + ` values('${prefix}transaction1', '${prefix}m@transaction1.com', now(), now())`); assert.equal(lastArgs[0], lastSql); assert.equal(lastArgs[1].affectedRows, 1); diff --git a/test/rds_init.sql b/test/rds_init.sql index d044a99..0218a91 100644 --- a/test/rds_init.sql +++ b/test/rds_init.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS `ali-sdk-test-user` ( +CREATE TABLE IF NOT EXISTS `myrds-test-user` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'primary key', `gmt_create` datetime NOT NULL COMMENT 'create time' DEFAULT NOW(), `gmt_modified` datetime NOT NULL COMMENT 'modified time' DEFAULT NOW(), diff --git a/test/sqlstring.test.ts b/test/sqlstring.test.ts index d1e037a..a39510f 100644 --- a/test/sqlstring.test.ts +++ b/test/sqlstring.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import mysql from 'mysql'; +import mysql from 'mysql2'; import { SqlString } from '../src/sqlstring'; import literals, { Literal } from '../src/literals'; diff --git a/tsconfig.json b/tsconfig.json index c2c34af..2e5a65d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "target": "ES2022" }, "include": [ - "src" + "src", + "typings/mysql/index.d.ts" ], } diff --git a/typings/mysql/index.d.ts b/typings/mysql/index.d.ts new file mode 100644 index 0000000..b9e7d2f --- /dev/null +++ b/typings/mysql/index.d.ts @@ -0,0 +1,12 @@ +import EventEmitter from 'events'; +import * as mysql2 from 'mysql2'; +import { Pool } from 'mysql2'; + +declare module "mysql2" { + module.exports = mysql2; + + export class Pool extends EventEmitter {} + export class ConnectionConfig { + constructor(options: mysql2.ConnectionOptions); + } +} \ No newline at end of file