Spaces:
Sleeping
Sleeping
Download database/database.py from JAYASWAROOP/detectai-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/spaces/JAYASWAROOP/detectai-backend/resolve/main/database/database.py
- Command line
-
hf download hf://spaces/JAYASWAROOP/detectai-backend/database/database.py
-
curl -L -o database.py https://huggingface.co/spaces/JAYASWAROOP/detectai-backend/resolve/main/database/database.py
1.04 kB
| # app/db/database.py | |
| import os | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker, declarative_base | |
| from dotenv import load_dotenv | |
| from app.core.config import DATABASE_URL # 👈 Import from centralized config | |
| # --- Load environment variables --- | |
| load_dotenv() | |
| # --- Create SQLAlchemy engine --- | |
| engine = create_engine( | |
| DATABASE_URL, | |
| echo=False, # set to True if you want SQL logs | |
| future=True, | |
| pool_pre_ping=True, | |
| pool_recycle=300 | |
| ) | |
| # --- Create session factory --- | |
| SessionLocal = sessionmaker( | |
| autocommit=False, | |
| autoflush=False, | |
| bind=engine, | |
| future=True | |
| ) | |
| # --- Base class for models --- | |
| Base = declarative_base() | |
| # --- Dependency for FastAPI routes --- | |
| def get_db(): | |
| """Dependency to get DB session for each request""" | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| # --- Optional Debug Print --- | |
| if __name__ == "__main__": | |
| print("✅ Database connected successfully!") | |
| print(f"Using DATABASE_URL = {DATABASE_URL}") | |