Get union type of type
field (@@delegate) from table with Polymorphic Relations
#1515
Answered
by
jiashengguo
piscopancer
asked this question in
Q&A
-
I wonder how I can conveniently extract and use this union type in my app I thought I can get it like so import { type Notification } from '@prisma/client'
type NotificationType = Notification['type'] but it's not the case because Schema for my model Notification {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
type String
senderId Int
sender User @relation("user-notification-sent", fields: [senderId], references: [id], onDelete: Cascade)
receiverId Int
receiver User @relation("user-notification-received", fields: [receiverId], references: [id], onDelete: Cascade)
@@delegate (type)
}
model AddedToGroupNotification extends Notification {
groupId Int
group Group @relation(fields: [groupId], references: [id], onDelete: Cascade)
}
model AddedToCourseNotification extends Notification {
courseId Int
course Course @relation(fields: [courseId], references: [id], onDelete: Cascade)
} What I want in the end is type NotificationType = 'AddedToGroupNotification' | 'AddedToCourseNotification' // not written by hand xd |
Beta Was this translation helpful? Give feedback.
Answered by
jiashengguo
Jun 25, 2024
Replies: 1 comment
-
@piscopancer, firslty sorry for the late response, we didn't check the discussion that often. What you want should be achieved by this: import type { Notification } from "@zenstackhq/runtime/prisma";
type NotificationType = Notification['type'] There is a post for a complete example of Polymorphic with more details: |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
piscopancer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@piscopancer, firslty sorry for the late response, we didn't check the discussion that often. What you want should be achieved by this:
There is a post for a complete example of Polymorphic with more details:
https://zenstack.dev/blog/ocp