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

[FR] New Hook: next #717

Open
FossPrime opened this issue May 5, 2023 · 0 comments
Open

[FR] New Hook: next #717

FossPrime opened this issue May 5, 2023 · 0 comments

Comments

@FossPrime
Copy link
Contributor

FossPrime commented May 5, 2023

Steps to reproduce

Problem: Pagination is a pain. It is so much of an ugly pain the pagination docs, don't ever try iterating over paginated results, instead showing how to disable pagination!

Solution: a next iterator hook.

Without Hook:

async function main() {
  allMessages = [];
  let page = 1;
  let result;

  do {
    result = await  app.service('messages').find({
      query: {
        $limit: 10, // Change `10` to the maximum number of messages you want to retrieve per page
        $skip: (page - 1) * 10, // Change `10` to the maximum number of messages you want to retrieve per page
      },
    });

    allMessages.push(...result.data);
    page++;
  } while (result.total > allMessages.length);

  allMessages.forEach(console.log)
}

With the Hook:

const main = async () => {
  const messageResult = await app.service('messages').find()
  for await (const res of messageResult.next) {
    res.forEach(console.log)
  }
}

Note how there is zero math involved in userland. Both userland code and the hook itself is somewhat stateless, every variable is a constant.

Proposed implementation:
https://stackblitz.com/edit/lowdb-qs-browser-nbkcvk?file=index.html%3AL89,app.ts

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

No branches or pull requests

1 participant