Download src/database.py from valent-p/vibra: direct link, hf CLI and curl.
- Browser
- Download file 1.07 kB
-
https://huggingface.co/spaces/valent-p/vibra/resolve/main/src/database.py
- Command line
-
hf download hf://spaces/valent-p/vibra/src/database.py
-
curl -L -o database.py https://huggingface.co/spaces/valent-p/vibra/resolve/main/src/database.py
1.07 kB
| # src/database.py | |
| from sqlalchemy import event | |
| from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine | |
| from .config import settings | |
| # Create a non-blocking asynchronous engine | |
| # timeout=30.0 gives busy transactions time to clear before failing | |
| engine = create_async_engine( | |
| settings.DATABASE_URL, | |
| connect_args={"timeout": 30.0}, | |
| ) | |
| # SQLite doesn't enable foreign keys or WAL mode by default. | |
| # This listener applies these performance configurations upon opening a connection. | |
| def set_sqlite_pragma(dbapi_connection, connection_record): | |
| cursor = dbapi_connection.cursor() | |
| cursor.execute("PRAGMA foreign_keys=ON;") | |
| cursor.execute("PRAGMA journal_mode=WAL;") | |
| cursor.close() | |
| # Async sessionmaker factory | |
| AsyncSessionLocal = async_sessionmaker( | |
| bind=engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False, | |
| ) | |
| # Dependency injector function used in HTTP routes | |
| async def get_db(): | |
| async with AsyncSessionLocal() as session: | |
| yield session | |