xxy aa98ea2623 @
Initial commit

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
2026-06-05 18:45:29 +08:00

29 lines
584 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
database/dependencies.py
FastAPI 依赖注入:获取数据库 Session。
每个请求创建新 Session请求结束后自动关闭。
"""
from collections.abc import Generator
from sqlalchemy.orm import Session
from database.core import SessionLocal
def get_db() -> Generator[Session, None, None]:
"""
获取数据库 Session用于 FastAPI Depends()。
用法:
@router.get("/items")
def list_items(db: Session = Depends(get_db)):
...
"""
db = SessionLocal()
try:
yield db
finally:
db.close()