pub struct Transaction<'p, DB> { /* private fields */ }Expand description
Transaction builder for composing multi-entity operations.
Use Transaction::new to create a builder, chain .with_*() methods
to declare which entities you’ll use, then call .run() to execute.
§Type Parameters
'p— Pool lifetimeDB— Database pool type (e.g.,PgPool)
§Example
ⓘ
Transaction::new(&pool)
.with_users()
.with_orders()
.run(|mut ctx| async move {
let user = ctx.users().find_by_id(id).await?;
ctx.orders().create(order).await?;
Ok(())
})
.await?;Implementations§
Source§impl<'p, DB> Transaction<'p, DB>
impl<'p, DB> Transaction<'p, DB>
Source§impl<'p> Transaction<'p, Pool<Postgres>>
impl<'p> Transaction<'p, Pool<Postgres>>
Sourcepub async fn run<F, Fut, T, E>(self, f: F) -> Result<T, E>
Available on crate feature postgres only.
pub async fn run<F, Fut, T, E>(self, f: F) -> Result<T, E>
postgres only.Execute a closure within a PostgreSQL transaction.
Automatically commits on Ok, rolls back on Err or drop.
§Type Parameters
F— Closure typeFut— Future returned by closureT— Success typeE— Error type (must be convertible from sqlx::Error)
§Example
ⓘ
Transaction::new(&pool)
.with_users()
.run(|mut ctx| async move {
let user = ctx.users().create(dto).await?;
Ok(user)
})
.await?;Sourcepub async fn run_with_commit<F, Fut, T, E>(self, f: F) -> Result<T, E>
Available on crate feature postgres only.
pub async fn run_with_commit<F, Fut, T, E>(self, f: F) -> Result<T, E>
postgres only.Execute a closure within a transaction with explicit commit.
Unlike run, this method requires the closure to explicitly
commit the transaction by calling ctx.commit().
§Example
ⓘ
Transaction::new(&pool)
.run_with_commit(|mut ctx| async move {
let user = ctx.users().create(dto).await?;
ctx.commit().await?;
Ok(user)
})
.await?;Auto Trait Implementations§
impl<'p, DB> Freeze for Transaction<'p, DB>
impl<'p, DB> RefUnwindSafe for Transaction<'p, DB>where
DB: RefUnwindSafe,
impl<'p, DB> Send for Transaction<'p, DB>where
DB: Sync,
impl<'p, DB> Sync for Transaction<'p, DB>where
DB: Sync,
impl<'p, DB> Unpin for Transaction<'p, DB>
impl<'p, DB> UnwindSafe for Transaction<'p, DB>where
DB: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more