Mongo
A Railway.Mongo is MongoDB as a Service:
the official mongo image, a Volume at /data/db, MONGO_* /
MONGO_URL variables, IPv6 bind for the private network, and an
optional TCP proxy for the public URL. The IaC helper is
Railway.mongo.
Private hostname is {name}.railway.internal. From a
Service, yield
ConnectMongo. From a laptop, use
publicConnectionUri.
Create Mongo
Section titled “Create Mongo”Pass a Project. Alchemy generates a unique name, password, volume, and a public TCP proxy.
const site = yield* Railway.Project("Site");const db = yield* Railway.mongo("Db", { project: site });Connect from a Service
Section titled “Connect from a Service”Yield ConnectMongo in Service init. Provide ConnectMongoHttp.
Pass conn.connectionString to the MongoDB driver (or
Railway.pingMongo).
import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";import * as Redacted from "effect/Redacted";
export default class Api extends Railway.Service<Api>()( "Api", { project: Site, main: import.meta.url, build: { install: ["mongodb"] }, }, Effect.gen(function* () { const conn = yield* Railway.ConnectMongo(Db); return { fetch: Effect.gen(function* () { const url = yield* conn.connectionString; const ping = yield* Railway.pingMongo(Redacted.value(url)); return HttpServerResponse.json(ping); }), }; }).pipe(Effect.provide(Railway.ConnectMongoHttp)),) {}To store Railway’s ${{Db.MONGO_URL}} template instead of a
resolved URI, pass Railway.ref
(Railway.ref(Db, "MONGO_URL")) as a
Variable value.
Public TCP
Section titled “Public TCP”public (default true) creates a TCP proxy on 27017.
publicConnectionUri is {domain}:{proxyPort} for laptop access.
const db = yield* Railway.mongo("Db", { project: site, public: false,});Where next
Section titled “Where next”Postgres and MySQL
are the same shape for SQL.
TcpProxy is the public TCP
resource.
The Mongo reference lists every prop.
The ConnectMongo reference is
the runtime binding.