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'
22
33import {
44 CONFIGS ,
@@ -19,13 +19,19 @@ import {
1919
2020forEach ( CONFIGS ) . describe ( 'PostgresJSDialect: %s' , ( config : TestConfig ) => {
2121 let ctx : TestContext
22+ let executedQueries : CompiledQuery [ ] = [ ]
2223
2324 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+ } )
2530 } )
2631
2732 beforeEach ( async ( ) => {
2833 await insertDefaultDataSet ( ctx )
34+ executedQueries = [ ]
2935 } )
3036
3137 afterEach ( async ( ) => {
@@ -87,6 +93,57 @@ forEach(CONFIGS).describe('PostgresJSDialect: %s', (config: TestConfig) => {
8793 }
8894 } )
8995
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+
90147 it ( 'should stream results' , async ( ) => {
91148 const males : unknown [ ] = [ ]
92149
0 commit comments