Update lib/db.js

This commit is contained in:
2026-04-26 23:00:28 +02:00
parent 0b6b60fcb8
commit 1963ed4369
+14 -26
View File
@@ -1,18 +1,19 @@
// lib/db.js
import Database from 'better-sqlite3';
import path from 'path';
const dbPath = path.resolve(process.cwd(), 'devbin.db');
const db = new Database(dbPath);
const db = new Database(path.join(process.cwd(), 'devbin.db'));
// This "Schema" block ensures new users get the database they need automatically
db.exec(`
CREATE TABLE IF NOT EXISTS pastes (
id TEXT PRIMARY KEY,
content TEXT NOT NULL,
language TEXT NOT NULL,
filename TEXT NOT NULL,
created_at TEXT NOT NULL,
content TEXT,
language TEXT,
filename TEXT,
created_at TEXT,
expires_at TEXT,
expiry_type TEXT DEFAULT 'time',
expiry_type TEXT,
view_count INTEGER DEFAULT 0,
allow_discussions INTEGER DEFAULT 0,
delete_token TEXT
@@ -20,25 +21,12 @@ db.exec(`
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
paste_id TEXT NOT NULL,
author TEXT NOT NULL,
content TEXT NOT NULL,
created_at TEXT NOT NULL,
FOREIGN KEY (paste_id) REFERENCES pastes (id) ON DELETE CASCADE
paste_id TEXT,
author TEXT,
content TEXT,
created_at TEXT,
FOREIGN KEY(paste_id) REFERENCES pastes(id) ON DELETE CASCADE
);
`);
// Migration for delete_token
try {
const tableInfo = db.prepare("PRAGMA table_info(pastes)").all();
if (!tableInfo.some(col => col.name === 'delete_token')) {
db.exec("ALTER TABLE pastes ADD COLUMN delete_token TEXT;");
}
if (!tableInfo.some(col => col.name === 'allow_discussions')) {
db.exec("ALTER TABLE pastes ADD COLUMN allow_discussions INTEGER DEFAULT 0;");
}
} catch (err) {
console.error("Migration failed:", err);
}
export default db;
export default db;