aiverse / app /db /session.py
junaidte14's picture
before claude
61f8245
Raw History Blame Contribute Delete
1.86 kB
"""
Database session management
Handles database connection, session creation, and dependency injection
"""
from sqlalchemy.ext.asyncio import (
create_async_engine,
async_sessionmaker,
AsyncSession,
AsyncEngine,
)
from typing import AsyncGenerator
from app.core.config import settings
from sqlalchemy.pool import NullPool
engine: AsyncEngine = create_async_engine(
settings.async_database_url,
echo=settings.DATABASE_ECHO,
poolclass=NullPool,
)
# Create async session factory
async_session_maker = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
autocommit=False,
autoflush=False,
)
async def get_db() -> AsyncGenerator[AsyncSession, None]:
"""
Dependency for getting database session
Yields:
AsyncSession: Database session
Usage:
@app.get("/users")
async def get_users(db: AsyncSession = Depends(get_db)):
result = await db.execute(select(User))
return result.scalars().all()
"""
async with async_session_maker() as session:
try:
yield session
#await session.commit()
except Exception:
#await session.rollback()
raise
finally:
await session.close()
async def init_db() -> None:
"""
Initialize database
Creates all tables if they don't exist
Note: In production, use Alembic migrations instead
"""
from app.db.base import Base
async with engine.begin() as conn:
# Drop all tables (only for development!)
# await conn.run_sync(Base.metadata.drop_all)
# Create all tables
await conn.run_sync(Base.metadata.create_all)
async def close_db() -> None:
"""
Close database connections
Call this on application shutdown
"""
await engine.dispose()