I follow the most common structure for nestjs where I have separate folder for each domain
src/
user/
controllers/
user.controller.ts
repositories/
user.repository.ts
services/
user.service.ts
user.module.ts
team/
// the same as above but for team entity
team.module.ts
app.module.ts
In my application, I have many-to-many relations between user and team. So, users can have many teams, as teams can have many users. For this purpose, I created table user_teams and an obvious separate repository for this table.
In most of places in my application I need to use functions like getAllUsersInTeam(idTeam: number) and getAllTeamsBelongsToUser(idUser: number) and I have implemented this functions in repository UserTeamRepository. My question is where the right place to keep this repository file? In which module? I have several ideas
Create a separate folder on the same level as the main domain entities
src/ user/ // user module files team/ // team module files user-team/ repositories/ user-team.repository.ts user-team.module.tsIn this case, potentially I will have a lot of folders for each many-to-many relations which will bring confusing and messing in the project.
Keep
UserTeamRepositoryin some of the modulesuserorteam.src/ user/ repositories/ user-team.repository.ts // or team/ repositories/ user-team.repository.tsIn this case I see problem with understading of the project. Why we keep one relations in one folder, but other we decided to keep in another way. How to fast search these kind of relations. I think new developers will be confused where find needed relations and where to keep new relations, cuz in good arhitecture should be no questions where to keep new class.
I'm open to any discussions and thoughts. Thanks for feedback in advance!