• Stars
    star
    137
  • Rank 257,432 (Top 6 %)
  • Language
    TypeScript
  • Created about 2 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Clean Architecture: TypeScript ExpressΒ API

By employing clean architecture, you can design applications with very low coupling and independent of technical implementation details. That way, the application becomes easy to maintain and flexible to change. Clean architecture allows us to create architectural boundaries between dependencies which allows components to be swapped in and out and be intrinsically testable.

The Plan

At a high level, we'd like our API to be structured in the following way

Folder Structure

Let's use files and folders to structure our application. Doing this allows us to communicate architecture intent:

/src
│── main.ts
│── server.ts
│── presentation
β”‚   └── routers
β”‚       └── contact-router.ts
β”œβ”€β”€ domain
β”‚   β”œβ”€β”€ interfaces
β”‚   β”‚   β”œβ”€β”€ repositories
β”‚   β”‚   β”‚    └── contact-repository.ts
β”‚   β”‚   └── use-cases
β”‚   β”‚       └── contact
β”‚   β”‚           β”œβ”€β”€ create-contact-use-case.ts
β”‚   β”‚           β”œβ”€β”€ delete-contact-use-case.ts
β”‚   β”‚           β”œβ”€β”€ get-all-contacts-use-case.ts
β”‚   β”‚           β”œβ”€β”€ get-one-contacts-use-case.ts
β”‚   β”‚           └── update-contact-use-case.ts
β”‚   β”œβ”€β”€ models
β”‚   β”‚   └── contact.ts
β”‚   β”œβ”€β”€ repositories
β”‚   β”‚   └── contact-repository.ts
β”‚   └── use-cases
β”‚       └── contact
β”‚           β”œβ”€β”€ create-contact.ts
β”‚           β”œβ”€β”€ delete-contact.ts
β”‚           β”œβ”€β”€ get-all-contacts.ts
β”‚           β”œβ”€β”€ get-one-contacts.ts
β”‚           └── update-contact.ts
└── data
    β”œβ”€β”€ interfaces
    β”‚   └── data-sources
    β”‚       β”œβ”€β”€ nosql-database-wrapper.ts
    β”‚       β”œβ”€β”€ sql-database-wrapper.ts
    β”‚       └── contact-data-source.ts
    └── data-sources
        β”œβ”€β”€ mongodb
        β”‚   └── mongodb-contact-data-source.ts
        └── postgresl
            └── pg-contact-data-source.ts

The presentation layer would mainly be used for inputting and outputting user data (API routes).

The inner core domain layer holds all business logic (use cases, repositories).

The data layer holds all infrastructure implementations (data sources).