Reputation: 311
I am new to SAILS. I have the following models.
models\Company.js
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
unique: true
},
description: {
type: 'string',
required: true
}
}
};
models\Project.js
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
key: {
type: 'string',
required: true,
unique: true
},
description: {
type: 'string'
},
company: {
type: 'string',
required: true
},
startedDateTime: {
type: 'datetime'
},
completedDateTime: {
type: 'datetime'
},
members: {
collection: 'ProjectMember',
via: 'project',
dominant: true
}
};
I need the model to be such that, there can be multiple companies but project must be unique for a particular company, but it can be same within two diffrent companies. How can I modify my models for getting this?
Upvotes: 1
Views: 294
Reputation: 622
There are no such config options in sails.
But you could use lifecycle callbacks to handle this manually. You could add check beforeValidate and afterValidate functions.
Take a look here: http://sailsjs.org/#/documentation/concepts/ORM/Lifecyclecallbacks.html
afterValidate: function(values, cb) {
var params = {company: values.company};
if (values._id) {
params['_id'] = {$not: values._id};
}
Project.find(params).exec(function(err, projects) {
if (projects.length > 0) return cb(new Error('Project already binded'));
cb();
});
}
This is only sample so it might not work please check conditions and events.
Upvotes: 1