sequelize中在[Op.or]使用[Op.like]
使用情况:sequelize一个搜索值模糊查询表的两个列
'use strict';
const Service = require('egg').Service;
const Op = this.ctx.model.Sequelize.Op;
class UserService extends Service {
async list(username, page, limit){
let where = {};
if (username != '') {
where = {
[Op.or]: [{ nickname: { [Op.like]: '%' + username + '%' } },
{ username: { [Op.like]: '%' + username + '%' } },
],
};
}
return await this.ctx.model.user.findAndCountAll({
where,
offset: (parseInt(params.page) - 1) * parseInt(params.limit),
limit: parseInt(params.limit),
order: [[ 'last_login_time', 'DESC' ]],
include: [
{
model: this.ctx.model.UserInfo,
attributes: [[ 'user_level', 'user_level'],],
},
],
});
}
}
module.exports = UserService;