auto-save 2026-04-13 19:12 (+1)
This commit is contained in:
19
api/app/db.py
Normal file
19
api/app/db.py
Normal file
@@ -0,0 +1,19 @@
|
||||
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
|
||||
Reference in New Issue
Block a user