Skip to content

Commit 0d1569d

Browse files
feat: use sql params (#14)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Query lifecycle events now include optional parameter values so observers can access bound parameters. * **Refactor** * Query handling now consistently accepts optional parameter values across connection, operator, and transaction layers, enabling parameterized queries and more flexible execution. * Public method signatures updated to support passing query values. * **Tests** * Test suite updated to use placeholder-based parameterized SQL, aligning assertions with the new parameter handling. <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: killa <killa07071201@gmail.com> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 314b425 commit 0d1569d

File tree

6 files changed

+23
-22
lines changed

6 files changed

+23
-22
lines changed

src/channels.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ export interface ConnectionEnqueueMessage {
2424

2525
export interface QueryStartMessage {
2626
connection: PoolConnectionPromisify;
27+
values?: object | any[];
2728
sql: string;
2829
}
2930

3031
export interface QueryEndMessage {
3132
connection: PoolConnectionPromisify;
3233
sql: string;
34+
values?: object | any[];
3335
duration: number;
3436
error?: Error;
3537
}

src/connection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export class RDSConnection extends Operator {
3232
return this.conn.release();
3333
}
3434

35-
async _query(sql: string) {
36-
return await this.conn.query(sql);
35+
async _query(sql: string, values?: object | any[]) {
36+
return await this.conn.query(sql, values);
3737
}
3838

3939
async beginTransaction() {

src/operator.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@ export abstract class Operator {
7575

7676
async query<T = any>(sql: string, values?: object | any[]): Promise<T> {
7777
// query(sql, values)
78-
if (values) {
79-
sql = this.format(sql, values);
80-
}
8178
if (this.beforeQueryHandlers.length > 0) {
8279
for (const beforeQueryHandler of this.beforeQueryHandlers) {
83-
const newSql = beforeQueryHandler(sql);
80+
const newSql = beforeQueryHandler(sql, values);
8481
if (newSql) {
8582
sql = newSql;
8683
}
@@ -95,10 +92,11 @@ export abstract class Operator {
9592
let lastError: Error | undefined;
9693
channels.queryStart.publish({
9794
sql,
95+
values,
9896
connection: this.#connection,
9997
} as QueryStartMessage);
10098
try {
101-
rows = await this._query(sql);
99+
rows = await this._query(sql, values);
102100
if (Array.isArray(rows)) {
103101
debug('[connection#%s] query get %o rows', this.threadId, rows.length);
104102
} else {
@@ -115,12 +113,13 @@ export abstract class Operator {
115113
channels.queryEnd.publish({
116114
sql,
117115
connection: this.#connection,
116+
values,
118117
duration,
119118
error: lastError,
120119
} as QueryEndMessage);
121120
if (this.afterQueryHandlers.length > 0) {
122121
for (const afterQueryHandler of this.afterQueryHandlers) {
123-
afterQueryHandler(sql, rows, duration, lastError);
122+
afterQueryHandler(sql, rows, duration, lastError, values);
124123
}
125124
}
126125
}
@@ -132,7 +131,7 @@ export abstract class Operator {
132131
}
133132

134133
// eslint-disable-next-line @typescript-eslint/no-unused-vars
135-
protected async _query(_sql: string): Promise<any> {
134+
protected async _query(_sql: string, _values?: object | any[]): Promise<any> {
136135
throw new Error('SubClass must impl this');
137136
}
138137

src/transaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export class RDSTransaction extends Operator {
3636
}
3737
}
3838

39-
protected async _query(sql: string) {
39+
protected async _query(sql: string, values?: object | any[]) {
4040
this.#check();
41-
return await this.conn!._query(sql);
41+
return await this.conn!._query(sql, values);
4242
}
4343

4444
#check() {

src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface RDSClientOptions extends PoolOptions {
1313
}
1414

1515
export interface PoolConnectionPromisify extends Omit<PoolConnection, 'query'> {
16-
query(sql: string): Promise<any>;
16+
query(sql: string, values?: any | any[] | { [param: string]: any }): Promise<any>;
1717
beginTransaction(): Promise<void>;
1818
commit(): Promise<void>;
1919
rollback(): Promise<void>;
@@ -63,8 +63,8 @@ export type LockTableOption = {
6363
tableAlias: string;
6464
};
6565

66-
export type BeforeQueryHandler = (sql: string) => string | undefined | void;
67-
export type AfterQueryHandler = (sql: string, result: any, execDuration: number, err?: Error) => void;
66+
export type BeforeQueryHandler = (sql: string, values?: object | any[]) => string | undefined | void;
67+
export type AfterQueryHandler = (sql: string, result: any, execDuration: number, err?: Error, values?: object | any[]) => void;
6868

6969
export type TransactionContext = Record<PropertyKey, RDSTransaction | null>;
7070
export type TransactionScope = (transaction: RDSTransaction) => Promise<any>;

test/client.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ describe('test/client.test.ts', () => {
357357
[ table, prefix + 'm@fengmk2.com' ]);
358358
assert.deepEqual(mockLogs, [
359359
'show tables',
360-
`select * from \`${table}\` where email = '${prefix + 'm@fengmk2.com'}' limit 1`,
360+
'select * from ?? where email = ? limit 1',
361361
]);
362362
});
363363
});
@@ -1478,7 +1478,7 @@ describe('test/client.test.ts', () => {
14781478
counter2After++;
14791479
});
14801480
await db.query('select * from ?? limit 10', [ table ]);
1481-
assert.equal(lastSql, 'select * from `myrds-test-user` limit 10');
1481+
assert.equal(lastSql, 'select * from ?? limit 10');
14821482
assert.equal(lastArgs[0], lastSql);
14831483
assert.equal(Array.isArray(lastArgs[1]), true);
14841484
assert.equal(count, 1);
@@ -1491,8 +1491,8 @@ describe('test/client.test.ts', () => {
14911491
values(?, ?, now(), now())`,
14921492
[ table, prefix + 'beginTransactionScope1', prefix + 'm@beginTransactionScope1.com' ]);
14931493
});
1494-
assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' +
1495-
` values('${prefix}beginTransactionScope1', '${prefix}m@beginTransactionScope1.com', now(), now())`);
1494+
assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' +
1495+
' values(?, ?, now(), now())');
14961496
assert.equal(lastArgs[0], lastSql);
14971497
assert.equal(lastArgs[1].affectedRows, 1);
14981498
assert.equal(count, 2);
@@ -1502,8 +1502,8 @@ describe('test/client.test.ts', () => {
15021502
values(?, ?, now(), now())`,
15031503
[ table, prefix + 'beginDoomedTransactionScope1', prefix + 'm@beginDoomedTransactionScope1.com' ]);
15041504
});
1505-
assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' +
1506-
` values('${prefix}beginDoomedTransactionScope1', '${prefix}m@beginDoomedTransactionScope1.com', now(), now())`);
1505+
assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' +
1506+
' values(?, ?, now(), now())');
15071507
assert.equal(lastArgs[0], lastSql);
15081508
assert.equal(lastArgs[1].affectedRows, 1);
15091509
assert.equal(count, 3);
@@ -1514,8 +1514,8 @@ describe('test/client.test.ts', () => {
15141514
values(?, ?, now(), now())`,
15151515
[ table, prefix + 'transaction1', prefix + 'm@transaction1.com' ]);
15161516
await conn.commit();
1517-
assert.equal(lastSql, 'insert into `myrds-test-user`(name, email, gmt_create, gmt_modified)\n' +
1518-
` values('${prefix}transaction1', '${prefix}m@transaction1.com', now(), now())`);
1517+
assert.equal(lastSql, 'insert into ??(name, email, gmt_create, gmt_modified)\n' +
1518+
' values(?, ?, now(), now())');
15191519
assert.equal(lastArgs[0], lastSql);
15201520
assert.equal(lastArgs[1].affectedRows, 1);
15211521
assert.equal(count, 4);

0 commit comments

Comments
 (0)