修复: 改用sqlite3替代better-sqlite3

This commit is contained in:
Backend Developer 2026-03-10 08:43:04 +00:00
parent f6c7502243
commit 1e4b74b7ca
2 changed files with 79 additions and 65 deletions

View File

@ -12,7 +12,7 @@
"express": "^4.18.0", "express": "^4.18.0",
"sqlite3": "^5.1.0", "sqlite3": "^5.1.0",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"multer": "^1.4.5", "multer": "^1.4.5-lts.1",
"bcrypt": "^5.1.0", "bcrypt": "^5.1.0",
"cors": "^2.8.5" "cors": "^2.8.5"
}, },

View File

@ -1,11 +1,12 @@
// Simple SQLite database wrapper // Simple SQLite database wrapper using sqlite3
// In production, use better-sqlite3 or similar const sqlite3 = require('sqlite3').verbose();
const Database = require('better-sqlite3'); const dbPath = process.env.DB_PATH || './clouddisk.db';
const db = new Database('clouddisk.db'); const db = new sqlite3.Database(dbPath);
// Initialize tables // Initialize tables
db.exec(` db.serialize(() => {
db.run(`
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL, username TEXT UNIQUE NOT NULL,
@ -15,8 +16,10 @@ db.exec(`
storage_limit INTEGER DEFAULT 10737418240, storage_limit INTEGER DEFAULT 10737418240,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
); )
`);
db.run(`
CREATE TABLE IF NOT EXISTS files ( CREATE TABLE IF NOT EXISTS files (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL, user_id INTEGER NOT NULL,
@ -32,8 +35,10 @@ db.exec(`
deleted_at DATETIME, deleted_at DATETIME,
FOREIGN KEY (user_id) REFERENCES users(id), FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (parent_id) REFERENCES files(id) FOREIGN KEY (parent_id) REFERENCES files(id)
); )
`);
db.run(`
CREATE TABLE IF NOT EXISTS shares ( CREATE TABLE IF NOT EXISTS shares (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
file_id INTEGER NOT NULL, file_id INTEGER NOT NULL,
@ -43,23 +48,10 @@ db.exec(`
view_count INTEGER DEFAULT 0, view_count INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (file_id) REFERENCES files(id) FOREIGN KEY (file_id) REFERENCES files(id)
); )
`);
CREATE TABLE IF NOT EXISTS sync_logs ( db.run(`
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
file_id INTEGER,
action TEXT,
status TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
);
`);
module.exports = db;
// Sync tables (for reference - would be in migration in production)
db.exec(`
CREATE TABLE IF NOT EXISTS sync_status ( CREATE TABLE IF NOT EXISTS sync_status (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL, user_id INTEGER NOT NULL,
@ -68,5 +60,27 @@ db.exec(`
total_files INTEGER DEFAULT 0, total_files INTEGER DEFAULT 0,
synced_files INTEGER DEFAULT 0, synced_files INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id) FOREIGN KEY (user_id) REFERENCES users(id)
); )
`); `);
});
// Wrapper functions to match the old API
const query = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.all(sql, params, (err, rows) => {
if (err) reject(err);
else resolve(rows);
});
});
};
const run = (sql, params = []) => {
return new Promise((resolve, reject) => {
db.run(sql, params, function(err) {
if (err) reject(err);
else resolve({ lastInsertRowid: this.lastID, changes: this.changes });
});
});
};
module.exports = { query, run, db };