Objection.js: Question: checking if attribute has been changed

Created on 7 Feb 2019  路  3Comments  路  Source: Vincit/objection.js

Is there a way to know what attributes have been changed?

I'm considering an API like:

const model = await Model.query().findById(id)
model.name = 'New name'
console.log(model.nameHasChanged) # => true
console.log(model.idHasChanged) # => false

or something like:

const model = await Model.query().findById(id)
model.name = 'New name'
console.log(model.name === model.persistedName) # => false

Most helpful comment

For a crappy and dangerous implementation, you could override $parseDatabaseJson method and save the object returned by super.$parseDatabaseJson to a property like $origAttrs. Properties that start with $ are excluded from all normal processing.

All 3 comments

There's no way to do that. There's no plausible way to implement that without extra queries and locks since the value can change at any given moment.

For a crappy and dangerous implementation, you could override $parseDatabaseJson method and save the object returned by super.$parseDatabaseJson to a property like $origAttrs. Properties that start with $ are excluded from all normal processing.

Indeed I was more interested in tracking dirty fields, like in ActiveRecord or Mongoose.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

koskimas picture koskimas  路  21Comments

jtlapp picture jtlapp  路  23Comments

linkenneth picture linkenneth  路  18Comments

ackerdev picture ackerdev  路  56Comments

koskimas picture koskimas  路  26Comments