diff --git a/lib/kysely-deno-sqlite.ts b/lib/kysely-deno-sqlite.ts deleted file mode 100644 index e7a4020..0000000 --- a/lib/kysely-deno-sqlite.ts +++ /dev/null @@ -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) => { - const result = query.allEntries(parameters as any); - query.finalize(); - return result; - }, - run: (parameters: ReadonlyArray) => { - query.execute(parameters as any); - query.finalize(); - return { - changes: this.#db.changes, - lastInsertRowid: this.#db.lastInsertRowId, - }; - }, - }; - } -} - -interface DenoSqliteDialectConfig extends Omit { - database: DenoSqlite | (() => Promise); -} - -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): DatabaseIntrospector { - return new SqliteIntrospector(db); - } -} - -export { DenoSqliteDatabase, DenoSqliteDialect, type DenoSqliteDialectConfig, DenoSqliteDriver }; diff --git a/lib/kysely-deno-sqlite/deps.ts b/lib/kysely-deno-sqlite/deps.ts new file mode 100644 index 0000000..ff95c11 --- /dev/null +++ b/lib/kysely-deno-sqlite/deps.ts @@ -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'; diff --git a/lib/kysely-deno-sqlite/mod.ts b/lib/kysely-deno-sqlite/mod.ts new file mode 100644 index 0000000..a0e1998 --- /dev/null +++ b/lib/kysely-deno-sqlite/mod.ts @@ -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'; diff --git a/lib/kysely-deno-sqlite/src/deno-sqlite-dialect-config.ts b/lib/kysely-deno-sqlite/src/deno-sqlite-dialect-config.ts new file mode 100644 index 0000000..12bba77 --- /dev/null +++ b/lib/kysely-deno-sqlite/src/deno-sqlite-dialect-config.ts @@ -0,0 +1,7 @@ +import { type DenoSqlite, type SqliteDialectConfig } from '../deps.ts'; + +interface DenoSqliteDialectConfig extends Omit { + database: DenoSqlite | (() => Promise); +} + +export type { DenoSqliteDialectConfig }; diff --git a/lib/kysely-deno-sqlite/src/deno-sqlite-dialect.ts b/lib/kysely-deno-sqlite/src/deno-sqlite-dialect.ts new file mode 100644 index 0000000..f3aa517 --- /dev/null +++ b/lib/kysely-deno-sqlite/src/deno-sqlite-dialect.ts @@ -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): DatabaseIntrospector { + return new SqliteIntrospector(db); + } +} + +export { DenoSqliteDialect }; diff --git a/lib/kysely-deno-sqlite/src/deno-sqlite-driver.ts b/lib/kysely-deno-sqlite/src/deno-sqlite-driver.ts new file mode 100644 index 0000000..c68809e --- /dev/null +++ b/lib/kysely-deno-sqlite/src/deno-sqlite-driver.ts @@ -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) => { + const result = query.allEntries(parameters as any); + query.finalize(); + return result; + }, + run: (parameters: ReadonlyArray) => { + query.execute(parameters as any); + query.finalize(); + return { + changes: this.#db.changes, + lastInsertRowid: this.#db.lastInsertRowId, + }; + }, + }; + } +} + +export { DenoSqliteDriver }; diff --git a/src/db/builder.ts b/src/db/builder.ts index dec5deb..8a029fb 100644 --- a/src/db/builder.ts +++ b/src/db/builder.ts @@ -1,5 +1,5 @@ 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 { events: EventsTable;