73 lines
2.0 KiB
JavaScript
73 lines
2.0 KiB
JavaScript
// Simple SQLite database wrapper
|
|
// In production, use better-sqlite3 or similar
|
|
|
|
const Database = require('better-sqlite3');
|
|
const db = new Database('clouddisk.db');
|
|
|
|
// Initialize tables
|
|
db.exec(`
|
|
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
|
|
);
|
|
|
|
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)
|
|
);
|
|
|
|
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)
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS sync_logs (
|
|
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 (
|
|
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)
|
|
);
|
|
`);
|