Skip to content

rasouza/node-clean-architecture

Repository files navigation

Code Coverage

Node Clean Architecture

Preview

Table of Contents

This backend implements a RESTful CRUD interface for users and complies with Eric Evan's DDD and Uncle Bob's Clean Architecture which is briefly explained here in the document.

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

Libs

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

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

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

rasouza/node-clean-architecture

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 v10.0.1 yarn.lock
@types/jest v28.1.1 yarn.lock
cross-env v7.0.3 yarn.lock
dotenv v8.2.0 yarn.lock
eslint-config-standard v16.0.2 yarn.lock
eslint-plugin-import v2.22.1 yarn.lock
eslint-plugin-jest v26.5.3 yarn.lock
eslint-plugin-node v11.1.0 yarn.lock
eslint-plugin-promise v4.2.1 yarn.lock
eslint-plugin-standard v5.0.0 yarn.lock
mongoose v5.10.3 yarn.lock
nyc v15.1.0 yarn.lock
pino v6.9.0 yarn.lock
pino-pretty v4.3.0 yarn.lock
semantic-release v19.0.2 yarn.lock

Generated via Stack Reports