Rust Tiny Orm

一个基于sqlx的极简orm,本项目将sql代码以derive属性方式和结构体进行关联,并自动生成相关CRUD相关API,可根据需要实现任意复杂度的数据获取。

本项目开发主要原因:

  1. 目前rust的orm产品都太大而全了,习惯了Python Django的自动化的实现,对DSL相当不习惯,个人觉得太繁琐了;
  2. 实际orm就是api和sql的转换,因为orm的实现限制,sql的复杂查询的实现都比较别扭,为了与数据库交互还得额外学习大量orm dsl的用法,有点折本求木了!

因此,是否可以将数据查询的sql和rust结合,由程序员自行控制sql的表现,这样在rust的高性能助力下,我们可以写成性能超级赞的数据库应用。

示例代码

``` use sqlx::mysql::MySqlPoolOptions; use sqlx::Row; use tinyormmacro_derive::{TinyOrm, TinyOrmQuery};

use super::*;

[derive(Debug,PartialEq, Eq)]

pub struct UserType { /// 类型编号 pub id: Option, /// 类型名称 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)]

[ormtablename_pref = "core"]

[ormtablename = "user"]

[ormselectfield = "user.*,usertype.name as usertypename, usertype.template"]

[ormselectjoin = "JOIN usertype ON usertype.id = usertypeid"]

[ormselectbyidwhere = "user.id = ? "]

[ormdeletewhere = "id = ? "]

pub struct TestUser { /// 类型编号 pub id: Option, /// 类型名称 pub name: String, /// 手机号码 pub mobilephone: String, /// 密码 password: String, /// 用户类型 pub usertype: UserType }

impl TestUser { /// 完整创建器 /// /// # 参数说明 /// /// * id 编号 /// * name 姓名 /// * mobilephone 手机 /// * password 密码 /// * usertype 用户类型 /// * org 对应机构 pub fn new( id: u32, name: &str, mobilephone: &str, password: &str, usertype: UserType, ) -> Self { Self { id: Some(id), name: name.into(), mobilephone: mobilephone.into(), password: password.into(), usertype, } } } /// 实现数据获取接口 impl TinyOrmData for TestUser { /// 将sql返回数据映射为TestUser fn ormrowmap(row: TinyOrmSqlRow) -> Self { TestUser::new( row.get::("id"), row.get("name"), row.get("mobilephone"), row.get("password"), UserType::new( row.get::("usertypeid"), row.get("usertypename"), row.get("template"), ) ) } } ```