Railway.MySQL reference
ConnectMySQL
Section titled “ConnectMySQL”Source:
src/Railway/ConnectMySQL.ts
Bind a MySQL service to a Railway Service and obtain
the Effect-native connection string for Drizzle.MySQL /
SQL.MySQL.
ConnectMySQL is the Context tag, the type, and the callable —
yield* Railway.ConnectMySQL(Db). Provide ConnectMySQLHttp.
ConnectMySQL: Examples
Section titled “ConnectMySQL: Examples”import * as Drizzle from "alchemy/Drizzle/MySQL";
const conn = yield* Railway.ConnectMySQL(Db);const db = yield* Drizzle.MySQL(conn.connectionString);
fetch: Effect.gen(function* () { const rows = yield* db.execute("select 1 as ok", "objects");});ConnectMySQL: Variable references
Section titled “ConnectMySQL: Variable references”ConnectMySQL packs a typed private URI onto the Service. To store
Railway’s template instead of a resolved URI (IaC db.env.MYSQL_URL),
pass Railway.ref(Db, "MYSQL_URL") as a Variable value or
Service.env entry.
const db = yield* Railway.MySQL("Db", { project: site });yield* Railway.Variable("MysqlUrl", { project: site, service: api, name: "MYSQL_URL", value: Railway.ref(db, "MYSQL_URL"),});ConnectMySQLHttp
Section titled “ConnectMySQLHttp”Source:
src/Railway/ConnectMySQLHttp.tsKind: Layer · Provides:Railway.ConnectMySQL
Implementation of ConnectMySQL. Provide it on the
Service Effect.
At deploy time this packs the private connection URI onto the host
(RAILWAY_MYSQL_*, MYSQL_URL). At runtime the client reads
process.env.
ConnectMySQLHttp: Provide the layer
Section titled “ConnectMySQLHttp: Provide the layer”Effect.gen(function* () { const conn = yield* Railway.ConnectMySQL(Db); const db = yield* Drizzle.MySQL(conn.connectionString);}).pipe(Effect.provide(Railway.ConnectMySQLHttp))Source:
src/Railway/MySQL.ts
A Railway.MySQL is a MySQL-as-a-Service: the official mysql image,
a Volume at /var/lib/mysql, MYSQL_* / MYSQL_URL variables, and
an optional TCP proxy for the public URL. This is Alchemy’s
mysql() helper (Railway IaC: mysql("mysql")).
Private hostname is {name}.railway.internal. From a
Service, yield ConnectMySQL. From a laptop, use
publicConnectionUri.
MySQL: Create MySQL
Section titled “MySQL: Create MySQL”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.MySQL("Db", { project: site });MySQL: Connect from a Service
Section titled “MySQL: Connect from a Service”Yield ConnectMySQL inside init. Provide
ConnectMySQLHttp. Pass conn.connectionString to
Drizzle.MySQL or SQL.MySQL. The binding packs the private
URI ({name}.railway.internal).
import * as Drizzle from "alchemy/Drizzle/MySQL";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
export default class Api extends Railway.Service<Api>()( "Api", { project: Site, main: import.meta.url, build: { install: ["mysql2"] } }, Effect.gen(function* () { const conn = yield* Railway.ConnectMySQL(Db); const db = yield* Drizzle.MySQL(conn.connectionString); return { fetch: Effect.gen(function* () { const rows = yield* db.execute("select 1 as ok", "objects"); return HttpServerResponse.json({ rows }); }), }; }).pipe(Effect.provide(Railway.ConnectMySQLHttp)),) {}MySQL: Public TCP
Section titled “MySQL: Public TCP”public (default true) creates a TCP proxy on 3306.
publicConnectionUri is {domain}:{proxyPort} for laptop access
and deploy-time migrations.
const db = yield* Railway.MySQL("Db", { project: site, public: false,});MySQL: Image
Section titled “MySQL: Image”Default is official MySQL 9. Pass image to pin another tag.
const db = yield* Railway.MySQL("Db", { project: site, image: "mysql:8",});MySQL: Module-scope declarations
Section titled “MySQL: 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.MySQL("Db", { project: Site });