Railway.MongoDB reference
ConnectMongo
Section titled “ConnectMongo”Source:
src/Railway/ConnectMongo.ts
Bind a Mongo service to a Railway Service and obtain
the Effect-native connection string for the MongoDB driver.
ConnectMongo is the Context tag, the type, and the callable —
yield* Railway.ConnectMongo(Db). Provide ConnectMongoHttp.
ConnectMongo: Examples
Section titled “ConnectMongo: Examples”import * as Redacted from "effect/Redacted";
const conn = yield* Railway.ConnectMongo(Db);
fetch: Effect.gen(function* () { const url = yield* conn.connectionString; const ping = yield* Railway.pingMongo(Redacted.value(url));});ConnectMongo: Variable references
Section titled “ConnectMongo: Variable references”ConnectMongo packs a typed private URI onto the Service. To store
Railway’s template instead of a resolved URI (IaC db.env.MONGO_URL),
pass Railway.ref(Db, "MONGO_URL") as a Variable value or
Service.env entry.
const db = yield* Railway.mongo("Db", { project: site });yield* Railway.Variable("MongoUrl", { project: site, service: api, name: "MONGO_URL", value: Railway.ref(db, "MONGO_URL"),});ConnectMongoHttp
Section titled “ConnectMongoHttp”Source:
src/Railway/ConnectMongoHttp.tsKind: Layer · Provides:Railway.ConnectMongo
Implementation of ConnectMongo. Provide it on the
Service Effect.
At deploy time this packs the private connection URI onto the host
(RAILWAY_MONGO_*, MONGO_URL). At runtime the client reads
process.env.
ConnectMongoHttp: Provide the layer
Section titled “ConnectMongoHttp: Provide the layer”Effect.gen(function* () { const conn = yield* Railway.ConnectMongo(Db); const url = yield* conn.connectionString;}).pipe(Effect.provide(Railway.ConnectMongoHttp))Source:
src/Railway/Mongo.ts
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.
Private hostname is {name}.railway.internal. From a
Service, yield ConnectMongo. From a laptop, use
publicConnectionUri. The IaC helper is mongo.
Mongo: Create Mongo
Section titled “Mongo: 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 });Mongo: Connect from a Service
Section titled “Mongo: Connect from a Service”Yield ConnectMongo inside init. Provide
ConnectMongoHttp. Pass conn.connectionString to the
MongoDB driver (or pingMongo). The binding packs the private
URI ({name}.railway.internal).
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)),) {}Mongo: Public TCP
Section titled “Mongo: 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,});Mongo: Image
Section titled “Mongo: Image”Default is official Mongo 8. Pass image to pin another tag.
const db = yield* Railway.mongo("Db", { project: site, image: "mongo:7",});Mongo: Module-scope declarations
Section titled “Mongo: Module-scope declarations”Resource-valued props accept the resource or an Effect producing it.
import * as Railway from "alchemy/Railway";
export const Site = Railway.Project("Site");export const Db = Railway.mongo("Db", { project: Site });