File tree 3 files changed +75
-1
lines changed
3 files changed +75
-1
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ import {
42
42
provideControlledConnection ,
43
43
} from './util/provide-controlled-connection.js'
44
44
45
+ // @ts -ignore
46
+ Symbol . asyncDispose ??= Symbol ( 'Symbol.asyncDispose' )
47
+
45
48
/**
46
49
* The main Kysely class.
47
50
*
@@ -87,7 +90,7 @@ import {
87
90
*/
88
91
export class Kysely < DB >
89
92
extends QueryCreator < DB >
90
- implements QueryExecutorProvider
93
+ implements QueryExecutorProvider , AsyncDisposable
91
94
{
92
95
readonly #props: KyselyProps
93
96
@@ -509,6 +512,10 @@ export class Kysely<DB>
509
512
510
513
return this . getExecutor ( ) . executeQuery < R > ( compiledQuery , queryId )
511
514
}
515
+
516
+ async [ Symbol . asyncDispose ] ( ) {
517
+ await this . destroy ( )
518
+ }
512
519
}
513
520
514
521
export class Transaction < DB > extends Kysely < DB > {
Original file line number Diff line number Diff line change
1
+ import {
2
+ CompiledQuery ,
3
+ DatabaseConnection ,
4
+ DummyDriver ,
5
+ Kysely ,
6
+ PostgresAdapter ,
7
+ PostgresIntrospector ,
8
+ PostgresQueryCompiler ,
9
+ QueryResult ,
10
+ RootOperationNode ,
11
+ sql ,
12
+ } from '../../..'
13
+ import { expect } from './test-setup'
14
+
15
+ describe ( 'async dispose' , function ( ) {
16
+ it ( 'should call destroy ' , async ( ) => {
17
+ const steps : string [ ] = [ ]
18
+
19
+ {
20
+ await using db = new Kysely ( {
21
+ dialect : {
22
+ createAdapter : ( ) => new PostgresAdapter ( ) ,
23
+ createDriver : ( ) =>
24
+ new ( class extends DummyDriver {
25
+ async acquireConnection ( ) {
26
+ return new ( class implements DatabaseConnection {
27
+ async executeQuery < R > ( ) : Promise < QueryResult < R > > {
28
+ steps . push ( 'executed' )
29
+ return { rows : [ ] }
30
+ }
31
+ streamQuery < R > ( ) : AsyncIterableIterator < QueryResult < R > > {
32
+ throw new Error ( 'Method not implemented.' )
33
+ }
34
+ } ) ( )
35
+ }
36
+ async destroy ( ) : Promise < void > {
37
+ steps . push ( 'destroyed' )
38
+ }
39
+ } ) ( ) ,
40
+ createIntrospector : ( db ) => new PostgresIntrospector ( db ) ,
41
+ createQueryCompiler : ( ) =>
42
+ new ( class extends PostgresQueryCompiler {
43
+ compileQuery ( node : RootOperationNode ) : CompiledQuery < unknown > {
44
+ const compiled = super . compileQuery ( node )
45
+ steps . push ( 'compiled' )
46
+ return compiled
47
+ }
48
+ } ) ( ) ,
49
+ } ,
50
+ } )
51
+
52
+ await sql `select 1` . execute ( db )
53
+ }
54
+
55
+ steps . push ( 'after runScope' )
56
+
57
+ expect ( steps ) . to . length . to . be . greaterThan ( 1 )
58
+ expect ( steps ) . to . deep . equal ( [
59
+ 'compiled' ,
60
+ 'executed' ,
61
+ 'destroyed' ,
62
+ 'after runScope' ,
63
+ ] )
64
+ } )
65
+ } )
Original file line number Diff line number Diff line change 2
2
"extends" : " ../../tsconfig-base.json" ,
3
3
"include" : [" src/**/*" ],
4
4
"compilerOptions" : {
5
+ "target" : " ES2022" ,
6
+ "lib" : [" ESNext" ],
5
7
"module" : " CommonJS" ,
6
8
"outDir" : " dist" ,
7
9
"skipLibCheck" : true
You can’t perform that action at this time.
0 commit comments