Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

6ª NextLevelWeek da RocketSeat - Trilha do NodeJS, ensinando a utilizar os frameworks backend para criação de rotas e gerenciamento de dados, o projeto NWL Valoriza!

License

Notifications You must be signed in to change notification settings

LeDragoX/nlw-valoriza-nodejs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NLW Valoriza - NLW6-NodeJS (06/2021)

Resumo

6ª NextLevelWeek da RocketSeat - Trilha do NodeJS, ensinando a utilizar os frameworks backend para criação de rotas e gerenciamento de dados, o projeto NWL Valoriza!

Tecnologias utilizadas:

  • NodeJS => TS;
  • TypeScript;
  • ExpressJS / Express-Async-Errors;
  • TS-Node-Dev;
  • TypeORM / Reflect-Metadata / SQLite;
  • UUID;
  • JWT (Json Web Token);
  • BCryptsJS;

Usage

Software Requirements:

# Curl
sudo apt install -y curl
# NodeJS + NPM
curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs
# Yarn
npm install --global yarn

Dependencies:

yarn init -y            # Initialize Project Repository
yarn add typescript -D  # Install Typescript dependencies | -D to install all dependencies
yarn tsc --init         # Init Typescript

# Use this to run the server (including any updates you made)
yarn dev

# As node don't understand 'typescript', do this
# to convert index.ts => index.js
# With ts-node-dev, this is no longer needed
#yarn tsc

# Add 'express'
yarn add express -D
yarn add @types/express -D

# To automatize .ts => .js conversion
# Use this library to spend less time and be more productive
yarn add ts-node-dev -D

# For SQLite (This):
yarn add typeorm reflect-metadata sqlite3
# For PostgreSQL:
#yarn add typeorm reflect-metadata mysql
# For MySQL:
#yarn add typeorm reflect-metadata mysql

# Add the *uuid* library to yarn:
yarn add uuid
yarn add @types/uuid -D

# Import async errors detection:
yarn add express-async-errors

# Install JWT lib
yarn add jsonwebtoken
yarn add @types/jsonwebtoken -D

# Library to Encrypt passwords
yarn add bcryptjs
yarn add @types/bcryptjs -D

Aula 1 - Node introduction

Click to expand

Annotations will be added out from code, to keep the code CLEAN.

Annotation src/server.ts:

// @types/express

/*
- GET     => Busca
- POST    => Inserção/Criação
- PUT     => Alterar dado já existente
- DELETE  => Remover
- PATCH   => Atualizar um dado, de um grupo (tipo uma correção)
*/

// Request  => Vem do Client (entrada)
// Response => Vem do Server (saída)

app.get("/test", (request, response) => {
  return response.send("|GET| Olá manito!");
});

app.post("/test-post", (request, response) => {
  return response.send("|POST| Olá manito!");
});

app.put("/test-put", (request, response) => {
  return response.send("|PUT| Olá manito!");
});

app.delete("/test-delete", (request, response) => {
  return response.send("|DELETE| Olá manito!");
});

app.patch("/test-patch", (request, response) => {
  return response.send("|PATCH| Olá manito!");
});

Step-by-Step

Install:

On tsconfig.json change:

  "strict": false,                                 /* Enable all strict type-checking options. */

On package.json add after the '"license":' line

  "scripts": {
    "dev": "ts-node-dev src/server.ts"
  },

Aula 2 - User Structure

Click to expand

Regras

  • Cadastro de usuário
    • [ x ] Não é permitido cadastrar mais de um usuário com o mesmo e-mail
    • [ x ] Não é permitido cadastrar usuário sem e-mail

Working with Database integration - SQLite

Annotation src/server.ts:

/*
TIPOS DE PARÂMETROS:
- Route Params  => http://localhost:3000/produtos/47358964378 (id)
- Query Params  => http://localhost:3000/produtos?name=teclado&description=bom&...
- Body Params   => {
  "name": "teclado"
  "description": "bom"
}
  *Body Params are not used with GET methods, only PUT, POST and PATCH
*/

This will use an ORM to ease the integration process, but we can use native drivers from other DBs too.

Migrations are good for a team creating individual tables on the DB. They store the history from the Entities

Create ormconfig.json:

{
  "type": "sqlite",
  "database": "src/database/database.sqlite",
  "cli": {
    "migrationsDir": "src/database/migrations"
  }
}

On package.json inside '"scripts": {'

  "typeorm": "ts-node-dev ./node_modules/typeorm/cli.js"

Add migration file running:

yarn typeorm migration:create -n CreateUsers

On ormconfig.json, add to find all migrations correctly:

  "migrations": ["src/database/migrations/*.ts"],

Run this to sync migrations inside the folder:

  yarn typeorm migration:run  # migration:revert to cancel

On ormconfig.json, to find all entities (Tables) correctly:

  "entities": ["src/entities/*.ts"],
  "cli": {
    "entitiesDir": "src/entities"
  }

Add entity file running: Run this to sync migrations inside the folder:

  yarn typeorm entity:create -n User

On tsconfig.json set:

  "strictPropertyInitialization": false,        /* Enable strict checking of property initialization in classes. */
  "experimentalDecorators": true,              /* Enables experimental support for ES7 decorators. */
  "emitDecoratorMetadata": true,               /* Enables experimental support for emitting type metadata for decorators. */

Migrations: Entity (User) <-> ORM <-> DB Repositories

Service: Server -> ( ) -> SERVICE (Validation) -> Repositories -> DB

Controller (Request / Response): -> Server -> Controller -> Service -> operations()...

Aula 3 - Continuing the application

Click to expand

Regras

  • Cadastro de TAG
    • [ x ] Não é permitido cadastrar mais de uma tag com o mesmo nome
    • [ x ] Não é permitido cadastrar tag sem nome
    • [ x ] Não é permitido o cadastro por usuários que não sejam administradores

Server -> routes -> Controller -> Service (throw new Error)

# Create new migration for tags
yarn typeorm migration:create -n CreateTags
# Run migration
yarn typeorm migration:run
# Create Tag entity
yarn typeorm entity:create -n Tag

Aula 4 - Working with JWT

Click to expand

Regras

  • Cadastro de elogios
    • [ x ] Não é permitido um usuário cadastrar um elogio para si
    • [ x ] Não é permitido cadastrar elogios para usuários inválidos
    • [ x ] O usuário precisa estar autenticado na aplicação

This will use the JWT library.

Changes on the Project

# Create a migration to Alter the User Table
yarn typeorm migration:create -n AlterUserAddPassword
# After adding the necessary modifications
yarn typeorm migration:run

If you want an external hash code

Use MD5 Hash Generator to transform: ledragoxnlwvalorizanodejs -> a8a2d0c0f2311a246a45d1a5045c95e6

# Compliments migration
yarn typeorm migration:create -n CreateCompliments
# After adding the necessary modifications
yarn typeorm migration:run

Registering a new Compliment:

{
  "tag_id": "8d352bfc-1087-4dfe-ac8e-b67ced92286e",
  "user_sender": "1662a6ed-dd0f-4765-916b-d0e097ba2829",
  "user_receiver": "0e93d95e-1431-400e-845b-831047c77ded",
  "message": "Obrigado pelo aulão Dani!"
}

Aula 5 - Concluding the project

Click to expand

On tsconfig.json change:

    "typeRoots": [
      "./src/@types"
    ], /* List of folders to include type definitions from. */
# Add a new library: Class Transformer
yarn add class-transformer

About

6ª NextLevelWeek da RocketSeat - Trilha do NodeJS, ensinando a utilizar os frameworks backend para criação de rotas e gerenciamento de dados, o projeto NWL Valoriza!

Topics

Resources

License

Stars

Watchers

Forks