init: guarddog-nexus project skeleton
This commit is contained in:
27
guarddog_nexus/database.py
Normal file
27
guarddog_nexus/database.py
Normal file
@@ -0,0 +1,27 @@
|
||||
"""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
|
||||
Reference in New Issue
Block a user