An open API service indexing awesome lists of open source software.

https://github.com/agungsptr/node-clean-ts

A TypeScript version of node-clean
https://github.com/agungsptr/node-clean-ts

clean-architecture express grpc mongodb nodejs typescript

Last synced: 2 months ago
JSON representation

A TypeScript version of node-clean

Awesome Lists containing this project

README

          




Node Clean TypeScript


A Node.js project template with clean architecture implementation


Explore the docs ยป














Table of Contents



  1. About The Project



  2. Getting Started



  3. Usage


  4. Contributing

  5. License

  6. Contact

  7. Acknowledgments

  8. Support Me

## About The Project
**This a TypeScript version of Node Clean**, if you want to see the JavaScript version follow this [way](https://github.com/agungsptr/node-clean).

This project aims to be used as a base backend project which implements clean architecture.
I create this project to help me build a backend application faster as a template project.
Feel free to use this project as your base application.
I am very happy if you are helped by using this template.

### Features

* RESTful
* gRPC
* Authentication using JWT
* CRUDS Resources
* Unit Test
* Coverage Test
* Load Test

### Built With

* [![NodeJS](https://img.shields.io/badge/node.js-6DA55F?style=for-the-badge&logo=node.js&logoColor=white)](https://nodejs.dev)
* [![Express](https://img.shields.io/badge/Express.js-404D59?style=for-the-badge&logo=express)](https://expressjs.com)
* [![Mongodb](https://img.shields.io/badge/MongoDB-4EA94B?style=for-the-badge&logo=mongodb&logoColor=white)](https://www.mongodb.com)
* [![Docker](https://img.shields.io/badge/docker-%230db7ed.svg?style=for-the-badge&logo=docker&logoColor=white)](https://www.docker.com)

## Getting Started

Let's start this project...

### Prerequisites

Before you dig into this project it very nice if you understand the clean architecture concept. I recommend you to read about the **Clean Architecture** first.

* Basic javascript
* VS Code (Recommend)
* Node >= v16.15.0
* Yarn
* GNU Make
* Docker

### Installation

1. Clone the repo

```sh
git clone https://github.com/agungsptr/node-clean-ts.git
```

2. Install Yarn packages

```sh
yarn install
```

3. Copy `.env.example` to `.env`

4. Setup `MongoDB` container

```sh
make infra
```

5. Seed database

```sh
make seed
```

6. Now you can run this project, this project running on port 7000 in your local

```sh
make dev
```

### Alternative Installation

Follow this instruction if you do not use `docker` or in `Windows` or if you having trouble with the installation above.

1. Clone the repo

```sh
git clone https://github.com/agungsptr/node-clean-ts.git
```

2. Make sure that you have installed `Node`, `Yarn`, and `Mongodb` in your local

3. Install Yarn packages

```sh
yarn install
```

4. Copy `.env.example` to `.env`

5. Setup `Mongodb`, for this you need to match your `Mongodb` to the `.env` file. To do that, please update the followings variable

```sh
- MONGO_HOST (your MongoDB host, ex. localhost if using your local machine)
- MONGO_PORT (your MongoDB port)
- MONGO_DBNAME (database name, you need to create it first in MongoDB)
- MONGO_USER (username to access your MongoDB)
- MONGO_PW (password for the user)
```

6. Seed database

```sh
yarn seed
```

7. Now you can run this project, this project running on port 7000 in your local

```sh
yarn dev
```

## Usage

### Web Services

* Login

```sh
curl --location --request POST 'localhost:7000/api/auth/login' \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "agungsptr",
"password": "24434"
}'
```

* Get All Users

```sh
curl --location --request GET 'localhost:7000/api/users' \
--header 'Authorization: Bearer '
```

For more request examples please use this postman collection [here](./NodeClean.postman_collection.json).

### gRPC

* Start gRPC Server

```sh
make grpc
```

After running the gRPC server, you can run example gRPC client, using this command

* Start gRPC Client

```sh
make grpc_client
```

### Unit Test

```sh
make test
```

The tests carried out include:

* Data Access (./data-access)
* Models (./models)
* Routes (./drivers/webservice/routes)

Screenshots result

![Unit Test 1](.github/resources/images/unit-test1.png)

![Unit Test 2](.github/resources/images/unit-test2.png)

### Coverage Test

```sh
make coverage_test
```

Screenshots result

![Coverage Test](.github/resources/images/coverage-test.png)

### Load Test

```sh
make load_test
```

After you run load test command above you can see the test result in graphic, for that please open html file in `test/output/report.html`.

Screenshots result

![Load Test 1](.github/resources/images/load-test1.png)

![Load Test 2](.github/resources/images/load-test2.png)

### Available Make Commands

```sh
# Infrastructure
make infra # Run only db container
make build # Build image
make compose-up # Run all container including db and app
make compose-down # Stop all containers
make purge # Delete all images
make auto # Auto command, to build image and run all containers
make wait-db # Check db is ready
make wait-app # Check app is ready

# Application
make start # Run webservice
make dev # Run webservice in dev mode with watching mode
make grpc # Run gRPC
make grpc_client # Run example gRPC client

# Database
make seed # Seeding database

# Testing
make test # Unit testing
make coverage_test # Coverage testing
make load_test # Load testing, to testing the app performace
```

## Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
Don't forget to give the project a star! Thanks again!

1. Fork the Project
2. Create your Feature Branch ( `git checkout -b feature/magic` )
3. Commit your Changes ( `git commit -m 'feature/magic: Add some magic'` )
4. Push to the Branch ( `git push origin feature/magic` )
5. Open a Pull Request

## License

Distributed under the MIT License. See [LICENSE](./LICENSE) for more information.

## Contact

Agung Saputra - agung.e.sptr@gmail.com

Project Link: [here](https://github.com/agungsptr/node-clean-ts)

## Acknowledgments

Resources that help me to build this project.

* [Clean Architecture Node.js - Build a REST API](https://mannhowie.com/clean-architecture-node)
* [Clean Architecture in ExpressJS Applications (NodeJS)](https://merlino.agency/blog/clean-architecture-in-express-js-applications)
* [JavaScript dependency injection in Node โ€“ friend or foe?](https://tsh.io/blog/dependency-injection-in-node-js)
* [Patterns โ€” Generic Repository with Typescript and Node.js](https://medium.com/@erickwendel/generic-repository-with-typescript-and-node-js-731c10a1b98e)
* [Express.js](https://expressjs.com)
* [MongoDB Documentation](https://www.mongodb.com/docs)
* [Mongoose Documentation](https://mongoosejs.com/docs)
* [gRPC Intoduction](https://grpc.io/docs/what-is-grpc/introduction)
* [gRPC Node Quick Start](https://grpc.io/docs/languages/node/quickstart)
* [Understanding Protocol Buffers](https://betterprogramming.pub/understanding-protocol-buffers-43c5bced0d47)
* [Official TypeScrip Doc](https://www.typescriptlang.org/docs)
* [Learn Typescript at freecodecamp](https://www.youtube.com/watch?v=30LWjhZzg50)

## Support Me

[!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/agungsptr)
Trakteer Saya