Skip to content

Commit

Permalink
Switch Toasty to use proc macros for schema declaration (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
carllerche authored Mar 12, 2025
1 parent 2bccf9e commit 6d08491
Show file tree
Hide file tree
Showing 101 changed files with 5,184 additions and 6,987 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
"src/cli",
"src/codegen",
"src/codegen2",
"src/core",
"src/macros",
"src/toasty",
Expand All @@ -18,6 +19,7 @@ members = [
# Examples
"examples/composite-key",
"examples/hello-toasty",
"examples/hello-toasty-macro",
"examples/cratehub",
"examples/user-has-one-profile",

Expand Down Expand Up @@ -46,11 +48,12 @@ rand = "0.8.5"
serde = { version = "1.0.214", features = ["derive"] }
serde_json = "1.0.132"
std-util = { path = "src/std-util" }
syn = "2.0.86"
syn = { version = "2.0.86", features = ["full", "extra-traits", "visit-mut"] }
tokio = { version = "1.18", features = ["full"] }
tokio-postgres = "0.7.13"
tokio-stream = { version = "0.1.16", default-features = false }
toasty-codegen = { path = "src/codegen" }
toasty-codegen2 = { path = "src/codegen2" }
toasty-core = { path = "src/core" }
toasty-driver = { path = "src/driver" }
uuid = { version = "1.11.0", features = ["v4", "fast-rng"] }
70 changes: 37 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,46 @@ Instead, Toasty exposes features based on the target database.

## Using Toasty

Projects that use toasty create a schema file to define the application's data
model. Here is the schema file from the
[hello-toasty](examples/hello-toasty/schema.toasty) example:
You will define your data model using Rust structs annotated with the
`#[toasty::model]` procedural macro. Here is the
[hello-toasty](examples/hello-toasty/src/main.rs) example.

```rust
model User {
#[derive(Debug)]
#[toasty::model]
struct User {
#[key]
#[auto]
id: Id,
id: Id<Self>,

name: String,

#[unique]
email: String,

#[has_many]
todos: [Todo],

moto: Option<String>,
}

model Todo {
#[derive(Debug)]
#[toasty::model]
struct Todo {
#[key]
#[auto]
id: Id,
id: Id<Self>,

#[index]
user_id: Id<User>,

#[relation(key = user_id, references = id)]
#[belongs_to(key = user_id, references = id)]
user: User,

title: String,
}
```

Using the Toasty CLI tool, you will generate all necessary Rust code for working
with this data model. The generated code for the above schema is
[here](examples/hello-toasty/src/db).

Then, you can easily work with the data model:

```rust
Expand All @@ -63,7 +64,7 @@ let user = User::create()
.await?;

// Load the user from the database
let user = User::find_by_id(&user.id).get(&db).await?
let user = User::get_by_id(&db, &user.id).await?

// Load and iterate the user's todos
let mut todos = user.todos().all(&db).await.unwrap();
Expand Down Expand Up @@ -96,36 +97,39 @@ default, a toasty application schema will map 1-1 with a database schema.
However, additional annotations may be specified to customize how the
application data model maps to the database schema.

For example, the [crate-hub](examples/cratehub/schema.toasty) examples shows how
For example, the [crate-hub](examples/cratehub/src/main.rs) examples shows how
to map multiple application models to a single database table.

```rust
table user_and_packages {
model User {
#[key]
#[auto]
id: Id,
#[derive(Debug)]
#[toasty::model(table = user_and_packages)]
struct User {
#[key]
#[auto]
id: Id<Self>,

name: String,
name: String,

#[unique]
email: String,
#[unique]
email: String,

packages: [Package],
}
#[has_many]
packages: [Package],
}

#[key(partition = user_id, local = id)]
model Package {
#[relation(key = user_id, references = id)]
user: User,
#[derive(Debug)]
#[toasty::model(table = user_and_packages)]
#[key(partition = user_id, local = id)]
struct Package {
#[belongs_to(key = user_id, references = id)]
user: User,

user_id: Id<User>,
user_id: Id<User>,

#[auto]
id: Id,
#[auto]
id: Id<Self>,

name: String,
}
name: String,
}
```

Expand Down
27 changes: 0 additions & 27 deletions examples/composite-key/schema.toasty

This file was deleted.

7 changes: 0 additions & 7 deletions examples/composite-key/src/db/mod.rs

This file was deleted.

Loading

0 comments on commit 6d08491

Please sign in to comment.