balderdashy/waterline

View on GitHub
lib/waterline/utils/system/lifecycle-callback-builder.js

Summary

Maintainability
A
0 mins
Test Coverage
//  ██████╗ ██╗   ██╗██╗██╗     ██████╗
//  ██╔══██╗██║   ██║██║██║     ██╔══██╗
//  ██████╔╝██║   ██║██║██║     ██║  ██║
//  ██╔══██╗██║   ██║██║██║     ██║  ██║
//  ██████╔╝╚██████╔╝██║███████╗██████╔╝
//  ╚═════╝  ╚═════╝ ╚═╝╚══════╝╚═════╝
//
//  ██╗     ██╗███████╗███████╗ ██████╗██╗   ██╗ ██████╗██╗     ███████╗
//  ██║     ██║██╔════╝██╔════╝██╔════╝╚██╗ ██╔╝██╔════╝██║     ██╔════╝
//  ██║     ██║█████╗  █████╗  ██║      ╚████╔╝ ██║     ██║     █████╗
//  ██║     ██║██╔══╝  ██╔══╝  ██║       ╚██╔╝  ██║     ██║     ██╔══╝
//  ███████╗██║██║     ███████╗╚██████╗   ██║   ╚██████╗███████╗███████╗
//  ╚══════╝╚═╝╚═╝     ╚══════╝ ╚═════╝   ╚═╝    ╚═════╝╚══════╝╚══════╝
//
//   ██████╗ █████╗ ██╗     ██╗     ██████╗  █████╗  ██████╗██╗  ██╗███████╗
//  ██╔════╝██╔══██╗██║     ██║     ██╔══██╗██╔══██╗██╔════╝██║ ██╔╝██╔════╝
//  ██║     ███████║██║     ██║     ██████╔╝███████║██║     █████╔╝ ███████╗
//  ██║     ██╔══██║██║     ██║     ██╔══██╗██╔══██║██║     ██╔═██╗ ╚════██║
//  ╚██████╗██║  ██║███████╗███████╗██████╔╝██║  ██║╚██████╗██║  ██╗███████║
//   ╚═════╝╚═╝  ╚═╝╚══════╝╚══════╝╚═════╝ ╚═╝  ╚═╝ ╚═════╝╚═╝  ╚═╝╚══════╝
//

var _ = require('@sailshq/lodash');

module.exports = function LifecycleCallbackBuilder(context) {
  // Build a list of accepted lifecycle callbacks
  var validCallbacks = [
    'beforeValidate',
    'afterValidate',
    'beforeUpdate',
    'afterUpdate',
    'beforeCreate',
    'afterCreate',
    'beforeDestroy',
    'afterDestroy',
    'beforeFind',
    'afterFind',
    'beforeFindOne',
    'afterFindOne'
  ];

  // Hold a mapping of functions to run at various times in the query lifecycle
  var callbacks = {};

  // Look for each type of callback in the collection
  _.each(validCallbacks, function(callbackName) {
    // If the callback isn't defined on the model there is nothing to do
    if (_.isUndefined(context[callbackName])) {
      return;
    }

    callbacks[callbackName] = context[callbackName];
  });

  return callbacks;
};