question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Stratecommented, Jul 23, 2018

It would great to have a hook to do some things after increment/decrement, for example send new value to socket

1reaction
mickhansencommented, Sep 12, 2015

increment/decrement is built to avoid race conditions by doing an atomic update. Because of the atomic update it’s not possible to have any hooks.

AFAIR increment does not return the new data, but it should be possible in postgres with RETURNING

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found