Files
xiaoxia-saas/apps/api/app/core/database.py
T

44 lines
1.1 KiB
Python

"""
Database configuration module.
This module provides database session management.
Configuration is now centralized in app.config.Settings.
"""
import os
from typing import Generator
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from app.config import get_settings
def get_engine():
"""Create SQLAlchemy engine from settings."""
settings = get_settings()
return create_engine(
settings.DATABASE_URL,
pool_size=settings.DATABASE_POOL_SIZE,
max_overflow=settings.DATABASE_MAX_OVERFLOW,
pool_timeout=settings.DATABASE_POOL_TIMEOUT,
pool_recycle=settings.DATABASE_POOL_RECYCLE,
)
# Create engine and session factory
_engine = get_engine()
_SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=_engine)
def get_db_session() -> Generator[Session, None, None]:
"""Get database session with automatic cleanup.
Yields:
Session: SQLAlchemy session
"""
session: Session = _SessionLocal()
try:
yield session
finally:
session.close()