Constant 'in' values as REST API value at ease

1.3k views Asked by At

In this SailsJS Model, we are keeping the static option values in 'in' key attribute.

status: {
    type : 'string',
    defaultsTo : 'Active',
    in : ['Active', 'InActive']
}

Are these values are available in REST API -> preferably in /OPTIONS format?

Would like to offer One Source of Truth for these Constant Values. Wanted to understand the best practices how these values can be sent via REST API for the Front End Application. When both front end application as well Thrid party API users can get the same source of truth, then its very easy to maintain the application. Please suggest.

Thanks and Regards, Raj

1

There are 1 answers

4
Evilsanta On BEST ANSWER

I am afraid you have to make an OptionController and manually do it. For these static options, you can write them in a config file, then access them with sails.config.xxx. For example: In config/whatever.js

module.exports={
    activeChoices:['active','inactive','donotknow','xxx']
}

Create the OptionController.js, and set the route

Route.js

'GET /statusOptions/ :{
    controller:'OptionController',
    action:'statusOptions'
}

Then in your api/Controller/OptionController.js

module.exports={
    statusOptions:function(req,res){
        return res.json(sails.config.activeChoices);
    }
}

In your model

status:{type:'string',defaultsTo:'Active',in:sails.config.activeChoices}