NestJS Guide

Andre Vianna
My Dev Zone
Published in
5 min readJan 1, 2022

Summary

  1. NestJS Introduction
  2. NestJS Concepts
  3. NestJS Coding
  4. References

1. NestJS Introduction

Hello, nest!

A progressive Node.js framework for building efficient, reliable and scalable server-side applications.

https://nestjs.com/

Comapre NestJS vs NodeJS

Types of NodeJS Frameworks

NodeJS Frameworks

Some statistics

Advantages of using NestJS

  • Nest can be scalable thanks to the flexibility of JavaScript and the robustness of TypeScript
  • Detailed and well-maintained documentation
  • It has active codebase development and maintenance
  • It is open source (MIT license)
  • Code generation helps to develop applications faster
  • Has a quickly growing community
  • Follows strict design principles that do a lot of basic development activities for developers

Disadvantages of using NestJS

  • It has Angular concepts, so for developers who don’t know Angular, it may be hard to grasp at first, even if you don’t need to know Angular before working with Nest
  • Steep learning curve
  • The community is small when compared with Express

Who uses NestJS?

  • Adidas
  • Autodesk
  • Neoteric
  • Sanofi

3. NestJS Coding

What is NestJS ?

  • NodeJS Framework
  • Architecture MVC — Model, View, Controller
  • Model — Business Layer
  • Controller — Persistent Layer
  • View — Process HTML, JSON

Advantages NestJS + TypeScript

  • Strongly Typed Language
  • Visualization of Development Errors

Advantages NestJS + Angular

  • Incorporates Modular Ideas and Angular Dependence Injection

Advantages NestJS

  • COC — Convention Over Configuration
  • Files
  • Folders
  • Structures of Development
  • TypeScript
  • Scalable Architecture
  • Easy Database Integration
  • Microservices Integration
  • Support REST API, GraphQL
nest new nestjs-guide
yarn start

NestJS Architechture Application

  • Create an Test Controller
nest g controller test
nest g controller test
  • Create an test Service
nest g service test
  • Create an Resource
nest g resource

API REST

API Resource Name

NestJS Database Integration

SQL (Sequelize)

This chapter applies only to TypeScript

WARNINGIn this article, you’ll learn how to create a DatabaseModule based on the Sequelize package from scratch using custom components. As a consequence, this technique contains a lot of overhead that you can avoid by using the dedicated, out-of-the-box @nestjs/sequelize package. To learn more, see here.

Sequelize is a popular Object Relational Mapper (ORM) written in a vanilla JavaScript, but there is a sequelize-typescript TypeScript wrapper which provides a set of decorators and other extras for the base sequelize.

Getting started#

To start the adventure with this library we have to install the following dependencies:

$ npm install --save sequelize sequelize-typescript mysql2
$ npm install --save-dev @types/sequelize
 yarn add --save sequelize sequelize-typescript sqlite3 yarn add @nestjs/sequelize yarn add -D @types/sequelizeimport { Sequelize } from 'sequelize-typescript';
import { Cat } from '../cats/cat.entity';

export const databaseProviders = [
{
provide: 'SEQUELIZE',
useFactory: async () => {
const sequelize = new Sequelize({
dialect: 'mysql',
host: 'localhost',
port: 3306,
username: 'root',
password: 'password',
database: 'nest',
});
sequelize.addModels([Cat]);
await sequelize.sync();
return sequelize;
},
},
];

--

--

Andre Vianna
My Dev Zone

Software Engineer & Data Scientist #ESG #Vision2030 #Blockchain #DataScience #iot #bigdata #analytics #machinelearning #deeplearning #dataviz