db — typed database access
db provides owning connections, result cursors, reusable prepared statements, transactions and a compile-time mapped SQL query builder.
Provider status
| Provider | Public constructor | Availability and evidence |
|---|---|---|
| SQLite | Connection.sqlite(path) | default native provider; local synchronous test evidence |
| PostgreSQL | Connection.postgres(conninfo) | optional provider; integration matrix pending |
| MySQL/MariaDB | Connection.mysql(conninfo) | optional provider; integration matrix pending |
| Redis | Connection.redis(conninfo) | optional provider; integration matrix pending |
| MongoDB | Connection.mongo(conninfo) | optional provider; integration matrix pending |
The checked-in default native artifact contains SQLite and links its platform library. The Vex API calls a native C provider layer; it is not a pure-Vex PostgreSQL driver or an async connection pool. An unavailable provider returns DbErrorKind.Unsupported; it never silently falls back to another database.
Quick start
This application fragment opens an in-memory database, creates a table, inserts one parameterized row and reads it back. Place it inside a function; the example terminates on setup failure to keep the successful path visible. A service should return the typed DbError to its caller instead of panicking.
import { Connection, DbValue } from "db";
let connection = match Connection.sqlite(":memory:") {
Result.Ok(value) => value,
Result.Err(err) => $panic(err.message()),
};
match connection.exec("CREATE TABLE users (id INTEGER, name TEXT)") {
Result.Ok(_) => { /* ready */ }
Result.Err(err) => $panic(err.message()),
}
let! values = Vec.new<DbValue>();
values.push(DbValue.I64(1 as i64));
values.push(DbValue.Text("Ada".toString()));
match connection.execParams("INSERT INTO users (id, name) VALUES (?, ?)", &values) {
Result.Ok(_) => {},
Result.Err(err) => $panic(err.message()),
};
let! rows = match connection.query("SELECT id, name FROM users ORDER BY id") {
Result.Ok(value) => value,
Result.Err(err) => $panic(err.message()),
};
while match rows.next() { Result.Ok(value) => value, Result.Err(err) => $panic(err.message()) } {
match rows.text(1 as usize) {
Result.Ok(Some(name)) => $println(name),
Result.Ok(None) => $println("NULL"),
Result.Err(err) => $panic(err.message()),
}
}The query prints Ada. Values are bound separately from SQL text, so a name containing quotes does not change the statement structure.
Connections and results use RAII. No close() or free() call is required.
Hot-loop performance
Prepare repeated SQL once. A PreparedStatement reuses the provider plan, parameter scratch storage and native result container:
import { Connection, DbValue } from "db";
let statement = match connection.prepare("SELECT ? + 1") {
Result.Ok(value) => value,
Result.Err(err) => $panic(err.message()),
};
let! params = Vec.new<DbValue>();
params.push(DbValue.I64(41 as i64));
let result = statement.queryParams(¶ms);On the 2026-08-18 M2 Max O3 baseline, prepared select + typed i64 decode is about 244 ns versus 625 ns for one-shot prepare/query/finalize. Prepared update is about 387 ns versus 1.35 us for the one-shot parameterized path. These historical measurements describe that host and workload, not a 0.4.0 release performance guarantee.
Safety model
- SQL values use
DbValue; raw fragments are explicit. - SQLite accepts exactly one statement per call.
NULL, empty text and empty bytes remain distinct.- A cursor owns its connection lifetime.
- A prepared cursor also owns its statement lifetime and prevents concurrent reuse of the same native plan.
- Text/bytes returned publicly are owning values. Provider row buffers do not escape.
See Connection and prepared statements and compile-time ORM.