feat: 保存并登记当前 Client (#11)
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, Union
|
||||
from typing import Any, Mapping, Optional, Union
|
||||
|
||||
from .db import initialize_database, open_database
|
||||
from .task_models import AppSettingRecord
|
||||
@@ -74,6 +74,35 @@ class SettingsRepository:
|
||||
connection.close()
|
||||
return AppSettingRecord(key, value, now)
|
||||
|
||||
def set_many(
|
||||
self, values: Mapping[str, Any], updated_at: Optional[str] = None
|
||||
) -> list[AppSettingRecord]:
|
||||
"""在同一事务中保存多个非敏感设置。"""
|
||||
|
||||
if not values:
|
||||
return []
|
||||
|
||||
normalized = [
|
||||
(self._validate_key(key), value) for key, value in values.items()
|
||||
]
|
||||
now = updated_at or _utc_now_iso()
|
||||
connection = open_database(self._db_path)
|
||||
try:
|
||||
with connection:
|
||||
for key, value in normalized:
|
||||
connection.execute(
|
||||
"INSERT INTO app_settings"
|
||||
" (setting_key, value_json, updated_at)"
|
||||
" VALUES (?, ?, ?)"
|
||||
" ON CONFLICT(setting_key) DO UPDATE SET"
|
||||
" value_json = excluded.value_json,"
|
||||
" updated_at = excluded.updated_at",
|
||||
(key, json.dumps(value, ensure_ascii=False), now),
|
||||
)
|
||||
finally:
|
||||
connection.close()
|
||||
return [AppSettingRecord(key, value, now) for key, value in normalized]
|
||||
|
||||
def delete(self, setting_key: str) -> bool:
|
||||
"""删除设置;确实删除了一条记录时返回 True。"""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user