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

officer-rosmarino picture officer-rosmarino  路  4Comments

falkenhawk picture falkenhawk  路  4Comments

mycahjay-nms picture mycahjay-nms  路  4Comments

purepear picture purepear  路  3Comments

bsdo64 picture bsdo64  路  3Comments