Toasty + Turso
Use Toasty, the async ORM from the Tokio project, with Turso Database.
Toasty is an async ORM for Rust maintained by the Tokio project. It ships a native Turso driver (toasty-driver-turso) that connects to a local Turso Database file or an in-memory database — the same engine you get from the turso crate.
The Toasty Turso driver currently targets local and in-memory databases. For sync against Turso Cloud, drop down to the turso crate directly.
Prerequisites
- Rust 1.95 or newer (Toasty’s minimum supported version)
- A Rust project (
cargo init) - Tokio as your async runtime
1. Install Toasty with the Turso driver
cargo add toasty --features turso
cargo add tokio --features full
cargo add uuid
2. Define your models
Toasty derives schema, queries, and relations from plain Rust structs:
#[derive(Debug, toasty::Model)]
struct User {
#[key]
#[auto]
id: uuid::Uuid,
name: String,
#[unique]
email: String,
#[has_many]
todos: toasty::Deferred<Vec<Todo>>,
}
#[derive(Debug, toasty::Model)]
struct Todo {
#[key]
#[auto]
id: uuid::Uuid,
#[index]
user_id: uuid::Uuid,
#[belongs_to(key = user_id, references = id)]
user: toasty::Deferred<User>,
title: String,
}
3. Connect to Turso
Pass a turso: URL to Db::builder — either an in-memory database or a file path:
In-memory
let db = toasty::Db::builder()
.models(toasty::models!(crate::*))
.connect("turso::memory:")
.await?;
File-backed
let db = toasty::Db::builder()
.models(toasty::models!(crate::*))
.connect("turso:./app.db")
.await?;
With concurrent writes (MVCC)
To enable Turso’s MVCC journal so multiple writers can run concurrently, build the driver explicitly and pass it to build():
use toasty_driver_turso::Turso;
let driver = Turso::file("app.db").concurrent_writes();
let db = toasty::Db::builder()
.models(toasty::models!(crate::*))
.build(driver)
.await?;
When concurrent_writes() is enabled, write-write conflicts surface as a serialization failure — check err.is_serialization_failure() and retry the transaction.
4. Create the schema and query
#[tokio::main]
async fn main() -> toasty::Result<()> {
let mut db = toasty::Db::builder()
.models(toasty::models!(crate::*))
.connect("turso:./app.db")
.await?;
db.push_schema().await?;
let user = toasty::create!(User {
name: "Ada Lovelace",
email: "ada@example.com",
})
.exec(&mut db)
.await?;
toasty::create!(in user.todos() { title: "ship Toasty + Turso" })
.exec(&mut db)
.await?;
let reloaded = User::get_by_email(&mut db, &user.email).await?;
println!("{reloaded:#?}");
Ok(())
}
Where to go next
- The Toasty guide covers models, relations, transactions, and migrations.
- The Turso chapter of the Toasty guide documents the connection URL forms, the
concurrent_writes()flag, and theexperimental_*toggles for Turso’s engine features. - The
tursocrate quickstart shows how to use Turso Database directly without an ORM, including sync against Turso Cloud.