Refactor kysely lib into separate files

This commit is contained in:
Alex Gleason 2023-08-06 21:57:26 -05:00
parent ed896278e0
commit 7c2f290775
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
7 changed files with 121 additions and 93 deletions

View File

@ -1,92 +0,0 @@
import {
type DatabaseIntrospector,
type Dialect,
type DialectAdapter,
type Driver,
Kysely,
type QueryCompiler,
SqliteAdapter,
type SqliteDatabase,
type SqliteDialectConfig,
SqliteDriver,
SqliteIntrospector,
SqliteQueryCompiler,
type SqliteStatement,
} from 'npm:kysely@^0.25.0';
import type { DB as DenoSqlite } from 'https://deno.land/x/sqlite@v3.7.3/mod.ts';
class DenoSqliteDatabase implements SqliteDatabase {
#db: DenoSqlite;
constructor(db: DenoSqlite) {
this.#db = db;
}
close(): void {
this.#db.close();
}
prepare(sql: string): SqliteStatement {
const query = this.#db.prepareQuery(sql);
return {
// HACK: implement an actual driver to fix this.
reader: true,
all: (parameters: ReadonlyArray<unknown>) => {
const result = query.allEntries(parameters as any);
query.finalize();
return result;
},
run: (parameters: ReadonlyArray<unknown>) => {
query.execute(parameters as any);
query.finalize();
return {
changes: this.#db.changes,
lastInsertRowid: this.#db.lastInsertRowId,
};
},
};
}
}
interface DenoSqliteDialectConfig extends Omit<SqliteDialectConfig, 'database'> {
database: DenoSqlite | (() => Promise<DenoSqlite>);
}
class DenoSqliteDriver extends SqliteDriver {
constructor(config: DenoSqliteDialectConfig) {
super({
...config,
database: async () =>
new DenoSqliteDatabase(
typeof config.database === 'function' ? await config.database() : config.database,
),
});
}
}
class DenoSqliteDialect implements Dialect {
readonly #config: DenoSqliteDialectConfig;
constructor(config: DenoSqliteDialectConfig) {
this.#config = Object.freeze({ ...config });
}
createDriver(): Driver {
return new DenoSqliteDriver(this.#config);
}
createQueryCompiler(): QueryCompiler {
return new SqliteQueryCompiler();
}
createAdapter(): DialectAdapter {
return new SqliteAdapter();
}
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
return new SqliteIntrospector(db);
}
}
export { DenoSqliteDatabase, DenoSqliteDialect, type DenoSqliteDialectConfig, DenoSqliteDriver };

View File

@ -0,0 +1,17 @@
export {
type DatabaseIntrospector,
type Dialect,
type DialectAdapter,
type Driver,
Kysely,
type QueryCompiler,
SqliteAdapter,
type SqliteDatabase,
type SqliteDialectConfig,
SqliteDriver,
SqliteIntrospector,
SqliteQueryCompiler,
type SqliteStatement,
} from 'npm:kysely@^0.25.0';
export type { DB as DenoSqlite } from 'https://deno.land/x/sqlite@v3.7.3/mod.ts';

View File

@ -0,0 +1,4 @@
export { DenoSqliteDialect } from './src/deno-sqlite-dialect.ts';
export { DenoSqliteDriver } from './src/deno-sqlite-driver.ts';
export type { DenoSqliteDialectConfig } from './src/deno-sqlite-dialect-config.ts';

View File

@ -0,0 +1,7 @@
import { type DenoSqlite, type SqliteDialectConfig } from '../deps.ts';
interface DenoSqliteDialectConfig extends Omit<SqliteDialectConfig, 'database'> {
database: DenoSqlite | (() => Promise<DenoSqlite>);
}
export type { DenoSqliteDialectConfig };

View File

@ -0,0 +1,41 @@
import {
type DatabaseIntrospector,
type Dialect,
type DialectAdapter,
type Driver,
Kysely,
type QueryCompiler,
SqliteAdapter,
SqliteIntrospector,
SqliteQueryCompiler,
} from '../deps.ts';
import { DenoSqliteDriver } from './deno-sqlite-driver.ts';
import type { DenoSqliteDialectConfig } from './deno-sqlite-dialect-config.ts';
class DenoSqliteDialect implements Dialect {
readonly #config: DenoSqliteDialectConfig;
constructor(config: DenoSqliteDialectConfig) {
this.#config = Object.freeze({ ...config });
}
createDriver(): Driver {
return new DenoSqliteDriver(this.#config);
}
createQueryCompiler(): QueryCompiler {
return new SqliteQueryCompiler();
}
createAdapter(): DialectAdapter {
return new SqliteAdapter();
}
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
return new SqliteIntrospector(db);
}
}
export { DenoSqliteDialect };

View File

@ -0,0 +1,51 @@
import { type DenoSqlite, type SqliteDatabase, SqliteDriver, type SqliteStatement } from '../deps.ts';
import type { DenoSqliteDialectConfig } from './deno-sqlite-dialect-config.ts';
class DenoSqliteDriver extends SqliteDriver {
constructor(config: DenoSqliteDialectConfig) {
super({
...config,
database: async () =>
new DenoSqliteDatabase(
typeof config.database === 'function' ? await config.database() : config.database,
),
});
}
}
/** HACK: This is an adapter class. */
class DenoSqliteDatabase implements SqliteDatabase {
#db: DenoSqlite;
constructor(db: DenoSqlite) {
this.#db = db;
}
close(): void {
this.#db.close();
}
prepare(sql: string): SqliteStatement {
const query = this.#db.prepareQuery(sql);
return {
// HACK: implement an actual driver to fix this.
reader: true,
all: (parameters: ReadonlyArray<unknown>) => {
const result = query.allEntries(parameters as any);
query.finalize();
return result;
},
run: (parameters: ReadonlyArray<unknown>) => {
query.execute(parameters as any);
query.finalize();
return {
changes: this.#db.changes,
lastInsertRowid: this.#db.lastInsertRowId,
};
},
};
}
}
export { DenoSqliteDriver };

View File

@ -1,5 +1,5 @@
import { Kysely, Sqlite } from '@/deps.ts'; import { Kysely, Sqlite } from '@/deps.ts';
import { DenoSqliteDialect } from '../../lib/kysely-deno-sqlite.ts'; import { DenoSqliteDialect } from '../../lib/kysely-deno-sqlite/mod.ts';
interface Tables { interface Tables {
events: EventsTable; events: EventsTable;