Model hooks are deprecated, except for afterInitialize.
Page Contents

Overview

Use model hooks to add custom logic to models that extend PersistedModel. Each hook is called before or after a specific event in the model’s lifecycle.

You can define the following model hooks, listed in the order that the events occur in a model lifecycle:

  • afterInitialize - triggers after a model has been initialized.
  • beforeValidate - triggers before validation is performed on a model. 
  • afterValidate - triggers after validation is performed on a model.
  • beforeSave - triggers before a model is saved to a data source.
  • afterSave - triggers after a model is saved to a data source.
  • beforeCreate - triggers before a model is created.
  • afterCreate - triggers after a model is created.
  • beforeUpdate - triggers before a model is updated.
  • afterUpdate - triggers after a model is updated.
  • beforeDestroy - triggers before a model is destroyed.
  • afterDestroy - triggers after a model is destroyed.

Best practice is to register model hooks in /common/models/your-model.js. This ensures hooks are registered during application initialization. If you need to register a hook at runtime, get a reference to the app object and register it right then and there.

afterInitialize

This hook is called after a model is initialized.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.afterInitialize = function() {
  //your logic goes here
};
//...

Most operations require initializing a model before actually performing an action, but there are a few cases where the initialize event is not triggered, such as HTTP requests to the exists, count, or bulk update REST endpoints.

beforeValidate

This hook is called before validatation is performed on a model.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.beforeValidate = function(next, modelInstance) {
  //your logic goes here - don't use modelInstance
  next();
};
//...

You must call next() to let LoopBack know you’re ready to go on after the hook’s logic has completed.

afterValidate

This hook is called after validation is performed on a model.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.afterValidate(next) {
  //your logic goes here
  next();
};
//...

You must call next() to let LoopBack know you’re ready to go on after the hook’s logic has completed.

beforeCreate

This hook is called just before a model is created.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.beforeCreate = function(next, modelInstance) {
  //your logic goes here
  next();
};
//...

LoopBack provides modelInstance as a reference to the model being created.  

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

afterCreate

This hook is called after a model is created.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.afterCreate = function(next) {
  //your logic goes here
  this.name = 'New coffee shop name; //you can access the created model via `this`
  next();
};
//...

Access the model being created with this.

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

beforeSave

This hook is called just before a model instance is saved.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.beforeSave = function(next, modelInstance) {
  //your logic goes here
  next();
};
//...

LoopBack provides modelInstance as a reference to the model being saved.

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

afterSave

This hook is called after a model is saved.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.afterSave = function(next) {
  //your logic goes here
  this.name = 'New coffee shop name; //you can access the created model via `this`
  next();
};
//...

Access the model being saved with this.

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

beforeUpdate

This hook is called just before a model is updated.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.beforeUpdate = function(next, modelInstance) {
  //your logic goes here
  next();
};
//...

LoopBack provides modelInstance as a reference to the model being updated.

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

afterUpdate

This hook is called after a model is updated.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.afterUpdate = function(next) {
  //your logic goes here
  this.name = 'New coffee shop name'; //you can access the created model via `this`
  next();
};
//...

LoopBack provides modelInstance as a reference to the model being saved.

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

beforeDestroy

This hook is called just before a model is destroyed.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.beforeDestroy = function(next, modelInstance) {
  //your logic goes here
  next();
};
//...

LoopBack provides modelInstance as a reference to the model being saved.

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.

afterDestroy

This hook is called after a model is destroyed.

Example

/common/models/coffee-shop.js

//...
CoffeeShop.afterDestroy = function(next) {
  //your logic goes here
  next();
};
//...

You must call next() to continue execution after the hook completes its logic. If you don’t the application will appear to hang.