#sqlite #sql-database #orm #mysql #sql #sql-query

parvati

Parvati: 确实是一个 ORM 库,而不是一个框架,用 Rust 编写

4 个版本 (稳定)

1.0.2 2024年4月26日
1.0.1 2024年3月20日
1.0.0 2024年3月19日
0.0.1 2024年3月19日

302数据库接口

Download history 119/week @ 2024-04-25 4/week @ 2024-05-02 5/week @ 2024-05-16 5/week @ 2024-05-23 1/week @ 2024-06-06 1/week @ 2024-06-20 1/week @ 2024-06-27 11/week @ 2024-07-04

每月261次 下载
parvati_derive 中使用

自定义许可证

210KB
3K SLoC

Parvati

Parvati

Parvati 库:确实是一个 ORM 库,而不是一个框架,用 Rust 编写

特性

我在我的 ORM 库中融入的主要思想是代码最简化,并易于使用。我希望用户不需要编写长链函数调用来构建简单的 SQL 查询。

  • SQLite 支持
  • MySQL 支持

使用方法

Cargo.toml

[dependencies]
parvati = {version = "1.0.2", features = ["sqlite"]} # or "mysql"
parvati_derive = "1.0.2"
#[tokio::test]
async fn test() -> Result<(), ORMError> {

    let file = std::path::Path::new("file1.db");
    if file.exists() {
        std::fs::remove_file(file)?;
    }

    let _ = env_logger::Builder::from_env(env_logger::Env::new().default_filter_or("debug")).try_init();

    let conn = ORM::connect("file1.db".to_string())?;
    let init_script = "create_table_sqlite.sql";
    conn.init(init_script).await?;

    #[derive(TableDeserialize, TableSerialize, Serialize, Deserialize, Debug, Clone)]
    #[table(name = "user")]
    pub struct User {
        pub id: i32,
        pub name: Option<String>,
        pub age: i32,
    }

    let mut user = User {
        id: 0,
        name: Some("John".to_string()),
        age: 30,
    };

    let mut user_from_db: User = conn.add(user.clone()).apply().await?;

    user.name = Some("Mary".to_string());
    let  _: User = conn.add(user.clone()).apply().await?;

    let user_opt: Option<User> = conn.find_one(user_from_db.id as u64).run().await?;
    log::debug!("User = {:?}", user_opt);

    let user_all: Vec<User> = conn.find_all().run().await?;
    log::debug!("Users = {:?}", user_all);

    user_from_db.name = Some("Mike".to_string());
    let _updated_rows: usize = conn.modify(user_from_db.clone()).run().await?;


    let user_many: Vec<User> = conn.find_many("id > 0").limit(2).run().await?;
    log::debug!("Users = {:?}", user_many);

    let query = format!("select * from user where name like {}", conn.protect("M%"));
    let result_set: Vec<Row> = conn.query(query.as_str()).exec().await?;
    for row in result_set {
        let id: i32 = row.get(0).unwrap();
        let name: Option<String> = row.get(1);
        log::debug!("User = id: {}, name: {:?}", id, name);
    }

    let updated_rows = conn.query_update("update user set age = 100").exec().await?;
    log::debug!("updated_rows: {}", updated_rows);
    let updated_rows: usize = conn.remove(user_from_db.clone()).run().await?;
    log::debug!("updated_rows: {}", updated_rows);
    conn.close().await?;

    Ok(())
}

示例输出

[2023-09-08T13:33:22Z DEBUG parvati] "CREATE TABLE user (id INTEGER PRIMARY KEY AUTOINCREMENT, name  TEXT,age INTEGER)"
[2023-09-08T13:33:22Z DEBUG parvati] "insert into user (name,age) values (\"John\",30)"
[2023-09-08T13:33:22Z DEBUG parvati] "select * from user where rowid = 1"
[2023-09-08T13:33:22Z DEBUG parvati] "insert into user (name,age) values (\"Mary\",30)"
[2023-09-08T13:33:22Z DEBUG parvati] "select * from user where rowid = 2"
[2023-09-08T13:33:22Z DEBUG parvati] "select * from user where id = 1"
[2023-09-08T13:33:22Z DEBUG test::tests] User = Some(User { id: 1, name: Some("John"), age: 30 })
[2023-09-08T13:33:22Z DEBUG parvati] "select * from user"
[2023-09-08T13:33:22Z DEBUG test::tests] Users = [User { id: 1, name: Some("John"), age: 30 }, User { id: 2, name: Some("Mary"), age: 30 }]
[2023-09-08T13:33:22Z DEBUG parvati] "update user set name = \"Mike\",age = 30 where id = 1"
[2023-09-08T13:33:22Z DEBUG parvati] "select * from user where id > 0 limit 2"
[2023-09-08T13:33:22Z DEBUG test::tests] Users = [User { id: 1, name: Some("Mike"), age: 30 }, User { id: 2, name: Some("Mary"), age: 30 }]
[2023-09-08T13:33:22Z DEBUG parvati] "select * from user where name like \"M%\""
[2023-09-08T13:33:22Z DEBUG test::tests] User = id: 1, name: Some("Mike")
[2023-09-08T13:33:22Z DEBUG test::tests] User = id: 2, name: Some("Mary")
[2023-09-08T13:33:22Z DEBUG parvati] "update user set age = 100"
[2023-09-08T13:33:22Z DEBUG test::tests] updated_rows: 2
[2023-09-08T13:33:22Z DEBUG parvati] "delete from user where id = 1"
[2023-09-08T13:33:22Z DEBUG test::tests] updated_rows: 1

依赖项

~3–21MB
~248K SLoC