Spaces:
Paused
Paused
Download server/db.py from keithproject/mem0: direct link, hf CLI and curl.
- Browser
- Download file 854 Bytes
-
https://huggingface.co/spaces/keithproject/mem0/resolve/main/server/db.py
- Command line
-
hf download hf://spaces/keithproject/mem0/server/db.py
-
curl -L -o db.py https://huggingface.co/spaces/keithproject/mem0/resolve/main/server/db.py
854 Bytes
| import os | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker | |
| def _build_database_url() -> str: | |
| host = os.environ.get("POSTGRES_HOST", "localhost") | |
| port = os.environ.get("POSTGRES_PORT", "5432") | |
| user = os.environ.get("POSTGRES_USER", "mem0") | |
| password = os.environ.get("POSTGRES_PASSWORD", "postgres") | |
| db = os.environ.get("APP_DB_NAME", "mem0_app") | |
| return f"postgresql+psycopg://{user}:{password}@{host}:{port}/{db}" | |
| engine = create_engine(_build_database_url(), pool_pre_ping=True) | |
| SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False) | |
| class Base(DeclarativeBase): | |
| pass | |
| def get_db(): | |
| """FastAPI dependency that yields a SQLAlchemy session.""" | |
| db: Session = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() |