/ts-comments-api

The goal of this project is to being a starting point to show what Clean Architecture would look like in a real world application developed with Typescript.

Primary LanguageTypeScriptMIT LicenseMIT

Comment Microservice API

To manage comments on posts of a blog or wiki page.

About

This is a Typescript version of the project DevMastery Comments Microservice API written in JavaScript.

The goal of this project is to be a starting point to show what Clean Architecture would look like in a real world application. This project tries to follow the rules of Uncle Bob's Clean Architecture. You will also find it named hexagonal, ports-and-adapters, or onion architecture.

The center of your application is not the database. Nor is it one or more of the frameworks you may be using. The center of your application is the use cases of your application - Unclebob (source)

⚠️ This is still (and maybe will always be) under development! Any PR is greatly welcome! 😄

Features

Prerequisites

To build and run this service locally you have two options:

  1. Docker (Recommended) - Develop as close to production as you can
  2. Install the tools directly on your machine (traditional).

Additional

Getting Started

1. Clone the repository.

git clone https://github.com/sagiomc/ts-comments-api <project_name>

2. Create a .env file on the root app directory.

Copy the content of sampledotenv and fill the appropriate values.

Running with Docker.

  1. Start up server and database. On the root app directory run the following command:

    docker-compose up

    This starts a local server using nodemon, which will watch for any file changes and will restart the server according to these changes. The command above will "freeze" your terminal window/tab. If you want to run the api in background you have to run docker-compose up -d.

    With default values, the server will be displayed to you as http://localhost:3000. The api use cases will be available in http://localhost:3000/api/comment.

    NOTE: To change the default values running with Docker, you have to add the following environment variables to .env file:

    HOST_PORT=<PORT> # Port where you'll consume the api from your host machine Ej. HOST_PORT=8080 --> localhost:8080
    HOST_INSPECT_PORT=<PORT> # Port where you'll inspect the api for debug purposes Ej. HOST_INSPECT_PORT=9229 --> Open socket in localhost:9229
    HOST_DB_PORT=<PORT> # Port where you'll consume the database server from your host machine Ej HOST_DB_PORT=27029 --> mongodb://localhost:27029
  2. Running commands inside docker containers like install/uninstall npm dependencies. To run commands inside containers you have following the next structure on the root api directory:

    docker-compose exec app <my-command>

    For example:

    docker-compose exec app npm install <dependency> # Installing dependencies
    docker-compose exec app npm run test:watch # Run jest in watch mode

Running locally (Traditional)

  1. Start up the mongodb server

    Usually this is just: mongod on the command line.

  2. On the root app directory, install the dependencies and set up the database, running:

    npm start setup
  3. To run in development mode where code is run by nodemon and re-transpiled any time:

    npm start serve

Project Structure

.
├── src
|   ├── domain                - Enterprise business rules
|   |   ├── entities          - Core business rules
|   |   └── {feat-name}       - Business features (add-comment, delete-comment, etc)
|   ├── dataproviders         - Interfaces data adapters implementations
|   └── frameworks            - Frameworks and drivers that exposes the app
├── test                      - Tests directory
|   ├── fixtures              - Accesories for tests
|   └── unit                  - Unit tests, this directory must follow the same structure as "src" directory
├── commands                  - Development utilities like banner and set up the database
└── dist                      - Compiled javascript files

Related Projects