21 lines
357 B
Python
21 lines
357 B
Python
"""
|
|
database/dependencies.py
|
|
FastAPI 依赖注入:获取数据库 Session。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Generator
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from database.core import SessionLocal
|
|
|
|
|
|
def get_db() -> Generator[Session, None, None]:
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|