Skip to content

MySQL

Connect to MySQL from JavaScript and TypeScript with elide:mysql: the elide:postgres API with ? placeholders, pooled connections and transactions.

Read offlineelide help api-mysql

On this page

elide:mysql connects to a MySQL server through a connection pool, with the same Promise-based API as elide:postgres.

javascript
// my.mjs
import { Database } from "elide:mysql";

// A mysql:// URL: user and password, then host, port and database.
const db = await Database.connect(process.env.DATABASE_URL);
await db.exec("CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name TEXT NOT NULL, age INT)");
console.log(await db.exec("INSERT INTO users (name, age) VALUES (?, ?)", "Ada", 36));
console.log(await (await db.query("SELECT name, age FROM users WHERE age > ?", 18)).all());
await db.close();
utf-8 · javascript · Lines: 9
Terminal
bash
elide run my.mjs
text
{affectedRows: 1, insertId: 1, insertIds: [1]}
[{name: "Ada", age: 36}]
utf-8 · text · Lines: 2

Connect

Database.connect() takes the same options as PostgreSQL, or a mysql:// URL; the default port is 3306. Connection failures reject with Connection failed: ….

Differences from PostgreSQL

MySQL PostgreSQL
Placeholders ? only $1, $2, … or ?
Default port 3306 5432
TLS mode property sslMode: disabled, required, verify_identity sslmode: disable, require, verify-full
Timeout properties Milliseconds Seconds

MySQL also accepts allowPublicKeyRetrieval (default true) and serverRSAPublicKeyFile, a PEM public key that pins the server’s RSA key when authenticating without TLS.

Queries, rows, statements, batches, transactions and poolStats work as on the PostgreSQL page.

Was this page helpful?