// Simple SQLite database wrapper using sqlite3 const sqlite3 = require('sqlite3').verbose(); const dbPath = process.env.DB_PATH || './clouddisk.db'; const db = new sqlite3.Database(dbPath); // Initialize tables db.serialize(() => { db.run(` CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL, email TEXT, storage_used INTEGER DEFAULT 0, storage_limit INTEGER DEFAULT 10737418240, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ) `); db.run(` CREATE TABLE IF NOT EXISTS files ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, parent_id INTEGER, name TEXT NOT NULL, type TEXT, size INTEGER DEFAULT 0, path TEXT, hash TEXT, is_folder INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP, deleted_at DATETIME, FOREIGN KEY (user_id) REFERENCES users(id), FOREIGN KEY (parent_id) REFERENCES files(id) ) `); db.run(` CREATE TABLE IF NOT EXISTS shares ( id INTEGER PRIMARY KEY AUTOINCREMENT, file_id INTEGER NOT NULL, share_token TEXT UNIQUE NOT NULL, password TEXT, expires_at DATETIME, view_count INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (file_id) REFERENCES files(id) ) `); db.run(` CREATE TABLE IF NOT EXISTS sync_status ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, 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) ) `); }); // 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 };