File size: 987 Bytes
ce2db9f 0d53b01 eeb3f17 ce2db9f eeb3f17 ce2db9f d1f2042 eeb3f17 ce2db9f eeb3f17 ce2db9f eeb3f17 0d53b01 eeb3f17 ce2db9f eeb3f17 ce2db9f eeb3f17 ce2db9f |
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 |
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) |