nestjs-typeorm-paginate icon indicating copy to clipboard operation
nestjs-typeorm-paginate copied to clipboard

feat: implement count query type (RAW|ENTITY)

Open appotter opened this issue 2 years ago • 9 comments

This allows the query builder to choose a count query type which is counts for RAW or counts for ENTITY when an entity has inner join with many to many relations

ref issue #803 #758


import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { CatEntity } from './entities';
import {
  paginate,
  Pagination,
  IPaginationOptions,
  CountQueryTypeEnum,
} from 'nestjs-typeorm-paginate';

@Injectable()
export class CatService {
  constructor(
    @InjectRepository(CatEntity)
    private readonly repository: Repository<CatEntity>,
  ) {}

  async paginate(): Promise<Pagination<CatEntity>> {
    return paginate<CatEntity>(this.repository, {
      page: 1,
      limit: 30,
      // default is CountQueryTypeEnum.RAW
      countQueryType: CountQueryTypeEnum.ENTITY,
    });
  }
}
Screenshot 2022-11-27 at 00 10 17

appotter avatar Nov 26 '22 17:11 appotter

Will this be merged ? I have same problem with N-N relationships and this package.

zarnautovic avatar Mar 20 '23 11:03 zarnautovic

This solution works for me using TypeORM in a Nest.js project with query builder and joins calculating the correct totalCount.

Please review and merge someone with permissions.

schuerg avatar Mar 29 '23 07:03 schuerg

I could also benefit from this change. @bashleigh could you help with this?

xadamxk avatar Jun 16 '23 16:06 xadamxk

@bashleigh is there a chance You can merge this please?

leandro-gomez avatar Aug 30 '23 15:08 leandro-gomez

@bashleigh Looking forward to this PR being merged

Adebayo-Adesegun avatar Oct 26 '23 10:10 Adebayo-Adesegun

@bashleigh would also like to get this merged

devi-prsd avatar Nov 17 '23 04:11 devi-prsd

Sorry it's been a while. I think I prefer this PR out of all the solutions I've seen. Don't suppose you'd mind adding some docs on the readme on how to use it and then I'd be happy to merge it and release as the next minor version

bashleigh avatar Mar 12 '24 19:03 bashleigh