// 导入统一的路径工具函数 import { getSystemDbPath } from './path.js'; import { executeWithRetry } from './utils.js'; import { getDbConnection } from './index.js'; /** * 从config表获取配置项 * @param {string} key - 配置项键名 * @returns {Promise<{key: string, value: string} | null>} 配置项对象,如果不存在返回null */ async function getConfig(key) { try { const db = await getDbConnection(getSystemDbPath()); const result = await executeWithRetry(db, async () => { return await db.getAsync('SELECT * FROM config WHERE key = ?', [key]); }); return result; } catch (error) { console.error(`获取配置项${key}失败:`, error); throw error; } } /** * 获取所有配置项列表 * @returns {Promise>} 配置项列表 */ async function getAllConfigs() { try { const db = await getDbConnection(getSystemDbPath()); const result = await executeWithRetry(db, async () => { return await db.allAsync('SELECT * FROM config'); }); return result; } catch (error) { console.error('获取配置项列表失败:', error); throw error; } } /** * 通过ID获取配置项 * @param {number} id - 配置项ID * @returns {Promise<{id: number, key: string, value: string, protected: number} | null>} 配置项对象,如果不存在返回null */ async function getConfigById(id) { try { const db = await getDbConnection(getSystemDbPath()); const result = await executeWithRetry(db, async () => { return await db.getAsync('SELECT * FROM config WHERE id = ?', [id]); }); return result; } catch (error) { console.error(`通过ID获取配置项${id}失败:`, error); throw error; } } /** * 更新或插入配置项 * @param {string} key - 配置项键名 * @param {string} value - 配置项值 * @returns {Promise} */ async function setConfig(key, value) { try { const db = await getDbConnection(getSystemDbPath()); // 先检查是否存在 const existing = await executeWithRetry(db, async () => { return await db.getAsync('SELECT * FROM config WHERE key = ?', [key]); }); if (existing) { // 检查是否受保护 // if (existing.protected === 1) { // throw new Error(`配置项${key}是受保护的,无法修改`); // } // 更新 await executeWithRetry(db, async () => { await db.runAsync('UPDATE config SET value = ? WHERE key = ?', [value, key]); console.log(`成功更新配置项: ${key}`); }); } else { // 插入 (默认不保护) await executeWithRetry(db, async () => { await db.runAsync('INSERT INTO config (key, value, protected) VALUES (?, ?, 0)', [key, value]); console.log(`成功插入配置项: ${key}`); }); } } catch (error) { console.error(`设置配置项${key}失败:`, error); throw error; } } /** * 删除配置项 * @param {number} id - 配置项ID * @returns {Promise} */ async function deleteConfig(id) { try { const db = await getDbConnection(getSystemDbPath()); // 先检查是否存在 const existing = await executeWithRetry(db, async () => { return await db.getAsync('SELECT * FROM config WHERE id = ?', [id]); }); if (!existing) { throw new Error(`配置项ID ${id} 不存在`); } // 检查是否受保护 if (existing.protected === 1) { throw new Error(`配置项 ${existing.key} 是受保护的,无法删除`); } // 删除 await executeWithRetry(db, async () => { await db.runAsync('DELETE FROM config WHERE id = ?', [id]); console.log(`成功删除配置项ID: ${id}`); }); } catch (error) { console.error(`删除配置项ID ${id} 失败:`, error); throw error; } } // 导出保持不变 export { getConfig, setConfig, getAllConfigs, getConfigById, deleteConfig };