AIdea-Server / src /db /database.py
Ahmed Mostafa
v1.5
d74863e
"""
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
from src.utils.config import settings
DATABASE_URL = settings.database_url
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