diff --git a/src/kysely.ts b/src/kysely.ts index 66bdb9694..b613ede4e 100644 --- a/src/kysely.ts +++ b/src/kysely.ts @@ -33,6 +33,9 @@ import { Expression } from './expression/expression.js' import { WithSchemaPlugin } from './plugin/with-schema/with-schema-plugin.js' import { DrainOuterGeneric } from './util/type-utils.js' +// @ts-ignore +Symbol.asyncDispose ??= Symbol('Symbol.asyncDispose') + /** * The main Kysely class. * diff --git a/test/node/src/async-dispose.test.ts b/test/node/src/async-dispose.test.ts index 5851be3db..72440ba46 100644 --- a/test/node/src/async-dispose.test.ts +++ b/test/node/src/async-dispose.test.ts @@ -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 { - 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(): Promise> { + steps.push('executed') + return { rows: [] } + } + streamQuery(): AsyncIterableIterator> { + throw new Error('Method not implemented.') + } + })() + } + async destroy(): Promise { + steps.push('destroyed') + } })(), - createIntrospector: (db) => new PostgresIntrospector(db), - createQueryCompiler: () => new (class SpiedOnPostgresQueryCompiler extends PostgresQueryCompiler { - compileQuery(node: RootOperationNode): CompiledQuery { - const compiled = super.compileQuery(node) - steps.push('compiled') - return compiled - } + createIntrospector: (db) => new PostgresIntrospector(db), + createQueryCompiler: () => + new (class extends PostgresQueryCompiler { + compileQuery(node: RootOperationNode): CompiledQuery { + 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', ]) }) }) diff --git a/tsconfig-base.json b/tsconfig-base.json index 4d8ff2f6f..dfc96da00 100644 --- a/tsconfig-base.json +++ b/tsconfig-base.json @@ -1,7 +1,7 @@ { "compilerOptions": { "moduleResolution": "Node", - "target": "ESNext", + "target": "ES2022", "lib": ["ESNext"], "declaration": true, "strict": true,