Add some ORM/Diesel helper macros
This commit is contained in:
parent
68ca95f166
commit
368a3ff10e
2 changed files with 78 additions and 0 deletions
75
src/helpers.rs
Normal file
75
src/helpers.rs
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
//! Some ORM helpers
|
||||||
|
//!
|
||||||
|
//! The macros, types and traits in this module are used to create a simple GraphQL API on top of
|
||||||
|
//! models that are in the database.
|
||||||
|
|
||||||
|
#![allow(unused_macros)]
|
||||||
|
|
||||||
|
macro_rules! all {
|
||||||
|
($model_name:ident, $conn:expr) => {{
|
||||||
|
use diesel::associations::HasTable;
|
||||||
|
|
||||||
|
let table = $crate::models::$model_name::table();
|
||||||
|
table.load(&$conn)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! retrieve {
|
||||||
|
($model_name:ident, $primary_key:expr, $conn:expr) => {{
|
||||||
|
use diesel::associations::HasTable;
|
||||||
|
|
||||||
|
let table = $crate::models::$model_name::table();
|
||||||
|
table.find($primary_key).first(&$conn)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! create {
|
||||||
|
($model_name:ident, $object:expr, $conn:expr) => {{
|
||||||
|
use diesel::associations::HasTable;
|
||||||
|
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl, Table};
|
||||||
|
|
||||||
|
let table = $crate::models::$model_name::table();
|
||||||
|
let primary_key = table.primary_key();
|
||||||
|
$conn.transaction::<$model_name, diesel::result::Error, _>(|| {
|
||||||
|
diesel::insert_into(table).values($object).execute(&$conn)?;
|
||||||
|
table.order(primary_key.desc()).first(&$conn)
|
||||||
|
})
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! update {
|
||||||
|
($model_name:ident, $primary_key:expr, $object:expr, $conn:expr) => {{
|
||||||
|
use diesel::associations::HasTable;
|
||||||
|
|
||||||
|
let table = $crate::models::$model_name::table();
|
||||||
|
$conn.transaction::<_, diesel::result::Error, _>(|| {
|
||||||
|
diesel::update(table.find($primary_key))
|
||||||
|
.set($object)
|
||||||
|
.execute(&$conn)?;
|
||||||
|
table.find($primary_key).first(&$conn)
|
||||||
|
})
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! destroy {
|
||||||
|
($model_name:ident, $primary_key:expr, $conn:expr) => {{
|
||||||
|
use diesel::associations::HasTable;
|
||||||
|
|
||||||
|
let table = $crate::models::$model_name::table();
|
||||||
|
let result: Result<$model_name, _> = get!($model_name, $primary_key, $conn);
|
||||||
|
match result {
|
||||||
|
Ok(_) => diesel::delete(table.find($primary_key)).execute(&$conn),
|
||||||
|
Err(e) => Err(e),
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! destroy_all {
|
||||||
|
($model_name:ident, $conn:expr) => {{
|
||||||
|
use diesel::associations::HasTable;
|
||||||
|
use diesel::RunQueryDsl;
|
||||||
|
|
||||||
|
let table = $crate::models::$model_name::table();
|
||||||
|
diesel::delete(table).execute(&$conn)
|
||||||
|
}};
|
||||||
|
}
|
|
@ -11,6 +11,9 @@ use rocket::{catchers, routes, Rocket};
|
||||||
use rocket_contrib::database;
|
use rocket_contrib::database;
|
||||||
use rocket_contrib::serve::StaticFiles;
|
use rocket_contrib::serve::StaticFiles;
|
||||||
|
|
||||||
|
#[macro_use]
|
||||||
|
pub mod helpers;
|
||||||
|
|
||||||
pub mod catchers;
|
pub mod catchers;
|
||||||
pub mod handlers;
|
pub mod handlers;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue