Spaces:
Paused
Paused
Download apps/api/src/app/db.py from nathycodes/presentercoach: direct link, hf CLI and curl.
- Browser
- Download file 778 Bytes
-
https://huggingface.co/spaces/nathycodes/presentercoach/resolve/main/apps/api/src/app/db.py
- Command line
-
hf download hf://spaces/nathycodes/presentercoach/apps/api/src/app/db.py
-
curl -L -o db.py https://huggingface.co/spaces/nathycodes/presentercoach/resolve/main/apps/api/src/app/db.py
778 Bytes
| from sqlalchemy import create_engine | |
| from sqlalchemy.engine.url import make_url | |
| from sqlalchemy.orm import declarative_base, sessionmaker | |
| from app.config import settings | |
| normalized_db_url = settings.database_url | |
| if normalized_db_url.startswith("postgres://"): | |
| normalized_db_url = normalized_db_url.replace("postgres://", "postgresql://", 1) | |
| url = make_url(normalized_db_url) | |
| connect_args = {} | |
| if url.drivername.startswith("sqlite"): | |
| connect_args = {"check_same_thread": False} | |
| engine = create_engine(normalized_db_url, pool_pre_ping=True, connect_args=connect_args) | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| Base = declarative_base() | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |