Skip to content

Sharlaan/NestJS-TypeORM-Boilerplate

Repository files navigation

NestJS Boilerplate Nest Logo

Table of Contents

Expand / collapse contents

Description

Inspired from : NestJS Boilerplate made with ❤️ by 💡VivifyIdeas💡.

Features

  • Scalable and configurable architecture with NestJS
  • Awesome DX with Typescript/TSLint, Hot Module Reload (HMR), and git hooks for formating/linting/testing
  • Auto-documentation with Swagger (and Typescript in code)
  • Flexible and secured connectivity to any DB with TypeORM (currently Postgres or MongoDB, see Installation section below)
  • JWT authentification with Passport, using NestJS Guards for authorization
  • Sensible environment variables stored in .env.XXX files, which can be derivated depending on mode developement, production or test
  • Multi-platform deployment compatibility thanks to Docker

Installation

Install dependencies

yarn

Environment Configuration

Integrated Configuration Module so you can just inject ConfigService and read all environment variables from .env files. Don't forget to make your own .env files !

MongoDB

Interesting post about admin authentication

How to connect via Shell (mongo client): mongo --host myhost.example.com --port 27017 --username myuser --password mypass --authenticationMechanism SCRAM-SHA-256 --authenticationDatabase admin mydb

Help with mongo commands In mongo shell (usually C:\Program Files\MongoDB\server\4.0\bin) : help admin or help connect

Note : for both PostgreSQL and MongoDB, i did not manage to connect on a different port than the default one...

mongo
> use dbname # the var 'db' will be assigned this value
> db.createUser({ user: "username", pwd: "secretpassword", roles: ["dbOwner"] })
> show users # username should appear as db's owner

then ctrl + c to exit the shell, then try to connect with
mongo dbname -u username -p (it will ask for your password)

(corresponding connection string would be mongodb://username:secretpassword@localhost:27017/)

Next, in .env.development, set these vars :

DATABASE_TYPE=mongodb
DATABASE_HOST=localhost
DATABASE_PORT=27017
DATABASE_USERNAME=username
DATABASE_PASSWORD=secretpassword
DATABASE_NAME=dbname

PostgreSQL

psql -U postgres # type the default password, usually 'postgres'
postgres= create user username with encrypted password 'secretpassword';
postgres= create database dbname with owner username;
postgres= \l # dbname should appear with username as owner

then ctrl + c to exit the shell, then try to connect with
psql -U username dbname (it will ask for your password)

Next, in .env.development, set these vars :

DATABASE_TYPE=postgres
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USERNAME=username
DATABASE_PASSWORD=secretpassword
DATABASE_NAME=dbname

Docker

To mount the project with Docker, you can use yarn deploy:local (which executes docker-compose up)

Alternatively, you can follow this method.
Its basic steps are :

  • mount an empty container
  • connect inside it into its shell terminal with docker exec -it <CONTAINER_ID> /bin/bash
  • mount and seed the db with above steps in Mongo or Postgres paragraphs

Usage

# development
yarn start

# watch mode
yarn start:dev

# production mode
yarn start:prod

Test

# unit tests
yarn test

# e2e tests
yarn test:e2e

# test coverage
yarn test:cov

Swagger

RESTful APIs you can describe with already integrated Swagger. To see all available endpoints visit http://localhost:3000/api/docs

TypeORM

TypeORM gives you possibility to use next db types: mysql, postgres, mariadb, sqlite, etc. Please look at docs for more details. We have provided working example with sqlite, but you have possibility to change this through ormconfig.json. By default you will get sqlite-example.sql file created in the root directory, but it's ignored by git.

Seeding

See TypeORM-Fixtures

Authentication - JWT

Already preconfigured JWT authentication. It's suggested to change current password hashing to something more secure. You can start use already working implementation of Login and Registration endpoints, just take a look at http://localhost:3000/api/docs.

Sources

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published