@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 60 lines 2.18 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import * as SQL from "@alchemy.run/cloudflare/D1/SQL";
3 import * as Effect from "effect/Effect";
4 import * as HttpServerRequest from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import { makeLayerUsers, makeSqlRoutes } from "./routes.ts";
7
8 export const Database = Cloudflare.D1.Database("SqlD1Database");
9
10 const TABLE = "alchemy_sql_users";
11
12 const DDL = `CREATE TABLE IF NOT EXISTS ${TABLE} (
13 id INTEGER PRIMARY KEY,
14 name TEXT NOT NULL,
15 email TEXT NOT NULL
16 )`;
17
18 /**
19 * Init effect for the whole route surface: binds the D1 database, builds the
20 * `SQL.D1` client and the `SQL.D1Layer`-backed users service, and returns
21 * the handler interface. Resources/bindings yielded here dedupe with any
22 * other usage in the Worker, so nothing needs to be threaded through `fetch`.
23 */
24 const D1Routes = Effect.gen(function* () {
25 const d1 = yield* Cloudflare.D1.QueryDatabase(Database);
26 const sql = yield* SQL.D1(d1);
27 const layerUsers = yield* makeLayerUsers(TABLE).pipe(Effect.provide(SQL.D1Layer(d1)));
28 return makeSqlRoutes({ sql, layerUsers, ddl: DDL, table: TABLE });
29 });
30
31 /**
32 * Effect-native Worker driving `SQL.D1` (the raw `@effect/sql-d1` client)
33 * over a bound D1 database. All routes come from the shared dialect-agnostic
34 * table in `routes.ts` — D1 has no dialect extras (`@effect/sql-d1` supports
35 * neither transactions nor `updateValues`).
36 */
37 export default class SqlD1Worker extends Cloudflare.Worker<SqlD1Worker>()(
38 "SqlD1Worker",
39 {
40 main: import.meta.url,
41 },
42 Effect.gen(function* () {
43 const routes = yield* D1Routes;
44
45 return {
46 fetch: Effect.gen(function* () {
47 const request = yield* HttpServerRequest.HttpServerRequest;
48 const response = yield* routes.handle(request);
49 if (response !== undefined) {
50 return response;
51 }
52 return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 });
53 }).pipe(
54 Effect.catchCause((cause) =>
55 HttpServerResponse.json({ error: String(cause) }, { status: 500 }),
56 ),
57 ),
58 };
59 }).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseBinding)),
60 ) {}