28 lines
748 B
Python
28 lines
748 B
Python
"""Async SQLite database setup via SQLAlchemy."""
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
|
from guarddog_nexus.config import config
|
|
|
|
DATABASE_URL = f"sqlite+aiosqlite:///{config.database_path}"
|
|
|
|
_engine = create_async_engine(DATABASE_URL, echo=False)
|
|
_async_session = async_sessionmaker(_engine, class_=AsyncSession, expire_on_commit=False)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
async def init_db():
|
|
import guarddog_nexus.models # noqa: F401
|
|
|
|
async with _engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
|
|
async def get_session() -> AsyncSession:
|
|
async with _async_session() as session:
|
|
yield session
|