Dev__
06/02/2021, 2:44 PMdraft:
typeof draft !== 'undefined'
? {
equals: draft // either true or false
}
: undefined
the value draft is either a true or a false. when the value is true it filters correctly and only shows me records with draft value set to true, but with false it shows me all records including the ones with true instead of only record with the draft value set to false . How could I fix thisDaniel Olavio Ferreira
06/02/2021, 2:46 PMDev__
06/02/2021, 2:49 PMmodel GenericOrder {
id String @id @db.VarChar(255)
....
draft Boolean @default(false) @db.Boolean
createdAt DateTime @default(now()) @db.Timestamptz
updatedAt DateTime @updatedAt @db.Timestamptz
}
the query:
this.prisma.genericOrder.findMany({
where: {
AND: [
{
draft:
typeof draft !== 'undefined'
? {
equals: draft
}
: undefined
},
{
createdAt:
typeof dateFrom !== 'undefined'
? {
gte: new Date(dateFrom)
}
: undefined
},
{
createdAt:
typeof newDateTo !== 'undefined'
? {
lte: newDateTo
}
: undefined
}
]
},
orderBy: orderByFilter,
take: Number(take),
skip: (Number(page) - 1) * Number(take)
})Daniel Olavio Ferreira
06/02/2021, 5:01 PMDaniel Olavio Ferreira
06/02/2021, 5:01 PMDev__
06/02/2021, 5:02 PMDev__
06/02/2021, 5:03 PMOmar Hamdan
06/02/2021, 8:45 PMOmar Hamdan
06/02/2021, 8:49 PMSamrith Shankar
06/02/2021, 9:55 PMdraft: !!draft
This will do two things:
• if draft is exists, pass true
• If draft is 0, null, undefined, ‘’, false: then pass false
But since for you type of draft is always Boolean or undefined, this simple line will doDev__
06/03/2021, 6:11 AMSamrith Shankar
06/03/2021, 6:12 AMdraft?Dev__
06/03/2021, 6:12 AMSamrith Shankar
06/03/2021, 6:12 AMDev__
06/03/2021, 6:18 AMdraft is optional right. it can come in as undefined. if I only do that you suggested, it sees the filter as false so only fetching records where draft is set to false. but if the filter is not on i need records where draft is both true and falseDev__
06/03/2021, 6:19 AMDev__
06/03/2021, 6:20 AMwhere: {
draft: {
equals: !!filters.draft
}
},
basically why I did the check for undefined was to skip the filter in prisma all together because its undefined and doesnt need to include it in the whereSamrith Shankar
06/03/2021, 6:22 AMfunction boolOrUndef(value) {
if(typeof value === "boolean") {
return value;
}
}
// Usage:
where: {
draft: boolOrUndef(filters.draft)
}Dev__
06/03/2021, 6:26 AMdraft is either a boolean or undefined, why does having a function for that work 🤔Samrith Shankar
06/03/2021, 6:31 AMSamrith Shankar
06/03/2021, 6:31 AMSamrith Shankar
06/03/2021, 6:34 AMconst condition = { where: {
AND: [
{
draft:
typeof draft !== 'undefined'
? {
equals: draft
}
: undefined
},
{
createdAt:
typeof dateFrom !== 'undefined'
? {
gte: new Date(dateFrom)
}
: undefined
},
{
createdAt:
typeof newDateTo !== 'undefined'
? {
lte: newDateTo
}
: undefined
}
]
},
}
And post the logged result of condition?Dev__
06/03/2021, 6:34 AMDev__
06/03/2021, 6:34 AMDev__
06/03/2021, 6:36 AMDev__
06/03/2021, 6:36 AMSamrith Shankar
06/03/2021, 6:37 AMwhere is?Samrith Shankar
06/03/2021, 6:37 AMDev__
06/03/2021, 6:43 AMhasObjectValues in the function I check if any of the filters has a value, if not then dont need to apply the where. but since draft can have a value of false it sees that as an incorrect value thus not accepting it...Dev__
06/03/2021, 6:43 AMSamrith Shankar
06/03/2021, 6:44 AM