From 631d4e8c56a00f719d9bbc763a981b43fc232eb5 Mon Sep 17 00:00:00 2001 From: axiosleo Date: Mon, 13 Oct 2025 10:36:38 +0800 Subject: [PATCH 1/2] fix(client): enhance createPool function to handle closed pools and add corresponding tests --- src/client.js | 8 ++++- tests/client.tests.js | 76 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/client.tests.js diff --git a/src/client.js b/src/client.js index 48ad579..635e9bf 100644 --- a/src/client.js +++ b/src/client.js @@ -70,7 +70,13 @@ const createPool = (options, name = null) => { const key = name ? name : `${options.host}:${options.port}:${options.user}:${options.password}:${options.database}`; if (clients[key]) { - return clients[key]; + const existingPool = clients[key]; + + if (existingPool._closed) { + delete clients[key]; + } else { + return existingPool; + } } const pool = mysql.createPool(options); clients[key] = pool; diff --git a/tests/client.tests.js b/tests/client.tests.js new file mode 100644 index 0000000..d85f62f --- /dev/null +++ b/tests/client.tests.js @@ -0,0 +1,76 @@ +'use strict'; + +/** + * @type {Chai.ExpectStatic} + */ +let expect = null; +const { createPool } = require('../src/client'); + +describe('client test case', () => { + before(async function () { + const chai = await import('chai'); + expect = chai.expect; + }); + + it('should create new pool when existing pool is closed', () => { + const options = { + host: 'localhost', + port: 3306, + user: 'test', + password: 'test', + database: 'test_db', + connectionLimit: 10 + }; + + // 创建第一个 pool + const pool1 = createPool(options, 'test_pool'); + expect(pool1).to.not.be.null; + + // 模拟 pool 被关闭 + pool1._closed = true; + + // 再次调用 createPool,应该创建新的 pool + const pool2 = createPool(options, 'test_pool'); + expect(pool2).to.not.be.null; + expect(pool2).to.not.equal(pool1); // 应该是不同的实例 + expect(pool2._closed).to.not.be.true; // 新 pool 应该是活跃的 + }); + + it('should return existing pool when pool is active', () => { + const options = { + host: 'localhost', + port: 3306, + user: 'test', + password: 'test', + database: 'test_db2', + connectionLimit: 10 + }; + + // 创建第一个 pool + const pool1 = createPool(options, 'test_pool2'); + expect(pool1).to.not.be.null; + + // 再次调用 createPool,应该返回相同的 pool + const pool2 = createPool(options, 'test_pool2'); + expect(pool2).to.equal(pool1); // 应该是相同的实例 + }); + + it('should handle pool without name parameter', () => { + const options = { + host: 'localhost', + port: 3306, + user: 'test', + password: 'test', + database: 'test_db3', + connectionLimit: 10 + }; + + // 创建 pool 不指定名称 + const pool1 = createPool(options); + expect(pool1).to.not.be.null; + + // 再次调用应该返回相同的 pool + const pool2 = createPool(options); + expect(pool2).to.equal(pool1); + }); +}); From 36b82cfc02848e819d9df2a413fd3be7e04e3193 Mon Sep 17 00:00:00 2001 From: axiosleo Date: Mon, 13 Oct 2025 10:37:00 +0800 Subject: [PATCH 2/2] fix(builder): update import path for ManageBuilderOptions in createTable method --- src/builder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builder.js b/src/builder.js index 6ce4abb..02833fc 100644 --- a/src/builder.js +++ b/src/builder.js @@ -488,7 +488,7 @@ class ManageSQLBuilder extends Builder { } /** - * @param {import('../index').ManageBuilderOptions} options + * @param {import('./migration').ManageBuilderOptions} options */ createTable(options) { _validate(options, {