# Toasty + Turso

> Use Toasty, the async ORM from the Tokio project, with Turso Database.
> Canonical HTML: https://tursodb.cn/docs/sdk/rust/orm/toasty/
> Upstream Source (MIT): https://docs.turso.tech/sdk/rust/orm/toasty
> Language: en

---
[Toasty](https://github.com/tokio-rs/toasty) is an async ORM for Rust maintained by the [Tokio](https://tokio.rs/) 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](/docs/sdk/rust/quickstart/).

<div class="doc-callout doc-callout-info"><div class="doc-callout-title">NOTE</div><div class="doc-callout-body">

The Toasty Turso driver currently targets local and in-memory databases. For sync against Turso Cloud, drop down to the [`turso` crate directly](/docs/sdk/rust/quickstart/#sync-push-and-pull).

</div></div>


## 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**

```bash
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:

```rust models.rs
#[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:

<details class="doc-accordion"><summary>In-memory</summary><div class="doc-accordion-body">

```rust
let db = toasty::Db::builder()
    .models(toasty::models!(crate::*))
    .connect("turso::memory:")
    .await?;
```

</div></details>


<details class="doc-accordion"><summary>File-backed</summary><div class="doc-accordion-body">

```rust
let db = toasty::Db::builder()
    .models(toasty::models!(crate::*))
    .connect("turso:./app.db")
    .await?;
```

</div></details>


<details class="doc-accordion"><summary>With concurrent writes (MVCC)</summary><div class="doc-accordion-body">

To enable Turso's MVCC journal so multiple writers can run concurrently, build the driver explicitly and pass it to `build()`:

```rust
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.

</div></details>


**4. Create the schema and query**

```rust
#[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](https://tokio-rs.github.io/toasty/) covers models, relations, transactions, and migrations.
* The [Turso chapter](https://tokio-rs.github.io/toasty/nightly/guide/turso.html) of the Toasty guide documents the connection URL forms, the `concurrent_writes()` flag, and the `experimental_*` toggles for Turso's engine features.
* The [`turso` crate quickstart](/docs/sdk/rust/quickstart/) shows how to use Turso Database directly without an ORM, including sync against Turso Cloud.
