nestjsx / crud

NestJs CRUD for RESTful APIs

Home Page:https://github.com/nestjsx/crud/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Association endpoints not exposed

jmelich opened this issue · comments

Given the following Plant entity:

@Entity()
export class Plant extends BaseEntity {
  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({
    nullable: true,
    default: '',
  })
  location: string;

  @Column({
    nullable: true,
    default: 0,
  })
  power: number;

  
  @ManyToOne((type) => User, (user) => user.plants)
  owner: User;

  @OneToMany((type) => Device, (device) => device.plant)
  device: Device[];
}

that entity has two relations, and per documentation should expose /owner and /device routes, but only the following endpoints are exposed or generated:

[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants/:id, GET} route +1ms
[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants, GET} route +1ms
[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants, POST} route +0ms
[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants/bulk, POST} route +1ms
[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants/:id, PATCH} route +1ms
[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants/:id, PUT} route +0ms
[Nest] 10726  - 03/06/2022, 1:10:51 AM     LOG [RouterExplorer] Mapped {/plants/:id, DELETE} route +1ms

GET /plants/1/owner

{
    "statusCode": 404,
    "message": "Cannot GET /plants/1/owner",
    "error": "Not Found"
}

I missing something or is required to do something extra to get that endpoints exposed?

Thanks in advance!

commented

Perhaps /entity/:id/something doesn't work for ManyToOne relations. You could add the join in your controller:

@Crud({
 ...
  query: {
    join: {
      owner: { eager: true },
  },
})

And then get a plant with her owner joined in: /plants/1

Eager: true will mean the owner is also joined when you request /plants. If you don't want that, remove eager: true and add joins in the http request when you do want the owner to be joined.

Careful!!! Users will now possibly have access to sensitive user data: The owner's password hash, name, e-mail etc.. You need to add more filtering on the backend to remove all that.

Actual for me as well. Version 4.6.2. Adding join does not help 😞

I'm looking for a solution to post/patch entity with relations and I am facing this problem too.

Perhaps /entity/:id/something doesn't work for ManyToOne relations.

Why the device field is not mapped as a route? It's a OneToMany

any news about this?

image

i am in a similar problem trying to consume /resource/:id/anotherResource i can see mapped routes when the api is running and the endpoints are register but if i try go consume them with post i always got 404

i am using Nest "@nestjs/common": "^9.0.0", any suggestions?

any news about this?

image

i am in a similar problem trying to consume /resource/:id/anotherResource i can see mapped routes when the api is running and the endpoints are register but if i try go consume them with post i always got 404

i am using Nest "@nestjs/common": "^9.0.0", any suggestions?

i did it, just make sure your config is working properly on main.ts and in your routes you can define into Decorators like Get, Post, Put, etc your path as complex as you need for example based on your controller base route you can define in a get method something like this Get(':id/resource2/:resourceId2') and you will get this endpoint --> /controllerBase/:id/resource2/:resourceId2

i hope this help someone