Schema Prisma

Contoh schema prisma

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

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

model User {
  id        Int       @id @default(autoincrement())
  name      String
  email     String    @unique
  points    Int       @default(0)
  orders    Order[]
  createdAt DateTime  @default(now())
}

model Product {
  id        Int       @id @default(autoincrement())
  name      String
  price     Float
  stock     Int       @default(0)
  stocks    Stock[]
  orders    Order[]
  createdAt DateTime  @default(now())
  categories CategoriesOnProduct[]
}

model Order {
  id        Int      @id @default(autoincrement())
  userId    Int
  productId Int
  quantity  Int
  user      User     @relation(fields: [userId], references: [id])
  product   Product  @relation(fields: [productId], references: [id])
  createdAt DateTime @default(now())
}

model Supplier {
  id        Int       @id @default(autoincrement())
  name      String
  stocks    Stock[]
  createdAt DateTime  @default(now())
}

model Stock {
  id         Int      @id @default(autoincrement())
  quantity   Int
  product    Product  @relation(fields: [productId], references: [id])
  productId  Int
  supplier   Supplier @relation(fields: [supplierId], references: [id])
  supplierId Int
  createdAt  DateTime @default(now())
  @@unique([productId, supplierId])
}

model Category {
  id      Int      @id @default(autoincrement())
  name    String
  products CategoriesOnProduct[]
  createdAt DateTime   @default(now())
}

model CategoriesOnProduct {
  id Int @id @default(autoincrement())
  product Product? @relation(fields: [productId], references: [id])
  productId Int?
  category Category? @relation(fields: [categoryId], references: [id])
  categoryId Int?
  createdAt  DateTime @default(now())
}

Last updated