Spaces:
Sleeping
Sleeping
Download app/database.py from aislyntech/attendance: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/spaces/aislyntech/attendance/resolve/main/app/database.py
- Command line
-
hf download hf://spaces/aislyntech/attendance/app/database.py
-
curl -L -o database.py https://huggingface.co/spaces/aislyntech/attendance/resolve/main/app/database.py
1.21 kB
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker, declarative_base | |
| from app.config import DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD | |
| # =============================== | |
| # DATABASE URL | |
| # =============================== | |
| DB_PORT = DB_PORT or 3306 | |
| DATABASE_URL = ( | |
| f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}" | |
| ) | |
| # =============================== | |
| # ENGINE CONFIGURATION | |
| # =============================== | |
| engine = create_engine( | |
| DATABASE_URL, | |
| pool_pre_ping=True, # auto reconnect if connection drops | |
| pool_size=5, # number of persistent connections | |
| max_overflow=10, # extra connections if needed | |
| echo=False # NEVER True in production | |
| ) | |
| # =============================== | |
| # SESSION | |
| # =============================== | |
| SessionLocal = sessionmaker( | |
| autocommit=False, | |
| autoflush=False, | |
| bind=engine | |
| ) | |
| # =============================== | |
| # BASE MODEL | |
| # =============================== | |
| Base = declarative_base() | |
| # =============================== | |
| # DEPENDENCY | |
| # =============================== | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() |