#orm #sql #postgresql #sqlbuilder #utilities

orm_macro

最简单的SQL构建器,映射到您的结构体,看起来像ORM

11个版本 (7个稳定)

1.3.0 2024年5月23日
1.2.1 2024年5月6日
0.1.3 2024年4月30日

#333数据库接口

MIT 协议

13KB
176

厌倦了学习超级复杂的ORM?厌烦了编写sqlbuilder.select("fields").from("table")(这会随着代码的演变而变得过时)?有时候你只是需要一个快速、易于使用的SQL语句,即使它发生变化,也能匹配你的结构体定义,那么这个crate就是为你准备的

免责声明

我是Rust的新手,当时我不知道版本是如何工作的,所以尽管版本号显示为1.,但这个crate仍然可能发生变化

目录

  1. 安装
  2. 使用

安装

将此放在您的cargo.toml中

orm_macro = "1.3.0"
orm_macro_derive = { version = "1.3.0", features = ["postgres"] }  

功能标志"postgres"使用postgres风格绑定,例如

DELETE FROM table WHERE id = $1 # postgres bindings
DELETE FROM table WHERE id = ? # this bindings are used by mysql and sqlite

如果您想使用mysql绑定,那么在您的cargo.toml中

orm_macro =  "1.3.0"
orm_macro_derive = { version = "1.3.0", features = ["mysql"] } 

使用

我将使用此结构体作为示例,并使用sqlx作为数据库驱动程序

///bring this to scope
use orm_macro::OrmRepository;
use orm_macro_derive::GetRepository;

//GetRepository will make a new struct with methods that 
//build sql statements using your struct fields
//The new struct will be named {yourStructName}Orm
#[derive(Debug, Default, GetRepository)]
#[table_name(books)]
#[id(id_books)] // Set the id of your table, this will be used in RETURNING and where clauses 
pub struct Books {
    pub id_books: i64,
    pub description: Option<String>,
    pub title: Option<String>,
    pub author_name : String,
}

// works really well with Dto's
#[derive(Debug, Default, GetRepository)]
#[table_name(books)]
#[id(id_books)] // Set the id of your table, this will be used en the RETURNING clauses 
pub struct BooksUpdateDto {
    pub description: Option<String>,
}


#[derive(Debug, Default, GetRepository)]
#[table_name(books)]
#[id(id_books)] // Set the id of your table, this will be used en the RETURNING clauses 
pub struct BooksCreateDto {
    pub title : String,
    pub description: Option<String>,
}

查找

async fn find_all() -> Result<Vec<Books>, sqlx::Error> {

    
        let builder = BooksOrm::builder();
    

        /// this would generate: SELECT id_books,description,title FROM books 
        ///since it is a string you can use it with any sql driver
        let sql =  builder.find();

        let db_response = sqlx::query_as(sql)
        .fetch_all(&executor)
        .await?;

        Ok(db_response)
  }

通过ID查找

async fn find_by_id() -> Result<Vec<Books>, sqlx::Error> {

    
        let builder = BooksOrm::builder();
    

        ///this generates: SELECT id_books,description,title,author_name FROM books WHERE id_books = $1
        ///This method will be named: find_by_{your_table_id}
        let sql =  builder.find_by_id_books();

        let db_response = sqlx::query_as(sql)
        .fetch_all(&executor)
        .await?;

        Ok(db_response)
  }

创建

async fn create(&self, body : BooksCreateDto) -> Result<Vec<Books>, sqlx::Error> {
        let builder =  BooksCreateDtoOrm::builder();

		let sql = builder.create();

		/// this would generate: INSERT INTO books (title,description) VALUES($1,$2) RETURNING id_books,title,description
        let db_response = sqlx::query_as(sql)
        .bind(body.title)
        .bind(body.description)
        .fetch_one(&executor)
        .await?;

        Ok(db_response)
 }

更新

    async fn update(body : BooksUpdateDto) -> Result<Vec<Books>, sqlx::Error> {

        let builder =  BooksUpdateDtoOrm::builder();

        /// this would generate: UPDATE books SET description = $1 WHERE id_books = $2 RETURNING id_books, description
		let sql = builder.update();

        let db_response = sqlx::query_as(sql))
        .bind(body.description)
        .fetch_all(&*self.db)
        .await?;


        Ok(db_response)
    }

删除

    async fn delete(id: i64) -> Result<Vec<Books>, sqlx::Error> {

        let builder =  BooksOrm::builder();

		/// this would generate: DELETE FROM books WHERE id_books = $1  RETURNING id_books,title,description,author_name
		let sql = builder.delete();
		
        let db_response = sqlx::query_as(sql)
        .bind(id)
        .fetch_one(&*self.db)
        .await?;

        Ok(db_response)
    }

请通过问题选项卡提出功能建议或报告错误

依赖关系

~280–730KB
~17K SLoC