73 lines
1.9 KiB
JavaScript
73 lines
1.9 KiB
JavaScript
// 删除或注释掉原来的路径相关代码
|
||
// import path from 'path';
|
||
// import fs from 'fs';
|
||
// import { app } from 'electron';
|
||
|
||
// 导入统一的路径工具函数
|
||
import { getSystemDbPath } from './path.js';
|
||
import { openDatabase, executeWithRetry } from './utils.js';
|
||
|
||
/**
|
||
* 从config表获取配置项
|
||
* @param {string} key - 配置项键名
|
||
* @returns {Promise<{key: string, value: string} | null>} 配置项对象,如果不存在返回null
|
||
*/
|
||
async function getConfig(key) {
|
||
try {
|
||
// 使用统一的数据库路径
|
||
const dbPath = getSystemDbPath();
|
||
|
||
const db = await openDatabase(dbPath);
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 更新或插入配置项
|
||
* @param {string} key - 配置项键名
|
||
* @param {string} value - 配置项值
|
||
* @returns {Promise<void>}
|
||
*/
|
||
async function setConfig(key, value) {
|
||
try {
|
||
// 使用统一的数据库路径
|
||
const dbPath = getSystemDbPath();
|
||
|
||
const db = await openDatabase(dbPath);
|
||
|
||
// 先检查是否存在
|
||
const existing = await executeWithRetry(db, async () => {
|
||
return await db.getAsync('SELECT * FROM config WHERE key = ?', [key]);
|
||
});
|
||
|
||
if (existing) {
|
||
// 更新
|
||
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) VALUES (?, ?)', [key, value]);
|
||
console.log(`成功插入配置项: ${key}`);
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.error(`设置配置项${key}失败:`, error);
|
||
throw error;
|
||
}
|
||
}
|
||
|
||
export {
|
||
getConfig,
|
||
setConfig
|
||
}; |