Node.js/Sequelize

[ORM] ๐Ÿ“š ์‹œํ€„๋ผ์ด์ฆˆ - where์ ˆ์— ๋ฐฐ์—ด ์‚ฌ์šฉ

์ธํŒŒ_ 2022. 7. 17. 10:07

Sequelize where in array

Sequelize where in array

์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ (1,2,3,4)๋ฅผ where์ ˆ์—์„œ ๋‹ค๋ฃฐ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ, sql๋ฌธ์€ ๋ณดํ†ต in ๋ฌธ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค.

select * 
from posts
where id in (1,2,3,4) -- 1 or 2 or 3 or 4

 

์‹œํ€„๋ผ์ด์ฆˆ์—์„œ๋„ sql์˜ in๋ฌธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด Op ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ๊ณต์‹ ์ง€์›ํ•œ๋‹ค.

const { Op } = require("sequelize");
const data = [1,2,3,4];
Post.findAll({
  where: {
        id: {[Op.in]: data}
        // not์ผ ๊ฒฝ์šฐ id: {[Op.notin]: [1,2,3,4]}
    }
});

 

ํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง๊ด€์ ์œผ๋กœ ์งง๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค.

const { Op } = require("sequelize");
const data = [1,2,3,4];
Post.findAll({
  where: {
        id: data
    }
});