Open benjamingb opened 3 years ago
Hello!
Did you try to pass something like that?
pub async fn stream_info(
conn: impl sqlx::Executor<'_, Database = sqlx::Postgres>,
stream_uuid: &str,
) -> Result<Stream, sqlx::Error> {}
Hi @Freyskeyd
I used it this way but it doesn't work either, the goal is for the trait to be generic
pub struct TodoRepo;
#[async_trait]
impl<P: sqlx::Executor<'_, Database = sqlx::Postgres>> ITodoRepo<P> for TodoRepo {
async fn list(pool: &P) -> Result<Vec<TodoType>> {
let rowset = sqlx::query_as!(TodoSchema, r#"SELECT * FROM todo"#)
.fetch_all(pool)
.await?
.iter()
.map(|row| hydrate(row))
.collect();
Ok(rowset)
}
}
|
| impl<'a, P: sqlx::Executor<'_, Database = sqlx::Postgres>> ITodoRepo<'a, P> for TodoRepo {
|
|
| .fetch_all(pool)
| ^^^^ the trait `sqlx::Executor<'_>` is not implemented for `&P`
Did you try to remove the &
?, you can still use a reference.
See:
Method pass a mutable ref: https://github.com/Freyskeyd/chekov/blob/master/event_store/src/storage/postgres.rs#L89
The read_stream
is here: https://github.com/Freyskeyd/chekov/blob/master/event_store/src/storage/postgres/sql.rs#L11
when I use a direct reference if it works
async fn list(pool: &PgPool) -> Result<Vec<TodoType>>
https://github.com/benjamingb/rust/todo.rs#L13
I am trying to implement it with a trait so that it is generic and can be used in other crates
Here is a reference to the project https://github.com/benjamingb/rust-gql/tree/develop/src
Mmh I didnt' have time to check on a running exemple but I think something like that would work no?
Or am I missing something ?
pub struct TodoRepo;
#[async_trait]
impl<P: sqlx::Database>> ITodoRepo<P> for TodoRepo {
async fn list(pool: impl sqlx::Executor<'_, Database = P>) -> Result<Vec<TodoType>> {
let rowset = sqlx::query_as!(TodoSchema, r#"SELECT * FROM todo"#)
.fetch_all(pool)
.await?
.iter()
.map(|row| hydrate(row))
.collect();
Ok(rowset)
}
}
Thank you very much for your help but it still doesn't work :(
Hi @benjamingb Did you find a solution? I'm facing the same problem
Hi @skerkour, not yet, I'm looking for other alternatives
Is it possible to pass the reference of a connection in a trait?
I am trying to do this but I get an error
This is how I invoke the list method