ppetzold / nestjs-paginate

Pagination and filtering helper method for TypeORM repositories or query builders using Nest.js framework :book::paperclip:
MIT License
398 stars 88 forks source link

How to find the number of records in a relation? #916

Open kenvals opened 2 months ago

kenvals commented 2 months ago

I'm trying to use pagination to display product cards.

However I have a connection Typeorm as: Products <-> ​accounting

ProductsEntity:

  @OneToMany(() => AccountingEntity, (accounting) => accounting.product, {})
  accounting: AccountingEntity[];

 AccountingEntity:
   @ManyToOne(() => ProductEntity, (product) => product.accounting, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'product_id' })
  product: ProductEntity;

I display my product cards, but I can’t do it to show only those records in which there is NO accounting! or vice versa there is only ACCOUNTING

How can I do this correctly?

My code:

 const result: any = await paginate(query, this.productRepository, {

      sortableColumns: ['uuid', 'name', 'inventory', 'series', 'count', 'accounting', 'author.last_name',],
      relations: ['accounting', 'author', 'user'],
      nullSort: 'last',
      defaultSortBy: [['uuid', 'DESC']],
      searchableColumns: ['uuid', 'name', 'count', 'author.last_name', 'author.first_name', 'author.surname', 'user.last_name', 'user.first_name', 'user.surname', 'series', 'inventory',],
      filterableColumns: {
        count: [FilterOperator.EQ, FilterOperator.BTW],
        accounting: true,
        author: true,
        user: true
      },
    });
MrSharpp commented 1 month ago

Please add a support to sort by relation count

ppetzold commented 1 month ago

I'm trying to use pagination to display product cards.

However I have a connection Typeorm as: Products <-> ​accounting

ProductsEntity:

  @OneToMany(() => AccountingEntity, (accounting) => accounting.product, {})
  accounting: AccountingEntity[];

 AccountingEntity:
   @ManyToOne(() => ProductEntity, (product) => product.accounting, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'product_id' })
  product: ProductEntity;

I display my product cards, but I can’t do it to show only those records in which there is NO accounting! or vice versa there is only ACCOUNTING

How can I do this correctly?

My code:

 const result: any = await paginate(query, this.productRepository, {

      sortableColumns: ['uuid', 'name', 'inventory', 'series', 'count', 'accounting', 'author.last_name',],
      relations: ['accounting', 'author', 'user'],
      nullSort: 'last',
      defaultSortBy: [['uuid', 'DESC']],
      searchableColumns: ['uuid', 'name', 'count', 'author.last_name', 'author.first_name', 'author.surname', 'user.last_name', 'user.first_name', 'user.surname', 'series', 'inventory',],
      filterableColumns: {
        count: [FilterOperator.EQ, FilterOperator.BTW],
        accounting: true,
        author: true,
        user: true
      },
    });

You can achieve this using where in pagination config

MrSharpp commented 1 month ago

@ppetzold What about ordering with the count of relation?