refactor: 数据库迁移到 ~/.showpic.db

This commit is contained in:
yinzhou.ma
2026-07-16 23:13:56 +08:00
parent 9316422ef1
commit e123e1badf
3 changed files with 17 additions and 102 deletions
+6 -4
View File
@@ -1,5 +1,6 @@
const express = require('express');
const path = require('path');
const os = require('os');
const multer = require('multer');
const initSqlJs = require('sql.js');
const { v4: uuidv4 } = require('uuid');
@@ -11,11 +12,12 @@ const FRONT_PORT = 80;
let db;
const DB_PATH = path.join(os.homedir(), '.showpic.db');
async function initDB() {
const SQL = await initSqlJs();
const dbPath = path.join(__dirname, 'data', 'sqlite.db');
if (fs.existsSync(dbPath)) {
db = new SQL.Database(fs.readFileSync(dbPath));
if (fs.existsSync(DB_PATH)) {
db = new SQL.Database(fs.readFileSync(DB_PATH));
} else {
db = new SQL.Database();
}
@@ -42,7 +44,7 @@ async function initDB() {
function saveDB() {
const data = db.export();
fs.writeFileSync(path.join(__dirname, 'data', 'sqlite.db'), Buffer.from(data));
fs.writeFileSync(DB_PATH, Buffer.from(data));
}
function runQuery(sql, params) {