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.

Problems with nested controller routes / nested route params

See original GitHub issue

I have used this library in conjunction with nestjs for almost a year now. Nest by istelf for 2, so I am fairly familiar with the API of both. Thank you for this work as it has been very nice to use overall.


https://github.com/nestjsx/crud/wiki/Controllers#get-one-resource

First off I think there may be a bug in the documentation:

image

It looks like you are missing a forward slash after perks.


Secondly, I have not been able to get nested routes to work at any point. An example of a nested route being something such as this:

/heroes/:heroId/perks/:id

The documentation kind of explains for this case:

If you have a controller path with that looks kinda similar to this /companies/:companyId/users you need to add this param option: @Crud({ ... params: { ... companyId: { field: 'companyId', type: 'number' }, }, ... })

But it doesn’t show you what the entities look like on the back end to make this happen. I think this is of key importance to get this to work.

  • Does one need to name id fields like ‘companyId’ in the entity or can ‘id’ suffice?
  • How deeply nested can one go using this pattern?
  • What does a complete example of this pattern look like?

This part of the documentation is seriously lacking. If I could get this feature to work I would consider adding a PR, but like I said, I have yet to be able to use this feature.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:5
  • Comments:11

github_iconTop GitHub Comments

4reactions
shineworkcommented, Sep 29, 2020

Hello @Jtosbornex! No, the eager join is not mandatory. Checkout my latest commit because there was an issue in my previous commit :

https://github.com/shinework/nest-crud-nested-controller

@Crud({
  model: {
    type: Article,
  },
  params: {
    authorId: {
      field: 'authorId',
      type: 'number',
    },
  },
})
@ApiTags('articles')
@Controller('/authors/:authorId/articles')
export class ArticleController implements CrudController<Article> {
  constructor(public service: ArticleService) {}
}

And you have to explicitly add a authorId field :

import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'
import { Author } from '../author/author.entity'
import { ApiProperty } from '@nestjs/swagger'

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  id: number

  @Column('text')
  content: string

  @ApiProperty({ type: () => Author })
  @ManyToOne(
    () => Author,
    author => author.articles,
  )
  author: Author

  @Column()
  authorId: number
}

Calling http://localhost:3000/authors/1/articles/3 results to:

SELECT "Article"."id" AS "Article_id", "Article"."content" AS "Article_content", "Article"."authorId" AS "Article_authorId" 
FROM "article" "Article" 
WHERE ("Article"."authorId" = $1 AND "Article"."id" = $2) -- PARAMETERS: [1,3]

http://localhost:3000/authors/1/articles:

SELECT "Article"."id" AS "Article_id", "Article"."content" AS "Article_content", "Article"."authorId" AS "Article_authorId" 
FROM "article" "Article" 
WHERE ("Article"."authorId" = $1) -- PARAMETERS: [1]

🤘

1reaction
Jtosbornexcommented, Mar 21, 2021

@avchugaev

I think you need to explicitly add in the following:

@Column() school_id:number;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding params in nested routes - ruby on rails
Since I am overriding it I would want that that default appending of parent resource is off. I want my routes to look...
Read more >
A Guide to Using Nested Routes in Ruby | Scout APM Blog
A guide detailing how Ruby simplifies the system of creating and managing routes and resources.
Read more >
Implementation of Nested and Un-nested Routes ... - Medium
First Attempt at the Tenants Controller with Nested Routes ... For the un-nested path, the params hash will contain the key id ....
Read more >
Nested Resources
Nested Resources · Get the owner id from the route. Enter this code at the bottom of the controller: · Update the view...
Read more >
Nested Route Parameters in Rails - Jaco Pretorius
The Rails routing configuration allows you override the named parameters in your routes.
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