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

main
ts 87 lines 3.21 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as SQL from "@alchemy.run/cloudflare/D1/SQL";
3 import * as Drizzle from "@alchemy.run/drizzle";
4 import * as Effect from "effect/Effect";
5 import * as HttpServerRequest from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import { DrizzleDb } from "./drizzle-database.ts";
8 import { Posts, relations, Users } from "./drizzle-schema.ts";
9
10 /**
11 * Effect-native Worker exercising BOTH D1 client layers over one database:
12 *
13 * - `Drizzle.D1(d1, { relations })` — the drizzle-orm `effect-d1` driver
14 * (typed query builder + relational queries);
15 * - `SQL.D1(d1)` — the raw `@effect/sql-d1` client
16 * (tagged-template SQL).
17 */
18 export default class D1DrizzleWorker extends Cloudflare.Worker<D1DrizzleWorker>()(
19 "D1DrizzleWorker",
20 {
21 main: import.meta.url,
22 },
23 Effect.gen(function* () {
24 const database = yield* DrizzleDb;
25 const d1 = yield* Cloudflare.D1.QueryDatabase(database);
26 const db = yield* Drizzle.D1(d1, { relations });
27 const sql = yield* SQL.D1(d1);
28
29 return {
30 fetch: Effect.gen(function* () {
31 const request = yield* HttpServerRequest.HttpServerRequest;
32 const segments = request.url.split("/").filter(Boolean);
33
34 if (request.method === "POST" && request.url === "/users") {
35 const body = (yield* request.json) as {
36 name: string;
37 email: string;
38 };
39 const [user] = yield* db
40 .insert(Users)
41 .values({ name: body.name, email: body.email })
42 .returning();
43 return yield* HttpServerResponse.json({ user });
44 }
45
46 if (request.method === "POST" && request.url === "/posts") {
47 const body = (yield* request.json) as {
48 userId: number;
49 title: string;
50 };
51 const [post] = yield* db
52 .insert(Posts)
53 .values({ userId: body.userId, title: body.title })
54 .returning();
55 return yield* HttpServerResponse.json({ post });
56 }
57
58 if (request.method === "GET" && request.url === "/users") {
59 const users = yield* db.select().from(Users);
60 return yield* HttpServerResponse.json({ users });
61 }
62
63 // GET /users/:id — relational query (RQB v2) joining posts.
64 if (request.method === "GET" && segments[0] === "users" && segments.length === 2) {
65 const id = Number(segments[1]);
66 const user = yield* db.query.Users.findFirst({
67 where: { id },
68 with: { posts: true },
69 });
70 return yield* HttpServerResponse.json({ user });
71 }
72
73 // GET /sql/users — the raw @effect/sql-d1 client.
74 if (request.method === "GET" && request.url === "/sql/users") {
75 const rows = yield* sql`SELECT id, name, email FROM users`;
76 return yield* HttpServerResponse.json({ rows });
77 }
78
79 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
80 }).pipe(
81 Effect.catchCause((cause) =>
82 HttpServerResponse.json({ error: String(cause) }, { status: 500 }),
83 ),
84 ),
85 };
86 }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseBinding)),
87 ) {}