Vincas Stonys
Vincas Stonys

Reputation: 1135

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.

Upvotes: 1

Views: 605

Answers (1)

salhk
salhk

Reputation: 126

Try this:

schema: true

link to doc:

http://sailsjs.org/documentation/concepts/models-and-orm/model-settings#?schema

Upvotes: 3

Related Questions