I want to query a post with id, slug, username, userId params. at least one of the param value are exists in the queries. Not required all of one.
const post = await prisma.post.findFirst({
where: {
OR: [
{
AND: [
{ published: true },
{
OR: [
{ id },
{
AND: [
{ slug },
{
author: {
profile: {
username
}
}
}
]
}
]
}
]
},
{
authorId: userId || undefined
}
]
},
...select
})
Database data (posts):
[{id: 1, published: false}, {id: 2, published: true}]
Query param is id: 1
but output is:
{id: 2, published: true}
Is there any wrong with my query?
Prisma Post model:
model Post {
id String @id @default(cuid())
title String
body String
slug String
published Boolean
draft Boolean @default(false)
author User @relation(fields: [authorId], references: [id])
authorId String
}
User model:
model User {
id String @id @default(cuid())
name String
email String @unique
password String?
posts Post[]
profile Profile?
}
Profile model:
model Profile {
id String @id @default(cuid())
bio String?
author User? @relation(fields: [authorId], references: [id])
authorId String?
phone String?
username String?
}