Skip to content

feat(db): SQLite module + initial migration (Task #1)#17

Open
lubobill1990 wants to merge 1 commit into
docs/multi-account-and-usage-billingfrom
feat/01-db-module
Open

feat(db): SQLite module + initial migration (Task #1)#17
lubobill1990 wants to merge 1 commit into
docs/multi-account-and-usage-billingfrom
feat/01-db-module

Conversation

@lubobill1990
Copy link
Copy Markdown
Collaborator

Summary

  • Add src/lib/db.ts with initDb/getDb/withTransaction over bun:sqlite (WAL, FK on, NORMAL sync)
  • Migration runner driven by meta.schema_version; ships migration 001 with the full schema (accounts, model_pricing[+versions], pricing_sync_log, usage_events, usage_daily, indexes)
  • Wire initDb into runServer and add --db-path flag (defaults to ~/.local/share/copilot-api/usage.sqlite)

Test plan

  • bun test — 37 pass (8 new in tests/db.test.ts)
  • bun run typecheck clean
  • bun run lint clean

Closes #1

🤖 Generated with Claude Code

Introduce bun:sqlite-backed db module with WAL mode, foreign keys,
and a meta-table-driven migration runner. Initial schema (001) creates
accounts, model_pricing, model_pricing_versions, pricing_sync_log,
usage_events, usage_daily and supporting indexes.

- src/lib/db.ts: initDb/getDb/withTransaction + test-only reset helper
- src/lib/migrations/001_initial.sql: full schema per design doc 02
- src/lib/paths.ts: USAGE_DB_PATH under APP_DIR
- src/start.ts: --db-path flag, initDb after ensurePaths
- src/types.d.ts: declare *.sql text imports
- tests/db.test.ts: 8 tests covering init, idempotency, tx, WAL, indexes

Closes #1

Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant