Skip to main content

Posts

Showing posts with the label NestJS

Use Redis Cache In NestJS Application

Redis Cache: Redis is an open-source in-memory data structure store, used as a database cache. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, etc. Caching can significantly improve application performance and its scalability by reducing the workload to generate the content. If our server application runs on multiple servers then it is easy to share the Redis Cache between them. Setup Redis Docker Image Container: For this sample to use Redis instance locally we will use Docker. If you don't have any prior knowledge of docker, not a problem just follow the steps below.  Click here for a video session on Redis docker setup Note: Skip this section if you already have redis direct instance or azure or any cloud provider that have redis Step1: Download docker into our local system "https://docs.docker.com/desktop/" . Docker was available for all desktop operating systems. Step2: After downloading the docker installer, t

NestJS In-Memory Cache

In this article, we are going to explore the steps to implement the In-Memory Cache in the NestJS application. Caching can significantly improve application performance and its scalability by reducing the workload to generate the content. In-Memory cache creates within the server application memory. Since it uses application memory it delivers content more fastly. But if our application runs on multiple nodes or multiple servers then the in-memory cache can't be shared between the nodes or server each will maintains its own in-memory cache. Create A Sample NestJS Application: Let's understand step by step implementation authentication in NestJs application, so let's begin our journey by creating a sample application. Command To Install CLI: npm i -g @nestjs/cli Command To Create NestJS App: nest new your_project_name Install Cache Npm Packages: NestJS Cache NPM Packages: npm install cache-manager npm install -D @types/cache-manager Register CacheModule: Now let'

Part-2 NestJS JWT(JSON Web Token) Authentication(Refresh Token Implementation)

In  Part-1  we have done complete implementation on NestJS application authentication by creating the user access token. Now we are going to explore the implementation of the refresh token. We will continue to work on the sample app we have used in  Part-1 . Refresh Token Flow: Refresh Token is a random string key that will be created along with the JWT access token and return to the valid client on successful logging in. Now for all subsequent requests will use the access token, but the access token is a short-lived token where as refresh token lives more time than the access token. On the expiration of the access token, the user instead of authenticating himself again passing his user name and password, the user can send the refresh token. The server on receiving a refresh token, first it validates against the storage(database, cache, etc). For a valid refresh token server will create a new access token and refresh token(like when authenticate using user name and password) return it

Part-1 NestJS JWT(Json Web Token) Authentication(Access Token Implementation)

In this article, we are going to explore the implementation steps of JWT(JSON Web Token) authentication in the NestJS application. In this process of authentication, we going to use the 'passport' library(nodejs library) where we write simple customizable authentication. Create A Sample NestJS Application: Let's understand step by step implementation authentication in NestJs application, so let's begin our journey by creating a sample application. Command To Install CLI: npm i -g @nestjs/cli Command To Create NestJS App: nest new your_project_name Create Users Model: Create a 'User' model that represents the table. For now, let's create a simple class in the upcoming steps we make it compatible to communicate with the database. src/users/users.ts: export class User{ id:number; userName: string; password: string; } Create A UsersService: The 'UsersService' is a logical container for our 'Users' data. For now, just mock the user&

NestJS API CRUD Operation Using PostgreSQL Relational Database

NestJS API is a server-side framework that runs on NodeJS frameworks like Express. NestJS application has the ability to talk with both SQL and Non-SQL databases. Here we will develop a sample NestJS API application that communicates with the PostgreSQL relational database. NestJS Database Communication Flow: The 'pg'(PostgreSQL NodeJS library) library was used for communicating with the PostgreSQL database. The 'typeorm'(NodeJS library) library is a framework used for querying the database or manipulating the database using the specified database library(in our case 'pg' node library). The 'typeorm' will be used for all database configurations also. The '@nestjs/typeorm'(NestJS ORM library) is an overlay or wrapper for the 'typeorm'(NodeJS library). By using '@nestjs/typeorm' it gives the advantage of Typescript compatibility and additional feature over 'typeorm'(NodeJS library). So while implementing NestJS application