mp_message/core/storage/base.py

17 lines
464 B
Python
Raw Normal View History

2023-12-10 13:04:10 +03:00
from contextlib import asynccontextmanager
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
class BaseStorage:
def __init__(self, session_maker: async_sessionmaker):
self.__session_maker: async_sessionmaker = session_maker
@asynccontextmanager
async def get_session(self) -> AsyncSession:
session = self.__session_maker()
try:
yield session
finally:
await session.aclose()