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

main
ts 80 lines 2.62 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 { MySQL } from "./MySQL.ts";
7
8 /**
9 * Bind a {@link MySQL} service to a Railway {@link Service} and obtain
10 * the Effect-native connection string for `Drizzle.MySQL` /
11 * `SQL.MySQL`.
12 *
13 * `ConnectMySQL` is the Context tag, the type, and the callable —
14 * `yield* Railway.ConnectMySQL(Db)`. Provide {@link ConnectMySQLHttp}.
15 *
16 * **Example:** Bind MySQL in a Service
17 * ```typescript
18 * import * as Drizzle from "@alchemy.run/drizzle/MySQL";
19 *
20 * const conn = yield* Railway.ConnectMySQL(Db);
21 * const db = yield* Drizzle.MySQL(conn.connectionString);
22 *
23 * fetch: Effect.gen(function* () {
24 * const rows = yield* db.execute("select 1 as ok", "objects");
25 * });
26 * ```
27 *
28 * ### Variable references
29 * `ConnectMySQL` packs a typed private URI onto the Service. To store
30 * Railway's template instead of a resolved URI (IaC `db.env.MYSQL_URL`),
31 * pass `Railway.ref(Db, "MYSQL_URL")` as a {@link Variable} `value` or
32 * `Service.env` entry.
33 *
34 * **Example:** Railway.ref
35 * ```typescript
36 * const db = yield* Railway.MySQL("Db", { project: site });
37 * yield* Railway.Variable("MysqlUrl", {
38 * project: site,
39 * service: api,
40 * name: "MYSQL_URL",
41 * value: Railway.ref(db, "MYSQL_URL"),
42 * });
43 * ```
44 *
45 * @binding
46 * @product MySQL
47 * @category Storage & Databases
48 */
49 export interface ConnectMySQL extends Binding.Service<
50 ConnectMySQL,
51 "Railway.ConnectMySQL",
52 (mysql: MySQL) => Effect.Effect<ConnectMySQLClient>
53 > {}
54
55 export const ConnectMySQL = Binding.Service<ConnectMySQL>("Railway.ConnectMySQL");
56
57 export const connectEnvKeys = (mysql: Pick<MySQL, "LogicalId">) => {
58 const id = mysql.LogicalId.replaceAll(/[^a-zA-Z0-9]/g, "_").toUpperCase();
59 return {
60 pooled: `RAILWAY_MYSQL_${id}_POOLED`,
61 direct: `RAILWAY_MYSQL_${id}_DIRECT`,
62 };
63 };
64
65 export class MySQLUrlMissing extends Data.TaggedError("Railway.MySQLUrlMissing")<{
66 resourceName: string;
67 }> {}
68
69 export interface ConnectMySQLClient {
70 /**
71 * Private (`{name}.railway.internal`) connection string. Pass this to
72 * {@link Drizzle.MySQL} or `SQL.MySQL` from a {@link Service}.
73 */
74 connectionString: Effect.Effect<Redacted.Redacted<string>, MySQLUrlMissing, RuntimeContext>;
75 /**
76 * Same private URI — Railway MySQL has no proxy-pooler split. Kept so
77 * callers matching the `ConnectPostgres` shape keep working.
78 */
79 directConnectionString: Effect.Effect<Redacted.Redacted<string>, MySQLUrlMissing, RuntimeContext>;
80 }