Reputation: 2019
I am new to Sails and creating a simple application.
I am stuck with data model now.
User
model is as follows:
module.exports = {
attributes: {
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
email: {
type: 'email',
required: true
},
password: {
type: 'String'
},
passwordSalt: {
type: 'String'
},
projects:{
collection: 'ProjectMember',
via: 'userId'
}
}
};
I need one more Model called TinyUser
which gets some of attributes from User (Like Foreign key to User), so that I can access TinyUser
instead of accessing User
directly.
TinyUser Model is as follows:
module.exports = {
tableName: 'User',
attributes: {
firstName:{
type: 'string'
},
lastName: {
type: 'string'
},
email: {
type: 'email'
}
}
};
ProjectMember model is as follows:
module.exports = {
attributes: {
projectId: {
model: 'Project'
},
userId: {
model: 'TinyUser'
},
projectRole: {
model: 'ProjectRole'
},
}
};
In Sails, Is there anyway I can save data in TinyUser
without actually creating but just holding some of attributes of User table data?
To Make it clear, If there is another table called Task and it is trying to access user data then TinyUser is better option as a model rather than User as it holds only required info. for task rather than storing all other fields which User does.
Is there any way I can fix this? Thanks In Advance
Upvotes: 1
Views: 786
Reputation: 606
Do you mean you need to have the "TinyUser" inherited from the "User" model?
You can do it like this.
/api/services/baseUserModel.js:
module.exports = {
attributes: {
firstName: 'string',
lastName: 'string',
email: {
type: 'email',
required: true
},
password: 'string',
passwordSalt: 'string',
projects: {
collection: 'ProjectMember',
via: 'userId'
}
}
};
/api/models/TinyUser.js:
var baseUserModel = require('../services/baseUserModel'),
_ = require('lodash');
module.exports = _.merge({
tableName: 'User',
attributes: {
firstName: 'string',
lastName: 'string',
email: {
type: 'email',
required: true
}
}
}, baseUserModel);
And the ProjectMember model should be:
module.exports = {
attributes: {
projectId: {
model: 'project'
},
userId: {
model: 'tinyuser'
},
projectRole: {
model: 'projectrole'
}
}
};
Upvotes: 1