The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { QueryDatabaseClient } from "@alchemy.run/cloudflare/D1/QueryDatabase"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | |
| 6 | /** |
| 7 | * Modality-agnostic D1 route handlers driving the Effect-native |
| 8 | * {@link QueryDatabaseClient} interface. Reused by the effect-worker shell. |
| 9 | * The async-worker shell drives the equivalent surface over the native |
| 10 | * `D1Database` binding (see `async-worker.ts`). |
| 11 | * |
| 12 | * Every worker stamps its rows with a `style` value ("effect" | "async") |
| 13 | * so the two workers can share one physical database. The table is shared; |
| 14 | * the rows are partitioned by `style`. |
| 15 | * |
| 16 | * POST /init — db.exec(CREATE TABLE) |
| 17 | * POST /seed — db.batch([prepare(...).bind(...), ...]) |
| 18 | * POST /users — db.prepare(...).bind(...).run() |
| 19 | * GET /users — db.prepare(...).all() |
| 20 | * GET /users/:id — db.prepare(...).bind(id).first() |
| 21 | * GET /raw — db.raw -> direct runtime D1Database access |
| 22 | */ |
| 23 | export const d1Routes = (db: QueryDatabaseClient, style: string) => |
| 24 | Effect.gen(function* () { |
| 25 | const request = yield* HttpServerRequest; |
| 26 | const url = new URL(request.url, "http://x"); |
| 27 | |
| 28 | if (request.method === "POST" && url.pathname === "/init") { |
| 29 | const result = yield* db.exec( |
| 30 | "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, style TEXT NOT NULL, name TEXT NOT NULL)", |
| 31 | ); |
| 32 | return yield* HttpServerResponse.json({ |
| 33 | count: result.count, |
| 34 | duration: result.duration, |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | if (request.method === "POST" && url.pathname === "/seed") { |
| 39 | const insert = db.prepare("INSERT OR REPLACE INTO users (id, style, name) VALUES (?, ?, ?)"); |
| 40 | const results = yield* db.batch([ |
| 41 | insert.bind(1, style, "alice"), |
| 42 | insert.bind(2, style, "bob"), |
| 43 | insert.bind(3, style, "carol"), |
| 44 | ]); |
| 45 | return yield* HttpServerResponse.json({ |
| 46 | batches: results.length, |
| 47 | success: results.every((r) => r.success), |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | if (request.method === "POST" && url.pathname === "/users") { |
| 52 | const body = (yield* request.json) as { id: number; name: string }; |
| 53 | const result = yield* db |
| 54 | .prepare("INSERT OR REPLACE INTO users (id, style, name) VALUES (?, ?, ?)") |
| 55 | .bind(body.id, style, body.name) |
| 56 | .run(); |
| 57 | return yield* HttpServerResponse.json({ |
| 58 | success: result.success, |
| 59 | meta: { changes: result.meta.changes }, |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | if (request.method === "GET" && url.pathname === "/users") { |
| 64 | const result = yield* db |
| 65 | .prepare("SELECT id, name FROM users WHERE style = ? ORDER BY id") |
| 66 | .bind(style) |
| 67 | .all<{ id: number; name: string }>(); |
| 68 | return yield* HttpServerResponse.json({ |
| 69 | success: result.success, |
| 70 | results: result.results, |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | const userMatch = url.pathname.match(/^\/users\/(\d+)$/); |
| 75 | if (request.method === "GET" && userMatch) { |
| 76 | const id = Number(userMatch[1]); |
| 77 | const row = yield* db |
| 78 | .prepare("SELECT id, name FROM users WHERE id = ? AND style = ?") |
| 79 | .bind(id, style) |
| 80 | .first<{ id: number; name: string }>(); |
| 81 | return yield* HttpServerResponse.json({ row }); |
| 82 | } |
| 83 | |
| 84 | if (request.method === "GET" && url.pathname === "/raw") { |
| 85 | // `db.raw` returns the underlying Cloudflare D1Database — the escape |
| 86 | // hatch libraries like Better Auth / Drizzle rely on. |
| 87 | const raw = yield* db.raw; |
| 88 | const result = yield* Effect.promise(() => |
| 89 | raw |
| 90 | .prepare("SELECT COUNT(*) as count FROM users WHERE style = ?") |
| 91 | .bind(style) |
| 92 | .first<{ count: number }>(), |
| 93 | ); |
| 94 | return yield* HttpServerResponse.json({ count: result?.count ?? 0 }); |
| 95 | } |
| 96 | |
| 97 | return HttpServerResponse.text("Not Found", { status: 404 }); |
| 98 | }); |