Spaces:
Sleeping
Sleeping
| from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession | |
| from sqlalchemy.orm import DeclarativeBase | |
| from .config import settings | |
| engine = create_async_engine(settings.database_url, echo=False) | |
| AsyncSessionLocal = async_sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) | |
| class Base(DeclarativeBase): | |
| pass | |
| async def get_db(): | |
| async with AsyncSessionLocal() as session: | |
| yield session | |
| async def init_db(): | |
| async with engine.begin() as conn: | |
| await conn.run_sync(Base.metadata.create_all) | |