node.js sequelize getter 和 setter 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21949554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do sequelize getter and setters work?
提问by user2316667
Summary of question:Conceptually, what are getters and setters and why would we use them?
问题摘要:从概念上讲,什么是 getter 和 setter,我们为什么要使用它们?
Excerpt from http://docs.sequelizejs.com/en/latest/docs/models-definition/?highlight=getterMethods#getters-setters:
It is possible to define 'object-property' getters and setter functions on your models, these can be used both for 'protecting' properties that map to database fields and for defining 'pseudo' properties.
可以在模型上定义“对象属性”getter 和 setter 函数,它们既可用于“保护”映射到数据库字段的属性,也可用于定义“伪”属性。
What does it mean by 'protect'? Against what?
What are 'psuedo' properties?
“保护”是什么意思?反对什么?
什么是“伪”属性?
I'm also struggling with the example code below. We appear to be setting 'title' twice. And what is the argument 'v'?
我也在为下面的示例代码苦苦挣扎。我们似乎两次设置“标题”。什么是参数“v”?
See below:
见下文:
var Foo = sequelize.define('Foo', {
title: {
type : Sequelize.STRING,
allowNull: false,
}
}, {
getterMethods : {
title : function() { /* do your magic here and return something! */ },
title_slug : function() { return slugify(this.title); }
},
setterMethods : {
title : function(v) { /* do your magic with the input here! */ },
}
});
A concrete example instead of "do magic" would be greatly appreciated!
一个具体的例子而不是“做魔术”将不胜感激!
回答by Jan Aagaard Meier
pseudo properties
伪属性
Would be properties that, from the perspective of the user seems like regular properties of the object, but do not exist in the database. Take for example a user object that has first names and last name fields. You could then create a fullname setter:
将是属性,从用户的角度来看似乎是对象的常规属性,但不存在于数据库中。以具有名字和姓氏字段的用户对象为例。然后你可以创建一个全名设置器:
var foo = sequelize.define('foo', {
..
}, {
getterMethods: {
fullName: function () {
return this.getDataValue('firstName') + ' ' + this.getDataValue('lastName')
}
},
setterMethods: {
fullName: function (value) {
var parts = value.split(' ')
this.setDataValue('lastName', parts[parts.length-1])
this.setDataValue('firstName', parts[0]) // this of course does not work if the user has several first names
}
}
})
When you have a user object you can simply do
当你有一个用户对象时,你可以简单地做
console.log(user.fullName)
To see the user's full name. The getter is then being invoked behind the scenes.
查看用户的全名。然后在幕后调用 getter。
Similarily, if you define a setter method for full name you could do
同样,如果你为全名定义一个 setter 方法,你可以这样做
user.fullName = 'John Doe'
Which would then split the passed string into two parts and save them in first and last name. (see the simplified example above)
然后将传递的字符串分成两部分并将它们保存在名字和姓氏中。(参见上面的简化示例)
Protect properties
保护财产
@ahiipsa already provided a good example of this. Getters are called when you do user.toJSON(), so you can use getters to easily remove sensitive data before sending it to the user.
@ahiipsa 已经提供了一个很好的例子。当您执行 user.toJSON() 时会调用 Getter,因此您可以使用 getter 轻松删除敏感数据,然后再将其发送给用户。

