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
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.
Most helpful comment
For a crappy and dangerous implementation, you could override
$parseDatabaseJsonmethod and save the object returned bysuper.$parseDatabaseJsonto a property like$origAttrs. Properties that start with$are excluded from all normal processing.