Download database.py from hanz245/whspr: direct link, hf CLI and curl.
- Browser
- Download file 826 Bytes
-
https://huggingface.co/spaces/hanz245/whspr/resolve/main/database.py
- Command line
-
hf download hf://spaces/hanz245/whspr/database.py
-
curl -L -o database.py https://huggingface.co/spaces/hanz245/whspr/resolve/main/database.py
826 Bytes
| """ | |
| Database connection — SQLAlchemy + PostgreSQL (Supabase) | |
| """ | |
| import os | |
| from dotenv import load_dotenv | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.orm import sessionmaker | |
| load_dotenv() | |
| DATABASE_URL = os.getenv("DATABASE_URL") | |
| if not DATABASE_URL: | |
| raise RuntimeError("DATABASE_URL is not set in .env") | |
| _connect_args = {} | |
| if DATABASE_URL.startswith("postgresql"): | |
| _connect_args = {"sslmode": "require"} | |
| engine = create_engine( | |
| DATABASE_URL, | |
| connect_args=_connect_args, | |
| pool_pre_ping=True, | |
| pool_recycle=3600, | |
| echo=False, | |
| ) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |