18

I am using Sequelize along with PostgreSQL in managing my database.
I would like to perform a case insensitive search query. When I googled it up, some people said that I can use "iLike" operator to do so. I tried to implement this way:

var getRadiosByGenre = function(Radio,Genre,genreName){
    Genre.findOne({where:{name: { $iLike: genreName}}})}

where genreName is a string. But, I keep getting this error:

Error: Invalid value { '$iLike': 'art' }

Does anyone know the correct way of using iLike with sequelize? Thanks mates(s). :)

AG_HIHI
  • 1,705
  • 5
  • 27
  • 69

1 Answers1

34

You should use Sequelize.Op :

var getRadiosByGenre = function(Radio,Genre,genreName) {
  Genre.findOne({
    where: {
      name: {
        [Sequelize.Op.iLike]: genreName
      }
    }
  });
}

Don't forget to add % before or after your genreName if you want to make a partial query.

See the docs here >

Brad
  • 159,648
  • 54
  • 349
  • 530
PhilippeAuriach
  • 2,418
  • 2
  • 22
  • 41
  • 1
    Thank you, for the reference but this is how to implement it exactly: var getRadiosByGenre = function(Radio,Genre,genreName){ Genre.findOne({where :{name:{[ Op.iLike ]:'%'+genreName}}}) – AG_HIHI Jul 23 '18 at 09:53
  • 2
    Yes as I stated you should add the % sign if needed ! glad I could help – PhilippeAuriach Jul 23 '18 at 10:29