Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objection js update with increments +1

How i can update value in database using objection ? My SQL query works perfect.

UPDATE "freePlace"
SET number = number-1
WHERE   date >= '2017-10-20'   AND date <= '2017-10-30' AND "idObject" = '1'

My objection code:

FreePlace.query().patch({number:number+1}).where('date', '>=', startDate)
  .andWhere('date', '<=', endDate)
  .andWhere('idParking', '=', parkingId)

Problem is in patch({number:number+1}) how i need do this ?

like image 786
Piotr Avatar asked Dec 10 '22 09:12

Piotr


1 Answers

You can use the increment/decrement methods:

FreePlace.query()
  .increment('number', 1)
  .where('date', '>=', startDate)
  .andWhere('date', '<=', endDate)
  .andWhere('idParking', '=', parkingId)

If you need to update something else in the same query, you can use raw

FreePlace.query()
  .patch({
    number: raw('number + 1'),
    somethingElse: 42
  })
  .where('date', '>=', startDate)
  .andWhere('date', '<=', endDate)
  .andWhere('idParking', '=', parkingId)

raw comes from const { raw } = require('objection')

like image 151
Sami Koskimäki Avatar answered Dec 24 '22 14:12

Sami Koskimäki