Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: overlapping - Checking if any value in array matches any value in the passed array

Using sequelize to check if the the db input property, which is array, has a given item. Have a Postgres database with data Events. Want to get one Event that will have any of these weekDays. Type of weekDays is ARRAY(integer).

Events.findOne({
  where: {
    weekDays: {
      $contains: [2, 3],
    },
  },
});

Tried to do with $contains, $any, or $like $any but all the time got the same error message.

TypeError: values.map is not a function

Sincerely thanks

like image 480
L0cu2s Avatar asked Jan 18 '18 16:01

L0cu2s


1 Answers

You are looking for overlapping values. Any value in you weekDays array that matches any values in your passed in array.

As such, you can use the PG specific Sequelize.Op.overlap operator:

See documentation here

Can then be used like

Events.findOne({
  where: {
    weekDays: {
      [Sequelize.Op.overlap]: [2, 3],
    },
  },
});
like image 110
Michael McCabe Avatar answered Sep 21 '22 08:09

Michael McCabe