|
| 1 | +# Database Setup for Task 9B: Feedback Storage |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This directory contains the database migration scripts and setup documentation for the feedback storage system. |
| 6 | + |
| 7 | +## Migration Files |
| 8 | + |
| 9 | +- `001_create_feedback_table.sql` - Creates the feedback table with proper schema and indexes |
| 10 | + |
| 11 | +## Setup Instructions |
| 12 | + |
| 13 | +### 1. Supabase Project Setup |
| 14 | + |
| 15 | +1. Create a new Supabase project at https://app.supabase.com |
| 16 | +2. Navigate to Settings > API in your project |
| 17 | +3. Copy your project URL and anon key |
| 18 | + |
| 19 | +### 2. Environment Variables |
| 20 | + |
| 21 | +Add the following to your `.env.local` file: |
| 22 | + |
| 23 | +```bash |
| 24 | +SUPABASE_URL=https://your-project-id.supabase.co |
| 25 | +SUPABASE_ANON_KEY=your_anon_key_here |
| 26 | +``` |
| 27 | + |
| 28 | +For server-side operations (migrations), also add: |
| 29 | + |
| 30 | +```bash |
| 31 | +SUPABASE_SERVICE_ROLE_KEY=your_service_role_key_here |
| 32 | +``` |
| 33 | + |
| 34 | +### 3. Running Migrations |
| 35 | + |
| 36 | +1. Go to your Supabase project dashboard |
| 37 | +2. Navigate to the SQL Editor |
| 38 | +3. Copy and paste the contents of `001_create_feedback_table.sql` |
| 39 | +4. Execute the SQL to create the table and indexes |
| 40 | + |
| 41 | +### 4. Verify Setup |
| 42 | + |
| 43 | +You can verify the setup by: |
| 44 | + |
| 45 | +1. Checking the table exists in the Table Editor |
| 46 | +2. Running the application and submitting feedback |
| 47 | +3. Checking the feedback table for new entries |
| 48 | + |
| 49 | +## Database Schema |
| 50 | + |
| 51 | +### feedback table |
| 52 | + |
| 53 | +- `feedback_id` (UUID, Primary Key) - Auto-generated unique identifier |
| 54 | +- `reflection_id` (VARCHAR(255)) - Links feedback to AI reflection responses |
| 55 | +- `feedback_type` (VARCHAR(10)) - Either 'positive' or 'negative' |
| 56 | +- `user_agent` (TEXT, Nullable) - Browser/device information for context |
| 57 | +- `created_at` (TIMESTAMP WITH TIME ZONE) - Automatic timestamp |
| 58 | + |
| 59 | +### Indexes |
| 60 | + |
| 61 | +- `idx_feedback_reflection_id` - For querying feedback by reflection |
| 62 | +- `idx_feedback_created_at` - For time-based queries and cleanup |
| 63 | + |
| 64 | +## Features |
| 65 | + |
| 66 | +- Anonymous feedback collection |
| 67 | +- User-agent capture for device context |
| 68 | +- Proper indexing for performance |
| 69 | +- ACID transactions for data integrity |
| 70 | +- Type-safe operations with TypeScript |
| 71 | +- Rate limiting and abuse prevention |
| 72 | +- Comprehensive error handling |
| 73 | + |
| 74 | +## API Usage |
| 75 | + |
| 76 | +The feedback system exposes a POST endpoint at `/api/feedback`: |
| 77 | + |
| 78 | +```typescript |
| 79 | +// Example request |
| 80 | +const response = await fetch('/api/feedback', { |
| 81 | + method: 'POST', |
| 82 | + headers: { |
| 83 | + 'Content-Type': 'application/json', |
| 84 | + }, |
| 85 | + body: JSON.stringify({ |
| 86 | + reflection_id: 'refl_123_abc', |
| 87 | + feedback_type: 'positive', |
| 88 | + }), |
| 89 | +}) |
| 90 | +``` |
| 91 | + |
| 92 | +## Security Considerations |
| 93 | + |
| 94 | +- All data is anonymous (no user identification) |
| 95 | +- Rate limiting prevents abuse (5 requests per minute per IP) |
| 96 | +- Input validation prevents SQL injection |
| 97 | +- User-agent length is limited to prevent overflow |
| 98 | +- Environment variables protect database credentials |
| 99 | + |
| 100 | +## Testing |
| 101 | + |
| 102 | +- Unit tests: `src/lib/__tests__/feedback-storage.test.ts` |
| 103 | +- Integration tests: `src/app/api/feedback/__tests__/route.test.ts` |
| 104 | +- Type validation tests: `src/types/__tests__/database.test.ts` |
| 105 | + |
| 106 | +Run tests with: |
| 107 | + |
| 108 | +```bash |
| 109 | +pnpm test src/lib/__tests__/feedback-storage.test.ts |
| 110 | +pnpm test src/app/api/feedback/__tests__/route.test.ts |
| 111 | +pnpm test src/types/__tests__/database.test.ts |
| 112 | +``` |
0 commit comments