Spaces:
Sleeping
Sleeping
Download backend/src/database.py from zonszer/docker-space-fastapi-react: direct link, hf CLI and curl.
- Browser
- Download file 763 Bytes
-
https://huggingface.co/spaces/zonszer/docker-space-fastapi-react/resolve/35d6865c8e98a85bf960de2283b86f6efd55683d/backend/src/database.py
- Command line
-
hf download hf://spaces/zonszer/docker-space-fastapi-react@35d6865c8e98a85bf960de2283b86f6efd55683d/backend/src/database.py
-
curl -L -o database.py https://huggingface.co/spaces/zonszer/docker-space-fastapi-react/resolve/35d6865c8e98a85bf960de2283b86f6efd55683d/backend/src/database.py
763 Bytes
| from contextlib import contextmanager | |
| from typing import Generator | |
| from sqlalchemy.engine import Engine | |
| from sqlmodel import Session, SQLModel, create_engine | |
| from . import constants | |
| _ENGINE_SINGLETON: Engine | None = None | |
| def get_engine() -> Engine: | |
| """Get the engine.""" | |
| global _ENGINE_SINGLETON | |
| if _ENGINE_SINGLETON is None: | |
| _ENGINE_SINGLETON = create_engine(constants.DATABASE_URL) | |
| return _ENGINE_SINGLETON | |
| def get_session() -> Generator[Session, None, None]: | |
| """Get a session from the engine.""" | |
| engine = get_engine() | |
| with Session(engine) as session: | |
| yield session | |
| def init_db() -> None: | |
| """Initialize the database.""" | |
| engine = get_engine() | |
| SQLModel.metadata.create_all(engine) | |