Setup and schema.prisma

Install and Init

Prisma is a type-safe ORM. You describe models in schema.prisma, and Prisma generates a fully typed client. Many NestJS teams prefer it to TypeORM for its autocomplete and simple queries.

Example

npm i @prisma/client
npm i -D prisma
npx prisma init

Example output

✔ Your Prisma schema was created at prisma/schema.prisma
  You can now open it in your favorite editor.

Next steps:
1. Set the DATABASE_URL in the .env file to point to your existing database.
2. Run prisma db pull to turn your database schema into a Prisma schema.
3. Run prisma generate to generate the Prisma Client.

The ShopEase Schema

prisma/schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Category {
  id       Int       @id @default(autoincrement())
  slug     String    @unique
  name     String
  products Product[]
}

model Product {
  id          Int         @id @default(autoincrement())
  name        String      @db.VarChar(120)
  description String?
  price       Decimal     @db.Decimal(10, 2)
  stock       Int         @default(0)
  category    Category    @relation(fields: [categoryId], references: [id])
  categoryId  Int
  orderItems  OrderItem[]
  createdAt   DateTime    @default(now())
  updatedAt   DateTime    @updatedAt

  @@index([categoryId])
}

model User {
  id           Int     @id @default(autoincrement())
  email        String  @unique
  name         String
  passwordHash String
  role         Role    @default(CUSTOMER)
  orders       Order[]
}

enum Role {
  CUSTOMER
  SELLER
  ADMIN
}

model Order {
  id        String      @id @default(uuid())
  user      User        @relation(fields: [userId], references: [id])
  userId    Int
  items     OrderItem[]
  total     Decimal     @db.Decimal(12, 2)
  status    String      @default("PENDING")
  createdAt DateTime    @default(now())
}

model OrderItem {
  id        Int     @id @default(autoincrement())
  order     Order   @relation(fields: [orderId], references: [id], onDelete: Cascade)
  orderId   String
  product   Product @relation(fields: [productId], references: [id])
  productId Int
  quantity  Int
  unitPrice Decimal @db.Decimal(10, 2)
}

Example

Create the tables and generate the client:

npx prisma migrate dev --name init

Example output

Applying migration `20260115100000_init`

The following migration(s) have been created and applied from new schema changes:

migrations/
  └─ 20260115100000_init/
    └─ migration.sql

Your database is now in sync with your schema.
✔ Generated Prisma Client (v6.2.1) to ./node_modules/@prisma/client in 98ms