Перейти к содержимому

Эта страница ещё не переведена и показана на английском.

MySQL

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

На этой странице
Open in ChatGPTOpen in Claude

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

MySQLPostgreSQL
Placeholders? only; $1 fails with Parameter index out of range$1, $2, … or ?
Default port33065432
ssl.keyIgnoredClient key path

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

Была ли эта страница полезной?