javascript 如何在 Sequelize.js 中定义对象数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25565212/
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 to define array of objects in Sequelize.js?
提问by Evan Lévesque
How can I define an array of objects field in Sequelize.js model?
I need something like this
如何在 Sequelize.js 模型中定义对象字段数组?
我需要这样的东西
{
"profiles" : [
{
"profile_id": 10,
"profile_pictures" : ["pic1.jpg","pic2.jpg","pic3.jpg"],
"profile_used_id" : 12
},
... // more profiles
]
}
I checked the docs, but couldn't find a relevant data type, am I missing something here ?
我检查了文档,但找不到相关的数据类型,我在这里遗漏了什么吗?
回答by mathisonian
I have successfully been mimicking arrays by defining my model like:
我已经成功地通过定义我的模型来模仿数组,如:
var MyModel = sequelize.define('MyModel', {
myArrayField: {
type: DataTypes.STRING,
get: function() {
return JSON.parse(this.getDataValue('myArrayField'));
},
set: function(val) {
return this.setDataValue('myArrayField', JSON.stringify(val));
}
}
}
回答by scharfmedia
I can think currently of 2 solutions (and a 3rd one if you are using PostgreSQL).
我目前可以想到 2 个解决方案(如果您使用的是 PostgreSQL,则是第 3 个)。
We have a relational database working behind, so do its principles apply also for Sequelize which is a ORM for relational databases. The easiest would be to create another table or entity and associate them as a 1:n relationship. For that matter add a new model in Sequelize and define its associations like described here: http://sequelizejs.com/articles/getting-started#associationsYou might have then 2 tables. One profile table having N pictures.
If its just a filename or url you could serialise a Javascript array to a JSON string like:
// before save
var mypics = ["pic1.jpg","pic2.jpg"]; profile.pictures = JSON.stringify( mypics ); profile.save()
// after load before use
var profile = Profile.get(1) pictures = JSON.parse(profile.pictures);
If you use PostgreSQL you could use the Array Datatype for this field, see: http://www.postgresql.org/docs/8.4/static/arrays.html
or the JSON Datatype:
http://www.postgresql.org/docs/9.3/interactive/datatype-json.html
我们有一个关系数据库在工作,所以它的原则也适用于 Sequelize,它是关系数据库的 ORM。最简单的方法是创建另一个表或实体并将它们关联为 1:n 关系。为此,在 Sequelize 中添加一个新模型并定义其关联,如下所述:http://sequelizejs.com/articles/getting-started#associations 然后您可能有 2 个表。一张个人资料表,有 N 张图片。
如果它只是一个文件名或 url,您可以将 Javascript 数组序列化为 JSON 字符串,例如:
// 保存前
var mypics = ["pic1.jpg","pic2.jpg"]; profile.pictures = JSON.stringify( mypics ); profile.save()
// 加载后使用
var profile = Profile.get(1) 图片 = JSON.parse(profile.pictures);
如果您使用 PostgreSQL,您可以使用该字段的数组数据类型,请参阅:http: //www.postgresql.org/docs/8.4/static/arrays.html
或 JSON 数据类型:
http://www.postgresql.org/docs/9.3/interactive/datatype-json.html
Go for 1 if your Picture object is or will be more complex in the feature. Or if you want to query by filename. Go for 2 if you dont do any data filtering or complex data in the future.
如果您的图片对象在功能中更复杂或将更复杂,请选择 1。或者,如果您想按文件名查询。如果您将来不进行任何数据过滤或复杂数据,请选择 2。
Go for 3? I think its best to stick to Sequelize abstraction and not use custom data types even its possible in that case. Maybe you better stick to 1 or 2.
去3?我认为最好坚持 Sequelize 抽象并且不使用自定义数据类型,即使在这种情况下也是可能的。也许你最好坚持 1 或 2。
回答by Benjamin Schurtenberger
Try to define an Array like this
尝试像这样定义一个数组
var profiles = {
profile_id: 10,
profile_pictures: {
"pic1.jpg",
"pic2.jpg",
"pic3.jpg"
},
profile_used_id: 12
}
var profiles = {
profile_id: 10,
profile_pictures: {
"pic1.jpg",
"pic2.jpg",
"pic3.jpg"
},
profile_used_id: 12
}