Skip to content

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.

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 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"),
});