Module transaction

Module transaction 

Source
Expand description

Transaction support for entity-derive.

This module provides type-safe transaction management with automatic commit/rollback semantics. It uses the builder pattern for composing multiple repositories into a single transaction context.

§Overview

§Example

use entity_derive::prelude::*;

async fn transfer(pool: &PgPool, from: Uuid, to: Uuid, amount: Decimal) -> Result<(), AppError> {
    Transaction::new(pool)
        .with_accounts()
        .with_transfers()
        .run(|mut ctx| async move {
            let from_acc = ctx.accounts().find_by_id(from).await?.ok_or(AppError::NotFound)?;

            ctx.accounts().update(from, UpdateAccount {
                balance: Some(from_acc.balance - amount),
            }).await?;

            ctx.transfers().create(CreateTransfer { from, to, amount }).await?;
            Ok(())
        })
        .await
}

Structs§

Transaction
Transaction builder for composing repositories.
TransactionContext
Active transaction context with repository adapters.

Enums§

TransactionError
Error type for transaction operations.

Traits§

TransactionOps
Trait for transaction types that can be committed or rolled back.
TransactionRunner
Trait for executing operations within a transaction.
Transactional
Trait for types that can begin a transaction.