27 lines
728 B
Python
27 lines
728 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import DeclarativeBase, sessionmaker
|
|
from backend.config.settings import settings
|
|
|
|
# MariaDB Connection URL
|
|
DATABASE_URL = f"mysql+pymysql://{settings.db_user}:{settings.db_password}@{settings.db_host}:{settings.db_port}/{settings.db_name}?charset=utf8mb4"
|
|
|
|
engine = create_engine(
|
|
DATABASE_URL,
|
|
echo=True,
|
|
pool_pre_ping=True
|
|
)
|
|
|
|
# 세션 팩토리 생성
|
|
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False)
|
|
|
|
# Base = declarative_Base()
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
# session 을 다른 모듈에서 사용할 수 있도록 제공
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close() |