beforeUpdate Hook, dirty not shows previous data

#1

Hello.

I’m trying to check dirty on beforeUpdate hook, but both old and new attributes are the same. Using dirty is the right way to check data changes inside a hook right?

Example:

Model App/Models/Discount

class Discount extends Model {
  static boot () {
    super.boot()
    this.addHook('beforeUpdate', 'DiscountHook.validatedDiscount')
  }
   ...
} 

Hook App/Models/Hook/DiscountHook

DiscountHook.validatedDiscount = async instance => {

  console.log(
    `Dirty Points: ${instance.dirty.pontos} - Updating Points: ${
      instance.pontos
    } --> PrevStatus: ${instance.dirty.aberta} / ${instance.aberta}`
  )

}

// Expected.: Dirty Points:  0 - Updating Points: 51 --> PrevStatus:  true / false
// Resultado: Dirty Points: 51 - Updating Points: 51 --> PrevStatus: false / false

Dirty is shown the updated data. Not the previous one!

dirty

0 Likes

#2

Mind sharing the code that performs the update

0 Likes

#3

Hello, sending the repo: [https://github.com/wemersonrv/adonis-hook-issue/blob/112f84b0be36dc770ef377d8981f410e7e122d6f/app/Models/Hooks/DiscountHook.js#L5)

The model data updating is on start/routes.js

0 Likes