Skip to content

MySQL

Source: src/SQL/MySQL.ts

Open an @effect/sql-mysql2 client (a connection pool) from a connection URL — a plain Redacted or an Effect of one, e.g. Hyperdrive’s connectionString:

import * as SQL from "alchemy/SQL/MySQL";
const hd = yield* Cloudflare.Hyperdrive.Connect(Hyperdrive);
const sql = yield* SQL.MySQL({ url: hd.connectionString });
fetch: Effect.gen(function* () {
const users = yield* sql`SELECT * FROM users`;
});

The pool opens on the first query of an execution, is reused for every query in it, and closes when the event settles (see makeExecutionMemo); plan/deploy never connect. Workers defaults (resolveMySQLConfig) are overridden in the config:

const sql = yield* SQL.MySQL({
url,
disablePreparedStatements: true,
poolConfig: { ssl: { rejectUnauthorized: true } },
});