mirror of
https://github.com/JefferyHcool/BiliNote.git
synced 2026-07-21 04:32:48 +08:00
feat(NoteForm): 增加文件上传状态反馈
This commit is contained in:
36
backend/app/db/engine.py
Normal file
36
backend/app/db/engine.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import os
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# 默认 SQLite,如果想换 PostgreSQL 或 MySQL,可以直接改 .env
|
||||
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///bili_note.db")
|
||||
|
||||
# SQLite 需要特定连接参数,其他数据库不需要
|
||||
engine_args = {}
|
||||
if DATABASE_URL.startswith("sqlite"):
|
||||
engine_args["connect_args"] = {"check_same_thread": False}
|
||||
|
||||
engine = create_engine(
|
||||
DATABASE_URL,
|
||||
echo=os.getenv("SQLALCHEMY_ECHO", "false").lower() == "true",
|
||||
**engine_args
|
||||
)
|
||||
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
|
||||
def get_engine():
|
||||
return engine
|
||||
|
||||
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
Reference in New Issue
Block a user