extremely claude-assisted go game based on atproto! working on cleaning up and giving a more unique design, still has a bit of a slop vibe to it.

Add error handling and validation to database init

- Add try-catch around database initialization
- Verify Kysely instance has selectFrom method before returning
- Add detailed error logging
- Return cached instance when available

+54 -40
+54 -40
src/lib/server/db.ts
··· 29 29 let localDb: Kysely<Database> | null = null; 30 30 31 31 function initLocalDb(): Kysely<Database> { 32 - if (localDb) return localDb; 32 + if (localDb) { 33 + console.log('[DB] Returning cached local database'); 34 + return localDb; 35 + } 33 36 34 - // Use CommonJS require for local development 35 - const Database = require('better-sqlite3'); 36 - const fs = require('fs'); 37 - const path = require('path'); 37 + try { 38 + // Use CommonJS require for local development 39 + const Database = require('better-sqlite3'); 40 + const fs = require('fs'); 41 + const path = require('path'); 38 42 39 - const dbPath = process.env.DATABASE_PATH || './data/app.db'; 40 - const dbDir = path.dirname(dbPath); 43 + const dbPath = process.env.DATABASE_PATH || './data/app.db'; 44 + const dbDir = path.dirname(dbPath); 41 45 42 - if (!fs.existsSync(dbDir)) { 43 - fs.mkdirSync(dbDir, { recursive: true }); 44 - } 46 + if (!fs.existsSync(dbDir)) { 47 + fs.mkdirSync(dbDir, { recursive: true }); 48 + } 45 49 46 - const sqlite = new Database(dbPath); 47 - sqlite.pragma('journal_mode = WAL'); 50 + const sqlite = new Database(dbPath); 51 + sqlite.pragma('journal_mode = WAL'); 48 52 49 - // Initialize tables 50 - sqlite.exec(` 51 - CREATE TABLE IF NOT EXISTS games ( 52 - id TEXT PRIMARY KEY, 53 - rkey TEXT NOT NULL, 54 - creator_did TEXT NOT NULL, 55 - player_one TEXT NOT NULL, 56 - player_two TEXT, 57 - board_size INTEGER NOT NULL DEFAULT 19, 58 - status TEXT NOT NULL CHECK(status IN ('waiting', 'active', 'completed')), 59 - action_count INTEGER NOT NULL DEFAULT 0, 60 - last_action_type TEXT, 61 - winner TEXT, 62 - handicap INTEGER DEFAULT 0, 63 - created_at TEXT NOT NULL, 64 - updated_at TEXT NOT NULL 65 - ); 53 + // Initialize tables 54 + sqlite.exec(` 55 + CREATE TABLE IF NOT EXISTS games ( 56 + id TEXT PRIMARY KEY, 57 + rkey TEXT NOT NULL, 58 + creator_did TEXT NOT NULL, 59 + player_one TEXT NOT NULL, 60 + player_two TEXT, 61 + board_size INTEGER NOT NULL DEFAULT 19, 62 + status TEXT NOT NULL CHECK(status IN ('waiting', 'active', 'completed')), 63 + action_count INTEGER NOT NULL DEFAULT 0, 64 + last_action_type TEXT, 65 + winner TEXT, 66 + handicap INTEGER DEFAULT 0, 67 + created_at TEXT NOT NULL, 68 + updated_at TEXT NOT NULL 69 + ); 66 70 67 - CREATE INDEX IF NOT EXISTS idx_games_status ON games(status); 68 - CREATE INDEX IF NOT EXISTS idx_games_player_one ON games(player_one); 69 - CREATE INDEX IF NOT EXISTS idx_games_player_two ON games(player_two); 70 - CREATE INDEX IF NOT EXISTS idx_games_rkey ON games(rkey); 71 - CREATE INDEX IF NOT EXISTS idx_games_creator_did ON games(creator_did); 72 - `); 71 + CREATE INDEX IF NOT EXISTS idx_games_status ON games(status); 72 + CREATE INDEX IF NOT EXISTS idx_games_player_one ON games(player_one); 73 + CREATE INDEX IF NOT EXISTS idx_games_player_two ON games(player_two); 74 + CREATE INDEX IF NOT EXISTS idx_games_rkey ON games(rkey); 75 + CREATE INDEX IF NOT EXISTS idx_games_creator_did ON games(creator_did); 76 + `); 77 + 78 + localDb = new Kysely<Database>({ 79 + dialect: new SqliteDialect({ database: sqlite }), 80 + }); 73 81 74 - localDb = new Kysely<Database>({ 75 - dialect: new SqliteDialect({ database: sqlite }), 76 - }); 82 + console.log('[DB] Local database initialized successfully, type:', typeof localDb, 'hasSelectFrom:', typeof localDb.selectFrom); 83 + 84 + // Verify it works 85 + if (typeof localDb.selectFrom !== 'function') { 86 + throw new Error('Kysely instance does not have selectFrom method!'); 87 + } 77 88 78 - console.log('[DB] Local database initialized:', typeof localDb, 'hasSelectFrom:', typeof localDb.selectFrom); 79 - return localDb; 89 + return localDb; 90 + } catch (err) { 91 + console.error('[DB] Error initializing local database:', err); 92 + throw err; 93 + } 80 94 } 81 95 82 96 export function getDb(platform: App.Platform | undefined): Kysely<Database> {