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 ?
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With