fix 数据库会话管理

This commit is contained in:
jxxghp
2023-08-16 10:22:45 +08:00
parent b086bbf015
commit da93328d50
29 changed files with 255 additions and 172 deletions

View File

@ -33,8 +33,11 @@ class DbOper:
_db: Session = None
def __init__(self, _db=SessionLocal()):
self._db = _db
def __init__(self, db: Session = None):
if db:
self._db = db
else:
self._db = SessionLocal()
def __del__(self):
if self._db:

View File

@ -15,7 +15,7 @@ class DownloadHistoryOper(DbOper):
按路径查询下载记录
:param path: 数据key
"""
return DownloadHistory.get_by_path(self._db, path)
return DownloadHistory.get_by_path(self._db, str(path))
def get_by_hash(self, download_hash: str) -> Any:
"""

View File

@ -1,7 +1,9 @@
import json
from typing import Optional
from app.db import DbOper, SessionLocal
from sqlalchemy.orm import Session
from app.db import DbOper
from app.db.models.mediaserver import MediaServerItem
@ -10,7 +12,7 @@ class MediaServerOper(DbOper):
媒体服务器数据管理
"""
def __init__(self, db=SessionLocal()):
def __init__(self, db: Session = None):
super().__init__(db)
def add(self, **kwargs) -> bool:

View File

@ -1,6 +1,8 @@
from typing import List
from app.db import DbOper, SessionLocal
from sqlalchemy.orm import Session
from app.db import DbOper
from app.db.models.rss import Rss
@ -9,7 +11,7 @@ class RssOper(DbOper):
RSS订阅数据管理
"""
def __init__(self, db=SessionLocal()):
def __init__(self, db: Session = None):
super().__init__(db)
def add(self, **kwargs) -> bool:

View File

@ -1,22 +1,24 @@
import json
from typing import Any, Union
from app.db import DbOper, SessionLocal
from sqlalchemy.orm import Session
from app.db import DbOper
from app.db.models.systemconfig import SystemConfig
from app.schemas.types import SystemConfigKey
from app.utils.object import ObjectUtils
from app.utils.singleton import Singleton
from app.schemas.types import SystemConfigKey
class SystemConfigOper(DbOper, metaclass=Singleton):
# 配置对象
__SYSTEMCONF: dict = {}
def __init__(self, _db=SessionLocal()):
def __init__(self, db: Session = None):
"""
加载配置到内存
"""
super().__init__(_db)
super().__init__(db)
for item in SystemConfig.list(self._db):
if ObjectUtils.is_obj(item.value):
self.__SYSTEMCONF[item.key] = json.loads(item.value)