Quickstart
This page shows you how to use starling-db in a Rust program. You will open a database, write and read records, and merge two copies of the data.
starling-db is in alpha. Its file format and its API can still change.
1. Add the crates
Section titled “1. Add the crates”cargo add starling-db serde_jsoncargo add serde --features derivecargo add uuid --features v4The uuid crate makes random IDs for your records.
2. Define a record type
Section titled “2. Define a record type”A record is a JSON object. Each record has an ID that you choose, and belongs to a named collection.
For most records, use a random ID. Choose an ID explains when to use a fixed one.
To use a struct as a record, derive Collection on it and name its collection. The struct must
also derive the serde traits.
use serde::{Deserialize, Serialize};use starling_db::typed::Collection;use starling_db::{Db, Result};use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, Collection)]#[collection("todos")]struct Todo { title: String, done: bool, due: Option<String>,}The derive adds one function for each field, such as Todo::done(). You use these functions to
write and query, so the compiler checks field names and types for you.
3. Open a database
Section titled “3. Open a database”// The path is the file. "laptop" is the device name of this copy.let mut laptop = Db::open(dir.join("laptop.db"), "laptop")?;Db::open takes two arguments:
- The path of the file. A database is one file. If the file does not exist, it is created. The file stays locked while the database is open.
- The device name. It identifies this copy of the data when copies merge. Use a different name for each copy, such as a random ID that you create once and store. Use the same name every time you open this copy. Merging explains why.
4. Write records
Section titled “4. Write records”All reads and writes happen in a transaction. Call begin to start one and commit to save its
changes. Either all of the changes are saved, or none are.
let mut tx = laptop.begin();let milk = Todo { title: "Buy milk".into(), done: false, due: Some("2026-12-01".into()),};let id = Uuid::new_v4().to_string(); // a random ID, so no other copy creates the same onemilk.insert(&mut tx, &id)?;Todo::update(&mut tx, &id, [Todo::done().set(true)])?; // changes only `done`tx.commit()?;5. Read records
Section titled “5. Read records”Read one record by its ID with get. Find records with select.
let mut tx = laptop.begin();let todo: Option<Todo> = Todo::get(&mut tx, &id)?;let done: Vec<(String, Todo)> = Todo::select() .filter(Todo::done().eq(true)) .order_by(Todo::title().asc()) .run(&mut tx)?;drop(tx);6. Merge two copies
Section titled “6. Merge two copies”Any two copies of a database can merge.
let mut phone = Db::open(dir.join("phone.db"), "phone")?;phone.merge_from(&mut laptop)?;assert_eq!(phone.root(), laptop.root()); // both hold the same recordsmerge_from works when your program has both databases open. To merge copies on different devices,
see Merging.
Next steps
Section titled “Next steps”- Records and queries lists every way to write, filter and sort records.
- Merging explains how copies merge and how to keep them in sync.
- Files explains where the data is stored and when a write is safe.
- docs.rs has the full API reference.