@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 47 lines 1.5 KB
Raw
1 import type { Database } from "bun:sqlite";
2 import { MigrationError, type SqlExecutor } from "@alchemy.run/sql/Migrations";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * A {@link SqlExecutor} over an in-memory `bun:sqlite` database — the unit
7 * test stand-in for the D1 HTTP executor / local workerd tunnel. Batches run
8 * in a real transaction, so mid-batch failures roll back like D1's batched
9 * query does.
10 */
11 export const makeSqliteExecutor = (db: Database): SqlExecutor => ({
12 dialect: "sqlite",
13 query: (sql, params) =>
14 Effect.try({
15 try: () =>
16 db.query(sql).all(...((params ?? []) as never[])) as Array<Record<string, unknown>>,
17 catch: (cause) => new MigrationError({ message: String(cause), cause }),
18 }),
19 batch: (statements) =>
20 Effect.try({
21 try: () => {
22 db.run("BEGIN");
23 try {
24 for (const statement of statements) db.run(statement);
25 db.run("COMMIT");
26 } catch (error) {
27 try {
28 db.run("ROLLBACK");
29 } catch {
30 // already rolled back
31 }
32 throw error;
33 }
34 },
35 catch: (cause) => new MigrationError({ message: String(cause), cause }),
36 }),
37 });
38
39 /** List user table names (excluding sqlite internals). */
40 export const tableNames = (db: Database): string[] =>
41 (
42 db
43 .query(
44 "SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' ORDER BY name;",
45 )
46 .all() as Array<{ name: string }>
47 ).map((row) => row.name);