시퀄라이즈 Op

M·2023년 7월 10일
0

TIL

목록 보기
27/42

Op객체

Op.gt 초과
Op.gte 이상
Op.lt 미만
Op.lte 이하
Op.ne 같지 않음
Op.or 또는
Op.in 배열 요소 중 하나
Op.notIn 배열 요소와 모두 다름

const Op = Sequelize.Op

[Op.and]: [{a: 5}, {b: 6}] // (a = 5) AND (b = 6)
[Op.or]: [{a: 5}, {a: 6}]  // (a = 5 OR a = 6)

[Op.gt]: 6,                // > 6
[Op.gte]: 6,               // >= 6

[Op.lt]: 10,               // < 10
[Op.lte]: 10,              // <= 10

[Op.ne]: 20,               // != 20
[Op.eq]: 3,                // = 3

[Op.is]: null              // IS NULL
[Op.not]: true,            // IS NOT TRUE

[Op.between]: [6, 10],     // BETWEEN 6 AND 10
[Op.notBetween]: [11, 15], // NOT BETWEEN 11 AND 15

[Op.in]: [1, 2],           // IN [1, 2]
[Op.notIn]: [1, 2],        // NOT IN [1, 2]

[Op.like]: '%hat',         // LIKE '%hat'
[Op.notLike]: '%hat'       // NOT LIKE '%hat'
[Op.startsWith]: 'hat'     // LIKE 'hat%'
[Op.endsWith]: 'hat'       // LIKE '%hat'
[Op.substring]: 'hat'      // LIKE '%hat%'

[Op.regexp]: '^[h|a|t]'    // REGEXP/~ '^[h|a|t]' (MySQL/PG only)
[Op.notRegexp]: '^[h|a|t]' // NOT REGEXP/!~ '^[h|a|t]' (MySQL/PG only)

[Op.like]: { // LIKE ANY ARRAY['cat', 'hat'] - also works for iLike and notLike
	[Op.any]: ['cat', 'hat']
} 

[Op.gt]: { // > ALL (SELECT 1)
	[Op.all]: literal('SELECT 1') 
}

Op.or

const user = User.findAll({
    attributes: ['name', 'age'],
    where: { 
        [Op.or]: [ // married = 0 or age > 30
            { married: false },
            { age: { [Op.gt]: 30 } }
        ],
    },
});

console.log(user.comment)
SELECT name, age FROM users WHERE married = 1 OR age > 30;

위와 같은 연산자들을 사용해서 복잡한 비교 연산을 쉽게 해낼 수 있다.

profile
자바스크립트부터 공부하는 사람

0개의 댓글