在Sequelize中使用原始批量更新查询

时间:2019-06-28 09:44:59

标签: node.js sequelize.js

我想将这种类型的请求与sequ​​elize一起使用,以在一个请求中进行大量更新(出于性能原因):

UPDATE employee
SET address = new.address,
name = new.name
from (values :updateStack) AS new(address, name, employeeId)
WHERE employee.id = new.employeeId

这里是updateStack的值:

[{
    address: 'France',
    name: 'Chris',
    employeeId: 21
}, {
    address: 'UK',
    name: 'Steve',
    employeeId: 42
}]

我不确定续集如何正确解析updateStack数组。 有什么想法吗?

此SQL查询工作正常:

UPDATE employee
SET address = new.address,
name = new.name
from (values ('France', 'Chris', 21), ('UK', 'Steve', 42)) AS new(address, name, employeeId)
WHERE employee.id = new.employeeId

谢谢,祝你有美好的一天。

1 个答案:

答案 0 :(得分:0)

我发现了如何做!

sequelize.query(
            `
                UPDATE employee
                SET address = new.address,
                name = new.name
                from (values ?) AS new(address, name, employeeId)
                WHERE employee.id = new.employeeId
            `,
            {
                replacements: [['France', 'Chris', 21], ['UK', 'Steve', 42]],
                type: models.models.sequelize.QueryTypes.INSERT
            }
    )