Files
37duAutomation/desktop/electron-shell/electron/db.js
T

43 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 壳子本地 SQLite(better-sqlite3):替代散落 JSON 承载新增数据。
// 数据库:%LOCALAPPDATA%\37duAutomation\shell.db
// 说明:
// - 既有 JSON(license/active/schedules/run_history/creds 等)是和老客户端(Python webhost)
// 共享的契约文件,保持原样不动;**新增数据一律入 SQLite**,避免再堆 JSON。
// - better-sqlite3 为原生模块:CLI 模式用系统 Node 的二进制;打包(electron-builder)时
// 自动 rebuild 为 Electron ABI,产物无需额外处理。
"use strict";
const path = require("path");
const Database = require("better-sqlite3");
const config = require("./config");
const { makeLogger } = require("./logger");
const logger = makeLogger("db");
const DB_PATH = path.join(config.APP_DATA, "shell.db");
// 打开即建库建表(幂等);WAL 提升并发读性能,写仍同步
const db = new Database(DB_PATH);
db.pragma("journal_mode = WAL");
function migrate() {
db.exec(`
-- 场景配置方案(多配置):同一场景多套命名参数,运行台/定时任务按需选用
CREATE TABLE IF NOT EXISTS plugin_configs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account TEXT NOT NULL,
plugin_id TEXT NOT NULL,
name TEXT NOT NULL,
fields TEXT NOT NULL DEFAULT '{}',
at TEXT NOT NULL DEFAULT '',
UNIQUE(account, plugin_id, name)
);
CREATE INDEX IF NOT EXISTS idx_pcfg_owner ON plugin_configs(account, plugin_id);
`);
logger.info(`SQLite 就绪:${DB_PATH}`);
}
migrate();
module.exports = { db, DB_PATH };