Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto use transaction in db.query #5

Merged
merged 2 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading