26 lines
553 B
Python
26 lines
553 B
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker, Session
|
|
from config import settings
|
|
|
|
# 创建数据库引擎
|
|
engine = create_engine(
|
|
settings.DATABASE_URL,
|
|
echo=settings.DEBUG,
|
|
pool_pre_ping=True,
|
|
pool_size=5,
|
|
max_overflow=10,
|
|
)
|
|
|
|
# 创建会话工厂
|
|
SessionLocal = sessionmaker(bind=engine)
|
|
|
|
|
|
def get_session() -> Session:
|
|
"""获取新的数据库会话
|
|
|
|
使用示例:
|
|
with get_session() as session:
|
|
result = session.query(Model).all()
|
|
"""
|
|
return SessionLocal()
|