Skip to main content

transaction

Function transaction 

Source
pub async fn transaction<F, Fut, R>(pool: &PgPool, f: F) -> OrmResult<R>
where F: FnOnce(Transaction<'static, Postgres>) -> Fut, Fut: Future<Output = OrmResult<R>>,
Expand description

Выполняет асинхронную функцию внутри транзакции. При ошибке откатывает, при успехе — коммитит.

let (user, profile) = orm::transaction(&pool, |tx| async move {
    let user = User::create(data, tx).await?;
    let profile = Profile::create(NewProfile { user_id: user.id }, tx).await?;
    Ok((user, profile))
}).await?;