This commit is contained in:
2024-03-04 10:15:28 +00:00
parent 4df5770e76
commit 92c52954c8
25 changed files with 80 additions and 114 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -1,5 +1,11 @@
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from contextlib import (AbstractContextManager, asynccontextmanager,
contextmanager)
from typing import Callable
from sqlalchemy.ext.asyncio import (AsyncSession, async_sessionmaker,
create_async_engine)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
Base = declarative_base()
@@ -7,16 +13,18 @@ Base = declarative_base()
class Database:
def __init__(self, db_url: str) -> None:
self._engine = create_async_engine(db_url, echo=True)
self._async_session = async_sessionmaker(
self._session_factory = async_sessionmaker(
self._engine,
class_=AsyncSession,
expire_on_commit=False,
)
@property
def session(self):
return self._session_factory()
async def __aenter__(self):
async with self._async_session() as session:
self.session = session
return self
return self
async def __aexit__(self, *args):
await self.session.rollback()

23
api/uow/uow_base.py Normal file
View File

@@ -0,0 +1,23 @@
from contextlib import AbstractContextManager
from typing import Iterable
from dependency_injector.providers import Callable
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from sqlalchemy.orm import Session
from api.model.user import User
class UowBase:
def __init__(
self,
session_factory,
) -> None:
self.session = session_factory
async def get_all_users(self):
async with self.session as s:
query = select(User)
rr = await s.execute(query)
return rr.scalars().all()