admin管理员组文章数量:1326616
Is there a way to extend (maybe inherit) model to add hooks and fields after model was defined?
So something like this:
User = sequelize.define("user", {
name: sequelize.String
});
makeStateful(User); // adds state,updated,added fields and some hooks
Is there a way to extend (maybe inherit) model to add hooks and fields after model was defined?
So something like this:
User = sequelize.define("user", {
name: sequelize.String
});
makeStateful(User); // adds state,updated,added fields and some hooks
Share
Improve this question
asked Oct 30, 2013 at 12:24
Plastic RabbitPlastic Rabbit
2,9994 gold badges28 silver badges27 bronze badges
1 Answer
Reset to default 7this is not possible at the moment. But you could easily make it work the other way around: Define your mixin before and use that when you define the model:
var Sequelize = require('sequelize')
, sequelize = new Sequelize('sequelize_test', 'root')
var mixin = {
attributes: {
state: Sequelize.STRING,
added_at: Sequelize.DATE
},
options: {
hooks: {
beforeValidate: function(instance, cb) {
console.log('Validating!!!')
cb()
}
}
}
}
var User = sequelize.define(
'Model'
, Sequelize.Utils._.extend({
username: Sequelize.STRING
}, mixin.attributes)
, Sequelize.Utils._.extend({
instanceMethods: {
foo: function() {
return this.username
}
}
}, mixin.options)
)
User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function(u) {
console.log(u.foo()) // 'foo'
})
})
本文标签: javascriptHow to extend Sequelize modelStack Overflow
版权声明:本文标题:javascript - How to extend Sequelize model - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742213864a2434206.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论