Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does sequelize have any change tracking built-in mechanism?

I was wondering whether sequelize implements (or plans to implement) any change tracking mechanism, which goal would be to avoid running unnecessary queries. For example:

var user = sequelize.User.find { where: { name: 'bob' } };

user.name = 'john';
user.save();

The sequelize will of course update the username. Now, imagine that the assignment is missing.

var user = sequelize.User.find { where: { name: 'bob' } };

user.save();

Is sequelize smart enough to figure it out that no database update is needed (and won't do one)? Is there any flag exposed by the sequelize object to see whether any changes were done to the model?

Or should I simply test by hand each property against the original model?

I have noticed an user._previousDataValues and user.options.isDirty properties on the sequelize model, but I am not sure what is their purpose and whether I should rely my code on those in any way.

like image 476
Overcommit Avatar asked Nov 01 '25 14:11

Overcommit


1 Answers

You can call user.changed() to get an array of keys that have been changed. However, I think that calling .save() will trigger the save regardless if there has been a change in the instance.

like image 161
Yuri Zarubin Avatar answered Nov 03 '25 04:11

Yuri Zarubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!