Skip to content

Commit

Permalink
feat: auto use transaction in db.query (#5)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced database query flexibility by allowing the use of
transactions or specific connections.

- **Tests**
- Implemented new tests to verify database queries within transactions
and with specified connections.

- **Chores**
	- Updated `.gitignore` to exclude IDE-specific settings files.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
killagu committed Apr 18, 2024
1 parent cd5a759 commit ba7cd37
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ coverage
lib
.eslintcache
package-lock.json
.idea
26 changes: 23 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ interface PoolPromisify extends Omit<Pool, 'query'> {
};
}

export interface QueryOptions {
conn?: RDSConnection | RDSTransaction;
}

export class RDSClient extends Operator {
static get literals() { return literals; }
static get escape() { return mysql.escape; }
Expand Down Expand Up @@ -83,12 +87,28 @@ export class RDSClient extends Operator {
});
}

async query<T = any>(sql: string, values?: object | any[]): Promise<T> {
const conn = await this.getConnection();
async query<T = any>(sql: string, values?: object | any[], options?: QueryOptions): Promise<T> {
let conn: RDSConnection | RDSTransaction;
let shouldReleaseConn = false;
if (options?.conn) {
conn = options.conn;
} else {
const ctx = this.#connectionStorage.getStore();
const ctxConn = ctx?.[this.#connectionStorageKey];
if (ctxConn) {
conn = ctxConn;
} else {
conn = await this.getConnection();
shouldReleaseConn = true;
}
}

try {
return await conn.query(sql, values);
} finally {
conn.release();
if (shouldReleaseConn) {
(conn as RDSConnection).release();
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,42 @@ describe('test/client.test.ts', () => {
await db1.end();
await db2.end();
});

it('should db.query in transaction scope works', async () => {
let querySql: string | undefined;
mm(RDSTransaction.prototype, 'query', async (sql: string) => {
querySql = sql;
});
await db.beginTransactionScope(async () => {
await db.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`,
[ table, prefix + 'multiple-instance1', prefix + 'm@multiple-instance.com' ]);
return db;
});
assert(querySql);
});

it('should db.query specify conn in transaction scope works', async () => {
let transactionQuerySql: string | undefined;
let connQuerySql: string | undefined;
mm(RDSTransaction.prototype, 'query', async (sql: string) => {
transactionQuerySql = sql;
});
const conn = await db.getConnection();
mm(conn, 'query', async (sql: string) => {
connQuerySql = sql;
});
await db.beginTransactionScope(async () => {
await db.query(`insert into ??(name, email, gmt_create, gmt_modified)
values(?, ?, now(), now())`,
[ table, prefix + 'multiple-instance1', prefix + 'm@multiple-instance.com' ], {
conn,
});
return db;
});
assert(connQuerySql);
assert(!transactionQuerySql);
});
});

describe('beginDoomedTransactionScope(scope)', () => {
Expand Down

0 comments on commit ba7cd37

Please sign in to comment.