Task 01 — DB module + migrations + meta
Depends on: —
Unblocks: 02, 06
Goal
Set up bun:sqlite with a versioned migration system so every later step can
just call getDb() and run prepared statements.
Scope
- New file
src/lib/db.ts.
- Schema for
accounts, usage_events, usage_daily,
model_pricing, model_pricing_versions, pricing_sync_log, meta.
- Migration runner driven by
meta.schema_version (start at 1).
- WAL mode,
synchronous = NORMAL.
- DB path resolved from
--db-path flag (default
~/.local/share/copilot-api/usage.sqlite); add the flag to start.ts
argument parser.
ensurePaths() (in src/lib/paths.ts) gains the DB directory.
Non-goals
- Any actual writes from feature code (later tasks own that).
- Pricing data seeding.
API surface
export function initDb(path: string): Database
export function getDb(): Database
export function withTransaction<T>(fn: (db: Database) => T): T
Migrations live in src/lib/migrations/001_initial.sql and are applied
in order based on filename.
Definition of Done
Task 01 — DB module + migrations + meta
Depends on: —
Unblocks: 02, 06
Goal
Set up
bun:sqlitewith a versioned migration system so every later step canjust call
getDb()and run prepared statements.Scope
src/lib/db.ts.accounts,usage_events,usage_daily,model_pricing,model_pricing_versions,pricing_sync_log,meta.meta.schema_version(start at1).synchronous = NORMAL.--db-pathflag (default~/.local/share/copilot-api/usage.sqlite); add the flag tostart.tsargument parser.
ensurePaths()(insrc/lib/paths.ts) gains the DB directory.Non-goals
API surface
Migrations live in
src/lib/migrations/001_initial.sqland are appliedin order based on filename.
Definition of Done
bun run devstarts and creates a fresh DB with all tables.bun run devdoes NOT re-apply migrations.meta.schema_version = 1and that all tables exist.--db-pathworks with a custom path.docs/tasks/01-db-module.mddocs/design/