ppetzold/nestjs-paginate

Nested `relationships` in `PaginateConfig` not working

Closed this issue ยท 6 comments

Helveg commented

Are nested relationships supported in the relations field of PaginateConfig? Whenever I have a nested relationship such as bankAccount.organization it throws the following error:

[Nest] 8412  - 07/09/2023, 19:29:04   ERROR [ExceptionsHandler] Relation with property path bankAccount.organization in entity was not found.
TypeORMError: Relation with property path bankAccount.organization in entity was not found.
    at JoinAttribute.getValue (C:\Users\pwd06\git\graag-gedaan\src\query-builder\JoinAttribute.ts:187:23)
    at JoinAttribute.get relation [as relation] (C:\Users\pwd06\git\graag-gedaan\src\query-builder\JoinAttribute.ts:191:53)
    at JoinAttribute.get metadata [as metadata] (C:\Users\pwd06\git\graag-gedaan\src\query-builder\JoinAttribute.ts:203:18)
    at SelectQueryBuilder.join (C:\Users\pwd06\git\graag-gedaan\src\query-builder\SelectQueryBuilder.ts:2038:53)
    at SelectQueryBuilder.leftJoin (C:\Users\pwd06\git\graag-gedaan\src\query-builder\SelectQueryBuilder.ts:467:14)
    at SelectQueryBuilder.leftJoinAndSelect (C:\Users\pwd06\git\graag-gedaan\src\query-builder\SelectQueryBuilder.ts:607:14)
    at C:\Users\pwd06\git\graag-gedaan\node_modules\nestjs-paginate\src\paginate.ts:259:26
    at Array.forEach (<anonymous>)
    at paginate (C:\Users\pwd06\git\graag-gedaan\node_modules\nestjs-paginate\src\paginate.ts:258:26)
    at PayoutsController.findAll (C:\Users\pwd06\git\graag-gedaan\src\core\resources\resource.controller.ts:185:30)

The code boils down to this:

      async findAll(@Paginate() query: PaginateQuery) {
        return await paginate(query, this.service.repo, {
          ...this.options.pagination,
          relations: ['any.nested.relation']
        });
      }

that seems to be a pretty basic usecase so I'm asking here as a sanity check. It happens with whichever nested relationship, and the same relationships do work when I use them in TypeORM myself.

try relations: { any: { nested: { relation: true } } }

Helveg commented

It doesn't appear to work, no error is thrown but no nested relationships are loaded either:

      async findAll(@Paginate() query: PaginateQuery) {
        return await paginate(query, this.service.repo, {
          ...this.options.pagination,
          relations: {bankAccount: {organization: true}}
        });
      }

but the returned page is:

Paginated {
  data: [
    Payout {
      id: 'b18b40f3-08ac-428e-9712-ada46fd0a4a5',
      createdAt: 2023-09-07T14:50:43.000Z,
      updatedAt: 2023-09-07T14:50:43.000Z,
      deletedAt: null,
      amount: null,
      paidAt: null
    },
...

Passing that same nested object {bankAccount: { organization: true } } to a TypeORM relations field does load the relationships.

In any case, after figuring this out, would you accept a PR that lets users use the dotted notation as well?

In any case, after figuring this out, would you accept a PR that lets users use the dotted notation as well?

Yep. Certainly, I actually thought the above was an alternate typeORM definition as well. If not, then I would almost say we drop it and adapt the typeORM way only in a breaking change.

Helveg commented

The problem was resolved by updating my version (sorry about that).

I also reviewed the TypeORM docs and the object notation is the the only mentioned notation. On StackOverflow users answer with both, so somehow this dotted notation does circulate. I'd propose for my support to add non-breaking support for the dot notation then.

The dot notation (relations: string[]) does have official support, and we could use the same utility snippet they use to convert between them:

                const relations = Array.isArray(this.findOptions.relations)
                    ? OrmUtils.propertyPathsToTruthyObject(
                          this.findOptions.relations,
                      )
                    : this.findOptions.relations

https://github.com/typeorm/typeorm/blob/befe4f9cbf17139bfaba83ae8a00e56b071df334/src/query-builder/SelectQueryBuilder.ts#L3097-L3101

I'll start on the PR

sounds good to me ๐Ÿš€

๐ŸŽ‰ This issue has been resolved in version 8.3.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€