Crates.io | sqlx-models-orm |
lib.rs | sqlx-models-orm |
version | 0.7.10 |
source | src |
created_at | 2022-05-27 23:30:13.898837 |
updated_at | 2024-09-11 12:08:40.796672 |
description | ActiveRecord based on SQLx for Postgres |
homepage | https://github.com/constata-eu/sqlx-models |
repository | https://github.com/constata-eu/sqlx-models |
max_upload_size | |
id | 595380 |
size | 39,953 |
ActiveRecord pattern for Rust based on SQLx. Write idiomatic DB code (Postgres only).
[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:
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),
}
}
let alice = app.human()
.insert(InsertHuman{
name: "Alice".to_string(),
age: Some(19),
likes_dogs_too: true,
})
.save().await?;
assert_eq!(alice.attrs, HumanAttrs{
id: 1,
name: "Alice".to_string(),
age: Some(19),
is_allowed_unlimited_cats: false,
likes_dogs_too: true,
});
let some_humans = app.human()
.select()
.limit(2)
.offset(1)
.likes_dogs_too_eq(false)
.order_by(HumanOrderBy::Name)
.desc(true)
.all().await?;
assert_eq!(some_humans, vec![alice]);
let updated_alice = alice.update().use_struct(UpdateHuman{
name: Some("Alice Alison".to_string()),
age: Some(None),
is_allowed_unlimited_cats: Some(true),
..Default::default()
}).save().await?;
assert_eq!(updated_alice.attrs, HumanAttrs{
id: 1,
name: "Alice Alison".to_string(),
age: None,
is_allowed_unlimited_cats: true,
likes_dogs_too: true,
});
alice.delete().await?;