19 lines
647 B
Python
19 lines
647 B
Python
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||
|
|
|
||
|
|
from backend.core.config import settings
|
||
|
|
from backend.db.base import Base
|
||
|
|
|
||
|
|
engine = create_async_engine(settings.db_url, echo=False, pool_pre_ping=True, future=True)
|
||
|
|
AsyncSessionLocal = async_sessionmaker(bind=engine, expire_on_commit=False, class_=AsyncSession)
|
||
|
|
|
||
|
|
|
||
|
|
async def get_session() -> AsyncSession:
|
||
|
|
async with AsyncSessionLocal() as session:
|
||
|
|
yield session
|
||
|
|
|
||
|
|
|
||
|
|
async def init_models() -> None:
|
||
|
|
"""Create all tables based on imported models."""
|
||
|
|
async with engine.begin() as conn:
|
||
|
|
await conn.run_sync(Base.metadata.create_all)
|