elide:mysql connects to a MySQL server through a connection pool, with the same Promise-based API as
elide:postgres.
// 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();elide run my.mjs{affectedRows: 1, insertId: 1, insertIds: [1]}
[{name: "Ada", age: 36}]Connect
Database.connect() takes the same options as PostgreSQL, or a mysql:// URL; the default
port is 3306. ssl.key is ignored. Connection failures reject with Failed to connect to MySQL: ….
Differences from PostgreSQL
| MySQL | PostgreSQL | |
|---|---|---|
| Placeholders | ? only; $1 fails with Parameter index out of range | $1, $2, … or ? |
| Default port | 3306 | 5432 |
ssl.key | Ignored | Client key path |
Queries, rows, statements, batches, transactions and poolStats work as on the
PostgreSQL page.
Related
- PostgreSQL: the shared API in full.
- Databases: choosing a driver.