Crates.io | rusql-alchemy |
lib.rs | rusql-alchemy |
version | 0.3.0 |
source | src |
created_at | 2024-06-18 21:53:21.013476 |
updated_at | 2024-08-22 16:21:06.938565 |
description | Rust Alchemy is Django ORM like lib for Rust |
homepage | https://github.com/russenger/rusql-alchemy |
repository | https://github.com/russenger/rusql-alchemy |
max_upload_size | |
id | 1276243 |
size | 32,553 |
Just for fun! XD
.env
fileDATABASE_URL=sqlite://<dabasase.db>
Cargo.toml
[dependencies.rusql-alchemy]
git = "https://github.com/j03-dev/rusql-alchemy"
branch= "main"
features = ["sqlite"] # the default features is sqlite
use rusql_alchemy::prelude::*;
#[derive(Debug, Model, FromRow)]
struct User {
#[model(primary_key=true, auto=true, null=false)]
id: Integer,
#[model(unique=true, null=false)]
name: String,
#[model(null=false)]
age: Integer,
#[model(default="user")]
role: String
}
.env
fileDATABASE_URL=postgres://<user>:<password>@<hostname>/<dbname>
Cargo.toml
[dependencies.rusql-alchemy]
git = "https://github.com/j03-dev/rusql-alchemy"
branch="main"
default-features = false
features = ["postgres"]
Serial
typeuse rusql_alchemy::prelude::*;
#[derive(Debug, Model, FromRow)]
struct User_ {
#[model(primary_key=true)]
id: Serial,
#[model(unique=true, null=false)]
name: String,
#[model(null=false)]
age: Integer,
#[model(default="user")]
role: String
}
use rusql_alchemy::prelude::*;
#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;
migrate([Use], &conn);
}
use rusql_alchemy::prelude::*;
#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;
User_ {
name: "johnDoe@gmail.com".to_string(),
email: "21john@gmail.com".to_string(),
password: "p455w0rd".to_string(),
age: 18,
weight: 60.0,
..Default::default()
}
.save(&conn)
.await;
let users = User_::all(&conn).await;
println!("{:#?}", users);
User_::create(
kwargs!(
name = "joe",
email = "24nomeniavo@gmail.com",
password = "strongpassword",
age = 19,
weight = 80.1
),
&conn,
)
.await;
}
use rusql_alchemy::prelude::*;
#[tokio::main]
async fn main() {
let conn = config::db::Database::new().await.conn;
let users = User_::all(&conn).await;
println!("{:#?}", users);
let user = User_::get(
kwargs!(email == "24nomeniavo@gmail.com").and(kwargs!(password == "strongpassword")),
&conn,
).await;
println!("{:#?}", user);
let users = User_::filter(kwargs!(age <= 18), &conn).await;
println!("{:#?}", users);
}
use rusql_alchemy::prelude::*;
#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;
if let Some(mut user) = User_::get(
kwargs!(email == "24nomeniavo@gmail.com").and(kwargs!(password == "strongpassword")),
&conn,
)
.await
{
user.role = "admin".into();
user.update(&conn).await;
}
}
use rusql_alchemy::prelude::*;
#[tokio::main]
async fn main() {
let conn = Database::new().await.conn;
if let Some(user) = User_::get(kwargs!(role == "admin"), &conn).await {
user.delete(&conn).await; // delete one
}
let users = User_::all(&conn).await;
users.delete(&conn).await; // delete all
}