Sequelize: How to retrieve updated record after sequelize update query without using another query

Viewed 4265
`    User.update({
  role: 'user'
}, {
  where: {
    email: email
  }
}).then(function(result) {
  //Returned value: either [0] or [1] i.e. record is updated or not
});
`

This is sequelize update query where User is model (user's role is updated by email). Its working fine but I want that using update query, I will get user's data as well. Is it possible to get it without using another query ?

2 Answers

If you are using postgresql then you can set returning true after where clause

where: { socketID: socket.id },
  returning: true,

and and if you are using other db like mysql as in my case then you cant to it in a single hit you have to use anther query like below

async function PersonalDetail(req, res) {
var Email = req.body.email;


    const user = await User.findOne({ where: { EmailAddress: Email, IsActive: true } });

    if (user != null) {
        user.UpdateDate = new Date();
        user.EmplomentID = req.body.EmplomentID;
        user.LivingID = req.body.LivingID;
        user.LocationID = req.body.LocationID;

        await user.save();

        res.json({
            IsSuccess: true,
            message: 'User updated',
            User: user
        });
    }
    else {
        res.json({
            IsSuccess: true,
            message: 'User Not updated',
            User: ""
        });
    }


}
Related