Skip to content

Boilerplate for building Node.js server with Nest.js framework, TypeORM for database, and Redis for authentication sessions.

Notifications You must be signed in to change notification settings

billyant24/nest-template-redis-auth

Repository files navigation

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

NPM Version Package License NPM Downloads Travis Linux Coverage Gitter Backers on Open Collective Sponsors on Open Collective

Description

Nest framework TypeScript starter template.

This template use REST API concept. The flow of simple CRUD (Create, Read, Update and Delete) applications can be described using the following steps:

  1. The controllers layer handles HTTP requests and delegates tasks to the services layer.
  2. The services layer is where most of the business logic lives.
  3. Services use repositories / DAOs to change / persist entities.
  4. Entities act as containers for the values, with setters and getters.

Folder structure

+-- config // Environment config files.
+-- src // Sources files
|   +-- auth
|   |   +-- guards // Guards.
|   |   +-- strategies // Authentication stratergies.
|   +-- common // Common files.
|   |   +-- constants // Common constants.
|   |   +-- decorators // Common decorators.
|   |   +-- interceptors // Common interceptors.
|   |   +-- response // General response definition.
|   |   +-- serializers // Common serializers.
|   +-- configs // Configurations folder.
|   |   +-- app // Application config.
|   |   +-- database // Database config.
|   +-- modules // Bussiness Modules.
|   |   +-- users // Example user module.
|   |   |   +-- dto // DTO (Data Transfer Object) Schema, Validation.
|   |   |   +-- user.entity.ts // TypeORM Entities.
|   |   |   +-- users.constants.ts // Example constants file.
|   |   |   +-- users.controller.ts // Example controller.
|   |   |   +-- users.module.ts // Example module file.
|   |   |   +-- users.repository.ts // Example repository.
|   |   |   +-- users.service.ts // Example service.
|   |   +-- ... // Other business modules.
+-- test // Jest testing.
+-- app.controller.ts // App controller.
+-- app.module.ts // App module file.
+-- app.service.ts // App service.
+-- main.ts // Main.

File Naming for Class

export class PascalCaseSuffix {} //= pascal-case.suffix.ts
// Except for suffix, PascalCase to hyphen-case
class FooBarNaming {} //= foo-bar.naming.ts
class FooController {} //= foo.controller.ts
class BarQueryDto {} //= bar-query.dto.ts

Interface Naming

// https://stackoverflow.com/questions/541912
// https://stackoverflow.com/questions/2814805
interface User {}
interface CustomeUser extends User {}
interface ThirdCustomeUser extends CustomeUser {}

Setting up

IDE/Editor

  • IntelliJ WebStorm
  • Any editor: Visual Studio Code, Sublime, Atom,...

Environment variable

  1. Create config/development.env and config/prodution.env based on config/sample.env.
  2. Modify config/development.env and config/production.env files for each environment.
  3. Modify src/configs/configs.constants.ts to import these environment values to the project.

Database

  • This template use TypeORM as Object–relational mapping library.
  • TypeORM supports many database programs.
  • Detail configuration can be done in src/configs/database/typeorm.config.ts.
  • By default, the project is configured to use PostgreSQL relational database management system.

Intantiate a module

See NestJS CRUD generator.

  • Default:
$ nest g resource modules/<module-name>

Select REST API. NestJS CLI will generate a boilerplate for the module.

  • If the structure is more complex:
$ nest g resource <module-name>

Copy the generated module to src/modules/<your desired folder>.

Logging

  1. development will log ['log', 'debug', 'error', 'verbose', 'warn'] levels.
  2. production will log ['error', 'warn'] levels.
  • Logging levels can be customized in main.ts for each environment.

Installation

Install npm packages

$ npm install

Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

Documentation

Swagger

# API, Swagger - src/swagger.ts
npm run doc:api #> http://localhost:3000/api
  • This project ultilize NestJS's CLI Plugin .
  • Please aware that there is no need to put @ApiProperty decorator for every DTOs properties. For more information, please visit the link above.

Dependency

"dependencies": {
    "@nestjs/common": "^7.0.0",
    "@nestjs/config": "^0.5.0",
    "@nestjs/core": "^7.0.0",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/passport": "^7.1.0",
    "@nestjs/platform-express": "^7.0.0",
    "@nestjs/swagger": "^4.7.4",
    "@nestjs/typeorm": "^7.1.4",
    "@types/bcrypt": "^3.0.0",
    "@types/passport-facebook": "^2.1.10",
    "bcrypt": "^5.0.0",
    "class-transformer": "^0.3.1",
    "class-validator": "^0.12.2",
    "dotenv": "^8.2.0",
    "passport": "^0.4.1",
    "passport-facebook": "^3.0.0",
    "passport-google-oauth": "^2.0.0",
    "passport-google-oauth20": "^2.0.0",
    "passport-jwt": "^4.0.0",
    "pg": "^8.5.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^6.5.4",
    "swagger-ui-express": "^4.1.4",
    "tslint": "^6.1.3",
    "typeorm": "^0.2.29",
    "uuid": "^8.3.1"
  },

License

Nest is MIT licensed.

About

Boilerplate for building Node.js server with Nest.js framework, TypeORM for database, and Redis for authentication sessions.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published