Skip to content
Advertisement

How to perform a search with conditional where parameters using Sequelize

Usually whenever I write a search query for SQL, I do something similar to this:

Basically this simulates a conditional WHERE clause. We only want to compare @searchParam to the column if @searchParam was provided.

Is there a way to replicate this using Sequelize?

EDIT: Here is my best attempt which fails:

UPDATE: I found a way to do it, but it feels like a workaround. I’m certain there has to be a more elegant way. This works, but is ugly:

Advertisement

Answer

You can just prepare object with needed conditions. Simple and easy to understand

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement