@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 95 lines 3.23 KB
Raw
1 import * as Binding from "alchemy/Binding";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import * as Data from "effect/Data";
4 import type * as Effect from "effect/Effect";
5 import type * as Redacted from "effect/Redacted";
6 import type { Postgres } from "./Postgres.ts";
7
8 /**
9 * Bind a {@link Postgres} database to a Railway {@link Service} or
10 * {@link Function} and obtain the Effect-native connection string for
11 * `Drizzle.Postgres` / `SQL.Postgres`.
12 *
13 * `ConnectPostgres` is the Context tag, the type, and the callable —
14 * `yield* Railway.ConnectPostgres(Db)`. Provide {@link ConnectPostgresHttp}.
15 *
16 * **Example:** Bind Postgres in a Function
17 * ```typescript
18 * import * as Drizzle from "@alchemy.run/drizzle/Postgres";
19 *
20 * export default class Query extends Railway.Function<Query>()(
21 * "Query",
22 * { project: Site, main: import.meta.url, build: { install: ["pg"] } },
23 * Effect.gen(function* () {
24 * const conn = yield* Railway.ConnectPostgres(Db);
25 * const db = yield* Drizzle.Postgres(conn.connectionString);
26 * return {
27 * fetch: db.execute("select 1 as ok").pipe(
28 * Effect.flatMap(HttpServerResponse.json),
29 * ),
30 * };
31 * }).pipe(Effect.provide(Railway.ConnectPostgresHttp)),
32 * ) {}
33 * ```
34 *
35 * ### Variable references
36 * `ConnectPostgres` packs a typed private URI onto the host. To store
37 * Railway's template instead of a resolved URI (IaC
38 * `db.env.DATABASE_URL`), pass `Railway.ref(Db, "DATABASE_URL")` as a
39 * {@link Variable} `value` or `Service.env` entry. Railway interpolates
40 * `${{Db.DATABASE_URL}}` at build/runtime.
41 *
42 * **Example:** Railway.ref
43 * ```typescript
44 * const db = yield* Railway.Postgres("Db", { project: site });
45 * yield* Railway.Variable("DatabaseUrl", {
46 * project: site,
47 * service: api,
48 * name: "DATABASE_URL",
49 * value: Railway.ref(db, "DATABASE_URL"),
50 * });
51 * ```
52 *
53 * @binding
54 * @product Postgres
55 * @category Storage & Databases
56 */
57 export interface ConnectPostgres extends Binding.Service<
58 ConnectPostgres,
59 "Railway.ConnectPostgres",
60 (postgres: Postgres) => Effect.Effect<ConnectPostgresClient>
61 > {}
62
63 export const ConnectPostgres = Binding.Service<ConnectPostgres>("Railway.ConnectPostgres");
64
65 export const DATABASE_URL_SECRET = "DATABASE_URL";
66 export const DATABASE_PUBLIC_URL_SECRET = "DATABASE_PUBLIC_URL";
67
68 export const connectEnvKeys = (postgres: Pick<Postgres, "LogicalId">) => {
69 const id = postgres.LogicalId.replaceAll(/[^a-zA-Z0-9]/g, "_").toUpperCase();
70 return {
71 pooled: `RAILWAY_POSTGRES_${id}_POOLED`,
72 direct: `RAILWAY_POSTGRES_${id}_DIRECT`,
73 };
74 };
75
76 export class PostgresUrlMissing extends Data.TaggedError("Railway.PostgresUrlMissing")<{
77 resourceName: string;
78 }> {}
79
80 export interface ConnectPostgresClient {
81 /**
82 * Private (`{name}.railway.internal`) connection string. Pass this to
83 * {@link Drizzle.Postgres} or `SQL.Postgres` from a {@link Service}.
84 */
85 connectionString: Effect.Effect<Redacted.Redacted<string>, PostgresUrlMissing, RuntimeContext>;
86 /**
87 * Same private URI — Railway Postgres has no PgBouncer split. Kept so
88 * callers matching the Fly `ConnectPostgres` shape keep working.
89 */
90 directConnectionString: Effect.Effect<
91 Redacted.Redacted<string>,
92 PostgresUrlMissing,
93 RuntimeContext
94 >;
95 }