···2929let localDb: Kysely<Database> | null = null;
30303131function initLocalDb(): Kysely<Database> {
3232- if (localDb) return localDb;
3232+ if (localDb) {
3333+ console.log('[DB] Returning cached local database');
3434+ return localDb;
3535+ }
33363434- // Use CommonJS require for local development
3535- const Database = require('better-sqlite3');
3636- const fs = require('fs');
3737- const path = require('path');
3737+ try {
3838+ // Use CommonJS require for local development
3939+ const Database = require('better-sqlite3');
4040+ const fs = require('fs');
4141+ const path = require('path');
38423939- const dbPath = process.env.DATABASE_PATH || './data/app.db';
4040- const dbDir = path.dirname(dbPath);
4343+ const dbPath = process.env.DATABASE_PATH || './data/app.db';
4444+ const dbDir = path.dirname(dbPath);
41454242- if (!fs.existsSync(dbDir)) {
4343- fs.mkdirSync(dbDir, { recursive: true });
4444- }
4646+ if (!fs.existsSync(dbDir)) {
4747+ fs.mkdirSync(dbDir, { recursive: true });
4848+ }
45494646- const sqlite = new Database(dbPath);
4747- sqlite.pragma('journal_mode = WAL');
5050+ const sqlite = new Database(dbPath);
5151+ sqlite.pragma('journal_mode = WAL');
48524949- // Initialize tables
5050- sqlite.exec(`
5151- CREATE TABLE IF NOT EXISTS games (
5252- id TEXT PRIMARY KEY,
5353- rkey TEXT NOT NULL,
5454- creator_did TEXT NOT NULL,
5555- player_one TEXT NOT NULL,
5656- player_two TEXT,
5757- board_size INTEGER NOT NULL DEFAULT 19,
5858- status TEXT NOT NULL CHECK(status IN ('waiting', 'active', 'completed')),
5959- action_count INTEGER NOT NULL DEFAULT 0,
6060- last_action_type TEXT,
6161- winner TEXT,
6262- handicap INTEGER DEFAULT 0,
6363- created_at TEXT NOT NULL,
6464- updated_at TEXT NOT NULL
6565- );
5353+ // Initialize tables
5454+ sqlite.exec(`
5555+ CREATE TABLE IF NOT EXISTS games (
5656+ id TEXT PRIMARY KEY,
5757+ rkey TEXT NOT NULL,
5858+ creator_did TEXT NOT NULL,
5959+ player_one TEXT NOT NULL,
6060+ player_two TEXT,
6161+ board_size INTEGER NOT NULL DEFAULT 19,
6262+ status TEXT NOT NULL CHECK(status IN ('waiting', 'active', 'completed')),
6363+ action_count INTEGER NOT NULL DEFAULT 0,
6464+ last_action_type TEXT,
6565+ winner TEXT,
6666+ handicap INTEGER DEFAULT 0,
6767+ created_at TEXT NOT NULL,
6868+ updated_at TEXT NOT NULL
6969+ );
66706767- CREATE INDEX IF NOT EXISTS idx_games_status ON games(status);
6868- CREATE INDEX IF NOT EXISTS idx_games_player_one ON games(player_one);
6969- CREATE INDEX IF NOT EXISTS idx_games_player_two ON games(player_two);
7070- CREATE INDEX IF NOT EXISTS idx_games_rkey ON games(rkey);
7171- CREATE INDEX IF NOT EXISTS idx_games_creator_did ON games(creator_did);
7272- `);
7171+ CREATE INDEX IF NOT EXISTS idx_games_status ON games(status);
7272+ CREATE INDEX IF NOT EXISTS idx_games_player_one ON games(player_one);
7373+ CREATE INDEX IF NOT EXISTS idx_games_player_two ON games(player_two);
7474+ CREATE INDEX IF NOT EXISTS idx_games_rkey ON games(rkey);
7575+ CREATE INDEX IF NOT EXISTS idx_games_creator_did ON games(creator_did);
7676+ `);
7777+7878+ localDb = new Kysely<Database>({
7979+ dialect: new SqliteDialect({ database: sqlite }),
8080+ });
73817474- localDb = new Kysely<Database>({
7575- dialect: new SqliteDialect({ database: sqlite }),
7676- });
8282+ console.log('[DB] Local database initialized successfully, type:', typeof localDb, 'hasSelectFrom:', typeof localDb.selectFrom);
8383+8484+ // Verify it works
8585+ if (typeof localDb.selectFrom !== 'function') {
8686+ throw new Error('Kysely instance does not have selectFrom method!');
8787+ }
77887878- console.log('[DB] Local database initialized:', typeof localDb, 'hasSelectFrom:', typeof localDb.selectFrom);
7979- return localDb;
8989+ return localDb;
9090+ } catch (err) {
9191+ console.error('[DB] Error initializing local database:', err);
9292+ throw err;
9393+ }
8094}
81958296export function getDb(platform: App.Platform | undefined): Kysely<Database> {