TypeORM/NestJS 中的分页

Pagination in TypeORM/NestJS

我必须在 findAll() 方法中引入分页。我真的不知道该怎么做。我试过了,但它给出了很多错误。为此,我使用了 typeorm 给出的 findAndCount() 方法,但我不确定它是如何工作的。

截至目前,以下方法 returning 所有记录。我需要一次 return 10 条记录。请建议我需要做什么修改。

async findAll(queryCertificateDto: QueryCertificateDto,page=1):  Promise<PaginatedResult> {
    
    let { country, sponser } = queryCertificateDto;

    const query = this.certificateRepository.createQueryBuilder('certificate');

    if (sponser) {
        sponser = sponser.toUpperCase();
        query.andWhere('Upper(certificate.sponser)=:sponser', { sponser });
    }

    if (country) {
        country = country.toUpperCase();
        query.andWhere('Upper(certificate.country)=:country', { country });
    }      
    const  certificates = query.getMany();     
    return certificates;
}

这是 PaginatedResult 文件。

 export class PaginatedResult {
        data: any[];
        meta: {
          total: number;
          page: number;
          last_page: number;
        };
      }
  

我尝试更改 findAll() 的代码,但 where 子句出错。我不确定如何处理 pagination.

中的 query.getMany()
 const take = query.take || 10
        const skip = query.skip || 0
       
      
        const [result, total] = await this.certificateRepository.findAndCount(
            {
                where: query.getMany(),   //this is giving error
                take:take,
                skip:skip
            }
        );
        return result;

我需要在这个方法中引入分页。任何帮助都会很有帮助。

Typeorm 有一个特定于您的用例的非常好的方法findAndCount

async findAll(queryCertificateDto: QueryCertificateDto):  Promise<PaginatedResult> {

    const take = queryCertificateDto.take || 10
    const skip = queryCertificateDto.skip || 0
    const country = queryCertificateDto.keyword || ''
    const sponser = queryCertificateDto.sponser || ''
    

    const query = this.certificateRepository.createQueryBuilder('certificate');

    const [result, total] = await this.certificateRepository.findAndCount(
        {
            where: { country: Like('%' + country + '%') AND sponser: Like('%' + sponser + '%') }, order: { name: "DESC" },
            take: take,
            skip: skip
        }
    );
     
    return {
        data: result,
        count: total
    };
}

可以找到有关存储库 class 的更多文档 here

你不需要 .getMany() 和你最后代码中的 where,结果是你需要的数据数组。

从您的第一个代码开始,您可以这样做:

async findAll(queryCertificateDto: QueryCertificateDto,page=1): Promise<PaginatedResult> {
    // let's say limit and offset are passed here too
    let { country, sponser, limit, offset } = queryCertificateDto;

    const query = this.certificateRepository.createQueryBuilder('certificate');

    if (sponser) {
        sponser = sponser.toUpperCase();
        query.andWhere('certificate.sponser = :sponser', { sponser });
    }

    if (country) {
        country = country.toUpperCase();
        query.andWhere('certificate.country = :country', { country });
    }

    // limit and take mean the same thing, while skip and offset mean the same thing
    const certificates = await query
        .orderBy("certificate.id", "ASC")
        .limit(limit || 10)
        .offset(offset || 0)
        .getMany();

    // if you want to count just replace the `.getMany()` with `.getManyandCount()`;

    return certificates;
}```