Overview
SlugShare uses PostgreSQL as its database and Prisma as the ORM (Object-Relational Mapping) layer. This page covers setting up your database schema and understanding the data models.Before proceeding, ensure you have completed the Environment Variables setup with a valid
DATABASE_URL.Database Schema
SlugShare uses six main database models:NextAuth.js Models
These models are required by NextAuth.js for authentication:- User - User accounts with email, name, phone, and profile image
- Account - OAuth account connections (Google, etc.)
- Session - Active user sessions
- VerificationToken - Email verification tokens
Application Models
These models power SlugShare’s core functionality:- Points - User dining points balance (one-to-one with User)
- Request - Point sharing requests between users
- Notification - User notifications for request status updates
Running Migrations
Verify database connection
First, ensure your database is accessible:If this succeeds, your
DATABASE_URL is correctly configured.Run Prisma migrations
Apply all database migrations to create the schema:This command will:
- Create the database if it doesn’t exist
- Run all migration files in
prisma/migrations/ - Generate the Prisma Client with TypeScript types
- Create all tables with proper relationships and indexes
You may be prompted to name your migration. This is optional for initial setup.
Database Models in Detail
User Model
id- Unique identifier (CUID)email- Required, unique email addresspoints- One-to-one relation with Points modelrequests- Requests created by this userdonations- Requests this user has accepted (as donor)
Points Model
userId- Unique foreign key to Userbalance- Current points balance (integer)updatedAt- Auto-updated timestamp
Points records are created on-demand using
upsert when a user first accesses their points balance. New users don’t automatically have a Points record.Request Model
requesterId- User creating the requestdonorId- User who accepted the request (nullable)location- UCSC dining hall locationpointsRequested- Number of points requestedstatus-"pending","accepted", or"declined"message- Optional message from requester
- Cowell/Stevenson
- Crown/Merrill
- Porter/Kresge
- Rachel Carson/Oakes
- College Nine/Ten
Notification Model
type- Notification type (e.g.,"request_accepted","request_declined")message- Notification message textread- Whether the user has read the notification
Common Prisma Commands
Database Schema Workflow
When you need to modify the database schema:Create migration
Generate and apply the migration:This creates a migration file in
prisma/migrations/ and applies it to your database.Update Prisma Client
Regenerate TypeScript types:
This happens automatically after
migrate dev, but you should run it manually if you only changed comments or client configuration.Points Transfer System
SlugShare uses Prisma transactions to ensure atomic point transfers. When a user accepts a request:- Validate the request status is “pending”
- Check the donor has sufficient balance
- Execute atomic transaction:
- Decrement donor’s points
- Increment requester’s points
- Update request status to “accepted”
- Set
donorIdon the request
app/api/requests/[id]/accept/route.ts):
Transactions ensure that if any operation fails, all changes are rolled back. This prevents partial transfers or data inconsistencies.
Seeding the Database (Optional)
To add test data for development, you can create a seed script:Troubleshooting
”Error: P1001: Can’t reach database server”
Your database connection failed. Check:- PostgreSQL is running
DATABASE_URLin.envis correct- Firewall/network allows connections
- Database credentials are valid
”Error: P3009: Failed to create database”
The database doesn’t exist. Create it manually:”Error: Unique constraint failed”
You’re trying to create duplicate data. Check:- Email addresses must be unique
- Each user can only have one Points record
- Provider + providerAccountId must be unique for OAuth accounts
”Prisma Client not generated”
Run:Next Steps
Your database is now set up! You can:- Start the development server:
npm run dev - Access the app: Open
http://localhost:3000 - View the database: Run
npx prisma studio