Abhishek
Abhishek

Reputation: 2019

Remove undesirable fields from being returned in sails

I am new to SailsJS and stuck in Data Model as follows:
I have a User model as follows:

module.exports = {
attributes: {
firstName: {
    type: 'string'
},
email: {
    type: 'email',
    required: true
},

password: {
    type: 'String'
},
passwordSalt: {
 type: 'String'
},
projects:{
 collection: 'ProjectMember',
 via: 'userId'
}  
}  
};  

Task Model :

module.exports = {  
 taskName: {  
  type: 'String'  
 },  
 userId: {  
    model: 'User'  
 }
};  

In Task model, it is getting all fields from User table which is not required while task data is rendered. I was planning to create one more model called TinyUser which stores only required fields to be shown when task data is rendered.

But TinyUser should just refer User table and get required data from it rather than we creating all data for TinyUser manually when user data is created.

Is there any way this can be achieved in Sails?
Thanks in Advance..

Upvotes: 1

Views: 407

Answers (2)

scgy5555
scgy5555

Reputation: 1

User.find({select:['firstName', 'email']}).exec()

Upvotes: 0

Meeker
Meeker

Reputation: 5979

I'm not sure about your question, but this will return a list of required attributes for any model

_.find(sails.models.<YOUR MODEL>._attributes, function(attr){return attr.required})

If your intent it to simply remove undesirable fields you can override the toJSON / toObject methods

see https://github.com/balderdashy/waterline-docs/blob/master/models.md#toobjecttojson-instance-methods

Upvotes: 1

Related Questions