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

Cursor pagination first: 0 returns all items #5501

Closed
5 tasks done
stefansundin opened this issue Apr 23, 2024 · 1 comment
Closed
5 tasks done

Cursor pagination first: 0 returns all items #5501

stefansundin opened this issue Apr 23, 2024 · 1 comment

Comments

@stefansundin
Copy link
Contributor

stefansundin commented Apr 23, 2024

Describe the bug

Passing first: 0 to findByCursor returns all items.

I have a situation where, in the front-end, I want to just retrieve the total count of items. I don't have an endpoint to just get meta information, so I am just issuing a query to list zero items and then reading the page information. Unfortunately, right now this retrieves all items which is a major performance issue. So as a workaround I am requesting a single item with first: 1.

Since limit: 0 returns zero items, I think first: 0 also should return zero items.

Reproduction

https://github.com/stefansundin/mikro-orm-reproduction/tree/first-0

test('basic CRUD example', async () => {
  for (let i=0; i < 100; i++) {
    orm.em.create(User, { name: `Foo ${i}` });
  }
  await orm.em.flush();
  orm.em.clear();

  // {limit: 0} works
  const [limitResult, limitTotalCount] = await orm.em.findAndCount(User, {}, { limit: 0 });
  expect(limitTotalCount).toBe(100);
  expect(limitResult.length).toBe(0);

  // {first: 1} works
  const result1 = await orm.em.findByCursor(User, {}, { first: 1, orderBy: [{name: 'ASC'}] });
  expect(result1.totalCount).toBe(100);
  expect(result1.length).toBe(1);

  // {first: 0} is a performance trap!
  const result2 = await orm.em.findByCursor(User, {}, { first: 0, orderBy: [{name: 'ASC'}] });
  expect(result2.totalCount).toBe(100);
  expect(result2.length).toBe(0); // <-- bug
});

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.2.1

Node.js version

18.20.2

Operating system

No response

Validations

@B4nan
Copy link
Member

B4nan commented Apr 24, 2024

Note that first: 0 shouldn't result in limit 0 but rather to limit 1, as with cursor based pagination we are overfetching by default to know whether there is a next page. You can disable that via overfetch: false.

@B4nan B4nan closed this as completed in 508389e Apr 24, 2024
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

2 participants