#orm #sqlite #postgresql #database-access #dao #flexible #rustorm

rustorm_dao

Dao 提供了一种灵活的方式来访问数据库中的数据

14 个不稳定版本 (6 个破坏性更新)

0.20.0 2021 年 3 月 9 日
0.18.0 2020 年 12 月 1 日
0.5.0 2020 年 1 月 8 日
0.4.3 2019 年 12 月 28 日
0.1.0 2018 年 3 月 11 日

#1707数据库接口

Download history 60/week @ 2024-03-11 46/week @ 2024-03-18 42/week @ 2024-03-25 107/week @ 2024-04-01 71/week @ 2024-04-08 57/week @ 2024-04-15 57/week @ 2024-04-22 52/week @ 2024-04-29 46/week @ 2024-05-06 60/week @ 2024-05-13 59/week @ 2024-05-20 75/week @ 2024-05-27 55/week @ 2024-06-03 53/week @ 2024-06-10 36/week @ 2024-06-17 35/week @ 2024-06-24

每月 192 次下载
6 个crate中使用 (4 个直接使用)

MIT 许可证

27KB
792 代码行

Build Status

rustorm

Rustorm

Financial Contributors on Open Collective Latest Version Build Status MIT licensed

Rustorm 是一个以 SQL 为主的 ORM,专注于数据库类型到适当 Rust 类型的转换的易用性。

选择记录

use rustorm::{
    DbError,
    FromDao,
    Pool,
    ToColumnNames,
    ToTableName,
};

#[derive(Debug, FromDao, ToColumnNames, ToTableName)]
struct Actor {
    actor_id: i32,
    first_name: String,
}

#[cfg(any(feature="with-postgres", feature = "with-sqlite"))]
fn main() {
    let mut pool = Pool::new();
    #[cfg(feature = "with-sqlite")]
    let db_url = "sqlite://sakila.db";
    #[cfg(feature = "with-postgres")]
    let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
    let em = pool.em(db_url).unwrap();
    let sql = "SELECT * FROM actor LIMIT 10";
    let actors: Result<Vec<Actor>, DbError> =
        em.execute_sql_with_return(sql, &[]);
    println!("Actor: {:#?}", actors);
    let actors = actors.unwrap();
    assert_eq!(actors.len(), 10);
    for actor in actors {
        println!("actor: {:?}", actor);
    }
}
#[cfg(feature="with-mysql")]
fn main() {
   println!("see examples for mysql usage, mysql has a little difference in the api");
}

插入并显示插入的记录

use chrono::{
    offset::Utc,
    DateTime,
    NaiveDate,
};
use rustorm::{
    DbError,
    FromDao,
    Pool,
    TableName,
    ToColumnNames,
    ToDao,
    ToTableName,
};


#[cfg(any(feature="with-postgres", feature = "with-sqlite"))]
fn main() {
    mod for_insert {
        use super::*;
        #[derive(Debug, PartialEq, ToDao, ToColumnNames, ToTableName)]
        pub struct Actor {
            pub first_name: String,
            pub last_name: String,
        }
    }

    mod for_retrieve {
        use super::*;
        #[derive(Debug, FromDao, ToColumnNames, ToTableName)]
        pub struct Actor {
            pub actor_id: i32,
            pub first_name: String,
            pub last_name: String,
            pub last_update: DateTime<Utc>,
        }
    }

    let mut pool = Pool::new();
    #[cfg(feature = "with-sqlite")]
    let db_url = "sqlite://sakila.db";
    #[cfg(feature = "with-postgres")]
    let db_url = "postgres://postgres:p0stgr3s@localhost/sakila";
    let em = pool.em(db_url).unwrap();
    let tom_cruise = for_insert::Actor {
        first_name: "TOM".into(),
        last_name: "CRUISE".to_string(),
    };
    let tom_hanks = for_insert::Actor {
        first_name: "TOM".into(),
        last_name: "HANKS".to_string(),
    };

    let actors: Result<Vec<for_retrieve::Actor>, DbError> =
        em.insert(&[&tom_cruise, &tom_hanks]);
    println!("Actor: {:#?}", actors);
    assert!(actors.is_ok());
    let actors = actors.unwrap();
    let today = Utc::now().date();
    assert_eq!(tom_cruise.first_name, actors[0].first_name);
    assert_eq!(tom_cruise.last_name, actors[0].last_name);
    assert_eq!(today, actors[0].last_update.date());

    assert_eq!(tom_hanks.first_name, actors[1].first_name);
    assert_eq!(tom_hanks.last_name, actors[1].last_name);
    assert_eq!(today, actors[1].last_update.date());
}
#[cfg(feature="with-mysql")]
fn main() {
   println!("see examples for mysql usage, mysql has a little difference in the api");
}

Rustorm 完全由 diwata 使用

许可证: MIT

贡献者

代码贡献者

此项目得益于所有贡献者。[贡献].

财务贡献者

成为财务贡献者,帮助我们维持社区。[贡献]

个人

组织

用您的组织支持此项目。您的徽标将显示在这里,并带有指向您网站的链接。[贡献]

依赖

~6.5MB
~120K SLoC