Is there a Hook after Incrementing/Decrementing an Instance?
See original GitHub issue“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?
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (6 by maintainers)
Top Results From Across the Web
Use React hook to implement a self-increment counter
This might looks fine since the state(counter) keeps incrementing, however it could freeze when combining with other hooks. What's the correct ...
Read more >Understanding hooks in react js - Medium
Let us write a simple counter example using hooks. Start by writing a functional component and three functions to increment, decrement and ...
Read more >Pattern usage for increment / decrement React (Hooks)
Action types are defined as an ENUM so when used in code the likelihood of typos is reduced since the string text isn't...
Read more >How to Increment and Decrement Number Using React Hooks
Lets see how to make increment decrement counter using React Hooks. We will be using useState() React Hook to achieve this.
Read more >Incrementing and Decrementing the State Variable on Button ...
Welcome, to ReactjJS Challenge in Hindi. We will see how to Incrementing and Decrementing the State Variable on Button clicked in React JS ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

It would great to have a hook to do some things after increment/decrement, for example send new value to socket
increment/decrementis built to avoid race conditions by doing an atomic update. Because of the atomic update it’s not possible to have any hooks.AFAIR
incrementdoes not return the new data, but it should be possible in postgres withRETURNING