mikemajesty / nestjs-boilerplate-tests_examples

The best nestjs boilerplate using Anti-corruption Layer pattern, secrets, error handler, logger, Mysql, Docker and unit tests.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NestJs boilerplate API

Check the contributing manual

Description

The best nestjs boilerplate using Anti-corruption Layer pattern, secrets, error handler, logger, Mysql, Docker and unit tests.

nest framework documentaion.

conventionalcommits framework documentaion.

commitizen framework documentaion.

Requirements

  • [docker]
  • [docker-compose]
  • [yarn]
  • [NVM]
    • Node >=14 <=15

Architecture

  • /jest/init.ts: Test startup settings like envs and mocks.
  • src/main.ts: Application entry point.
  • src/modules/: Application modules, may contain; services, controller, repositories and etc.
  • src/modules/common/: Common modules that used for two or many modules.
  • src/modules/common/**/adpater.ts: Used to communicated with controller and others modules. Controllers and Modules must communicated with abstraction, not implementation.
  • src/modules/global/: Globals modules that is visibles for all modules.
  • src/utils/: Utilities for the application that will not necessarily only be used within modules.
  • src/static/: Application static files like JSON, CSV etc.

prerequisites

  1. create file
$ touch .env
  1. add on file
ENV=dev
PORT=3000
TZ='America/Sao_Paulo'

# url
HELLO_WORD_SERVICE='https://sandbox.api.service.nhs.uk/hello-world/hello/world'

# database
DB_HOST='db'
DB_USERNAME='root'
DB_PASSWORD='admin'
DB_DATABASE='customer'

--

Installation

$ yarn

--

Running the app

# development
$ yarn start:dev
# dev/hml/prd environment
$ docker-compose up --build

--

Test

# unit tests
$ yarn test

# e2e tests
$ yarn  test:e2e

# test coverage
$ yarn  test:coverage

--

Swagger

# swagger documentation
http://localhost:3000/api/#/

--

Migrations

# create migration
$ yarn migration:create --name=<migrationname>

--

Generate version

$ yarn tag:production

--

Usage

  • throw error
import { AppException } from 'utils/error';

throw new AppException({
  error: 'Error message',
  status: HttpStatus.INTERNAL_SERVER_ERROR, //optional
});
  • logs
import { ILoggerService } from '../global/logger/adapter';

export class Example {
  constructor(
    private readonly loggerService: ILoggerService,
  ) {}

  async example(): void {
    this.loggerService.log(
      data.message,
      `${Example.name}/${this.example.name}`,// log context
    );

    this.loggerService.error(new ErrorRest({
      error: 'Error message',
      status: HttpStatus.INTERNAL_SERVER_ERROR, //optional
    }));

  }
}
  • envs
import { ISecretsService } from '../global/secrets/adapter';

@Injectable()
export class Example {
  constructor(
    private readonly secretService: ISecretsService,
  ) {}

  async example(): void {
    this.secretService.url.HELLO_WORD;
  }
}

The following is a list of all the people that have contributed to nest-boilerplate. Thanks for your contributions!

mikemajesty

License

It is available under the MIT license. License

About

The best nestjs boilerplate using Anti-corruption Layer pattern, secrets, error handler, logger, Mysql, Docker and unit tests.


Languages

Language:TypeScript 93.6%Language:Shell 3.6%Language:JavaScript 2.0%Language:Dockerfile 0.8%