version: 'v2.5.1'
This is my demo of a RESTful API implementing Robert Martin's clean architecture in TypeScript. I do not use any third party packages for inversion of control, like InversifyJS. I use dependency injection in order to maintain a one-way flow of dependency. I also use interfaces to satisfy Bob Martin's requirement of "use case output/input ports", which are then implemented by their use cases.
- Web Delivery Mechanism
- Express.js
- Database Repositories
- In-memory
- Sequelize
- MariaDB
- PostgreSQL
- External Services
- jsonplaceholder (example)
I tried to organize the application's files and folders according to this diagram:
These are the routes this API uses:
/users |
|||
---|---|---|---|
Method | Route | Description | |
GET | /api/v1/users/:id |
get one user | |
GET | /api/v1/users |
get all users | |
DELETE | /api/v1/users/:id |
delete one user | |
PATCH | /api/v1/users:id |
update one user | |
POST | /api/v1/users |
create one user |
/products |
|||
---|---|---|---|
Method | Route | Description | |
GET | /api/v1/products/:id |
get one product | |
GET | /api/v1/products |
get all products | |
DELETE | /api/v1/products/:id |
delete one product | |
PATCH | /api/v1/products/:id |
update one product | |
POST | /api/v1/products |
create one product |
/orders |
|||
---|---|---|---|
Method | Route | Description | |
GET | /api/v1/orders/:id |
get one order | |
GET | /api/v1/orders |
get all orders | |
DELETE | /api/v1/orders/:id |
delete one order | |
PUT | /api/v1/orders/:id |
update one order | |
POST | /api/v1/orders |
create one order |
/images |
|||
---|---|---|---|
Method | Route | Description | |
GET | /api/v1/images/:id |
get one image | |
GET | /api/v1/images |
get all images | |
DELETE | /api/v1/images/:id |
delete one image | |
POST | /api/v1/images/:id/update |
update one image | |
POST | /api/v1/images |
create one image |
- Node.js
- TypeScript
- MariaDB or PostgreSQL
git clone
cd clean-architecture-model-ts
npm install
In the .env
file, update the following environment variables for sequelize to work correctly.
- DB_DIALECT
- (Enter "inMemory" to use the in-memory database)
- DB_NAME
- DB_USERNAME
- DB_PASSWORD
- DB_HOST
- DB_PORT
- PORT
- (Defaults to 3000)
To run in production mode where code is transpiled into a dist
folder and run directly in node
:
npm start
To run in development mode where code is run by ts-node-dev and re-transpiled any time there is a change:
npm run dev:watch