I am looking for a way to update only a nested key/value inside jsonb column.
here is the User model:
const User= sequelize.define('User', {
id: DataTypes.INTEGER,
name:DataTypes.STRING,
// other fields
data: DataTypes.JSONB
});
current data column value:
{
'email':{'verified':false,'token':'random token'},
'phone:{'verified':true}
}
I want to update email.verified property to true using a single update query.
I tried following codes:
models.User.update({'data.email.verified':true},
{where:{id:1}}).then()...
models.User.update({data:{email:{verified:true}}},
{where:{id:1}}).then()...
the updated column would be:
{
'email':{'verified':true},
}
I also tried this:
models.User.findById(1).then(user => {
user.set('data.email.verified', true);
user.save().then()...
});
this works but i don't want to execute two query (select and then update) for only updating a field.
Is it possible to do this using sequelize with a single update query?
model.yourJsonB.userName
model.yourJsonB.userName = "Lisa" // traditional prop change
model.changed("yourJsonB", true) // << forces sequelize to understand this json has been updated
await model.save()
I found this here: https://sequelize.org/master/manual/upgrade-to-v6.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With