Sequelize 复杂过滤器 | Node.js

Sequelize Complex Filter | Node.js

我想过滤,但这取决于用户,例如

Data.findAll({
    where: {
        name: {
            [Op.or]: [
                { [Op.like]: ['%samsung%'] },
                { [Op.like]: ['%iphone%'] },
                { [Op.like]: ['%alcatel%']}
            ]
        }
    }
}

如果用户只选择 Samsung,我该如何进行仅按 Samsung 过滤?

假设 req.query.brands 存储单个搜索字符串或字符串数​​组,我们可以动态构建 Op.or 条件:

const brands = [].concat(req.query.brands)
const brandConditions = brands.map(x => ({
   [Op.like]: `%${x}%`
})
const foundItems = await Data.findAll({
    where: {
        name: {
            [Op.or]: brandConditions
        }
    }
}