Code Monkey home page Code Monkey logo

nest-clean-architecture's Introduction

Nest Logo

NestJS Clean Architecture Example

license

Introduction

Clean Architecture is a software design pattern that separates the code into multiple concentric circles of responsibility, where the innermost circle contains the business logic and the outermost circle contains the delivery mechanism. This structure helps to ensure that the code is maintainable, testable, and scalable over time.

Folders Structure

The following is a typical folder structure for implementing Clean Architecture in a NestJS application:

.
├── + core
│   ├── + base
│   │   ├── - entity.ts
│   │   ├── - mapper.ts
│   │   ├── - repository.ts
│   │   └── - use-case.ts
│   │
│   ├── + domain
│   │   ├── + entities
│   │   └── + mappers
│   │
│   └── + repositories
│
├── + infra
│   ├── + data
│   └── + framework
│
├── + shared
│   └── + dtos
│
└── + use-cases
  • core: contains the business logic of the application, such as entities, use cases, and interfaces.

  • infra: contains the implementation of the interfaces defined in the core, such as data sources and services.

  • shared: contains code that can be shared between multiple modules, such as models and DTOs.

  • use-cases: contains the implementation of the use cases defined in the core, such as controllers and middleware.

Core Folder

The core folder contains the core logic of the application, such as entities, use cases, and interfaces. It should have no knowledge of the delivery mechanism, such as the HTTP API or the database.

Entities

Entities are the objects that represent the business domain, such as user, product, or order. They should be defined in a plain TypeScript class, with properties and methods that match the business requirements.

Use Cases

Use cases are the actions that can be performed in the application, such as creating a user, retrieving a product, or processing an order. They should be defined as interfaces, with a clear definition of the inputs and outputs.

Repositories

Repositories are the contracts that define the expected behavior of the delivery mechanism. They must be defined in the core, without implementation details.

Infra Folder

The infra folder contains the implementation of the interfaces defined in the core, such as data sources and services. It should have no knowledge of the business logic.

Data Sources

Data sources are the objects that access the data storage, such as a database or a REST API. They should implement the interfaces defined in the core, with the specific implementation details.

Shared Folder

The shared folder contains code that can be shared between multiple modules, such as models and DTOs. It should have no knowledge of the business logic or the delivery mechanism.

DTOs

DTOs are the objects that represent the data transfer between different layers of the application, such as a user DTO or a product DTO. They should be defined in a plain TypeScript class, with properties and methods that match the data requirements.

Use-Cases Folder

The use-cases folder contains the implementation of the use cases defined in the core, such as controllers and middleware. It should have no knowledge of the business logic.

Installation

$ yarn install

Running the app

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

Test

# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov

License

This project is licensed under the terms of the MIT license.

nest-clean-architecture's People

Contributors

wesleey avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.