Skip to content

AnoopHegde/restful-app

 
 

Repository files navigation

Restful User App

Overview

RESTful API that allows users to create, retrieve, update, and delete data on a PostgreSQL database

Requirement

  • node >= 18.18.0
  • postgres >= 16.1
  • docker >= 20.10.6 (intergrating docker-compose version)

Starting up project

By CLI

Step 1 (install needed depedencies)

Run the following command in root folder to install needed depedencies

npm install

Step 2 (run migration for database)

Remember create rest_app database before migrating.

Run the following command for migrating

npm run migration:run

Step 3 (startup the server)

Run the following command in root folder to startup the server

npm run start:dev

Step 3 (check server is running or not)

Open browser and go to link http://localhost:3000/swagger or http://127.0.0.1:3000/swagger for swagger UI and begin testing the API

By docker

Step 1 (startup docker containers)

Run the following command in root folder

docker compose up -d

Screen Shot 2023-12-17 at 19 52 28

Two containers:

  1. restful-app-database-1 listens on 5432 port (for postgres database)
  2. restful-app-nest-1 listens on 3000 port (for server code)

Please notice that the containers name maybe different in your local machine, to confirm container name, try to run:

docker ps

and pay attention to the containers those have restful-app prefix in name.

Step 2 (run migration for database)

Run the following command for migrating

docker exec restful-app-nest-1 npm run migration:run

Step 3 (check server is running or not)

Open browser and go to link http://localhost:3000/swagger or http://127.0.0.1:3000/swagger for swagger UI and begin testing the API

Run unit test

By CLI

Step 1 (run migration for test)

Remember create rest_app_test database before migrating.

Run the following command for test database migrating

npm run migration:test:run

Step 2 (execute the test)

Run the following command for executing test (for all)

npm run test

Run the following command for executing test (for specify file)

npm run test [file_path]

for example:

npm run test src/users/infrastructure/rdb-repository/user/index.spec.ts

By Docker

After starting up the docker container for both database and server

Step 1 (run migration for test)

Run the following command for test database migrating

docker exec restful-app-nest-1 npm run migration:test:run

Step 2 (execute the test)

Run the following command for executing test (for all)

docker exec restful-app-nest-1 npm run test

Run the following command for executing test (for specify file)

docker exec restful-app-nest-1 npm run test [file_path]

for example:

docker exec restful-app-nest-1 npm run test src/users/infrastructure/rdb-repository/user/index.spec.ts

Architecture

Using DDD and clean architecture here

Screen Shot 2023-12-17 at 20 20 18

Have 4 main layers:

  • domain: business domain logic will be here
  • application: feature logic will be here
  • presentation: API endpoints will be here
  • infrastructure: interacts with external system (DB, APIs, ...)

In application layer, I split it into

  • command: for updating, creating and deleting data logic.
  • query: for getting data logic only.

Notice about Depedency Inversion principal here

Screen Shot 2023-12-17 at 20 25 56

The inside layer will never be depending on the outside layer, for example:

  • domain will never be depending on application
  • application will never be depending on presentation

And the outside layer will never be depending on the inside layer, for example:

  • application will be depending on domain
  • presentation will be depending on application

Each layer will have its own Error Class

  • domain will have DomainError
  • application will have CommandError and QueryError
  • presentation will have PresentationError
  • infrastructure will have InfrastructureError

See libs/exception folder for more details.

Request and response flow (focusing on authen, transaction and error middleware)

Screen Shot 2023-12-18 at 8 38 49

Additional

Migration generate

Run the following command

npm run migration:generate --name="MigrationName"

to create new migration if you want.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 99.0%
  • Other 1.0%