27 lines
669 B
Python
27 lines
669 B
Python
"""
|
|
Database configuration and session management
|
|
"""
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker, Session
|
|
from typing import Generator
|
|
|
|
from app.config import settings
|
|
|
|
engine = create_engine(
|
|
settings.database_url,
|
|
connect_args={"check_same_thread": False} if "sqlite" in settings.database_url else {}
|
|
)
|
|
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
"""Get database session"""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |