sequelize / sequelize/sequelize
Is there a Hook after Incrementing/Decrementing an Instance?
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 30.4k
- Forks
- 4.3k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 68
Description
"beforeUpdate"-Hook is not triggered if the Model Instance is incremented/decremented. Is there another way?
What I'm trying to archive is:
- updating a model instance multiple times in a very short period of time (avoid race conditions)
- only a part of the update is possible with increment/decrement (which should avoid race conditions?)
- the other value to update depends on the newly updated data (how to do this without race conditions?)
Using the build method is not possible because I have to determine the new value from the newly updated values, which i not have without race conditions.
// not possible because I don't know the actual values without race conditions?
modelInstance.build({}, {isNewRecord = false}).save();
So the possible solution I thought is, a Hook in combination with a race condition free increment/decrement method!? Any other possible sulutions?
Thank you
EDIT:
I didn't realize that Increment returns a Promise and the new Data. So this solution should work without race conditions:
Counter.findById(...).then(counter =>
// this counter data is async and not necessary correct, right?
// but it the doesn't matter here, because we are only incrementing now!
counter.increment({
'count1': addValue1,
'count2': addValue2
}).then(counter => {
// this counter data should be good to go, right?
Counter.build({
id: counter.id,
greater: (counter.count1 >= counter.count2) ? 1 : 2
},{isNewRecord: false})
.save(['greater']);
});
Any comments?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the instance increment/decrement entry points and how the beforeUpdate hook is applied. Review the Promise result and the follow-up build/save example to determine the expected behavior under concurrent updates. Done should clearly specify whether a hook is needed and what race-condition-safe update behavior is supported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend, database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100