Hooks Not working


#1

My Model

'use strict'

const Model = use('Model')

class Service extends Model {
  static get table() {
    return 'services'
  }

  static boot() {
    super.boot()
    this.addHook('afterCreate', async (serviceIstance) => {
      console.log(serviceIstance)//It never runs
    })
  }
}

module.exports = Service

My Controller

let service = await Database
                .insert(dataToSave)
                .into('services')

I am using postgres as a database.
And records are effectively created in the database, but the hooks are not executed.

Do you have to do some additional configuration to make them work?

I appreciate your help


#2

Hey @antojsh! :wave:

Hooks are defined in the model scope, in your code you are using the Database Service that don’t use the Model.

const service = await Service.create(dataToSave)