hello, i have a column in postgres with boolean va...
# orm-help
d
hello, i have a column in postgres with boolean values. now when I apply a boolean filter with prisma on that column it doesnt work as intended
Copy code
draft:
	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 this
d
Hey, could you provide more info about your model and the query that you are trying to perform?
d
The model:
Copy code
model 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:
Copy code
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)
})
d
My question is, do you need to check if draft was passed inside of the query? Just brainstorming here
Cause I'm not sure if that is a good practice
d
Wdym? Do you mean the check for if undefined or not?
Its an optional filter but that doesn't change the fact that it doesn't work when draft is false?
o
I think this is because you’re strictly checking for the type of undefined and I’m not sure if in JavaScript false has type undefined. so the first filter Andra is never getting applied and only the last two might be. So you’re getting everything as a result
I also second making the logic simpler if at all possible. How about this? Draft? <prisma spam> ? undefined
s
You can simply do:
Copy code
draft: !!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 do
d
@Samrith Shankar hi thanks for the reply, I already tried that one before but it doesnt work unfortunatly
s
What error does it give? Coz what you’re doing seems extremely unnecessary. What’s the value you get for
draft?
d
oh wait, you meant skipping to check for undefined, will trry that
s
Yeah!
d
oke, so the thing is,
draft
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
false
@Samrith Shankar
Copy code
where: {
	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
where
s
Gotcha. In that case, could you try using a function like so?
Copy code
function boolOrUndef(value) {
    if(typeof value === "boolean") {
        return value;
    }
}

// Usage:
where: { 
  draft: boolOrUndef(filters.draft)
}
d
cool, that works thanks! but isnt it the same as not using a function? I mean,
draft
is either a
boolean
or
undefined
, why does having a function for that work 🤔
s
To be honest, I can't really answer your question without having a look at your entire code (prisma.model.findMany....)
Actually, I found your code snippet. Checking to see if I can make sense of this 😄
Also, could you try doing this:
Copy code
const 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
?
d
wait, i'll send it in a pastebin, if I implement it in my whole query it doesnt work. probably something off with my query then
the draft doesnt work with the above query in the pastebin
everything else works fine
s
Can you also log what the generated
where
is?
And paste it here?
d
oke, this was my fault! it works as expected but I did something stupid haha. here
hasObjectValues
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...
thanks for the input, helped me figure it out! 😄
s
I thought so 😄 Sometimes it is just the little things. I'm glad your issue got sorted!