From 4b075cdca76210c5cf7c73dc887cc1c8b01d4391 Mon Sep 17 00:00:00 2001 From: Dallas Hoffman Date: Mon, 22 Jul 2024 20:12:40 -0400 Subject: [PATCH] SQLocalKysely clean-up --- src/kysely/client.ts | 53 +++++++++++++++------------------- test/kysely/migrations.test.ts | 10 +++++-- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/kysely/client.ts b/src/kysely/client.ts index 8a00c7c..5caa2a0 100644 --- a/src/kysely/client.ts +++ b/src/kysely/client.ts @@ -9,25 +9,11 @@ import { SqliteIntrospector, SqliteQueryCompiler, } from 'kysely'; -import { convertRowsToObjects } from '../lib/convert-rows-to-objects.js'; export class SQLocalKysely extends SQLocal { - private executor = async ( - query: CompiledQuery - ): Promise> => { - const { rows, columns } = await this.exec( - query.sql, - query.parameters as unknown[], - 'all' - ); - return { - rows: convertRowsToObjects(rows, columns) as T[], - }; - }; - dialect: Dialect = { createAdapter: () => new SqliteAdapter(), - createDriver: () => new SQLocalKyselyDriver(this, this.executor), + createDriver: () => new SQLocalKyselyDriver(this), createIntrospector: (db) => new SqliteIntrospector(db), createQueryCompiler: () => new SqliteQueryCompiler(), }; @@ -35,46 +21,53 @@ export class SQLocalKysely extends SQLocal { class SQLocalKyselyDriver implements Driver { private client: SQLocalKysely; - private executor: SQLocalKysely['executor']; - constructor(client: SQLocalKysely, executor: SQLocalKysely['executor']) { + constructor(client: SQLocalKysely) { this.client = client; - this.executor = executor; } + async init(): Promise {} + async acquireConnection(): Promise { - return new SQLocalKyselyConnection(this.executor); + return new SQLocalKyselyConnection(this.client); } - async beginTransaction(connection: DatabaseConnection): Promise { + async releaseConnection(): Promise {} + + async beginTransaction(connection: SQLocalKyselyConnection): Promise { await connection.executeQuery(CompiledQuery.raw('BEGIN')); } - async commitTransaction(connection: DatabaseConnection): Promise { + async commitTransaction(connection: SQLocalKyselyConnection): Promise { await connection.executeQuery(CompiledQuery.raw('COMMIT')); } - async rollbackTransaction(connection: DatabaseConnection): Promise { + async rollbackTransaction( + connection: SQLocalKyselyConnection + ): Promise { await connection.executeQuery(CompiledQuery.raw('ROLLBACK')); } async destroy(): Promise { await this.client.destroy(); } - - async init(): Promise {} - async releaseConnection(): Promise {} } class SQLocalKyselyConnection implements DatabaseConnection { - private executor: SQLocalKysely['executor']; + private client: SQLocalKysely; - constructor(executor: SQLocalKysely['executor']) { - this.executor = executor; + constructor(client: SQLocalKysely) { + this.client = client; } - async executeQuery(query: CompiledQuery): Promise> { - return await this.executor(query); + async executeQuery( + query: CompiledQuery + ): Promise> { + const rows = await this.client.sql(query.sql, ...query.parameters); + + return { + rows: rows as Result[], + }; } async *streamQuery(): AsyncGenerator { diff --git a/test/kysely/migrations.test.ts b/test/kysely/migrations.test.ts index 1243b4e..f2c8ad1 100644 --- a/test/kysely/migrations.test.ts +++ b/test/kysely/migrations.test.ts @@ -1,9 +1,10 @@ -import { describe, expect, it } from 'vitest'; +import { afterEach, describe, expect, it } from 'vitest'; import { SQLocalKysely } from '../../src/kysely'; import { Kysely, Migrator } from 'kysely'; describe('kysely migrations', () => { - const { dialect } = new SQLocalKysely('kysely-migrations-test.sqlite3'); + const databasePath = 'kysely-migrations-test.sqlite3'; + const { dialect } = new SQLocalKysely(databasePath); const db = new Kysely({ dialect }); const migrator = new Migrator({ @@ -27,6 +28,11 @@ describe('kysely migrations', () => { return table?.columns.map((column) => column.name); }; + afterEach(async () => { + const opfs = await navigator.storage.getDirectory(); + await opfs.removeEntry(databasePath); + }); + it('should migrate the database', async () => { expect(await getTableNames()).toEqual([]);