Build services Browse docs
On this page 6 sections
Build services
SQLite storage
Models are the schema, the runtime owns the database. Reads, parameterized queries, writes that fail honestly, and what first boot really does.
By the end of this page you can read rows, run a parameterized query, and handle a write that fails. Orbit links SQLite through its runtime for embedded service storage. Models provide the schema metadata; the runtime exposes the database operations to generated programs. This page follows examples/blog_api.orb and examples/sqlite_notes.orb — every behavior below is the one those files rely on.
Models are the schema
A model declares a record with typed fields, and the name doubles as the constructor:
model Note {
id: string
title: string
body: string
author_id: string
created_at: string
is_private: bool
}
The same declaration does two jobs:
| You write | What happens |
|---|---|
Note(…) |
builds an in-memory value with positional fields and dot access |
Note.all() / Note.where(…) / Note.create(…) / Note.delete(…) |
talks to the database layer |
The model is the schema: each model maps to one table (lowercase plural, so Note becomes notes), created with CREATE TABLE IF NOT EXISTS at startup. string becomes TEXT, int and bool become INTEGER, float becomes REAL, and a field named id is the primary key. Changes to an existing table go through migrations lines (see First boot and files).
Reads
Reads come from the seeded table, so GET works on a fresh start with no setup:
route GET "/posts" {
val posts = Note.all()
return ok 200 posts
}
The reads and writes every model carries:
| Method | Returns | Notes |
|---|---|---|
M.all() |
JSON array string | full scan |
M.order(col) / M.order_desc(col) |
JSON array string | scan by a validated column; anything else answers [] |
M.where(cond) |
JSON array string | raw fragment; keep it constant |
M.where(cond, p1..pn) |
JSON array string | one ? per param, escaped in order |
M.find(id) |
JSON object string | one row by key; "" when missing |
M.first() |
JSON object string | first row; "" on an empty table |
M.count() / M.count(cond, p...) |
int |
counts without fetching rows |
M.create(json) |
bool |
false on duplicate id or unusable payload |
M.update(id, json) |
bool |
writes only the keys present, never id; false when no row changed |
M.delete(id) |
bool |
true only when a row was removed |
Reads answer JSON strings, not typed model values, so your route returns them directly with ok 200. There are no joins, no ORDER BY/LIMIT modifiers and no transactions. On a fresh start, curl "http://127.0.0.1:8080/posts/search" answers 200 with [{"id":"note_101",...}] (the seeded rows).
Parameterized queries
Never build SQL by concatenating query values. Put a ? placeholder in the statement and pass the value next to it. The runtime keeps the input parameterized:
route GET "/posts/search" {
val author = req.query("author")
if (author == "") {
return ok 200 Note.all()
}
val found = Note.where("author_id = ?", author)
return ok 200 found
}
The value travels beside the statement, never inside it. Here is the call and what comes back (canned, from blog_api.orb):
| Request | Query run | Status and body |
|---|---|---|
curl "http://127.0.0.1:8080/posts/search?author=usr_admin" |
Note.where("author_id = ?", author) |
200, [{"id":"note_103","author_id":"usr_admin",...}] |
curl "http://127.0.0.1:8080/posts/search" |
Note.all() (empty author) |
200, seeded rows |
A secured variant from sqlite_notes.orb filters on a literal predicate instead — same rule, no user input in the string:
route GET "/v1/notes/secured" {
val token = req.bearer_token()
if (token == "") {
err 401 "Unauthorized: Bearer authentication token missing or invalid"
}
val secure_notes = Note.where("is_private = true")
return ok 200 secure_notes
}
Writes that fail honestly
A write fails honestly when it answers false instead of pretending it worked. Note.create() stores the row and answers true. Re-posting an id that already exists violates the primary key and answers false. Payloads with no usable fields also answer false. Your route branches on that boolean:
| Call | Answer | Route sends |
|---|---|---|
Note.create(...), new id |
true |
ok 201 with "status":"created" |
Note.create(...), id already exists |
false |
err 400 "Failed to save note to database" |
Note.create(...), no usable fields |
false |
err 400 "Failed to save note to database" |
The route:
route POST "/v1/notes" {
val saved = Note.create("""{"id":"note_103","title":"New Orbit Note","body":"Note dynamically inserted into SQLite database","author_id":"usr_admin","created_at":"2026-07-22T01:45:00Z","is_private":false}""")
if (saved) {
return ok 201 """{"status":"created","id":"note_103","message":"Note saved to SQLite database"}"""
}
err 400 "Failed to save note to database"
}
Deletion mirrors it: a role gate, then a boolean you must branch on.
| Caller | Route sends |
|---|---|
| without the admin role | err 403 "Forbidden: Admin role required to delete notes" |
admin, Note.delete(id) is true |
ok 200 with "status":"deleted" |
admin, Note.delete(id) is false |
err 404 "Note not found in database" |
route DELETE "/v1/notes/:id" {
val id = req.param("id")
if (!req.has_role("admin")) {
err 403 "Forbidden: Admin role required to delete notes"
}
val deleted = Note.delete(id)
if (deleted) {
return ok 200 """{"status":"deleted","id":"${id}"}"""
}
err 404 "Note not found in database"
}
First boot and files
On first start the runtime creates the database at .orbit/data/app.db under the project directory and seeds demo rows, so reads answer 200 immediately. That first boot is slow (~1.7 s of table creation and seeding) and every later boot is fast (~8 ms with the file already in place) — never compare a cold start against a warm one, and never publish a number without saying which one you measured.
Schema changes after the first boot use top-level migrations lines. Each line runs once, in declaration order, inside a transaction, and a _orbit_migrations table records what was applied, so rerunning the program does not re-apply it:
migrations "ALTER TABLE notes ADD COLUMN rank INTEGER"
migrations "CREATE INDEX IF NOT EXISTS ix_notes_rank ON notes(rank)"
A failed statement prints orbit: migrations failed to apply and exits 1. Append new lines at the end and never edit or delete an existing one: the runner versions by position. orbit migrate <file> applies the program’s migrations and exits. ORBIT_MIGRATE_DOWN=<version> rolls back to that version. The built-in seed rows land in every fresh database, so delete them when provisioning if they do not belong in production.
Database service exits with no message (Windows)
sqlite3.dll must sit next to the exe: Windows loads it from the application directory. The build links against runtime/vendor/win-x64/sqlite3.lib, but the DLL has to travel with the binary. Services without a database do not need it.
What stays yours
Transaction boundaries, backups, and input validation remain application responsibilities — the runtime gives you storage, not a DBA. Run one writer per database file; two processes against one file are untested. Stopping a service on Windows is an immediate kill, so quiesce traffic before you stop and copy the file. The troubleshooting page covers the failure modes in encounter order, and the limits page lists what 0.1.0 cannot do yet.
What next
A service that stores rows also gets request counters and per-route costs with no extra code: see observability.