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

main
mdx 87 lines 2.55 KB
Raw
1 ---
2 title: Mongo
3 description: Official MongoDB in a Project. Bind Railway.ConnectMongo and ping with the MongoDB driver.
4 ---
5
6 A [`Railway.Mongo`](/providers/railway/mongo) is MongoDB as a Service:
7 the official `mongo` image, a Volume at `/data/db`, `MONGO_*` /
8 `MONGO_URL` variables, IPv6 bind for the private network, and an
9 optional TCP proxy for the public URL. The IaC helper is
10 `Railway.mongo`.
11
12 Private hostname is `{name}.railway.internal`. From a
13 [Service](/railway/compute/services), yield
14 [`ConnectMongo`](/providers/railway/connectmongo). From a laptop, use
15 `publicConnectionUri`.
16
17 ## Create Mongo
18
19 Pass a Project. Alchemy generates a unique name, password, volume,
20 and a public TCP proxy.
21
22 ```typescript
23 const site = yield* Railway.Project("Site");
24 const db = yield* Railway.mongo("Db", { project: site });
25 ```
26
27 :::caution[Changing `project` replaces Mongo]
28 A new service + volume are created in the new Project. The old
29 service and volume are deleted. Data is not copied.
30 :::
31
32 ## Connect from a Service
33
34 Yield `ConnectMongo` in the Service's constructor. Provide `ConnectMongoHttp`.
35 Pass `conn.connectionString` to the MongoDB driver (or
36 `Railway.pingMongo`).
37
38 ```typescript
39 import * as HttpServerResponse from "effect/http/HttpServerResponse";
40 import * as Redacted from "effect/Redacted";
41
42 export default class Api extends Railway.Service<Api>()(
43 "Api",
44 {
45 project: Site,
46 main: import.meta.url,
47 build: { install: ["mongodb"] },
48 },
49 Effect.gen(function* () {
50 const conn = yield* Railway.ConnectMongo(Db);
51 return {
52 fetch: Effect.gen(function* () {
53 const url = yield* conn.connectionString;
54 const ping = yield* Railway.pingMongo(Redacted.value(url));
55 return HttpServerResponse.json(ping);
56 }),
57 };
58 }).pipe(Effect.provide(Railway.ConnectMongoHttp)),
59 ) {}
60 ```
61
62 To store Railway's `${{Db.MONGO_URL}}` template instead of a
63 resolved URI, pass [`Railway.ref`](/providers/railway/ref)
64 (`Railway.ref(Db, "MONGO_URL")`) as a
65 [Variable](/railway/data/variables) `value`.
66
67 ## Public TCP
68
69 `public` (default `true`) creates a TCP proxy on 27017.
70 `publicConnectionUri` is `{domain}:{proxyPort}` for laptop access.
71
72 ```typescript
73 const db = yield* Railway.mongo("Db", {
74 project: site,
75 public: false,
76 });
77 ```
78
79 ## Where next
80
81 [Postgres](/railway/data/postgres) and [MySQL](/railway/data/mysql)
82 are the same shape for SQL.
83 [TcpProxy](/railway/networking#tcp-proxies) is the public TCP
84 resource.
85 The [`Mongo` reference](/providers/railway/mongo) lists every prop.
86 The [`ConnectMongo` reference](/providers/railway/connectmongo) is
87 the runtime binding.