24 lines
682 B
Python
24 lines
682 B
Python
|
|
"""SQLAlchemy async engine and session."""
|
||
|
|
|
||
|
|
from collections.abc import AsyncGenerator
|
||
|
|
|
||
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||
|
|
from sqlalchemy.orm import DeclarativeBase
|
||
|
|
|
||
|
|
from app.config import settings
|
||
|
|
|
||
|
|
|
||
|
|
class Base(DeclarativeBase):
|
||
|
|
"""Base class for all SQLAlchemy models."""
|
||
|
|
pass
|
||
|
|
|
||
|
|
|
||
|
|
engine = create_async_engine(settings.DATABASE_URL, echo=False, future=True)
|
||
|
|
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
||
|
|
|
||
|
|
|
||
|
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
||
|
|
"""Dependency: yields a database session."""
|
||
|
|
async with async_session() as session:
|
||
|
|
yield session
|