40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
"""Database base configuration"""
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
|
from app.core.config import settings
|
|
|
|
# Create base class for models
|
|
Base = declarative_base()
|
|
|
|
# Select database URL based on configuration
|
|
database_url = settings.database_url_supabase if settings.use_supabase else settings.database_url
|
|
|
|
# Create async engine
|
|
engine = create_async_engine(
|
|
database_url,
|
|
echo=settings.debug,
|
|
future=True,
|
|
pool_pre_ping=True, # Enable connection health checks
|
|
pool_size=5,
|
|
max_overflow=10
|
|
)
|
|
|
|
# Create async session factory
|
|
async_session = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False
|
|
)
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
"""Dependency to get database session"""
|
|
async with async_session() as session:
|
|
try:
|
|
yield session
|
|
await session.commit()
|
|
except Exception:
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close() |