refactor(storage): migrate to Upstash REST SDK with KEY_PREFIX

Replace node-redis with @upstash/redis REST client and route all keys through
a centralized adapter that prepends KEY_PREFIX (default 'vngeoguessr:'). The
new Upstash DB can now be safely shared with other Vercel projects without
key collisions.

- src/lib/upstash.js: new adapter with getJson/putJson/del + zAdd/zRange/
  zScore/zRank/zRevRank/zRemRangeByRank helpers; reads UPSTASH_REDIS_REST_URL
  or KV_REST_API_URL aliases
- src/lib/session.js, src/lib/leaderboard.js: use the adapter; public APIs
  unchanged so api routes stay untouched
- src/lib/redis.js: removed
- scripts/migrate-upstash.js: one-shot copy from old TCP DB to new REST DB,
  prepending the prefix; idempotent, with --dry-run flag
- docs/tech-stack.md: reflect the new SDK and prefix model
This commit is contained in:
tiennm99 committed 2026-05-10 00:00:47 +07:00
1 parent f0f19508c4
commit 53ee7957e7
8 files changed
+423 -162

No files matched your search

+7 -5
View File
@@ -17,10 +17,12 @@
- **Server-side Calculations**: All geographic processing on backend
## Data Storage & Session Management
- **Redis**: Complete session and leaderboard storage
- **Redis Sorted Sets**: Leaderboard ranking with automatic trimming
- **Upstash Redis (REST)**: Session and leaderboard storage via `@upstash/redis` SDK
- **Vercel Marketplace integration**: credentials provided as `KV_REST_API_URL`/`KV_REST_API_TOKEN` (or vanilla `UPSTASH_REDIS_REST_URL`/`UPSTASH_REDIS_REST_TOKEN`)
- **Project key prefix**: all physical keys carry `KEY_PREFIX` (default `vngeoguessr:`) so the same Upstash DB can be safely shared with other Vercel projects without collisions. Prefix is applied centrally in `src/lib/upstash.js`; callers pass logical keys only.
- **Sorted Sets**: Leaderboard ranking with automatic trimming (top 200)
- **UUID v4**: Session identifier generation
- **30-minute Session Expiry**: Automatic Redis-based cleanup
- **30-minute Session Expiry**: Automatic TTL-based cleanup
## UI Components & Styling
- **shadcn/ui**: Complete component library with "new-york" style
@@ -41,7 +43,7 @@
- **@vercel/speed-insights**: Performance monitoring
## Key Dependencies
- **uuid**: v11.1.0 for unique session identifier generation
- **redis**: v5.8.0 for data persistence
- **uuid**: unique session identifier generation
- **@upstash/redis**: REST-based Upstash client (replaces node-redis); fluid-compute friendly, no socket pooling required
- **JavaScript Only**: No TypeScript - pure JavaScript implementation
- **Individual Parameters**: Functions use separate parameters instead of object destructuring