Menu

Blog

DDD Hapi.js Project Structure and Architecture

20th March 2019 / In Javascript Domain Driven Development

In order for Domain Driven Design to be useful in your team, you must be able to have the correct environment in order to achieve this. One of the important steps in order for your project to be successful is to structure your project in away that allows for these interactions. By all means this structure is not set in stone but I can say it has helped me quite a lot. Firstly, in order for the benefits of ubiquitous language to take effect, we must change the structure for common project structures I’ve seen. Not saying that these structures are incorrect, but I feel like they miss the point of trying to make it easy for the everyday domain expert or other members of the team. Instead of making the structure as technical structure (models, services, repositories etc), make it by domain. Now, if you feel your rest API is very micro-esq (dealing with a simple thing), maybe the structure in technical terms make sense. But if your API deals with various sub domains it is better to divide them by said sub domains. Each sub domain can then be its own hapi.js plugin. In my case I use Schemervice for both application/domain services and repositories. A problem arises because there might be services that are used various times and this can cause repetition. Next I will be trying to divide that into its own plugin so it can be a dependency and be used in all plugins. For now what I do is create separate repositories for each sub domain. This way I can have different functions and develop each sub domain API separate. this has its share of advantages and disadvantages. . Another advantage with having them being their own plugins is that you can scale them separately which is a benefit you get by using domain driven development.

In terms of test, I have all of the necessary files for running the tests using factories and such and then a separate folder for the actual tests. For now I have the tests divided by subdomain but as more complexity comes I will probably divide it into scenarios so the files aren’t too big.

You can my project structure at https://github.com/captain-yuca/spotshare-backend