Rust Tiny Orm
一个基于sqlx的极简orm,本项目将sql代码以derive属性方式和结构体进行关联,并自动生成相关CRUD相关API,可根据需要实现任意复杂度的数据获取。
本项目开发主要原因:
- 目前rust的orm产品都太大而全了,习惯了Python Django的自动化的实现,对DSL相当不习惯,个人觉得太繁琐了;
- 实际orm就是api和sql的转换,因为orm的实现限制,sql的复杂查询的实现都比较别扭,为了与数据库交互还得额外学习大量orm dsl的用法,有点舍本求木了!
因此,是否可以将数据查询的sql和rust结合,由程序员自行控制sql的表现,这样在rust的高性能助力下,我们可以写成性能超级赞的数据库应用。
示例代码
use sqlx::mysql::MySqlPoolOptions;
use sqlx::Row;
use tiny_orm_macro_derive::{TinyOrm, TinyOrmQuery};
use super::*;
#[derive(Debug,PartialEq, Eq)]
pub struct UserType {
/// 类型编号
pub id: Option<u32>,
/// 类型名称
pub name: String,
/// 中断短信模板
pub template: String,
}
impl UserType {
/// 完整创建器
///
/// # 参数说明
///
/// * id 类型编号
/// * name 类型名称
/// * template 中断短信模板
pub fn new(id: u32, name: &str, template: &str) -> Self {
UserType {
id: Some(id),
name: name.into(),
template: template.into(),
}
}
}
#[allow(dead_code)]
#[derive(TinyOrm, TinyOrmQuery, Debug,PartialEq, Eq)]
#[orm_table_name_pref = "core"]
#[orm_table_name = "user"]
#[orm_select_field = "user.*,user_type.name as user_type_name, user_type.template"]
#[orm_select_join = "JOIN user_type ON user_type.id = user_type_id"]
#[orm_select_by_id_where = "user.id = ? "]
#[orm_delete_where = "id = ? "]
pub struct TestUser {
/// 类型编号
pub id: Option<u32>,
/// 类型名称
pub name: String,
/// 手机号码
pub mobile_phone: String,
/// 密码
password: String,
/// 用户类型
pub user_type: UserType
}
impl TestUser {
/// 完整创建器
///
/// # 参数说明
///
/// * id 编号
/// * name 姓名
/// * mobile_phone 手机
/// * password 密码
/// * user_type 用户类型
/// * org 对应机构
pub fn new(
id: u32,
name: &str,
mobile_phone: &str,
password: &str,
user_type: UserType,
) -> Self {
Self {
id: Some(id),
name: name.into(),
mobile_phone: mobile_phone.into(),
password: password.into(),
user_type,
}
}
}
/// 实现数据获取接口
impl TinyOrmData for TestUser {
/// 将sql返回数据映射为TestUser
fn orm_row_map(row: TinyOrmSqlRow) -> Self {
TestUser::new(
row.get::<u32, _>("id"),
row.get("name"),
row.get("mobile_phone"),
row.get("password"),
UserType::new(
row.get::<u32, _>("user_type_id"),
row.get("user_type_name"),
row.get("template"),
)
)
}
}