Spaces:
Sleeping
Sleeping
| """ | |
| Database connection and session management. | |
| """ | |
| from typing import AsyncGenerator | |
| from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlmodel import SQLModel | |
| from sqlmodel.ext.asyncio.session import AsyncSession | |
| DATABASE_URL = "sqlite+aiosqlite:///./database.db" | |
| async_engine: AsyncEngine = create_async_engine( | |
| DATABASE_URL, | |
| echo=False, | |
| future=True, | |
| ) | |
| async def create_db_and_tables(): | |
| """Create database tables defined in SQLModel models.""" | |
| async with async_engine.begin() as conn: | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| async def get_session() -> AsyncGenerator[AsyncSession, None]: | |
| """ | |
| Dependency to provide async database session. | |
| Yields: | |
| AsyncSession: Database session | |
| """ | |
| async_session = sessionmaker( | |
| bind=async_engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False | |
| ) | |
| async with async_session() as session: | |
| yield session |