Home

Awesome

NestJS Starter Kit [v2]

License: MIT build tests

This starter kit has the following outline:

This is a Github Template Repository, so it can be easily used as a starter template for other repositories.

Sample implementations

To view sample implementations based on this starter kit, please visit the nestjs-sample-solutions repository.

Starter kit Features

One of our main principals has been to keep the starter kit as lightweight as possible. With that in mind, here are some of the features that we have added in this starter kit.

FeatureInfoProgress
AuthenticationJWTDone
AuthorizationRBAC (Role based)Done
ORM IntegrationTypeORMDone
DB MigrationsTypeORMDone
LoggingwinstonDone
Request Validationclass-validatorDone
PaginationSQL offset & limitDone
Docker ReadyDockerfileDone
Devcontainer-Done
Auto-generated OpenAPI-Done
Auto-generated ChangeLog-WIP

Apart from these features above, our start-kit comes loaded with a bunch of minor awesomeness like prettier integration, commit-linting husky hooks, package import sorting, SonarCloud github actions, docker-compose for database dependencies, etc. :D

Consulting

Most of the features added to this starter kit have already been tried out in production applications by us here at MonstarLab. Our production applications are more feature rich, and we constantly strive to bring those features to this OSS starter kit.

If you would like to use a more feature rich starter kit, with more awesome features from Day 1, then please reach out to us and we can collaborate on it together as technology partners. :)

Installation

Note: when using docker, all the npm commands can also be performed using ./scripts/npm (for example ./scripts/npm install). This script allows you to run the same commands inside the same environment and versions than the service, without relying on what is installed on the host.

$ npm install

Create a .env file from the template .env.template file.

Generate public and private key pair for jwt authentication:

With docker

Run this command:

./scripts/generate-jwt-keys

It will output something like this. You only need to add it to your .env file.

To setup the JWT keys, please add the following values to your .env file:
JWT_PUBLIC_KEY_BASE64="(long base64 content)"
JWT_PRIVATE_KEY_BASE64="(long base64 content)"

Without docker

$ ssh-keygen -t rsa -b 2048 -m PEM -f jwtRS256.key
# Don't add passphrase
$ openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

You may save these key files in ./local directory as it is ignored in git.

Encode keys to base64:

$ base64 -i local/jwtRS256.key

$ base64 -i local/jwtRS256.key.pub

Must enter the base64 of the key files in .env:

JWT_PUBLIC_KEY_BASE64=BASE64_OF_JWT_PUBLIC_KEY
JWT_PRIVATE_KEY_BASE64=BASE64_OF_JWT_PRIVATE_KEY

Running the app

We can run the project with or without docker.

Local

To run the server without Docker we need this pre-requisite:

Commands:

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Docker

# build image
$ docker build -t my-app .

# run container from image
$ docker run -p 3000:3000 --volume 'pwd':/usr/src/app --network --env-file .env my-app

# run using docker compose
$ docker compose up

Learn more about Docker conventions here. (WIP - Currently this is an internal org link.)

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Migrations

# using docker
$ docker compose exec app npm run migration:run

# generate migration (replace CreateUsers with name of the migration)
$ npm run migration:generate --name=CreateUsers

# run migration
$ npm run migration:run

# revert migration
$ npm run migration:revert

Architecture

Contributors

External Links

<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo.svg" width="150" alt="Nest Logo" /></a>

SonarCloud