Objection.js: implement a save()

Created on 19 Dec 2018  路  2Comments  路  Source: Vincit/objection.js

I am really digging this ORM. As I understand it, columns of an sql database are returned as object properties in javascript. I have been able to query and then modify an object after it is retrieved. like so
sensors = await Sensor.query()
.where("monitorId", 245)
if (sensor.length > 0){
mySensor = sensors[0];
mySensor.monitorID = 7874;
}
Now I have a sensor with a monitorID of 7874
is there a simple method to save this object back without using patch() ?

I ask because I write code for a data processing lab where this property may change a few times, and I dont want to have to track several objects and changes over the course of the process nor save the record every time a property is changed.

is this possible?

Most helpful comment

Yes,

you can say:

await mySensor.$query().patch()

You don't need to keep track of stuff even with the static patch method because you can always do this:

await Sensor.query().patch(mySensor).where('id', mySensor.id)

There's also the findById shortcut by the way.

All 2 comments

Yes,

you can say:

await mySensor.$query().patch()

You don't need to keep track of stuff even with the static patch method because you can always do this:

await Sensor.query().patch(mySensor).where('id', mySensor.id)

There's also the findById shortcut by the way.

This rocks, thanks !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ahlid picture Ahlid  路  3Comments

nicolaracco picture nicolaracco  路  3Comments

apronin83 picture apronin83  路  3Comments

njleonzhang picture njleonzhang  路  4Comments

louis-etne picture louis-etne  路  4Comments