I have two models: Post model and a PostItem model. A Post can have multiple PostItems.
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(300)
postItemNoteStatus String @db.Text
postItems PostItem[]
}
model PostItem {
id Int @id @default(autoincrement())
postId Int
url String? @db.VarChar(1000)
note String? @db.VarChar(300)
post Post @relation(fields: [postId], references: [id])
}
I want to find a Post and return all of its PostItems, but only include the note field in PostItem if postItemNoteStatus is visible. I would like to do this in one query. At the moment, this is what I have:
db.post.findFirst({
where: { id},
include: { postItems: true }
})
if(post.postItemNoteStatus === 'hidden') {
post.postItems.forEach((pi) => {
delete pi.note;
});
}
This works, but I have to manually delete notes if the postItemNoteStatus is hidden. I would like to be able to do this elegantly in a query. Is this possible given the models and relation I have?