ShipNowKit uses Prisma to manage database schema. All database model definitions are in the db/prisma/schema.prisma file.
Schema File Locations
Database schema definitions are in the following files:
- Main schema:
db/prisma/schema.prisma- Currently used schema - Variant schemas:
db/prisma/variants/- Schema variants for different databasesmysql.prismapostgresql.prismasqlite.prismamongodb.prisma
Update Schema
Add New Models
To add a new model (e.g., "Post"), add it to the schema.prisma file:
model Post {
id String @id @default(cuid())
title String
content String?
author User @relation(fields: [authorId], references: [id])
authorId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("posts")
}Then add the relationship in the User model:
model User {
// ... other fields
posts Post[]
}Modify Existing Models
To modify an existing model, directly edit the model definition in the schema.prisma file:
model User {
id String @id @default(cuid())
name String?
email String @unique
// Add new fields
phone String? @unique
bio String?
// ... other fields
}Add Enum Types
To add a new enum type:
enum PostStatus {
draft
published
archived
}
model Post {
// ... other fields
status PostStatus @default(draft)
}Migrate Changes
ShipNowKit provides two ways to apply schema changes: Migration and Push.
Method 1: Create Migration (Recommended for Production)
Migrations create migration history records, suitable for scenarios requiring version control:
npm run db:migrateThis command will:
- Detect changes in the schema file
- Generate migration files (saved in the
db/prisma/migrations/directory) - Apply migrations to the database
- Regenerate Prisma Client
Migration file naming format: YYYYMMDDHHMMSS_migration_name/
Migration Naming: When running npm run db:migrate, Prisma will prompt you to enter a migration name. It's recommended to use descriptive names, such as add_post_model or add_user_phone_field.
Method 2: Direct Push (Suitable for Development)
If you just want to quickly push schema changes to the database, you can use:
npm run db:pushThis command will:
- Directly apply schema changes to the database
- Will not create migration files
- Regenerate Prisma Client
Development Use: db:push does not create migration history, suitable for rapid prototyping. Production environments should use db:migrate to maintain migration history.
Generate Prisma Client
After updating the schema, you need to regenerate Prisma Client:
npm run db:generateAuto-Generation: When you run npm run db:migrate or npm run db:push, Prisma Client will automatically regenerate. The development server will also automatically run db:generate on startup.
Database Reset (Use with Caution)
If you need to reset the database and reapply all migrations:
npm run db:resetData Loss Warning: db:reset will delete all data in the database! Use only in development environments and ensure there is no important data.
Multi-Database Support
ShipNowKit supports switching between multiple databases. When switching database types:
-
Run the switch script:
npm run db:use:postgresql # or other database type -
Update environment variables: Ensure the
DATABASE_URLin.env.localmatches the new database type -
Apply migrations: Run
npm run db:migrateornpm run db:push
Using Database Studio
Learn how to use Prisma Studio to visually view and edit data in your database. Prisma Studio provides a graphical interface for browsing and managing database records.
Authentication Overview
Learn how to use authentication features in ShipNowKit. ShipNowKit uses better-auth for authentication and provides all necessary UI components for authentication flows.