Database Configuration ====================== Database connection and session management. .. automodule:: ai4drpm.db.database :members: :undoc-members: :show-inheritance: Overview -------- This module provides database configuration and session management: Key Components -------------- Database URL ~~~~~~~~~~~~ Configurable database connection string supporting: * PostgreSQL (production) * SQLite (development/testing) * Connection pooling configuration SessionLocal ~~~~~~~~~~~~ SQLAlchemy session factory: .. code-block:: python from ai4drpm.db.database import SessionLocal db = SessionLocal() try: # Database operations pass finally: db.close() get_db Dependency ~~~~~~~~~~~~~~~~~ FastAPI dependency for database sessions: .. code-block:: python from ai4drpm.db.database import get_db from fastapi import Depends @app.get("/resources") def get_resources(db: Session = Depends(get_db)): # db session automatically managed pass Engine Configuration ~~~~~~~~~~~~~~~~~~~~ SQLAlchemy engine with: * Connection pooling (pool_size, max_overflow) * Connection timeout settings * Echo mode for SQL logging (development) * Pool pre-ping for connection health checks Features -------- * **Connection Pooling**: Efficient connection reuse * **Automatic Session Management**: Context managers for proper cleanup * **Transaction Support**: Commit/rollback handling * **Health Checks**: Connection validation * **Migration Support**: Alembic integration Usage Example ------------- .. code-block:: python from ai4drpm.db.database import SessionLocal, engine, Base # Create all tables Base.metadata.create_all(bind=engine) # Use session db = SessionLocal() try: # Perform operations result = db.query(User).all() db.commit() except Exception: db.rollback() raise finally: db.close() # Or use as context manager with SessionLocal() as db: result = db.query(User).all()