39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
"""BaseRepository: enforces transaction boundary at UoW layer.
|
|
|
|
Repository layer must NOT commit/rollback. Use _flush() instead.
|
|
Transaction boundary is controlled by SharedUnitOfWork in Service/UseCase layer.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
class BaseRepository:
|
|
"""Base repository class that prohibits commit/rollback.
|
|
|
|
Subclasses should use self.db for ORM operations and
|
|
await self._flush to get DB-generated values,
|
|
but must NEVER call self.db.commit or self.db.rollback.
|
|
"""
|
|
|
|
def __init__(self, db: AsyncSession) -> None:
|
|
self._db = db
|
|
|
|
@property
|
|
def db(self) -> AsyncSession:
|
|
return self._db
|
|
|
|
async def _flush(self) -> None:
|
|
"""Flush to DB without committing. Safe alternative to commit."""
|
|
await self._db.flush()
|
|
|
|
async def commit(self) -> None:
|
|
raise NotImplementedError(
|
|
"Repository must not commit. Use SharedUnitOfWork for transaction control."
|
|
)
|
|
|
|
async def rollback(self) -> None:
|
|
raise NotImplementedError(
|
|
"Repository must not rollback. Use SharedUnitOfWork for transaction control."
|
|
)
|