Adraen
Adraen

Reputation: 25

Sails.js disable query params in find blueprint

Currently cleaning up some of the code in my sails backend and I'm not too sure how to configure properly the blueprint for the particular controller.

So I want one of my endpoint to always return only the last 10 results and disable the ability for limit/find/where as a query param. So far I've done this simply by overriding find in the controller but I was wondering if there was a way to configure the blueprint.

I've added in the controller _config the following, which allows to set default values but can still be replaced with ?limit=15.

_config: { sort: 'id_str desc', limit: 5 }

Thanks.

Upvotes: 0

Views: 502

Answers (1)

Fissio
Fissio

Reputation: 3758

You could write a policy that executes upon find requests to your model.

api/policies/limitResults.js

module.exports = function(req, res, next) {
  req.query.limit = 10;
  req.query.sort = 'id_str desc';
  return next();
}

config/policies.js

ModelController: {
  'find': ['limitResults']
}

This should overwrite the limit parameter of the request and sort the results in descending order.

Upvotes: 1

Related Questions