returnfn("You can't grant this user an access level above 10!")
thrownewError("You can't grant this user an access level above 10!")
}
returnfn()
})
```
This example will emit an error:
```js
User.create({username:'Not a Boss',accessLevel:20}).error(function(err){
User.create({username:'Not a Boss',accessLevel:20}).catch(function(err){
console.log(err)// You can't grant this user an access level above 10!
})
```
...
...
@@ -206,6 +205,29 @@ DELETE FROM `table` WHERE associatedIdentifiier = associatedIdentifier.primaryKe
However, adding `hooks: true` explicitly tells Sequelize that optimization is not of your concern and will perform a `SELECT` on the associated objects and destroy each instance one by one in order to be able to call the hooks with the right parameters.
## Promises and callbacks
Sequelize will look at the function length of your hook callback to determine whether or not you're using callbacks or promises.
```js
// Will stall if the condition isn't met since the callback is never called
returncallback("You can't grant this user an access level above 10!");
}
});
// Will never stall since returning undefined will act as a resolved promise with an undefined value
User.beforeCreate(function(user,options){
if(user.accessLevel>10&&user.username!=="Boss"){
returnthrownewError("You can't grant this user an access level above 10!");
}
if(something){
returnPromise.reject();
}
});
```
## A Note About Transactions
Note that many model operations in Sequelize allow you to specify a transaction in the options parameter of the method. If a transaction _is_ specified in the original call, it will be present in the options parameter passed to the hook function. For example, consider the following snippet:
...
...
@@ -214,7 +236,7 @@ Note that many model operations in Sequelize allow you to specify a transaction
// Here we use the promise-style of async hooks rather than
// the callback.
User.hook('afterCreate',function(user,options){
// 'trans' will be available in options.transaction
// 'transaction' will be available in options.transaction
// This operation will be part of the same transaction as the