Query polymorphic tables' related tables #1516
Unanswered
piscopancer
asked this question in
Q&A
Replies: 1 comment
-
I attach my temporary solution, please explain what I am doing wrong or right and how it can be improved or if there is a better way to do it export function getSharedNotificationsFindManyArgs(receiverId: number) {
return {
where: {
receiverId,
},
include: {
sender: {
select: {
id: true,
name: true,
surname: true,
middlename: true,
},
},
},
} satisfies Prisma.NotificationFindManyArgs
}
export async function queryNotifications(receiverId: number) {
const { where, include } = getSharedNotificationsFindManyArgs(receiverId)
const notifications = await db.$transaction(async (tx) => {
const course = await tx.addedToCourseNotification.findMany({
where,
include: {
...include,
course: true,
},
})
const group = await tx.addedToGroupNotification.findMany({
where,
include: {
...include,
group: true,
},
})
return [course, group]
})
return notifications.flat()
} The problem I'm encountering with this approach is that I cannot discriminate over |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Currently I got 2 models representing subtype of
Notification
and they both have different relations togroup
model andcourse
model accordinglyAddedToGroupNotification
has reference togroup
(and I need to query group too to show it's title in the notification),AddedToCourseNotification
has reference tocourse
(and I need to query course too to show it's title in the notification).This is how I query all notifications
And this is how I need to query every type of Notification separately, based on what they have
AddedToCourseNotification
AddedToGroupNotification
What's the best option I can do here? I really need to
include
sender
from theNotification
parent model (I already do it),course
(if type isAddedToCourseNotification
),group
(if type isAddedToGroupNotification
).This way I will be able to display the
title
of the course and many other fields ofcourse
model in my "general use" component belowBeta Was this translation helpful? Give feedback.
All reactions