4

just wanted to ask, how do i update a value in my mysql DB + 1 using bookshelf.js?

as example: Update table set value = value +1 where x = y ... ;

6
  • 1
    What have you tried so far? And what exactly was the problem? Commented Jun 23, 2018 at 18:00
  • 1
    im using this: await Wallet.forge(row).save({balance: balance + 1}, {patch:true}) i just dont know how to tell bookshelf to update it with +1. yeah i can retrieve the balance before that with a select query, but since its async it could happen that it selects the same value twice for the same user if he clicks twice before updating it. thats why i want it into one query. this doesnt work because balance is not defined, using 'balance + 1' as a string also doesnt work here Commented Jun 23, 2018 at 18:03
  • 1
    solved the problem: await table.query().where('id', id).decrement('value', value) Commented Jun 23, 2018 at 18:37
  • You can self-answer your question, so this advise is here for the next person looking for a way to do this. Commented Jun 23, 2018 at 18:38
  • .decrement() does the exact opposite of what you want. You should use .increment() instead. Commented Jun 24, 2018 at 14:36

1 Answer 1

4

solved the problem: await table.query().where('id', id).increment('value', value)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Also if you want to use it within transation open in the bookshelf way, just call transacting function after query : new Model().query().transacting(t).where('id', id).increment('value', value), and bookshelf will auto commit or rollback on Promise resolving or failing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.