Spaces:
Sleeping
Sleeping
| from typing import AsyncGenerator | |
| from sqlmodel import SQLModel | |
| from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine | |
| from sqlmodel.ext.asyncio.session import AsyncSession | |
| from src.core.config import settings | |
| async_engine = create_async_engine( | |
| settings.ASYNC_DATABASE_URL, | |
| echo=False, | |
| future=True, | |
| pool_size=20, | |
| max_overflow=40, | |
| pool_timeout=30, | |
| connect_args={"ssl": True}, | |
| ) | |
| async_session = async_sessionmaker( | |
| async_engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| autoflush=False | |
| ) | |
| async def init_db(): | |
| async with async_engine.begin() as conn: | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| async def get_async_session() -> AsyncGenerator[AsyncSession, None]: | |
| async with async_session() as session: | |
| yield session | |