@zgeoff/kysely-bun-sqlite 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 zgeoff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,42 @@
1
- # Temporary Holding Version
1
+ # @zgeoff/kysely-bun-sqlite
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A Kysely driver and dialect over one `bun:sqlite` database handle. Kysely ships no `bun:sqlite`
4
+ dialect, so a repo that pairs the two builds its dialect from this package.
5
+
6
+ ## Install
7
+
8
+ ```sh
9
+ bun add @zgeoff/kysely-bun-sqlite kysely
10
+ ```
11
+
12
+ Kysely is a peer dependency, so the repo picks its own Kysely version within the peer range.
13
+
14
+ ## Usage
15
+
16
+ The repo opens the handle and sets its pragmas, then passes the handle to `buildSQLiteDialect`.
17
+
18
+ ```ts
19
+ import { Database } from 'bun:sqlite';
20
+ import { buildSQLiteDialect } from '@zgeoff/kysely-bun-sqlite';
21
+ import { Kysely } from 'kysely';
22
+
23
+ interface LibrarySchema {
24
+ books: { title: string };
25
+ }
26
+
27
+ const sqlite = new Database('library.db', { create: true });
28
+
29
+ sqlite.run('CREATE TABLE IF NOT EXISTS books (title TEXT NOT NULL)');
30
+
31
+ const db = new Kysely<LibrarySchema>({ dialect: buildSQLiteDialect(sqlite) });
32
+
33
+ const titles = await db.selectFrom('books').select('title').execute();
34
+
35
+ await db.destroy();
36
+ sqlite.close();
37
+ ```
38
+
39
+ The opener owns the handle. `db.destroy()` waits for the caller that holds the connection and leaves
40
+ the handle open, so the opener closes the handle after `db.destroy()` resolves.
41
+
42
+ The driver refuses a streaming query, so `stream()` on a Kysely query throws.
@@ -0,0 +1,3 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { Dialect } from 'kysely';
3
+ export declare function buildSQLiteDialect(sqlite: Database): Dialect;
@@ -0,0 +1,15 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import type { DatabaseConnection, Driver } from 'kysely';
3
+ export declare class BunSQLiteDriver implements Driver {
4
+ private readonly connection;
5
+ private turn;
6
+ private releaseTurn;
7
+ constructor(sqlite: Database);
8
+ init(): Promise<void>;
9
+ acquireConnection(): Promise<DatabaseConnection>;
10
+ beginTransaction(connection: DatabaseConnection): Promise<void>;
11
+ commitTransaction(connection: DatabaseConnection): Promise<void>;
12
+ rollbackTransaction(connection: DatabaseConnection): Promise<void>;
13
+ releaseConnection(): Promise<void>;
14
+ destroy(): Promise<void>;
15
+ }
@@ -0,0 +1,2 @@
1
+ export { buildSQLiteDialect } from './build-sqlite-dialect.ts';
2
+ export { BunSQLiteDriver } from './bun-sqlite-driver.ts';
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
2
  "name": "@zgeoff/kysely-bun-sqlite",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "A Kysely driver and dialect for a bun:sqlite database handle",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/zgeoff/tools.git",
9
+ "directory": "packages/kysely-bun-sqlite"
10
+ },
11
+ "files": [
12
+ "src",
13
+ "!src/*.test.ts",
14
+ "dist"
15
+ ],
16
+ "type": "module",
17
+ "exports": {
18
+ ".": {
19
+ "types": "./dist/index.d.ts",
20
+ "default": "./src/index.ts"
21
+ }
22
+ },
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "scripts": {
27
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
28
+ "prepare": "bun run build",
29
+ "typecheck": "tsc"
30
+ },
31
+ "devDependencies": {
32
+ "@types/bun": "1.3.14",
33
+ "kysely": "0.29.6",
34
+ "typescript": "7.0.1-rc"
35
+ },
36
+ "peerDependencies": {
37
+ "kysely": ">=0.29.5 <0.30.0"
38
+ },
39
+ "engines": {
40
+ "bun": ">=1.3"
41
+ }
42
+ }
@@ -0,0 +1,13 @@
1
+ import type { Database } from 'bun:sqlite';
2
+ import { SqliteAdapter, SqliteIntrospector, SqliteQueryCompiler } from 'kysely';
3
+ import type { Dialect } from 'kysely';
4
+ import { BunSQLiteDriver } from './bun-sqlite-driver.ts';
5
+
6
+ export function buildSQLiteDialect(sqlite: Database): Dialect {
7
+ return {
8
+ createAdapter: () => new SqliteAdapter(),
9
+ createDriver: () => new BunSQLiteDriver(sqlite),
10
+ createIntrospector: (kysely) => new SqliteIntrospector(kysely),
11
+ createQueryCompiler: () => new SqliteQueryCompiler(),
12
+ };
13
+ }
@@ -0,0 +1,91 @@
1
+ import type { Database, SQLQueryBindings } from 'bun:sqlite';
2
+ import { CompiledQuery } from 'kysely';
3
+ import type { DatabaseConnection, Driver, QueryResult } from 'kysely';
4
+
5
+ // oxlint-disable zgeoff/function-verb -- Kysely's Driver interface fixes the method names
6
+
7
+ // Kysely's RuntimeDriver serializes the connection: SqliteAdapter reports no
8
+ // support for multiple connections, so RuntimeDriver holds a mutex from each
9
+ // acquire to its release.
10
+ export class BunSQLiteDriver implements Driver {
11
+ private readonly connection: DatabaseConnection;
12
+
13
+ // The driver's own turn chain exists only for the destroy() wait, because
14
+ // Kysely calls destroy() outside RuntimeDriver's mutex.
15
+ private turn: Promise<void> = Promise.resolve();
16
+
17
+ private releaseTurn: () => void = () => {};
18
+
19
+ constructor(sqlite: Database) {
20
+ this.connection = buildConnection(sqlite);
21
+ }
22
+
23
+ init(): Promise<void> {
24
+ return Promise.resolve();
25
+ }
26
+
27
+ async acquireConnection(): Promise<DatabaseConnection> {
28
+ const previous = this.turn;
29
+ const next = Promise.withResolvers<void>();
30
+
31
+ this.turn = next.promise;
32
+
33
+ await previous;
34
+
35
+ this.releaseTurn = next.resolve;
36
+
37
+ return this.connection;
38
+ }
39
+
40
+ async beginTransaction(connection: DatabaseConnection): Promise<void> {
41
+ await connection.executeQuery(CompiledQuery.raw('begin'));
42
+ }
43
+
44
+ async commitTransaction(connection: DatabaseConnection): Promise<void> {
45
+ await connection.executeQuery(CompiledQuery.raw('commit'));
46
+ }
47
+
48
+ async rollbackTransaction(connection: DatabaseConnection): Promise<void> {
49
+ await connection.executeQuery(CompiledQuery.raw('rollback'));
50
+ }
51
+
52
+ releaseConnection(): Promise<void> {
53
+ this.releaseTurn();
54
+
55
+ return Promise.resolve();
56
+ }
57
+
58
+ // The opener owns the handle and closes it, so destroy() leaves the handle
59
+ // open and only waits for the caller that holds the connection.
60
+ async destroy(): Promise<void> {
61
+ await this.acquireConnection();
62
+ await this.releaseConnection();
63
+ }
64
+ }
65
+
66
+ function buildConnection(sqlite: Database): DatabaseConnection {
67
+ return {
68
+ executeQuery<R>(compiledQuery: CompiledQuery): Promise<QueryResult<R>> {
69
+ const stmt = sqlite.query<R, SQLQueryBindings[]>(compiledQuery.sql);
70
+
71
+ // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- Kysely's SQLite compiler binds only values that bun:sqlite accepts
72
+ const bindings = compiledQuery.parameters as SQLQueryBindings[];
73
+
74
+ if (stmt.columnNames.length > 0) {
75
+ return Promise.resolve({ rows: stmt.all(...bindings) });
76
+ }
77
+
78
+ const result = stmt.run(...bindings);
79
+
80
+ return Promise.resolve({
81
+ rows: [],
82
+ numAffectedRows: BigInt(result.changes),
83
+ insertId: BigInt(result.lastInsertRowid),
84
+ });
85
+ },
86
+
87
+ streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
88
+ throw new Error('BunSQLiteDriver does not support streaming queries');
89
+ },
90
+ };
91
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { buildSQLiteDialect } from './build-sqlite-dialect.ts';
2
+ export { BunSQLiteDriver } from './bun-sqlite-driver.ts';