Skip to content

Commit 7108744

Browse files
committed
feat: add support for tx options
1 parent 964bb1a commit 7108744

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

packages/transactional-adapters/transactional-adapter-pg-promise/src/lib/transactional-adapter-pg-promise.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { IDatabase, ITask } from 'pg-promise';
33

44
export type PgPromiseDbOrTx = IDatabase<unknown> | ITask<unknown>;
55

6+
type TxOptions = Parameters<PgPromiseDbOrTx['tx']>[0];
7+
68
export interface PgPromiseTransactionalAdapterOptions {
79
/**
810
* The injection token for the pg-promise instance.
911
*/
10-
pgPromiseInstanceToken: any;
12+
dbInstanceToken: any;
1113
}
1214

1315
export class TransactionalAdapterPgPromise
@@ -16,15 +18,21 @@ export class TransactionalAdapterPgPromise
1618
connectionToken: any;
1719

1820
constructor(options: PgPromiseTransactionalAdapterOptions) {
19-
this.connectionToken = options.pgPromiseInstanceToken;
21+
this.connectionToken = options.dbInstanceToken;
2022
}
2123

2224
optionsFactory = (pgPromiseDbOrTxInstance: PgPromiseDbOrTx) => ({
2325
wrapWithTransaction: async (
24-
options: any, // FIXME
26+
options: TxOptions | null,
2527
fn: (...args: any[]) => Promise<any>,
2628
setClient: (client?: PgPromiseDbOrTx) => void,
2729
) => {
30+
if (options) {
31+
return pgPromiseDbOrTxInstance.tx(options, (tx) => {
32+
setClient(tx);
33+
return fn();
34+
});
35+
}
2836
return pgPromiseDbOrTxInstance.tx((tx) => {
2937
setClient(tx);
3038
return fn();

packages/transactional-adapters/transactional-adapter-pg-promise/test/transactional-adapter-pg-promise.spec.ts

+17-16
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,22 @@ import { PgPromiseDbOrTx, TransactionalAdapterPgPromise } from '../src';
1010
import pgPromise from 'pg-promise';
1111
import { execSync } from 'node:child_process';
1212

13+
type UserRecord = { id: number; name: string; email: string };
14+
1315
const PG_PROMISE = 'PG_PROMISE';
1416

15-
type UserRecord = { id: number; name: string; email: string };
17+
const pgp = pgPromise();
18+
const db = pgp({
19+
host: 'localhost',
20+
user: 'postgres',
21+
password: 'postgres',
22+
database: 'postgres',
23+
});
24+
25+
const { TransactionMode, isolationLevel } = pgp.txMode;
26+
const transactionMode = new TransactionMode({
27+
tiLevel: isolationLevel.serializable,
28+
});
1629

1730
@Injectable()
1831
class UserRepository {
@@ -52,9 +65,7 @@ class UserService {
5265
return { r1, r2 };
5366
}
5467

55-
@Transactional<TransactionalAdapterPgPromise>({
56-
isolationLevel: 'serializable', // FIXME
57-
})
68+
@Transactional<TransactionalAdapterPgPromise>({ mode: transactionMode })
5869
async transactionWithDecoratorWithOptions() {
5970
const r1 = await this.userRepository.createUser('James');
6071
const r2 = await this.db.oneOrNone<UserRecord>(
@@ -67,9 +78,7 @@ class UserService {
6778

6879
async transactionWithFunctionWrapper() {
6980
return this.txHost.withTransaction(
70-
{
71-
isolationLevel: 'serializable',
72-
},
81+
{ mode: transactionMode },
7382
async () => {
7483
const r1 = await this.userRepository.createUser('Joe');
7584
const r2 = await this.db.oneOrNone<UserRecord>(
@@ -89,14 +98,6 @@ class UserService {
8998
}
9099
}
91100

92-
const pgp = pgPromise();
93-
const db = pgp({
94-
host: 'localhost',
95-
user: 'postgres',
96-
password: 'postgres',
97-
database: 'postgres',
98-
});
99-
100101
@Module({
101102
providers: [
102103
{
@@ -116,7 +117,7 @@ class PgPromiseModule {}
116117
new ClsPluginTransactional({
117118
imports: [PgPromiseModule],
118119
adapter: new TransactionalAdapterPgPromise({
119-
pgPromiseInstanceToken: PG_PROMISE,
120+
dbInstanceToken: PG_PROMISE,
120121
}),
121122
}),
122123
],

0 commit comments

Comments
 (0)