20 lines
649 B
Python
20 lines
649 B
Python
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession
|
|
from sqlmodel import SQLModel
|
|
from .config import settings
|
|
|
|
engine = create_async_engine(settings.database_url, echo=False, future=True)
|
|
AsyncSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
async def init_db() -> None:
|
|
# Import models so SQLModel.metadata sees them
|
|
from . import models # noqa: F401
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
|
|
async def get_session() -> AsyncSession:
|
|
async with AsyncSessionLocal() as session:
|
|
yield session
|