ForcePilot/backend/package/yuxi/repositories/base_repository.py
Kris f9f08221fc chore: 整理代码风格与优化细节
本次提交包含多类代码优化:
1. 修复多处单行代码换行格式,统一代码排版
2. 为外部系统模块新增快捷菜单配置
3. 优化前端API请求参数命名一致性
4. 补充媒体下载超限错误码与领域异常类
5. 完善仓储层更新逻辑,支持显式清空字段
6. 优化部分测试用例与工具函数代码结构
7. 为微信插件白名单缓存增加过期时间
2026-07-14 15:13:29 +08:00

36 lines
1.1 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.")