Currently, I have been utilizing SequelizeJS for my data access layer. My main concern revolves around creating custom Sequelize model calls.
For instance, if I wish to implement caching with Redis for the model.find() method in order to skip the normal relational database call when data is cached. I have only come across one way to achieve this:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
id: DataTypes.INTEGER,
firstName: DataTypes.INTEGER,
middleName: DataTypes.INTEGER,
lastName: DataTypes.INTEGER,
email: DataTypes.INTEGER,
username: DataTypes.INTEGER,
password: DataTypes.INTEGER,
superAdminFlag: DataTypes.INTEGER,
requirePasswordChangeFlag: DataTypes.INTEGER,
lastPasswordChangeDateTime: DataTypes.INTEGER,
createdTimestamp: DataTypes.INTEGER,
updatedTimestamp: DataTypes.INTEGER,
type: DataTypes.INTEGER,
status: DataTypes.STRING(32)
}, {
sequelizeSetup: function(models) {
User.hasMany(models.OauthToken, {
foreignKey: 'userId',
through: null
});
}
});
//create a custom object that links to the Sequelize model object
var MyUser = Object.create(User);
//add custom logic to the find() method for this model
MyUser.find = function(options, queryOptions) {
console.log('custom logic');
return User.find(options, queryOptions);
};
return MyUser;
};
My inquiry pertains to whether or not this method is appropriate for customizing SequelizeJS model methods and if it may lead to any potential issues (specifically concerning the model object itself rather than instances of the model object).