import { Kysely } from 'kysely'; import { D1Dialect } from 'kysely-d1'; import type { App } from '@sveltejs/kit'; export interface GameRecord { id: string; // AT URI (game_at_uri) rkey: string; // Record key (TID) creator_did: string; // DID of whoever created the game (owns the ATProto record) player_one: string | null; player_two: string | null; board_size: number; status: 'waiting' | 'active' | 'completed'; action_count: number; last_action_type: string | null; winner: string | null; handicap: number; created_at: string; updated_at: string; } export interface Database { games: GameRecord; } export function getDb(platform: App.Platform | undefined): Kysely { // Production: Use Cloudflare D1 if (platform?.env?.DB) { return new Kysely({ dialect: new D1Dialect({ database: platform.env.DB }), }); } // No D1 configured throw new Error( 'Database not configured. In Cloudflare, set up D1 and bind it as "DB". ' + 'For local development, use `wrangler pages dev` with D1 bindings.' ); }