refactor: 数据库迁移到 ~/.showpic.db
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user