The database layer

midad talks to SQLite through seven builtins. You write SQL; the layer carries it, binds its parameters and gives you the rows back. There is no ORM, no query builder and no migration generator, and that is deliberate.

The grant

A connection is a capability, granted by name in the frontmatter:

permissions:
  db: ["app.db"]

A program with no db grant cannot open one, in the interpreter or in a compiled build, and the grant is checked before the call the way fs_read is.

The surface

call answers
db.open(path) a connection, as a number
db.query(conn, sql, params) a list of maps, one per row
db.exec(conn, sql, params) the number of affected rows
db.begin(conn) opens a transaction
db.commit(conn) / db.rollback(conn) ends it
db.close(conn) closes the connection

Rows are maps whose keys are the column names. A column that is SQL NULL comes back as null: it prints null, type_of answers null, and is_null is true. That is not an empty option, which prints None and reports option.

A worked example

app.md
---
midad: "0.1"
module: app
permissions:
  db: ["app.db"]
---

# app

```exec
const conn = db.open("app.db");
db.exec(conn, "create table if not exists users (id integer primary key, email text)", []);
db.exec(conn, "insert into users (email) values (?)", ["a@example.com"]);
db.exec(conn, "insert into users (email) values (?)", ["b@example.com"]);

const rows = db.query(conn, "select id, email from users order by id", []);
print(str(rows));

const changed = db.exec(conn, "update users set email = ? where id = ?", ["c@example.com", 1]);
print("updated " + str(changed));
db.close(conn);
```
output
[{"email":"a@example.com","id":1},{"email":"b@example.com","id":2}]
updated 1

Parameters only

There is no API that takes a query with the values already in it. db.query takes text and a list, and every value in that list is bound by SQLite's own parameter mechanism. A value can never become SQL.

What that does not cover, and the repository says so plainly:

  • Identifiers cannot be bound. A table name, a column name, an ORDER BY term and a LIMIT are not values in any SQL dialect. A program that builds them from user input is building SQL, and the layer will not stop it.
  • Authorization is not the database's job. Binding stops injection; it does not stop a program from letting one user read another user's row.
  • The database user's own permissions still apply. The layer connects as the user the path or DSN names, with whatever rights that user has.

A parameter can be a string, a number, a float, a boolean or null. A list or a map in params is an error naming the position, not a stringified value.

Transactions

db.begin(conn) opens a transaction (begin immediate), and db.commit or db.rollback ends it. The transaction belongs to the connection: there is no separate handle, a second begin is SQLite's own error, and a transaction still open when the connection closes is rolled back by SQLite.

The row cap

db.query answers at most 10,000 rows and then refuses with an error that says to add a limit or aggregate in SQL. The cap is the same number in the interpreter and in a compiled build. It is there so one query cannot take the process down; it is not a paging API.

Which backends have it

backend db.*
interpreter yes
C (--native-all, --backend c) yes, with -ldl on Linux
Cranelift refused by name
midad-rt (a Cranelift binary) refused by name

Nothing links a database library: the interpreter loads the system's SQLite with dlopen at the first db.open, so a program that never opens one stays a single static binary, and a machine without the library gets a refusal that names it instead of a link error. On Linux that is libsqlite3.so.0.

What is not built

PostgreSQL and MySQL are designed and not built. Also not built: a caller-set row cap, a prepared-statement cache, a per-connection timeout, and a callback form of db.tx. The limits page has the full list, and docs/DATABASE.md in the repository has the design.

On this page