Database API Reference

Built-in SQLite, PostgreSQL, and MySQL modules

Elide provides three SQL modules:

ModuleEntry pointConnection model
SQLiteelide:sqliteEmbedded and synchronous
PostgreSQLelide:postgresNetwork connection and Promise-based API
MySQLelide:mysqlNetwork connection and Promise-based API

There is no Elide.db global. Import the driver you need.

SQLite

import { Database } from "elide:sqlite";

const db = new Database();
try {
  db.exec("CREATE TABLE checks (ok INTEGER)");
  db.exec("INSERT INTO checks VALUES (1)");

  console.log(db.query("SELECT ok FROM checks").get());
  // { ok: 1 }
} finally {
  db.close();
}

new Database() and new Database(":memory:") create an in-memory database. A path opens a file-backed database. The instance exposes query, prepare, exec, transaction, loadExtension, serialize, and close.

A statement exposes all(), values(), get(), and run().

Elide 1.5.0 does not provide reliable SQLite parameter handling. In particular, string values, named parameters, numbered parameters, and parameterized writes do not behave reliably. Do not interpolate untrusted values into SQL; use another database interface for queries that require parameters.

Transactions wrap a synchronous callback:

import { Database } from "elide:sqlite";

const db = new Database();
try {
  db.exec("CREATE TABLE checks (ok INTEGER)");
  const insert = db.transaction(() => {
    db.exec("INSERT INTO checks VALUES (1)");
  });

  insert();
  console.log(db.query("SELECT COUNT(*) AS count FROM checks").get());
  // { count: 1 }
} finally {
  db.close();
}

PostgreSQL and MySQL

Both network drivers expose a Database factory with connect:

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. A configuration object requires database, user, and password. Connection and query behavior requires a running database server and should be validated against the server version and configuration used by your application.

See also