22 lines
646 B
Python
22 lines
646 B
Python
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
from app.config.settings import settings
|
|
|
|
engine = create_async_engine(settings.database_url, echo=settings.debug)
|
|
async_session = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async with async_session() as session:
|
|
yield session
|
|
|
|
|
|
async def init_db():
|
|
import app.models # noqa: F401 — ensure all models are registered
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|