Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use search sails.js models using REST without Shortcuts blueprint?

I have turned off shortcuts blueprints (/model/findAll wont work) and now I want to search the model, using REST, but I did not find anything, is there any documentation or do I have to write my own views for that?

Thanks

like image 662
Visgean Skeloru Avatar asked Oct 10 '13 10:10

Visgean Skeloru


1 Answers

In Sails.js, the CRUD and REST blueprint routes are enabled for development, but for production I suggest that you have your own actions that can find/create/update/delete models in the given controller

Sails automatically maps the actions in your controllers to corresponding routes, without you having to manually configure them, to turn this off: set the actions flag to false in the config/controllers.js file

This is best explained with an example, so here is a basic model that has some RESTful routes

models/User.js

Our model has two attributes and does not enforce uniqueness

module.exports = {

  attributes: {
    name: 'string',
    email: 'string'
  }

};

controllers/UserController.js

Our controller has two actions, findAll returns all users and findByName returns users with given name

module.exports = {

  findAll: function (req, res) {
    User.find().done(function (err, users) {
      if (err) {
        res.send(400);
      } else {
        res.send(users);
      }
    });
  },

  findByName: function(req, res) {
    var name = req.param('name');
    User.findByName(name).done(function (err, users) {
      if (err) {
        res.send(400);
      } else {
        res.send(users);
      }
    });
  }

};

config/routes.js

Our routes configuration has two routes that respond only to GET requests, /user/all will run the findAll action and /user/name/:name will run findByName action with name given as a parameter in the URL

module.exports.routes = {

  'GET /user/all': 'UserController.findAll',
  'GET /user/name/:name': 'UserController.findByName'

};

For more information, be sure to check out the documentation of controllers in Sails.js

like image 101
scott Avatar answered Oct 02 '22 12:10

scott