File size: 780 Bytes
ca7a2c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
"""Database session and engine configuration."""
from collections.abc import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.core.config import settings
# Create async engine
engine = create_async_engine(
settings.database_url,
echo=False, # Disable SQL logging (embedding vectors are too verbose)
pool_pre_ping=True,
)
# Session factory
async_session_maker = async_sessionmaker(
engine,
class_=AsyncSession,
expire_on_commit=False,
)
async def get_db() -> AsyncGenerator[AsyncSession, None]:
"""Dependency for getting database session."""
async with async_session_maker() as session:
try:
yield session
finally:
await session.close()
|