The Unit of Work (UoW) pattern is essential for maintaining data integrity, ensuring that multiple database operations either all succeed or all fail together (atomicity).Documentation Index
Fetch the complete documentation index at: https://fluxcrud.mahimai.dev/llms.txt
Use this file to discover all available pages before exploring further.
Why Unit of Work?
By default, FluxCRUD repositories can manage their own sessions (simple mode). However, when you need to perform an operation that involves:- Creating a User
- Creating a Profile for that User
- Sending a welcome email (simulated)
Usage
Use theUnitOfWork class as an async context manager. It automatically handles transaction management: begin, commit, and rollback.
How It Works
- Shared Session: The UoW creates a single SQLAlchemy
AsyncSessionand shares it across all repositories retrieved viauow.repository(). - Auto-Rollback: If an exception occurs within the
async withblock, the transaction is strictly rolled back. - Auto-Commit: If the block exits successfully,
commit()is called. - Resource Cleanup: The session is guaranteed to close, preventing connection leaks.
Type Safety
Theuow.repository(Model, Schema) method is fully typed, ensuring that you get a Repository[Model, Schema] instance with correct type hints for all operations.

