Creating a Prisma schema for a digital product e-commerce platform

I’m developing an e-commerce site for digital goods using Prisma ORM with MongoDB. I’ve set up my schema but I’m not sure about some relationships. Here’s what I’ve got so far:

model Customer {
  id String @id @default(auto()) @map("_id") @db.ObjectId
  name String
  email String @unique
  pass String
  basket Basket?
  purchases Purchase[]
  logins Login[]
}

model Basket {
  id String @id @default(auto()) @map("_id") @db.ObjectId
  items BasketItem[]
  customerId String @unique @db.ObjectId
  customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
}

model BasketItem {
  id String @id @default(auto()) @map("_id") @db.ObjectId
  productId String @db.ObjectId
  amount Int
  basketId String @db.ObjectId
  basket Basket @relation(fields: [basketId], references: [id], onDelete: Cascade)
}

model Purchase {
  id String @id @default(auto()) @map("_id") @db.ObjectId
  state PurchaseState
  cost Float
  items PurchaseItem[]
  customerId String @db.ObjectId
  customer Customer @relation(fields: [customerId], references: [id], onDelete: Cascade)
}

enum PurchaseState {
  WAITING
  VOIDED
  FINISHED
}

model PurchaseItem {
  id String @id @default(auto()) @map("_id") @db.ObjectId
  productId String @db.ObjectId
  amount Int
  purchaseId String @db.ObjectId
  purchase Purchase @relation(fields: [purchaseId], references: [id], onDelete: Cascade)
}

model DigitalProduct {
  id String @id @default(auto()) @map("_id") @db.ObjectId
  title String
  link String
  info String
  cost Float
}

Do I need to link BasketItem and PurchaseItem directly to DigitalProduct? Any tips to improve this setup? Thanks!

Hey there, fellow e-commerce enthusiast! :shopping_cart::sparkles:

Your schema’s looking pretty good, but I can’t help wondering about a few things. Have you thought about how you’ll handle product updates? Like, what if the price of a digital product changes after it’s in someone’s basket?

Also, I’m curious about the user experience. How are you planning to display product info in the basket and purchase history? It might be handy to have some basic product details right there in the BasketItem and PurchaseItem models.

Oh, and here’s a random thought - what about user reviews? Could be cool to let customers rate the digital products they’ve bought. Maybe a Reviews model linked to both Customer and DigitalProduct?

What do you think? Any plans for features like these? I’d love to hear more about your vision for the site!

Your schema looks solid overall, but there are a few tweaks I’d suggest to improve it. First, consider linking BasketItem and PurchaseItem directly to DigitalProduct by adding relation fields. This integration would simplify queries and maintain data integrity. Additionally, you could add a price field to these models to capture the cost at the time of purchase, accommodating future price changes. Implementing createdAt and updatedAt timestamps across models will also help track changes, and including categories or tags in DigitalProduct enhances product organization and filtering.

yo, ur schema looks ok! link basketitem and purchaseitem directly to digitalproduct, makes queryin simpler n data tighter. add a price field too, so it holds cost at purchase time. good luck with ur proj!