ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • sequelize Op 연산자
    node.js 2023. 7. 16. 23:30

    Op 연산자

    where 객체 안에서 사용되는 Symbol 타입의 연산자이다.

    해당 연산자를 사용하여 복잡한 비교 연산을 지원해준다.

     

    일반연산자

    // Logical operator
    [Op.and]: [{ a: 5 }, { b: 6 }],  // (a = 5) && (b = 6) 그리고
    [Op.or]: [{ a: 5 }, { b: 6 }],  // (a = 5) || (b = 6) 또는

    // Basics
    [Op.eq]: 3,  // = 3
    [Op.ne]: 20,  // != 20 같지않음
    [Op.is]: null,  // IS NULL
    [Op.not]: true,  // IS NOT TRUE
    [Op.or]: [5, 6],  // (someAttribute = 5) OR (someAttribute = 6)

    // Using dialect specific column identifiers (PG in the following example):
    [Op.col]: 'user.organization_id',        // = "user"."organization_id"

    // Number comparisons
    [Op.gt]: 6,  // > 6 초과
    [Op.gte]: 6,  // >= 6 이상
    [Op.lt]: 10,  // < 10 미만
    [Op.lte]: 10,  // <= 10 이하
    [Op.between]: [6, 10],  // BETWEEN 6 AND 10
    [Op.notBetween]: [11, 15],  // NOT BETWEEN 11 AND 15

    // Other operators
    [Op.all]: sequelize.literal('SELECT 1'), // > ALL (SELECT 1)

    [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.iLike]: '%hat',  // ILIKE '%hat' (case insensitive) (PG only)
    [Op.notILike]: '%hat',  // NOT ILIKE '%hat'  (PG only)
    [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.iRegexp]: '^[h|a|t]',  // ~* '^[h|a|t]' (PG only)
    [Op.notIRegexp]: '^[h|a|t]',  // !~* '^[h|a|t]' (PG only)

    [Op.any]: [2, 3],  // ANY ARRAY[2, 3]::INTEGER (PG only)
    [Op.match]: Sequelize.fn('to_tsquery', 'fat & rat') // match text search for strings 'fat' and 'rat' (PG only)

    // In Postgres, Op.like/Op.iLike/Op.notLike can be combined to Op.any:
    [Op.like]: { [Op.any]: ['cat', 'hat'] }  // LIKE ANY ARRAY['cat', 'hat']

     

    범위연산자

    범위연산자 또한  지원하여 다양한 종류의 범위를 쿼리할 수 있다.

    [Op.contains]: 2,            // @> '2'::integer  (PG range contains element operator)
    [Op.contains]: [1, 2],       // @> [1, 2)        (PG range contains range operator)
    [Op.contained]: [1, 2],      // <@ [1, 2)        (PG range is contained by operator)
    [Op.overlap]: [1, 2],        // && [1, 2)        (PG range overlap (have points in common) operator)
    [Op.adjacent]: [1, 2],       // -|- [1, 2)       (PG range is adjacent to operator)
    [Op.strictLeft]: [1, 2],     // << [1, 2)        (PG range strictly left of operator)
    [Op.strictRight]: [1, 2],    // >> [1, 2)        (PG range strictly right of operator)
    [Op.noExtendRight]: [1, 2],  // &< [1, 2)        (PG range does not extend to the right of operator)
    [Op.noExtendLeft]: [1, 2],   // &> [1, 2)        (PG range does not extend to the left of operator)

     

    연산자 조합

    where 조건 객체에서 연산자들끼리의 중복 사용또한 가능하다.

    조건 객체에서 별 다른 연산자로 엮지않고 속성을 나열한다면 AND로 간주한다.

    연산자안에서 여러 컬럼을 다루는 경우에는 각 컬럼에 대한 조건을 담은 객체의 배열로 작성해야한다.

     

    'node.js' 카테고리의 다른 글

    환경변수 dotenv  (0) 2023.07.16
    객체 지향에 대해  (0) 2023.07.06
    Access Token 과 Refresh Token  (0) 2023.07.06
    Prettier 사용법  (0) 2023.07.04
    JWT 토큰 인증이란 ? (Cookie, Session, Token)  (0) 2023.06.21
Designed by Tistory.