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

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

Open
kenvals opened this issue May 5, 2024 · 3 comments
Open

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

kenvals opened this issue May 5, 2024 · 3 comments

Comments

@kenvals
Copy link

kenvals commented May 5, 2024

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
Copy link

MrSharpp commented May 17, 2024

Please add a support to sort by relation count

@ppetzold
Copy link
Owner

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
Copy link

@ppetzold What about ordering with the count of relation?

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

3 participants