Skip to content

Commit

Permalink
SQLocalKysely clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Jul 23, 2024
1 parent ab928b4 commit 4b075cd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 32 deletions.
53 changes: 23 additions & 30 deletions src/kysely/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,65 @@ import {
SqliteIntrospector,
SqliteQueryCompiler,
} from 'kysely';
import { convertRowsToObjects } from '../lib/convert-rows-to-objects.js';

export class SQLocalKysely extends SQLocal {
private executor = async <T>(
query: CompiledQuery
): Promise<QueryResult<T>> => {
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(),
};
}

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<void> {}

async acquireConnection(): Promise<SQLocalKyselyConnection> {
return new SQLocalKyselyConnection(this.executor);
return new SQLocalKyselyConnection(this.client);
}

async beginTransaction(connection: DatabaseConnection): Promise<void> {
async releaseConnection(): Promise<void> {}

async beginTransaction(connection: SQLocalKyselyConnection): Promise<void> {
await connection.executeQuery(CompiledQuery.raw('BEGIN'));
}

async commitTransaction(connection: DatabaseConnection): Promise<void> {
async commitTransaction(connection: SQLocalKyselyConnection): Promise<void> {
await connection.executeQuery(CompiledQuery.raw('COMMIT'));
}

async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
async rollbackTransaction(
connection: SQLocalKyselyConnection
): Promise<void> {
await connection.executeQuery(CompiledQuery.raw('ROLLBACK'));
}

async destroy(): Promise<void> {
await this.client.destroy();
}

async init(): Promise<void> {}
async releaseConnection(): Promise<void> {}
}

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<T>(query: CompiledQuery): Promise<QueryResult<T>> {
return await this.executor<T>(query);
async executeQuery<Result>(
query: CompiledQuery
): Promise<QueryResult<Result>> {
const rows = await this.client.sql(query.sql, ...query.parameters);

return {
rows: rows as Result[],
};
}

async *streamQuery(): AsyncGenerator<never, void, unknown> {
Expand Down
10 changes: 8 additions & 2 deletions test/kysely/migrations.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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([]);

Expand Down

0 comments on commit 4b075cd

Please sign in to comment.