Skip to content

Commit 9fd963c

Browse files
committed
feat: implement mysql execute
1 parent a208cf2 commit 9fd963c

File tree

5 files changed

+129
-1
lines changed

5 files changed

+129
-1
lines changed

src/client.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,31 @@ export class RDSClient extends Operator {
137137
}
138138
}
139139

140+
async execute<T = any>(sql: string, values?: object | any[], options?: QueryOptions): Promise<T> {
141+
let conn: RDSConnection | RDSTransaction;
142+
let shouldReleaseConn = false;
143+
if (options?.conn) {
144+
conn = options.conn;
145+
} else {
146+
const ctx = this.#connectionStorage.getStore();
147+
const ctxConn = ctx?.[this.#connectionStorageKey];
148+
if (ctxConn) {
149+
conn = ctxConn;
150+
} else {
151+
conn = await this.getConnection();
152+
shouldReleaseConn = true;
153+
}
154+
}
155+
156+
try {
157+
return await conn.execute(sql, values);
158+
} finally {
159+
if (shouldReleaseConn) {
160+
(conn as RDSConnection).release();
161+
}
162+
}
163+
}
164+
140165
get pool() {
141166
return this.#pool;
142167
}

src/connection.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export class RDSConnection extends Operator {
1616
if (!this.conn[kWrapToRDS]) {
1717
[
1818
'query',
19+
'execute',
1920
'beginTransaction',
2021
'commit',
2122
'rollback',
@@ -36,6 +37,10 @@ export class RDSConnection extends Operator {
3637
return await this.conn.query(sql, values);
3738
}
3839

40+
async execute<T = any>(sql: string, values?: object | any[]): Promise<T> {
41+
return await this.conn.execute(sql, values);
42+
}
43+
3944
async beginTransaction() {
4045
return await this.conn.beginTransaction();
4146
}

src/transaction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ export class RDSTransaction extends Operator {
4141
return await this.conn!._query(sql, values);
4242
}
4343

44+
async execute<T = any>(sql: string, values?: object | any[]): Promise<T> {
45+
this.#check();
46+
return await this.conn!.execute(sql, values);
47+
}
48+
4449
#check() {
4550
if (!this.conn) {
4651
throw new Error('transaction was commit or rollback');

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ export interface RDSClientOptions extends PoolOptions {
1212
logging?: Logging;
1313
}
1414

15-
export interface PoolConnectionPromisify extends Omit<PoolConnection, 'query'> {
15+
export interface PoolConnectionPromisify extends Omit<PoolConnection, 'query' | 'execute'> {
1616
query(sql: string, values?: any | any[] | { [param: string]: any }): Promise<any>;
17+
execute(sql: string, values?: any | any[] | { [param: string]: any }): Promise<any>;
1718
beginTransaction(): Promise<void>;
1819
commit(): Promise<void>;
1920
rollback(): Promise<void>;

test/client.test.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,4 +1617,96 @@ describe('test/client.test.ts', () => {
16171617
});
16181618

16191619
});
1620+
1621+
describe('execute()', () => {
1622+
it('should execute sql with parameters', async () => {
1623+
const result = await db.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1624+
[ prefix + 'execute-test', prefix + 'm@execute-test.com' ]);
1625+
assert.equal(result.affectedRows, 1);
1626+
assert(result.insertId > 0);
1627+
});
1628+
1629+
it('should execute select query', async () => {
1630+
await db.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1631+
[ prefix + 'execute-select-test', prefix + 'm@execute-select-test.com' ]);
1632+
1633+
const rows = await db.execute('SELECT * FROM `myrds-test-user` WHERE email = ?',
1634+
[ prefix + 'm@execute-select-test.com' ]);
1635+
assert(Array.isArray(rows));
1636+
assert.equal(rows.length, 1);
1637+
assert.equal(rows[0].name, prefix + 'execute-select-test');
1638+
});
1639+
1640+
it('should execute in transaction', async () => {
1641+
const tran = await db.beginTransaction();
1642+
try {
1643+
const result = await db.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1644+
[ prefix + 'execute-transaction-test', prefix + 'm@execute-transaction-test.com' ],
1645+
{ conn: tran });
1646+
assert.equal(result.affectedRows, 1);
1647+
await tran.commit();
1648+
} catch (err) {
1649+
await tran.rollback();
1650+
throw err;
1651+
}
1652+
1653+
const rows = await db.execute('SELECT * FROM `myrds-test-user` WHERE email = ?',
1654+
[ prefix + 'm@execute-transaction-test.com' ]);
1655+
assert.equal(rows.length, 1);
1656+
});
1657+
1658+
it('should execute in transaction scope', async () => {
1659+
await db.beginTransactionScope(async (tran) => {
1660+
const result = await db.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1661+
[ prefix + 'execute-scope-test', prefix + 'm@execute-scope-test.com' ],
1662+
{ conn: tran });
1663+
assert.equal(result.affectedRows, 1);
1664+
});
1665+
1666+
const rows = await db.execute('SELECT * FROM `myrds-test-user` WHERE email = ?',
1667+
[ prefix + 'm@execute-scope-test.com' ]);
1668+
assert.equal(rows.length, 1);
1669+
});
1670+
1671+
it('should execute with connection', async () => {
1672+
const conn = await db.getConnection();
1673+
try {
1674+
const result = await conn.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1675+
[ prefix + 'execute-conn-test', prefix + 'm@execute-conn-test.com' ]);
1676+
assert.equal(result.affectedRows, 1);
1677+
} finally {
1678+
conn.release();
1679+
}
1680+
1681+
const rows = await db.execute('SELECT * FROM `myrds-test-user` WHERE email = ?',
1682+
[ prefix + 'm@execute-conn-test.com' ]);
1683+
assert.equal(rows.length, 1);
1684+
});
1685+
1686+
it('should execute update query', async () => {
1687+
await db.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1688+
[ prefix + 'execute-update-test', prefix + 'm@execute-update-test.com' ]);
1689+
1690+
const result = await db.execute('UPDATE `myrds-test-user` SET email = ? WHERE name = ?',
1691+
[ prefix + 'm@execute-updated.com', prefix + 'execute-update-test' ]);
1692+
assert.equal(result.affectedRows, 1);
1693+
1694+
const rows = await db.execute('SELECT * FROM `myrds-test-user` WHERE email = ?',
1695+
[ prefix + 'm@execute-updated.com' ]);
1696+
assert.equal(rows.length, 1);
1697+
});
1698+
1699+
it('should execute delete query', async () => {
1700+
await db.execute('INSERT INTO `myrds-test-user` (name, email, gmt_create, gmt_modified) VALUES(?, ?, now(), now())',
1701+
[ prefix + 'execute-delete-test', prefix + 'm@execute-delete-test.com' ]);
1702+
1703+
const result = await db.execute('DELETE FROM `myrds-test-user` WHERE name = ?',
1704+
[ prefix + 'execute-delete-test' ]);
1705+
assert.equal(result.affectedRows, 1);
1706+
1707+
const rows = await db.execute('SELECT * FROM `myrds-test-user` WHERE name = ?',
1708+
[ prefix + 'execute-delete-test' ]);
1709+
assert.equal(rows.length, 0);
1710+
});
1711+
});
16201712
});

0 commit comments

Comments
 (0)