Edit Page

Lifecycle callbacks

Overview

#

Lifecycle callbacks are functions that are called before or after certain model methods. For example, you might use lifecycle callbacks to automatically compute the value of a fullName attribute before creating or updating a User record.

Sails exposes a handful of lifecycle callbacks by default:

Lifecycle callbacks on .create()
#

The afterCreate lifecycle callback will only be run on queries that have the fetch meta flag set to true. For more information on using the meta flags, see Waterline Queries.

beforeCreate is also run on bulk inserts of data when you call .createEach(). However, afterCreate is not.

Lifecycle callbacks on .update()
#

The afterUpdate lifecycle callback will only be run on .update() queries that have the fetch meta flag set to true. For more information on using the meta flags, see Waterline Queries.

Lifecycle callbacks on .destroy()
#

The afterDestroy lifecycle callback will only be run on .destroy() queries that have the fetch meta flag set to true. For more information on using the meta flags, see Waterline Queries.

Example

#

If you want to hash a password before saving in the database, you might use the beforeCreate lifecycle callback.

// User.js
module.exports = {

  attributes: {

    username: {
      type: 'string',
      required: true
    },

    password: {
      type: 'string',
      minLength: 6,
      required: true
    }

  },


  beforeCreate: function (valuesToSet, proceed) {
    // Hash password
    sails.helpers.passwords.hashPassword(valuesToSet.password).exec((err, hashedPassword)=>{
      if (err) { return proceed(err); }
      valuesToSet.password = hashedPassword;
      return proceed();
    });//_∏_
  }

};

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Concepts