Sail.js dynamic associations

时间:2016-02-15 09:50:21

标签: javascript sails.js waterline

I need to join owner model by ownerType value; if ownerType == org, i need to set owner.model to org

// api/models/Request.js
module.exports = {
    attributes: {
        product: 'string',
        ownerType: {
            type: 'string',
            enum:['org','person']
        },
        ownerId:'int',
        owner: {
         model: ''// model by ownerType
       }
    }
};`

`// api/models/Org.js
module.exports = {
    attributes: {
        name: 'string',
        requests:{
            collection:'request'
        }
    }
};`

`// api/models/Person.js
module.exports = {
    attributes: {
        firstName: 'string',
        requests:{
            collection:'request'
        }
    }
};

Any right way to do dynamic association? Or it can be done only in controller?

0 个答案:

没有答案