···11+# Database to ATProtocol Migration Index
22+33+This document tracks all changes made during the migration from database-backed storage to ATProtocol-only data fetching.
44+55+## Overview
66+77+**Goal**: Remove all database dependencies and fetch data entirely from ATProtocol/Constellation.
88+99+**Approach**:
1010+- Use Constellation API for game discovery
1111+- Calculate action counts dynamically by fetching moves/passes
1212+- Always fetch fresh data from ATProtocol
1313+1414+---
1515+1616+## Changes Made
1717+1818+### 1. ATProtocol Client Enhancements
1919+**File**: `src/lib/atproto-client.ts`
2020+**Commit**: 91f7916
2121+2222+**Added Functions**:
2323+- `fetchGameResigns(gameAtUri)` - Fetch resign records from Constellation backlinks
2424+- `calculateGameMetadata(gameUri, gameRecord, creatorDid, rkey)` - Calculate action count, last action type, and updated timestamp dynamically
2525+- `listPlayerGames(did)` - List all game records from a player's PDS
2626+- `fetchAllGames(knownPlayerDids)` - Aggregate games from multiple players and compute metadata
2727+2828+**New Interface**:
2929+- `GameWithMetadata` - Extends GameRecord with computed fields: `uri`, `rkey`, `creatorDid`, `actionCount`, `lastActionType`, `updatedAt`
3030+3131+**Purpose**: Provide functions to fetch and compute game metadata without database queries.
3232+3333+---
3434+3535+## Remaining Changes
3636+3737+### 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
4242+4343+### 3. Game Page
4444+**File**: `src/routes/game/[id]/+page.server.ts`
4545+- Remove database dependency
4646+- Fetch game record directly from ATProtocol
4747+4848+### 4. Profile Page
4949+**File**: `src/routes/profile/[did]/+page.server.ts`
5050+- Remove database queries
5151+- Use `listPlayerGames()` for player-specific game listing
5252+5353+### 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+6464+### 6. Cleanup
6565+- Remove `src/lib/server/db.ts`
6666+- Remove database dependencies from package.json
6767+- Remove DATABASE_PATH from environment variables
6868+6969+---
7070+7171+## Testing Checklist
7272+7373+After migration, test:
7474+- [ ] Homepage loads and displays games correctly
7575+- [ ] Game page works with all game states
7676+- [ ] Profile page shows player's games
7777+- [ ] Creating new games works
7878+- [ ] Joining games works
7979+- [ ] Making moves works
8080+- [ ] Passing works
8181+- [ ] Resigning works
8282+- [ ] Scoring works
8383+- [ ] Reactions work
8484+8585+---
8686+8787+## Rollback Instructions
8888+8989+If something breaks:
9090+9191+1. Revert to commit before migration: `git log --oneline` to find the commit
9292+2. Check this index to see what was changed
9393+3. Each commit is atomic and can be reverted individually
9494+9595+---
9696+9797+## Notes
9898+9999+- The database was only used as a discovery index, not as source of truth
100100+- All game data was already in ATProtocol records
101101+- This migration makes the app more decentralized and resilient