Skip to content

solutelabs/nestjs-boilerplate

Repository files navigation

NestJs Boilerplate(0.0.1)

Features

  • Quick Setup - Quickly setup and configure your application.

  • Fully integrated Typeorm - Any applications requires database, so we have integrated Typeorm (why?).

  • Migration & Seeding - Easy and elegant support for migration and seedind data.

  • Support for authentication & Authorization - We have added built in api, guards and decorators, so you don't need to worry about.

Getting Started


Project Setup

cd workspace

git clone https://github.com/solutelabs/nestjs-boilerplate your_project_name
cd your_project_name

node setup.js

Project Configuration

Before starting the application configure few variables in the .env file

#ENVIRONMENTS
PORT=

#DATABASE ENVs
DATABASE_URL=

#JWT
JWT_SECRET=
JWT_EXPIRES_IN_DAYS=

WEBHOOK_SECRET=

#password length
MIN_PASSWORD_LENGTH=
MAX_PASSWORD_LENGTH=

RESET_PASSWORD_SLUG=
INVITE_SLUG=

ENVIRONMENT=

Running the app

Docker

Build image

$ sudo docker build -t app-name .

Run image

$ sudo docker run  --network=host app-name

Local machine

Install dependencies

$ npm install

Run app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Project Structure

structure

  • Application is divided in 2 parts
    • app - this contains APIs related code
    • core - this contains code related to scaffolding the app.(like logger, guards, validator, etc)
    • even if we don’t add any module in app folder the application will bootstrap as it is loosely coupled.
  • Module are plug and play. for ex: add S3 module in app module if u want to have S3 related api’s in your app.
  • Module like Auth and user needs to instantiated together as both are dependent on each other.

Commit guidelines


Commitizen

commitizen is a command line utility that makes it easier to create commit messages following the conventional commit format specification.

Install globally

npm install -g commitizen

Use git cz instead of git commit to use commitizen.

Add and commit with Commitizen

Configuration file: .czrc.


Commitlint

commitlint checks if your commit messages meet the conventional commit format.

In general the pattern mostly looks like this:

type(scope?): subject  #scope is optional

Are you a good commitizen ?


branch-name-lint

branch-name-lint Validates and lints the git branch name. Create a config file or use the default configuration file. Use it in husky config file to make sure that your branch will not be rejected by some pesky Jenkins branch name conventions. You may use it as part of a CI process or just as an handy npx command.


Basics


Authentication & Authorisation


This application comes with built in support for authorization. It comes with built-in api's like

  • Login
  • Forget and Change password
  • Send and Verify otp

Extended support with gaurds for authentication

  • Auth Gaurd
  • Role Gaurd
  • Webhook Gaurd
  • Throttler Guard

Auth Gaurd

Use this when you want to make your api secure/ unauthenticated user can't access it

@Query(() => String)
@UseGuards(JwtAuthGuard)
async test() {
    return 'test'
  }

Role Gaurd

Use this when you want to make your api accessible to a certain set user with specific role

@Query(() => String)
@UseGuards(JwtAuthGuard, RolesGuard)
@Role('role')
async test() {
    return 'test'
  }

Webhook Gaurd

Use this when you want to secure your webhook created using hasura

@Query(() => String)
@UseGuards(WebhookAuthGuard)
async test() {
    return 'test'
  }

Throttler Guard

Use this when you want to limit your api to be hit not more than n no of times

@Query(() => String)
@UseGuards(GqlThrottlerGuard)
async test() {
    return 'test'
  }

Communication


This application comes with built in services for communication via.

  • Mail (Postmark)
  • Message (Twillio)
  • Notification (One Signal)

Mail

To use, inject the mail service and call the desired method

  // inject email service
  constructor(
    private readonly emailService: EmailService,
  ) {}

  // call method
  await this.emailService.sendInvitation(payload)

Message

To use, inject the message service and call the desired method

  // inject message service
  constructor(
    private readonly smsService: SmsService,
  ) {}

  // call method
  await this.smsService.sendSms(contactNumber: string, message: string)

Notification

To use, inject the notification service and call the desired method

  // inject email service
  constructor(
    private readonly onesignalService: OnesignalService,
  ) {}

  // call method
  await this.onesignalService.createNotificatioBaseOnExternalId(payload)

Aditional Notes

  • Before starting the application start hasura's docker container for connecting to DB.

  • To use GqlThrottlerGuard add it to imports array in app.module.ts like this:

ThrottlerModule.forRoot({
      ttl: 60,
      limit: 5,
    })
  • To use mail, message and notification service add it to providers array for respective modules in which this services are going to be used

Repository

  • Repository is basically use to create contract between your application with database. that means for each table you can create repository. syntax will be following:
@Entity('<YOUR_TABLE_NAME>')
export class UserEntity extends BaseEntity  {
  @Column('text')
  email: string;
}
  • in this example @Entity decorator is use to connect your table with the repository and the parameter defines the table name.

  • BaseEntity is the parent class so all the common column which are used in every table will be written over there so just we need to extend this example, already shown in the above example.