简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM