0
votes

I have following raw query that I need to shift into Sequelize ORM query

SELECT count(*) AS count FROM user_posts AS user_posts WHERE (user_posts.source_id = '621' AND user_posts.source_type = 'game' AND user_posts.user_id = 87 AND user_posts.post_type_id = 7 AND user_posts.archive = false) OR user_posts.local_db_path = 'xxx-xxx-xxx';

I have written following query in Sequelize ORM

let result = await Post.count({
      where:{
        local_db_path:local_db_path,
        [Op.or]:[
          {
            [Op.and]:[
              { source_id:inputs.source_id },
              { source_type:MAP_SOURCE_TYPE[inputs.source_type] },
              { user_id:user.id },
              { post_type_id:7 },
              { archive:false },
            ]
          }
        ]
      }      
    });

but this query add AND operation between local_db_path and braces condition but I need OR operator instead of AND.

1

1 Answers

2
votes

You have to move local_db_path to Op.or cases:

let result = await Post.count({
  where: {
    [Op.or]: [
      {
        [Op.and]: [
          { source_id: inputs.source_id },
          { source_type: MAP_SOURCE_TYPE[inputs.source_type] },
          { user_id: user.id },
          { post_type_id: 7 },
          { archive: false },
        ],
      },
      {
        local_db_path: 'xxx-xxx-xxx',
      },
    ],
  },
});