-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbase_repository.py
41 lines (32 loc) · 1.36 KB
/
base_repository.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from typing import Generic, TypeVar, Type, Union, Optional
from uuid import UUID
from fastapi import Depends
from sqlalchemy.orm import selectinload
from sqlmodel import SQLModel, select
from app.common.exceptions.http import NotFoundException
from app.common.infra.sql_adaptors import get_async_session, AsyncSession
ModelType = TypeVar("ModelType", bound=SQLModel)
class BaseSQLRepository(Generic[ModelType]):
def __init__(self, model: Type[ModelType], session: AsyncSession = Depends(get_async_session)):
"""
Object with default methods to Create, Read, Update and Delete (CRUD).
"""
self.model = model
self.session = session
async def get(self, *, uid: Union[UUID, str]) -> Optional[ModelType]:
response = await self.session.exec(
select(self.model)
.where(self.model.id == uid)
.options(selectinload('*'))
)
response = response.one_or_none()
if not response:
raise NotFoundException(detail="Not found with ID {}".format(uid))
return response
async def add(self, *, model: ModelType):
await self.save(model=model, refresh=True)
async def save(self, *, model: ModelType, refresh=False):
await self.session.add(model)
await self.session.commit()
if refresh:
await self.session.refresh(model)