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.

Include and filter by properties in nested relations

See original GitHub issue

Hi,

I’m trying to get relations to work with nested relations. Let’s say I have entities A, B, and C. A has a Many-to-One to B, and B has a One-to-One to C. If I am using the pagination on A, but trying to filter by values on C, it looks like I can’t. relations option seems to only accept “surface” relations, and I’ve been trying to build a custom query builder but even if I can manage to include these relations, filtering seems to still be broken.

Am I missing something? Thanks.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
emriverocommented, Jun 3, 2022

@FlyingOddo @ppetzold Hey! Apologies for forgetting to do the PR. As I had to make some more changes I have to check that the tests are passing I will do it as soon as possible.

2reactions
thegriglatcommented, Jul 27, 2022

Hi all, it seems I found a workaround for nested relations

The main idea is to use custom queryBuilder which will mask needed relation as core entity’s relation

Assume the following relations between entities: User -> Photos -> Comments and we need to filter by comments.likes property. When we will use comments.likes in filter or searchBy query – nestjs-paginate will try to find user.comments property in user_comments table name – and use our manual left join! PROFIT!

Snipper to illustrate the idea (at least smth like that works in my project):

const qb = this.userRepository
      .createQueryBuilder('user')
      .leftJoinAndSelect('user.photos', 'user_photos')
      // fake user.photos.comments relation, so paginate will assume that comments is user's property
      .leftJoinAndSelect('user_photos.comments', 'user_comments')

    await paginate<User>(request, qb, {
      relations: ['photos'],
      filterableColumns: {
        // use comments.likes as paginate will append `user_` prefix and will use left join above
        'comments.likes': [FilterOperator.EQ, FilterOperator.NOT, FilterOperator.IN],
        // any needed here as comments.likes isn't user property and otherwise will break TS types
      } as any,
    })
Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Filtering with EF Core through multiple nested object ...
I assume you have relation between Reports and UserReportFilter tables since you have ReportId in UserReportFilter table.
Read more >
How to filter nested relations · Discussion #2980 · mikro-orm ...
How to filter nested relations. ... The bookings received should only be the bookings that have the property createdBy equal to the provided...
Read more >
Relation queries (Concepts) - Prisma
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Read more >
Filter on (nested) relationship attribute? - JSON API
Another I'm wondering is how to filter on nested relationships. ... The value of the include parameter MUST be a comma-separated (U+002C ...
Read more >
Filtering relations / nested relations in Notion : r/Notion - Reddit
I want to have the following properties: a. name - no problem here b. client (relation) - no problem here either
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