Open Jtosbornex opened 4 years ago
Here is a working example of nested routes :) https://github.com/shinework/nest-crud-nested-controller
@shinework I appreciate the example. This is very helpful.
So I see two main takeaways from your articles controller:
@Controller('/authors/:id/articles')
... params: { id: { field: 'author', type: 'number', }, }, ...
My only question for you now is this eager join necessary in the author controller?
under
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]
🤘
@shinework thank you for this minimal example. I will definitely be able to use this pattern now because of your comments!
@shinework Thank you for example. Did you see something similar for mongoose and MongoDB?
What's the point in creating an additional field?
I need it to work normally with JoinColumn
:
Is that possible?
@avchugaev
I think you need to explicitly add in the following:
@Column() school_id:number;
@Jtosbornex thanks, it solved my problem. But to be honest, the solution is a bit unexpected. 😉
Hi @shinework , loved your answer. But I got a problem in not equal to scenario. e.g. I want records where authorId is not equal (!=) to the params.authorId. or how do I use params value in query.filter?
@talhazafar444 so you want the route
GET /authors/:authorId/articles
to give you all of the articles for authors that do not have id = :authorId ?
This seems like it would make more sense to do on the articles getMany endpoint
GET /articles
Then you can you use the requests library to create your query params
https://github.com/nestjsx/crud/wiki/Requests#filter
or use something like this if you are constructing the URL manually.
GET /articles?filter=authorId||$neq||12345
where 12345 is the id for the author you dont want.
does it works for M2M relations?
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:
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:
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.
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.