clean-architecture

Structure NestJS/TypeScript backends with Clean Architecture, DDD, and Hexagonal layers.

Updated Mar 5, 2026
One-click install
npx skills add https://github.com/truongnat/emplus --skill clean-architecture-truongnat
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: clean-architecture
Source: https://github.com/truongnat/emplus/tree/main/.agents/skills/clean-architecture
Command: npx skills add https://github.com/truongnat/emplus --skill clean-architecture-truongnat

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Clean Architecture, DDD, and Hexagonal Architecture solve the problem of tightly-coupled NestJS backends that become hard to test, refactor, and evolve as business complexity grows.

Core Features & Use Cases

  • Layered architecture with a strict dependency rule: Keep the Domain core independent of NestJS, TypeORM, and HTTP concerns so changes don’t ripple across the system.
  • DDD modeling with entities, value objects, aggregates, and domain events: Encapsulate business rules in rich domain models and emit events when important state transitions occur.
  • Ports & adapters with application use cases: Define repository/service contracts in the domain (ports) and implement infrastructure details in adapters, wiring everything via NestJS dependency injection.
  • Practical NestJS/TypeScript structure: Organize src folders into domain, application, infrastructure, and adapters to make implementation consistent and discoverable.
  • Testability and refactoring support: Enable fast pure unit tests for domain logic and reduce “anemic domain model” regressions during incremental refactors.

A practical example is refactoring an existing NestJS CRUD module into a layered design by moving business rules into aggregates and replacing direct ORM dependencies with repository ports and persistence adapters.

Quick Start

Use the clean-architecture skill to restructure your NestJS feature into domain entities/value objects, application use cases, and ports/adapters with dependency inversion.

Frequently Asked Questions about clean-architecture

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure a NestJS backend using Clean Architecture and Domain-Driven Design?

To structure a NestJS backend with Clean Architecture and DDD, organize your source code into domain, application, infrastructure, and adapter layers. Keep the domain core independent of NestJS and TypeORM, encapsulating business rules in rich entities and use-case orchestration.

What is the best way to refactor an anemic domain model in a TypeScript backend?

Refactoring an anemic domain model in TypeScript involves moving business rules out of services and into rich domain aggregates. You enforce invariants within these aggregates and define repository ports in the domain, implementing infrastructure details in outer adapters.

How do ports and adapters work in a NestJS application?

Ports and adapters in a NestJS application work by defining repository and service contracts, known as ports, within the dependency-free domain core. Infrastructure details are implemented as adapters in outer layers and wired into the application via NestJS dependency injection.

Can I use Clean Architecture to make my NestJS modules more testable?

Yes, you can use Clean Architecture to make NestJS modules highly testable. By isolating domain logic from TypeORM and HTTP concerns, you enable fast pure unit tests for domain rules and reduce regressions during incremental refactors of tightly coupled features.

When should I use Hexagonal Architecture for my backend?

You should use Hexagonal Architecture when building complex backend features that require loosely coupled, testable systems with clear bounded contexts. It is ideal for evolving NestJS projects where business complexity makes tightly coupled code hard to maintain.

Does Clean Architecture support domain events in NestJS?

Yes, Clean Architecture supports domain events in NestJS by encapsulating business rules in rich domain models and emitting events when important state transitions occur. This maintains a strict dependency rule while allowing infrastructure adapters to react to domain changes.