Adds hooks / callbacks / lifecycle events
This commit adds the ability of hooks for the DAOFactory. The following hooks (in their order of operations) are: (1) beforeValidate(dao, fn) (-) validate (2) afterValidate(dao, fn) (3) beforeBulkCreate(daos, fields, fn) beforeBulkDestroy(daos, fields, fn) beforeBulkUpdate(daos, fields, fn) (4) beforeCreate(dao, fn) beforeDestroy(dao, fn) beforeUpdate(dao, fn) (-) create / destroy / update (5) afterCreate(dao, fn) aftreDestroy(dao, fn) afterUpdate(dao, fn) (6) afterBulkCreate(daos, fields, fn) afterBulkDestory(daos, fields, fn) afterBulkUpdate(daos, fields, fn) There's a new file called hooks.js which works very similar to mixins.js which just extends a prototype. You can add the hooks like so... ... via .define(): var User = sequelize.define('User', { username: DataTypes.STRING, mood: { type: DataTypes.ENUM, values: ['happy', 'sad', 'neutral'] } }, { hooks: { beforeValidate: function(user, fn) { user.mood = 'happy' fn(null, user) }, afterValidate: function(user, fn) { user.username = 'Toni' fn(null, user) } } }) ... via .hook() method var User = sequelize.define('User', { username: DataTypes.STRING, mood: { type: DataTypes.ENUM, values: ['happy', 'sad', 'neutral'] } }) User.hook('beforeValidate', function(user, fn) { user.mood = 'happy' fn(null, user) }) User.hook('afterValidate', function(user, fn) { user.username = 'Toni' fn(null, user) }) ... via direct method: var User = sequelize.define('User', { username: DataTypes.STRING, mood: { type: DataTypes.ENUM, values: ['happy', 'sad', 'neutral'] } }) User.beforeValidate(function(user, fn) { user.mood = 'happy' fn(null, user) }) User.afterValidate(function(user, fn) { user.username = 'Toni' fn(null, user) }) Quick example: User.beforeCreate(function(user, fn) { if (user.accessLevel > 10 && user.username !== "Boss") { return fn("You can't grant this user that level!") } return fn() }) User.create({ username: 'Not a Boss', accessLevel: 20 }).error(function(err) { console.log(err) // You can't grant this user that level! }) As of right now, each hook will process in the order they where implemented / added to the factory. To invoke the hooks simply run... Model.runHooks.call(Model.options.hooks.<hook>, <args>, <callback>) Some model hooks have two or three paramters sent to each hook depending on it's type. Model.beforeBulkCreate(function(records, fields, fn) { // records = the first argument sent to .bulkCreate // fields = the second argument sent to .bulkCreate }) Model.bulkCreate([ {username: 'Toni'}, // part of records argument {username: 'Tobi'} // part of records argument ], ['username'] /* part of fields argument */) Model.beforeBulkUpdate(function(attributes, where, fn) { // attributes = first argument sent to Model.update // where = second argument sent to Model.update }) Model.update( {gender: 'Male'} /*attribures argument*/, {username: 'Tom'} /*where argument*/ ) Model.beforeBulkDestroy(function(whereClause, fn) { // whereClause = first argument sent to Model.destroy }) Model.destroy({username: 'Tom'} /*whereClause argument*/) For 1.7.x backwards compatibility, I've added a new method called .hookValidate() since .validate() is a synchronous function. All of Sequelize's API functions will invoke .hookValidate(), but if you utilize the .validate() function outside of Sequelize then you'll need to update your code if you want to run before/afterValidate hooks. Sequelzie 2.0.x will not need this change simply because it's .validate() method is already asynchronous. However, it will have the .hookValdate() function in order to make the transition from 1.7 to 2.0 smoother and easier. Eventually we'll want to deprecate this function. In addition to this commit, I've also completed the following tasks: Move validation of enum attribute value to validate method I had to complete that task in order to get the validate hooks working properly. And the last thing, I fixed executables.test.js if your DB didn't use the default values for config/config.js, this was causing errors for me on my local machine.
Showing
with
422 additions
and
240 deletions
This diff is collapsed.
Click to expand it.
test/hooks.test.js
0 → 100644
This diff could not be displayed because it is too large.
-
Please register or sign in to post a comment