#lib #orm #primary-key #django-orm #rust

rusql-alchemy

Rust Alchemy is Django ORM like lib for Rust

3 unstable releases

0.2.0 Jun 30, 2024
0.1.2 Jun 19, 2024
0.1.1 Jun 19, 2024
0.1.0 Jun 18, 2024

#341 in Database interfaces

Download history 253/week @ 2024-06-13 111/week @ 2024-06-20 168/week @ 2024-06-27 14/week @ 2024-07-04

546 downloads per month
Used in russenger

Apache-2.0

27KB
423 lines

Rusql Alchemy

Rusql Alchemy is Django ORM like library for Rust

Why ?

Just for fun! XD

Sqlite

Setup .env file

DATABASE_URL=sqlite://<dabasase.db>

Setup Cargo.toml

[dependencies.rusql-alchemy]
git = "https://github.com/j03-dev/rusql-alchemy"
branch= "main"
features = ["sqlite"] # the default features is sqlite

Model

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
}

Postgres

Setup .env file

DATABASE_URL=postgres://<user>:<password>@<hostname>/<dbname>

Setup Cargo.toml

[dependencies.rusql-alchemy]
git = "https://github.com/j03-dev/rusql-alchemy"
branch="main"
default-features = false
features = ["postgres"]

Model: In postgres primary key should be Serial type

use 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
}

Migrate

use rusql_alchemy::prelude::*;

#[tokio::main]
async fn main() {
    let conn = Database::new().await.conn;
    migrate([Use], &conn);
}

Query

Insert

#[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;
}

Select

#[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);
}

Update

#[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;
    }
}

Delete

#[tokio::main]
async fn main() {
    let conn = config::db::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;
    user.delete(&conn).await; // delete all
}

Dependencies

~10–27MB
~399K SLoC