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

rusql-alchemy

Rust Alchemy 是类似于 Django ORM 的 Rust 库

5 个不稳定版本

新版本 0.3.0 2024 年 8 月 22 日
0.3.0-rc2024 年 8 月 6 日
0.2.0 2024 年 6 月 30 日
0.1.2 2024 年 6 月 19 日

#609 in 数据库接口

Download history 253/week @ 2024-06-13 111/week @ 2024-06-20 168/week @ 2024-06-27 14/week @ 2024-07-04 32/week @ 2024-07-25 86/week @ 2024-08-01 17/week @ 2024-08-08

每月下载量 135
用于 russenger

Apache-2.0

33KB
457

Rusql Alchemy

Rusql Alchemy 是类似于 Django ORM 的 Rust 库

为什么?

只是为了好玩!XD

Sqlite

设置 .env 文件

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

Postgres

设置 .env 文件

DATABASE_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"]

模型:在 postgres 中,主键应该是 Serial 类型

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
}

迁移

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: "[email protected]".to_string(),
        email: "[email protected]".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 = "[email protected]",
            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 == "[email protected]").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 == "[email protected]").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
}

依赖项

~10–26MB
~401K SLoC