Skip to content

Commit

Permalink
it works.
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov committed Oct 19, 2024
1 parent 640135c commit 222c3cf
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
3 changes: 3 additions & 0 deletions src/kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ import {
provideControlledConnection,
} from './util/provide-controlled-connection.js'

// @ts-ignore
Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose')

/**
* The main Kysely class.
*
Expand Down
65 changes: 39 additions & 26 deletions test/node/src/async-dispose.test.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,65 @@
import {
CompiledQuery,
DatabaseConnection,
DummyDriver,
Kysely,
PostgresAdapter,
PostgresIntrospector,
PostgresQueryCompiler,
QueryResult,
RootOperationNode,
sql,
} from '../../..'
import { expect } from './test-setup'

describe('async dispose', function () {
it.only('should call destroy ', async () => {
const steps: string[] = [];
it('should call destroy ', async () => {
const steps: string[] = []

async function runScope() {
await using db = new Kysely({
dialect: {
createAdapter: () => new PostgresAdapter(),
createDriver: () => new (class SpiedOnDummyDriver extends DummyDriver {
async destroy(): Promise<void> {
await new Promise((resolve) => setTimeout(resolve, 100))
steps.push('destroyed')
}
{
await using db = new Kysely({
dialect: {
createAdapter: () => new PostgresAdapter(),
createDriver: () =>
new (class extends DummyDriver {
async acquireConnection() {
return new (class implements DatabaseConnection {
async executeQuery<R>(): Promise<QueryResult<R>> {
steps.push('executed')
return { rows: [] }
}
streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
throw new Error('Method not implemented.')
}
})()
}
async destroy(): Promise<void> {
steps.push('destroyed')
}
})(),
createIntrospector: (db) => new PostgresIntrospector(db),
createQueryCompiler: () => new (class SpiedOnPostgresQueryCompiler extends PostgresQueryCompiler {
compileQuery(node: RootOperationNode): CompiledQuery<unknown> {
const compiled = super.compileQuery(node)
steps.push('compiled')
return compiled
}
createIntrospector: (db) => new PostgresIntrospector(db),
createQueryCompiler: () =>
new (class extends PostgresQueryCompiler {
compileQuery(node: RootOperationNode): CompiledQuery<unknown> {
const compiled = super.compileQuery(node)
steps.push('compiled')
return compiled
}
})(),
},
})
},
})

sql`select 1`.compile(db);
await sql`select 1`.execute(db)
}

await runScope()

steps.push('after runScope')

expect(steps).to.length.to.be.greaterThan(1)
expect(steps).to.deep.equal([
'compiled',
'destroyed',
'after runScope',
'compiled',
'executed',
'destroyed',
'after runScope',
])
})
})
2 changes: 1 addition & 1 deletion tsconfig-base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"moduleResolution": "Node",
"target": "ESNext",
"target": "ES2022",
"lib": ["ESNext"],
"declaration": true,
"strict": true,
Expand Down

0 comments on commit 222c3cf

Please sign in to comment.