Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ae5cb1e

Browse files
committedOct 24, 2024·
await using kysely = new Kysely() support. (#1167)
1 parent ee04272 commit ae5cb1e

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed
 

‎src/kysely.ts

+8-1
Original file line numberDiff line numberDiff 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
*/
8891
export 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

514521
export class Transaction<DB> extends Kysely<DB> {

‎test/node/src/async-dispose.test.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
})

‎test/node/tsconfig.json

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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

0 commit comments

Comments
 (0)
Please sign in to comment.