Hi all. I have a schema modelling question. In my ...
# orm-help
m
Hi all. I have a schema modelling question. In my order management system an order can be in three different states;
new
,
packing
,
shipped
. Each different state has additional fields tracking when the state was entered and other information. I'd like to create the schema so that I can query across all the different states. This is what I have so far:
Copy code
generator client {
  provider = "prisma-client-js"
}

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

type LineItem {
  productId String
  name      String
  quantity  Int
}

enum Status {
  NEW
  PACKING
  SHIPPED
}

model NewOrder {
  id        String     @id @default(auto()) @map("_id") @db.ObjectId
  status    Status     @default(NEW)  
  lineItems LineItem[]
  createdAt DateTime   @default(now())
}

model PackingOrder {
  id        String     @id @default(auto()) @map("_id") @db.ObjectId
  status    Status     @default(PACKING)
  lineItems LineItem[]
  createdAt DateTime   
  packingStartedAt DateTime @default(now())
}

model ShippedOrder {
  id        String     @id @default(auto()) @map("_id") @db.ObjectId
  status    Status     @default(SHIPPED)
  lineItems LineItem[]
  createdAt DateTime   
  packingStartedAt DateTime 
  shippedAt DateTime @default(now())
}
Is there it possible to achieve this or am I barking up the wrong tree? Thanks!
n
Hey @Max πŸ‘‹ Welcome to our Slack community! prisma lightblue Is the status value going to change in
NewOrder
,
PackingOrder
and
ShippedOrder
models? Basically, can a document in NewOrder have the status of Packing or Shipped?
m
Hi @Nurul! Thanks for your response. πŸ™ In the schema that I've suggested the status would always be the same in each model. Ie,
NewOrder
would always have a status of
NEW
. I added the status so that on the client I can combine results from all three models and can differentiate them by the discriminated union with
status
as the discriminator. Ideally I'd like to have all three models combined into one model for easy querying but as I understand it Prisma doesn't currently support inheritance or union types, right?
n
Yes, that’s correct. We do have a Feature Request for Union types and Inheritance but they are not natively supported yet.
It would be helpful if you could leave a πŸ‘ to the feature requests, so that our product team can prioritise it.
m
@Nurul Done. simple smile I've added a πŸ‘ to both issues.
πŸ™Œ 1
In the mean time, I've read through those issues but am still unsure how I would model an order that can be in different states with each state having different data. Ie, union type. Any ideas how best to approach this in the mean time @Nurul? Thanks! πŸ™
v
πŸ‘‹ Hey @max - this is just to let us know if we haven't forgotten about your last question. We're just experiencing a large number of questions and @Nurul will get to it asap. Thank you for your patience! πŸ™ 😊