39 lines
883 B
Python
39 lines
883 B
Python
"""
|
|
Database session management for the sim-search API.
|
|
|
|
This module provides utilities for creating and managing database sessions.
|
|
"""
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from app.core.config import settings
|
|
|
|
# Create SQLAlchemy engine
|
|
engine = create_engine(
|
|
settings.SQLALCHEMY_DATABASE_URI,
|
|
pool_pre_ping=True,
|
|
connect_args={"check_same_thread": False} if settings.SQLALCHEMY_DATABASE_URI.startswith("sqlite") else {},
|
|
)
|
|
|
|
# Create session factory
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
# Create base class for models
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db():
|
|
"""
|
|
Get a database session.
|
|
|
|
Yields:
|
|
SQLAlchemy session
|
|
"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|