IntroductionTech StackQuick Start
Database OverviewUsing Database ClientUsing Database StudioUpdate Schema and Migrate Changes
Logo
Database
X logoShare to XLinkedIn logoShare to LinkedIn

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 databases
    • mysql.prisma
    • postgresql.prisma
    • sqlite.prisma
    • mongodb.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:migrate

This command will:

  1. Detect changes in the schema file
  2. Generate migration files (saved in the db/prisma/migrations/ directory)
  3. Apply migrations to the database
  4. 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:push

This command will:

  1. Directly apply schema changes to the database
  2. Will not create migration files
  3. 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:generate

Auto-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:reset

Data 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:

  1. Run the switch script:

    npm run db:use:postgresql  # or other database type
  2. Update environment variables: Ensure the DATABASE_URL in .env.local matches the new database type

  3. Apply migrations: Run npm run db:migrate or npm 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.

On this page

Schema File Locations
Update Schema
Add New Models
Modify Existing Models
Add Enum Types
Migrate Changes
Method 1: Create Migration (Recommended for Production)
Method 2: Direct Push (Suitable for Development)
Generate Prisma Client
Database Reset (Use with Caution)
Multi-Database Support