Self-hosted US Congress monitoring platform with AI policy briefs, bill/member/topic follows, ntfy + RSS + email notifications, alignment scoring, collections, and draft-letter generator. Authored by: Jack Levy
54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
from contextlib import asynccontextmanager
|
|
from typing import AsyncGenerator
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
|
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
|
|
|
from app.config import settings
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
# ─── Async engine (FastAPI) ───────────────────────────────────────────────────
|
|
|
|
async_engine = create_async_engine(
|
|
settings.DATABASE_URL,
|
|
echo=False,
|
|
pool_size=10,
|
|
max_overflow=20,
|
|
)
|
|
|
|
AsyncSessionLocal = async_sessionmaker(
|
|
async_engine,
|
|
expire_on_commit=False,
|
|
class_=AsyncSession,
|
|
)
|
|
|
|
|
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
|
async with AsyncSessionLocal() as session:
|
|
yield session
|
|
|
|
|
|
# ─── Sync engine (Celery workers) ────────────────────────────────────────────
|
|
|
|
sync_engine = create_engine(
|
|
settings.SYNC_DATABASE_URL,
|
|
pool_size=5,
|
|
max_overflow=10,
|
|
pool_pre_ping=True,
|
|
)
|
|
|
|
SyncSessionLocal = sessionmaker(
|
|
bind=sync_engine,
|
|
autoflush=False,
|
|
autocommit=False,
|
|
)
|
|
|
|
|
|
def get_sync_db() -> Session:
|
|
return SyncSessionLocal()
|