Download src/apps/shared/db/__init__.py from anaselamr/yaz: direct link, hf CLI and curl.
- Browser
- Download file 963 Bytes
-
https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/shared/db/__init__.py
- Command line
-
hf download hf://spaces/anaselamr/yaz/src/apps/shared/db/__init__.py
-
curl -L -o __init__.py https://huggingface.co/spaces/anaselamr/yaz/resolve/main/src/apps/shared/db/__init__.py
963 Bytes
| """ | |
| Shared database functionality for YAZ Care Platform apps | |
| """ | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import declarative_base | |
| from sqlalchemy.orm import sessionmaker, Session | |
| from typing import Generator | |
| import os | |
| # Database URL from environment | |
| DATABASE_URL = os.getenv('DATABASE_URL', 'sqlite:///./data/databases/yaz_care.db') | |
| # Create engine | |
| engine = create_engine(DATABASE_URL) | |
| # Create session factory | |
| SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | |
| # Base class for models | |
| Base = declarative_base() | |
| def get_db_session() -> Generator[Session, None, None]: | |
| """ | |
| Dependency to get database session | |
| """ | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| def get_db(): | |
| """ | |
| Alternative function to get database session | |
| """ | |
| return SessionLocal() | |
| # Export commonly used items | |
| __all__ = ['get_db_session', 'get_db', 'Base', 'engine', 'SessionLocal'] | |