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

main
ts 57 lines 2.13 KB
Raw
1 // Async (non-Effect) Worker that exercises the native D1 binding against
2 // the local workerd simulator: exec / prepared statements / batch.
3 interface Env {
4 DB: {
5 exec(sql: string): Promise<{ count: number }>;
6 prepare(sql: string): {
7 bind(...params: unknown[]): {
8 run(): Promise<{ success: boolean }>;
9 all<T>(): Promise<{ results: T[] }>;
10 };
11 run(): Promise<{ success: boolean }>;
12 all<T>(): Promise<{ results: T[] }>;
13 first<T>(): Promise<T | null>;
14 };
15 };
16 }
17
18 export default {
19 fetch: async (request: Request, env: Env) => {
20 const url = new URL(request.url);
21 if (url.pathname === "/roundtrip") {
22 await env.DB.exec(
23 "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT NOT NULL)",
24 );
25 await env.DB.prepare("DELETE FROM users").run();
26 await env.DB.prepare("INSERT INTO users (name) VALUES (?)").bind("alice").run();
27 await env.DB.prepare("INSERT INTO users (name) VALUES (?)").bind("bob").run();
28 const all = await env.DB.prepare("SELECT name FROM users ORDER BY name").all<{
29 name: string;
30 }>();
31 const first = await env.DB.prepare("SELECT COUNT(*) AS n FROM users").first<{ n: number }>();
32 return Response.json({
33 names: all.results.map((r) => r.name),
34 count: first?.n ?? null,
35 });
36 }
37 if (url.pathname === "/tables") {
38 const tables = await env.DB.prepare(
39 "SELECT name FROM sqlite_schema WHERE type = 'table' ORDER BY name",
40 ).all<{ name: string }>();
41 return Response.json({ tables: tables.results.map((t) => t.name) });
42 }
43 if (url.pathname === "/migrations") {
44 const rows = await env.DB.prepare(
45 "SELECT id, name FROM __alchemy_migrations ORDER BY id",
46 ).all<{ id: string; name: string }>();
47 return Response.json({ migrations: rows.results });
48 }
49 if (url.pathname === "/users") {
50 const rows = await env.DB.prepare("SELECT name FROM users ORDER BY name").all<{
51 name: string;
52 }>();
53 return Response.json({ users: rows.results.map((r) => r.name) });
54 }
55 return new Response("not found", { status: 404 });
56 },
57 };