The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Hyperdrive } from "@cloudflare/workers-types"; |
| 2 | import { Client } from "pg"; |
| 3 | |
| 4 | /** |
| 5 | * Async Worker that binds a looked-up Hyperdrive configuration via |
| 6 | * `env: { HD: lookup }` and runs SQL through it. Under `alchemy dev` the |
| 7 | * connection string points at the lookup's `dev` origin. |
| 8 | */ |
| 9 | export default { |
| 10 | async fetch(_request: Request, env: { HD: Hyperdrive }): Promise<Response> { |
| 11 | const client = new Client({ connectionString: env.HD.connectionString }); |
| 12 | try { |
| 13 | await client.connect(); |
| 14 | const result = await client.query("SELECT 1 + 1 AS sum, current_database() AS db"); |
| 15 | return Response.json({ row: result.rows[0], host: env.HD.host }); |
| 16 | } catch (error) { |
| 17 | return new Response(String(error), { status: 500 }); |
| 18 | } finally { |
| 19 | await client.end().catch(() => {}); |
| 20 | } |
| 21 | }, |
| 22 | }; |