ActiveRecord pattern for Rust based on SQLx. Write idiomatic DB code (Postgres only).
toml
[dependencies]
sqlx-models-orm = "0.1"
Read the in-depth tutorial that doubles as a "kitchen sink" test in the examples
These are just some of the time-saving, boilerplate-killing features:
rust
model!{
state: App,
table: humans,
struct Human {
#[sqlx_model_hints(int4, default)]
id: i32,
#[sqlx_model_hints(varchar)]
name: String,
#[sqlx_model_hints(int4)]
age: Option<i32>,
#[sqlx_model_hints(boolean, default)]
is_allowed_unlimited_cats: bool,
#[sqlx_model_hints(boolean)]
likes_dogs_too: bool,
},
has_many {
Cat(human_id),
}
}
```rust let alice = app.human() .insert(InsertHuman{ name: "Alice".tostring(), age: Some(19), likesdogs_too: true, }) .save().await?;
asserteq!(alice.attrs, HumanAttrs{ id: 1, name: "Alice".tostring(), age: Some(19), isallowedunlimitedcats: false, likesdogs_too: true, }); ```
```rust let somehumans = app.human() .select() .limit(2) .offset(1) .likesdogstooeq(false) .order_by(HumanOrderBy::Name) .desc(true) .all().await?;
asserteq!(somehumans, vec![alice]); ```
```rust let updatedalice = alice.update().usestruct(UpdateHuman{ name: Some("Alice Alison".tostring()), age: Some(None), isallowedunlimitedcats: Some(true), ..Default::default() }).save().await?;
asserteq!(updatedalice.attrs, HumanAttrs{ id: 1, name: "Alice Alison".tostring(), age: None, isallowedunlimitedcats: true, likesdogstoo: true, }); ```
rust
alice.delete().await?;