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

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