Skip to content

Railway.MongoDB reference

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

Source: src/Railway/ConnectMongoHttp.ts Kind: 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.

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.

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

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)),
) {}

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

Default is official Mongo 8. Pass image to pin another tag.

const db = yield* Railway.mongo("Db", {
project: site,
image: "mongo:7",
});

Resource-valued props accept the resource or an Effect producing it.

src/db.ts
import * as Railway from "alchemy/Railway";
export const Site = Railway.Project("Site");
export const Db = Railway.mongo("Db", { project: Site });