Files
urllists/database.py
2025-11-30 12:19:31 +00:00

28 lines
701 B
Python

from typing import AsyncGenerator
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker, declarative_base
from config import get_settings
settings = get_settings()
Base = declarative_base()
engine = create_async_engine(
settings.DATABASE_URL,
echo=settings.DEBUG, # Only echo SQL in debug mode
future=True
)
AsyncSessionLocal = sessionmaker(
autocommit=False,
autoflush=False,
bind=engine,
class_=AsyncSession,
expire_on_commit=False, # Important for async sessions
)
async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
async with AsyncSessionLocal() as session:
yield session