-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support custom query lifecricle (#104)
- Loading branch information
Showing
6 changed files
with
177 additions
and
3 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
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 |
---|---|---|
|
@@ -1203,4 +1203,59 @@ describe('test/client.test.ts', () => { | |
await db2.end(); | ||
}); | ||
}); | ||
|
||
describe('query lifecricle work', () => { | ||
it('should work on client and transactions', async () => { | ||
const db = new RDSClient(config); | ||
let count = 0; | ||
let lastSql = ''; | ||
db.beforeQuery(sql => { | ||
count++; | ||
lastSql = sql; | ||
}); | ||
let lastArgs: any; | ||
db.afterQuery((...args) => { | ||
lastArgs = args; | ||
}); | ||
await db.query('select * from ?? limit 10', [ table ]); | ||
assert.equal(lastSql, 'select * from `ali-sdk-test-user` limit 10'); | ||
assert.equal(lastArgs[0], lastSql); | ||
assert.equal(Array.isArray(lastArgs[1]), true); | ||
assert.equal(count, 1); | ||
|
||
await db.beginTransactionScope(async conn => { | ||
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified) | ||
values(?, ?, now(), now())`, | ||
[ table, prefix + 'beginTransactionScope1', prefix + '[email protected]' ]); | ||
}); | ||
assert.equal(lastSql, 'insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified)\n' + | ||
` values('${prefix}beginTransactionScope1', '${prefix}[email protected]', now(), now())`); | ||
assert.equal(lastArgs[0], lastSql); | ||
assert.equal(lastArgs[1].affectedRows, 1); | ||
assert.equal(count, 2); | ||
|
||
await db.beginDoomedTransactionScope(async conn => { | ||
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified) | ||
values(?, ?, now(), now())`, | ||
[ table, prefix + 'beginDoomedTransactionScope1', prefix + '[email protected]' ]); | ||
}); | ||
assert.equal(lastSql, 'insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified)\n' + | ||
` values('${prefix}beginDoomedTransactionScope1', '${prefix}[email protected]', now(), now())`); | ||
assert.equal(lastArgs[0], lastSql); | ||
assert.equal(lastArgs[1].affectedRows, 1); | ||
assert.equal(count, 3); | ||
|
||
const conn = await db.getConnection(); | ||
await conn.beginTransaction(); | ||
await conn.query(`insert into ??(name, email, gmt_create, gmt_modified) | ||
values(?, ?, now(), now())`, | ||
[ table, prefix + 'transaction1', prefix + '[email protected]' ]); | ||
await conn.commit(); | ||
assert.equal(lastSql, 'insert into `ali-sdk-test-user`(name, email, gmt_create, gmt_modified)\n' + | ||
` values('${prefix}transaction1', '${prefix}[email protected]', now(), now())`); | ||
assert.equal(lastArgs[0], lastSql); | ||
assert.equal(lastArgs[1].affectedRows, 1); | ||
assert.equal(count, 4); | ||
}); | ||
}); | ||
}); |
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