|
| 1 | +# Adding Vercel KV Storage |
| 2 | + |
| 3 | +This guide explains how to add Vercel KV for persistent cloud storage in the Object UI Studio. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. A Vercel account |
| 8 | +2. The playground deployed to Vercel (or set up locally with Vercel CLI) |
| 9 | + |
| 10 | +## Step 1: Install Vercel KV Package |
| 11 | + |
| 12 | +Add the `@vercel/kv` package to the playground: |
| 13 | + |
| 14 | +```bash |
| 15 | +cd apps/playground |
| 16 | +pnpm add @vercel/kv |
| 17 | +``` |
| 18 | + |
| 19 | +Or if using the root: |
| 20 | +```bash |
| 21 | +pnpm add @vercel/kv --filter @apps/playground |
| 22 | +``` |
| 23 | + |
| 24 | +## Step 2: Set Up Vercel KV Database |
| 25 | + |
| 26 | +### Option A: Using Vercel Dashboard |
| 27 | + |
| 28 | +1. Go to your Vercel dashboard: https://vercel.com/dashboard |
| 29 | +2. Select your project |
| 30 | +3. Go to the "Storage" tab |
| 31 | +4. Click "Create Database" → "KV" |
| 32 | +5. Name your database (e.g., `objectui-designs`) |
| 33 | +6. Click "Create" |
| 34 | +7. Copy the environment variables provided |
| 35 | + |
| 36 | +### Option B: Using Vercel CLI |
| 37 | + |
| 38 | +```bash |
| 39 | +vercel env pull .env.local |
| 40 | +``` |
| 41 | + |
| 42 | +## Step 3: Configure Environment Variables |
| 43 | + |
| 44 | +Create a `.env.local` file in `apps/playground/`: |
| 45 | + |
| 46 | +```env |
| 47 | +# Vercel KV |
| 48 | +KV_URL="your_kv_url_here" |
| 49 | +KV_REST_API_URL="your_kv_rest_api_url_here" |
| 50 | +KV_REST_API_TOKEN="your_kv_rest_api_token_here" |
| 51 | +KV_REST_API_READ_ONLY_TOKEN="your_kv_rest_api_read_only_token_here" |
| 52 | +``` |
| 53 | + |
| 54 | +These will be automatically set in Vercel when you create the KV database. |
| 55 | + |
| 56 | +## Step 4: Update serverStorage.ts |
| 57 | + |
| 58 | +The storage module has been updated to support both in-memory (fallback) and Vercel KV storage. |
| 59 | + |
| 60 | +Key changes: |
| 61 | +- Detects if KV is available via environment variables |
| 62 | +- Falls back to in-memory storage if KV is not configured |
| 63 | +- Uses Redis-like commands for KV operations |
| 64 | +- Stores designs with prefix `design:` and shared designs with prefix `shared:` |
| 65 | + |
| 66 | +## Step 5: Update .gitignore |
| 67 | + |
| 68 | +Add to your `.gitignore`: |
| 69 | + |
| 70 | +``` |
| 71 | +.env*.local |
| 72 | +.vercel |
| 73 | +``` |
| 74 | + |
| 75 | +## Step 6: Deploy to Vercel |
| 76 | + |
| 77 | +```bash |
| 78 | +vercel deploy |
| 79 | +``` |
| 80 | + |
| 81 | +The KV environment variables will be automatically available in production. |
| 82 | + |
| 83 | +## Usage |
| 84 | + |
| 85 | +No code changes needed in your API routes! The storage module automatically: |
| 86 | +1. Uses Vercel KV if available (in production/Vercel environment) |
| 87 | +2. Falls back to in-memory storage for local development |
| 88 | + |
| 89 | +## Testing Locally with KV |
| 90 | + |
| 91 | +To test KV locally: |
| 92 | + |
| 93 | +1. Install Vercel CLI: `npm i -g vercel` |
| 94 | +2. Link your project: `vercel link` |
| 95 | +3. Pull environment variables: `vercel env pull .env.local` |
| 96 | +4. Run dev server: `pnpm dev` |
| 97 | + |
| 98 | +## Data Structure in KV |
| 99 | + |
| 100 | +``` |
| 101 | +design:{id} → JSON string of Design object |
| 102 | +shared:{shareId} → JSON string of Design object |
| 103 | +designs:all → Set of all design IDs |
| 104 | +``` |
| 105 | + |
| 106 | +## Monitoring |
| 107 | + |
| 108 | +View your KV database in Vercel Dashboard → Storage → Your KV Database |
| 109 | + |
| 110 | +## Migration from localStorage |
| 111 | + |
| 112 | +The current implementation keeps localStorage on the client side as a cache/fallback. To sync with KV: |
| 113 | + |
| 114 | +1. Client components will make API calls to server routes |
| 115 | +2. Server routes use KV for persistence |
| 116 | +3. localStorage serves as client-side cache for offline support |
| 117 | + |
| 118 | +## Cost |
| 119 | + |
| 120 | +Vercel KV free tier includes: |
| 121 | +- 256 MB storage |
| 122 | +- 3000 requests per day |
| 123 | + |
| 124 | +Perfect for prototyping and small projects! |
0 commit comments