Loopback max limit of records returned from server

Viewed 516

Is there an option in loopback to set max amount of records returned by server?

For example I have 1000 users in db. I want server return no more then 20 users. But if someone sends 1 5 10 15 in filter loopback should return that amount.

There is an option "scope" for a model in loopback. But this option overwrites limit param specified by request.

1 Answers

You can use Limit and Skip provided by the loopback

Something like this /cars?filter[limit]=10&filter[skip]=0

/cars?filter[limit]=10&filter[skip]=10

The following REST requests illustrate how to paginate a query result. Each request request returns ten records: the first returns the first ten, the second returns the 11th through the 20th, and so on…

For a Default implementation you can always do this in the js file of the model

Model.beforeRemote('find', function(ctx, instance, next) {
    if (!ctx.args.filter || !ctx.args.filter.limit) {
      if (!ctx.args.filter) ctx.args.filter = {};
      ctx.args.filter.limit = 10;
    }
    next();
  });
Related