2

I would like to use this type of request with sequelize to make large number of updates in one request (for performance reasons) :

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

Here is the value of updateStack :

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

I'm not sure how sequelize can properly parse the updateStack array. Any idea ?

This SQL query is working fine :

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

Thank you and have a good day.

1

1 Answer 1

6

I've discovered how to do it !

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
            }
    )
Sign up to request clarification or add additional context in comments.

1 Comment

which DB are you using?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.