Spaces:
Running
Running
| """SQLAlchemy async engine + session factory.""" | |
| from __future__ import annotations | |
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | |
| from sqlalchemy.orm import DeclarativeBase | |
| from app.config import get_settings | |
| class Base(DeclarativeBase): | |
| pass | |
| _settings = get_settings() | |
| engine = create_async_engine( | |
| _settings.database_url, | |
| echo=False, | |
| pool_pre_ping=True, | |
| ) | |
| SessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) | |
| async def get_session() -> AsyncSession: # type: ignore[misc] | |
| async with SessionLocal() as session: | |
| yield session | |