beeman / nestjs-typeorm-nx

14 stars 5 forks source link

Circular Dependencies #4

Open johannesschobel opened 5 years ago

johannesschobel commented 5 years ago

Dear @beeman ,

again, i am sorry to bother you. I ran into an issue I would like to ask you about your opinion. In #2 , we already discussed about the overall project structure (how to build libs, apps should be "empty shells" and so on).

I then began to restructure my api (built with nest) and move everything into dedicated libs. So i created an libs/api/auth, an libs/api/user, ... library and link them together in a lib/api/main lib that may then be imported in the apps/api project. So far so good.

However, i quickly ran into "circular dependency" issues. Consider the following example: I have the 2 libs api/auth and api/profile both with their respective models. Thereby, one Account has one Profile assigned (i.e., there is a @OneToOne(...) relationship between them!) From the "data model" perspective, this is entirely valid and true, but boom - there you have your circular dependency!

How do you deal with such issues? For me it makes sense to store the entities all in one place (i.e., in their respective library). However, this dramatically increases the connections / dependencies between the libraries, making it extremely hard to deal with circular dependencies.

What is your approach? All the best and cheers

johannesschobel commented 5 years ago

To clarify the issue described above: If i run ng serve api, i get the following output:

WARNING in Circular dependency detected:
libs\api\auth\src\index.ts -> 
libs\api\auth\src\lib\api-auth.ts -> 
libs\api\auth\src\lib\auth.module.ts -> 
libs\api\auth\src\lib\controllers\auth.controller.ts -> 
libs\api\auth\src\lib\services\auth.service.ts -> 
libs\api\auth\src\lib\data\entities\account.entity.ts -> 

libs\api\profile\src\index.ts -> 
libs\api\profile\src\lib\api-profile.ts -> 
libs\api\profile\src\lib\profile.module.ts -> 
libs\api\profile\src\lib\controllers\myself.controller.ts -> 
libs\api\profile\src\lib\services\myself.service.ts -> 
libs\api\profile\src\lib\services\profile.service.ts -> 
libs\api\profile\src\lib\data\entities\profile.entity.ts -> 

libs\api\auth\src\index.ts

formatted for better readability