forked from ali-sdk/ali-rds
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dynamic retrieval of database connection configuration (ali-sdk…
…#110) test fix: Node.js >= 20 will return localhost ipv6 address
- Loading branch information
Showing
7 changed files
with
127 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import MySQLPoolConfig from 'mysql/lib/PoolConfig'; | ||
import type { PoolConfig, ConnectionConfig } from 'mysql'; | ||
import type { GetConnectionConfig } from './types'; | ||
|
||
export class RDSPoolConfig extends MySQLPoolConfig { | ||
#getConnectionConfig: GetConnectionConfig; | ||
|
||
constructor(options: PoolConfig, getConnectionConfig: GetConnectionConfig) { | ||
super(options); | ||
this.#getConnectionConfig = getConnectionConfig; | ||
} | ||
|
||
newConnectionConfig(): ConnectionConfig { | ||
return { | ||
...super.newConnectionConfig(), | ||
...this.#getConnectionConfig(), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import mm from 'mm'; | ||
import config from './config'; | ||
import { RDSClient } from '../src/client'; | ||
|
||
describe('test/PoolConfig.test.ts', () => { | ||
const prefix = 'prefix-PoolConfig' + process.version + '-'; | ||
const table = 'ali-sdk-test-user'; | ||
let db: RDSClient; | ||
let index = 0; | ||
let newConnectionCount = 0; | ||
before(async () => { | ||
db = new RDSClient({ | ||
// test getConnectionConfig | ||
connectionLimit: 2, | ||
getConnectionConfig() { | ||
console.log('get connection config index: %d', ++index); | ||
return config; | ||
}, | ||
}); | ||
db.pool.on('acquire', conn => { | ||
console.log('acquire connection %o', conn.threadId); | ||
}); | ||
db.pool.on('connection', conn => { | ||
newConnectionCount++; | ||
console.log('new connection %o', conn.threadId); | ||
}); | ||
db.pool.on('enqueue', () => { | ||
console.log('Waiting for available connection slot'); | ||
}); | ||
db.pool.on('release', conn => { | ||
console.log('release connection %o', conn.threadId); | ||
}); | ||
|
||
try { | ||
const sql = await fs.readFile(path.join(__dirname, 'rds_init.sql'), 'utf-8'); | ||
await db.query(sql); | ||
} catch (err) { | ||
console.log('init table error: %s', err); | ||
} | ||
await db.query('delete from ?? where name like ?', [ table, prefix + '%' ]); | ||
}); | ||
|
||
after(async () => { | ||
return await db.end(); | ||
}); | ||
|
||
afterEach(() => { | ||
mm.restore(); | ||
}); | ||
|
||
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, 'test'); | ||
assert.equal(index, 2); | ||
}); | ||
|
||
it('should connect rds success', async () => { | ||
const rows = await db.query('show tables'); | ||
// console.log(rows); | ||
assert(rows); | ||
assert(Array.isArray(rows)); | ||
assert.equal(index, 2); | ||
const results = await Promise.all([ | ||
db.query('show tables'), | ||
db.query('show tables'), | ||
db.query('show tables'), | ||
]); | ||
assert.equal(results.length, 3); | ||
assert(results[0]); | ||
assert(Array.isArray(results[0])); | ||
assert(results[1]); | ||
assert(Array.isArray(results[1])); | ||
assert(results[2]); | ||
assert(Array.isArray(results[2])); | ||
assert.equal(index, 3); | ||
assert.equal(newConnectionCount, 2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters