Makhfi_AI / db /session.py
Aasher's picture
fix(db): update async engine configuration to use connection pool settings
d1f2042
raw
history blame contribute delete
987 Bytes
from sqlmodel import SQLModel
from sqlalchemy.ext.asyncio import create_async_engine
from sqlmodel.ext.asyncio.session import AsyncSession
from core.config import get_settings
settings = get_settings()
async_engine = create_async_engine(
settings.DATABASE_URL,
echo=False,
future=True,
pool_pre_ping=True,
pool_size=10, # The number of connections to keep open in the pool
max_overflow=5, # The number of connections to allow in "overflow"
pool_timeout=30, # How long to wait for a connection from the pool
)
async def get_db():
"""
Dependency function to get an async database session.
Ensures the session is always closed after the request.
"""
async with AsyncSession(async_engine) as session:
yield session
async def create_db_and_tables():
"""
Utility function to create database tables asynchronously.
"""
async with async_engine.begin() as conn:
await conn.run_sync(SQLModel.metadata.create_all)