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

main
ts 89 lines 3.47 KB
Raw
1 import * as PgClient from "@effect/sql-pg/PgClient";
2 import { makeExecutionMemo } from "alchemy/Runtime/ExecutionMemo";
3 import { proxyChain } from "alchemy/Util/proxy-chain";
4 import * as Context from "effect/Context";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7 import type * as Redacted from "effect/Redacted";
8 import * as Sql from "effect/sql/SqlClient";
9 import { resolveConnectionOptions } from "./PostgresTls.ts";
10
11 /**
12 * Options for {@link Postgres}: `@effect/sql-pg`'s pool configuration, with
13 * `url` widened to also accept an Effect (e.g. a Hyperdrive connection
14 * string, which resolves from the Worker environment at runtime).
15 */
16 export type PostgresConfig<E, R> = Omit<PgClient.PgPoolConfig, "url"> & {
17 readonly url: Redacted.Redacted<string> | Effect.Effect<Redacted.Redacted<string>, E, R>;
18 };
19
20 /**
21 * Open an `@effect/sql-pg` client (a connection pool) from a connection URL.
22 *
23 * Accepts a plain `Redacted` URL or an Effect of one — e.g.
24 * `Cloudflare.Hyperdrive.Connect(...)` or `Fly.ConnectPostgres(...)`'s
25 * `connectionString` — and returns a
26 * `PgClient` (which implements the generic `SqlClient` interface) wrapped in
27 * a chainable Proxy, so it can be resolved once at init and used from any
28 * handler:
29 *
30 * ```typescript
31 * import * as SQL from "@alchemy.run/sql/Postgres";
32 *
33 * const hd = yield* Cloudflare.Hyperdrive.Connect(Hyperdrive);
34 * const sql = yield* SQL.Postgres({ url: hd.connectionString });
35 *
36 * fetch: Effect.gen(function* () {
37 * const users = yield* sql`SELECT * FROM users`;
38 * });
39 * ```
40 *
41 * The pool is built lazily on the first query and memoized on the current
42 * execution's `Scope` (via {@link makeExecutionMemo}), so it's created at
43 * most once per execution — a Worker `fetch`/`queue`/`scheduled` event, a
44 * Durable Object call, a Workflow run, or a Lambda invocation — and its
45 * `end` finalizer fires when the event settles. Yielding the connection URL
46 * is likewise deferred, so deploy / plan-time invocations never connect.
47 *
48 * @binding
49 */
50 export const Postgres = <E = never, R = never>(config: PostgresConfig<E, R>) =>
51 Effect.map(
52 makeExecutionMemo(
53 Effect.gen(function* () {
54 const { url, ...pool } = config;
55 const resolved = Effect.isEffect(url) ? yield* url : url;
56 const pgCtx = yield* Layer.build(
57 PgClient.layer({
58 ...pool,
59 ...resolveConnectionOptions(resolved, pool.ssl),
60 }),
61 );
62 return Context.get(pgCtx, PgClient.PgClient);
63 }),
64 ),
65 (client) => proxyChain<PgClient.PgClient>(client),
66 );
67
68 /**
69 * Provide an `@effect/sql-pg` client as the `PgClient` and generic
70 * `SqlClient` services, so cloud-agnostic services written against
71 * `SqlClient.SqlClient` (or drizzle's `effect-postgres` driver, which
72 * depends on `PgClient`) run on the configured pool:
73 *
74 * ```typescript
75 * const hd = yield* Cloudflare.Hyperdrive.Connect(Hyperdrive);
76 * const app = yield* makeApp.pipe(
77 * Effect.provide(SQL.PostgresLayer({ url: hd.connectionString })),
78 * );
79 * ```
80 *
81 * The layer itself builds synchronously at init; the underlying pool is
82 * created lazily per execution (see {@link Postgres}).
83 */
84 export const PostgresLayer = <E = never, R = never>(config: PostgresConfig<E, R>) =>
85 // Derive SqlClient from the single PgClient build so both tags share one
86 // per-execution pool.
87 Layer.effect(Sql.SqlClient, PgClient.PgClient).pipe(
88 Layer.provideMerge(Layer.effect(PgClient.PgClient, Postgres(config))),
89 );