···11# Database to ATProtocol Migration Index
2233-This document tracks all changes made during the migration from database-backed storage to ATProtocol-only data fetching.
33+This document tracks all changes made during the database optimization effort.
4455## Overview
6677-**Goal**: Remove all database dependencies and fetch data entirely from ATProtocol/Constellation.
77+**Goal**: Minimize database usage while ensuring ATProtocol is the source of truth.
8899-**Approach**:
1010-- Use Constellation API for game discovery
1111-- Calculate action counts dynamically by fetching moves/passes
1212-- Always fetch fresh data from ATProtocol
99+**Revised Approach** (after testing):
1010+- **Keep database as discovery index** for homepage and game lookups
1111+- **ATProtocol remains source of truth** - all game data written there first
1212+- **Database kept in sync** when ATProtocol records are created/updated
1313+- Database is necessary for efficient discovery of games user is not directly involved in
1414+1515+**Why keep the database?**
1616+- Constellation doesn't have a simple API to list all game records
1717+- Need rkey → creator DID mapping for game lookups
1818+- Efficient filtering by status, time, player without querying multiple PDSs
1919+- Better UX with fast queries vs slow PDS federation
13201421---
15221623## Changes Made
17241825### 1. ATProtocol Client Enhancements
1919-**File**: `src/lib/atproto-client.ts`
2020-**Commit**: 91f7916
2626+**Files**: `src/lib/atproto-client.ts`
2727+**Commits**: 91f7916, c520d33
21282229**Added Functions**:
2330- `fetchGameResigns(gameAtUri)` - Fetch resign records from Constellation backlinks
2424-- `calculateGameMetadata(gameUri, gameRecord, creatorDid, rkey)` - Calculate action count, last action type, and updated timestamp dynamically
3131+- `calculateGameMetadata(gameUri, gameRecord, creatorDid, rkey)` - Calculate action count, last action type, and updated timestamp dynamically by fetching moves/passes/resigns
2532- `listPlayerGames(did)` - List all game records from a player's PDS
2633- `fetchAllGames(knownPlayerDids)` - Aggregate games from multiple players and compute metadata
3434+- `findGame(creatorDid, rkey)` - Fetch a specific game and compute its metadata
3535+- `findGameByRkey(rkey, potentialCreatorDids)` - Search for a game by rkey across multiple PDSs
27362837**New Interface**:
2938- `GameWithMetadata` - Extends GameRecord with computed fields: `uri`, `rkey`, `creatorDid`, `actionCount`, `lastActionType`, `updatedAt`
30393131-**Purpose**: Provide functions to fetch and compute game metadata without database queries.
4040+**Purpose**: These functions CAN be used to fetch game data directly from ATProtocol without database, but in practice the database is kept for efficient indexing and discovery.
4141+4242+**Status**: ✅ Completed - Functions available for use where needed
32433344---
34453535-## Remaining Changes
4646+### 2. Database Strategy Revision
4747+**Files**: `src/routes/+page.server.ts`, `src/routes/game/[id]/+page.server.ts`
4848+**Commit**: c520d33
36493737-### 2. Homepage (In Progress)
3838-**File**: `src/routes/+page.server.ts`
3939-- Remove `getDb()` import
4040-- Replace database queries with ATProtocol fetching
4141-- Use `fetchAllGames()` or player-specific queries
5050+**Decision**: Keep database as discovery index
5151+- Homepage continues using database for efficient game listing and filtering
5252+- Game page continues using database for rkey lookups
5353+- Profile page will continue using database for player game queries
42544343-### 3. Game Page
4444-**File**: `src/routes/game/[id]/+page.server.ts`
4545-- Remove database dependency
4646-- Fetch game record directly from ATProtocol
5555+**Rationale**:
5656+- Constellation lacks comprehensive record listing APIs
5757+- Database provides fast rkey → game data mapping
5858+- Efficient filtering by status, time, player
5959+- Better performance for discovery of games user is not involved in
47604848-### 4. Profile Page
4949-**File**: `src/routes/profile/[did]/+page.server.ts`
5050-- Remove database queries
5151-- Use `listPlayerGames()` for player-specific game listing
6161+**Status**: ✅ Completed - Database retained for reads
52625353-### 5. API Routes
5454-**Files**: Multiple API endpoint files
5555-- `/api/games/+server.ts` (create game)
5656-- `/api/games/[id]/join/+server.ts` (join game)
5757-- `/api/games/[id]/move/+server.ts` (make move)
5858-- `/api/games/[id]/pass/+server.ts` (pass turn)
5959-- `/api/games/[id]/cancel/+server.ts` (resign)
6060-- `/api/games/[id]/score/+server.ts` (submit scores)
6161-- `/api/games/[id]/reaction/+server.ts` (add reaction)
6262-- Other routes: board, nudge, nudge-image, scoring-board, og-image, profile
6363+---
63646464-### 6. Cleanup
6565-- Remove `src/lib/server/db.ts`
6666-- Remove database dependencies from package.json
6767-- Remove DATABASE_PATH from environment variables
6565+## Future Improvements
6666+6767+### Potential Enhancements:
6868+1. **Write-through cache pattern**: Update ATProtocol first, then sync to database
6969+2. **Database rebuild utility**: Script to rebuild database from ATProtocol records
7070+3. **Eventual consistency**: Background job to sync database with ATProtocol periodically
7171+4. **Minimize database writes**: Only update database on game state changes that affect discovery
7272+5. **Remove database from individual game pages**: Could fetch directly from ATProtocol using functions from #1 above
7373+7474+---
7575+7676+## Current State
7777+7878+**Database Usage**:
7979+- ✅ Homepage - uses database for game discovery
8080+- ✅ Game page - uses database for rkey lookup
8181+- ⏸️ Profile page - still uses database (unchanged)
8282+- ⏸️ API routes - still use database for reads and writes (unchanged)
8383+8484+**ATProtocol Usage**:
8585+- ✅ All game data, moves, passes, resigns written to ATProtocol
8686+- ✅ Helper functions available to fetch directly from ATProtocol
8787+- ✅ Database kept in sync when records are created/updated
68886989---
7090