File tree Expand file tree Collapse file tree 3 files changed +75
-1
lines changed Expand file tree Collapse file tree 3 files changed +75
-1
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ import {
4242 provideControlledConnection ,
4343} from './util/provide-controlled-connection.js'
4444
45+ // @ts -ignore
46+ Symbol . asyncDispose ??= Symbol ( 'Symbol.asyncDispose' )
47+
4548/**
4649 * The main Kysely class.
4750 *
@@ -87,7 +90,7 @@ import {
8790 */
8891export class Kysely < DB >
8992 extends QueryCreator < DB >
90- implements QueryExecutorProvider
93+ implements QueryExecutorProvider , AsyncDisposable
9194{
9295 readonly #props: KyselyProps
9396
@@ -509,6 +512,10 @@ export class Kysely<DB>
509512
510513 return this . getExecutor ( ) . executeQuery < R > ( compiledQuery , queryId )
511514 }
515+
516+ async [ Symbol . asyncDispose ] ( ) {
517+ await this . destroy ( )
518+ }
512519}
513520
514521export 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 22 "extends" : " ../../tsconfig-base.json" ,
33 "include" : [" src/**/*" ],
44 "compilerOptions" : {
5+ "target" : " ES2022" ,
6+ "lib" : [" ESNext" ],
57 "module" : " CommonJS" ,
68 "outDir" : " dist" ,
79 "skipLibCheck" : true
You can’t perform that action at this time.
0 commit comments