Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default scope options override options provided with rest queries #678

Closed
vlamic opened this issue Aug 4, 2015 · 6 comments
Closed

Default scope options override options provided with rest queries #678

vlamic opened this issue Aug 4, 2015 · 6 comments
Assignees

Comments

@vlamic
Copy link

vlamic commented Aug 4, 2015

Hi,

First of all thank you guys for the amazing framework. I love it!

I'm not sure if it's an issue or I'm mis-using the libraries. What I want to achieve is a default pagination where a query would limit items by default (as my collection could be really large and I don't want someone accidentally trying to return 5 millions of documents). So, I have specified the scope on a model:

.
.
  "scope": {
    "limit": 3
  },
.
.

When I query for the items I get the correct number of them in the result (3):

http://localhost:3001/api/items?filter={"fields": { "id": true } }


[
    {
        id: "55c099d2ade17e831c9aa25f"
    },
    {
        id: "55c099d2ade17e831c9aa260"
    },
    {
        id: "55c099d2ade17e831c9aa261"
    }
]

When I add an additional limit filter to the query above, I still get the same 3 items in the result (but I'm expecting 2 only):

http://localhost:3001/api/items?filter={"fields": { "id": true }, "limit": 2 }

[
    {
        id: "55c099d2ade17e831c9aa25f"
    },
    {
        id: "55c099d2ade17e831c9aa260"
    },
    {
        id: "55c099d2ade17e831c9aa261"
    }
]

Could you please advise if I'm using the package wrongly or it's a bug? Is there a better way of doing a default pagination? Thanks!

@sachinik19
Copy link

I'm facing the same issue. I feel the default scope doesn't get override. It's kind of a hard limit which will always supershade the values provided by user.

Is there any way to override it from a Model.js remote function.

@sachinik19
Copy link

Ok. I've found out a solution to do it using a operation hook. Hope it will be of your help

    MyModel.observe('access', function setMaxLimit(ctx, next) {     
        var limit = 24;
        var filter = ctx.query;
        if(!filter) { ctx.query = filter = {}; }
        if(!filter.limit || filter.limit > limit) filter.limit = limit;
        next();
    });

}

@0ff
Copy link

0ff commented Dec 8, 2015

In fact I think this was not intended and I've just submitted PR #787 to change that :-)

@christiaanwesterbeek
Copy link

Nice solution @sachinik19 . Using it now.

@ebarault
Copy link
Contributor

for those interested in @off 's PR, please see my post here
some changes are probably needed too with regards to the filters overriding policies currently in place

@mrfelton
Copy link
Contributor

For an overridable default/max limit you can check out https://github.com/fullcube/loopback-ds-resultset-limit-mixin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests