add base update one

This commit is contained in:
IluaAir
2025-09-06 13:53:14 +03:00
parent d7e522d362
commit 7fe13be684
4 changed files with 31 additions and 11 deletions

View File

@@ -1,11 +1,11 @@
from typing import Any, Generic, Mapping, Sequence, Type, TypeVar
from sqlalchemy import delete, insert, select
from sqlalchemy import delete, insert, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from src.core.database import Base
from src.core.interfaces import HasId
ModelType = TypeVar("ModelType", bound=Base)
ModelType = TypeVar("ModelType", bound=HasId)
class BaseRepo(Generic[ModelType]):
@@ -44,3 +44,14 @@ class BaseRepo(Generic[ModelType]):
async def delete_one(self, **filter_by) -> None:
await self.session.execute(delete(self.model).filter_by(**filter_by))
async def update_one(self, id: int, data: dict[str, Any]) -> ModelType:
stmt = (
update(self.model)
.where(self.model.id == id)
.values(data)
.returning(self.model)
)
result = await self.session.execute(stmt)
model = result.scalar_one()
return model