Hooks allow us to perform actions at different points throughout the life cycle of a model. For example, we could do something before an item is updated or after an item is created.
If we want all of our tags to be stored in all caps we could use a beforeCreate hook. This way if someone adds a tag Taco it will be stored as TACO and all of our tags will be the same.
//in models/tag.js -- tag model//... some other model code abovehooks:{beforeCreate:function(tag,options,cb){ //take the tag name and capitalize ittag.name =tag.name.toUpperCase(); //pass the updated tag object backcb(null, tag);}}//... some other model code below
We can use beforeCreate hook to automatically hash a users password before it is created (AKA before the data is inserted into the database).