Replies: 2 comments 2 replies
-
There is no need to use a generic parameter there, as all of the pooled connection types implement async fn do_query_generic(cxn: &mut AsyncPgConnection, id: i32) {}
let mut cxn: Object<AsyncDieselConnectionManager<AsyncPgConnection>> =
pool.get().await.expect("get connection");
do_query_generic(&mut *cxn, 1).await; Otherwise it's a choice between either referring to the backend type or specifying a whole bunch of query specific trait bounds so that rustc can proof that you can call that function for all possible backends. (The later one is complicated and should be avoided) |
Beta Was this translation helpful? Give feedback.
-
Understood (and I had that much working using The use case I'm considering is migration projects, where one is trying to seamlessly transition from one database backend to another while preserving an API that has a number of helper functions that perform simple filters on queries, or a light transform on a key type before performing the query. For my specific case, I'm looking at a Mysql-to-Postgres migration project, so it seems relevant (and interesting!). |
Beta Was this translation helpful? Give feedback.
-
This is likely more of a Rust typing question than a diesel-async question, but I think it's probably worth covering here.
I've been struggling with coming up with a way to wrap queries that take a connection handle, whether obtained directly or from a pool. The goal is to continue to use a [generic] handle without passing a pool handle (or even requiring a pool) into the wrapper function.
Here's a code sample that demonstrates the most generic way I was able to do this. I still end up having to refer to the database backend in the functions, and the problem I'm trying to solve (mostly out of principle, since I don't really want to use other databases) is how to not refer to the backend.
This is a complete example (I included the schema/model so that it will compile, even though it won't run without extra effort).
So, the question there is: how does one write
do_query_generic()
there, referencing a generic backend?Beta Was this translation helpful? Give feedback.
All reactions