Help with conditional parameters in a .where () statement please

I've got URL searchParams for category, difficulty and not (which is a record to exclude). The parameters are optional and are used as filters in my question bank app.

I'm struggling to build a drizzle query that conditionally supports the presence of these parameters and I wonder if someone can help.

question = await db.select().from(Question)
.where(
  (category && (sql`${Question.category} = ${category}`)) && 
  (difficulty && (sql`${Question.difficulty} >= ${difficulty}`)) && 
  (notUid && (sql`${Question.uid} != ${notUid}`))
)
.orderBy(sql`RANDOM()`)
.limit(1)
.get()


This is what I have that doesn't work. Note thanks to @Mykhailo for the
RANDOM()
bit
Was this page helpful?