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

main
ts 90 lines 3.5 KB
Raw
1 import * as DSQL from "@alchemy.run/aws/DSQL";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
4 import { eq, sql } from "drizzle-orm";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import path from "pathe";
10 import { Db } from "./db.ts";
11 import { Widgets } from "./schema.ts";
12
13 const main = path.resolve(import.meta.dirname, "drizzle-handler.ts");
14
15 export class DsqlDrizzleFunction extends Lambda.Function<Lambda.Function>()(
16 "DsqlDrizzleFunction",
17 ) {}
18
19 /**
20 * Lambda fixture proving the full-stack Drizzle-over-DSQL path: the
21 * `DSQL.Connect` binding mints an IAM auth token per execution, and
22 * `Drizzle.Postgres` builds its execution-scoped pool from the resulting
23 * connection URL. No VPC — DSQL's endpoint is public and IAM-gated.
24 */
25 export default DsqlDrizzleFunction.make(
26 {
27 main,
28 functionUrl: true,
29 timeout: Duration.seconds(30),
30 },
31 Effect.gen(function* () {
32 const cluster = yield* Db;
33 const conn = yield* DSQL.Connect(cluster, { admin: true });
34 // `proxyChain` defers the connect to the first query, so the pool (and
35 // the IAM token backing it) is built inside the invocation, per
36 // execution — a fresh token can never outlive its pool.
37 const db = yield* Drizzle.Postgres(conn.pipe(Effect.map((info) => info.url)));
38
39 return {
40 fetch: Effect.gen(function* () {
41 const request = yield* HttpServerRequest;
42 const url = new URL(request.originalUrl);
43 const pathname = url.pathname;
44
45 // Cheap readiness probe — proves connect + token auth end-to-end.
46 if (request.method === "GET" && pathname === "/health") {
47 // postgres-js `execute()` resolves to the row array itself.
48 const rows = yield* db.execute(sql`SELECT 1 AS one`);
49 return yield* HttpServerResponse.json({ rows });
50 }
51
52 // DSQL runs DDL outside DML transactions; each execute is its own
53 // autocommit statement. DELETE (not DROP) keeps reruns fast.
54 if (request.method === "POST" && pathname === "/setup") {
55 yield* db.execute(
56 sql`CREATE TABLE IF NOT EXISTS dsql_connect_widgets (id integer PRIMARY KEY, title text NOT NULL)`,
57 );
58 yield* db.execute(sql`DELETE FROM dsql_connect_widgets`);
59 return yield* HttpServerResponse.json({ success: true });
60 }
61
62 if (request.method === "POST" && pathname === "/insert") {
63 const body = (yield* request.json) as unknown as {
64 id: number;
65 title: string;
66 };
67 yield* db.insert(Widgets).values({ id: body.id, title: body.title });
68 return yield* HttpServerResponse.json({ success: true });
69 }
70
71 if (request.method === "GET" && pathname === "/select") {
72 const id = url.searchParams.get("id");
73 if (!id) {
74 return HttpServerResponse.text("Missing id", { status: 400 });
75 }
76 const rows = yield* db
77 .select()
78 .from(Widgets)
79 .where(eq(Widgets.id, Number(id)));
80 return yield* HttpServerResponse.json({ rows });
81 }
82
83 return yield* HttpServerResponse.json(
84 { error: "Not found", method: request.method, pathname },
85 { status: 404 },
86 );
87 }).pipe(Effect.orDie),
88 };
89 }).pipe(Effect.provide(DSQL.ConnectHttp)),
90 );