python-wechat-kf/app/database.py

17 lines
533 B
Python

from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from app.config import settings
engine = create_async_engine(settings.database_url, echo=False)
async_session = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async def get_db() -> AsyncSession:
async with async_session() as session:
yield session
async def init_db():
from app.models.message import Base
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)