修复: 改用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,72 +1,86 @@
// 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(() => {
CREATE TABLE IF NOT EXISTS users ( db.run(`
id INTEGER PRIMARY KEY AUTOINCREMENT, CREATE TABLE IF NOT EXISTS users (
username TEXT UNIQUE NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT,
password_hash TEXT NOT NULL, username TEXT UNIQUE NOT NULL,
email TEXT, password_hash TEXT NOT NULL,
storage_used INTEGER DEFAULT 0, email TEXT,
storage_limit INTEGER DEFAULT 10737418240, storage_used INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, storage_limit INTEGER DEFAULT 10737418240,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
); updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
CREATE TABLE IF NOT EXISTS files ( db.run(`
id INTEGER PRIMARY KEY AUTOINCREMENT, CREATE TABLE IF NOT EXISTS files (
user_id INTEGER NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT,
parent_id INTEGER, user_id INTEGER NOT NULL,
name TEXT NOT NULL, parent_id INTEGER,
type TEXT, name TEXT NOT NULL,
size INTEGER DEFAULT 0, type TEXT,
path TEXT, size INTEGER DEFAULT 0,
hash TEXT, path TEXT,
is_folder INTEGER DEFAULT 0, hash TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, is_folder INTEGER DEFAULT 0,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
deleted_at DATETIME, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id), deleted_at DATETIME,
FOREIGN KEY (parent_id) REFERENCES files(id) FOREIGN KEY (user_id) REFERENCES users(id),
); FOREIGN KEY (parent_id) REFERENCES files(id)
)
`);
CREATE TABLE IF NOT EXISTS shares ( db.run(`
id INTEGER PRIMARY KEY AUTOINCREMENT, CREATE TABLE IF NOT EXISTS shares (
file_id INTEGER NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT,
share_token TEXT UNIQUE NOT NULL, file_id INTEGER NOT NULL,
password TEXT, share_token TEXT UNIQUE NOT NULL,
expires_at DATETIME, password TEXT,
view_count INTEGER DEFAULT 0, expires_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, view_count INTEGER DEFAULT 0,
FOREIGN KEY (file_id) REFERENCES files(id) created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
); FOREIGN KEY (file_id) REFERENCES files(id)
)
`);
CREATE TABLE IF NOT EXISTS sync_logs ( db.run(`
id INTEGER PRIMARY KEY AUTOINCREMENT, CREATE TABLE IF NOT EXISTS sync_status (
user_id INTEGER NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT,
file_id INTEGER, user_id INTEGER NOT NULL,
action TEXT, last_sync_time DATETIME,
status TEXT, status TEXT DEFAULT 'idle',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP, total_files INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id) synced_files INTEGER DEFAULT 0,
); FOREIGN KEY (user_id) REFERENCES users(id)
`); )
`);
});
module.exports = db; // 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);
});
});
};
// Sync tables (for reference - would be in migration in production) const run = (sql, params = []) => {
db.exec(` return new Promise((resolve, reject) => {
CREATE TABLE IF NOT EXISTS sync_status ( db.run(sql, params, function(err) {
id INTEGER PRIMARY KEY AUTOINCREMENT, if (err) reject(err);
user_id INTEGER NOT NULL, else resolve({ lastInsertRowid: this.lastID, changes: this.changes });
last_sync_time DATETIME, });
status TEXT DEFAULT 'idle', });
total_files INTEGER DEFAULT 0, };
synced_files INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users(id) module.exports = { query, run, db };
);
`);