Skip to main content

Module unit_of_work

Module unit_of_work 

Source
Expand description

Unit of Work pattern implementation for SQLModel Session.

The Unit of Work pattern tracks all changes made during a session and flushes them atomically in the correct dependency order.

§Overview

The Unit of Work:

  • Tracks new objects to INSERT
  • Tracks modified (dirty) objects to UPDATE
  • Tracks deleted objects to DELETE
  • Determines flush order based on foreign key dependencies
  • Detects dependency cycles and reports errors
  • Executes all changes in a single atomic transaction

§Example

let mut uow = UnitOfWork::new();

// Register models (extracts FK dependencies)
uow.register_model::<Team>();
uow.register_model::<Hero>();

// Track changes
uow.track_new(&team, &team_key);
uow.track_new(&hero, &hero_key);
uow.track_dirty(&existing_hero, &hero_key);
uow.track_deleted(&old_team, &old_team_key);

// Compute flush plan (checks for cycles)
let plan = uow.compute_flush_plan()?;

// Execute (in a transaction)
plan.execute(&cx, &conn).await?;

Structs§

PendingCounts
Count of pending operations by type.
UnitOfWork
Tracks and manages all pending changes in a session.

Enums§

UowError
Error type for Unit of Work operations.