Function for Diesel Read operations (CRUD) of generic types #4391
Unanswered
schalimidi
asked this question in
Q&A
Replies: 1 comment 2 replies
-
Its possible to write such generic functions but I advice not to do that in application code as you get a lot of complexity from the trait bounds and you essentially only replace a minor bit of code with such calls. In the end |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The following function is for insert or create:
use diesel::query_builder::{InsertStatement};
use diesel::query_dsl::methods::{ExecuteDsl};
pub fn insert_into_table<T, M>(conn: &Pgconnection, table: T, records: M)
where
T: Table,
M: diesel::Insertable,
InsertStatement<T, M::Values>: ExecuteDsl,
{
diesel::insert_into(table)
.values(records)
.execute(conn);
}
Can do something like this for reads or load?
pub fn read_from_table<T, M>(conn: &Pgconnection, table: T, records: M)
where
T: Table,
SelectStatement: ExecuteDsl,
{
diesel::load(table)
.execute(conn);
}
Beta Was this translation helpful? Give feedback.
All reactions