21 lines
470 B
Python
21 lines
470 B
Python
from pathlib import Path
|
|
|
|
from sqlmodel import SQLModel, Session, create_engine
|
|
|
|
# 数据库文件存放在 backend/data/ 目录下
|
|
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
|
|
DATA_DIR.mkdir(exist_ok=True)
|
|
|
|
DATABASE_URL = f"sqlite:///{DATA_DIR / 'noval.db'}"
|
|
|
|
engine = create_engine(DATABASE_URL, echo=False)
|
|
|
|
|
|
def init_db() -> None:
|
|
SQLModel.metadata.create_all(engine)
|
|
|
|
|
|
def get_session():
|
|
with Session(engine) as session:
|
|
yield session
|