Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to version updates? #17

Closed
dialogik opened this issue Jun 18, 2020 · 5 comments
Closed

How to version updates? #17

dialogik opened this issue Jun 18, 2020 · 5 comments

Comments

@dialogik
Copy link

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?

@ivmarcos
Copy link
Owner

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!

@dialogik
Copy link
Author

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

@dialogik
Copy link
Author

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()?

@ivmarcos
Copy link
Owner

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(). 👍

@dialogik
Copy link
Author

Great to know. Thank you for your explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants