import Surreal from 'surrealdb'; class Database { constructor() { this.client = null; this.connected = false; } async connect() { if (this.connected) return; const url = process.env.SURREAL_URL; const user = process.env.SURREAL_USER; const pass = process.env.SURREAL_PASS; if (!url) { console.warn('[DB] No SURREAL_URL set — running in memory-only mode'); this.connected = false; return; } try { this.client = new Surreal(); await this.client.connect(url); await this.client.signin({ username: user, password: pass }); await this.client.use({ namespace: 'kalbot', database: 'kalbot' }); this.connected = true; console.log('[DB] Connected to SurrealDB'); } catch (e) { console.error('[DB] Connection failed:', e.message); this.connected = false; } } async query(sql, vars = {}) { if (!this.connected) return [[]]; try { return await this.client.query(sql, vars); } catch (e) { console.error('[DB] Query error:', e.message); return [[]]; } } async create(table, data) { if (!this.connected) return null; try { return await this.client.create(table, data); } catch (e) { console.error('[DB] Create error:', e.message); return null; } } async select(table) { if (!this.connected) return []; try { return await this.client.select(table); } catch (e) { console.error('[DB] Select error:', e.message); return []; } } } export const db = new Database();