Sequelize JSON field update only one field

Viewed 1250

I have a JSON field like this. {Price:0,Title:'Test'}. I want to update Price value with 10 on MYSQL with sequelize. I am using MYSQL 8 and JSON field supported. But in sequelize want to change Only Price with sequelize directly write to the field with {Price:10} and another fields are deleting. How can I solve this?

(async () => {
  await e_products_v2.update(
    {
      Preferences: {
        Price: 0,
      },
    },
    { where: { id: 1 } }
  );
})();
2 Answers
let p = await db.e_products_v2.findOne({ where: { id: 20 } })
        
p.Preferences = { ...p.Preferences, Price: 10 }
await p.update({ Preferences: p.Preferences })

here am updating the branch_details inside json column bank_details which persist the other details inside the branch_details json column.

Hope this will be useful for those who are using nested JSON.

const record = await Test.findOne({ where: { id: 1 } });
record.bank_details = [{
  ...record.bank_details[0],
  branch_details: { name: 'Branch Name', ifsc: 'UTI00001111' }
}];
record.changed('bank_details', true);
await record.save();
Related