Correct use of ManyToMany relations #261
-
Hi, Now I am working on a ManyToMany relation with a @jointable, and actually expected nestjs-query to handle (CRUD) this relation automatically, using the JoinTable. But when inserting a new Event Object, the relation with the User Object is not filled. Do I understand this wrong? Or am I missing something? Here is my basic code example: //user.entity.ts
//user.dto.ts
//event.entity.ts
//event.dto.ts
Do I need to write a custom resolver to handle the ManyToMany relation between Event and User (Participants)? Grateful for any advice or hints how to make this work 🙏🏼 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
If you are doing many to many without the TypeORM's |
Beta Was this translation helpful? Give feedback.
-
Finally managed to map the relation correctly by using a custom hook 🌈 The issue is that I receive an array of participantIds and first need to fetch the participants based on these ids, for TypeORM to be able to make the proper relation in the JoinTable. This is done now by defining the ManyToMany relation in the Event entity like this:
Defining a custom hook event-participant.hook.ts, which catches the mutation and fetches the corresponding participants (Users) for the given ID'S:
registering the Hook and UserModule in event.module.ts:
And finally applying the hook to my event.dto.ts to catche the mutation and map the participants:
now I am able to retrieve the correct relation in my ObjectType:
Works like a charm! 🚀 |
Beta Was this translation helpful? Give feedback.
Finally managed to map the relation correctly by using a custom hook 🌈
The issue is that I receive an array of participantIds and first need to fetch the participants based on these ids, for TypeORM to be able to make the proper relation in the JoinTable.
This is done now by defining the ManyToMany relation in the Event entity like this:
Defining a custom hook event-participant.hook.ts, which catches the mutation and fetches the correspond…