• Stars
    star
    150
  • Rank 247,323 (Top 5 %)
  • Language
    TypeScript
  • Created over 2 years ago
  • Updated over 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).