Spaces:
Sleeping
Sleeping
Download database.py from puneethasshankar/manasu: direct link, hf CLI and curl.
- Browser
- Download file 1.05 kB
-
https://huggingface.co/spaces/puneethasshankar/manasu/resolve/main/database.py
- Command line
-
hf download hf://spaces/puneethasshankar/manasu/database.py
-
curl -L -o database.py https://huggingface.co/spaces/puneethasshankar/manasu/resolve/main/database.py
1.05 kB
| from typing import AsyncGenerator | |
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | |
| from sqlalchemy.orm import DeclarativeBase | |
| from config import settings | |
| engine = create_async_engine( | |
| settings.database_url, | |
| echo=False, # logs all SQL — set False in production | |
| pool_size=10, | |
| max_overflow=20, | |
| pool_pre_ping=True, # reconnect if Neon pooler closed an idle connection | |
| pool_recycle=300, | |
| ) | |
| AsyncSessionLocal = async_sessionmaker( | |
| bind=engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| ) | |
| class Base(DeclarativeBase): | |
| pass | |
| async def get_db() -> AsyncGenerator[AsyncSession, None]: | |
| """FastAPI dependency — yields a session and closes it after the request.""" | |
| async with AsyncSessionLocal() as session: | |
| try: | |
| yield session | |
| await session.commit() | |
| except Exception: | |
| await session.rollback() | |
| raise | |
| finally: | |
| await session.close() |