Skip to content

Records and queries

This page lists every way to write and read records in Rust. If you have not opened a database yet, start with the quickstart.

#[derive(Debug, Serialize, Deserialize, Collection)]
#[collection("todos")]
struct Todo {
title: String,
done: bool,
priority: u32,
due: Option<String>,
tags: Vec<String>,
}

The struct is stored the way serde serializes it. Serde attributes such as #[serde(rename = "…")] and #[serde(default)] work as usual.

The derive adds one function for each field, such as Todo::done(). You use these functions to name fields in writes and queries.

You choose the ID of each record. Which kind of ID to use depends on the record:

  • Use a random ID for an ordinary record, such as a to-do or a note. The uuid crate makes one. Two copies then never create the same ID.
  • Use a fixed ID for a record that every copy must share, such as your app’s settings, or a record that has an ID in another system. If two copies each create the record, the two merge into one, field by field.
let mut tx = db.begin();
// A random ID for an ordinary record.
tx.insert("lists", &Uuid::new_v4().to_string(), json!({ "name": "Groceries" }))?;
// A fixed ID for a record that every copy must share.
tx.insert("lists", "home", json!({ "name": "Home" }))?;
let id = Uuid::new_v4().to_string();
todo("Buy milk", 2, &["home"]).insert(&mut tx, &id)?;
todo("Ship", 3, &[]).insert(&mut tx, &Uuid::new_v4().to_string())?;
Todo::update(&mut tx, &id, [Todo::done().set(true), Todo::due().set("2026-12-01".to_string())])?;
Todo::delete(&mut tx, &id)?;
tx.commit()?; // saves all four writes together
Call What it does
insert Creates a record. It fails if the ID is in use or was deleted.
update Changes the fields you give and keeps the others.
delete Deletes the record.

Changes are saved when you call commit. If you drop the transaction without calling commit, nothing is saved.

You cannot remove a field from a record. An Option field that you set to None is stored as null.

Names have these limits:

  • A field name must not be empty and must not contain spaces or other whitespace.
  • A collection name and an ID must not be empty and must not contain /.

get returns None when a record was never created and also when it was deleted. Use state to tell these apart. A state is Live, Deleted or Absent.

let mut tx = db.begin();
assert!(Todo::get(&mut tx, &id)?.is_none()); // None, because the record was deleted
assert_eq!(tx.state("todos", &id)?, State::Deleted);

Build a filter from the field functions, and pass it to filter. A filter can test only the top-level fields of a record, not fields nested inside an object.

let urgent = Todo::done().eq(false).and(Todo::priority().gte(2u32));
let urgent_or_due = urgent.or(Todo::due().lt("2026-12-01"));
let found: Vec<(String, Todo)> = Todo::select().filter(urgent_or_due).run(&mut tx)?;
Todo::select().filter(Todo::priority().is_in([1u32, 2])).run(&mut tx)?;
Todo::select().filter(Todo::done().ne(true)).run(&mut tx)?;
Todo::select().filter(Todo::title().starts_with("Buy")).run(&mut tx)?;
Todo::select().filter(Todo::tags().contains("home")).run(&mut tx)?;
Todo::select().filter(Todo::due().is_null()).run(&mut tx)?;

The tests you can use depend on the type of the field. The compiler rejects a test that does not fit.

Test Field types Records it matches
eq Booleans, numbers, strings The field equals the value.
ne Booleans, numbers, strings The field holds a different value. Records without the field do not match.
is_in Booleans, numbers, strings The field equals any value in the list.
lt, lte, gt, gte Numbers, strings The field is within the limits.
starts_with Strings The field starts with the text. The test is case-sensitive.
contains Vec The array includes the value.
is_null Option The field holds null.

Combine filters with and and or.

A number in a filter must have the same type as the field. For a u32 field, write 2u32, not 2.

Without order_by, records come back in order of their IDs.

With order_by, records are sorted by the field you name, then by ID. Records are left out when the field is missing, or when it holds an array or an object.

Use offset and limit to get one page of results. Use count to get the total number of matches.

let not_done = || Todo::select().filter(Todo::done().eq(false)).order_by(Todo::due().desc());
let second_page = not_done().offset(25).limit(25).run(&mut tx)?;
let total = not_done().count(&mut tx)?; // all matches, ignoring offset and limit

count_by groups the matching records by the value of one field, and counts each group.

let by_priority = Todo::count_by(&mut tx, Todo::priority(), Some(Todo::done().eq(false)))?;

You do not need a struct. The transaction also reads and writes serde_json values directly. A query is then a JSON object, with the same form as in TypeScript.

let mut tx = db.begin();
tx.update("lists", "home", json!({ "name": "At home" }))?;
let lists = tx.query("lists", &Query::parse(&json!({ "where": { "name": { "startsWith": "At" } } }))?)?;
tx.commit()?;

starling-db has no indexes. This affects how fast each call is:

  • get is fast at any size, because it reads one record directly.
  • run, count and count_by read every record in the collection.
  • A query with a limit and no order_by stops reading when it has enough records.

The home page shows timings for 100,000 records.

starling-db does not have:

  • joins
  • sums, averages, minimums or maximums
  • substring search or full-text search
  • filters on fields inside nested objects
  • sorting by more than one field
  • unique constraints or foreign-key constraints
  • a single call that inserts or updates

There are no constraints because a constraint could not survive a merge. Two copies could each make a change that is valid alone but breaks the rule when combined.