I was trying through some advanced techniques used to refactor sequilize.js models and came across how instanceMethods method can be used and util functions can be attached to it.
Example:
function get_instance_methods(sequelize) {
return {
is_admin : function() {
return this.admin === true;
},
};
};
and then the above can be used like so :
module.exports = function(sequelize, DataTypes) {
var instance_methods = get_instance_methods(sequelize);
var User = sequelize.define("User", {
email : {
type : DataTypes.STRING,
allowNull : false
},
}, {
instanceMethods : instance_methods,
});
return User;
};
But now I came across a mixin being defined and then being used like so inside a modal HERE .
withCompanyAwareness.call ( instance_methods, sequelize ) ;
the code for the mixin itself is being defined HERE. A snapshot of what the mixin looks like can be found below :-
module.exports = function(sequelize){
// more methods defined here .. just adding a snapshot here.
this.get_company_with_all_leave_types = function() {
return this.getCompany({
include : [{
model : sequelize.models.LeaveType,
as : 'leave_types',
}],
order : [
[{ model : sequelize.models.LeaveType, as : 'leave_types' }, 'sort_order', 'DESC'],
[{ model : sequelize.models.LeaveType, as : 'leave_types' }, 'name']
]
});
};
};
What exactly is the purpose of defining a mixin vs using instance methods? Why is there a need for defining a mixin?