A thing that has been bothering me for a while is how to separate related aggregate roots into modules. For example, given a documents and an authors module, and the following endpoints:
/documentsto list all documents/authorsto list all authors/documents/:id/authorsto list authors of a document/authors/:id/documentsto list documents of an author
How would we separate this into modules? I tend to think about NestJS modules as potentially being their own microservice.
Option 1
documentsmodule containsDocumentsControllerwith both/documents*endpoints andDocumentsRepository. TheDocumentsRepositoryhas methodslistandlistAuthorsForDocument(id)authorsmodule containsAuthorsControllerwith both/authors*endpoints andAuthorsRepository. TheAuthorsRepositoryhas methodslistandlistDocumentsForAuthor(id)
This seems wrong, because the DocumentsRepository is purely meant for fetching and saving Document domain objects.
Option 2
documentsmodule containsDocumentsControllerwith both/documents*endpoints andDocumentsRepository. TheDocumentsRepositoryhas methodslistandlistByAuthor(id)authorsmodule containsAuthorsControllerwith both/authors*endpoints andAuthorsRepository. TheAuthorsRepositoryhas methodslistandlistByDocument(id)
The documents module provides the AuthorsRepository so that /documents/:id/authors could use the listByDocument method. The authors module provides the DocumentsRepository so that /authors/:id/documents could use the listByAuthor method.
Option 3
documentsmodule containsDocumentsControllerwhich has the/documentsand/authors/:id/documentsendpoints andDocumentsRepository. TheDocumentsRepositoryhas methodslistandlistByAuthor(id).authorsmodule containsAuthorsControllerwhich has the/authorsand/documents/:id/authorsendpoints andAuthorsRepository. TheAuthorsRepositoryhas methodslistandlistByDocument(id).
I guess I prefer option 3 as there are no imports between modules but it seems to into NestJSs way of defining endpoints (e.g. a @Controller('/documents') with endpoints @Get() for /documents and @Get(':id/authors') for /documents/:id/authors.
What are reasons to prefer on of these methods over the other or is there a better way?
It does not matter but if you want to be more consistent, here is another idea.
Module A
Module B
Module AB
This implementation might make your code more consistent.