1
+ import type { ConnectorType } from '../connectors' ;
1
2
import type { WithSQLExecutor } from '../execute' ;
2
3
import { type Connection } from './connection' ;
3
4
4
5
export interface DatabaseTransaction <
5
- ConnectorType extends string = string ,
6
+ Connector extends ConnectorType = ConnectorType ,
6
7
DbClient = unknown ,
7
8
> extends WithSQLExecutor {
8
- connector : ConnectorType ;
9
- connection : Connection < ConnectorType , DbClient > ;
9
+ connector : Connector ;
10
+ connection : Connection < Connector , DbClient > ;
10
11
begin : ( ) => Promise < void > ;
11
12
commit : ( ) => Promise < void > ;
12
13
rollback : ( error ?: unknown ) => Promise < void > ;
13
14
}
14
15
15
16
export interface DatabaseTransactionFactory <
16
- ConnectorType extends string = string ,
17
+ Connector extends ConnectorType = ConnectorType ,
17
18
DbClient = unknown ,
18
19
> {
19
- transaction : ( ) => DatabaseTransaction < ConnectorType , DbClient > ;
20
+ transaction : ( ) => DatabaseTransaction < Connector , DbClient > ;
20
21
21
22
withTransaction : < Result = never > (
22
23
handle : (
23
- transaction : DatabaseTransaction < ConnectorType , DbClient > ,
24
+ transaction : DatabaseTransaction < Connector , DbClient > ,
24
25
) => Promise < TransactionResult < Result > | Result > ,
25
26
) => Promise < Result > ;
26
27
}
@@ -38,13 +39,13 @@ const toTransactionResult = <Result>(
38
39
: { success : true , result : transactionResult } ;
39
40
40
41
export const executeInTransaction = async <
41
- ConnectorType extends string = string ,
42
+ Connector extends ConnectorType = ConnectorType ,
42
43
DbClient = unknown ,
43
44
Result = void ,
44
45
> (
45
- transaction : DatabaseTransaction < ConnectorType , DbClient > ,
46
+ transaction : DatabaseTransaction < Connector , DbClient > ,
46
47
handle : (
47
- transaction : DatabaseTransaction < ConnectorType , DbClient > ,
48
+ transaction : DatabaseTransaction < Connector , DbClient > ,
48
49
) => Promise < TransactionResult < Result > | Result > ,
49
50
) : Promise < Result > => {
50
51
await transaction . begin ( ) ;
@@ -63,14 +64,14 @@ export const executeInTransaction = async <
63
64
} ;
64
65
65
66
export const transactionFactoryWithDbClient = <
66
- ConnectorType extends string = string ,
67
+ Connector extends ConnectorType = ConnectorType ,
67
68
DbClient = unknown ,
68
69
> (
69
70
connect : ( ) => Promise < DbClient > ,
70
71
initTransaction : (
71
72
client : Promise < DbClient > ,
72
- ) => DatabaseTransaction < ConnectorType , DbClient > ,
73
- ) : DatabaseTransactionFactory < ConnectorType , DbClient > => ( {
73
+ ) => DatabaseTransaction < Connector , DbClient > ,
74
+ ) : DatabaseTransactionFactory < Connector , DbClient > => ( {
74
75
transaction : ( ) => initTransaction ( connect ( ) ) ,
75
76
withTransaction : ( handle ) =>
76
77
executeInTransaction ( initTransaction ( connect ( ) ) , handle ) ,
0 commit comments