Spaces:
Runtime error
Runtime error
Download db.py from myousaf-codes/ccr-hackathon-todo: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/myousaf-codes/ccr-hackathon-todo/resolve/main/db.py
- Command line
-
hf download hf://spaces/myousaf-codes/ccr-hackathon-todo/db.py
-
curl -L -o db.py https://huggingface.co/spaces/myousaf-codes/ccr-hackathon-todo/resolve/main/db.py
1.01 kB
| from sqlmodel import Session, create_engine | |
| from sqlalchemy import Engine | |
| from typing import Generator | |
| import os | |
| from dotenv import load_dotenv | |
| # Load environment variables | |
| load_dotenv() | |
| # Get database URL from environment | |
| DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./todo_app.db") | |
| # Create the database engine | |
| engine: Engine = create_engine(DATABASE_URL, echo=False) | |
| def get_engine() -> Engine: | |
| """ | |
| Returns the database engine instance. | |
| """ | |
| return engine | |
| def get_session() -> Generator[Session, None, None]: | |
| """ | |
| Yields a database session for use with dependency injection. | |
| """ | |
| with Session(engine) as session: | |
| yield session | |
| def create_db_and_tables(): | |
| """ | |
| Creates database tables based on SQLModel models. | |
| This should be called when initializing the application. | |
| """ | |
| from sqlmodel import SQLModel | |
| from models import User, Task | |
| # Import models to ensure they're registered with SQLModel | |
| SQLModel.metadata.create_all(engine) |