Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rasouza/node-clean-architecture

Clean Architecture implementation written in NodeJS
https://github.com/rasouza/node-clean-architecture

architecture backend clean-architecture nodejs

Last synced: about 2 months ago
JSON representation

Clean Architecture implementation written in NodeJS

Awesome Lists containing this project

README

        

![Code Coverage](https://img.shields.io/badge/coverage-96%25-green?style=flat-square)

# Node Clean Architecture

![Preview](preview.png)

## Table of Contents
- [Node Clean Architecture](#node-clean-architecture)
- [Table of Contents](#table-of-contents)
- [Libs](#libs)
- [Installation](#installation)
- [Testing](#testing)
- [Clean Architecture](#clean-architecture)
- [Folder structure](#folder-structure)
- [The Dependency Rule](#the-dependency-rule)
- [Typical Request](#typical-request)
- [Troubleshooting](#troubleshooting)
- [Log `connected to MongoDB database!` doesn't appear](#log-connected-to-mongodb-database-doesnt-appear)
- [I'm getting `EADDRINUSE` upon application start](#im-getting-eaddrinuse-upon-application-start)

This backend implements a [RESTful](https://restfulapi.net/) CRUD interface for users and complies with Eric Evan's [DDD](https://en.wikipedia.org/wiki/Domain-driven_design) and Uncle Bob's [Clean Architecture](https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) which is briefly explained here in the document.

It also exposes a `/docs/` endpoint for further reference and `/coverage/` for test coverage.

## Libs
* [Restify](http://restify.com/)
* [Mongoose](https://mongoosejs.com/)
* [Lodash FP](https://github.com/lodash/lodash/wiki/FP-Guide) Functional Programming version
* [Awilix](https://github.com/jeffijoe/awilix) as Dependency Injection container
* [dotenv](https://www.npmjs.com/package/dotenv)

## Installation

```
docker-compose up -d
cp .env.example .env
npm start
```
You should get
```
restify listening at http://[::]:8080
connected to MongoDB database!
```
Access http://localhost:8080/docs/ and http://localhost:8080/coverage/

## Testing

```
npm test
```
It uses an in-memory DB to run tests so you don't need to have mongodb up and running

## Clean Architecture

![Cleab Architecture](https://blog.cleancoder.com/uncle-bob/images/2012-08-13-the-clean-architecture/CleanArchitecture.jpg)

### Folder structure
```

└ application → Application services layer
└ use_cases → Application business rules
└ domain → Enterprise core business layer such as domain model objects (Aggregates, Entities, Value Objects) and repository interfaces
└ infrastructure → Frameworks, drivers and tools such as Database, the Web Framework, mailing/logging/glue code etc.
└ config → Application configuration files, modules and services
└ container.js → Module that manage service implementations by environment
└ database → Database ORMs middleware
└ schemas → Mongoose schemas
└ repositories → Implementation of domain repository interfaces
└ webserver → Restify Web server configuration (server, routes, plugins, etc.)
└ server.js → Restify server definition
└ ports/http → Adapters and formatters for use cases and entities to external agency such as Database or the Web
└ UserController.js → Restify route handlers
└ routes.js → Restify route definitions
└ errors.js → Standard errors for the whole application
└ index.js → Main application entry point
```

### The Dependency Rule

>The overriding rule that makes this architecture work is The Dependency Rule. This rule says that source code dependencies can only point inwards. Nothing in an inner circle can know anything at all about something in an outer circle. In particular, the name of something declared in an outer circle must not be mentioned by the code in the an inner circle. That includes, functions, classes. variables, or any other named software entity.

Extracted from https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html#the-dependency-rule

### Typical Request

![Request](architecture.jpg)

## Troubleshooting

### Log `connected to MongoDB database!` doesn't appear
The backend uses its own database (`users`) to run its business logic, so you need to ensure this database is created with proper user credentials. The script `mongo-init.js` is run when `docker-compose up` runs for the first time.

Check in `docker-compose logs mongo` to see if something unusual is happening

### I'm getting `EADDRINUSE` upon application start
You need port `8080` to be free in order to boot up the application. Check if it's already in use and shut the application down before you `npm start` again

# Stack Report
![](https://img.stackshare.io/repo.svg "repo") [rasouza/node-clean-architecture](https://github.com/rasouza/node-clean-architecture)![](https://img.stackshare.io/public_badge.svg "public")



|28
Tools used|3
Contributors|10/11/23
Report generated|06/06/22
Last commit date|
|------|------|------|------|

## Languages (2)


JavaScript


JavaScript


CSS 3


CSS 3


## Frameworks (1)


Fastify


Fastify


v3.9.2

## Data (1)


Mongoose


Mongoose


v5.10.3

## DevOps (7)


Dotenv


Dotenv


ESLint


ESLint


Git


Git


Jest


Jest


v28.1.0

Yarn


Yarn


nodemon


nodemon


v2.0.4

npm


npm


## Other (2)


Shell


Shell


semantic-release


semantic-release


## Open source packages (15)

## npm (15)

|NAME|VERSION|SOURCE FILE|
|------|------|------|
|[@semantic-release/git](https://github.com/semantic-release/git)|v10.0.1|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[@types/jest](http://definitelytyped.org/)|v28.1.1|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[cross-env](https://github.com/kentcdodds/cross-env)|v7.0.3|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[dotenv](https://github.com/motdotla/dotenv)|v8.2.0|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[eslint-config-standard](https://github.com/standard/eslint-config-standard)|v16.0.2|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import)|v2.22.1|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[eslint-plugin-jest](https://github.com/jest-community/eslint-plugin-jest)|v26.5.3|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[eslint-plugin-node](https://github.com/mysticatea/eslint-plugin-node)|v11.1.0|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[eslint-plugin-promise](https://github.com/xjamundx/eslint-plugin-promise)|v4.2.1|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[eslint-plugin-standard](https://github.com/standard/eslint-plugin-standard)|v5.0.0|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[mongoose](https://mongoosejs.com)|v5.10.3|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[nyc](https://github.com/istanbuljs/nyc)|v15.1.0|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[pino](http://getpino.io)|v6.9.0|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[pino-pretty](https://github.com/pinojs/pino-pretty)|v4.3.0|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|
|[semantic-release](https://github.com/semantic-release/semantic-release)|v19.0.2|[yarn.lock](https://github.com/rasouza/node-clean-architecture/blob/master/yarn.lock)|



Generated via [Stack Reports](https://stackshare.io/stack-report)