duyluonglc / lucid-mongo

Mongodb ODM for adonis framework
325 stars 61 forks source link

beforeUpdate Hook not work #227

Open 01luisfonseca opened 5 years ago

01luisfonseca commented 5 years ago

I have the next model:

'use strict'

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class User extends Model {
  static boot () {
    super.boot()

    this.addHook('beforeCreate', 'App/Models/Hooks/UidCreatorHook.uid')
    this.addHook('beforeUpdate', 'App/Models/Hooks/UserPasswordHook.hash')
  }

  static get createdAtColumn () {
    return null
  }
  static get updatedAtColumn () {
    return null
  }
  static get incrementing () {
    return false
  }
  static get hidden () {
    return ['password', '_id']
  }
}

module.exports = User

And the hook:

'use strict'

const Hash = use('Hash')

const UserPasswordHook = exports = module.exports = {}

UserPasswordHook.hash = async (userInstance) => {
  if (userInstance.dirty.password) {
    userInstance.password = await Hash.make(userInstance.password)
  }
}

And the hook not works. The function isnt executed.

thanks for the help