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

main
ts 88 lines 3.39 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import * as D1Client from "@effect/sql-d1/D1Client";
3 import { makeExecutionMemo } from "alchemy/Runtime/ExecutionMemo";
4 import { proxyChain } from "alchemy/Util/proxy-chain";
5 import * as Context from "effect/Context";
6 import * as Effect from "effect/Effect";
7 import * as Layer from "effect/Layer";
8 import * as Sql from "effect/sql/SqlClient";
9
10 /**
11 * A source of the raw Cloudflare `D1Database` binding. Structurally matches
12 * the client returned by `Cloudflare.D1.QueryDatabase(db)` (its `raw`
13 * accessor), so both the client and a bare effect are accepted.
14 */
15 export type D1DatabaseSource<E, R> =
16 | Effect.Effect<runtime.D1Database, E, R>
17 | { readonly raw: Effect.Effect<runtime.D1Database, E, R> };
18
19 /**
20 * Options forwarded to `@effect/sql-d1`'s `D1Client` (everything except the
21 * `db` binding itself, which alchemy resolves from the Worker environment).
22 */
23 export type D1Config = Omit<D1Client.D1ClientConfig, "db">;
24
25 /**
26 * Open an `@effect/sql-d1` client over a Cloudflare D1 binding.
27 *
28 * Accepts the client returned by `Cloudflare.D1.QueryDatabase(db)` — or its
29 * `raw` effect directly — and returns a `D1Client` (which implements the
30 * generic `SqlClient` interface) wrapped in a chainable Proxy, so it can be
31 * resolved once at Worker init and used from any handler:
32 *
33 * ```typescript
34 * import * as SQL from "@alchemy.run/cloudflare/D1/SQL";
35 *
36 * const d1 = yield* Cloudflare.D1.QueryDatabase(Db);
37 * const sql = yield* SQL.D1(d1);
38 *
39 * fetch: Effect.gen(function* () {
40 * const users = yield* sql`SELECT * FROM users`;
41 * });
42 * ```
43 *
44 * The client build is deferred until the first query and memoized on the
45 * current execution's `Scope` (via {@link makeExecutionMemo}), so the
46 * `D1Client` (and its prepared-statement cache) is built at most once per
47 * execution — a Worker `fetch`/`queue`/`scheduled` event, a Durable Object
48 * call, or a Workflow run — and torn down when the event settles. Deploy /
49 * plan-time invocations never touch D1.
50 *
51 * @binding
52 * @title SQL
53 */
54 export const D1 = <E = never, R = never>(database: D1DatabaseSource<E, R>, config?: D1Config) =>
55 Effect.map(
56 makeExecutionMemo(
57 Effect.gen(function* () {
58 const db = yield* Effect.isEffect(database) ? database : database.raw;
59 const d1Ctx = yield* Layer.build(D1Client.layer({ ...config, db }));
60 return Context.get(d1Ctx, D1Client.D1Client);
61 }),
62 ),
63 (client) => proxyChain<D1Client.D1Client>(client),
64 );
65
66 /**
67 * Provide an `@effect/sql-d1` client as the `D1Client` and generic
68 * `SqlClient` services, so cloud-agnostic services written against
69 * `SqlClient.SqlClient` (or drizzle's `effect-d1` driver, which depends on
70 * `D1Client`) run on a bound D1 database:
71 *
72 * ```typescript
73 * const d1 = yield* Cloudflare.D1.QueryDatabase(Db);
74 * const app = yield* makeApp.pipe(Effect.provide(SQL.D1Layer(d1)));
75 * ```
76 *
77 * The layer itself builds synchronously at Worker init; the underlying
78 * `D1Client` is created lazily per execution (see {@link D1}).
79 */
80 export const D1Layer = <E = never, R = never>(
81 database: D1DatabaseSource<E, R>,
82 config?: D1Config,
83 ) =>
84 // Derive SqlClient from the single D1Client build so both tags share one
85 // per-execution client (and one prepared-statement cache).
86 Layer.effect(Sql.SqlClient, D1Client.D1Client).pipe(
87 Layer.provideMerge(Layer.effect(D1Client.D1Client, D1(database, config))),
88 );