[英]Sails.js - how to save only model's fields into database?
Let's say I have a model defined: 假设我定义了一个模型:
module.exports = {
attributes: {
username: {
type: 'string',
required: true,
unique: true
}
}
}
Then I create an object that I will save to database: 然后我创建一个将保存到数据库的对象:
var obj = {
username: 'blabla',
score: 100,
whatever: 'else'
}
Model.create(obj).then(...);
The additional fields will also be persisted into database. 其他字段也将保留在数据库中。 The question is - how do I save only the fields defined in model?
问题是 - 如何只保存模型中定义的字段? In this case - username, even when trying to save all 3 fields.
在这种情况下 - 用户名,即使在尝试保存所有3个字段时也是如此。
I used to do it like: 我以前这样做:
Model.create({username: obj.username}).then(...);
But I wonder if there is a way to do this without having to map every field explicitly, because it's not very easy to maintain, and the model kinda loses its purpose. 但我想知道是否有办法做到这一点,而不必明确地映射每个字段,因为它不是很容易维护,并且模型有点失去其目的。
Try this: 尝试这个:
schema: true
link to doc: 链接到doc:
http://sailsjs.org/documentation/concepts/models-and-orm/model-settings#?schema http://sailsjs.org/documentation/concepts/models-and-orm/model-settings#?schema
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.