@zgeoff/kysely-bun-sqlite 0.0.0-stage → 0.0.1
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 +21 -0
- package/README.md +43 -2
- package/dist/build-sqlite-dialect.d.ts +3 -0
- package/dist/bun-sqlite-driver.d.ts +15 -0
- package/dist/index.d.ts +2 -0
- package/package.json +40 -4
- package/src/build-sqlite-dialect.ts +13 -0
- package/src/bun-sqlite-driver.ts +91 -0
- package/src/index.ts +2 -0
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,44 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zgeoff/kysely-bun-sqlite
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Private until its first publish to npm.
|
|
4
|
+
|
|
5
|
+
A Kysely driver and dialect over one `bun:sqlite` database handle. Kysely ships no `bun:sqlite`
|
|
6
|
+
dialect, so a repo that pairs the two builds its dialect from this package.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
bun add @zgeoff/kysely-bun-sqlite kysely
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Kysely is a peer dependency, so the repo picks its own Kysely version within the peer range.
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
The repo opens the handle and sets its pragmas, then passes the handle to `buildSQLiteDialect`.
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { Database } from 'bun:sqlite';
|
|
22
|
+
import { buildSQLiteDialect } from '@zgeoff/kysely-bun-sqlite';
|
|
23
|
+
import { Kysely } from 'kysely';
|
|
24
|
+
|
|
25
|
+
interface LibrarySchema {
|
|
26
|
+
books: { title: string };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const sqlite = new Database('library.db', { create: true });
|
|
30
|
+
|
|
31
|
+
sqlite.run('CREATE TABLE IF NOT EXISTS books (title TEXT NOT NULL)');
|
|
32
|
+
|
|
33
|
+
const db = new Kysely<LibrarySchema>({ dialect: buildSQLiteDialect(sqlite) });
|
|
34
|
+
|
|
35
|
+
const titles = await db.selectFrom('books').select('title').execute();
|
|
36
|
+
|
|
37
|
+
await db.destroy();
|
|
38
|
+
sqlite.close();
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The opener owns the handle. `db.destroy()` waits for the caller that holds the connection and leaves
|
|
42
|
+
the handle open, so the opener closes the handle after `db.destroy()` resolves.
|
|
43
|
+
|
|
44
|
+
The driver refuses a streaming query, so `stream()` on a Kysely query throws.
|
|
@@ -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
|
+
}
|
package/dist/index.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zgeoff/kysely-bun-sqlite",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
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