Kysely Dialect for @std/sqlite

Caveats

It doesn't support transactions, there's no real way to do them on top of @std/sqlite AFAICT.

Usage

Create valimport { VtDialect } from "https://esm.town/v/easrng/kyselyVtDialect"; import { Kysely } from "npm:kysely"; const db = new Kysely({ dialect: new VtDialect(), });

Demo

See @easrng/kyselyVtDemo, which uses this along with @easrng/kyselyVtTypes to generate schema types.

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { sqlite } from "https://esm.town/v/std/sqlite";
import * as kysely from "npm:kysely@^0.27.3";
export class VtDialect implements kysely.Dialect {
createAdapter(): kysely.DialectAdapter {
return new kysely.SqliteAdapter();
}
createDriver(): kysely.Driver {
return new VtDriver();
}
createIntrospector(db: kysely.Kysely<any>): kysely.DatabaseIntrospector {
return new kysely.SqliteIntrospector(db);
}
createQueryCompiler(): kysely.QueryCompiler {
return new kysely.SqliteQueryCompiler();
}
}
export class VtDriver implements kysely.Driver {
async init(): Promise<void> {
}
async acquireConnection(): Promise<VtConnection> {
return new VtConnection();
}
async beginTransaction(
connection: VtConnection,
_settings: kysely.TransactionSettings,
): Promise<void> {
throw new Error("val.town does not support transactions");
}
async commitTransaction(connection: VtConnection): Promise<void> {
throw new Error("val.town does not support transactions");
}
async rollbackTransaction(connection: VtConnection): Promise<void> {
throw new Error("val.town does not support transactions");
}
async releaseConnection(connection: VtConnection): Promise<void> {
}
async destroy(): Promise<void> {
}
}
export class VtConnection implements kysely.DatabaseConnection {
async executeQuery<R>(compiledQuery: kysely.CompiledQuery): Promise<kysely.QueryResult<R>> {
const result = await sqlite.execute({
sql: compiledQuery.sql,
args: compiledQuery.parameters as any,
});
return {
numAffectedRows: BigInt(result.rowsAffected),
rows: result.rows.map(row => Object.fromEntries(row.map((value, i) => [result.columns[i], value]))) as R[],
};
}
async *streamQuery<R>(
_compiledQuery: kysely.CompiledQuery,
_chunkSize: number,
): AsyncIterableIterator<kysely.QueryResult<R>> {
throw new Error("val.town does not support streaming yet");
}
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.