This guide explains how to migrate from JSON file storage to a proper Prisma database.
The Prisma schema has been updated to include:
- User model with authentication fields
- Question model with proper relationships
- Comment model with cascade deletion
- Removed views field as requested
# Generate Prisma client
npm run db:generate
# Push schema to database
npm run db:push
# Migrate existing data from JSON files
npm run db:migratemodel User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
questions Question[]
comments Comment[]
}
model Question {
id String @id @default(cuid())
title String
description String
tags String // JSON string
authorId String
author User @relation(fields: [authorId], references: [id])
createdAt DateTime @default(now())
votes Int @default(0)
comments Comment[]
}
model Comment {
id String @id @default(cuid())
content String
authorId String
author User @relation(fields: [authorId], references: [id])
questionId String
question Question @relation(fields: [questionId], references: [id], onDelete: Cascade)
createdAt DateTime @default(now())
votes Int @default(0)
}/api/questions- Now uses Prisma for CRUD operations/api/questions/[id]- Individual question operations/api/questions/[id]/comments- Comment operations
- ✅ Proper database relationships with foreign keys
- ✅ Cascade deletion - Comments are deleted when questions are deleted
- ✅ User management - Proper user creation and updates
- ✅ Data integrity - No more JSON file corruption risks
- ✅ Scalability - Can handle large amounts of data
- ✅ Performance - Optimized database queries
- Data Integrity: Foreign key constraints ensure data consistency
- Performance: Indexed queries are much faster than JSON file reads
- Scalability: Can handle thousands of questions and comments
- Reliability: No risk of JSON file corruption
- Type Safety: Full TypeScript support with Prisma client
- Migrations: Easy schema updates and data migrations
The migration script (scripts/migrate-data.js) will:
- Read existing questions from
data/questions.json - Create users in the database
- Create questions with proper relationships
- Create comments with proper relationships
- Preserve all existing data and timestamps
Make sure your .env file includes:
# Database
DATABASE_URL="file:./dev.db"
# NextAuth
NEXTAUTH_SECRET=your-secret-here
NEXTAUTH_URL=http://localhost:3000
# Google OAuth
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secretAfter running the migration:
- Test the application - Verify all features work correctly
- Remove JSON files - Delete
data/questions.jsonafter confirming migration - Update documentation - Remove references to JSON file storage
- Monitor performance - Database queries should be much faster
The application now uses a proper database with Prisma, providing better performance, reliability, and scalability!