Skip to content

Port to Neon + Better Auth, with SQL-aggregated hot paths#6

Merged
f1shyfang merged 1 commit into
mainfrom
port/supabase-to-neon-betterauth
Jun 1, 2026
Merged

Port to Neon + Better Auth, with SQL-aggregated hot paths#6
f1shyfang merged 1 commit into
mainfrom
port/supabase-to-neon-betterauth

Conversation

@f1shyfang
Copy link
Copy Markdown
Owner

This branch is what's currently deployed to production (betcha on Vercel, commit 48c22fb). It contains two pieces of work:

1. Port from Supabase → Neon + Better Auth (commit 3fa0130)

Migrates identity/auth off Supabase onto Neon Postgres + Better Auth; removes the Supabase client/admin/RLS layer and supabase/migrations.

2. Performance: SQL-aggregated hot read paths (commit 48c22fb, this session)

The hot read paths were doing O(N) work in JS and running queries sequentially. All four changes are test-first, with Vitest integration tests against an ephemeral Neon branch asserting both correctness and the performance invariant (query count, SQL shape, parallel dispatch).

Optimization Before → After
Balance fetch every ledger row + JS sum → single SELECT SUM(delta)
Market detail 7 sequential queries → gated reads + Promise.all + aggregate counts
Leaderboard pull all group ledger rows + JS aggregate → windowed GROUP BY (SUM OVER + ROW_NUMBER)
Indexes seq scans → ix_ledger_entries_user_id, ix_markets_group_id (migration 009)

API route handlers now delegate to tested server/queries/* modules (-153 lines in the handlers).

Test harness

New Vitest suite (npm run test:perf) runs test/**/*.vitest.js against a Neon branch (.env.test.local, gitignored). Data-access functions take an injectable query so tests can spy on call counts / concurrency.

Verification

  • npm run test:perf → 13 passed
  • Legacy npm test (resolve integration) → 12 passed, no regressions
  • npm run build → clean
  • Migration 009 already applied to production Neon (ix_ledger_entries_user_id, ix_markets_group_id verified present)
  • Production smoke check: /api/health 200, homepage 200, unauthenticated market detail 401, no runtime errors

🤖 Generated with Claude Code

…, add indexes

Hot read paths were doing O(N) work in JS and running queries sequentially.
All four changes are driven by Vitest integration tests against a Neon branch
that assert both correctness and the performance invariant (query count, SQL
shape, parallel dispatch).

- balance: single SELECT SUM(delta) instead of fetching every ledger row
- market detail: 7 sequential queries -> gated reads + Promise.all + aggregate
  counts; balance via SUM
- leaderboard: windowed GROUP BY (SUM OVER + ROW_NUMBER) instead of pulling all
  ledger rows and summing in JS
- migration 009: indexes on ledger_entries(user_id) and markets(group_id)

API handlers now delegate to tested server/queries/* modules (-153 lines).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 1, 2026 01:10
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
betcha Ready Ready Preview, Comment Jun 1, 2026 1:11am

@f1shyfang f1shyfang merged commit 1a41b0a into main Jun 1, 2026
2 of 3 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

2 participants