ivmarcos/sequelize-version

How to version updates?

Closed this issue ยท 5 comments

I defined my model and added Version:

const Book = sequelize.define('Book', {});
const BookVersion = new Version(Book);

When I create a book, a version row is correctly inserted. But when modify the entity using Book.update() it doesn't create a new version row.

The doc example uses instance attribute modification and persisting via save(). Since mentioned in #9 (comment), I assume that update() also works. Can you please specify how this can be implemented?

Hi @dialogik! It is necessary to use the individualHooks: true to trigger the sequelize hooks, which is required to make this lib work as intended. Be aware that this can cause a dramatic reduction in performance, as mentioned in https://sequelize.org/master/manual/hooks.html. I updated the readme today mentioning this: https://github.com/ivmarcos/sequelize-version#important-notes. Thanks!

Can I avoid performace reductions by using the .save() approach?

To clarify my question: I now changed my code so that it follows the .save() approach (with pre fetching it) instead of using the .update() way. I did not set individualHooks: true or whatsoever. But it is working as expected and documented. So is this way free of performance reductions compared to setting individualHooks: true and using .update()?

The performance reduction will occur according the number of records your query will affect when you use Model.update, because sequelize will select all those records and trigger the afterUpdate hook for each one. If only one record is affected, ex. Model.update({data: 1}, {where: {id: 1}, individualHooks: true}) no obviously reduction is expected. Plus, individualHooks: true must be informed only for class methods, like Model.update or Model.destroy, not for instance methods, like instance.save(). ๐Ÿ‘

Great to know. Thank you for your explanation.