Arel  

ci ![Latest Version] downloads

Install

Cargo.toml arel = { version = "0.2", features = ["runtime-tokio-native-tls", "sqlite"] }

Demo

```rust use arel::prelude::*;

[arel]

struct User { #[arel(primarykey)] id: i64, name: String, #[arel(rename = "type")] r#type: String, expiredat: Option>, } impl Arel for User {}

// init db from Environment std::env::setvar("DATABASEURL", "sqlite::memory:"); arel::visitor::init().await?; // or init db from code arel::visitor::getorinit(|| Box::pin(async { arel::DatabasePoolOptions::new().max_connections(5).connect("sqlite::memory:").await })).await?;

// total count let count = User::query().selectsql("COUNT(*)").fetchcount().await?; println!("total: {}", count);

// create let mut activeuser = ArelActiveUser { name: Set("n1"), ..Default::default() }; let ret = activeuser.save().await?; println!("{}", ret.rows_affected());

// select let user: User = User::query().r#where("id", 1).fetchoneas().await?; let activeuser: ArelActiveUser = user.into();

let user: ArelUser = User::query().r#where("id", 1).fetchoneas().await?; let activeuser: ArelActiveUser = user.into();

let uesrs: Vec = User::query().whererange("id", ..=10).fetchallas().await?; let uesrs: Vec = User::query().r#where("id", vec![1, 2, 3]).fetchall_as().await?;

// update let user: User = User::query().fetchoneas().await?; let mut activeuser: ArelActiveUser = user.into(); // activeuser.name.set("n-1"); activeuser.assign(&ArelActiveUser { name: Set("n-1"), ..Default::default() }); let ret = activeuser.save().await?; println!("{}", ret.rows_affected());

// destroy let ret = activeuser.destroy().await?; println!("{}", ret.rowsaffected()); ```


Query

select

rust User::query().select(vec!(["id", "name"])).to_sql();

where

rust let sql = User::query().r#where("name", "n1").r#where("id", 1).to_sql(); // where_not let sql = User::query().where_not(id: vec![1, 2, 3]).to_sql(); // where_range let sql = User::query().where_range("age", 18..25).to_sql();

joins

rust let sql = User::query().join::<Wallet>(arel::JoinType::InnerJoin).to_sql(); let sql = User::query().join_sql("INNER JOIN wallet on user.id = wallet.user_id").to_sql();

lock

rust // should use in transaction let user: User = User::query().r#where("name", "n1").lock().fetch_one_as_exec(tx);

group & having

rust let sql = User::query().group(vec!["name"]).having("age", 18..).to_sql();

order

rust let sql = User::query().order("created_at", arel::SortType::Desc).to_sql(); let sql = User::query().order_asc().to_sql(); let sql = User::query().order_desc().to_sql();

limit & offset

rust let sql = User::query().limit(10).to_sql(); let sql = User::query().offset(10)(); let sql = User::query().paginate(1, 10).to_sql();


Insert

transaction

rust User::with_transaction(|tx| { Box::pin(async move { // for entry in 1i32..=100 { // sqlx::query("INSERT INTO user (name) VALUES ($1)") // .bind(format!("name-{}", entry)) // .bind("Admin") // .execute(tx.as_mut()) // .await?; // } let mut active_user = ArelActiveUser { name: Set("n1"), r#type: Set("ADMIN"), ..Default::default() }; active_user.save_exec(tx.as_mut()).await?; Ok(None) }) }) .await?;

Update

increment

rust let user: User = User::query().r#where("id", 1).fetch_one_as().await?; let mut active_user: ArelActiveUser = user.into(); active_user.increment("lock_version", 5, |active_model, step| { let value = active_model.lock_version.try_get_i32().unwrap_or(0) + step; active_model.lock_version.set_unchanged(value); }).await?;