mirror of
https://github.com/Kludex/awesome-fastapi-projects.git
synced 2024-12-18 08:10:18 +00:00
28 lines
715 B
Python
28 lines
715 B
Python
|
"""
|
||
|
The Unit of Work pattern implementation.
|
||
|
|
||
|
To learn more about the UoW, see:
|
||
|
https://www.cosmicpython.com/book/chapter_06_uow.html
|
||
|
"""
|
||
|
from collections.abc import AsyncGenerator
|
||
|
from contextlib import asynccontextmanager
|
||
|
|
||
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||
|
|
||
|
from app.database import async_session_maker
|
||
|
|
||
|
|
||
|
@asynccontextmanager
|
||
|
async def async_session_uow() -> AsyncGenerator[AsyncSession, None]:
|
||
|
"""
|
||
|
Provide a transactional scope around a series of operations.
|
||
|
|
||
|
:return: a UoW instance
|
||
|
"""
|
||
|
async with async_session_maker() as session:
|
||
|
async with session.begin():
|
||
|
try:
|
||
|
yield session
|
||
|
finally:
|
||
|
await session.rollback()
|