简体   繁体   中英

Sequelize Case Sensitive Query -- Using BINARY

this is my query in mysql

i want to use this query in sequelize orm

 SELECT
        `uid`,
        `username`
    FROM
        `users` AS `users`
    WHERE
        BINARY 
        `users`.`username` IN ('hammad', 'sAad')
    AND `users`.`status` = 'ACTIVE'
    ORDER BY
        FIELD(
            `username`,
            'hammad',
            'saad.ahmed'
        )
    LIMIT 20;

I want sequelize query

this is the solution for finding case sensitive where condition

db.models.users.findAll({

            attributes: ['uid', 'username'],
            where: {
                username: db.sequelize.where(
                    db.sequelize.literal('BINARY username IN ('),
                    `'${users.join("', '")}'`,
                    db.sequelize.literal(')'),
                ),
                status: 'ACTIVE'
            },
            order: !_.isEmpty(users) ? [[db.sequelize.fn('FIELD', db.sequelize.col('username'), ...users)]] : [],
            limit: 20
        });
UserModel.findAll({
  attributes: ['uid', 'username'],
  where : {
     username : {
          [Op.in]: [ 'hammad' , 'sAad']
        },
     status : 'active'
     },
  order : ['username']
});

[Op.and]: where(fn('binary', col('username')), { [Op.in]: ['xxx','xxx2'] })

if you using sequelize.literal<\/code> , it can lead to SQL Injection.

const query = {
  where: {
    username: sequelize.where(
      sequelize.fn(
        'BINARY',
        sequelize.col('username')
      ),
      username
    )
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM