sync
This commit is contained in:
BIN
api/uow/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
api/uow/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
api/uow/__pycache__/database.cpython-311.pyc
Normal file
BIN
api/uow/__pycache__/database.cpython-311.pyc
Normal file
Binary file not shown.
BIN
api/uow/__pycache__/uow_base.cpython-311.pyc
Normal file
BIN
api/uow/__pycache__/uow_base.cpython-311.pyc
Normal file
Binary file not shown.
@@ -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
23
api/uow/uow_base.py
Normal 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()
|
Reference in New Issue
Block a user