This project is a TypeScript application using Express, designed with a Hexagonal Architecture (Ports and Adapters). It demonstrates the separation of concerns, making the application more maintainable, testable, and scalable.
- Scaffold Hexagonal Pattern TS
- Table of Contents
- Installation
- Usage
- Project Structure
- Endpoints
- Running Tests
- Contributing
- License
-
Clone the repository:
git clone https://github.com/j-e-0/scaffold-hexagonal-pattern-ts cd scaffold-hexagonal-pattern-ts
-
Install the dependencies:
npm install
-
Create a
.env
file in the root directory and add your environment variables:PORT=3000 DB_HOST=localhost DB_USER=user DB_PASSWORD=user DB_NAME=database
-
Start the application:
npm run start
-
The API will be running at
http://localhost:3000
.
project/
├── src/
│ ├── app/
│ │ ├── api/
│ │ │ ├── v1/
│ │ │ │ ├── userRouter.ts
│ │ │ │ ├── productRouter.ts
│ │ │ │ ├── schemas/
│ │ │ │ │ ├── UserSchema.ts
│ │ │ │ │ ├── ProductSchema.ts
│ │ │ └── index.ts
│ │ ├── config/
│ │ │ ├── index.ts
│ │ ├── middlewares/
│ │ │ ├── validate.ts
│ │ │ ├── validateNumber.ts
│ │ └── main.ts
│ ├── domain/
│ │ ├── interfaces/
│ │ │ ├── ProductRepositoryInterface.ts
│ │ │ ├── UserRepositoryInterface.ts
│ │ ├── models/
│ │ │ ├── User.ts
│ │ │ ├── Product.ts
│ │ ├── services/
│ │ │ ├── UserService.ts
│ │ │ ├── ProductService.ts
│ ├── infrastructure/
│ │ ├── db/
│ │ │ ├── index.ts
│ │ │ ├── database.ts
│ │ │ ├── testDatabase.ts
│ │ │ ├── models/
│ │ │ │ ├── Product.ts
│ │ │ │ ├── User.ts
│ │ │ ├── repositories/
│ │ │ │ ├── UserRepository.ts
│ │ │ │ ├── ProductRepository.ts
│ ├── tests/
│ │ ├── user.test.ts
│ │ ├── product.test.ts
├── .env
├── package.json
├── tsconfig.json
└── README.md
-
app/: Application layer containing API routes and main configuration.
- api/: API routes and controllers.
- schemas/: Schemas for data validation for API contract.
- config/: Main application configurations.
- middlewares/: Middlewares configurations to validate entry endpoints.
- main.ts: Application entry point.
- api/: API routes and controllers.
-
domain/: Domain layer containing business logic.
- interfaces/: Domain interfaces representing communication and inverse responsabilities.
- models/: Domain models representing business entities.
- services/: Domain services containing business logic.
-
infrastructure/: Infrastructure layer containing database interactions.
- db/: Database configuration and repository implementations.
-
tests/: Test files for unit and integration tests.
-
POST /api/v1/users: Create a new user
-
Request Body:
{ "username": "john", "email": "[email protected]", "password": "password" }
-
-
GET /api/v1/users/:id: Get a user by ID
-
POST /api/v1/products: Create a new product
-
Request Body:
{ "name": "Product1", "price": 100 }
-
-
GET /api/v1/products/:id: Get a product by ID
-
To run the tests, use the following command:
npm run test
Contributions are welcome! Please fork the repository and create a pull request with your changes.
This project is licensed under the MIT License.