Database API Reference

The elide:sqlite, elide:postgres, and elide:mysql modules.

Import a driver; there is no Elide.db global.

ModuleAPI
elide:sqliteEmbedded, synchronous
elide:postgresNetwork, Promise-based
elide:mysqlNetwork, Promise-based

SQLite

import { Database } from "elide:sqlite";

const db = new Database(); // or new Database(":memory:"), or new Database("app.db")
db.exec("CREATE TABLE users (name TEXT, age INTEGER)");
db.prepare("INSERT INTO users VALUES (?, ?)").run("Ada", 36);
db.prepare("INSERT INTO users VALUES ($name, $age)").run({ $name: "Alan", $age: 41 });
console.log(db.prepare("SELECT name FROM users WHERE age > ?").all(40)); // [{name: "Alan"}]

const insert = db.transaction(() => db.exec("INSERT INTO users VALUES ('Grace', 85)"));
insert();
db.close();
  • Database has query, prepare, exec, transaction, loadExtension, serialize, and close.
  • Statements have all(), values(), get(), and run(), with positional (?, ?1) or named ($name) parameters.
  • transaction(fn) wraps a synchronous callback.
  • node:sqlite exports the same Database class; it does not export DatabaseSync.

PostgreSQL and MySQL

import { Database } from "elide:postgres";

const db = await Database.connect({
  host: "127.0.0.1",
  port: 5432,
  database: "app",
  user: "app",
  password: process.env.DB_PASSWORD,
});
await db.close();

Use elide:mysql and port 3306 for MySQL. database, user, and password are required.