You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#[derive(Clone,Debug)]pubstructStore{pubconnection:PgPool,}pubstructUserContext{pubtenant_id:String,pubuser_id:String,}implStore{pubasyncfnnew(db_url:&str) -> Self{matchPgPoolOptions::new().max_connections(8).connect(db_url).await{Ok(pool) => Store{connection: pool },Err(e) => {panic!("Couldn't establish DB connection! Error: {}", e)}}}}
And repositories, for instance, AccountsRepo:
pubstructAccountsRepo{pubpool:PgPool,}implFrom<PgPool>forAccountsRepo{fnfrom(pool:PgPool) -> Self{Self{ pool }}}implAccountsRepo{pubfnnew(pool:PgPool) -> Self{Self{ pool }}#[tracing::instrument(skip(self))]pubasyncfnget_user_by_id(&self,id:Uuid) -> Result<Option<User>, sqlx::Error>{
sqlx::query_as::<_,User>(r#" ... "#,).bind(id).fetch_optional(&self.pool).await}
...
}
And it's used like:
let repo = AccountsRepo::from(state.db_store.connection.clone());
Is there an idiomatic and neat way to make the repositories ALSO accept transactions (or any Executor)? For example, so it can ALSO be used like this:
let pool = state.db_store.connection.clone();letmut tx = match pool.begin().await{Ok(tx) => tx,Err(e) => returnCustomAppError::from(e).into_response(),};let repo = AccountsRepo::from(&mut*tx);// For transactions
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I currently have a setup where I have a store:
And repositories, for instance, AccountsRepo:
And it's used like:
Is there an idiomatic and neat way to make the repositories ALSO accept transactions (or any
Executor
)? For example, so it can ALSO be used like this:Beta Was this translation helpful? Give feedback.
All reactions