Download lib/db.ts from Jakecybr/dnd2: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/spaces/Jakecybr/dnd2/resolve/main/lib/db.ts
- Command line
-
hf download hf://spaces/Jakecybr/dnd2/lib/db.ts
-
curl -L -o db.ts https://huggingface.co/spaces/Jakecybr/dnd2/resolve/main/lib/db.ts
2.53 kB
| import Database from 'better-sqlite3'; | |
| import fs from 'fs'; | |
| import path from 'path'; | |
| // CRITICAL: Point to the persistent mount in Hugging Face | |
| const DATA_DIR = '/data'; | |
| const DB_PATH = path.join(DATA_DIR, 'campaign.db'); | |
| const IMG_DIR = path.join(DATA_DIR, 'images'); | |
| // --- THE FIX IS HERE --- | |
| // We use { recursive: true } to prevent "EEXIST" errors during build | |
| try { | |
| if (!fs.existsSync(DATA_DIR)) { | |
| // If /data (persistent) doesn't exist, fallback to local ./data | |
| fs.mkdirSync('./data', { recursive: true }); | |
| } else { | |
| // If /data exists, ensure the images folder exists | |
| fs.mkdirSync(IMG_DIR, { recursive: true }); | |
| } | |
| } catch (e) { | |
| console.log("Directory creation warning (safe to ignore during build):", e); | |
| } | |
| // Select path based on environment | |
| // Logic: If /data exists (HF Production), use it. Otherwise use local ./data | |
| const finalDbPath = fs.existsSync(DATA_DIR) ? DB_PATH : './data/campaign.db'; | |
| const db = new Database(finalDbPath); | |
| db.pragma('journal_mode = WAL'); | |
| // 1. Create Tables | |
| db.exec(` | |
| CREATE TABLE IF NOT EXISTS users ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| username TEXT UNIQUE, | |
| password TEXT, | |
| role TEXT DEFAULT 'player', | |
| avatar_url TEXT | |
| ); | |
| CREATE TABLE IF NOT EXISTS logs ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| title TEXT, | |
| content TEXT, | |
| is_secret INTEGER DEFAULT 0, | |
| game_date TEXT, | |
| created_at DATETIME DEFAULT CURRENT_TIMESTAMP | |
| ); | |
| CREATE TABLE IF NOT EXISTS tech_tree ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| name TEXT, | |
| status TEXT DEFAULT 'locked', | |
| progress INTEGER DEFAULT 0 | |
| ); | |
| CREATE TABLE IF NOT EXISTS characters ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| user_id INTEGER, | |
| name TEXT, | |
| class_type TEXT, | |
| inventory TEXT, | |
| appearance TEXT, | |
| image_url TEXT, | |
| FOREIGN KEY(user_id) REFERENCES users(id) | |
| ); | |
| CREATE TABLE IF NOT EXISTS journal ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| user_id INTEGER, | |
| title TEXT, | |
| content TEXT, | |
| is_public INTEGER DEFAULT 0, | |
| created_at DATETIME DEFAULT CURRENT_TIMESTAMP, | |
| FOREIGN KEY(user_id) REFERENCES users(id) | |
| ); | |
| `); | |
| // 2. SELF-HEALING: Add 'appearance' column if it's missing | |
| try { | |
| const columns = db.prepare("PRAGMA table_info(characters)").all() as any[]; | |
| const hasAppearance = columns.some(c => c.name === 'appearance'); | |
| if (!hasAppearance) { | |
| db.prepare("ALTER TABLE characters ADD COLUMN appearance TEXT").run(); | |
| } | |
| } catch (e) { | |
| // Ignore migration errors during build | |
| } | |
| export default db; |