sailfork / t3code-github / .repos/ alchemy-effect/ website/ src/ content/ docs/ railway/ data/ mongo.mdx

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

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

:::caution[Changing project replaces Mongo] A new service + volume are created in the new Project. The old service and volume are deleted. Data is not copied. :::

Connect from a Service

Yield ConnectMongo in the Service's constructor. Provide ConnectMongoHttp. Pass conn.connectionString to the MongoDB driver (or Railway.pingMongo).

import * as HttpServerResponse from "effect/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

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

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.