olavim / objection-cursor

Cursor based pagination plugin for Objection.js
MIT License
30 stars 8 forks source link

Why previous cursor only back to one item? #4

Closed marufmax closed 5 years ago

marufmax commented 5 years ago

Hi, When I pass previous cursor it only back to one item. Is there any workaround I can go to next limit?

Thanks

olavim commented 5 years ago

Sorry for taking so long to reply.

I'm not sure I understand what your problem is. Are you saying you are seeing behavior like below?

go to next page

go to previous page

marufmax commented 5 years ago

yea but go to previous page is: Items 18-19 it just loading one item

olavim commented 5 years ago

That sounds like a bug. Can you give me your query and example data that fails.

olavim commented 5 years ago

Also, make sure that when you call previousCursorPage(cursor), the passed cursor comes from pageInfo.previous.

idododu commented 5 years ago

hope below exam make sense.

       if (params) {
          if (params.pagesize) {
            query.limit(params.pagesize)
          }
          if (params.next) {
            return query.cursorPage(params.next)
          } else if (params.prev) {
            return query.previousCursorPage(params.prev)
          }
        }
        return query.cursorPage()
olavim commented 5 years ago

@marufmax did you resolve the problem?

marufmax commented 5 years ago

this is how i am resolving this: Controller:

module.exports.getByCity = async (req, res) => {
    let response;
    if (req.query.next)  {
        response = await Weather.query()
            .where('city', req.params.city)
            .orderBy('id')
            .cursorPage(req.query.next)
            .limit(50);
    } else if(req.query.back) {
        response = await Weather.query()
            .where('city', req.params.city)
            .orderBy('id')
            .previousCursorPage(req.query.back)
            .limit(50);
    } else {
        response = await Weather.query()
            .where('city', req.params.city)
            .orderBy('id')
            .cursorPage()
            .limit(50);
    }

    response.pageInfo.nextPageUrl = req.path + '?next=' + response.pageInfo.next;
    response.pageInfo.previousPageUrl = req.path + '?back=' + response.pageInfo.previous;

    return res.json(response);

I believe there must be a simple way of doing this.

olavim commented 5 years ago

@marufmax you can simplify your code to

module.exports.getByCity = async (req, res) => {
    const cursor = req.query.next !== undefined ? req.query.next : req.query.back;
    const before = req.query.back !== undefined;

    const response = await Weather.query()
            .where('city', req.params.city)
            .orderBy('id')
            .cursorPage(cursor, before)
            .limit(50);

    response.pageInfo.nextPageUrl = req.path + '?next=' + response.pageInfo.next;
    response.pageInfo.previousPageUrl = req.path + '?back=' + response.pageInfo.previous;

    return res.json(response);
}

But I'm more interested in whether or not the code works as expected. Are you still getting only one item when going to previous page?


Edit: check for undefined instead of falsy.

olavim commented 5 years ago

closing as stale