question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong totalCount when using joins in QB

See original GitHub issue

In this commit https://github.com/nestjsx/nestjs-typeorm-paginate/commit/890ee164e15a1495782917b8443f0cd4cca0b7c0#diff-c58e144dadf00250381ed55e6ce83245cda76aca84131ad494fd4911932f656f. a custom total count query was introduced instead of TypeORMs qb.getCount();

this query being SELECT COUNT(*) FROM (…) returns number of all rows returned by query which would be like (m*n* k…) depending on how many rows were joined

the original implementation of TypeORMs getCount does SELECT COUNT(DISTINCT("table"."id")) AS "cnt" FROM (..) which returns correct number of entities even when using JOINs

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:7
  • Comments:27 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
WalleksMRcommented, May 25, 2022

@ayeen, thank you for your advice.

I know about this option and used it. But in my case the result was not completely correct. When I set paginationType: PaginationTypeEnum.TAKE_AND_SKIP, I’ve got the correct value (count) of items, but meta.totalItems was incorrect. For example, items is an array of 5 elements (which is true), but meta.totalItems is 10.

My solution with this problem, was return the function metaTransformer and modifying the return of totalItems and totalPages.

async listAll({
    page,
    limit,
  }: IListAllProps): Promise<Pagination<PeopleM>> {
    const peoples = this.peopleEntityRepository
      .createQueryBuilder('p')
      .leftJoinAndSelect('p.lead_step', 'lead')
      .leftJoinAndSelect('p.schedules', 's')
      .select(['p', 'lead', 's.id', 's.started_at', 's.finished_at']);

    // Here I get total items of table peoples
    const totalItems = await peoples.getCount();
    
    return await paginate<People>(peoples, {
      limit,
      page,
      paginationType: PaginationTypeEnum.TAKE_AND_SKIP,
      metaTransformer: ({ currentPage, itemCount, itemsPerPage }) => {

     // Calculating the total of pages
        const totalPages = Math.round(totalItems / itemsPerPage);
        return {
          currentPage,
          itemCount,
          itemsPerPage,

         // Returning in this two row
          totalItems,
          totalPages: totalPages === 0 ? 1 : totalPages,
        };
      },
    });
  }

I know it’s not right this way, but was that I found at moment.

4reactions
NikolayYakovenkocommented, Jan 24, 2022

@ayeen, thank you for your advice.

I know about this option and used it. But in my case the result was not completely correct. When I set paginationType: PaginationTypeEnum.TAKE_AND_SKIP, I’ve got the correct value (count) of items, but meta.totalItems was incorrect. For example, items is an array of 5 elements (which is true), but meta.totalItems is 10.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Query operations and syntax - Intuit Developer
To find out how many entities will be returned by a query, specify the COUNT keyword. The number of entities is returned as...
Read more >
Error converting value "Bounced Email" to type ... - GitHub
Invoice object, I am encountering the following error for some invoices: Error converting value "Bounced Email" to type 'Intuit.Ipp.Data.
Read more >
Count Rows in Doctrine QueryBuilder - symfony - Stack Overflow
Example working with grouping, union and stuff. Problem: $qb = $em->createQueryBuilder() ->select('m.id', 'rm.id') ->from('Model', 'm') ->join('m.
Read more >
NFL Insider Weighs In on the Complexities the Packers Deal ...
Jordan Love, the 2020 first-round pick QB of the Packers stares at his third ... 5 lunges alternative exercises for people with bad...
Read more >
NFL Insider Weighs In on the Complexities the Packers Deal ...
Jordan Love, the 2020 first-round pick QB of the Packers stares at ... However, Rodgers himself had to deal with it while competing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found