Tired of learning super complex Orms? bored of doing sqlbuilder.select("fields").from("table")?
sometimes you just want a quick, easy to use sql statement that matches your structs definitions even if it changes, well this crate is for you
Currently this only supports postgres
Usage examples
use orm_macro::OrmRepository;
use orm_macro_derive::GetRepository;
#[derive(Debug, Default, GetRepository)]
#[table_name("books")]
pub struct Books {
pub id: i64,
pub description: Option<String>,
pub title: Option<String>,
pub author_name : String,
}
#[derive(Debug, Default, GetRepository)]
#[table_name("books")]
pub struct BooksUpdateDto {
pub description: Option<String>,
}
#[derive(Debug, Default, GetRepository)]
#[table_name("books")]
pub struct BooksCreateDto {
pub title : String,
pub description: Option<String>,
}
pub struct BookRepository {}
impl BookRepository {
async fn update(&self, body : BooksUpdateDto) -> Result<Vec<Books>, sqlx::Error> {
let sql = BooksUpdateDtoOrm::builder().update();
let db_response = sqlx::query_as(sql.as_str())
.bind(body.description)
.fetch_all(&*self.db)
.await?;
Ok(db_response)
}
async fn create(&self, body : BooksCreateDto) -> Result<Vec<Books>, sqlx::Error> {
let sql = BooksCreateDtoOrm::builder().create();
let db_response = sqlx::query_as(sql.as_str())
.bind(body.title)
.bind(body.description)
.fetch_one(&*self.db)
.await?;
Ok(db_response)
}
async fn delete(&self, id: i64 ) -> Result<Vec<Books>, sqlx::Error> {
let sql = BooksOrm::builder().delete();
let db_response = sqlx::query_as(sql.as_str())
.bind(id)
.fetch_one(&*self.db)
.await?;
Ok(db_response)
}
async fn find_all(&self) -> Result<Vec<Books>, sqlx::Error> {
let sql = BooksOrm::builder().find();
let db_response = sqlx::query_as(sql.as_str())
.fetch_all(&*self.db)
.await?;
Ok(db_response)
}
}