1
- import { DeleteResult , InsertResult , UpdateResult , sql , type Kysely , type Transaction } from 'kysely'
1
+ import { CompiledQuery , DeleteResult , InsertResult , UpdateResult , sql , type Kysely , type Transaction } from 'kysely'
2
2
3
3
import {
4
4
CONFIGS ,
@@ -19,13 +19,19 @@ import {
19
19
20
20
forEach ( CONFIGS ) . describe ( 'PostgresJSDialect: %s' , ( config : TestConfig ) => {
21
21
let ctx : TestContext
22
+ let executedQueries : CompiledQuery [ ] = [ ]
22
23
23
24
before ( async function ( ) {
24
- ctx = await initTest ( this , config . config )
25
+ ctx = await initTest ( this , config . config , ( event ) => {
26
+ if ( event . level === 'query' ) {
27
+ executedQueries . push ( event . query )
28
+ }
29
+ } )
25
30
} )
26
31
27
32
beforeEach ( async ( ) => {
28
33
await insertDefaultDataSet ( ctx )
34
+ executedQueries = [ ]
29
35
} )
30
36
31
37
afterEach ( async ( ) => {
@@ -87,6 +93,57 @@ forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => {
87
93
}
88
94
} )
89
95
96
+ it ( 'should set the transaction isolation level' , async ( ) => {
97
+ await ctx . db
98
+ . transaction ( )
99
+ . setIsolationLevel ( 'serializable' )
100
+ . execute ( async ( trx ) => {
101
+ await trx
102
+ . insertInto ( 'person' )
103
+ . values ( {
104
+ first_name : 'Foo' ,
105
+ last_name : 'Barson' ,
106
+ gender : 'male' ,
107
+ } )
108
+ . execute ( )
109
+ } )
110
+
111
+ expect (
112
+ executedQueries . map ( ( it ) => ( {
113
+ sql : it . sql ,
114
+ parameters : it . parameters ,
115
+ } ) ) ,
116
+ ) . to . eql ( [
117
+ {
118
+ sql : 'start transaction isolation level serializable' ,
119
+ parameters : [ ] ,
120
+ } ,
121
+ {
122
+ sql : 'insert into "person" ("first_name", "last_name", "gender") values ($1, $2, $3)' ,
123
+ parameters : [ 'Foo' , 'Barson' , 'male' ] ,
124
+ } ,
125
+ { sql : 'commit' , parameters : [ ] } ,
126
+ ] )
127
+ } )
128
+
129
+ it ( 'should be able to start a transaction with a single connection' , async ( ) => {
130
+ const result = await ctx . db . connection ( ) . execute ( ( db ) => {
131
+ return db . transaction ( ) . execute ( ( trx ) => {
132
+ return trx
133
+ . insertInto ( 'person' )
134
+ . values ( {
135
+ first_name : 'Foo' ,
136
+ last_name : 'Barson' ,
137
+ gender : 'male' ,
138
+ } )
139
+ . returning ( 'first_name' )
140
+ . executeTakeFirstOrThrow ( )
141
+ } )
142
+ } )
143
+
144
+ expect ( result . first_name ) . to . equal ( 'Foo' )
145
+ } )
146
+
90
147
it ( 'should stream results' , async ( ) => {
91
148
const males : unknown [ ] = [ ]
92
149
0 commit comments