Node JS Full Course 2026 | Complete Backend Development Course | 17 Hours — Transcript
Full transcript
- 0:00Hey everyone, welcome back. So in this
- 0:01particular video, you're going to learn
- 0:03each and everything that you need to
- 0:04know to start your journey with NodeJS.
- 0:06And this is a open challenge to each and
- 0:08every one of you. Just watch this
- 0:10complete video and then show me a better
- 0:12course than this one on YouTube even in
- 0:15the paid courses. Okay. So let's see
- 0:17what are the things we're going to
- 0:18cover. So first of all, I wanted to
- 0:19mention the very key point and that is
- 0:21this complete course is built on
- 0:22posgress and this is not a MongoDB
- 0:25course that you usually find on other
- 0:27YouTube videos. Okay. second. So let's
- 0:29see all the sections because this is
- 0:30very very important. Uh you need to know
- 0:32that what are the things we going we are
- 0:34covering and in which order. Okay.
- 0:36Before that uh wanted to mention one
- 0:38more point and that is this particular
- 0:40video took me more than 2 months to
- 0:41create. So please like this video,
- 0:44comment and also if possible please
- 0:46share with your friends and this will be
- 0:47very much helpful because this will help
- 0:49my channel to grow and it will also help
- 0:52to complete 100k as soon as possible. So
- 0:54first of all you can see that this is
- 0:56actually divided into 11 section. So we
- 0:58are going to start with setup. So first
- 1:00we will understand how you can install
- 1:02NodeJS runtime basics, npm package.json
- 1:05and scripts, TypeScript configuration
- 1:08and process object. Then we'll move to
- 1:10our next module which is very very
- 1:12important. We are going to understand
- 1:13all the code modules. So we are going to
- 1:15understand crypto module, OS module,
- 1:17path module. Then we are going to
- 1:19understand timers asynchronous flow file
- 1:22system APIs, buffers, URL module, event
- 1:25emitter and we are going to end this
- 1:27particular module with streams. Then we
- 1:30have a dedicated section on HTTP module.
- 1:32So here we are going to understand
- 1:34native HTTP module without express. Here
- 1:37we are going to cover native HTTP
- 1:39server, manual routing, reading request
- 1:41bodies, JSON responses and external API
- 1:45implementation. Then we are going to
- 1:46have a little more advanced uh section
- 1:49we are going we are going to understand
- 1:50NodeJS internals. So NodeJS runtime
- 1:52internals V8 engine live blocking versus
- 1:56non-blocking and cluster module. Okay.
- 1:59Then we have a complete dedicated
- 2:01section on postgress. Now this is a
- 2:03mandatory section. This you need to
- 2:05complete before we are going to uh start
- 2:07working on the CRUD. So here you're
- 2:08going to understand database, schema,
- 2:10tables, data types, constants, keys, uh
- 2:14complete SQL, CRUD, relationships, join,
- 2:17aggregations and indexes and
- 2:18transactions. Okay. Then we are going to
- 2:20have our main master project that we are
- 2:23going to build and this will be huge
- 2:24project. You're going to understand a
- 2:26lot of things. So first the first module
- 2:28will be to understand how you can create
- 2:30a proper express project, proper folder
- 2:33structure. So you can see that we are
- 2:34going to have our application setup with
- 2:36express poller. We are going to use
- 2:38scalable folder structure routes health
- 2:41checks server life cycle docker compost
- 2:43posgress setup custom database migration
- 2:46because we are not using any third party
- 2:48packages and centralized error handling.
- 2:50Then we are going to have our o and JWT.
- 2:53So we're going to have user registration
- 2:54flow login JWT generation authentication
- 2:57middleware admin middleware and we are
- 2:59also going to implement Google O login.
- 3:02Then we'll move to our CRUD and admin
- 3:04API. So we're going to have our normal
- 3:05crap because this is actually mandatory.
- 3:07So we are going to create user task,
- 3:09fetch, update, delete and then admin
- 3:11operations. Then moving to next one,
- 3:13we're going to have a complete dedicated
- 3:14section on file upload and we're going
- 3:16to use cloudinary. So with this we're
- 3:18also going to use mult. So this will be
- 3:20how you can handle multipart upload
- 3:22flow, multiple middleware, cloudinary
- 3:24upload helper, banner creation and fetch
- 3:27APIs. Then we're going to move to our
- 3:28radius section. So in this particular
- 3:30section we're going to cover radius. So
- 3:32this is also a mandatory section. So
- 3:33here you're going to understand all the
- 3:35radius concept how you can create radius
- 3:37client cash asset pattern still cast
- 3:39invalidation rate limiting and popsup
- 3:41concept and then we'll move to our next
- 3:43section which will be how we are going
- 3:45to integrate radius and bool mq and you
- 3:47can implement background jobs for this
- 3:48one we're going to implement this one in
- 3:50our previous project only so we're going
- 3:51to understand how you can create cues
- 3:53jobs workers blemq project setup
- 3:55cloudinary deletion in the background
- 3:57and also how you can implement a
- 3:59reliable asynchronous architecture so
- 4:01all those things we are going to cover
- 4:02in this whole module. All right. So
- 4:04before we'll start so two very important
- 4:06points I wanted to mention. The first
- 4:07one is that I finally created the
- 4:09official LinkedIn page for age debop. So
- 4:12I highly request everyone to follow this
- 4:13particular page and help me to reach at
- 4:16least 5,000 followers as soon as
- 4:18possible because some of you already
- 4:19know that I'm not that much active on
- 4:21social media and don't promote all these
- 4:23videos and everything. But because my
- 4:24channel is almost close to 100K so I
- 4:27thought this will be right time to start
- 4:29the page. So I think I need help from
- 4:31all of you. So I've added the link in
- 4:32the description. Second, uh I think some
- 4:35of you already know that I have a
- 4:36upcoming course on my debub which is my
- 4:39uh website and you can see that almost
- 4:415,000 people already joined the weight
- 4:42list. So in case you are interested,
- 4:44please uh join this course. I added the
- 4:47link in the description also. Third
- 4:49thing uh to get the source code of this
- 4:51enter nodejs uh course what you can do
- 4:53right I've added the link in the
- 4:56description that is also completely
- 4:58free. So you can just go to the project
- 4:59section and you will be able to download
- 5:01the source code for free. So with this
- 5:03now let's get started and good luck.
- 5:06Okay. So now let's get started. So this
- 5:08will be a very long video. I think I
- 5:10already explained and also by the length
- 5:12of this video you can understand. So
- 5:14what I'll suggest right so a couple of
- 5:15things I wanted to mention. Uh so first
- 5:17one is I think a lot of people already
- 5:19asked me that I already have multiple
- 5:21NodeJS uh videos that uh after watching
- 5:25those videos do I still need to watch
- 5:27this particular video? See this answer
- 5:29is little tricky the why I'm telling
- 5:31this because I already uploaded one
- 5:34video that doesn't mean that in this
- 5:36particular video I'm going to discover
- 5:37some new things and I'm going to explain
- 5:39you are getting at the end of the video
- 5:41you'll see that we are going to
- 5:43understand the almost similar concept
- 5:45that I already covered in the previous
- 5:46video but then what is the difference
- 5:49okay the difference will be first of all
- 5:51in this particular video some of the
- 5:53concept and some of most of the
- 5:55explanation will be much much much more
- 5:57detailed Okay. And you'll understand
- 5:59this one very very clearly once I start
- 6:01this particular video. Second, this
- 6:04video we are going to use posgress,
- 6:06right? I already mentioned in the
- 6:07previous video and almost all videos
- 6:09that are available on YouTube, we'll see
- 6:10that most of the videos are built on
- 6:12MongoDB, right? Uh because it's easy to
- 6:15record and most of the uh student want
- 6:17to learn MongoDB also because M was very
- 6:20popular. I think it's still very popular
- 6:22but now nowadays everyone wants to learn
- 6:24posgress. So that is the main change in
- 6:26this particular video. So there will be
- 6:28one dedicated section where you are
- 6:30going to understand poseless also. So
- 6:31I'm going to add some video that I
- 6:33already created very recently. So it
- 6:36will be around 4 hours. So that I will
- 6:38be adding in middle so that before we
- 6:40are going to start the project. So you
- 6:42will see that there will be a postgress
- 6:43section. Third, there are some core
- 6:46modules we are going to understand much
- 6:47detail way. Okay. And this is very very
- 6:49important. So once I start you'll
- 6:51understand what I'm talking about.
- 6:53Fourth one the main project that we are
- 6:55going to build that will be much more
- 6:57detailed. Now in the previous part one
- 6:59video whatever I covered so it was like
- 7:01a normal CR. Now here also we are going
- 7:03to cover normal CRUD but there will be
- 7:05some more advanced topic we are going to
- 7:07understand. For example how you can work
- 7:08with radius how you can implement
- 7:10background job. Now the main question
- 7:13comes to this that if I already watch
- 7:15this video or if I already know NodeJS
- 7:18can I still watch this video? The answer
- 7:20is absolutely yes. Okay. So you can
- 7:22watch this video. This video is going to
- 7:24give you fantastic idea about NodeJS.
- 7:26How you can work, how you can create
- 7:28proper folder structure and it will give
- 7:30you complete overview. Okay. So you
- 7:32don't have to watch multiple videos.
- 7:33Second when I'm going to upload the part
- 7:35two of NodeJS which will be NodeJS
- 7:38microservices with Postgress and Kafka,
- 7:40you'll see that it will be much more
- 7:41easier for you to understand. So I hope
- 7:43that this will answer some of the query.
- 7:45Now with that let's get started. The
- 7:47first of all you can see that I have a
- 7:49blank folder. We are going to use VS
- 7:51code for this and we will take some
- 7:52things uh most of the things very very
- 7:54slowly but there are some prerequisite
- 7:56that you need to know. So first
- 7:57prerequisite will be you should know
- 8:00JavaScript ES6 that is mandatory. We are
- 8:03not going to cover JavaScript ES6 or
- 8:05very very basic concepts because I
- 8:07already uh I thought that okay you know
- 8:09this okay I will consider this you
- 8:11already know this concept and then you
- 8:13are watching this video. If you haven't
- 8:15know all of this there is no point to
- 8:16watch this video. So first you you have
- 8:18to watch or learn JavaScript ES6
- 8:20concept. Second TypeScript basics you
- 8:24need to know okay because this video
- 8:26actually will be built completely on
- 8:27TypeScript. So I already have a
- 8:28TypeScript video also. So with that now
- 8:31what we going to do right? So how this
- 8:33first section will be [snorts] covered.
- 8:35So this will be our first section. You
- 8:36can see that I created NodeJS basics and
- 8:39uh core modules we are going to cover.
- 8:41So Node basics and NodeJS code modules.
- 8:44So now first what you have to do right?
- 8:46So let's go here and just search for
- 8:48NodeJS and then you have to download
- 8:50NodeJS. Now here one thing you have to
- 8:52understand. So first of all before we we
- 8:55will understand that okay what actually
- 8:56you need to download. First you have to
- 8:58understand what actually NodeJS is.
- 9:00Okay. So let's do one thing I'm going to
- 9:02create one file here and I'm just going
- 9:04to give this one file name as concepts
- 9:07MD and this is how we are going to do.
- 9:09So first let's understand some of the
- 9:11very very key point and this is going to
- 9:13help you while giving interview also. So
- 9:15first of all, NodeJS is
- 9:18so I'm just going to give like this uh
- 9:20let's give it like this. So I'll do
- 9:22NodeJS is JavaScript or JS runtime.
- 9:28So what I mean by this? So first of all
- 9:29you have to understand what actually
- 9:30runtime this particular word means. So
- 9:33this runtime means that it's actually an
- 9:35environment where you are actually
- 9:37running some code. Okay. So runtime is
- 9:39the environment where you run some code.
- 9:42Now in the browser what happen right
- 9:44where JavaScript runs in JavaScript
- 9:47runs inside your Chrome Firefox suffer
- 9:50you all these browsers but in NodeJS
- 9:52your JavaScript runs where on your
- 9:54computer or maybe let's say you're
- 9:56running NodeJS on a server. So that
- 9:58means in NodeJS your JS runs outside
- 10:03of the browser. Now this is the first
- 10:05three thing you have to understand.
- 10:07Okay, this is very very very important.
- 10:09Okay. And this single line whatever I
- 10:11have added here. This is the main
- 10:12foundation of your backend development.
- 10:14Whatever you are going to do with
- 10:16JavaScript. So that means NodeJS is JS
- 10:19runtime. Runtime means we are running
- 10:21some code and this code we are not
- 10:24running inside the browser. We are
- 10:25running outside the browser. And this
- 10:27can be let's say you are running on your
- 10:29computer or on a server. I hope this is
- 10:32clear right. So first of all this is the
- 10:34first thing you have to understand what
- 10:35actually NodeJS uses. So NodeJS uses
- 10:37something called V8. Now what actually
- 10:40this V8 now this V8 is the same JS
- 10:43engine that Google Chrome also uses.
- 10:47Okay. So what actually this is going to
- 10:49do right? So this is going to take take
- 10:51your JavaScript code and it's going to
- 10:53run on your machine. Okay. Now one thing
- 10:56I wanted to mention that when you're
- 10:58learning NodeJS as a developer you don't
- 11:00have to go into depth of learning V8.
- 11:02There is no I'm not saying no point but
- 11:04you don't have to do that. You just have
- 11:06to understand this particular point that
- 11:08NodeJS is not a new language. Okay. It
- 11:10is actually JavaScript. We are running
- 11:12JavaScript only. But this NodeJS has
- 11:15some extra server side capabilities. So
- 11:17that NodeJS we can run inside a browser
- 11:20not inside a browser. Again NodeJS we
- 11:23are running outside the browser. So this
- 11:26is one very very important point I
- 11:27wanted to mention. So first of all what
- 11:29we are going to do right. So let's go
- 11:30here and first you have to understand
- 11:32one very important point and that is
- 11:34that which version we have to uh sorry
- 11:36we have to install and you'll see that
- 11:38here you are going to see something
- 11:40called LTS now what actually this LTS
- 11:43means this one this LTS now this LTS
- 11:46means long-term support now let's say if
- 11:48you're a student or let's say in college
- 11:50or maybe let's say I will consider like
- 11:52less than 3 years of experience right so
- 11:54you are completely like I'll say in
- 11:56beginner phase so what you should do
- 11:58right you should start with this LTS
- 12:00version. So what this means nothing very
- 12:03very simple. So LTS means this is a
- 12:06NodeJS release version and this is a
- 12:08stable version. Okay means what will
- 12:11happen? Let's say there are some bug
- 12:13reported right. So what will happen
- 12:14right? So they are going to fix that
- 12:16particular bug as soon as possible
- 12:18because lot of developers uses NodeJS
- 12:20worldwide. So this is uh we called as
- 12:23long-term support version. So this is as
- 12:26LTS. So what you have to do right
- 12:28whatever you are using okay let's say
- 12:30you're using Windows you using Linux or
- 12:32something else try to download the
- 12:35latest version of the NodeJS and latest
- 12:38version in the sense I'm talking about
- 12:39the LTS version of the NodeJS you don't
- 12:41have to do anything just have to install
- 12:43this and once this is done you'll see
- 12:45that NodeJS will be installed now here
- 12:47you will see something as npm so what
- 12:50actually this npm is now npm is actually
- 12:52the node package managers node package
- 12:55manager so Here I'm just going to do so
- 12:58your npm is node package manager. So
- 13:04what this is going to do? So this is
- 13:06going to help you to install various
- 13:08packages. It's going to help you to run
- 13:10your project scripts. So we are going to
- 13:12have some scripts you'll understand and
- 13:14also the most important part it will
- 13:16help you to manage your dependencies
- 13:18using package.json. So we have something
- 13:20called as package.json file. Okay. So
- 13:23this is why we need this npm. Now you'll
- 13:27see that once you do that okay so just
- 13:29download it and install it's very very
- 13:31simple once you'll do that you go to
- 13:33terminal and or maybe you can directly
- 13:35check from here. So what you have to do
- 13:37right you are going to do node version
- 13:39v. So this v basically we wanted to
- 13:42understand what is the version of node
- 13:43that we are running in this particular
- 13:45machine and if I enter you see that you
- 13:47are going to have this 24.14.0 you this
- 13:51is actually not the LTS version but it's
- 13:53fine if you want you can directly
- 13:55download this version from here also
- 13:57okay so this is the first thing I wanted
- 13:59to mention so now next thing will be
- 14:01what we going to do right here also
- 14:04let's do one thing okay I wanted to show
- 14:06you the npm version also so if you do
- 14:08npm version B you are also going to get
- 14:10the npm version here so now first what
- 14:13we have to do right so first you see
- 14:15that we don't have anything here right
- 14:16so we don't know what file we have to
- 14:17create what folder structure you should
- 14:19normally
- 14:20create while creating any products and
- 14:22levelable NodeJS application. But before
- 14:23that what we have to do right we have to
- 14:24understand some of the concept but
- 14:26before doing that first let's do one
- 14:28thing let's create our package dojson
- 14:30file. So what actually this package
- 14:32dojson file means because this is where
- 14:34we are going to start and learning each
- 14:36and everything. So here what I'm going
- 14:37to do right because I already mentioned
- 14:39that npm is your node package manager
- 14:41which help you to run your uh scripts.
- 14:44It's going to help you to create your
- 14:45NodeJS project. It is going to help you
- 14:48to install packages. Basically it is
- 14:50going to help you to manage your
- 14:51project. something like that. So here
- 14:53what I'm going to do right I am going to
- 14:54create a new node project. Okay, new
- 14:57node project. So how I'm going to do
- 14:59that? So I'm going to do npm in it. So
- 15:01init means we going to initialize
- 15:03something and I'm just going to give
- 15:04minus y. Okay, hyphen y. So here what
- 15:07I'm going to do right if I do enter so
- 15:10this is going to create a package.json
- 15:12on your root. Okay, root means whatever
- 15:15folder that you have created on the root
- 15:17you are going to get this particular
- 15:18file. Now this is one of the most
- 15:20important file you have to understand
- 15:21here. Okay, that what actually this file
- 15:23is doing. But before that what I'm going
- 15:25to do right I'm going to install some
- 15:28dependencies and uh and dev
- 15:30dependencies. So I'm pretty sure if you
- 15:32don't know the terms so I'm going to
- 15:33explain what actually that is. So what
- 15:35dependency I'm going to create and why
- 15:37we need those dependency because
- 15:39whenever we are going to create any
- 15:41project right so we need some packages
- 15:43or some out ofthebox things that we
- 15:45don't want to write. So those out
- 15:47ofthe-box things or features will be
- 15:49provided by those packages. Right. So
- 15:51what I'll do right for now because we
- 15:52are going to use Typescript. So I'm
- 15:54going to install TypeScript here. Okay.
- 15:56And here what I'm going to do right. So
- 15:58first of all or maybe let's do one
- 16:00thing. Maybe I'm doing or teaching in a
- 16:02different maybe wrong way. First let's
- 16:04understand what actually this package
- 16:05do.json is and then I'm going to create
- 16:07it. It will be easier. [snorts] So first
- 16:09you'll see that you are getting quite a
- 16:10few things. Okay. First you have to
- 16:12understand what actually each and every
- 16:14property is. So name name is your
- 16:16project identifier. Okay, this is your
- 16:18project identifier. Version is your
- 16:20current release version. So this is your
- 16:22project release version. Description
- 16:24whatever description you wanted to give.
- 16:26I think I don't have to explain here.
- 16:28Then you have the important part you
- 16:30have to understand here you have the
- 16:32type. So type here is common JS. Okay.
- 16:34So this is your module system hint. Then
- 16:37we have very very important part which
- 16:39is script. Now this script is going to
- 16:42help you to do something. Now I'm not
- 16:44going to explain what actually we are
- 16:46going to do because these when we are
- 16:47going to implement a project and run the
- 16:49script you'll understand. Now when I'm
- 16:51talking about it's going to help you to
- 16:52do something means here you can create
- 16:54various script. For example let's say I
- 16:56wanted to run some test. So I can keep a
- 16:58test script. I wanted to run my local
- 17:01server. I'm going to create a script
- 17:02which is going to help me to run the
- 17:04local server. I'm going to build
- 17:06something. I can add a build script.
- 17:09You're getting right. I wanted to start
- 17:11this particular project. So I can add a
- 17:13start script something like this. Okay.
- 17:16Now why actually we need this particular
- 17:18package dojson file. Now simply you have
- 17:19to understand that this particular file
- 17:23I usually call this one as identity
- 17:25card. So that mean this particular file
- 17:26is going to tell me that what actually
- 17:28this project is and what dependencies
- 17:31this project is holding or what
- 17:33dependencies this particular project is
- 17:35using. Now let's see if I currently do
- 17:38this. I have five developers that we are
- 17:41working in a team. Now the second
- 17:44developer recently joined your team and
- 17:46then you wanted to that particular
- 17:48developer want to understand okay what
- 17:49all things you are using in this
- 17:50particular project. If you share this
- 17:52particular file right package.json so
- 17:54this developer will understand okay
- 17:56these all script I have this is my main
- 17:59main entry file. Okay this is my root
- 18:01file that you are going to understand
- 18:03later and okay I have these
- 18:04dependencies. Okay, that mean these
- 18:06dependencies we are using and I also
- 18:08have these dev dependencies. Now here we
- 18:10are going to understand what actually
- 18:11dependencies versus dev dependencies but
- 18:14before that you need to understand that
- 18:15this project is nothing but holding the
- 18:17information that okay what actually this
- 18:20project is doing as simple as that. So
- 18:22now what I'm going to do right first I
- 18:24am going to install some of the uh
- 18:26dependencies that we need. So first I
- 18:28need what because we are using
- 18:29typescript we need typescript what
- 18:31actually typescript is going to do. So
- 18:33here I'm just going to do npm and I'll
- 18:35do install and I'll do hyphen d. You'll
- 18:37understand what actually you're doing.
- 18:38So you're installing a dev dependency.
- 18:40Here I'm just going to first install
- 18:41typescript. What typescript is going to
- 18:43do? This is going to compile your
- 18:44typescript to your javascript. Then we
- 18:47have something called tsx. Very very
- 18:49important. Why? When we're going to run
- 18:51typescript, what this particular package
- 18:53is going to do? This is going to run
- 18:54your typescript directly whenever you
- 18:57are going to do the development. Okay?
- 18:59You don't have to create any manual
- 19:00build setup for this. And for this we
- 19:03also need a types and we need the node
- 19:06types. So because we are using
- 19:08typescript so we need to do what we need
- 19:10to give type definition right and this
- 19:12is what we are going to use for that. So
- 19:14we need three packages typescript tsx
- 19:17and types/ node. So this is the first
- 19:20thing we are going to do. I will install
- 19:22this and we'll see that what will happen
- 19:24the moment we are going to install there
- 19:25will be a node modules will be created
- 19:27and these node modules is going to hold
- 19:29all this information just now we have
- 19:31installed. Okay. So this node modules is
- 19:33going to have all this. You see that we
- 19:34have typescript tsx types. Now you don't
- 19:37have to focus or you don't have to
- 19:38change anything inside of this. Okay. Do
- 19:40not do anything until unless you're
- 19:42creating your own common package. Let's
- 19:44say you're creating your own custom
- 19:45package and you wanted to check
- 19:47something. Right? Normally you should
- 19:49not change anything inside the node
- 19:51modules. Okay. So you don't have to do
- 19:52anything here. You'll see that based on
- 19:54whatever packages we are doing or
- 19:56installing it will create those folders
- 19:58or dependencies and the subd
- 20:00dependencies. So you can see that you
- 20:01have the subfolder also inside this. So
- 20:04this is the first thing that we have to
- 20:06do. Now what I will do right now let's
- 20:08say I wanted to add some particular
- 20:12file here. Okay. Okay. This is the uh
- 20:14script I wanted to add. Okay. Now before
- 20:16doing that also you have to understand
- 20:18one thing that currently we don't have
- 20:19any particular file file in the sense
- 20:21let's say whenever we are going to write
- 20:22some code we need to write code right.
- 20:24So in that case we need to have some
- 20:26entry file. That means whenever I'm
- 20:28going to run this particular project,
- 20:30TypeScript will understand okay this
- 20:32particular file code I need to see or I
- 20:35need to watch and based on that I need
- 20:36to run that. So that means we need to
- 20:39create some file. But before that also
- 20:41we need to understand one very important
- 20:42concept which is if you're a beginner
- 20:44this can be a very important interview
- 20:46question also that what actually
- 20:47dependencies and what is the dev
- 20:50dependencies extremely important
- 20:52interview question. I myself got this
- 20:54question so many times. So to uh explain
- 20:58in a simpler way dependencies will be
- 21:01dependencies are something is needed
- 21:04when the app is running. Dev
- 21:06dependencies is something needed only
- 21:10when we are developing or we are
- 21:12building. So let's put in this way. So
- 21:14I'll do dependencies
- 21:16or something needed when the app is
- 21:21running and dev dependencies will be
- 21:30something needed when or while
- 21:33developing
- 21:36or building. Okay. Now here you have to
- 21:41understand a couple of things. Okay.
- 21:42These are very important. So you have
- 21:44dependencies. Dependencies are something
- 21:45like runtime package. Extremely
- 21:48important concept. Okay. You have to
- 21:50understand all of these because if you
- 21:51really wanted to learn NodeJS and
- 21:53everything, you need to know in details
- 21:55so that you don't depend on AI tools all
- 21:58the time because as a developer you
- 21:59should know all these basic things and
- 22:01you are going to believe me or not. Some
- 22:04people also lost inter not lost some
- 22:07people also failed interview by not
- 22:09answering these basic questions also. So
- 22:11when we are talking about dependencies
- 22:12what actually dependencies are right so
- 22:14you have we call this one as runtime
- 22:16packages right so whenever you're going
- 22:19to write some code right you're going to
- 22:20import something okay or maybe let's say
- 22:23the your code whatever you're writing it
- 22:25imports and those particular packages we
- 22:27uses while serving request or we are
- 22:30doing something real work for example
- 22:32let's say there is a HTTP framework that
- 22:35your serverts file imports whenever your
- 22:38code will start now this will be your
- 22:41runtime packages means you are running
- 22:43some actual code development packages
- 22:46will be your dev dependencies means
- 22:47something you use to write compile or
- 22:50check your code okay not something that
- 22:54you need when you are going to run that
- 22:56particular code in production very very
- 22:58important okay so I'm going to repeat
- 23:00once again runtime packages will be
- 23:02something your code imports and uses
- 23:06while doing an actual work development
- 23:08related packages or dev dependencies
- 23:10will is something something you use to
- 23:12write compile or check code but not
- 23:15something you need while running that
- 23:17particular code in production. Okay. So
- 23:19that will be the
- 23:21uh difference between dependencies and
- 23:22the dev dependencies. Now you can see
- 23:24that clearly you will see these dev
- 23:26dependencies of this TypeScript compiler
- 23:28or dev runner or let's say we have a
- 23:30lint package this you need when you're
- 23:32going to do your development. Okay. So
- 23:33this will be the proper definition.
- 23:36Now what will be the dependencies then?
- 23:38So I'll take one example let's say
- 23:40express. Now express will be your
- 23:42dependencies. Now express is your actual
- 23:45running API server that you're going to
- 23:47import and you're going to use in your
- 23:49code. So that means that particular code
- 23:51when you're going to run inside in
- 23:52production send you need that express
- 23:55package right? So whenever you're going
- 23:56to run or build that code in production
- 23:58first you need to install that express
- 24:00package then only you can use that
- 24:02right? Let's say what will happen? Let's
- 24:04say you're missing some dependencies and
- 24:05you try to run. So your build will fail
- 24:08and your app can crash also you're
- 24:10getting. So that is the definition or
- 24:12difference between dependencies versus
- 24:14the dev dependencies. So I think this is
- 24:17all about this one. Now what else? Maybe
- 24:19let's understand some of the npm uh
- 24:22script also. Right? Let's do one thing.
- 24:24What are we going to do? Right? Maybe
- 24:25let's create one folder here. So I'm
- 24:28going to create a source folder. Okay.
- 24:29Source folder means inside this I'm
- 24:31going to write all my code and here I'm
- 24:33going to create index.ts file. So
- 24:35index.ts means this is where I'm going
- 24:37to write some code. Okay, we are not
- 24:39doing anything but we are going to do
- 24:41something. First you have to understand
- 24:42that how we are going to write script
- 24:46inside this package dojson and what
- 24:48actually this script means? You have to
- 24:51understand that npm scripts are nothing
- 24:53but that you are doing something. I
- 24:54already mentioned that one. Let's say
- 24:56this particular file I wanted to run. So
- 25:00now for this task what I'll do right I'm
- 25:03going to add one particular script and
- 25:05whenever I'm going to run that script as
- 25:07a shortcut your tsx or your typescript
- 25:10will understand I need to run this
- 25:11particular file. So let's see how this
- 25:13will work. For example let's say
- 25:16first of all I'm just going to do like
- 25:18this here. I'm just going to add a de
- 25:20script. Let's say whenever I'm going
- 25:22doing development I need to do what? I
- 25:24need to test my changes right that I'm
- 25:26doing something if something is
- 25:27breaking. So here what I'll do remember
- 25:29I already mentioned we installed this
- 25:31tsx package right. So this tsx is going
- 25:34to do what? It's going to help us to see
- 25:36whenever I'm going to run this
- 25:38particular script that whether this
- 25:40particular file code is running properly
- 25:42or not. So how I'm going to do that? So
- 25:44I need to watch this file right? I need
- 25:46to run this file. So use something
- 25:48called watch. So I'll do tsx watch means
- 25:50ts I'm telling tsx to watch which file
- 25:53source/index.ts
- 25:55file. So that means I'm simply telling
- 25:57that okay whenever I'm going to do
- 25:59right. So whenever I'm going to run this
- 26:01particular npm rundev so if I just do
- 26:04here nothing will happen. Okay. So maybe
- 26:06let's do one thing. We can also console
- 26:09log something here. So you'll
- 26:11understand. So I'll do right. I'll just
- 26:13do a log and I'll do hello NodeJS.
- 26:18And now if I just do npm rundev. So
- 26:21whenever I'm doing npm. So I'm telling
- 26:22npm to run what? Dev script. So npm will
- 26:25go here. It will check. Okay dev means I
- 26:27need to run this particular file. So
- 26:30sour/index.ts. If I run here, I'll see
- 26:32that you're getting node hello nodejs.
- 26:35Right? So that means tsx is now watching
- 26:37this particular file. And here if I
- 26:40let's see if I do save and you see that
- 26:42now you're getting hello node js2. Now
- 26:44this is the important of this particular
- 26:46term. watch means every time you're
- 26:48going to change something change means
- 26:50you are changing the code right let's
- 26:51say I change from hello NodeJS to my
- 26:54name right so if I save this you'll see
- 26:56that this DSX this particular package is
- 26:58watching your code real time and
- 27:00whenever you're making any change it is
- 27:02updating this one so this is what script
- 27:04is going to do now for example in this
- 27:06particular script what you're doing we
- 27:08are doing some development work now
- 27:09let's say you're working on some
- 27:11database and in the database you need to
- 27:13do some migration you can add a
- 27:14migration script and you can do whatever
- 27:16what it needs to do. Let's say you need
- 27:17to do something test, right? You are
- 27:20going to write some unit test. So you
- 27:22can do also like this. Okay, unit test I
- 27:24need to write or maybe unit something
- 27:26like this and you can write a command
- 27:27here. Let's say you are doing some ite
- 27:30test. So you can add a script and you
- 27:32can do whatever command you need to run.
- 27:33So whenever now next time you will do
- 27:35npm run ite so it's going to check this
- 27:38particular script and it's going to run
- 27:40that. So this is about the npm script.
- 27:42Why this is very important? because you
- 27:43need to understand all of these before
- 27:45we even start writing any code. Okay. So
- 27:48now I think we have covered almost all
- 27:49the things and we also covered what
- 27:52actually package package.json is. Node
- 27:55modules will have all your dependencies
- 27:57and the subd dependencies. Okay, you
- 28:00don't have to change anything here. Now
- 28:02let's understand one very important
- 28:04concept. Now I I'm not sure if I should
- 28:07explain this one now but still I'll give
- 28:10you some prop basic idea. Okay, we are
- 28:12not covering much and that is that how
- 28:14should you create a proper folder
- 28:16structure in a nodejs although we
- 28:18haven't covered any uh express or
- 28:20anything. Okay. So what you should do
- 28:22right I'm going to give very basic
- 28:24overview. So don't take this one as a
- 28:26complete idea. Okay. Because we are not
- 28:28going to cover like middleware routes
- 28:30and everything. What you should do right
- 28:33how I'm going to explain or when I
- 28:35actually run this nodes you have to
- 28:37understand that whenever you're going to
- 28:38create a new folder or you're going to
- 28:40create any file you need to understand
- 28:42whether that particular file actually
- 28:45holding any responsibility or not. And
- 28:47this is the best way to put anything.
- 28:50what I'm talking about holding any
- 28:51responsibility because let's say
- 28:53currently I'm creating a util folder.
- 28:56Okay. Now in this utils folder you can't
- 28:59put any routing related logic. Routing
- 29:02means okay whenever my UI is going to do
- 29:05a API call and then whenever you will do
- 29:07API call it will do what? It's going to
- 29:09check that particular route or that
- 29:10particular API route is present or not.
- 29:12So that means if you're putting your
- 29:14routing logic inside utils folder that
- 29:16doesn't make any sense right that is
- 29:18also bad. Next let's say you are mixing
- 29:23your service logic
- 29:26and your DB logic. DB logic means I'll
- 29:29do DB repository pattern. DP repository
- 29:32pattern means where you're going in
- 29:33which file you are going to actually do
- 29:34some DB queries at the same time there
- 29:36also you are doing service logic. Now
- 29:38see you can do that but this is not a
- 29:40proper folder structure. Okay. Similarly
- 29:43let's say you are creating a routing and
- 29:45in this routing you are adding service
- 29:47logic. This is also bad. Okay. So this
- 29:49is also bad practice. So what I mean by
- 29:52this, I'm not going to cover any terms
- 29:53for now. But you have to understand
- 29:55remember this particular line that
- 29:57whenever you're creating any folder or
- 29:59any file, you need to understand whether
- 30:01that file holding any responsibility or
- 30:04not. As simple as that. Okay. Second,
- 30:07there is one more rule. You should
- 30:09always have one always have one entry
- 30:13file. Entry file or one root file. Now
- 30:16in this case our root file is
- 30:18source/index.ts. So whenever I'll go
- 30:20here right package.json whenever I'm
- 30:23going to run this particular command
- 30:24these files should hold each and every
- 30:27information of your entire code. What
- 30:29else will happen? Let's say I'm just
- 30:30going to give that you have two routes
- 30:33you are using something like that or two
- 30:35utils file helper function you are using
- 30:37that will be best way to explain. So
- 30:39what I'm talking about now let's say you
- 30:42have two more one utils folder and
- 30:44inside that you have two files one file
- 30:46you are using here in this particular
- 30:48root file and the second file you are
- 30:50not using okay now in your package.json
- 30:52JSON when you are going to run this
- 30:54particular command right ntpm rundev
- 30:56you'll see that the first file or first
- 30:59util file whatever you have imported
- 31:01that output you'll be able to see but
- 31:03because you are not importing the second
- 31:05file so how [snorts] we are going to
- 31:06check that you're getting what I'm
- 31:08talking about so my point is your root
- 31:10file this particular root file should
- 31:13contain
- 31:15these files should contain the
- 31:17information of
- 31:20all or information of your entire source
- 31:25code at the end what you're going to do
- 31:28right you're going to import all of this
- 31:29but you need to import let's say you're
- 31:31not importing something it's not going
- 31:33to work you're getting what I'm talking
- 31:35about so something like this so now I
- 31:38think this is all about now again this
- 31:40is not the folder structure completely
- 31:41we have done because until unless we
- 31:43start express or we'll do some practical
- 31:46project it will be very difficult but
- 31:47till this part this is going to give you
- 31:49almost all the ideas that what actually
- 31:51node J is why we need to install the LTS
- 31:54version of NodeJS. We should install
- 31:56mean we no need to you can install the
- 31:58latest version also but it's always
- 32:00better to install the LTS version. Third
- 32:02what actually npm is what is
- 32:04package.json right and some of the very
- 32:06very important concept right uh
- 32:08dependencies versus dev dependencies
- 32:11what actually npm scripts are and all
- 32:13these things. So with this now what will
- 32:15be the next step right next step will be
- 32:17see we are not going to create any
- 32:18project or anything because we need to
- 32:20cover the npm uh sorry nodejs core
- 32:22modules. Now first of all you need to
- 32:24understand what all core modules we are
- 32:26going to cover here. So what I've
- 32:28decided right I'm going to cover all
- 32:29these. So I'm just going to write here
- 32:31so that you'll understand. So first of
- 32:32all we will start with process object
- 32:36process object of nodejs. Second we are
- 32:38going to cover the path not path maybe
- 32:40let's start with
- 32:42env variables I think I can cover
- 32:44enviables here also
- 32:48or maybe let's do one thing no let's
- 32:50cover process object because env
- 32:52variables we'll do practically when we
- 32:54are going to do project after process
- 32:56object let's cover the path module very
- 32:58very important then we're going to cover
- 33:00the file system or file system module
- 33:03then I'll cover the event loop basics
- 33:06because these things you need to know
- 33:08else like it will be like incomplete.
- 33:10Okay. So event loop we are going to
- 33:12understand that then we are going to
- 33:14understand some call back extremely
- 33:16important means like two important
- 33:18concept. Okay. Why this is too
- 33:20important? Because this is extremely
- 33:22important for beginner level of
- 33:23interview questions right. So when
- 33:25you're giving interview these interview
- 33:27questions are very important. What is
- 33:28the call back? Why we need promise? Why
- 33:31we need asynav? What is the difference?
- 33:34Okay, how this is relevant because see
- 33:36callbacks are extremely important
- 33:38concept. Okay, so whenever you're going
- 33:39to write any function let's see that
- 33:41you're going to install some third party
- 33:42packages there will be too much using of
- 33:45call back promise and async coverage. So
- 33:46these three are very important
- 33:48non-negotiable you have to learn all of
- 33:49these we are also going to learn some
- 33:51event emitter concept then buffers we
- 33:55also buffers what else we also
- 33:57understand some of the uh okay crypto
- 33:59module we have to understand see that
- 34:01that's the reason I have actually taken
- 34:04all these important important modules
- 34:05also everything we can't cover then very
- 34:08important errors how we can do error
- 34:10handling properly without using any
- 34:12packages or anything proper error
- 34:14handling and also you are going to cover
- 34:16some of the basics stream concept. Now
- 34:19here you'll see that we are not going to
- 34:20cover some of the thing for example here
- 34:23we are going to not cover worker threads
- 34:26and we are not going to cover here child
- 34:29processes. Now these two concept plus
- 34:33uh some very advanced
- 34:36advanced level of concepts
- 34:40these will move to NodeJS
- 34:45version sorry NodeJS part three now this
- 34:49will be pure NodeJS advanced module I'm
- 34:51going to create but not this in this one
- 34:54because this doesn't make sense because
- 34:55this will be only too much okay so all
- 34:58these things we're going to cover in the
- 34:59next But you can't skip any of these
- 35:01because everything whatever you're going
- 35:03to learn we are going to actually apply
- 35:04in project directly. So first you
- 35:08complete all of this and then you move
- 35:09to HTTP module. Okay. This is how this
- 35:12course will be built. So first we are
- 35:14going to cover the core modules and then
- 35:15we'll understand node HTTP module that
- 35:17will be a completely different section
- 35:19and then we'll start with our project.
- 35:21So with this now let's move on to the
- 35:23next one. Okay everyone. So now what we
- 35:25going to do right? So we are going to
- 35:26understand all the core concepts and the
- 35:28core modules uh that uh you need to know
- 35:31before you're going to start the HTTP
- 35:32module. Okay. And each and every file
- 35:34that we are going to cover will be very
- 35:36important. Now one thing I wanted to
- 35:38mention I think I already explained that
- 35:40one that there are two or three core
- 35:42modules that we are not going to cover.
- 35:44Okay. And the first one will be uh
- 35:46worker threads and child process. Now
- 35:48child process worker threads will be
- 35:50little complicated concept and once I'll
- 35:53upload the node microservices I think I
- 35:55haven't created any video on node
- 35:57internals and some very advanced topics
- 35:59so there will be uh there is a
- 36:01possibility that if these two videos uh
- 36:04get good response so I'm going to upload
- 36:06a part three okay and that one will not
- 36:08be that much practical so in that one we
- 36:10are going to only cover node internal
- 36:12systems and the advanced core modules
- 36:14okay so first what we need to do right
- 36:17see uh I think we We have already
- 36:18installed all of these correct but you
- 36:20have to understand that when we are
- 36:21using Typescript right so Typescript
- 36:23needs to know that how it's going to
- 36:25compile the code to JavaScript right so
- 36:27we need a TypeScript configuration
- 36:29related file and that file we call as
- 36:31tsconffigjson
- 36:33so what we are going to do right so
- 36:35first of all we have to create a
- 36:36tsconfig dot JSON file and what you need
- 36:39to do right go to Google and just search
- 36:41for I think you can use any AI tools
- 36:43also tsconfig file NodeJS okay so what
- 36:46this particular file So, TypeScript is
- 36:48going to read this particular block
- 36:50whatever you are seeing here and then
- 36:52it's going to decide that how it's going
- 36:54to compile the project. Okay, this is
- 36:56very simple. So, for now what I'm going
- 36:58to do right, you can copy this exact and
- 37:00then we'll paste it here. So, first you
- 37:01need to understand some of the important
- 37:03parts. Okay, you uh it will take some
- 37:05time. Let's say if you don't have any
- 37:07idea. So, once you do some project
- 37:09you'll see that each and everything will
- 37:10be uh you'll be familiar that what
- 37:12actually each and every property is
- 37:14doing. So, first one is target ES22. So
- 37:17this target uh what this particular
- 37:20thing controls so this is going to
- 37:21controls which JavaScript uh version
- 37:24TypeScript is going to compile once it
- 37:26will start compiling your code. Okay.
- 37:28And in this one we're using uh 20 uh
- 37:312022 version because this is actually
- 37:34modern version. So you can use this one.
- 37:35So you can see that they mentioned that
- 37:37this is the compile JavaScript version.
- 37:39Okay. Then we have module note next. So
- 37:42this is also important. So this is going
- 37:43to tell Typescript that which model
- 37:45system we are going to use in the
- 37:47compiled files. Okay. Now in this case
- 37:50what this node next means? So here this
- 37:53is going to match how NodeJS handles
- 37:56your ES module and your package.json.
- 37:59Okay. Now uh let's move on to the next
- 38:02one which is module resolution. This is
- 38:04almost same. Then you have one very
- 38:06important which is root directory. In
- 38:08this one we are going to use the source.
- 38:10So we can also do like this right. So
- 38:11this will be your source. So this is
- 38:13where Typescript is going to check your
- 38:14source code and based on that it's going
- 38:16to compile. Okay. So this will be our
- 38:18source uh folder. We'll keep it like
- 38:21this. This is our output directory. So
- 38:23this is where TypeScript is going to
- 38:25compile all your all your JavaScript and
- 38:28it's going to keep in this particular
- 38:29folder dish folder. Then we have some
- 38:31TypeScript related functionality. If you
- 38:33already learned my TypeScript video, I
- 38:35think I have a two videos. I already
- 38:37covered each and every point in details.
- 38:39strict uh strict true no implicit any
- 38:42and strict null checks all of these okay
- 38:44and now some of the things you have to
- 38:46understand let's go here okay so you'll
- 38:48see that ES module uh inter op so what
- 38:51this is going to do right so this is
- 38:52going to this going to make default
- 38:54imports from your common JS package and
- 38:57it will be easier to use okay this is
- 38:59when this is helpful so this is going to
- 39:01be very helpful when let's say you have
- 39:03a node package and that particular node
- 39:05package actually written in older module
- 39:08style okay So that is the reason they
- 39:10mention that this is going to support
- 39:12common JS and ES module inter. Uh then
- 39:15what else? Okay, this is important. So
- 39:17include is nothing but this is going to
- 39:20this is where you're going to have your
- 39:21code and exclude means we're not going
- 39:23to include node modules and the bish
- 39:25folder where we are going to have our
- 39:28compiled files. Right? So this is where
- 39:29we are going to have your build files
- 39:31and this is your external packages. So
- 39:32we don't need the node modules. Now why
- 39:34this is very important because you have
- 39:36to understand why we need this
- 39:38particular file and you'll see that
- 39:39whatever project you are going to create
- 39:41if that particular project is using
- 39:43TypeScript you are going to see this
- 39:44TypeScript configuration file. So this
- 39:46is one particular uh section we I wanted
- 39:49to cover. Now with this so what are we
- 39:51going to do right? So in the next part
- 39:52we are going to start learning all the
- 39:55core modules and each and every core
- 39:56modules will be very very important. So
- 39:58let's start with that in the next part.
- 40:01Okay. So let's start with the first file
- 40:03which will be so I'm just going to
- 40:04create here 01 and let's do process
- 40:08object. TS.
- 40:11So let's close all of these. So first of
- 40:14all what actually process module is
- 40:16right. So see process is nothing but
- 40:18process usually represent your current
- 40:21nodej or current running nodejs program.
- 40:23Okay what actually we are going to
- 40:26achieve from this particular module or
- 40:28what actually this is going to give you
- 40:30access. So this is going to give you
- 40:32some of the very important concept some
- 40:33of the runtime information you need. For
- 40:35example let's say you need some env
- 40:37variables that we are going to use. What
- 40:39I mean by env variables? So let's say in
- 40:41your project you are going to use some
- 40:42password or maybe let's say you're going
- 40:44to use some secrets. So those secrets
- 40:46you're not going to hardcode in your
- 40:47code right. So what you need to do you
- 40:49need to keep those particular
- 40:52sensitive information in a certain file
- 40:53and we usually call that one as a env
- 40:56file right. So this is where you are
- 40:57going to keep all your env variables. So
- 40:59this process object is going to give you
- 41:00the access or this is going to give you
- 41:02certain functionality so that you will
- 41:04be able to read those variables and you
- 41:06can use in your code. Okay. This is also
- 41:08going to give you some of your command
- 41:10line arguments.
- 41:12Okay. Also, this is going to give you
- 41:14some of the uh methods that from where
- 41:17you will be able to exit your code.
- 41:18Let's say something goes wrong. So,
- 41:20you'll be able to exit. Also, uh I I'm
- 41:22not sure if some other t uh teachers
- 41:25usually cover this. This is also going
- 41:27to give you some process life cycle
- 41:30events. Okay. This is also very very
- 41:32important. Okay. So now how this is this
- 41:36is going to be very much important in a
- 41:37real backend project. We see at the end
- 41:40you have to understand whatever module
- 41:41you are going to cover whether that is
- 41:43actually useful in a real world or
- 41:45NodeJS project or not. Okay. Uh why?
- 41:48Because why it's very important because
- 41:50there is no point of learning all of
- 41:52this if you're not able to use this one
- 41:53in a real project. So in a backend
- 41:55project this is let's say you're going
- 41:57to read some your backend code you read
- 42:00backend code from your env file. So this
- 42:02is where you need this process or let's
- 42:04say maybe let's say you need to read
- 42:06your secrets
- 42:08correct maybe let's say secrets can be
- 42:10your DB URL means your database URL or
- 42:13your API keys or there can be password
- 42:16or there can be some Google O secret so
- 42:19it can be anything right so you need to
- 42:20read all of these so this will be
- 42:22helpful you need to let's say you need
- 42:23to read some CLI arguments
- 42:27in scripts this is where you also can
- 42:30use this right So how you are going to
- 42:33use this one? So we have something
- 42:34called process.inv.
- 42:36But from where this process actually we
- 42:39are going to get. So you need to import
- 42:41something right. So we are going to
- 42:42import process from. So we need to
- 42:45import from node. But this will be from
- 42:48our process model. And also for this we
- 42:50need to add the types I think. Okay. So
- 42:53we need to go to our types sorry
- 42:55tsconfig.json
- 42:57and let's add here. So we going to add a
- 43:00types and types will be node. So save
- 43:04this and here you can see that now you
- 43:07are going to get this. So this process
- 43:09is going to give you something from
- 43:11where you are going to so you'll see
- 43:13that if I just do process env you'll see
- 43:15that this is where we are going to read
- 43:16our env uh variables. But remember when
- 43:20we are going to start our actual node
- 43:22capstone project you'll see that I'm
- 43:23going to explain one particular package
- 43:25that is called env. So I just wanted to
- 43:27mention this one so that you'll
- 43:29remember. So we have something called NV
- 43:31and this package is going to help us
- 43:32directly to read these enviables in a
- 43:35NodeJS project. Okay, this is very much
- 43:37helpful. But for now I think this is not
- 43:39needed. So first you will understand one
- 43:40thing is that this process environ
- 43:44variables. So this process env is going
- 43:46to contains all your env variables.
- 43:49Right? So now let's say what I need to
- 43:52do right maybe what I'm going to do
- 43:54right here. Let's do something like
- 43:55this. So I'm just going to take a con.
- 43:57Okay. And I'm just going to do a node.
- 43:59And maybe what I can do, let's do this
- 44:01one as node env. And now what I'm going
- 44:04to do, I'll do process env.
- 44:07So I'll do process env. And let's say I
- 44:11wanted to take this from my node env or
- 44:13else I'm just going to take this one by
- 44:15default as development. Okay. Now one
- 44:19thing you have to understand here we are
- 44:20not checking that how you are going to
- 44:23use env and everything that we are going
- 44:24to see actually in the practical project
- 44:26we are understanding the core module and
- 44:29what are the bad practices and what are
- 44:31the good practices that you should
- 44:32follow for example what will be a bad
- 44:34bad practice here so let's say you're
- 44:36doing like this so you'll take a con
- 44:38port and you're doing 3,00 you should
- 44:40never do this one okay you should never
- 44:42hardcode enviables
- 44:44anywhere in your source code you can
- 44:46take a default value right something
- 44:48like this you can take take a default
- 44:50value but you should always read this
- 44:51one from a certain file okay so now what
- 44:55we are going to do right also very very
- 44:57important I forgot this one these
- 44:59process enviables uh sorry values that
- 45:02whatever you'll have these will be are
- 45:04always either string or undefined so I'm
- 45:07just going to repeat this one so this
- 45:08process
- 45:10envi
- 45:16string
- 45:17or undefined. Now why I'm actually
- 45:19mentioning this particular point?
- 45:21Consider you have this right? So you're
- 45:23doing port 4,000 something like this.
- 45:25You have this particular value. When you
- 45:28are going to actually use this one using
- 45:30process envile
- 45:35what you should do right let's say this
- 45:36is a number you should always do like
- 45:38this. So you're going to do port and
- 45:40then you are going to actually convert
- 45:41this one to a number. Okay. So you're
- 45:43going to do like this. So process enven
- 45:48code or else this will be 3,000. Okay.
- 45:51So you should always convert this one to
- 45:53that particular data type that you need.
- 45:55Okay. So this is another thing I wanted
- 45:56to mention. Now let's we understand that
- 45:59how we are going to read this although
- 46:00we haven't added any file or those that
- 46:03you are going to discuss later. So for
- 46:05getting noise I think it's coming from
- 46:06outside. Okay. Uh like people will be
- 46:10people will go crazy sometime with this
- 46:12bikes and everything. So you'll get too
- 46:14much message. Okay. So this one thing we
- 46:17understood that okay this is where
- 46:18you're going to read the enviable.
- 46:20Second thing we understood that you
- 46:22should always you should means not
- 46:25should but use I sometimes you can take
- 46:27some values by default. For example
- 46:30let's say if you don't have this value
- 46:31so it will take always from this uh
- 46:34default value. If I hover here you'll
- 46:35see this is what I mentioned that either
- 46:37it will be string or this will be
- 46:38undefined. So if you have something
- 46:40where you need to use that particular
- 46:42value as a number, so you should always
- 46:44convert this one to a number. So these
- 46:46all the practices that you should
- 46:48follow. All right. So now moving to the
- 46:49next one. Let's understand another
- 46:51concept. So I'm just going to comment it
- 46:53here. So let's do process.org. Okay. So
- 46:56this is actually arg v. So but inside
- 46:58I'm just going to explain this one as
- 47:01process.org. So let's go here. So you'll
- 47:04see that this is the built-in property.
- 47:05Now here why I actually open this one
- 47:07you need to understand one thing that
- 47:09[snorts] this is a built-in property
- 47:11that returns an array containing the
- 47:14command line arguments. The important
- 47:15part is that the actual custom argument
- 47:18that you're going to pass to your script
- 47:20that will start from index two. Why?
- 47:23Because these usually look like this. So
- 47:26first two elements okay you see that the
- 47:28first two elements in the array are
- 47:29always reserved by the node js. So the
- 47:32first one process.org
- 47:34zero will be the absolute file path of
- 47:37the node just executable running the
- 47:38script. The first index will be absolute
- 47:42file path of the JavaScript file that we
- 47:44are going to execute and the third one
- 47:46will be any custom input that we are
- 47:48giving. So consider like this. So you'll
- 47:50see that we have this particular file
- 47:52right now how this process.org V
- 47:54actually looks like. So you'll consider
- 47:56like this. You have this array and here
- 47:58you're going to have the first one. So
- 48:00first one will be the path to the node.
- 48:02So this will be the node path. Second
- 48:05one will be path to your file. So to
- 48:08your file means in this case the path
- 48:10will be this path. So let's say if I do
- 48:12a relative path and then if I just
- 48:14replace it here. So this will be the
- 48:16path that node is going to execute. And
- 48:18the third one will be the custom
- 48:20argument that you are going to give. So
- 48:21let's I'm just going to run a start
- 48:23command. So this will start from here.
- 48:25So that is the reason you'll see that if
- 48:27I'm not sure if seen some nodes as code
- 48:30you'll see that whenever there is a
- 48:31process. Mark V you'll see that it will
- 48:34always start from index number two.
- 48:37Okay, index number two means the third
- 48:38place. So first two always will be
- 48:40reserved by node. Okay, so this is the
- 48:42reason this line is very important and
- 48:44also this is a very important interview
- 48:46question also. So let's see this one
- 48:48with example. So I'm just going to
- 48:49comment it from here also. Let's remove
- 48:51this. What I'm going to do right, I'm
- 48:54going to take a command. Okay, now
- 48:56you're going to understand some of the
- 48:57concept. Normally in other uh projects
- 48:59or other videos you'll see that people
- 49:01will not cover all of these. So here I'm
- 49:03just going to take the uh this process
- 49:06with index 2 and then I'm just going to
- 49:08take by default this will be our start
- 49:10command. Now what you can do right you
- 49:13can also run different flags very very
- 49:15important concept. So let's say I wanted
- 49:17to I wanted to take a fail crash and
- 49:19also uh sorry fail command and also I
- 49:21wanted to take a crash flag not command
- 49:24my bad. So one I'm just going to take a
- 49:26fill
- 49:29uh flag and also I'm just going to take
- 49:31a grass flag. So how we are going to do
- 49:34this? So I'm simp simply going to do con
- 49:37sud fail and this will be process dot
- 49:40arg and this arg includes fail. Okay so
- 49:45this is the first thing and we are going
- 49:47to do like this and then I'm just going
- 49:49to copy this and let's take one for
- 49:51crash. So this will be my crash. So here
- 49:54I'm just going to check if in this
- 49:56command that we are going to run whether
- 49:57we have this crash flag or not. Okay. So
- 50:00now what we're going to do right we
- 50:02going to run this manually and we'll see
- 50:03that what uh the output will be. But
- 50:06before that you also need to understand
- 50:08another very very important uh uh event
- 50:10and that will be process on. Okay. So
- 50:13this is a method and here you can use
- 50:15this exit method. What this exit method
- 50:18is going to do. So this is not method
- 50:20this will be event. Okay. So this exit
- 50:22event is only helpful or only useful for
- 50:26the final synchronous code. Okay, you
- 50:29don't have to start you do not do not
- 50:31start as sync here. Okay, now why you
- 50:34might ask me? Because at this point when
- 50:36you're going to run this process on
- 50:39right node is already setting down. So
- 50:41node is already setting down. So you
- 50:43don't have to do any async work here.
- 50:46Okay. So when you're going to use this,
- 50:47when you're going to use this one, the
- 50:49best use cases will be let's say you're
- 50:51going to use some final log or maybe
- 50:53let's say you are going to do some final
- 50:55cleanup. This is extremely helpful. So
- 50:57what you can do right so we have process
- 50:59do onexit and it's going to take a call
- 51:00back and here I'm just going to give a
- 51:02code and let's say I'm just going to do
- 51:04a log here that let's do a log and
- 51:07[clears throat] let's take so I'll just
- 51:08do process finished
- 51:12uh with exit code and then let's pass
- 51:14the code here. Okay. So now let's save
- 51:17this. So now what you're going to do
- 51:18right and also uh maybe let's take one
- 51:22more uh function here. So let's do
- 51:25function and I'm simply going to do run
- 51:27app
- 51:30and we'll take void.
- 51:33And here what I'll do right I'm simply
- 51:34going to do log. Let's log this uh
- 51:38command also. I'm just going to uh what
- 51:41we can do right? I think let's skip the
- 51:44uh this command only. Okay, save this
- 51:47and also now what you're going to do
- 51:48right. So first of all we have to check
- 51:50if this is failing or not. So I'm just
- 51:52going to do if this fails if should
- 51:55fail. So in that case I'm just going to
- 51:57log error and I'm simply going to do
- 52:00manual
- 52:02uh failure
- 52:04triggered
- 52:05with
- 52:07fail flag. Okay. And then I'm also going
- 52:11to take one more and I'll do if this
- 52:13class. So I'll do suit crash manual
- 52:16crash triggered with crash flag. Okay.
- 52:22And then at the end I'm just going to
- 52:23run this app. So I'll save this. So now
- 52:26let's go to package uh JSON first. Also
- 52:30let's go to our script. And in this uh
- 52:33script maybe first let's take the build.
- 52:35So I'm just going to use the build sts
- 52:39C. And then I'm simply going to take the
- 52:41first one and this will be we are going
- 52:43to use tsx. I'm simply going to do
- 52:46source because we have source inside we
- 52:48have this particular file name. Right?
- 52:50So we'll copy this. So I'll do source/
- 52:54uh 01 process object ds. So now save
- 52:58this. I'll see that what will happen.
- 53:00First I'll go here clear this and then
- 53:04I'm going to do npm run and we have to
- 53:06run the script. So the script will be
- 53:08first 01. This is the script we need to
- 53:11run. Enter. And you'll see that we are
- 53:13getting command start and process
- 53:15[clears throat] finished with exit code
- 53:17zero. Right? And what actually zero
- 53:19means? Now this is also very very
- 53:20important. So zero means success, one
- 53:23means failure. Okay. Now what I'll do
- 53:26right? So I'll go here and see here when
- 53:29this will fail. Now let's say now we are
- 53:31doing a manual failure. That is fine.
- 53:33But consider there is something actually
- 53:34is wrong. And what you need to do right
- 53:37whenever you're going to do any
- 53:38functionality here this is not log right
- 53:40because see when you're going to actual
- 53:42production level application you're
- 53:44going to not going to log something
- 53:45you're going to do actual some
- 53:46functionality then you're going to do
- 53:49process.exit and you'll see that this is
- 53:51the reason in some code you'll see this
- 53:53process.exit
- 53:54one and this one means this is a
- 53:57failure. You're getting the same thing
- 53:58you have to do here also. Do process
- 54:00do.exit exit and then you'll do one. Now
- 54:04I'll save this and you'll see that first
- 54:05of all what I'm going to do I'll clear
- 54:07this and now I can directly do like
- 54:08this. I'll do npm run 01
- 54:1201
- 54:13and then I'm going to do start. Okay, so
- 54:16this is going to start it. So you'll see
- 54:17that we getting start command but the
- 54:19moment I'm just going to do npm run or
- 54:22let's write it. So I'll do npm run 01 -
- 54:27start. And now I'm going to give a flag
- 54:29here. So I'll do hyphen -y fail. Okay.
- 54:32And you see if I do enter and you'll see
- 54:35that now manual failure triggered with
- 54:37fail flag. And there is process finished
- 54:39with exit code one. Okay. The same thing
- 54:42you can do with crash also. So we'll do
- 54:45npm run 01 -
- 54:48start and then I'll do hyphen crash. And
- 54:51if I do enter here, you'll see the
- 54:52manual crash is now triggered and
- 54:54process finished with exit code one. So
- 54:56this is how you to use command line
- 54:58arguments. Okay, this is extremely
- 54:59important concept. You you don't have
- 55:01any idea how important these things are.
- 55:03You'll see that when you're going to do
- 55:04some express level of project, right?
- 55:05Let's say you're using express and
- 55:06you're creating uh some complicated rest
- 55:08API, you'll see that in your code, there
- 55:10is a possibility you're going to have
- 55:12this line or you're going to have this
- 55:14particular line. Maybe you have you have
- 55:16read some other source code, you'll see
- 55:18that this line might be present. And
- 55:19this is the actual reason of using this
- 55:21process.exit of zero and process.exit of
- 55:24one. Okay. So now I think this is uh
- 55:28done. I think we don't have that much
- 55:29practical because but still you need to
- 55:31know all these concept whatever you have
- 55:33covered. So with this now let's move on
- 55:35to the next mod module which will be uh
- 55:38let's cover the crypto module because
- 55:40crypto is one of the most important and
- 55:41you are going to actually use crypto
- 55:43module in a rest API project. So first
- 55:45we're going to cover the crypto module
- 55:46and then we'll move to the some other
- 55:48modules in the next part. So moving to
- 55:51the next one. So let's create one more
- 55:53file. I hope that you all are writing
- 55:55all of this along with me right why
- 55:57because if your basics are not clear
- 55:59what I always mention in all my videos
- 56:01you're not going to be a good developer
- 56:03in any tech whatever you will learn okay
- 56:06so what when I'm talking about writing
- 56:08means try to implement the code that I'm
- 56:11writing and then pause the video or
- 56:12maybe let's say later also you can do
- 56:14and then go and experiment and learn
- 56:16some more things on the same module and
- 56:18then try to write some more code okay so
- 56:20basically what I'm trying to say do not
- 56:22just write what I'm writing write what
- 56:25I'm writing plus write something that
- 56:27I'm not writing you are getting what I'm
- 56:29talking about means that what will
- 56:31happen let's you are doing crypto module
- 56:33you're going to learn so from a uh
- 56:35tutorial point of view what how much I
- 56:37can do I can give you the basic idea
- 56:39this is a module this is the reason this
- 56:41module exist and these all are the
- 56:43methods I'm not going to cover each and
- 56:45everything if you go to the actual note
- 56:47documentation you'll see that this
- 56:48particular module if you try to learn
- 56:50each and everything it will take you
- 56:51seven 7 days but This is not how you're
- 56:54going to learn right when you're going
- 56:56to learn a certain thing you have to
- 56:58understand what are the things that we
- 57:00usually actually use in a real project.
- 57:03So that means what what you need to do
- 57:05you need to learn from a video learn or
- 57:08uh read some of the documentation the
- 57:10best will be read some blogs and then
- 57:12make use some some some AI tools and
- 57:14everything and then try to write some
- 57:16code by yourself. So first what we are
- 57:18going to do right we going to understand
- 57:20one more module which is called crypto
- 57:22module and you'll see that you are
- 57:23actually going to really going to use
- 57:25this one in so many projects. Okay, this
- 57:27is one of the most important module that
- 57:28you should know. Now, this you're going
- 57:31to usually import from again node.
- 57:34Okay, so you can do direct like this.
- 57:36Okay, crypto. But I usually like this
- 57:37node and then I'm simply going to do
- 57:39crypto. So this will be my crypto
- 57:41module. What actually crypto module is
- 57:43again this is a built-in NodeJS module.
- 57:46Okay, so this is already built-in NodeJS
- 57:48module system. Built-in NodeJS module
- 57:51system. Okay. So now what you're going
- 57:54to do, right? So first of all you have
- 57:55to understand that these you're going to
- 57:57do or these you're going to use any
- 57:59security related tasks as simple as that
- 58:02means consider some of the things I'm
- 58:04going to give you are going to use a lot
- 58:07creating random UU ids or you're going
- 58:10to use normal ids you you can also use
- 58:13this one create
- 58:16secure token very important hashing data
- 58:20extremely important okay you are going
- 58:22to use a lot to hash data
- 58:25also very important sometime we going to
- 58:27use this one to verify I'm just going to
- 58:29do to verify if the data was changed or
- 58:35not changed so I'll do data was not
- 58:36changed something like this okay uh what
- 58:39else also encryption and uh decryption
- 58:42so I'll do encrypt and decrypt so all
- 58:46this will be the most used functionality
- 58:48that you're going to do in this one
- 58:50we're going to understand some of the
- 58:51very important methods so first will be
- 58:52crypto dot
- 58:54random I think it's called U ID this one
- 58:58okay random UI ID then random bytes
- 59:02something called create uh has and then
- 59:06also you have create hmax something like
- 59:07this and you'll see that if I do create
- 59:09only you're getting this many things and
- 59:11it's almost next to impossible for me to
- 59:13cover each and everything right so we're
- 59:14going to only cover the things that we
- 59:16usually use most so first let's start
- 59:19with crypto dot random uu U ID. Okay. So
- 59:25why why actually you need this one. So
- 59:27first of all you have to understand what
- 59:28UID means. So this is means uh if I'm
- 59:31not wrong it's called uni universal uh
- 59:33unique identifier something like this.
- 59:35Let's check once. So I'll do UID full
- 59:39form universal or universal versially
- 59:42I'm not universal unique identifier.
- 59:44Okay. So universal unique identifier. So
- 59:47this is nothing but it's a uh 128 bit
- 59:50level. But let's understand how much I
- 59:52know. Now I'll try to explain in a
- 59:53simpler way. So U ID I think we have
- 59:56already seen it's called universal
- 59:57unique identifier. This simply means
- 59:59when you need a unique ID right? So you
- 1:00:01need a unique ID you're going to use
- 1:00:03this. This can be most used will be user
- 1:00:05ID something like this. You have your
- 1:00:07order ID. You're creating some payment
- 1:00:08system session ID. Okay this is also
- 1:00:11important or request ID all these
- 1:00:12things. Okay. Now here uh what we are
- 1:00:16going to do? So let's I'm just going to
- 1:00:18do this one as request ID. And you
- 1:00:20simply going to do crypto dot random uu
- 1:00:25id. That's it. You don't have to do
- 1:00:26anything here. And you just simply do
- 1:00:28log. And I'm simply going to log this
- 1:00:31request ID. Okay. So if I save this. So
- 1:00:34let's run all of these side by side. So
- 1:00:36I'll go to package.json. I'll create one
- 1:00:38more. So I'll do 02. This will be tsx
- 1:00:42source slash
- 1:00:44and let's copy the name
- 1:00:48dot ts. Save this and then we going to
- 1:00:52clear and then I'm just going to run npm
- 1:00:54run 02. Why? Because we have this
- 1:00:57particular script. And if I do enter and
- 1:00:59you'll see that you're getting a unique
- 1:01:01UU ID. Okay. So next one. So let's go
- 1:01:04here. So next will be random bytes.
- 1:01:07Okay. So you're going to do crypto dot
- 1:01:09random bytes.
- 1:01:12Okay. Why actually you need this? You
- 1:01:14also need to do actually use cases,
- 1:01:15right? So this will be when you're going
- 1:01:16to create secure random bytes. This is
- 1:01:19usually useful very useful when you're
- 1:01:21going to do some pass password reset
- 1:01:23feature. So let's say you're going to
- 1:01:25create a password reset token and then
- 1:01:27user is going to validate you are going
- 1:01:29to validate validate the user based on
- 1:01:31this reset token or let's say you wanted
- 1:01:33to pass a email verification token. This
- 1:01:35is also very important, right?
- 1:01:38session secret and also you can also use
- 1:01:42this one in API keys also. Okay. So now
- 1:01:45let's see if I just try to do like this.
- 1:01:47So I do random bytes and then I'm simply
- 1:01:49going to do like this 16. Okay. Okay.
- 1:01:51Let's not import this one directly like
- 1:01:53this for now. Let's keep it. So what
- 1:01:55actually mean by this? So it's simply
- 1:01:57telling that okay give me random bytes
- 1:01:58but give me 16 random bytes. Okay. So
- 1:02:02how this usually works? So first what we
- 1:02:05going to do right? So first I'm just
- 1:02:06going to do a give me reset token. So
- 1:02:09I'm just going to do crypto dot random
- 1:02:11bytes give me 16 random bytes and then
- 1:02:14we are going to convert these bytes to a
- 1:02:16hex format. Okay. So in hex format what
- 1:02:19will happen right? One bite will become
- 1:02:21two characters. Okay. So that means when
- 1:02:24you're going to get a 16 byt mean it
- 1:02:26will become a 32 character string. Very
- 1:02:29important right? So you can just verify.
- 1:02:31So here what I'm going to do so is one
- 1:02:34byte is two characters
- 1:02:38because if I have random bytes
- 1:02:4316 means 32 character string. Let's see.
- 1:02:50Ah my bad. So it's not actually two
- 1:02:52characters but it will become two
- 1:02:54characters string when you convert into
- 1:02:56a hex format. I think this I mentioned
- 1:02:57that you have to convert this one to a
- 1:02:59hex format. So what I mean by this? So
- 1:03:00this is simply telling that you create a
- 1:03:0216 uh bytes but you're going to do a two
- 1:03:05string and then you're going to convert
- 1:03:07this one to hex format. So this time it
- 1:03:09will become I'll show you my bad. Yes.
- 1:03:12In hex format only I'm asking
- 1:03:21yes. So when you're going to format raw
- 1:03:23data as hexadimal every single bite into
- 1:03:26translate into uh exactly two text
- 1:03:29characters means in this case now this
- 1:03:3116 byt will become a 32 character
- 1:03:33string. So this will be a 32 string and
- 1:03:37also we can validate this. So now here
- 1:03:39I'm simply going to do log and I'll do
- 1:03:42reset token save this and let's run once
- 1:03:46again. So I'll do clear
- 1:03:49and you can see that this will be your
- 1:03:51and you can also check this right 1 2 3
- 1:03:534 5 6 7 8 9 10 11 12 13 14 15 16 17 18
- 1:03:5919 20 1 2 3 4 5 6 7 8 9 10 11 12. Okay,
- 1:04:04so this will become 32 characters and we
- 1:04:07can verify is this 32 characters. Why
- 1:04:10I'm explaining you'll understand that
- 1:04:11why I'm going into too much details
- 1:04:13because this is what we are going to
- 1:04:14actually use in a project.
- 1:04:16See this string is exactly 32
- 1:04:18characters. It's a valid MD5 has or a 16
- 1:04:22by hexadimal string. Since 16 byt into
- 1:04:24two characters. So this will be 32
- 1:04:26characters. Okay. And you'll see that
- 1:04:28exactly these particular things you're
- 1:04:30going to use actual project when you're
- 1:04:31going to build this. Let's say password
- 1:04:33reset token feature email verification.
- 1:04:35You're going to use this one. Okay. Then
- 1:04:37next we have something called crypto dot
- 1:04:40I think we let's do create h has because
- 1:04:42this is also important. Okay. Create
- 1:04:43hash. So create h has will be has what
- 1:04:47what actually has is going to do? So h
- 1:04:49is going to convert your data to a fixed
- 1:04:51length string. Okay, fixed length
- 1:04:54string. Very important, right? So now
- 1:04:57consider you have something like this.
- 1:04:58So you have a hello.
- 1:05:01Let's do like this. Hello. And this you
- 1:05:03wanted to convert to a hash. Something
- 1:05:05like this. Now here directly from a hash
- 1:05:09to this hello is not possible. Okay. So
- 1:05:12now what you have to do right hashing
- 1:05:15what whenever you have something called
- 1:05:17hashing it always one way. So that
- 1:05:19simply means let's say you have a input
- 1:05:21that input can become has but you can't
- 1:05:25convert that has directly to the
- 1:05:27original input. You can also verify. So
- 1:05:29if I just do has
- 1:05:32one way means if I if I have a input
- 1:05:38right that become has I can't convert
- 1:05:45that has back to same original
- 1:05:50input correct so let's see
- 1:05:55uh see so it's a strictly one way
- 1:05:57cryptographic operation means you can't
- 1:06:00reverse it. Okay, this is very
- 1:06:02important. So that is the reason I
- 1:06:04mentioned that H is going to convert
- 1:06:05your data to a fixed length string and
- 1:06:08it is one way. One way means if your
- 1:06:10input becomes H has you can't convert
- 1:06:13that has back to the original input.
- 1:06:15It's not possible. But this is actually
- 1:06:17helpful if you wanted to check your data
- 1:06:19whether that particular data changed
- 1:06:21cast key or let's you wanted to compare
- 1:06:23something. So let's say I have a con and
- 1:06:26I'll do text and maybe let's do hello
- 1:06:28node or whatever you wanted to take and
- 1:06:30let's say I wanted to create a has and
- 1:06:32then I'm simply going to do crypto dot
- 1:06:34create has and here I'm simply going to
- 1:06:37give algorithm. So in this case I'm
- 1:06:39going to give sh a uh 256 and here I'm
- 1:06:43simply going to do dot update to text
- 1:06:47and then what you have to do you have to
- 1:06:48digest this one to a hex into hex
- 1:06:52format. Okay. And then you can just log
- 1:06:54this hash here. Okay. So now if I save
- 1:06:59this, I think we have to pass the text
- 1:07:02this one this text. So if I save this,
- 1:07:05you'll see that this will create a hash.
- 1:07:09Okay. So it's going to create a hash
- 1:07:11here. So this is about how you can make
- 1:07:14use of this crypto.re.
- 1:07:16Then we have something called let's do
- 1:07:18this one also, right? Maybe let's create
- 1:07:21uh let's understand one more which is
- 1:07:23also very important that is called
- 1:07:24crypto dot create hmac.
- 1:07:29So what this is going to do? See first
- 1:07:31of all you need to know what actually
- 1:07:33HMAC means. So it means you have a hash
- 1:07:36based message authentication code
- 1:07:39something like this right? It's like a
- 1:07:41hash only. It's similar to h has but
- 1:07:43it's like more like of a think like a
- 1:07:45secret key. You have a secret key. So in
- 1:07:47normal has right if you have a normal
- 1:07:49hash something like this you have a
- 1:07:51normal has
- 1:07:53normal has how it will work. So you have
- 1:07:55a data and you're going to convert this
- 1:07:57one to has something like this in HMAC
- 1:08:00what will happen to explain in a simpler
- 1:08:02way you have a data but at the same time
- 1:08:05let's say this is a secret so you have a
- 1:08:06secret you have a secret okay what I'm
- 1:08:11doing man and this will be your signed h
- 1:08:15okay so this is the difference
- 1:08:18so now okay one of the best use of using
- 1:08:22this create hmac will be whenever you're
- 1:08:24going to do any kind of webbook related
- 1:08:26uh functionality right these can be
- 1:08:27helpful or maybe signed token also
- 1:08:30signed tokens right so let's say I have
- 1:08:33a secret key so I'll do secret key and
- 1:08:36this will be let's say my super secret
- 1:08:42key something like this and then I have
- 1:08:44a message also here so message and let's
- 1:08:47do user
- 1:08:49id equal to 1 and here now you are going
- 1:08:52to create a signature so how We're going
- 1:08:54to create a signature. So, we'll do
- 1:08:56crypto dot create
- 1:09:01crypto dot create
- 1:09:03h uh no create uh hmac here. You're
- 1:09:08going to give so I'll do sh a 256
- 1:09:12and you're going to pass the secret
- 1:09:14here. Okay. So, you have to pass the
- 1:09:15secret here. So, do secret.
- 1:09:19Let's do secret. So, I'll do secret.
- 1:09:24and then you're going to update. So now
- 1:09:25I'm going to update my message and then
- 1:09:27I'm just going to digest this one to X.
- 1:09:32So this is going to create a signature
- 1:09:35has. So this is going to create a
- 1:09:37signature has. Okay. So now you can also
- 1:09:39verify this one. So let's say first
- 1:09:42let's log this
- 1:09:48and let's clear this. And if I run this
- 1:09:50one, you'll see that it's going to
- 1:09:51create this last one. Okay? So it's
- 1:09:54going to create a signature has. Okay?
- 1:09:55Because you're passing a data plus
- 1:09:57secret. Previously, you are only passing
- 1:09:59data. See, you're not passing any
- 1:10:00secret. So that is the difference. So
- 1:10:02now let's say you want to do verify. So
- 1:10:04do con signature
- 1:10:07verify something like this verify. And
- 1:10:11then you can do crypto dot create hmac.
- 1:10:17And here you're going to pass the so
- 1:10:19I'll do sha 256
- 1:10:22pass the secret and here what you can do
- 1:10:25right uh
- 1:10:28same thing you'll do update
- 1:10:31update the message update message mean
- 1:10:34this message and then you're going to
- 1:10:36just do dot digest to x and now what you
- 1:10:41can do right if I do log and you'll see
- 1:10:44so I'll do signature
- 1:10:46is valid and matching and I'm simply
- 1:10:50going to do if this signature that I
- 1:10:51created this one this signature and this
- 1:10:54signature to verify both are same or not
- 1:10:56so you'll understand so do signature to
- 1:10:58verify okay so if I save this and let's
- 1:11:01see what you're getting
- 1:11:05and you can see that is getting
- 1:11:06signature is valid and matching it's
- 1:11:08true and makes sense so this is where
- 1:11:10what you can do right consider this we
- 1:11:11are doing manually but in a real project
- 1:11:13you are going to let's say getting this
- 1:11:15token from some other place And you're
- 1:11:16going to verify. So you'll be able to
- 1:11:18verify this one using this create uh
- 1:11:20HMAC this particular method. Okay. So
- 1:11:23now if you go here right so you
- 1:11:25understood some very important things.
- 1:11:27Random U ID is going to create unique
- 1:11:30ids. Random is going to do what? It's
- 1:11:32going to create random tokens secret
- 1:11:35random tokens. You have create h has
- 1:11:37create is going to do what? It's going
- 1:11:38to create oneway hash. That is very
- 1:11:40important. I already explained. and
- 1:11:42create hmac is going to create h has
- 1:11:44using a secret key using a secret key so
- 1:11:47with this this particular module is now
- 1:11:50completed and now let's move on to the
- 1:11:51next one all right so let's move on to
- 1:11:53the next one which will be uh so let's
- 1:11:55create another file and this will be OS
- 1:11:57module okay now this will be sort but
- 1:12:01still it is actually important so that's
- 1:12:02the reason actually I've decided to keep
- 1:12:04this so first of all OS means I think we
- 1:12:06all know this means operating system
- 1:12:08right so what OS module is going to give
- 1:12:10you information is going to give to give
- 1:12:12you the information about your license.
- 1:12:14Okay. Now, some of the common use cases
- 1:12:16now all those see I'll be very honest
- 1:12:18when you're going to create any rest
- 1:12:19APIs until unless you're going to have
- 1:12:20very specific functionality related to
- 1:12:23that particular project you're not going
- 1:12:25to use that much of this particular
- 1:12:27model. Okay. But some of the very
- 1:12:29important use cases will be let's say
- 1:12:31you wanted to check your operating
- 1:12:32system. So first of all let's say you
- 1:12:34wanted to check your CPU information. So
- 1:12:37in that case this is important. Or let's
- 1:12:38say you wanted to check your memory
- 1:12:40information. This is also uh important
- 1:12:42and also sometimes you also need to find
- 1:12:44your home directories or let's say you
- 1:12:47wanted to check some temp directories.
- 1:12:49So that time also this is important. So
- 1:12:51how we are going to use this? So first
- 1:12:52of all we are going to import something.
- 1:12:54So I'm going to import star as os from
- 1:12:57so we are going to use from node and
- 1:12:59this will be from os module. Okay. So
- 1:13:02first of all let's do one thing. I'm
- 1:13:03just going to create a very simple
- 1:13:04function and we are going to understand
- 1:13:06one by one. So I'll do run os demo. what
- 1:13:10this is going to return let's give void
- 1:13:12and then let's start with the first one
- 1:13:13and I'm going to take this one as let's
- 1:13:15say platform okay and here I'm simply
- 1:13:18going to do osplatform
- 1:13:20now let's do one thing let's uh also
- 1:13:23maybe run this so I'm just going to run
- 1:13:25this here and let's save this go to
- 1:13:28package dojson let's create one more so
- 1:13:31I'll do 03 so do tsx source/
- 1:13:35and this will be rename copy is
- 1:13:40ds save and now first of all let's start
- 1:13:43with the first one which will be what
- 1:13:45you're getting in the platform correct
- 1:13:47so I'll do npm run 03 enter and you can
- 1:13:50see that we are getting platform darwin
- 1:13:52the platform this will defer whatever
- 1:13:53platform you're using and based on that
- 1:13:56this will change uh what will be the
- 1:13:58next things let's check the architecture
- 1:14:00so I'm just going to do console.log log
- 1:14:02and let's do architecture and this will
- 1:14:04be your OS.arch arc and this will give
- 1:14:07you the architecture right return the
- 1:14:09operating system CPU architecture for
- 1:14:11which the NodeJS binary was compiled
- 1:14:13okay so let's save this and if you just
- 1:14:16clear and run one second and you'll see
- 1:14:18that you're going to get the
- 1:14:19architecture okay you can also get your
- 1:14:22OS type now also you might be thinking
- 1:14:25these things are not that much difficult
- 1:14:27but actually these are actually your
- 1:14:29node basics right these is going to make
- 1:14:31you better the all these concepts right
- 1:14:33is going to make you better node
- 1:14:35developer consider let's say one person
- 1:14:36is starting [snorts] learning NodeJS and
- 1:14:38directly jumping to the rest API but on
- 1:14:40the other side what you are doing you
- 1:14:42are learning each and every NodeJS core
- 1:14:43module you know how you can create APIs
- 1:14:46using only HTTP module how you can
- 1:14:48create routes and everything and then
- 1:14:49you jump to express a node it will give
- 1:14:51you way better advantage whenever you'll
- 1:14:53be going for interview especially for
- 1:14:54interview this will be a game changer
- 1:14:56all these modules whatever you are
- 1:14:58seeing and I can guarantee whenever
- 1:15:00you'll go for any very complicated
- 1:15:02interview right where they will test
- 1:15:03your concepts concept conceptual
- 1:15:05concepts especially they are going to
- 1:15:08ask you questions from all these module
- 1:15:09I can guarantee even if you go to lead
- 1:15:11late code discussion and everything
- 1:15:12you'll see that these kind of questions
- 1:15:14so many people usually get so this will
- 1:15:16be your OS type so let's run again and
- 1:15:19you'll see that you're going to get your
- 1:15:20OS type also here what else you can very
- 1:15:23important you can also get your OS
- 1:15:25release also so let's say here you can
- 1:15:27do write OSR release okay and if you
- 1:15:30save this let's see if I run once again
- 1:15:33you're going to get that also Okay, what
- 1:15:35else? As I already mentioned that you
- 1:15:37are also going to get your home
- 1:15:38directory. So let's I'm just going to do
- 1:15:40home directory.
- 1:15:43And what you can do you can do os doome
- 1:15:45directory. Okay. And the same way you
- 1:15:47can also get your temp directory. So
- 1:15:50let's see if I do console.log and let's
- 1:15:52do temp directory
- 1:15:55and this is going to give you this temp
- 1:15:57directory. Save this.
- 1:16:01And you can see that this is my home
- 1:16:03directory and this is my temp directory.
- 1:16:06Now how you're going to get the CPU
- 1:16:08information? Now CPU information you're
- 1:16:10going to get from so do con let's do
- 1:16:12CPUs. So we do OS dot CPU CPUs and here
- 1:16:17you can you will be able to get the
- 1:16:18count also. Let's say if I just do CPUs
- 1:16:21dot uh I think it's called length. So
- 1:16:23you'll be able to get the CPU count also
- 1:16:25here. Maybe let's do one thing or let's
- 1:16:28expand this one little here and then
- 1:16:30clear this and then if I run here you
- 1:16:32are going to get the CPU's count also.
- 1:16:35Okay. So now uh consider that you wanted
- 1:16:38to check your CPU model that is also
- 1:16:41possible. So let's see if I just wanted
- 1:16:42to give the okay my CPUs dot length if
- 1:16:46this is greater than zero. So what I can
- 1:16:49do I can do uh inside this I can do a
- 1:16:51log and I'll do a first CPU model or
- 1:16:54something like this. So do first CPU
- 1:16:56model and I'm simply going to do CPUs
- 1:17:00I think CPU of zero dot model. I think
- 1:17:03this is going to give you the model
- 1:17:04here. Okay. If you do dot you see the
- 1:17:06model speed times all these things
- 1:17:08you're going to get. So I'll do model.
- 1:17:09Let's do the CPUs of 0 dot speed and
- 1:17:14also I'll do CPUs dot of 0 dot times. So
- 1:17:19you will be able to get all of this
- 1:17:20information also. So clear and run this
- 1:17:23and you can see that first CPU and all
- 1:17:25this information the speed and
- 1:17:27everything you're going to get. Okay. So
- 1:17:29this is done. You can also get the
- 1:17:31memory information also from here
- 1:17:32directly. Okay. And let's say for now
- 1:17:35I'm just going to keep it simple. So I'm
- 1:17:37just going to log here and I'll do OS
- 1:17:39dot total memory. See directly you'll be
- 1:17:41able to get return the total memory of
- 1:17:42the system memory in bytes. Okay. What
- 1:17:44you can do, you can also convert these
- 1:17:46bytes to GB if you wanted to check and
- 1:17:48also you'll be able to get the free
- 1:17:50memory also here.
- 1:17:52Save this and let's clear and if I run
- 1:17:56all of these
- 1:17:59so this will be total uh the memory and
- 1:18:02the free memory all these. So everything
- 1:18:04you're going to get from this right
- 1:18:06mostly as I already mentioned OS CPU
- 1:18:08information you're going to get memory
- 1:18:10and home and temp directory all these
- 1:18:12things you you will be able to
- 1:18:14understand from this particular model I
- 1:18:16think that's all we don't have that much
- 1:18:18so what will be the next one next one
- 1:18:20will be path module we are going to
- 1:18:21understand that is also very important
- 1:18:23so let's move on to the next one okay so
- 1:18:25let's go to the next one which will be
- 1:18:27path module uh very very important okay
- 1:18:30this one actually you're going to use in
- 1:18:32real project okay whenever you're going
- 1:18:34to build. So first of all, uh what path
- 1:18:36module actually helps? So this is going
- 1:18:38to help you to build and read the file
- 1:18:41paths. Okay, it will help you to build
- 1:18:44and read file paths first of all. Okay,
- 1:18:49so first how we are going to do this?
- 1:18:50We're going to import path from you can
- 1:18:53see that you're getting the import also.
- 1:18:54So it will be from node and path module.
- 1:18:57So now uh how we are going to do this?
- 1:18:59Now before that we also need to
- 1:19:00understand some of the bad practices
- 1:19:02that you should not do. For example,
- 1:19:04let's say you wanted to get some file
- 1:19:07file path. Okay. Or maybe let's say you
- 1:19:10wanted to create some uh folder
- 1:19:13something like this. Just think of any
- 1:19:15scenario what you're going to do right.
- 1:19:17So let's say here I'm just going to take
- 1:19:19like I have a const and I have a file
- 1:19:21path something like this. And here what
- 1:19:23you're doing right? So let's say you
- 1:19:25have a project root. Okay. This is your
- 1:19:27project root. This is the root path of
- 1:19:29your project and then you're doing like
- 1:19:31this. Okay, I'm just doing uh add
- 1:19:33uploads plus uh add file name something
- 1:19:37like this. Now this is not a good
- 1:19:38practice. So here instead of doing this
- 1:19:40whenever you're going to have this kind
- 1:19:42of situation you are going to use path
- 1:19:44module instead of instead of doing like
- 1:19:46this. Okay. So how you're going to do
- 1:19:48this? So this path module is going to
- 1:19:50give you this very very important
- 1:19:52methods. There are so many but one of
- 1:19:54the most important is join. Okay. This
- 1:19:56path dot join it's going to do what? So
- 1:19:59it's going to first of all it's going to
- 1:20:00use correct separator for the correct
- 1:20:03operating system. This is one of the
- 1:20:05most core concept or core interview
- 1:20:06questions also you can understand that
- 1:20:08why actually you should not do like
- 1:20:09this. Instead why you should use
- 1:20:11something called path dot join. Now here
- 1:20:14you can give that okay this is so or
- 1:20:16this is going to use as the correct
- 1:20:19separator
- 1:20:23for the correct sorry current operating
- 1:20:26system or current OS now you might be
- 1:20:28thinking what actually you mean by this
- 1:20:30now sometimes see whenever we have path
- 1:20:32let's say we're using Mac it can be like
- 1:20:34this so let's say have a users/
- 1:20:37maybe my name let's I have a project
- 1:20:39folder and let's have a file something
- 1:20:41like this
- 1:20:44okay but the same in Windows it can
- 1:20:46happen like this so let's say you have a
- 1:20:47C directory something like this and then
- 1:20:49you have users then you have maybe let's
- 1:20:51say my name then you have project and
- 1:20:54then you you have filet txt you're
- 1:20:56getting so what this path dojo join is
- 1:20:59going to do it's going to use the
- 1:21:00correct separator for the current OS and
- 1:21:03very very important then this is the
- 1:21:05first thing then you have something
- 1:21:06called uh here we have something called
- 1:21:09process
- 1:21:11CW WD what this is going to do this is
- 1:21:14going to give you the current working
- 1:21:15directory. Current working directory
- 1:21:17means again this can also be interview
- 1:21:20question that what we mean by current
- 1:21:22working directory CWD. So current
- 1:21:25working directory means the folder from
- 1:21:27where your NodeJS process actually start
- 1:21:30uh started the path or the folder I
- 1:21:33think the folder will be better way to
- 1:21:34put. So the folder from where
- 1:21:38the NodeJS
- 1:21:40process uh was started. Okay. So this
- 1:21:44will be your process CWD. So now let's
- 1:21:47say what I need to do do right. So first
- 1:21:49of all I need to get my project root. So
- 1:21:52what I will do is I'll do con and first
- 1:21:54of all I wanted to get a project root.
- 1:21:56So I'll do project root. Give me my
- 1:21:57project root. So I'm just going to do
- 1:21:59process and then I'm just going to do
- 1:22:00CWD. So this is going to give me my
- 1:22:03project root. Okay. So now let's see if
- 1:22:05I just do log here and let's log this
- 1:22:08project root. Save this. Go here. And
- 1:22:11let's add one more.
- 1:22:14And I'm just going to do tsx source/
- 1:22:22paste it here. Save this.
- 1:22:26So I'll do npm run 04. And you can see
- 1:22:29that you are going to get your project
- 1:22:31directory. And see this path, right?
- 1:22:33this path or the separator can change.
- 1:22:35If you're using Windows, you can check
- 1:22:36this. So, because I'm using Mac, this is
- 1:22:38coming like this, right? So, YouTube
- 1:22:402026 projects NodeJS 26 one NodeJS
- 1:22:44basics NodeJS code modules. So, this is
- 1:22:46my current working directory. This is
- 1:22:49where I'm actually my NodeJS process
- 1:22:51actually started previously. So, this is
- 1:22:53going to give you my project loop. Okay.
- 1:22:56So, this is the first thing. So, now
- 1:22:58let's say what we are going to do right
- 1:23:00now. I'm just going to create some uh
- 1:23:03hypothetical sees and because we're not
- 1:23:04creating a proper project right here. So
- 1:23:07because we need to understand the
- 1:23:08concept so what I need to do right let's
- 1:23:10say you're creating some project where
- 1:23:12user actually uploads some file okay and
- 1:23:16that we wanted to store that particular
- 1:23:18file under this particular folder I'm
- 1:23:20just giving okay we have uploads/ users
- 1:23:23and let's say the this is my user ID
- 1:23:25which is 42 or whatever you can take and
- 1:23:27then I wanted to add this profile.png
- 1:23:30png. This is what I wanted to upload
- 1:23:33something like this or I wanted to store
- 1:23:35right now as I already mentioned that
- 1:23:38instead of manually you are going to do
- 1:23:39this what we going to use we already
- 1:23:41mentioned we're going to use proc path
- 1:23:43dot join okay because I'm repeating
- 1:23:45again this is going to use the correct
- 1:23:47separator based on the current OS or
- 1:23:50operating system that you you are using.
- 1:23:52So now what I'm going to do right I'm
- 1:23:54simply going to take a const and I'll do
- 1:23:56user ID and let's say I'll do 42 or 43
- 1:23:5923 whatever you wanted to take and I'll
- 1:24:01do a original file name this is the one
- 1:24:03that I wanted to store okay and this can
- 1:24:05be let's say I have a profile dot let's
- 1:24:07take the same only okay this one so copy
- 1:24:10this and then I wanted to take this one
- 1:24:11I wanted to store this right [snorts]
- 1:24:14how this will work so first of all I'm
- 1:24:16just going to do con and I'll do upload
- 1:24:18file path okay now I wanted to take path
- 1:24:22and I wanted to take join. So now how
- 1:24:25how it will work. So first of all I
- 1:24:27wanted to take my project root. Okay. So
- 1:24:29this will be my project root and then I
- 1:24:32wanted to take all the folders or path
- 1:24:35that I wanted to store in. So in this
- 1:24:37case I wanted to store in /uploads/
- 1:24:41users/ user ID which is the dynamic
- 1:24:43value and then the original file name.
- 1:24:45So in this case this will be uploads.
- 1:24:47This is the parent. This is the child.
- 1:24:50So that means user is child of uploads.
- 1:24:53So then user will come. So it will go
- 1:24:55sequentially. You're getting it's not
- 1:24:57that much difficult to understand. It's
- 1:24:59simple. And then 42 will come under user
- 1:25:02folder. That means 42 is dynamic value
- 1:25:05which is the parent of sorry which is
- 1:25:06the child of uh users. Okay. So now uh
- 1:25:10what you have to do let's take the user
- 1:25:12ID here and then I'm just going to take
- 1:25:14the original file name. That's it. Okay.
- 1:25:17What path dojo join is going to do? It's
- 1:25:19going to create a path string. It is not
- 1:25:22going to create folders or it will not
- 1:25:25check whether the file exist or not.
- 1:25:27This is very very important concept. So
- 1:25:29we are going to understand one very
- 1:25:30important concept that path dot join.
- 1:25:33This is going to create
- 1:25:36create a path string. It is actually not
- 1:25:39going to create the folder. It will not
- 1:25:41create it will not create the folder.
- 1:25:44And you'll understand which module we
- 1:25:46are going to use to create the folder
- 1:25:47later all of this. But this is very
- 1:25:49important part. So path dot join
- 1:25:51actually not going to create the folder.
- 1:25:52It only going to create a path. Okay.
- 1:25:55And that path will be a string. So path
- 1:25:57string it's going to create also it
- 1:25:59doesn't check if I'm not wrong. It's
- 1:26:01usually not check. It does does not
- 1:26:03check the uh no it does not check
- 1:26:06whether the file exist or not. Right? So
- 1:26:09file exist or not. It's not going to
- 1:26:11check. So now let's say if I just log
- 1:26:13this upload file path. So I'll upload
- 1:26:16file path save this and you'll see that
- 1:26:19it's going to create a path string.
- 1:26:21Okay.
- 1:26:23And you can see that now notice
- 1:26:25carefully. So user this and that and
- 1:26:27then we have um one nodejs and you can
- 1:26:30see this is a string. Okay. So we have
- 1:26:31one nodejs node js core modules. This is
- 1:26:34the folder. Inside this we have uploads.
- 1:26:38We don't have any folder. See it not
- 1:26:39created any folder. Then we have a
- 1:26:41users. Then we have 42. And then we have
- 1:26:43profile.photo.png.
- 1:26:45So then you might be asking when we can
- 1:26:47use this. So you can use this when you
- 1:26:48have to use when you have to actually
- 1:26:50create the path string. So that it is
- 1:26:52very very important. It is actually not
- 1:26:54going to create your uh folder that
- 1:26:56you're going to understand later.
- 1:26:57[snorts] Uh what else? I think this will
- 1:27:00be I think that's all. Okay. Let's
- 1:27:02understand some of the very important
- 1:27:04point that how you're going to get some
- 1:27:05of the properties from this. So let's
- 1:27:07say I wanted to get the file name. What
- 1:27:10you can do right? You can do something
- 1:27:12like path dot base name and then you
- 1:27:15pass this upload
- 1:27:17file path. What this is going to do?
- 1:27:19Path dot base name is going to give you
- 1:27:22it's going to return the final part of a
- 1:27:24path. Final part final part of a path.
- 1:27:31So now in this case whatever we have
- 1:27:33created right this one. Notice this one.
- 1:27:35So what this is going to return? So this
- 1:27:38will be your base name. So this is the
- 1:27:39final path last one. Okay. So this will
- 1:27:42be your base name. Base name here. Why
- 1:27:44this is helpful? Let's say you want to
- 1:27:45only wanted to extract your visible file
- 1:27:47name. I don't want the full thing. I
- 1:27:50only want the last the visible file
- 1:27:51name. So in that case what you can do
- 1:27:53that from your full path you will be
- 1:27:55able to get your base name or file name.
- 1:27:58So now let's see if I just do log file
- 1:28:00name here.
- 1:28:02Save this and you'll see that you're
- 1:28:04going to get that profile.png.
- 1:28:07Say you're getting profile.photo.png.
- 1:28:10Okay. Next, you have something called
- 1:28:11path extension. So, this is going to
- 1:28:13give you the extension name. So, let's
- 1:28:15say if I just going to do okay, I'll do
- 1:28:17con
- 1:28:20uh let's do file extension.
- 1:28:23So, do f path dot extension name. And
- 1:28:26you can again pass the upload file path.
- 1:28:29So now save this and let's see if I
- 1:28:33paste it here. So what you're going to
- 1:28:34get? You're going to get the extension
- 1:28:36which will be png. So this is my
- 1:28:38extension. Okay. Also you will be able
- 1:28:41to see I'm only discussing the important
- 1:28:44one and you you'll see that most of the
- 1:28:46times you are going to use all these
- 1:28:48properties only in a project. And here
- 1:28:49you also need to understand that you can
- 1:28:51also get your parent folder from this.
- 1:28:53So let's see if I just do path dot
- 1:28:55directory name and pass the upload this
- 1:28:58one. So this is going to give you what
- 1:29:00it's going to give you this one. So it's
- 1:29:02going to give you uh this uh / project
- 1:29:05sorry not project this part uploads user
- 1:29:08and 42 means it's going to give you the
- 1:29:10parent path here. Okay. So if I just log
- 1:29:12this
- 1:29:19uh notice here see users from here. So
- 1:29:22from here till this part. So till this
- 1:29:24part notice carefully. So let me just
- 1:29:26remove this or just complicated. Save
- 1:29:28this
- 1:29:31and you notice this one. So this is your
- 1:29:33full path right and this is your parent
- 1:29:36path means it makes sense right because
- 1:29:39see notice carefully the one that I
- 1:29:42explained that upload upload folder is
- 1:29:44child of this user folder is child of
- 1:29:48this user ID which is a dynamical value
- 1:29:51is a child of this so profile.photo
- 1:29:53photo.png will be child of this because
- 1:29:56this is actually inside this particular
- 1:29:58folder. So that is the reason you're
- 1:29:59getting this one as your parent folder.
- 1:30:02Okay. So all these things you'll be able
- 1:30:03to do. So the most important thing you
- 1:30:06know what you have to understand that
- 1:30:07what path dojo join usually does. Second
- 1:30:11very important that path dojo join is
- 1:30:13going to create a path string. It will
- 1:30:15not create the actual folder. See that's
- 1:30:17why you're not seeing the folder here.
- 1:30:18So that is also important. And also you
- 1:30:21are going to get file name, file
- 1:30:22extension, parent, folder and all these
- 1:30:24things from using this path dot and all
- 1:30:27the properties that whatever is
- 1:30:28available. For example, if you just do
- 1:30:30here path dot and you'll see that you're
- 1:30:33going to base name delimiter all this
- 1:30:35extension format is absolute join all
- 1:30:37this most of the times I also don't know
- 1:30:40each and everything right what will
- 1:30:42happen whenever you're going to create
- 1:30:43actual project right let's say you have
- 1:30:44a certain requirement you'll understand
- 1:30:46what I'm talking about and in that
- 1:30:48particular project you need to use this
- 1:30:50let's say matches glove this particular
- 1:30:52method so you do the web search right
- 1:30:55and then you'll came to know okay this
- 1:30:56is what you need to do and that is how
- 1:30:58you're going to learn thing. You can't
- 1:31:00memorize each and everything. It's
- 1:31:01absolutely not possible. And this is not
- 1:31:03how you should learn all these things.
- 1:31:05Learn what is absolutely needed because
- 1:31:06what will happen at most of the times
- 1:31:08you're going to use path dot join uh and
- 1:31:11this one uh path.base name and
- 1:31:14path.extension name. This is also very
- 1:31:15very important. So that's all about the
- 1:31:18path module. Very important. Now with
- 1:31:19this now let's move on to the next one.
- 1:31:21Okay. So let's move on to the next one.
- 1:31:23So we'll understand uh timers module.
- 1:31:25Okay. So again this is another very
- 1:31:28important and you are frequently uh
- 1:31:30going to use this one in various
- 1:31:31project. So what are the things we are
- 1:31:33going to understand? First of all you
- 1:31:35have to know that why actually need
- 1:31:37timers right. So first of all we usually
- 1:31:40use timers when we want to run code
- 1:31:42after some delay. So let's say we want
- 1:31:44to run something after some delay. Uh
- 1:31:48second would be let's say maybe you want
- 1:31:50to run some particular code repeatedly
- 1:31:55uh repeatedly after some interval after
- 1:31:58some interval maybe let's say you wanted
- 1:31:59to do something in after every 2 seconds
- 1:32:02something like this right
- 1:32:05uh so these two will be the most
- 1:32:07frequent ones and uh here what are the
- 1:32:10timer functions that we usually use so
- 1:32:12first of all we have something called
- 1:32:13set timeout we have something called set
- 1:32:16interval
- 1:32:18We have something called clear timeout.
- 1:32:22We have something called clear interval.
- 1:32:25And also there is one more set
- 1:32:28immediate. Okay. So mostly you are going
- 1:32:30to use all of these and believe me this
- 1:32:32is very very important concept like
- 1:32:34there are lot of places you are going to
- 1:32:36use set timeout, set interval or even
- 1:32:38set immediate also. Okay. So first let's
- 1:32:42do one thing. What are you going to do
- 1:32:43right? So let's start with a simple set
- 1:32:45timeout example. So what I'm going to do
- 1:32:47right, so first let's take a function
- 1:32:50and I'm just going to do run set
- 1:32:54time out example. Okay. And here let's
- 1:32:58return a void. So first of all I'm just
- 1:33:01going to do log and I'm simply going to
- 1:33:04give a sequence or else it will be
- 1:33:05tricky. So I'll do set timeout
- 1:33:09example started and then we are going to
- 1:33:13take set timeout. So you're going to
- 1:33:15take set time out like this and then
- 1:33:17there will be a call back. Okay. And
- 1:33:20then here we are going to understand uh
- 1:33:22sorry not understand we're going to give
- 1:33:24a delay. So that means we are simply
- 1:33:26telling that these particular set
- 1:33:28timeout will run after 1 second because
- 1:33:31we're giving 1,000 millisecond which is
- 1:33:331 second. So here we are simply going to
- 1:33:35do log and here I'm simply going to do
- 1:33:38that this runs after 1
- 1:33:44second. Okay. And then after this I'm
- 1:33:47simply going to do a log one more. And
- 1:33:49here I'm simply going to do three and
- 1:33:51I'll do this run immediately.
- 1:33:54So what will happen right? Node is not
- 1:33:57going to wait. Node doesn't wait.
- 1:34:03Okay, that's it. So now let's save this.
- 1:34:07So first of all, this is one very basic
- 1:34:09example. So simply what we are doing,
- 1:34:12we're simply telling that okay, we are
- 1:34:14running a particular function where we
- 1:34:15have a set time out. We have given that
- 1:34:17okay, after 1 second, this is going to
- 1:34:19run. Okay. So now what we will do,
- 1:34:21right, maybe let's take one um let's
- 1:34:24take one simple function first and then
- 1:34:25we'll do one by one. So I'll do async
- 1:34:28not async I think we can directly take a
- 1:34:30function and I'm simply going to do run
- 1:34:32timer demo and here this is going to
- 1:34:35return void and here I'm simply going to
- 1:34:37do run set timeout example okay and here
- 1:34:41I'm simply going to call this so I'll do
- 1:34:42run timer demo so for now save this and
- 1:34:46we'll go here let's go to package dojson
- 1:34:49so I'm going to 05
- 1:34:51uh which will be tsx source/
- 1:34:54and then we have this timers module.
- 1:34:59Okay. So let's clear this and then I'm
- 1:35:02simply going to do npm run 05 enter. And
- 1:35:05you can see that so set timeout example
- 1:35:08started. This is started. I already
- 1:35:11mentioned that this particular console
- 1:35:13here node is not going to wait for this
- 1:35:16that okay this will run and then we have
- 1:35:18this. So this one will run and then this
- 1:35:20one will run. No first it's going to run
- 1:35:22and then you can see that this runs
- 1:35:24immediately. node doesn't wait and then
- 1:35:26after 1 second node is going to run this
- 1:35:28particular set timeout. Okay. Now this
- 1:35:30is very important because set time set
- 1:35:32timeout is one of the most important
- 1:35:34feature that you are not only need in
- 1:35:36back end but also there are various use
- 1:35:38cases you're going to use this one in
- 1:35:40front side also. So this will be the
- 1:35:43first one. Now let's say uh how you can
- 1:35:45clear uh timeout. Okay. So how you can
- 1:35:48do that? So first of all you're going to
- 1:35:50take a function and you're going to do
- 1:35:52run clear time out
- 1:35:56example
- 1:35:58and here you're going to do void. So so
- 1:36:02first of all you're going to take a
- 1:36:03timer ID and here let's I'm just going
- 1:36:06to take a set time mode and here let's
- 1:36:09do let's take this one 2cond timer.
- 1:36:11Okay. And then I'm simply going to tell
- 1:36:13that okay this message uh let's do one
- 1:36:17thing. Let's log here first
- 1:36:21and then I'm simply going to do this
- 1:36:23message will not run. Okay. Now why? So
- 1:36:29first of all you have to do something
- 1:36:31like clear time out and then I'm simply
- 1:36:34going to pass this time timer ID. What
- 1:36:36actually we are doing right? We are
- 1:36:37actually canceling the timer. We are
- 1:36:40canceling this particular timer before
- 1:36:41even it will get a chance to run. And
- 1:36:44then I'm simply going to do a log here.
- 1:36:47And I'll do a log. And I'll do uh four.
- 1:36:50Let's give four because we have this on
- 1:36:52uh third. So I'll do four. And I'll do
- 1:36:54clear timeout.
- 1:36:57Cancelled
- 1:36:59the 2C timer means it will cancel this
- 1:37:02particular timer. This will never run.
- 1:37:04Okay, we are immediately canceling this.
- 1:37:06So that mean this timer is going to
- 1:37:08cancel before it will even get chance to
- 1:37:10run. Okay. So this is why whenever you
- 1:37:12need to clear some timer if you're using
- 1:37:15set timeout you can use clear time out
- 1:37:16and you have to pass that okay that
- 1:37:18timer okay here which is your set time
- 1:37:21out that you are keeping in a variable.
- 1:37:23Okay just try to explain this one in a
- 1:37:25simpler way and then let's run it here
- 1:37:28and then we can understand this one very
- 1:37:29very simple way. So save this
- 1:37:32and I'll just clear and then run once
- 1:37:36again.
- 1:37:37Okay. So you see that set timeout
- 1:37:39example started this run immediately
- 1:37:41node doesn't wait clear timeout
- 1:37:43cancelled 2 second timer means here you
- 1:37:48can see I already mentioned that clear
- 1:37:50timeout is going to cancel this
- 1:37:51particular timer here okay and this
- 1:37:53message will not run if you wanted to
- 1:37:56check this just remove this save this
- 1:37:59and clear once again and you'll see that
- 1:38:01what will happen
- 1:38:04okay this message will not run now we
- 1:38:06are getting this But or else you can do
- 1:38:08that this message will run after this
- 1:38:11message will run after 2 seconds. I'm
- 1:38:12just giving example. Okay. So 2 seconds
- 1:38:14something like this. Save this. And then
- 1:38:17here I'll do this one. Let's run once
- 1:38:19again.
- 1:38:22Okay. So that means here clear timeout
- 1:38:24is the respon is actually responsible so
- 1:38:27that this part will not run. So here
- 1:38:29I'll just go revert this. So this one
- 1:38:31will not run.
- 1:38:35Okay. So this will never run. So this
- 1:38:37will be how you can use clear timeout.
- 1:38:40Uh what else is left? Okay. So let's
- 1:38:42see. Uh set interval. Okay. Set interval
- 1:38:44and set timeout is actually different.
- 1:38:47Set interval is going to run the same
- 1:38:49call back again and again after a
- 1:38:51certain time after a fixed delay. So set
- 1:38:54interval is going to is going to
- 1:38:58run the call back
- 1:39:01again and again after the fixed delay.
- 1:39:07Okay, very important and this I think I
- 1:39:09don't have to explain here also these
- 1:39:10can be extremely important interview
- 1:39:12questions also right so what is the
- 1:39:14difference between set timeout and set
- 1:39:16interval mostly you're going to get this
- 1:39:17even they will not ask set immediate but
- 1:39:19definitely you are going to get if
- 1:39:21you're giving for beginner level of
- 1:39:22interview questions right so I'll do
- 1:39:24function and I'll do run set
- 1:39:28interval example
- 1:39:32and here we'll do void so first I'm just
- 1:39:34going to take a count okay see how we
- 1:39:36doing this. So I'll do do a count and
- 1:39:38I'm just going to take an interval ID.
- 1:39:40So I'll do interval
- 1:39:42id here. What you have to do? You have
- 1:39:44to take a set interval. Set interval
- 1:39:46you're going to take this is also going
- 1:39:48to take a call back. And here what I'm
- 1:39:50doing nothing. We are just simply doing
- 1:39:52count ++. Okay. Just notice what you're
- 1:39:55doing. You have to understand each and
- 1:39:56every line. And then I'm simply doing
- 1:39:58I'm simply doing a log here. And let's
- 1:40:01do this one as maybe uh last we have
- 1:40:03given four, right? So let's give five
- 1:40:05and I'll do set interval.
- 1:40:09Set interval tick and here you're going
- 1:40:13to give the count. Okay. And then what
- 1:40:16I'll do right if see what you're doing
- 1:40:19if the count is equal to three means the
- 1:40:21moment count will become three. So in
- 1:40:24that case what you're going to do you're
- 1:40:25going to clear this interval. So how
- 1:40:27you're going to clear the interval? Just
- 1:40:28like we have done clear time out to
- 1:40:30clear a interval you're going to do
- 1:40:31clear interval that okay now I I don't
- 1:40:33need this interval anymore but if you
- 1:40:35wanted to do this you have to pass the
- 1:40:37time out timeout is your interval ID so
- 1:40:39that means you have to pass this
- 1:40:40interval ID or else node doesn't know
- 1:40:42that which interval or which timer you
- 1:40:45wanted to close or you wanted to clear
- 1:40:47okay and then you're simply going to do
- 1:40:49one more log here and let's give six and
- 1:40:51I'll do set interval stopped so I'll do
- 1:40:54set interval stopped
- 1:40:57how we have to give this man. So I'll do
- 1:41:00six set interval stopped. That's it. Now
- 1:41:04I think okay this part okay here I'm
- 1:41:07just going to give maybe let's give this
- 1:41:08one a 500 uh millisecond or.5 second. So
- 1:41:12now we save this and then we going to
- 1:41:14run this set um time interval example
- 1:41:18and I'm just going to comment this one
- 1:41:20okay so that you'll not get confused.
- 1:41:22Save this. Now let's go here clear and
- 1:41:26let's run one second.
- 1:41:29That's it. So 1 2 3 stopped. So every
- 1:41:345 seconds if you do this one let's say
- 1:41:371.5. Okay. So see what will happen. So
- 1:41:39it will take some more time but the
- 1:41:41result will be same. Okay. So let's see.
- 1:41:43So 1
- 1:41:462 3 and it will stop. So that means
- 1:41:50every 1.5 seconds this particular
- 1:41:53interval will run. It will increase the
- 1:41:55count. The moment it will become three,
- 1:41:57it's going to clear the interval and
- 1:41:58you're going you are going to get this.
- 1:42:00As I already mentioned, I'm going to
- 1:42:01make things very simple and see that if
- 1:42:03you do this on multiple times, do the
- 1:42:05same example even multiple times, you'll
- 1:42:07see that you'll be able to do any kind
- 1:42:08of example. Whatever project you're
- 1:42:10going to have, you're not going to have
- 1:42:11any kind of difficulty that what
- 1:42:13actually you're going to do and then you
- 1:42:15are going to have one more. Right? So,
- 1:42:16I'm just going to comment it here. Save
- 1:42:18this. So, let's take one more which is
- 1:42:20called set immediate. Now the difference
- 1:42:22is set immediate is going to run a
- 1:42:25particular call back after the current
- 1:42:27synchronous code finishes. Okay. Now set
- 1:42:30immediate is not going to wait when you
- 1:42:33have something called set time out. So
- 1:42:35it's not going to wait for a fixed time
- 1:42:36like set time out. So it's going to it's
- 1:42:39it's something like this. So it will say
- 1:42:41that I'm going to run as soon as the
- 1:42:44current script is done. So current
- 1:42:45synchronous code is done. Okay. So if
- 1:42:47you go here just do this one that what
- 1:42:49is set immediate right so you have set
- 1:42:52immediate
- 1:42:54what's set time out see here it will say
- 1:42:57that set immediate is going to run after
- 1:42:58current current runs immediately after
- 1:43:01the current IO polling phase finishes
- 1:43:04it's giving the term difficult like for
- 1:43:07beginner it will be difficult to
- 1:43:08understand so it's simply telling that
- 1:43:10okay I'm going to run this particular
- 1:43:12call back after my current synchronous
- 1:43:14code finishes immediately but set
- 1:43:15timeout is going to do what it's going
- 1:43:17to wait for that time that if you're
- 1:43:18giving 5 second timer it's going to run
- 1:43:20after 5 seconds only. So that is the
- 1:43:22difference. Okay. So now let's see what
- 1:43:24is happening. So I'm just going to do
- 1:43:26function and I'll do set im uh immediate
- 1:43:29right. So I'll do set immediate example
- 1:43:32and let's give this one a void.
- 1:43:35So I'll do set immediate.
- 1:43:39And now uh I'm just going to do here log
- 1:43:42this.
- 1:43:45So last we have six. So I'll do seven.
- 1:43:49So I'll do here set immediate
- 1:43:54call back. Okay. And then I'm simply
- 1:43:57going to do log and I am going to do a
- 1:44:01let's say synchronous
- 1:44:04synchronous. I need to take the
- 1:44:06spelling. Okay. You don't have a
- 1:44:08synchronous here.
- 1:44:10I think S Y N C H R O N you know US I
- 1:44:15think this is correct. I don't know.
- 1:44:17Okay. So the synchronous code after set
- 1:44:19immediate.
- 1:44:22Okay. So now let's run this and we'll
- 1:44:26run all of this. Okay. We going to run
- 1:44:28this also. And let's run all these also.
- 1:44:31And I'm going to make this one 1,000.
- 1:44:34Let's save this. And let's run once
- 1:44:36again all of this and we'll see what is
- 1:44:38happening.
- 1:44:43All right. Set timeout example started.
- 1:44:45This runs immediately. Node doesn't
- 1:44:47wait. So that means makes sense. So this
- 1:44:50is going to run. This is going to run
- 1:44:52immediately. Okay. Next you're getting
- 1:44:55clear timeout cancel this one 2 second
- 1:44:57later. This part and then if you notice
- 1:45:01one thing very very important. See this
- 1:45:03part right? So this one inside set
- 1:45:05timeout you see the uh where it is
- 1:45:08running. It's running here but before
- 1:45:10that synchronous code after set
- 1:45:12immediate means this is actually
- 1:45:13finished means all your synchronous code
- 1:45:15already ran and then it will run
- 1:45:17immediately. See that is the reason
- 1:45:19after the synchronous code after set
- 1:45:20immediate you're getting the set
- 1:45:22immediate call back. So now this will
- 1:45:24already satisfies what I mentioned right
- 1:45:26that set immediate is going to run once
- 1:45:28all your current synchronous code
- 1:45:30finishes. So once this will run right so
- 1:45:32you're getting set immediate call back
- 1:45:33now the set timeout will run. So set
- 1:45:36time out means this one will run you're
- 1:45:37getting this will run after 1 second
- 1:45:39correct and then you're getting your set
- 1:45:41immediate sorry set interval. So set
- 1:45:43interval means you're getting count one,
- 1:45:45count two, count three which is this and
- 1:45:49then set uh interval stopped and it
- 1:45:51makes sense right. So this will be the
- 1:45:53whole flow. Okay. So this will be the
- 1:45:55difference. Now you can also do like
- 1:45:57this right? You can also uh implement a
- 1:46:00promise based timer. Okay. So let's see
- 1:46:02that also. I thought I'm not going to
- 1:46:04cover but let's do this one. So I'll do
- 1:46:05let's say I have a async function and
- 1:46:08I'm just going to do run promise
- 1:46:12promise timer
- 1:46:15example
- 1:46:16and here this is going to return a
- 1:46:19promise and this will be void. So how we
- 1:46:22going to do this? I'm just going to do a
- 1:46:23log and I'm just going to do a line and
- 1:46:26let's do this one as waiting for promise
- 1:46:31best timer.
- 1:46:33What you can do that let's say you don't
- 1:46:35want to use set time. What you can do?
- 1:46:37You can use something called slip. So
- 1:46:39from where you're going to import this.
- 1:46:41So let's go to the top and then I'm
- 1:46:43going to import something from this
- 1:46:45particular package. So node and then you
- 1:46:48have something called timers. And then
- 1:46:50you see you're getting promises. We'll
- 1:46:51do / promises. And here you're going to
- 1:46:53get a set time out.
- 1:46:55And then let's say I just wanted to do
- 1:46:57as slip something like this.
- 1:47:01Okay. And this one you're going to use
- 1:47:03here. So what I can do let's say
- 1:47:05directly I can use like this this is
- 1:47:07running and then I can do a because we
- 1:47:08have a sync and I can use this slip and
- 1:47:11I can give this 1,000 whatever time you
- 1:47:13wanted to give the delay okay and then
- 1:47:16you can simply just do console.log log
- 1:47:18and then we'll do dot promise
- 1:47:23promise based timer
- 1:47:27finishes
- 1:47:29after 1.5 seconds something like this
- 1:47:32okay or maybe let's wanted to give 5
- 1:47:34seconds just to check so let's give 5.5
- 1:47:37seconds that's it and then how you can
- 1:47:39run this you just can't directly run
- 1:47:40like this okay this is the thing you
- 1:47:42have to understand so this is a promise
- 1:47:44and this is going to give a catch and
- 1:47:47Here I'll do error. Let's do unknown
- 1:47:50because we don't know what is the error.
- 1:47:52Okay. What I'm doing wrong? Okay.
- 1:47:58And then uh let's do error here
- 1:48:02and I'll do timer
- 1:48:05demo field.
- 1:48:07And here I'm going to pass the error. So
- 1:48:10now save this and let's run all of this
- 1:48:12once again and you'll see clearly what
- 1:48:14is happening. Enter.
- 1:48:18three it will stop and see that it will
- 1:48:20wait 5 seconds and then it's going to
- 1:48:23run okay so promise base timer runs
- 1:48:26after 1.5 seconds I think I've given
- 1:48:28this one change okay change this one to
- 1:48:30not 1.5 5.5 okay so I think this is done
- 1:48:33also you can change this one let's say
- 1:48:35if I change this one to 500 or 1,500
- 1:48:40let's see what will happen so save this
- 1:48:44and if you clear this and run once Once
- 1:48:46again
- 1:48:48see it run here and then set time out
- 1:48:51set interval this will can differ like
- 1:48:54maybe let's say it will run and then
- 1:48:55this promise based uh this timer will
- 1:48:57run but at the concept will remain same.
- 1:48:59So I hope that this will give a clear
- 1:49:01picture that when set timeout will run
- 1:49:03when you're going to use set timeout
- 1:49:04when you're going to use set interval
- 1:49:06what is the difference between set
- 1:49:07timeout and set immediate and what is
- 1:49:10promise based timer. So with this now
- 1:49:12let's move on to the next one. Okay, so
- 1:49:14moving to the next one. One of the
- 1:49:16almost I will say this one is one of the
- 1:49:18most important concept that everyone
- 1:49:19should master and that is call back
- 1:49:22promises and as I'm pretty much sure
- 1:49:24some of you already know this one but
- 1:49:25still I will suggest don't skip this
- 1:49:27because there is a possibility that you
- 1:49:29are going to get lot of interview
- 1:49:30queries from this particular section.
- 1:49:32Okay and this is really important. Uh so
- 1:49:35we will create so let's do call back
- 1:49:39promises and async right so we have all
- 1:49:42these uh let's give asynav module ts
- 1:49:47okay so first of all let's do one thing
- 1:49:49let's take a type and we'll make this
- 1:49:51one as practical as possible and this
- 1:49:54user okay so this is a type of user and
- 1:49:57you're doing a API call and this user is
- 1:49:59going to have a ID number it's going to
- 1:50:01have a name let's say take this one as a
- 1:50:03string and let's say this also will have
- 1:50:05a role. This role can be either this
- 1:50:07will be a student or
- 1:50:10let's say normal user or there will be
- 1:50:11super admin.
- 1:50:13You can make this one as super admin
- 1:50:14also. Let's do this one as Z user.
- 1:50:17So now I'm simply going to do con users
- 1:50:20and type will be this user of array. So
- 1:50:23we're going to have arrow of users
- 1:50:24correct. So you're going to have ID
- 1:50:26which will be one. Sorry, this will be
- 1:50:28number. You're going to have a name.
- 1:50:29Let's take this one as my name. And then
- 1:50:31I'm simply going to take a role and this
- 1:50:34role will be
- 1:50:36so let's take this one as super admin.
- 1:50:38I'll take one more user or let's say two
- 1:50:40more user. Okay. So I'll do ID2. Let's
- 1:50:43do John and let's make this one as user.
- 1:50:46Okay. And then I'll just paste this one.
- 1:50:50So, id three. Uh let's do I don't know
- 1:50:53what uh I uh John or uh let's do maybe
- 1:51:00Roman
- 1:51:02and let's keep this one as user.
- 1:51:05So now what you have to do right the
- 1:51:07task is uh your interviewer or whatever
- 1:51:10you're doing your task is you have to
- 1:51:12actually fetch the list of user or let's
- 1:51:14say you have to fetch a single user. You
- 1:51:16have to fetch a single user using
- 1:51:19callback style using promises using
- 1:51:22asynchron and both will be different.
- 1:51:24Okay. Now this is why this is very
- 1:51:26important because callbacks are one of
- 1:51:28the most important features that you are
- 1:51:30going to use in almost all the places
- 1:51:32and whatever packages even you are using
- 1:51:34you'll see that there will be uses of
- 1:51:36callbacks. Okay. So first what we are
- 1:51:38going to do right uh in this case. So
- 1:51:41first let's do one thing. Let's start
- 1:51:43with the uh with the call back example.
- 1:51:46What actually is a call back? Now that
- 1:51:47is one of the most important thing. See
- 1:51:49call back is nothing but in a simple way
- 1:51:51call back is a function that you're
- 1:51:54going to pass into another function. Now
- 1:51:56this is the proper definition. So call
- 1:51:57back is itself it's a function. Okay.
- 1:52:00Callback is a function and this function
- 1:52:04this function you are passing to a
- 1:52:08different function
- 1:52:10as a parameter something like this right
- 1:52:13so now what will happen right so in this
- 1:52:15case in when you're implementing this
- 1:52:18one let's say in a nodej project the
- 1:52:20classic nodej pattern is like this so
- 1:52:22you have your call back okay this call
- 1:52:25back either is going to have a error and
- 1:52:27this is or else this is going to have a
- 1:52:29result this This is probably one of the
- 1:52:31most important concept that you should
- 1:52:33know. Okay, I'm really uh explaining all
- 1:52:35of these because whatever you're going
- 1:52:36to implement let's say you're
- 1:52:37implementing cloudinary file upload and
- 1:52:39you'll see that there is structure the
- 1:52:40package structure that whenever you're
- 1:52:42going to use this you'll see that there
- 1:52:43will be a call back let's say and there
- 1:52:45can be a error. So if there is a error
- 1:52:47you're going to reject if there is a
- 1:52:49success you're going to get the result.
- 1:52:50So this is a classic NodeJS call back
- 1:52:53pattern. Okay. So this is
- 1:52:57this is a classic NodeJS
- 1:53:01pattern. So this is how it will work. So
- 1:53:03look call back pattern
- 1:53:06how this will work. So let's say I'm
- 1:53:08just going to create a function and I'm
- 1:53:10not sure see I can guarantee so this is
- 1:53:13the challenge pause the video and try to
- 1:53:15implement what you have to do right
- 1:53:16let's say you are going to create a
- 1:53:19function that function is going to
- 1:53:20receive a call back and that function
- 1:53:22also is going to receive a user ID so
- 1:53:25let's see if I'm going to pass the user
- 1:53:26ID to so it's going to give me this user
- 1:53:29so let's see if you can do it without
- 1:53:31watching this part okay then you can
- 1:53:33resume and you'll see that you will some
- 1:53:35of you will find this difficulty Because
- 1:53:37implementing using pure call back let's
- 1:53:40say you're giving interview is very
- 1:53:41tricky using asyn you can do it but
- 1:53:45check whether you can do it using call
- 1:53:46back or not you're getting so now let's
- 1:53:48I'm just going to do find user and I
- 1:53:51will give you one uh practical
- 1:53:53experience okay this is not just about
- 1:53:55teaching actually giving proper
- 1:53:56experience uh that I faced from uh in
- 1:54:00past I've given more than 100 plus
- 1:54:02interviews and there is a 100% I
- 1:54:04remember that I actually got this call
- 1:54:07back and promise related example in more
- 1:54:09than 25 to 30 interviews I think even
- 1:54:13more okay so there is a huge possibility
- 1:54:15that you're going to get questions from
- 1:54:16promises implement custom promise you're
- 1:54:19not going to use the native promise
- 1:54:21features you're going to implement
- 1:54:23promise chain all of these you are going
- 1:54:26to fetch API call using callback you're
- 1:54:28not going to use any promises or async
- 1:54:30so these things are very important so to
- 1:54:32find user and actually I'm telling all
- 1:54:34of these for those if you're currently
- 1:54:36Let's say in college, third year, second
- 1:54:38year, fourth year or let's say you have
- 1:54:39a less than five years of experience.
- 1:54:41Okay, consider five years of experience
- 1:54:42and you're preparing for interview. This
- 1:54:44can be very important all these things.
- 1:54:47Uh so this will be a function. So now
- 1:54:50here what I mentioned that this is going
- 1:54:51to receive a user ID. So this will be my
- 1:54:54user ID which will be my uh what is that
- 1:54:56input and then I'm going to receive a
- 1:54:58call back. Okay, this is the my call
- 1:55:00back function. I already mentioned the
- 1:55:02call back is itself it's a function that
- 1:55:04we are passing in another function. So
- 1:55:06this is my another function I'm passing
- 1:55:07call back as a input and here this
- 1:55:10callback is going to have either there
- 1:55:12can be error which will be either error
- 1:55:15or it can be null okay and then it's
- 1:55:19going to give me a user okay so it's
- 1:55:20going to give me user information so
- 1:55:23here what I'm going to do right so this
- 1:55:25user will be my this user so I have this
- 1:55:27user type correct so this is what it's
- 1:55:30going to receive now you see this
- 1:55:32pattern this is matching either it's
- 1:55:34going to give me error or it's going to
- 1:55:35give give me result okay and then return
- 1:55:38type will be void that's it
- 1:55:42return type so this function return type
- 1:55:43will be this will be void and here I'm
- 1:55:45simply going to take a set time out now
- 1:55:48see whatever we have learned so far in
- 1:55:50the previous part we are applying all
- 1:55:51these things and here what we can do
- 1:55:53right let's take maybe what we can do
- 1:55:57let's take 0.5 second now what I'm going
- 1:56:00to do right you are actually doing API
- 1:56:03call think you are doing a actual API
- 1:56:05got something like this right normally
- 1:56:07you are not going to use callback okay
- 1:56:08you are mostly going to use async cover
- 1:56:10promise but again you should know all of
- 1:56:12this and this is where you're going to
- 1:56:13do okay I have my user so I'll do users
- 1:56:16dotfind
- 1:56:19so this user is nothing but these users
- 1:56:21you're going to have so do users do find
- 1:56:24so do current user
- 1:56:27and we do current user do ID will be
- 1:56:30equal to equal to this user ID that
- 1:56:31you're going to receive this one so Now
- 1:56:35this is the most important part. What
- 1:56:37will happen if you don't get this user?
- 1:56:40Consider that I'm passing user ID 100
- 1:56:42and that user ID 100 is not present. So
- 1:56:45what will happen? So this user is not
- 1:56:47found. Correct? So this is error. So if
- 1:56:50the user is not found, you need to call
- 1:56:52this call back. Now this is where
- 1:56:54student find it uh this one difficulty.
- 1:56:56So you have to call this callback and
- 1:56:58what you have to you have to pass error.
- 1:57:00So here you are going to simply pass
- 1:57:01okay you have to pass a new error and
- 1:57:04then I'm simply going to take user with
- 1:57:06ID this user ID is not present or not
- 1:57:10found you're getting that's it and then
- 1:57:13what you have to do you have to return
- 1:57:14from here immediately so you have to
- 1:57:16return from this that's it if this is
- 1:57:20present so that means you are getting
- 1:57:21result here so let's say you're passing
- 1:57:23user ID3 so you're going to get what ID
- 1:57:263 name Roman role user so in this case
- 1:57:29you're going to all the call back what
- 1:57:30will be error error will be null because
- 1:57:33you don't have any error and what will
- 1:57:36be result will be user so this will be
- 1:57:39your result now you see that how much
- 1:57:40easier it is to understand right so this
- 1:57:43is your uh function where you're passing
- 1:57:46call back as a input consider you
- 1:57:48passing in a different function and then
- 1:57:50you're simply checking we are doing
- 1:57:51something here you don't know what we
- 1:57:52are doing something we are doing if we
- 1:57:54are not getting result we are calling
- 1:57:55the call back passing the error if we
- 1:57:58are getting result
- 1:57:59passing the error as null and user
- 1:58:02information we are passing but how we
- 1:58:03are going to call this to call this you
- 1:58:05can simply just do right so to find user
- 1:58:07with call back we're going to pass a
- 1:58:10user ID let's say I'm going to pass
- 1:58:11three and here what you have to do you
- 1:58:13have to pass a call back function see
- 1:58:15it's call back is going to receive error
- 1:58:17or it's going to receive user correct so
- 1:58:19here what you have to pass you to pass
- 1:58:21error
- 1:58:23and you're going to pass user and now
- 1:58:26what you're going to get if error you
- 1:58:28are getting so let's say we're passing
- 1:58:29error here right so if we are getting
- 1:58:31error
- 1:58:34so I'm simply going to log and I'll do
- 1:58:37call back
- 1:58:39error and you're going to pass error dot
- 1:58:42message that's it right and what you
- 1:58:44have to do again you have to return from
- 1:58:46here if this is not the case so you're
- 1:58:49going to do what you're going to log so
- 1:58:50you're going to get call back result so
- 1:58:53what will be your call back result so
- 1:58:55this user this user is going to give
- 1:58:58give you what ID. This user is going to
- 1:59:00give you the name and this user is going
- 1:59:02to give you the role. That's it. So if
- 1:59:05you save this now, let's I'm going to
- 1:59:07run this here.
- 1:59:10Uh let's run copy this.
- 1:59:18Okay. So do tsx
- 1:59:22source/ this set this and let's I'm
- 1:59:25going to run npm ram 06.
- 1:59:28That's it. So you're getting call back
- 1:59:30result three Roman user. Why? Because we
- 1:59:33are passing three. Let's I'm going to
- 1:59:34pass 30. So 30 is not present. So it's
- 1:59:36going to give me what error. So here if
- 1:59:38I just do you see that call back uh with
- 1:59:41ID 30 was not found. Call back sorry
- 1:59:44user with ID 30 was not found. So user
- 1:59:47with ID 30 was not found. So this is
- 1:59:50calling here and returning the result.
- 1:59:52It's very much simpler to understand. So
- 1:59:54this is all about what call back. So now
- 1:59:57the same example we're going to do using
- 2:00:00promise and async in the next part. All
- 2:00:02right. So now let's do the same example
- 2:00:04using promise and asyncate. Okay.
- 2:00:06Because at the end you have to remember
- 2:00:08that when you're going to implement your
- 2:00:09project, you will be using mostly async
- 2:00:11because of the syntax. It's much easier
- 2:00:13to uh use. But again we need to
- 2:00:16understand this call back that we have
- 2:00:18call back and then we have something
- 2:00:20called call back hill. I'll not go into
- 2:00:22that much details and that is the reason
- 2:00:24we have something called promises and
- 2:00:25from promises we have something called
- 2:00:27async. Okay. So first let's create one
- 2:00:30more function and let's try to implement
- 2:00:33this also. So fetch user with promise.
- 2:00:37So what this is going to receive? It's
- 2:00:38going to receive the user ID. Again user
- 2:00:40ID will be type number. And what will be
- 2:00:43the return type? See remember the return
- 2:00:45type you have to be very very careful
- 2:00:46when you're going to use promises. So
- 2:00:48you have to return a promise. Okay, this
- 2:00:50is going to return a promise and the
- 2:00:51type of this promise will be user. So
- 2:00:54that means we are actually at the end
- 2:00:57returning a user information that user
- 2:01:00will have a ID, name and role. Okay. So
- 2:01:02this is the important line whatever we
- 2:01:04have done here because what will happen
- 2:01:07until unless you're going to return that
- 2:01:08you'll see that you are going to get
- 2:01:10error because you will not return. So
- 2:01:12let's say you return a string value,
- 2:01:14you're going to get error. Okay, because
- 2:01:15you need to return in this certain
- 2:01:17format only. So now what will be the
- 2:01:20next step? Uh these see what I I've seen
- 2:01:24from my own experience right or
- 2:01:26professional experience or giving
- 2:01:28interview that interviewers will not ask
- 2:01:30you more questions on as they can ask
- 2:01:32you but in the implementation they are
- 2:01:34going to ask you that okay you implement
- 2:01:36this one using promises. Why I'm going
- 2:01:38to tell you because promises you need to
- 2:01:40first see first of all to understand
- 2:01:42asyn better you need to know promises
- 2:01:44very well to understand promises very
- 2:01:47well you need to know call backs very
- 2:01:48well so from callbacks and promises only
- 2:01:51you are going to get questions in
- 2:01:52interview I can 100% guarantee and
- 2:01:55sometimes they will go little like
- 2:01:58advanc level for example let's say they
- 2:01:59can simply give you that okay create a
- 2:02:01promise polyfield or create a promise
- 2:02:03all polyfield but don't use any native
- 2:02:05promise features okay so understanding
- 2:02:08promises. So for example, let's say you
- 2:02:09are actually returning promise here. So
- 2:02:11how you going to return a promise? You
- 2:02:13need to know that okay, I'm going to
- 2:02:14return a promise. So this will be my new
- 2:02:16promise. Something like this. Promise is
- 2:02:18going to have a resolve and a reject
- 2:02:21property.
- 2:02:22And this now here what you can do that
- 2:02:25you can simply take a set time out.
- 2:02:26Let's I'm just going to take a set time
- 2:02:27out here because we're not doing actual
- 2:02:29API call here. And now here let's give
- 2:02:32this one as 1,000 milliseconds. And
- 2:02:35inside this we can do like that, right?
- 2:02:37So this line only we can copy. Now what
- 2:02:40is the important part? Important part is
- 2:02:42what will happen if this user is not
- 2:02:43found. Now in this case now let's see
- 2:02:46user is not found. What you have to do
- 2:02:48you you don't have any callable right?
- 2:02:50So you need to reject. So that is the
- 2:02:52reason you have something called reject.
- 2:02:53So promise will give you this reject uh
- 2:02:56here functionality and here you can
- 2:02:58actually reject and throw one error.
- 2:03:00Okay that this particular user ID is not
- 2:03:03present. user with this ID data was not
- 2:03:08found. Uh let's do this one. This will
- 2:03:11be user ID. Correct? So we have user ID.
- 2:03:14This one this is the case. Now again you
- 2:03:17have to return. But if this is not the
- 2:03:19case that means you are actually getting
- 2:03:20the result. So in that case you have to
- 2:03:22resolve it. Means now I already have the
- 2:03:24result. So I'm going to resolve the
- 2:03:25result and the type of result will be
- 2:03:27user. That's it. And this is done. Okay.
- 2:03:31So this will be how we are going to
- 2:03:33write using promises. Now how we going
- 2:03:35to call this one? Okay. Now see calling
- 2:03:38it's very simple. So what we can do
- 2:03:39right you can just comment out this one
- 2:03:41for now. And now let's say after this I
- 2:03:44simply do find uh user
- 2:03:47what name I have given fetch user I have
- 2:03:50given right. So let's do find user. Find
- 2:03:52user with promise. So I'll do find user.
- 2:03:57Find user with promise. We have to pass
- 2:03:59the user ID. So let's give I'll give the
- 2:04:00user ID one and then what this will
- 2:04:02give. So this is going to give me dot
- 2:04:04then dot then simply means once promise
- 2:04:08will run promise will either resolve the
- 2:04:11value or promise is going to reject the
- 2:04:12value. If promise resolve the value. So
- 2:04:14in that case we're going to get that
- 2:04:16value in our then block. If promise is
- 2:04:19going to reject the value we are going
- 2:04:20to catch that particular error. So in
- 2:04:22this case in the resolve uh block what
- 2:04:26we'll be getting in the resolve block we
- 2:04:27are going to get the user information
- 2:04:29because the user information we are
- 2:04:30sending. So in this case we're going to
- 2:04:32get the user here and then you can
- 2:04:34simply just do like this. So here I
- 2:04:37we'll go here. Let's say I'm just going
- 2:04:39to log this only. Copy this and paste it
- 2:04:43here. And let's make this one as promise
- 2:04:45result. Okay. And if there is any error
- 2:04:48so we'll do catch and this will be my
- 2:04:51error
- 2:04:52type will be error and here we are going
- 2:04:55to simply tell okay that means promise
- 2:04:57is actually catching some error and we
- 2:05:00don't know what is the error is so I'm
- 2:05:01just going to do promise error so this
- 2:05:03will give me error dot message that's it
- 2:05:06so now let's save this and then we going
- 2:05:08to run the same thing and we'll see what
- 2:05:10is happening here so let's clear first
- 2:05:12run
- 2:05:14okay so you're getting promise result
- 2:05:15one sum super admin that is correct what
- 2:05:18will happen if I give here 100 right now
- 2:05:20remember this 100 is not present so it's
- 2:05:22going to throw error correct so if you
- 2:05:24go here so you'll see that we are
- 2:05:26getting promise error user with ID sorry
- 2:05:29user with 100 data means this ID data
- 2:05:32was not found so this is how we going to
- 2:05:34write the same thing using promises
- 2:05:36correct now let's keep keep this one
- 2:05:38like this now what how we going to do
- 2:05:40using async async is actually simple so
- 2:05:43here first of all I'm going to take
- 2:05:44async now remember you have to know also
- 2:05:46So that async it actually built on top
- 2:05:48of promises only. So it's like a
- 2:05:50promises. The reason we use as because
- 2:05:52it's much more easier syntax wise and
- 2:05:54it's also easily you can read it. Okay.
- 2:05:57So let's say you have written some
- 2:05:58particular code and other developer is
- 2:06:00reading your code. They will be able to
- 2:06:02understand it's not like they they will
- 2:06:04not be able to understand promise code
- 2:06:05but it's like much more cleaner. Okay.
- 2:06:07That is the main reason. Uh so here so
- 2:06:11first of all you have to do async. So
- 2:06:13this will be function and I'll do find
- 2:06:16user with async a now. So I'll do async
- 2:06:19a. So again this is going to receive the
- 2:06:22user id which will be number what this
- 2:06:25is going to return is will be promise
- 2:06:28and here I'm for now I'm just going to
- 2:06:30do void and then what you have to do you
- 2:06:32always have to take a try and catch
- 2:06:34block right. So if something goes wrong
- 2:06:36your catch block is going to catch that
- 2:06:39particular error. So this is where
- 2:06:40you're going to catch the error and in
- 2:06:42the try you are going to try to get your
- 2:06:45result. So in this case let's have a
- 2:06:46user and I'll do a and then what you can
- 2:06:49do right you can call this method only
- 2:06:51find user with promise. So I can here I
- 2:06:53can simply do find user with promise and
- 2:06:55you can pass this user id at the end
- 2:06:58this is promise only admin right uh now
- 2:07:00here you can just log this I'll do log
- 2:07:04and simply let's do sync ait
- 2:07:10and here you can just log this one I'll
- 2:07:12just do user dot name I think this that
- 2:07:16is fine only that's it and here what you
- 2:07:18can do right you can simp message so you
- 2:07:21have to check if this error is a
- 2:07:23instance of error. So in that case we're
- 2:07:26going to render error dot message or
- 2:07:28else I'm just going to do unknown error.
- 2:07:30Okay.
- 2:07:36Okay. That's it. Also you can just log
- 2:07:39this here. So I'll do log. So I'll do a
- 2:07:42sync error
- 2:07:45and then you're going to do a message
- 2:07:48here. That's it. So save this. Now what
- 2:07:51you have to do you just have to call
- 2:07:53this method. So you go here comment it
- 2:07:55this one and then you can directly call
- 2:07:57this and then you have to pass the uh
- 2:07:59ID. So I pass one. So now this is going
- 2:08:02to go here and it's going to check okay
- 2:08:05this is async. It's going to wait for
- 2:08:08this promise to run. It's going to get
- 2:08:10the result. So it's going to try to get
- 2:08:12the result. If the result is present
- 2:08:14it's going to render the result. If
- 2:08:15there is a error so it's going to catch
- 2:08:17the error. And if you go here and let's
- 2:08:19try to run this.
- 2:08:22So you are going to get async sum. But
- 2:08:25if I go here and let's say I'll give 100
- 2:08:28and this is not present. So in that case
- 2:08:30this will give you a error that this
- 2:08:33data is not found. So this promise is
- 2:08:35actually throwing error here. Right? So
- 2:08:37this promise is going to reject this
- 2:08:38error and this block whatever it's going
- 2:08:42to catch the error here. So that's the
- 2:08:44reason I check in if this instance of
- 2:08:46error. So we're going to render error
- 2:08:47dot message. This is actually instance
- 2:08:49of error. So we are sending the error
- 2:08:51here and this is getting catched here
- 2:08:54and that is the reason you're getting
- 2:08:55the result here. Okay. So this will be a
- 2:08:59proper introduction or proper way to
- 2:09:01explain that what actually call backs
- 2:09:03are. Callbacks are actually most
- 2:09:04important that why we need call back
- 2:09:06functions and everything. Uh then you
- 2:09:08have promises then you have async but at
- 2:09:10the end as are nothing but it's promises
- 2:09:13only. But from syntax wise it's much
- 2:09:15much more easier and readable. So that
- 2:09:16is the reason we use async. So with this
- 2:09:18now let's move on to the next one. Okay.
- 2:09:20So moving to the next one. One of the
- 2:09:22most important core module that we are
- 2:09:24going to understand and that is file
- 2:09:25system module. Okay. And this is another
- 2:09:28very very important. So let's close
- 2:09:29everything.
- 2:09:32Let's open the package.json.
- 2:09:35Okay. So first of all we have to
- 2:09:37understand that what actually file
- 2:09:38system is right. So or why we need file
- 2:09:41system. So first of all fs means file
- 2:09:43system. Okay. Now file system is going
- 2:09:46to allow you to work with files and
- 2:09:48folders. So simply you think like that
- 2:09:50you have to create a folder or you have
- 2:09:52to write some files, you have to read
- 2:09:54some files, you're going to delete some
- 2:09:56files, you're going to check your file
- 2:09:58information. So all those things we can
- 2:10:00do using file system. This is one of the
- 2:10:03uh core modules that we need to know
- 2:10:05before we will start implementing our
- 2:10:07rest APIs and everything. Okay. So this
- 2:10:09is going to help you to create folders.
- 2:10:11It's going to help you to write files.
- 2:10:13It's going to help you to read files.
- 2:10:16It's going to help you to check file
- 2:10:18information.
- 2:10:20Also going to help you to delete files.
- 2:10:22So all these things you can do using
- 2:10:23file system. Okay. Now in node there are
- 2:10:26three ways we can do this or we can work
- 2:10:28with file system. The first one is using
- 2:10:30synchronous APIs. Okay. So we have first
- 2:10:33sync APIs. Okay. So we have something
- 2:10:36called uh so FS for file system and then
- 2:10:39we have read file sync something like
- 2:10:41this. Okay. So this is where you are
- 2:10:43going to read the file synchronous way.
- 2:10:45Then we have call back APIs. Okay. So
- 2:10:48there are callback APIs that you can
- 2:10:49use. For example, FS read file. So that
- 2:10:52is a callback API. Okay. And the third
- 2:10:54one is promise APIs. And now understand
- 2:10:56that why in the previous section we have
- 2:10:57discussed about the callbacks, promises,
- 2:10:59async all of these. Now why this is very
- 2:11:01important because see whenever you're
- 2:11:03going to create project now here in this
- 2:11:05particular file or whatever things we're
- 2:11:07going to discuss I can't give you each
- 2:11:08and every information on file system. So
- 2:11:10what I mean by this so let's you know
- 2:11:12this is sync APIs or this is callback
- 2:11:15API this is promise API based on the
- 2:11:18project that you are building you need
- 2:11:20to know or you need to understand okay
- 2:11:22which method you need to use in that
- 2:11:24certain position. So let's say you have
- 2:11:26a particular project you are building
- 2:11:28and there are two places you need to use
- 2:11:30file system in feature one you need to
- 2:11:32use promise API in the feature two you
- 2:11:34need to use sync API now no one is going
- 2:11:36to tell you okay that okay you have to
- 2:11:38do this or no tutorial video is going to
- 2:11:40teach you all of this okay they can give
- 2:11:42you the uh path okay this is where you
- 2:11:44have to use but by practice you need to
- 2:11:47understand all all of this by yourself
- 2:11:49and then only you can create your
- 2:11:50project or else what will happen I'm
- 2:11:51going to tell you you watch videos after
- 2:11:54videos you watch blogs blogs use AI
- 2:11:56tools. You're not going to create
- 2:11:58project by yourself. Especially if
- 2:11:59you're let's working in a project or
- 2:12:01let's say where project not project I
- 2:12:05will say even I'll say let's say you're
- 2:12:06working in a company and let's say there
- 2:12:08is some uh project which is already
- 2:12:10built and you are only working on the
- 2:12:13fixes so that the new fixes comes. So
- 2:12:15you need to understand all of this or
- 2:12:16say it will be very difficult for you to
- 2:12:18do all of this. Second, if you're
- 2:12:20working in some uh freelancing uh work,
- 2:12:23then this will be much more difficult
- 2:12:24because in freelancing the requirement
- 2:12:26can be very very tricky, right? Your
- 2:12:28client requirement. So why I mentioning
- 2:12:30all of this means the simple way to
- 2:12:33explain all of this that don't blindly
- 2:12:35uh learn [clears throat]
- 2:12:37whatever I'm teaching here. Okay, try to
- 2:12:39learn whatever I'm teaching plus try to
- 2:12:42apply some of the things you should
- 2:12:44learn by yourself and then create
- 2:12:46project. This is why or else I'm I'm
- 2:12:48noticing that some people are actually
- 2:12:50commenting you don't explain this much
- 2:12:52you first explain you don't expl do not
- 2:12:54explain in the file go to whiteboard
- 2:12:56explain there come back and then explain
- 2:12:59even if I do that I still what you are
- 2:13:01going to do you're going to memorize
- 2:13:02each and everything no one can memorize
- 2:13:04okay every method you need to know the
- 2:13:07process and then based on that you have
- 2:13:09to create projects as much as possible
- 2:13:12okay so we have sync APIs then we have
- 2:13:14call back APIs it [snorts] and then you
- 2:13:16have something called promise APIs. So
- 2:13:18all these three we have to understand
- 2:13:20and this is very important and what in
- 2:13:23modern [clears throat] NodeJS
- 2:13:24application what you're going to use
- 2:13:25definitely this is what you have learned
- 2:13:26so far we are going to use promise APIs
- 2:13:28correct because promise you see
- 2:13:29callbacks also you can use but you what
- 2:13:32will happen if your code is very
- 2:13:33complicated or the feature is very
- 2:13:34complicated that will end up in call
- 2:13:37back help so you're going to write call
- 2:13:38back inside call back inside callback
- 2:13:40like that so you're not going to do that
- 2:13:42so first of all let's do one thing maybe
- 2:13:44let's uh create first one so I'm just
- 2:13:47going to create the sync example. Okay.
- 2:13:50So I'm just going to do function and you
- 2:13:52do run sync
- 2:13:55example.
- 2:13:57Okay. Let's understand this one uh
- 2:13:59properly. Okay. Because uh this is one
- 2:14:02of the most important concept you don't
- 2:14:04understand.
- 2:14:05So first of all sync APIs are easier to
- 2:14:08understand. Okay. This is very easy. So
- 2:14:10it's like a synchronous APIs. Now here
- 2:14:13what we going to do right? See first of
- 2:14:15all you have to understand that what
- 2:14:17actually synchronous uh no not what
- 2:14:20where actually you should use this this
- 2:14:21is actually very important right now
- 2:14:23first of all you need to use this
- 2:14:24synchronous API when you have some let's
- 2:14:27say you're creating some small scripts
- 2:14:29you're creating some small startup
- 2:14:32startup scripts there you can use this
- 2:14:34or maybe you're going to build some
- 2:14:36scripts there also you can use this one
- 2:14:39uh maybe you're using some local demos
- 2:14:41something like this you can do all of
- 2:14:43this right where it is not good, not
- 2:14:46good or even bad or even bad practice.
- 2:14:49So where you should not should not use
- 2:14:51this. So let's say you are uh
- 2:14:53implementing some HTTP request and you
- 2:14:55are using this one in HTTP request
- 2:14:57handlers. This is not good, right?
- 2:14:59Because by the name you understand this
- 2:15:00is a synchronous API. Okay. Also let's
- 2:15:04say you're creating some uh very
- 2:15:06scalable backend application, your
- 2:15:07traffic is huge. So when there will be
- 2:15:10lot of traffic, never use this one. So
- 2:15:12high traffic APIs do not use this. Okay.
- 2:15:14[snorts] Or maybe I will suggest this is
- 2:15:16what I believe that if you're
- 2:15:17implementing some background jobs and
- 2:15:19everything right you should not use this
- 2:15:20sync API also. So in BG or background so
- 2:15:23do background jobs and everything you
- 2:15:25should not use this sync APIs. Okay. But
- 2:15:27first of all let's see that uh how we
- 2:15:29are going to uh do this one right or how
- 2:15:32you're going to implement this. So first
- 2:15:33of all let's do one thing here right? uh
- 2:15:37let's go maybe here or you can do no not
- 2:15:39here because this will help you to
- 2:15:41understand
- 2:15:43maybe on top
- 2:15:46so first let's do one thing I'm just
- 2:15:47going to take a folder okay that I'm
- 2:15:49going to create so I'm just going to do
- 2:15:50a demo folder path so I'm going to
- 2:15:53create a path so how we are going to get
- 2:15:55the path current directory see we have
- 2:15:58already discussed in this path module
- 2:16:00whatever we have learned in the previous
- 2:16:01section we are implementing the same
- 2:16:03concept again and again so we need to do
- 2:16:05path so you're going import node uh path
- 2:16:08you're going to do a join and then how
- 2:16:11we going to get the current directory so
- 2:16:13we have something called process cwd you
- 2:16:16remember and then I'm just going to
- 2:16:17create one uh path here so I'm just
- 2:16:19going to give file system
- 2:16:22and then let's give this one as file
- 2:16:24system demo so I'm just going to do fs
- 2:16:26demo so this will be my folder where I
- 2:16:28wanted to create this okay and now after
- 2:16:31this I'm just going to do con sync file
- 2:16:34path so I need to create a file actual
- 2:16:37file. Remember in the path module I
- 2:16:39mentioned that this will actually not
- 2:16:41create the folder.
- 2:16:42[clears throat and snorts]
- 2:16:43It will not create the folder. It
- 2:16:44doesn't check whether the file exist or
- 2:16:46not. But this file system is where we
- 2:16:48are going to actually check all of this.
- 2:16:49Okay, this is where we are going to
- 2:16:51actually create the file or folder. So
- 2:16:53here I'm just going to do path dot join.
- 2:16:56I'm going to take the demo folder path
- 2:16:58and let's do this one as sync note txt.
- 2:17:03So this is where I wanted to create this
- 2:17:05particular file. Okay. So this will be
- 2:17:07your project directory file system fs
- 2:17:10demo and then we're going to have the
- 2:17:11sync node txt. Okay. So now uh maybe
- 2:17:15also let's take a another type here over
- 2:17:17here.
- 2:17:19So I'm just going to do type. So I do
- 2:17:21file result. So I just let's take a
- 2:17:24style. Uh this will be string. Uh file
- 2:17:27name also I'm just going to take which
- 2:17:29will be string. Let's take a content
- 2:17:32which will be string. Let's take a size
- 2:17:34in bytes and this will be number. So
- 2:17:38this will be the uh result that I wanted
- 2:17:41to return. So this will be my return
- 2:17:43type. So here I'm just going to return
- 2:17:45this file result. Okay.
- 2:17:48So now uh how we going to first of all
- 2:17:52write something in a file. So we need
- 2:17:55some method right. So this where we are
- 2:17:57going to use this sync example. So we
- 2:17:59have something called fs. So fs from
- 2:18:01node fs. So first of all let's go and
- 2:18:02import the fs module because we haven't
- 2:18:05imported the fs module only. So I'm just
- 2:18:07going to do import file system from. So
- 2:18:10I'll do node file system. Okay. So now
- 2:18:13this is where you're going to get a
- 2:18:15method. So now I'm just going to do
- 2:18:17what? I'm going to write something. I'll
- 2:18:19do write content to a file. How I can do
- 2:18:23that? So I'll do fs dot write and see
- 2:18:26that we have write file sync. So write
- 2:18:29file sync is going to write something in
- 2:18:30a file. Okay. Now here one very very
- 2:18:33important point if this particular file
- 2:18:35doesn't exist node is going to create
- 2:18:38it. Okay. And if the file is already
- 2:18:41present so whatever node is going to
- 2:18:43replace the content. So consider here
- 2:18:45I'm just going to take this and here
- 2:18:48what it will take it will take the file
- 2:18:50okay uh fs.path or the file descriptor
- 2:18:54okay and the actual data. So now I'm
- 2:18:56just going to give my sync file path.
- 2:18:58Sync file path mean this is where I
- 2:19:00wanted to uh save this. I'll do sync
- 2:19:03file path and then I'm just going to do
- 2:19:06a container. I'll do created using sync
- 2:19:10file system and then here what I'll do
- 2:19:13right I'll give a type. So I'll do u uh
- 2:19:15UTF8. So this is nothing but this is
- 2:19:18simply we are telling note that we are
- 2:19:20writing a simple text. Okay. And now
- 2:19:23what will happen right? So this is the
- 2:19:24first thing. Next we have one more
- 2:19:27method that also you need to understand.
- 2:19:28So first you understand that fs do.
- 2:19:30Write file sync is going to write
- 2:19:32content to a file. If the file doesn't
- 2:19:34exist you can note it down. Then node is
- 2:19:36going to create it if the file already
- 2:19:38exist. So node is going to replace the
- 2:19:40content. Okay. And what this is going to
- 2:19:43do this is going to tell node that okay
- 2:19:44we are writing normal text. Then we have
- 2:19:46something called fs.append file sync.
- 2:19:49What this is going to do? This is going
- 2:19:51to add content at the end of the file at
- 2:19:54the end of this file. Okay. And it's not
- 2:19:56going to replace your existing content.
- 2:19:58So here what you can do, right? You can
- 2:20:00use something called
- 2:20:02because by the name you understand,
- 2:20:03right? So you append file sync and
- 2:20:06you'll do sync file path and I'm just
- 2:20:08going to do here give a space. I'll do
- 2:20:10appended maybe appended using sync fs
- 2:20:14like this. And here also I'm just going
- 2:20:16to do UTF8.
- 2:20:18That's it. So this is two things we
- 2:20:21going to do.
- 2:20:24Oh, we have to give comma man. Okay. So
- 2:20:28now this will to write and this is to
- 2:20:30append. Now how we are going to read
- 2:20:32file means read file content. Okay. And
- 2:20:36for this one we have something called
- 2:20:38this is also simple. We have to use fs
- 2:20:40dot read file sync mean we're going to
- 2:20:42read something. So here we are going to
- 2:20:45do. So let's say I wanted to read the
- 2:20:46content of this particular file. con
- 2:20:50content and we do fs dot read file sync
- 2:20:55and here you're going to give the path
- 2:20:57and give the type so we'll do UTF8
- 2:21:00that's it now remember what will be the
- 2:21:03type so we have the style file name
- 2:21:05content and size in bytes so how you're
- 2:21:07going to get the size in bytes so here
- 2:21:09you have something called uh fs dot stat
- 2:21:12uh stats sync if I'm not wrong so here
- 2:21:15you'll be you'll be getting your stats
- 2:21:16so you do if you do stats here. So fs
- 2:21:19do.stats sync. So fs.stat sync is going
- 2:21:23to give you the information about the
- 2:21:25file. Okay. Now this is also going to
- 2:21:27give you the about the file or also
- 2:21:29about a folder also. And here in this
- 2:21:32particular variable that will be a
- 2:21:34property that is called stats dot size
- 2:21:37and that is going to give you the file
- 2:21:38size in bytes. So here what you have to
- 2:21:41do you have to simply pass the sync file
- 2:21:43path. That's it. So now we are going to
- 2:21:45return the result. Okay. So first of all
- 2:21:46what we going to do? So let's start with
- 2:21:48first we have a style. So this will be
- 2:21:50sync. Uh then we are going to return the
- 2:21:53content. Then we also need to return the
- 2:21:55file. Now here how we going to return
- 2:21:56the file. Now see this is where you have
- 2:21:58to see that how uh the previous concept
- 2:22:01that we have learned so far. We are
- 2:22:03applying here in this path module we
- 2:22:05have already covered. This path dobbase
- 2:22:07name is going to give you the file name.
- 2:22:09Similarly here also we're going to do
- 2:22:10path dot base name and here we're going
- 2:22:12to pass the sync file path. And then
- 2:22:15you're going to pass the size. How you
- 2:22:16going to get the size? So this stats is
- 2:22:18going to give you see here you're going
- 2:22:20to get a lot of options. So first we
- 2:22:22need stats dot size. That's it. So we'll
- 2:22:24save this. Now let's go here and create
- 2:22:26a main function. So as sync function
- 2:22:30main and what will be the because we are
- 2:22:32going to work with promises also. So
- 2:22:34let's keep promise only here. And then
- 2:22:36I'm simply going to take a try catch. Uh
- 2:22:40here this will be error. And I'm simply
- 2:22:42going to take the same thing that we
- 2:22:44have learned so far. So if this error is
- 2:22:46instance of error. So we're going to
- 2:22:48render error dot message
- 2:22:51or else I'm just going to do unknown
- 2:22:53error.
- 2:22:56And now let's uh log one error.
- 2:23:00So I'll do file system error and render
- 2:23:02the message.
- 2:23:04Okay. And then we're going to call this
- 2:23:06main. Okay. So this is done. See first
- 2:23:08of all we have to understand another
- 2:23:10thing that when you have this demo
- 2:23:11folder path so we have to check that if
- 2:23:14this path is actually present or not
- 2:23:16you're getting what I'm talking about.
- 2:23:17So how we going to check and if this is
- 2:23:19not present so we're going to create
- 2:23:20this particular folder. So that also
- 2:23:22we're going to understand that how you
- 2:23:24can create folder. So first let's do
- 2:23:26like this do insure file no sorry insure
- 2:23:30demo folder present or exists. So this
- 2:23:34is what we're going to check. So now
- 2:23:35copy this and let's go here and I'm
- 2:23:38simply going to do function and NC demo
- 2:23:40folder exist. I'm going to return void
- 2:23:43and now we are going to check that if
- 2:23:45this is exist or not. So how we going to
- 2:23:47check exist or not? So we have one
- 2:23:49method that is called a do.exist sync
- 2:23:52and [snorts] this is going to tell that
- 2:23:54okay if this is present. So if this is
- 2:23:55false so that means this folder is not
- 2:23:58present. So here we're going to pass the
- 2:23:59demo folder path. So if this demo folder
- 2:24:01path is not present, so we need to
- 2:24:03create a uh demo folder. Correct? How we
- 2:24:06going to create? So we have something
- 2:24:07called FS. MKDIR sync. Now this MKDI
- 2:24:12sync is going to create a particular
- 2:24:14folder synchronously. Okay, this not
- 2:24:16will be a sync. This will be
- 2:24:17synchronously it's going to create.
- 2:24:19Okay, so it's simply going to fs.m MKDIR
- 2:24:22sync. Okay, because we are working with
- 2:24:24sync. And here you're going to pass the
- 2:24:26demo folder path. And you can pass one
- 2:24:28more option here and that is called
- 2:24:29recursive will be true. Now this
- 2:24:31recursive is nothing but simply telling
- 2:24:33that you are going to create the full
- 2:24:35folder path if the parts of it doesn't
- 2:24:38exist. So it's going to create the
- 2:24:40complete folder path here. Okay. Now
- 2:24:43okay now you might be thinking okay why
- 2:24:45actually you're using this recursive
- 2:24:46equal to true. So let me explain in a
- 2:24:48simpler way. So what will happen right?
- 2:24:50If you don't give this sometime this can
- 2:24:51fail if your parent folder doesn't
- 2:24:54exist. So it's simply telling that even
- 2:24:55if the parent folder is not exist still
- 2:24:58you are going to create this. So first
- 2:24:59we're going to use this. So this is
- 2:25:01going to check if this present or not.
- 2:25:03And now what I can do right I will go
- 2:25:05here and I'm simply going to do con sync
- 2:25:08sync result and this will be my run sync
- 2:25:12example and then I'm simply going to do
- 2:25:14log and for now I'll just do like this.
- 2:25:17I'll do sync result. So save this. Now
- 2:25:20we'll go here and create one more. So
- 2:25:24I'll do 07
- 2:25:26and this will be my tsx source slash and
- 2:25:29then I'm just going to take rename copy
- 2:25:31this
- 2:25:33paste it here and then let's run this
- 2:25:36and see we'll see what will happen. So
- 2:25:37first I'm just going to npm run 07.
- 2:25:39Enter. And you can see that now we
- 2:25:41already have a file system folder
- 2:25:43created. Fantastic. And you see that we
- 2:25:45have a style sync created using this
- 2:25:47file name is this size in bytes is 45
- 2:25:49bytes. If you go here you have fs demo.
- 2:25:52This is what you have created. So now
- 2:25:54[snorts] you have this fs demo this
- 2:25:56folder right file system fs demo and
- 2:25:59then you have sync note text. And if you
- 2:26:01go here you have this created using sync
- 2:26:04uh fs and appended using sync fs. Okay.
- 2:26:07So this is how you're going to do what
- 2:26:09you're going to create folder and file
- 2:26:11synchronously. So this is one only one
- 2:26:13of the section. Now in the next section
- 2:26:14we are going to understand using call
- 2:26:16back and promises. See promises one is
- 2:26:18more important. So let's see how we
- 2:26:19going to do that in the next part. All
- 2:26:21right. So now let's understand how we
- 2:26:23can use call back based APIs. Right? So
- 2:26:25here first of all we need to know I
- 2:26:26think this pattern you remember that we
- 2:26:28have call back and in the call back we
- 2:26:30usually have error or the result. Right?
- 2:26:33So now let's create the function and we
- 2:26:35understand practically. So I'm just
- 2:26:36going to do run call back example
- 2:26:40here. This will be returning a promise
- 2:26:43and here I'm just going to return a file
- 2:26:46result also. Let's go to the top and
- 2:26:49create two more here. So one will be
- 2:26:51callback
- 2:26:53file path and also one will be the
- 2:26:55promise file path. So for this one let's
- 2:26:59keep this one as callback note and this
- 2:27:02will be my promise note. Okay.
- 2:27:06So first of all what we're going to do
- 2:27:07right so for this one we are going to
- 2:27:09wrap this call back example in a promise
- 2:27:12so that we can actually run this from
- 2:27:14this our runner okay from this main
- 2:27:16function but inside we are actually
- 2:27:18going to use our callback style APIs
- 2:27:20only. So first what we going to do right
- 2:27:22so first let's I'm just going to return
- 2:27:24a new promise and here I'll do a resolve
- 2:27:28reject.
- 2:27:30So now let's understand what actually we
- 2:27:32are doing here. So first of all uh just
- 2:27:34like we have uh understood that we have
- 2:27:36something called fs.right file sync
- 2:27:39correct for synchronous APIs. So what we
- 2:27:41need to use? So here we are going to use
- 2:27:43something called a do.t write file only.
- 2:27:46Okay. So here now we are going to pass
- 2:27:48our path. So our path will be our
- 2:27:51callback file path. And here uh sorry
- 2:27:55and uh call back uh file path comma
- 2:28:00let's do created
- 2:28:03using call back fs and we are going to
- 2:28:08pass our type which will be UTF8.
- 2:28:11Okay. So now here what we are going to
- 2:28:13do right you getting error. Okay. So
- 2:28:16first of all let's take the call back
- 2:28:18here. So we going to take the uh call
- 2:28:20back. So first I'm just going to do here
- 2:28:22write error.
- 2:28:27Okay. And then inside this first we need
- 2:28:30to understand that if we have error so
- 2:28:31we are going to reject the error. Right.
- 2:28:33So we are going to simply do if we have
- 2:28:35write error we're going to do reject. We
- 2:28:38are going to pass the error and then we
- 2:28:40are going to return. Okay. So this is
- 2:28:43the first thing that we are going to do.
- 2:28:45So this is about how we are going to
- 2:28:46write. Okay. So now what I have to do
- 2:28:50right uh also let's do one thing I think
- 2:28:52this will be 1 minute I think we are
- 2:28:55closing this one here
- 2:28:57after this okay here we are going to do
- 2:28:59append
- 2:29:01same only instead of append file sync
- 2:29:04you're going to use append file and then
- 2:29:06again you're going to I don't like this
- 2:29:08popup very limiting okay so here we are
- 2:29:11going to use this and then I'm simply
- 2:29:13going to do append
- 2:29:15using call back fs give space and then
- 2:29:20you're going to give the type which will
- 2:29:21be UTF
- 2:29:248
- 2:29:26that's it and then again also you're
- 2:29:28going to take another call back right so
- 2:29:30here you are going to have the call back
- 2:29:31so here we are going to do if there is
- 2:29:33any append error
- 2:29:36and then inside this we're going to
- 2:29:37again check if there is append error so
- 2:29:40we're going to reject the error and
- 2:29:41you'll see that this is actually what we
- 2:29:43call about call back hell because you
- 2:29:46are actually writing call back after
- 2:29:47call back Okay. And this will be return.
- 2:29:50So this about your append inside this
- 2:29:52again we are going to now read the file
- 2:29:54information. Right. So just like we have
- 2:29:56done uh write append read. So similarly
- 2:30:00here also now we're going to do fs dot
- 2:30:02read file not sync read file only. Here
- 2:30:05also you're going to pass callback file
- 2:30:07path and then we're going to pass the
- 2:30:10type which will be UTF8. And then what
- 2:30:12this is going to give? So here we are
- 2:30:13going to give uh so first of all we
- 2:30:16going to get the error. So let's take
- 2:30:18read error or else we're going to get
- 2:30:20the result which will be content the
- 2:30:23actual file content and then inside this
- 2:30:26we're going to again check if we have
- 2:30:27read error we're going to reject
- 2:30:31read error and then we're going to
- 2:30:33return okay and now what we can do right
- 2:30:36so inside this again we have to read the
- 2:30:39stat so simply if I just go here you can
- 2:30:41see that we are using a method inside
- 2:30:45call back first we are checking we are
- 2:30:46writing another call back inside that we
- 2:30:48are writing another call back. Now here
- 2:30:50we need to write another call back. Why
- 2:30:51we need to read the stat. So we going to
- 2:30:53use not stat sync but instead we going
- 2:30:56to use stat and here we're going to pass
- 2:30:58the call back file path and then this is
- 2:31:01going to give you either error. So I'm
- 2:31:02just going to do stat error
- 2:31:05and or else this will be stats array
- 2:31:10this popup man. So and then we are going
- 2:31:14to simply check if this stat error is
- 2:31:15present. We are going to reject
- 2:31:18the stat error or else and then we are
- 2:31:20going to return and at the end now we
- 2:31:23have to return the result but you can't
- 2:31:25return the result directly. You have to
- 2:31:27resolve it. So you are going to resolve
- 2:31:29it. So you're going to pass a style. So
- 2:31:31this will be your call back. You're
- 2:31:33going to pass the content. This content
- 2:31:36will be your uh this content will be we
- 2:31:38have created the content this content.
- 2:31:40Okay. This content that you have. Then
- 2:31:42you're going to pass the size in bytes.
- 2:31:44So size and bytes will be your stats and
- 2:31:46this stats is going to give you size and
- 2:31:50also you're going to have the file name.
- 2:31:52File name will be your path dot base
- 2:31:55name and you're going to pass a call
- 2:31:57file path. That's it. So this is done.
- 2:31:59Now you'll see if you go here and you
- 2:32:01notice that we are writing fs dot this
- 2:32:03is one our method right and inside call
- 2:32:07back. So inside this call back we have
- 2:32:09everything inside this we have append
- 2:32:11file inside append file call back we are
- 2:32:14having the read file inside read file we
- 2:32:16are having the stat and at the end we
- 2:32:18are resolving the result so this is a
- 2:32:20classic example of call back help so
- 2:32:22you're using call back after call back
- 2:32:24after call back like this and then what
- 2:32:26you can do right you can just do con and
- 2:32:28I'll do call back result so this will be
- 2:32:32a so I'll do call back what is the name
- 2:32:34we going to run call back example and
- 2:32:37then we're going to pass it here.
- 2:32:40Save this and let's run it.
- 2:32:44Enter. And you can see that. Fantastic.
- 2:32:46So now inside FS demo we have another.
- 2:32:49So now this time you're getting call
- 2:32:51back created using callback size by is
- 2:32:5451. And this is the file name because
- 2:32:56this is the file name we have created.
- 2:32:58Correct. So scroll down and let's go
- 2:33:00here. You can see that created using
- 2:33:01callback file system. So this will be a
- 2:33:04classic example of how you can implement
- 2:33:06using callback. Okay. So with this now
- 2:33:08let's move to the next one which will be
- 2:33:10how you can work with promises. This is
- 2:33:11exactly what you usually will be doing
- 2:33:14in your project. So let's do this one in
- 2:33:16the next part. Okay. So now let's create
- 2:33:18using promises, right? Promises one. Uh
- 2:33:20so first of all how we going to use
- 2:33:21promises? So we have we need to import
- 2:33:23something right. So we going to import
- 2:33:25fs promises
- 2:33:27from. So we have this module right? So
- 2:33:29we have mode file system / promises. So
- 2:33:32this is what we going to use. Okay. And
- 2:33:34this is much more easier and you'll
- 2:33:36understand what actually we are doing
- 2:33:37here. So let's do the promise API right.
- 2:33:40So here I'm simply going to uh take
- 2:33:42async. So I'll do function and this will
- 2:33:45be run promise
- 2:33:47example.
- 2:33:50What this is going to return again
- 2:33:51promise and the type will be file
- 2:33:54result. So first what we going to do? So
- 2:33:57first we need to write right. So to
- 2:34:00write we are going to use fs promises
- 2:34:02and this is going to give you this write
- 2:34:03file method. Okay. So this is where we
- 2:34:06are going to use our promise file path
- 2:34:10comma. So I'm just going to do this one
- 2:34:11as created using promise APIs. See again
- 2:34:16at the end it is the same thing right
- 2:34:18whatever we are doing. So this is going
- 2:34:19to write same way we are going to have.
- 2:34:23So we'll copy this and then we'll paste
- 2:34:25it here. You're going to have append
- 2:34:26file. So this will be appended using
- 2:34:29promise APIs. Okay.
- 2:34:33And then you're going to have the
- 2:34:35content. So to get the content remember
- 2:34:38content is the file content, right? So
- 2:34:40we going to do a so. So you have fs
- 2:34:41promises dot read file
- 2:34:46pass the promise file path
- 2:34:49and we are going to tell okay we are
- 2:34:50using text and similarly we're going to
- 2:34:52use stats. I'm not explaining because
- 2:34:54it's repeating. So you're going to do fs
- 2:34:56promises dot stats
- 2:34:59not stat uh this will be stat not stats
- 2:35:02and path and then we are going to
- 2:35:04return. So this will be style. So this
- 2:35:06will be promises right? So we're using
- 2:35:08promises. Uh then we have the content.
- 2:35:12Then file name. So this will be your
- 2:35:14path dot base name. This will be the
- 2:35:17promise file path. And then you're going
- 2:35:19to do size in bytes which will be stats
- 2:35:22dot size. That's it. And now we are
- 2:35:26going to simply use this in our uh here.
- 2:35:28So I'll do con.
- 2:35:31Let's do promise result.
- 2:35:34And this will be run promise example.
- 2:35:37And we're going to just log it here.
- 2:35:40The only difference is you are importing
- 2:35:42this one from file system promises
- 2:35:44module. And then if I just run it here.
- 2:35:46So we're going to get promises and one
- 2:35:48mode will be created, right? So if you
- 2:35:51go here sorry if you go here created
- 2:35:53using promise APIs appended using
- 2:35:55promise APIs. So I think this is going
- 2:35:58to give a proper understanding of how
- 2:35:59you can work with file system. How you
- 2:36:01can create file folder how you can check
- 2:36:03if that is exist or not. How you can
- 2:36:05create path and very very important how
- 2:36:07you're going to make use of both path
- 2:36:10module and file system together to
- 2:36:12implement this. Okay this is very
- 2:36:14important because consider you don't
- 2:36:15know that what is path.base name. So you
- 2:36:18will not be able to use right. So that
- 2:36:19is the reason we've discussed the path
- 2:36:21module previously and then we discuss
- 2:36:23the file system uh module. Okay. So with
- 2:36:27this now let's move on to the next
- 2:36:29module which will be another very
- 2:36:30important module and this will be
- 2:36:32buffers. Okay. So we will create one
- 2:36:34more file here. So I'm just going to do
- 2:36:36buffers.ts.
- 2:36:38Okay. Now this is another important. So
- 2:36:41first of all you need to understand that
- 2:36:42what actually buffers are and why we
- 2:36:44need buffers. Right. So first you
- 2:36:46whenever you need to work with raw
- 2:36:49binary data raw binary data and that
- 2:36:54that is where you are going to use
- 2:36:55buffers right now what actually binary
- 2:36:58data means now I'm not sure if some of
- 2:37:00you know means see binary data means
- 2:37:02when you have your data stored in bytes
- 2:37:06so your binary data means
- 2:37:09when you have
- 2:37:11your data stored in bytes as simple as
- 2:37:15that Right? Now you need to understand
- 2:37:17that when uh in JavaScript, right?
- 2:37:19Whenever you use something called
- 2:37:21strings, now these strings are good for
- 2:37:24normal text. But when you work with
- 2:37:26bytes or especially raw bytes, this is
- 2:37:29where you can't use strings that much.
- 2:37:31So you need to use something called
- 2:37:32buffers. Now one of the most important
- 2:37:34interview questions can be that where
- 2:37:37where all the places okay that you can
- 2:37:39use buffers. So first of all, one will
- 2:37:41be reading files. very important uh
- 2:37:45receiving very like there can be believe
- 2:37:48me or not there can be lot of
- 2:37:50complicated and very advanced level of
- 2:37:52places where you can or you need to use
- 2:37:54buffers okay so don't take this
- 2:37:56particular module lightly this is a very
- 2:37:58important module so when you're going to
- 2:38:01receive HTTP request bodies then also
- 2:38:03you can use buffers okay working with
- 2:38:06streams very very important you'll see
- 2:38:08that whenever you're going to work with
- 2:38:09streams right you are going to use or
- 2:38:10you need buffers uh
- 2:38:13handling images, PDFs, files uh and
- 2:38:18videos there also you need buffers also
- 2:38:21sometime you need to use encrypt and
- 2:38:23maybe let's say hashing then also you
- 2:38:25need buffers so these all will be very
- 2:38:28very frequently you are going to use
- 2:38:29buffers so first see if I I'm going to
- 2:38:32repeat one thing that what actually
- 2:38:34buffers and when you need to use buffer
- 2:38:37so first we need to mention that okay
- 2:38:38buffer we are going to use whenever we
- 2:38:40need to work with raw binary data data.
- 2:38:43Okay, raw binary data and raw why
- 2:38:45because JavaScript so they can ask okay
- 2:38:47why not JavaScript uh strings because
- 2:38:50strings are actually good only for
- 2:38:52normal text when you need to use raw
- 2:38:55binary data the binary data that is
- 2:38:58stored in bytes this is where you need
- 2:39:00buffers okay so just like this you have
- 2:39:03string so string means your human
- 2:39:06readable text then you have buffer means
- 2:39:11your raw byes
- 2:39:13like this. Okay. Now, first of all, you
- 2:39:15need to know how you can create normal
- 2:39:17or simple buffer. So, we have something
- 2:39:19called buffer dot from method. So, what
- 2:39:22you can do right, you can simply do
- 2:39:23let's I'm just going to create a text
- 2:39:25buffer. So, I'll do con text buffer. So,
- 2:39:28I I'll directly use buffer
- 2:39:31dot from and see that if I just go here,
- 2:39:34this has crazy amount of method. Okay,
- 2:39:36there is no way I can cover each and
- 2:39:38everything. But I'll try to cover only
- 2:39:40the best one. For example, how you can
- 2:39:42create buffer. So we give a buffer dot
- 2:39:44from and let's say I wanted to pass a
- 2:39:47text here. So this would be my text node
- 2:39:49something like this, right? So this is
- 2:39:51going to do what? This is going to
- 2:39:53create a buffer from a normal text
- 2:39:56because this is a text correct. So what
- 2:39:59will happen? This buff uh sorry this
- 2:40:02normal text will become bytes internally
- 2:40:06you're getting. So this is where we need
- 2:40:08or we are creating buffer from a normal
- 2:40:10text. Now consider if I'm just going to
- 2:40:12log this text buffer what will happen?
- 2:40:15So if you log this text buffer you are
- 2:40:18going to see
- 2:40:20can you guess? So you're going to see
- 2:40:22bytes and this will be hexa decimal
- 2:40:25format. Okay. So this is also very
- 2:40:26important. So let's test it. So I'm just
- 2:40:28going to save it and let's go to
- 2:40:30package.json JSON.
- 2:40:33So 08
- 2:40:35and I'll do tsx source/. So we have 08
- 2:40:39buffers.
- 2:40:41TS save.
- 2:40:45And then if I do clear and let's run
- 2:40:47again and you can see that you're
- 2:40:49getting buffer and this is you're
- 2:40:51getting actually this is not normal text
- 2:40:53right you can clearly see it. So this is
- 2:40:55simply bytes in hexadimal format. Okay.
- 2:40:59What will happen? Each uh value is one
- 2:41:01by. So here each value is one by. So n
- 2:41:06is actually uh sorry n is actually this
- 2:41:094 e right you are seeing here. Then you
- 2:41:12have o equal to this 6f then you have
- 2:41:16what is node n o d right? So d is
- 2:41:20becoming 64 like this right? So this is
- 2:41:23how you can create buffer. So now let's
- 2:41:25say what I need to do right I wanted to
- 2:41:28convert this particular raw buffer into
- 2:41:30my readable text only. So you have
- 2:41:32something called two string method and
- 2:41:34this two string method is going to
- 2:41:35convert that bite into the normal text.
- 2:41:37So for example
- 2:41:40uh if I go here and let's say I'll do
- 2:41:42log and I'm just going to do text buffer
- 2:41:46dot to string another very important and
- 2:41:48here you have to give the the type I
- 2:41:50think we already explained UTF- 8. So
- 2:41:53node you'll understand we are actually
- 2:41:54working with normal text or we are
- 2:41:56converting something to a normal text
- 2:41:58and if I just enter here you can see
- 2:42:00that you're getting node back right
- 2:42:01makes sense. So that means technically
- 2:42:04this text buffer whatever you have
- 2:42:06created from this and this particular
- 2:42:08line both are same. So this is in
- 2:42:10readable view and not readable uh
- 2:42:12readable means readable view only means
- 2:42:14we can human readable view and this is
- 2:42:17in buffer in bytes. Okay. So these are
- 2:42:20one of the most important two concept.
- 2:42:22Now let's understand some more uh
- 2:42:24important methods that mostly we're
- 2:42:26going to use. So something called buffer
- 2:42:28length. So now let's I'm just going to
- 2:42:30do here. So let's take a uh English
- 2:42:32buffer. I'm just going to give any
- 2:42:34random name whatever. So do buffer dot
- 2:42:37from and maybe I'll do hello. Okay. And
- 2:42:41now what I can do right I can simply
- 2:42:43just do here English buffer.length.
- 2:42:46And this is going to give you the length
- 2:42:47here. So save this. And if I just run
- 2:42:50this here. So we're going to get five.
- 2:42:53Okay. Also uh here what actually five
- 2:42:57means? So hello we have what? Five
- 2:42:59characters. Correct. 1 2 3 4 5. Five
- 2:43:02characters equal to five bytes. Okay. So
- 2:43:05this will be your buff uh buffer.length.
- 2:43:08So now you can also use another very
- 2:43:10important method that is called
- 2:43:12buffer.alloc.
- 2:43:13This buffer.alloc is going to do what?
- 2:43:15It's going to create a buffer with a
- 2:43:17fixed number of bytes. Very important.
- 2:43:19So what I'm talking about? also have
- 2:43:21something called allocation. So in short
- 2:43:24it's called alloc. So it's simply
- 2:43:26allocation like this. So it's like con
- 2:43:28I'm just going to do a fixed buffer. So
- 2:43:30do fixed buffer.
- 2:43:32So do buffer dot alloc alloc and then uh
- 2:43:37you're going to pass let's say five. So
- 2:43:39this is going to create a fixed number
- 2:43:40of buffer. So here if I just log this
- 2:43:44and let's say I'm just going to do empty
- 2:43:45fixed empty fixed. This is empty buffer
- 2:43:48but fixed buffer and this will be fixed
- 2:43:51buffer here and if you just log it right
- 2:43:53see what you're getting.
- 2:43:57So you can see that it's going to create
- 2:43:58empty buffer but it's going to create
- 2:44:00fixed buffer means we have given five.
- 2:44:02So it's going to create five.
- 2:44:05Now what you can do right we have
- 2:44:06another important see I already
- 2:44:07mentioned whatever methods that I have
- 2:44:09actually decided to cover. Remember
- 2:44:11there is a reason I'm not doing all of
- 2:44:13this blindly because you'll see whenever
- 2:44:15you're going to work in a project mostly
- 2:44:17you're going to use all of these only.
- 2:44:18So buffer from buffer uh uh that buffer
- 2:44:22dot to string buffer dot uh length
- 2:44:25buffer dotalloc all of this and then we
- 2:44:27have something called write. What write
- 2:44:30is going to do? So what you can do right
- 2:44:31you can write text into a existing
- 2:44:33buffer also. Okay. Now consider what we
- 2:44:36can do right. This fixed buffer is
- 2:44:39currently having what five bytes. And
- 2:44:42now what we are going to do right I'm
- 2:44:43going to add let's say three more bytes.
- 2:44:45So what I can do right I can take this
- 2:44:47fixed buffer and I can do something like
- 2:44:49write and this right is going to I'm
- 2:44:51just going to add API or whatever you're
- 2:44:53going to give. So what will happen this
- 2:44:55API actually need three bytes. Okay. So
- 2:44:57it's going to fit you're getting what
- 2:44:59I'm talking about and the remaining
- 2:45:00bytes because it has already has five.
- 2:45:02the remaining bytes will remain zero.
- 2:45:04It's very important. So what we are
- 2:45:06simply doing, I'm going to repeat once
- 2:45:07again. So we created fixed buffer of
- 2:45:09five bytes, right? All are empty, right?
- 2:45:12And now we are writing this API into
- 2:45:14this buffer. Fixed buffer. So this is
- 2:45:17going to take three bytes and this has
- 2:45:18five. So two will remain empty. So now
- 2:45:21if I just log here. So let's do fixed
- 2:45:24buffer.
- 2:45:27Fixed buffer up right.
- 2:45:31And I'm simply going to do fixed buffer.
- 2:45:36And I'll do log. And let's do fixed
- 2:45:39buffer.
- 2:45:41I'll do fixed buffer as text. So how we
- 2:45:46going to do text? So do fixed buffer dot
- 2:45:48two string.
- 2:45:50And then we're going to use UTF. Right?
- 2:45:53Save this and we'll see what is
- 2:45:55happening. Right?
- 2:46:00So you see we have see first one is
- 2:46:03empty fixed buffer after right is three
- 2:46:05because API is going to take three bytes
- 2:46:06and rest two will remain empty and then
- 2:46:09this is our fixed buffer as text is API
- 2:46:11okay fantastic so that means this is how
- 2:46:13you're going to do this also you can do
- 2:46:16something like this you can use let's
- 2:46:18discuss buffer concat method also
- 2:46:20because what will happen right so
- 2:46:22whenever you're going to work let's say
- 2:46:23you're working in a real project and
- 2:46:25you're getting data in chunks because
- 2:46:27that is possible that when your data is
- 2:46:29huge you are not going to get the data
- 2:46:31at once. So what will happen? You're
- 2:46:33going to get in chunks. So this will
- 2:46:35called as chunk. Chunk one, chunk two
- 2:46:37like this. What you can do right you can
- 2:46:39actually
- 2:46:40uh combine all of these into one buffer.
- 2:46:42So let's say I'm just going to get
- 2:46:44chunks. So I'm going to getting three
- 2:46:46chunks right. So how how I'm getting? So
- 2:46:48I have a buffer dot from. So this is my
- 2:46:51first chunk and this is hello. Then I'm
- 2:46:54going to give comma and I'm getting my
- 2:46:56second chunk. So I do buffer. And I'll
- 2:46:58do hello node.
- 2:47:00And then I'm just going to do buffer dot
- 2:47:03from and I'm just going to do hello
- 2:47:05nodejs something like so I have three
- 2:47:07chunks. What I can do right I can
- 2:47:09combine all of these into one. So I do
- 2:47:11combine buffer
- 2:47:14and this will be chunks not chunk what
- 2:47:17I'm doing. So buffer doconet
- 2:47:20and you're going to use this chunks. So
- 2:47:22what this is going to do this is going
- 2:47:24to combine all of this into one and then
- 2:47:27you're going to simply log this. If I
- 2:47:29just do log here, combine buffer and
- 2:47:32also let's do combine buffer.2 string
- 2:47:35and I'm just going to do UTF8. So you'll
- 2:47:37understand clearly what is happening
- 2:47:39here. So let's log this [screaming]
- 2:47:42and you can see that what you're
- 2:47:44getting. So you're getting hello NodeJS
- 2:47:46and this is the combined buffer. So
- 2:47:47you're getting hello NodeJS. Makes
- 2:47:49sense, right? So that means this
- 2:47:51actually combined and created one
- 2:47:53buffer. Okay. So I think this is done.
- 2:47:56uh also I think I think this should be
- 2:47:58enough right so what we have covered so
- 2:48:00far so covered that buffer is going to
- 2:48:01store raw bytes buffer from is going to
- 2:48:04create buffer from data buffer tworing
- 2:48:07is going to convert your text from your
- 2:48:09buffer bufferlength is going to give you
- 2:48:12the length buffer allocation is going to
- 2:48:14do what it's going to create empty
- 2:48:15buffer buffer write is going to write
- 2:48:19your data into a buffer and buffer
- 2:48:21concat is going to combine multiple
- 2:48:23buffer chunks believe me if you can
- 2:48:25master all of this right most of the
- 2:48:27time you'll be able to work with buffers
- 2:48:29okay so with this now let's move on to
- 2:48:31the next one all right so moving to the
- 2:48:33next one so let's create and normally
- 2:48:36other videos I'm not sure I think they
- 2:48:38usually not cover this but but I'm going
- 2:48:40to cover this one okay this is another
- 2:48:41important one and this will be URL
- 2:48:43module okay so let's see in the next
- 2:48:46part we going to have the HTTP module
- 2:48:48right and there it will be actually
- 2:48:49helpful and also when we are going to
- 2:48:51actually implement our APIs and
- 2:48:52everything so what we're going to do
- 2:48:54right So this URL module is going to
- 2:48:57help you to work with URLs uh safely
- 2:49:00something like that. Okay. So because
- 2:49:02see what will happen whenever you're
- 2:49:04going to do any back end development
- 2:49:05maybe you need some protocol information
- 2:49:07or you need which host name you're using
- 2:49:09or maybe you need the path or maybe you
- 2:49:11need the query parameters mostly query
- 2:49:13parameters and all you are going to use
- 2:49:15a lot. So consider maybe I'm just going
- 2:49:18to take this example right. So I'm just
- 2:49:19going to do HTTP
- 2:49:21HTTPS. Let's take API.ample example.com
- 2:49:25whatever you want to take.example.com
- 2:49:27example.com then I have / users and then
- 2:49:29here I'm going to have let's I'm going
- 2:49:30to use a paginess and page two and then
- 2:49:33I'm using limit will be 20 or 10
- 2:49:35whatever you wanted to take right so in
- 2:49:36this case what will be your path name
- 2:49:38your path name is this one so/ user page
- 2:49:41is actually two and limit is 10 which is
- 2:49:43what this is your query patterns that
- 2:49:44you are actually doing right so let's uh
- 2:49:47implement this one in a simpler way so
- 2:49:48what I'm going to do right I'm going to
- 2:49:50take a function and then I'm just going
- 2:49:52to do run url demo uh let's return void
- 2:49:56only we going to return and things will
- 2:49:57do void and then I'm simply going to do
- 2:49:59con API URL. So how we are going to do
- 2:50:02here? We are going to create a API sorry
- 2:50:04we are going to create a not API man we
- 2:50:06are going to create a URL object. How
- 2:50:08you going to create a URL object? So the
- 2:50:10question is how you can create how to
- 2:50:12create
- 2:50:14URL object from URL string. Now this is
- 2:50:18very important. Okay you need to know
- 2:50:20all of this. You see that in most
- 2:50:21project we usually use this. we have to
- 2:50:24do new URL and here you have to pass the
- 2:50:27uh string. So for example let's I'm just
- 2:50:29going to take https colon and I'm do
- 2:50:32let's say apias devob I'm just going to
- 2:50:35take a debub as example and here I'm
- 2:50:37just going to do user information I'm
- 2:50:39getting and here I'll do page equal to
- 2:50:41two and then let's say let's take a
- 2:50:44limit the limit will be 10 and also I'm
- 2:50:46just going to take a sort sort will be
- 2:50:48latest something like this
- 2:50:52okay so now what I'm doing don't need
- 2:50:55this okay so now what we going to do
- 2:50:57Right. Oh, I think it was correct only.
- 2:51:00So here I thought I've taken this one in
- 2:51:02middle. So here I'm just going to log
- 2:51:03couple of things. Okay. So I'll do log
- 2:51:06and let's do full URL. So how you are
- 2:51:08going to get the full URL. So you have
- 2:51:10to do something like this. So do API
- 2:51:11URL. Here. So this is going to give you
- 2:51:14the full URL. Okay. And [snorts] before
- 2:51:16that also let's do one thing. We are
- 2:51:18going to run this. Okay. So I'm just
- 2:51:20going to run this here. So I'll do run
- 2:51:22URL demo. Save this. Uh let's copy this.
- 2:51:26go to package dojson and here I'll do 09
- 2:51:31and I'll do tsx source/ this
- 2:51:36and let's run one by one. So I'll do 09
- 2:51:40and I'll see that you're going to get
- 2:51:41the full URL. Okay, so this is going to
- 2:51:43give you the full URL. Now what I have
- 2:51:45to get right so a couple of things I
- 2:51:47need to get. So I need to get the
- 2:51:48protocol. So you can do API URL
- 2:51:50dotprotocol.
- 2:51:52You're going to get API URL dot host
- 2:51:55name. This is going to give you the host
- 2:51:56name. You're going to get the path name
- 2:51:58API URL.path name and very very
- 2:52:02important you are also going to get your
- 2:52:04search uh string or query string. How?
- 2:52:07So you'll do AP URL dot search. And if
- 2:52:10you save all of these and let's run all
- 2:52:12of these and see what you're getting.
- 2:52:18Okay. So you're getting this. This is
- 2:52:20the https your protocol. Then you're
- 2:52:22going to have your host name
- 2:52:23api.hdep.com. debugab.com this is your
- 2:52:25path name and this is your search or
- 2:52:27query string right and this is your
- 2:52:29complete one complete means your href so
- 2:52:32this all the information you're going to
- 2:52:34get now what actually search param
- 2:52:36usually do right so there is a method
- 2:52:38method called search panam one of the
- 2:52:40most important concept that you need to
- 2:52:42know whenever you'll be working with
- 2:52:43mostly you are going to work with filter
- 2:52:45search sort all these things okay so
- 2:52:48what is search pam so search param is
- 2:52:51going to give you the capacity to read
- 2:52:53query parameters. Okay, so query
- 2:52:55parameters means where it will start
- 2:52:58with normally with this. So you have
- 2:52:59your uh URL and then you have something
- 2:53:01starting with this question mark and
- 2:53:03after right side whatever you're going
- 2:53:04to have usually you'll call this one as
- 2:53:06query parameters. Okay. So now let's say
- 2:53:09I wanted to get my page information. So
- 2:53:11what I can do right I will do API URL
- 2:53:14and this is going to give me my search
- 2:53:15params and this search param is going to
- 2:53:17give you this. See we have get append
- 2:53:19delete entries blah blah lot of things
- 2:53:21you're going to get get because I need
- 2:53:23to get the information of page. So this
- 2:53:25will be your key and this will be your
- 2:53:27value. So my page is the key value is
- 2:53:29will be two. Limit is key. 10 is value.
- 2:53:32Sort is key. Latest is value. So here I
- 2:53:34wanted to get page. So I'll do page
- 2:53:36here. So this is going to give me page
- 2:53:38information. Copy this. And now let's
- 2:53:40say I wanted to get my limit.
- 2:53:43So this will be my limit.
- 2:53:46And also I'm just going to get a sort.
- 2:53:48This will be my sort. And then if I just
- 2:53:50log all of this and you're going to get
- 2:53:52page
- 2:53:54limit and sort, right? You save all of
- 2:53:57these. For now, I'll comment it out.
- 2:53:59This one here
- 2:54:02and then run it. So you're going to get
- 2:54:042 10 and latest. So you're getting 2 10
- 2:54:07and latest. So this is where you're
- 2:54:09going to use called search params. Okay.
- 2:54:12You can also update the search params.
- 2:54:15how you're going to update. So let's I
- 2:54:16wanted to update my page from two from
- 2:54:19two to three, right? I can do that. So
- 2:54:22I'll do API URL dot search param and
- 2:54:24then you're going to do set and set is
- 2:54:26will be your key value. So key is your
- 2:54:28page value will be your let's say 10. So
- 2:54:32from uh this will be string. Okay. So
- 2:54:35this from two it will be changed to 10.
- 2:54:38Let's say you wanted to change your
- 2:54:39limit from 10 to 20. So I can do like
- 2:54:42this. So I'll do apurl searchparence dot
- 2:54:45set and this will be what is that limit
- 2:54:48limit will be maybe 20. Okay. So now
- 2:54:52what I can do right I can just log it
- 2:54:54here. So I do log and I'll do apur.h
- 2:54:58and see what will happen. Okay. And if I
- 2:55:00just run it.
- 2:55:03So you see that previously you have 210
- 2:55:05latest. Now your page is 10 limit is 20.
- 2:55:08Sort is latest. Means using this set you
- 2:55:11will be able to update these values
- 2:55:13also. Okay. Now you have also another
- 2:55:16very very important something called URL
- 2:55:18search params. This also you can use
- 2:55:20separately. Now when this is very
- 2:55:22helpful let's say wanted to build some
- 2:55:24query string. So in that case URL search
- 2:55:27param is very very very important
- 2:55:29concept very important mean like too
- 2:55:31important because you are actually going
- 2:55:33to use this one in a real project. So
- 2:55:35how you can do this? So let's I wanted
- 2:55:36to create some query params. So I'll do
- 2:55:39new URL search params and this is where
- 2:55:42you can actually give let's say my
- 2:55:43search value is nodej
- 2:55:46my page is one and my limit is
- 2:55:52my limit is let's say five and now what
- 2:55:54I can do right I can simply log this so
- 2:55:57I do log and you can do query params and
- 2:56:00you can do two string
- 2:56:02query params dot two string okay so now
- 2:56:07let's save this And if I just log it
- 2:56:09here, you'll see that what you're going
- 2:56:10to get.
- 2:56:13See, you're getting search nodejs and
- 2:56:16page equal to 1 and limit equal to 5.
- 2:56:19That means this is going to create a
- 2:56:20search param which you can also append
- 2:56:22in your URL. Okay. So this is all the
- 2:56:25things that you are going to uh or you
- 2:56:28can use from your URL module. Okay. So
- 2:56:30these all are very important and
- 2:56:32whatever we have discussed in this
- 2:56:33particular module that actually you're
- 2:56:35going to use in a project. So with this
- 2:56:37now let's move on to the next one. Okay.
- 2:56:38So moving to the next one. So let's
- 2:56:40understand one more and this will be so
- 2:56:42we are going to understand event
- 2:56:44emitters. Okay. So let's do event
- 2:56:47emitter.ts.
- 2:56:49So first we'll understand that what
- 2:56:51actually event emitter is. Now event
- 2:56:53emitter will be or say event emitter
- 2:56:55you're going to use when let's say you
- 2:56:57have
- 2:56:59consider this one that you have one part
- 2:57:01of application that want to announce
- 2:57:02that something happened and other part
- 2:57:05of that application that part want to
- 2:57:07listen to that particular that okay this
- 2:57:09this is something happened and based on
- 2:57:11this I wanted to do something okay so
- 2:57:14simple example will be let's say uh you
- 2:57:17are implementing a functionality okay
- 2:57:19and the functionality is that user
- 2:57:20actually registered
- 2:57:22So once user will comes to your
- 2:57:24application they're going to give your
- 2:57:25email password or whatever right uh name
- 2:57:27blah blah once user registered so
- 2:57:30consider this one as a one part of your
- 2:57:32app so it's doing something right so
- 2:57:35once user will register after this we
- 2:57:38are going to do something okay so for
- 2:57:40example let's say there can be
- 2:57:42possibility that okay we have a feature
- 2:57:44that once user will register we need to
- 2:57:46send a welcome email correct or maybe
- 2:57:50let's say we need to write a log or
- 2:57:54maybe let's say we need to notify some
- 2:57:56other service some other service that
- 2:57:57you have
- 2:58:00okay notify some other service so now if
- 2:58:04you see that this is like a two-way
- 2:58:06thing is happening two-way in the sense
- 2:58:07means I'm trying to explain in a simpler
- 2:58:09way that you are doing something so this
- 2:58:11is what you're doing and based on that
- 2:58:13you want to do something so based on
- 2:58:15user registration you want to do some
- 2:58:17other activity okay so this is where we
- 2:58:20have something called event right so
- 2:58:22this is like event so it's like I'm not
- 2:58:25sure if you're familiar with Kafka so
- 2:58:27it's like a you have a producer so
- 2:58:29something is producing right something
- 2:58:32and that producer is sending that
- 2:58:35information to a uh mediator and then
- 2:58:38consumer is going to consume it so it's
- 2:58:40like this right so you have a producer
- 2:58:42consumer so consider like this so here
- 2:58:45what we have to do right so event
- 2:58:47emitter is going to let's say emit one
- 2:58:49particular event and listener what
- 2:58:52they're going to do? So listeners is
- 2:58:54going to handle that particular listener
- 2:58:56is going to listen to that event and
- 2:58:58based on this is going to do something.
- 2:59:00So for example, let's say here we have
- 2:59:02something like this. So event emitter is
- 2:59:04going to emit one event and then you
- 2:59:07have listeners. Listeners is going to
- 2:59:10listen to this event
- 2:59:13and then it's going to do something.
- 2:59:14It's going to do something based on
- 2:59:16this.
- 2:59:18So here we have some method that you
- 2:59:19need to know which are very important.
- 2:59:21So first one will be
- 2:59:24do on. So what this is going to do? So
- 2:59:26this is going to register
- 2:59:28one listener. So this is the first
- 2:59:30thing. Okay. So every time let's say
- 2:59:34there is a event and that event is
- 2:59:36emitted it's going to reg it's going to
- 2:59:39run and register one listener. Okay. So
- 2:59:41dot on method. Then you have something
- 2:59:44called dot once. Now this is also
- 2:59:47important. Now what this is going to do
- 2:59:48this is going to register a particular
- 2:59:50listener. So this is also register one
- 2:59:52listener but the difference is it's
- 2:59:54going to run only once. That run that
- 2:59:57runs only one time. Okay. And then you
- 3:00:02have something called dot emit. So dot
- 3:00:04emit is going to do what? It's going to
- 3:00:06triggers an event and it's going to send
- 3:00:10that event to the listeners.
- 3:00:13So mostly you need to master all these
- 3:00:15three things. Okay. So first of all
- 3:00:17let's say we will create uh so let's
- 3:00:19take con and I'll do app events. Maybe
- 3:00:22let's do app events only and then I'm
- 3:00:24simply going to do new and here I'm just
- 3:00:26going to do event emitter. This event
- 3:00:28emitter you're going to import from node
- 3:00:31events. Okay. So this will be from node
- 3:00:34colon events and this event emitter. So
- 3:00:37this will be your event emitter. Right.
- 3:00:39So here now what we going to do right?
- 3:00:41So let's say we are let's take this
- 3:00:44example only user registration. So I'm
- 3:00:46just going to take one type and I'll do
- 3:00:49user register payload
- 3:00:54and here we're going to have a num ID
- 3:00:56which will be number and also we're
- 3:00:58going to have a email which will be
- 3:01:00string. Okay.
- 3:01:03Now what I'm going to do right so here
- 3:01:05let's say I have a listener one first
- 3:01:07listener. So when this listener will run
- 3:01:10whatever we are going to write. So
- 3:01:11whenever let's say we have a event and
- 3:01:14that event is going to do something. So
- 3:01:16whenever user will register we are going
- 3:01:18to emit one particular event and once
- 3:01:20we're going to emit that event listener
- 3:01:23is going to listen to that particular
- 3:01:25event and listener is going to run.
- 3:01:27Okay. So in this case in real example
- 3:01:29will be once you'll see that once you
- 3:01:31register any particular website you'll
- 3:01:33see that you are going to get some
- 3:01:35welcome email. Correct. So it's
- 3:01:36something like this. So here what I'm
- 3:01:38going to do right so I will be having so
- 3:01:40I'm just going to app events do on and
- 3:01:43here I'm going to pass a event name this
- 3:01:45event name will be once user will
- 3:01:48register so this is my event name
- 3:01:50consider okay user registered and once
- 3:01:53user will register so whenever this
- 3:01:55particular we are going to emit this
- 3:01:58particular event we are going to pass
- 3:01:59some event okay sorry pass some payload
- 3:02:01and that payload we are going to receive
- 3:02:03here but before doing this what we going
- 3:02:05to do like maybe let's say uh what I'm
- 3:02:08going to do right let's keep it like
- 3:02:09this okay I'm just going to create one
- 3:02:12function and let's do register user so
- 3:02:15this is my actual function where you're
- 3:02:17going to do something so here for now
- 3:02:19I'm just going to give void and I'll do
- 3:02:21con user and user is going to have ID
- 3:02:24one and let's do user is going to have a
- 3:02:26email and I'll take my email here
- 3:02:28whatever you want to take now what I'm
- 3:02:30going to do right so first of all I'm
- 3:02:32just going to do log and I'll do user
- 3:02:34saved so in n in actual application what
- 3:02:36you're going to do you're going to do
- 3:02:37some DB operation and you are going to
- 3:02:39save the user to your DB something like
- 3:02:41this correct and now this is where you
- 3:02:43are going to emit the event see remember
- 3:02:45you need to emit so once you emit the
- 3:02:47event your listener is going to listen
- 3:02:49to that event so here I'm just going to
- 3:02:51do app events do emit and here we are
- 3:02:53going to give the event name okay now
- 3:02:55remember whatever you're emitting the
- 3:02:57event name and the listener event name
- 3:02:59should remain same so here we are
- 3:03:02actually emitting uh that okay user is
- 3:03:04now registered And then we are going to
- 3:03:06pass a user payload. So this will be my
- 3:03:09payload or this will be my data this
- 3:03:11particular event emitter uh event
- 3:03:12emitter is emitting correct. [snorts] So
- 3:03:15now what will happen when I'm going to
- 3:03:16listen to this particular event this
- 3:03:19data whatever I'm sending here this
- 3:03:22listener will be able to access and
- 3:03:24based on this you can do whatever you
- 3:03:25want. For example let's say wanted to
- 3:03:26send a welcome email you can do this
- 3:03:28right. For example, in this user, you
- 3:03:31have the email information. So, we'll be
- 3:03:32able to send that uh email to that
- 3:03:34particular user. So, this is how it will
- 3:03:36work. So, now what you can do, right?
- 3:03:38When this listener is going to listen,
- 3:03:40it will actually match this particular
- 3:03:42event, whatever event we have. And now,
- 3:03:45this is where we are going to get our
- 3:03:46payload. So, here I'm just going to do
- 3:03:48us a register payload.
- 3:03:51And here what we can do can let's say
- 3:03:52I'm just going to log here and I'm just
- 3:03:54going to do email
- 3:03:57listener
- 3:03:59and this email listener is going to do
- 3:04:01let's I'm just going to do a welcome
- 3:04:02email send to
- 3:04:05this user
- 3:04:08and here you're going to pass user do
- 3:04:10email okay so this is going to do that
- 3:04:13now what you can do right this is a very
- 3:04:15important concept that you can have
- 3:04:17multiple listener for the same event
- 3:04:19okay so let's say you wanted to create
- 3:04:21one more listener here. Okay. So maybe
- 3:04:23I'm just going to do app events do on
- 3:04:26and I'm going to give same uh event name
- 3:04:28user register.
- 3:04:32Okay. And then let's say I'm just going
- 3:04:34to take the same thing. So I'll do user
- 3:04:36will be user uh payload. And this is
- 3:04:39where I'm simply going to do what maybe
- 3:04:41let's say I'm just going to log event
- 3:04:43right. So something like this. And here
- 3:04:45I'm just going to do log listener. And
- 3:04:48then I'm just going to do user. Username
- 3:04:50is user do ID and email is and here I'm
- 3:04:55just going to do user do email. Okay. So
- 3:04:58that means you can do what right? You
- 3:05:00can actually register multiple listener
- 3:05:02for the same event also. Okay. So this
- 3:05:06is the first thing that we need to
- 3:05:07understand. Now also let's do one thing.
- 3:05:09After this what we can do right? I'm
- 3:05:10just going to do a log and I'm simply
- 3:05:13going to do register user. And here I'll
- 3:05:17do event listener sorry event listener
- 3:05:22completed something like this okay and
- 3:05:25now let's say first of all I'm just
- 3:05:27going to log this register user here and
- 3:05:29then we're going to understand one more
- 3:05:30concept but this is how it will work
- 3:05:32okay this is how actually in the project
- 3:05:35also we're going to implement so at the
- 3:05:37end it's going to emit event and on the
- 3:05:39other side the listener is going to
- 3:05:41listen to that event and it's going to
- 3:05:42do a real job okay so now let's go here
- 3:05:45I'm just going to copy this and I'll do
- 3:05:4810 ts source/ this and if I go here
- 3:05:56let's run this
- 3:05:59so user saved email listener welcome
- 3:06:01email sent to user sum log listener so
- 3:06:04that means you can see we emitate the
- 3:06:06event only once but we have a two
- 3:06:08listener so both listener will work okay
- 3:06:10and you have log listener and register
- 3:06:13user event listener completed so this is
- 3:06:15how it's going to work. So now we have
- 3:06:17one more concept and that is that how
- 3:06:18you can use once. Once means this one
- 3:06:21register one listener that run only one
- 3:06:23time. So now let's say what I can do
- 3:06:26right I can create one more here. So I
- 3:06:27do app events do on let's have one more
- 3:06:30event here and that is app dot start
- 3:06:32means once our app will start we wanted
- 3:06:34to do something. Okay and here I'm
- 3:06:38simply going to do log and let's do once
- 3:06:41listener. So I'll do once listener and
- 3:06:44now uh app
- 3:06:48app started let's move here. So app
- 3:06:51started something like this. So whenever
- 3:06:53our app will run that time I wanted to
- 3:06:55run this and now what I'm going to do
- 3:06:57right see notice if I go here and I'll
- 3:07:00do app events do emit
- 3:07:02and I have this app dot started this
- 3:07:05event.
- 3:07:07So I'm going to emit this and then I'm
- 3:07:09also going to emit this one more time.
- 3:07:12But what will happen right? Because we
- 3:07:13are using once even though we are
- 3:07:15emitting twice listener will be
- 3:07:18registered with only once because we
- 3:07:20have once here. Oh we need to do once
- 3:07:22here we need to do once. Okay. So this
- 3:07:24will be once. So it will only run first
- 3:07:26thing and if I go here let's run this
- 3:07:29again.
- 3:07:31So you see that once listener app
- 3:07:33started user saved email listener and
- 3:07:35all this. So this will be the uh concept
- 3:07:38of uh event listener that what actually
- 3:07:41event is the very important method how
- 3:07:43we are going to emit event and how
- 3:07:45listener is going to listen to that
- 3:07:46event. So with this now let's move on to
- 3:07:48the next one. So with the core modules I
- 3:07:50think we are almost at the end and now
- 3:07:52let's finish this particular section
- 3:07:54with so I'm just going to 11 and we are
- 3:07:56going to understand streams okay streams
- 3:07:58are also very important. So first of all
- 3:08:02you have to understand again as usual
- 3:08:03right that when you're going to use
- 3:08:05stream and why actually you need it. So
- 3:08:07first of all streams are used when the
- 3:08:09data uh you want to handle. So whatever
- 3:08:11data you're handling piece by piece.
- 3:08:14Okay. So you want to handle the data
- 3:08:15piece by piece. Okay. Now what I mean by
- 3:08:18this? So here what you're doing right
- 3:08:20you are not loading the data not loading
- 3:08:22the data not loading the data like
- 3:08:26everything at once. Everything
- 3:08:29at once. So this will be better way to
- 3:08:31put it. So you're not loading the data
- 3:08:32all at once. You are handling it piece
- 3:08:35by piece. Right? Now the best example of
- 3:08:38you uh stream using will be let's say
- 3:08:40you wanted to read some large files.
- 3:08:43Okay. Or you want to upload files. This
- 3:08:46is also very very important. This is
- 3:08:48where you can actually use streams.
- 3:08:50Okay. Or same will be downloading. So
- 3:08:52you want to download files. Let's say
- 3:08:54you're downloading very big files. So
- 3:08:56instead of downloading all at once,
- 3:08:57you're downloading piece by piece.
- 3:08:58Right? Some sometime you are also going
- 3:09:01to use let's say you're improving audio
- 3:09:02video uh processing that also you can
- 3:09:06use. Now see whatever this uh we are
- 3:09:09discussing that all these use cases are
- 3:09:10actually real and you are actually going
- 3:09:12to use this in projects also you need
- 3:09:14this one sometimes in compressions also.
- 3:09:18So this is will be where you're going to
- 3:09:20use uh streams. Okay. Now uh first of
- 3:09:23all what will happen right the main idea
- 3:09:25or the core concept of stream will be
- 3:09:27that stream doesn't give you the data
- 3:09:29immediately. So it's not going to give
- 3:09:30you the data immediately. It's going to
- 3:09:32give you the data is in small and we
- 3:09:35already know this particular term that
- 3:09:37we have discussed and that is called
- 3:09:39chunk. Right? So remember I already
- 3:09:41mentioned this one chunks. So here it's
- 3:09:43going to give you the data in small
- 3:09:45pieces that we call as chunks. Okay.
- 3:09:48So now how it's going to do? So let's
- 3:09:51say I have this that okay here is here
- 3:09:54is my full 500 MB file something like
- 3:09:59this stream what it's going to do it's
- 3:10:01going to do this okay so look here is
- 3:10:03chunk one
- 3:10:06then it's it will do here is chunk two 3
- 3:10:114 5 something like this okay so it's
- 3:10:14going to give you in chunk why now first
- 3:10:16of all one of the most advantage would
- 3:10:18be this will be memory efficient because
- 3:10:20you can Right? We are not handling all
- 3:10:22the data at once. So there will be not
- 3:10:25too much overloading on memory. Right?
- 3:10:26So this will be memory efficient. Okay.
- 3:10:29Now u also you also need to understand
- 3:10:32some of the stream types. Okay. I I
- 3:10:34forgot to mention this. So for example,
- 3:10:35let's say we have something called uh
- 3:10:38stream types. Stream types are actually
- 3:10:40important. So we have readable stream,
- 3:10:42right? So you have readable readable
- 3:10:44streams. I'm not sure if the spelling is
- 3:10:46wrong. If it is wrong, I think readable.
- 3:10:48Okay. So readable stream. Readable
- 3:10:50streams will be or readable stream will
- 3:10:52be like where you have some data. So you
- 3:10:54have some source of data something like
- 3:10:56this.
- 3:10:57Okay. Now here maybe let's say example
- 3:11:00will be uh what maybe HTTP request body
- 3:11:04or file you're going to read some data
- 3:11:06or read some data from a file big file
- 3:11:08right so this will be readable then you
- 3:11:11have something called writable stream.
- 3:11:14Writable stem will be like uh there will
- 3:11:16be some place or some uh I don't know
- 3:11:19the English term man okay uh destination
- 3:11:22so there will be some destination where
- 3:11:23you're going to data you want to write
- 3:11:25the data so there will be destination
- 3:11:28where the data where the data is written
- 3:11:32okay and then you have something called
- 3:11:35transform transform transform stream now
- 3:11:38this is also very important transform
- 3:11:40means it's kind of a stream that's going
- 3:11:42to read your data it's is going to
- 3:11:44change it also. Okay. And then very
- 3:11:47important, it's going to pass that
- 3:11:48stream to forward. So pass it forward.
- 3:11:50So it's going to read the data.
- 3:11:53Read the data if needed. It's going to
- 3:11:55change it. So it's going to change the
- 3:11:56data and then it's going to pass the
- 3:11:58data. Pass that pass that forward. Okay.
- 3:12:04So example will be let's say this one
- 3:12:06only. Compression will be a proper
- 3:12:07example for this, right? Or maybe let's
- 3:12:09say you want to the basic example. Let's
- 3:12:11say you wanted to convert your text to
- 3:12:13upper case. So we are going to see some
- 3:12:14example but from a definition wise
- 3:12:16right. So all these key points whatever
- 3:12:18I have explained are actually very
- 3:12:19important. So let's start with readable.
- 3:12:21Readable means I already mentioned. So
- 3:12:23there are some method you need to also
- 3:12:25understand. So let's create a readable
- 3:12:26stream. So I'm just going to do readable
- 3:12:29and I'll do string. Okay. What you're
- 3:12:31going to do right you're going to use
- 3:12:32something called readable. And this
- 3:12:34readable from where you're going to use
- 3:12:36this readable you're going to import
- 3:12:38from this node uh stream. So you can see
- 3:12:40that you have this one. So vable you're
- 3:12:42going to use from your stream package.
- 3:12:44So you have node colon stream.
- 3:12:47Okay. So what this is going to do right.
- 3:12:49So first of all let's create this. So
- 3:12:50I'm just going to readable and you have
- 3:12:52to use this form. So here what this is
- 3:12:55going to do this is going to create a
- 3:12:56readable string but it's going to create
- 3:12:58from an array. Each item that you're
- 3:13:00going to have in this particular array
- 3:13:02will become what will become chunks.
- 3:13:04Chunks are very important. Okay. So
- 3:13:06whatever array you are going to have is
- 3:13:08it will become chunks. How you're going
- 3:13:10to do this? So we're going to do like
- 3:13:11this. So every data let's have a hello
- 3:13:14give space uh space let's say maybe I'll
- 3:13:17do from let's do nodejs node js and I'm
- 3:13:22just going to do streams something like
- 3:13:24this right. So this will be readable
- 3:13:28stream. So very simple one. Now let's
- 3:13:31understand transform one. So what you're
- 3:13:33going to do right maybe let's uh take
- 3:13:35that example that we are going to
- 3:13:36convert some text to upper case. So what
- 3:13:38I'll do right I'm just going to create a
- 3:13:40const and I'll do uppercase
- 3:13:43transform.
- 3:13:46So here we're going to use something
- 3:13:48called new transform and this transform
- 3:13:50you're going to import from node stream
- 3:13:51only. So what this is going to do right.
- 3:13:54So here you are going to use a transform
- 3:13:56method. So you can see that you have a
- 3:13:57transform. Transform is going to take a
- 3:13:59chunk encoding and there will be a call
- 3:14:01back. Okay. Chunk. What is chunk? Chunk
- 3:14:04is usually buffer in the buffer concept
- 3:14:06we have discussed right. So chunk will
- 3:14:07be buffer. What we are going to do right
- 3:14:09here? Let's say we have some uh string
- 3:14:12and then we are going to convert that
- 3:14:14chunk to a string that also discussed
- 3:14:16that you can directly do chunk dot to
- 3:14:18string and based on that you can do
- 3:14:19certain operation and at the end what
- 3:14:21you can do you can pass that result to
- 3:14:23the call back. Call back format also you
- 3:14:25have discussed the classic callback
- 3:14:27format call back and then you're going
- 3:14:29to have error and then you're going to
- 3:14:30have result. You remember this part I
- 3:14:32discussed. So consider so here first of
- 3:14:34all let's take this one at transform. So
- 3:14:36in this transform this is going to take
- 3:14:38chunk this is also going to take the
- 3:14:41encoding and this is also going to take
- 3:14:43a callback which is very very important.
- 3:14:45Okay. So now let's say I wanted to do
- 3:14:47something here. So first of all I will
- 3:14:49be having a text and then I'm just going
- 3:14:51to do chunk dot twing.
- 3:14:54Okay. And here so let's take the call
- 3:14:58back. So error will be null and I'm just
- 3:15:00going to do so this is what I'm just
- 3:15:01going to do. Text dot two uppercase.
- 3:15:05So we're going to do text dot two
- 3:15:07uppercase. So what we are doing right we
- 3:15:09are actually sending this transform data
- 3:15:12this transform data where so it's going
- 3:15:14to send this data forward. Okay. Null
- 3:15:17means we don't have any error. Okay. And
- 3:15:19this result is nothing but it will be
- 3:15:21the new chunk that is going to move to
- 3:15:24the next string. Now I know that this
- 3:15:26might be little tricky but you need to
- 3:15:28understand all of this right that how
- 3:15:29readable uh transform and writable all
- 3:15:32these string works or that there is no
- 3:15:33point because these are very very basic
- 3:15:35concept. Now what we're going to do
- 3:15:37right now let's write one writable
- 3:15:39string. So writable string is going to
- 3:15:40do it's going to receive the chunk and
- 3:15:43then it's going to do something we don't
- 3:15:44know what is why because that is going
- 3:15:46to depend uh based on the project or
- 3:15:48based on the feature you are doing.
- 3:15:50Okay. So what this is going to do right
- 3:15:52simple way just remember or you can note
- 3:15:54it down writable is going to write data
- 3:15:56to a particular file or it's possible
- 3:15:59it's going to send data to HTTP server
- 3:16:01also or maybe let's say it's going to
- 3:16:02upload data to some cloud that is also
- 3:16:06possible. Okay. So now
- 3:16:09uh let's do writable stream. So how you
- 3:16:12can do write you are going to use
- 3:16:13something called writable. So you'll see
- 3:16:15that we have writable this one. Okay.
- 3:16:18And now here you're going to do
- 3:16:19something called write. See, write also
- 3:16:21is going to receive what? Chunk,
- 3:16:22encoding, call back. So, I'm just going
- 3:16:24to take right. So, I'll do chunk. Uh, we
- 3:16:27have the encoding and the call back. So,
- 3:16:29do encoding and the call back. So, first
- 3:16:33I'm just going to log something here and
- 3:16:35let's do received
- 3:16:38chunk and here I'm simply going to do
- 3:16:40chunk dot to string. So, this is the
- 3:16:44first thing. So, now what we're going to
- 3:16:46do right, we are simply going to call
- 3:16:48this callback. You have to understand
- 3:16:50till this time we have already discussed
- 3:16:52that multiple times what actually call
- 3:16:54back is but why we need this call back
- 3:16:56here. What this call back is going to
- 3:16:57do. So this call back is going to tell
- 3:16:59node that okay I already have handled
- 3:17:01this particular chunk. Now you can send
- 3:17:05this one to next one. Simple. Okay. So
- 3:17:08this means this call back is simply
- 3:17:10telling that okay I'm done with my job.
- 3:17:11Now you do the rest or move this one to
- 3:17:14next one. And now you are going to we
- 3:17:16are going to combine all of these to a
- 3:17:18certain not certain we are going to
- 3:17:20combine all these readable transform
- 3:17:22writable into one particular concept and
- 3:17:24that we call as pipeline. Now pipeline
- 3:17:27is very very important. What pipeline is
- 3:17:29going to do? Now you see that each and
- 3:17:30everything is individual right? So it's
- 3:17:32going to read something. It's going to
- 3:17:33create a readable stream. It's going to
- 3:17:35do some action here. It's going to uh
- 3:17:38convert this one text to upper case and
- 3:17:40it's going to write this data to some
- 3:17:41place whatever it is. But all these are
- 3:17:44independent. There is no connection. So
- 3:17:46that means what we're going to do? We
- 3:17:47need to use something that is called
- 3:17:48pipeline. Pipeline is going to connect
- 3:17:50all these stream together. Okay. So now
- 3:17:53we are going to create a uh let's do a
- 3:17:56function and I'm just going to do a main
- 3:17:58and here I'm just going to have a
- 3:18:00promise.
- 3:18:02Let's do void. So how this will work? So
- 3:18:05first of all we're going to take a try
- 3:18:06catch. So in the catch we're going to do
- 3:18:09error. So first let's handle the error.
- 3:18:11So we'll do the same thing. So I'll do
- 3:18:13message if this error is instance of
- 3:18:15error. So I'm going to do error dot
- 3:18:19message or else I'm just going to do
- 3:18:21unknown error.
- 3:18:24And here I'm just going to do
- 3:18:27stream
- 3:18:29failed and here I will do message.
- 3:18:33Okay. Now in the try so we going to use
- 3:18:36something called a and we going to use
- 3:18:38something called pipeline and this
- 3:18:39pipeline we are going to import from
- 3:18:41node string promises okay so from here
- 3:18:44we are going to import so from node
- 3:18:45streams has promises you're going to
- 3:18:47have so what this pipeline is going to
- 3:18:49do now this is actually very important
- 3:18:50concept okay and this can be if you know
- 3:18:52all this right and let's see if you can
- 3:18:54answer all of this in interview you'll
- 3:18:55be like okay your interview will be
- 3:18:57actually impressed okay if you know
- 3:18:59because these are pure node concept we
- 3:19:01are not using any framework or anything
- 3:19:03like Right pipeline is going to connect
- 3:19:05all your stream together. Okay. So we
- 3:19:08have first we have a readable string
- 3:19:10correct and then readable string we
- 3:19:12going to do what? We are going to upper
- 3:19:13case. So we going to convert that text
- 3:19:15to upper case and then we going to use
- 3:19:17the rightable. So now what you can do
- 3:19:19right you can actually directly pass all
- 3:19:21your source here. Source means you can
- 3:19:22pass your readable. Then I'm just going
- 3:19:25to do my uppercase. So this is my
- 3:19:28transform. So we are transforming
- 3:19:29something. We are doing some action and
- 3:19:30then we going to do a writable. That's
- 3:19:32it. And then what you can do, you can
- 3:19:34just do a log and this stream sorry do
- 3:19:38stream completed something like this.
- 3:19:41That's it. And then what you're going to
- 3:19:42do right, you're going to just uh not
- 3:19:45here outside. We're going to call this
- 3:19:47main.
- 3:19:49So save this.
- 3:19:51Now let's copy this. Go here.
- 3:19:57So we'll do tsx source/ this
- 3:20:03and let's do here. We'll see what you're
- 3:20:05getting. So you're getting received
- 3:20:06chunk hello from nodejs uh streams and
- 3:20:11uh string completed. So you can see that
- 3:20:14from where you are getting this one
- 3:20:17receipt chunk means that means you are
- 3:20:19writing. Now if you notice one thing
- 3:20:22when you have created the readable we
- 3:20:24have this one in lower case but this
- 3:20:26transform stream is going to do what is
- 3:20:28actually transforming this stream and
- 3:20:30then it actually passing this one to a
- 3:20:32writable stream and then it's going to
- 3:20:34simply saying that okay now I've
- 3:20:36received it for for example here we are
- 3:20:38simply logging right but in actual
- 3:20:39project maybe you're writing this one to
- 3:20:41a file system or maybe you're sending
- 3:20:43this data to some place we are storing
- 3:20:45the data to our cloud whatever it is but
- 3:20:47you can see that this is actually
- 3:20:48received the data Right. So chunk we are
- 3:20:51getting capital and who is doing this
- 3:20:53capital? This uppercase transform this
- 3:20:55stream and then we simply telling that
- 3:20:57now this call back is telling okay
- 3:20:59writeable stream task is done. You go
- 3:21:01next. Now it will check here we don't
- 3:21:03have any other stream. So now stream
- 3:21:05will be completed. So this is how you
- 3:21:07can make use of this stream. So couple
- 3:21:10of key points you have to understand
- 3:21:11that stream work with chunks. Streams
- 3:21:13work with chunks. Readable stream what
- 3:21:16it's going to do? It's going to produce
- 3:21:17the data. Writable stream is going to
- 3:21:19consume the data and it's going to do
- 3:21:21something. And transform stream is
- 3:21:22actually very important. Transform
- 3:21:24actually doing something. It's actually
- 3:21:26modifying the data in the middle. See,
- 3:21:28it's actually converting this one to
- 3:21:29uppercase before it will pass to the
- 3:21:31right angle and pipeline is going to
- 3:21:33combine all your stream together. Okay.
- 3:21:36One thing you don't have to use stream
- 3:21:38everywhere. This is also another
- 3:21:39important concept and this is also a bad
- 3:21:41practice if you're using streams
- 3:21:42everywhere. So, so it's like oh I know
- 3:21:45stream and I have to show off my skill
- 3:21:46and then I'm using. Don't do like this.
- 3:21:48If your data is very large then only you
- 3:21:50have to use streams. Okay. So with this
- 3:21:53now our core module is completed. So I
- 3:21:57think we have almost covered most of the
- 3:21:58things and some of the things are very
- 3:22:00important. I think in other videos you
- 3:22:01will not find in this much details. So
- 3:22:04with this now let's move on to the next
- 3:22:06section. All right. So till this part
- 3:22:08what we have completed. So first section
- 3:22:10is completed right. So you have learned
- 3:22:12that what actually NodeJS is setup
- 3:22:15installation and all the code modules
- 3:22:17and the concepts and I think this is
- 3:22:19more than enough. See whatever concept
- 3:22:20that I mentioned uh that are a little
- 3:22:23more advanced. So for that one I will be
- 3:22:25creating a part three. Now you might be
- 3:22:27thinking that okay I already have a part
- 3:22:28three that I have uploaded last year but
- 3:22:30this is just to mention that part three
- 3:22:32is completely different because in that
- 3:22:34part three we have discussed uh
- 3:22:35different tech like uh I think if I'm
- 3:22:38able to recolct I've discussed with uh
- 3:22:40something like burn hono and then I've
- 3:22:42covered NJS but this part three will be
- 3:22:45completely different all right so here
- 3:22:46we are only going to cover NodeJS
- 3:22:48advanced concept why part three because
- 3:22:50in the part two we are going to have
- 3:22:52NodeJS microservices okay if you're
- 3:22:54following this channel you already know
- 3:22:55what I'm talking about so I think
- 3:22:57whatever we have discussed in the
- 3:22:58previous section that is more than
- 3:23:00enough okay but now the real uh concepts
- 3:23:02that we will start learning so first of
- 3:23:05all what we are going to learn so first
- 3:23:06we'll start with learning node HTTP
- 3:23:09module because this is where it will
- 3:23:11become more and more practical okay and
- 3:23:13from this particular section whatever
- 3:23:15things I'm going to repeat whatever
- 3:23:17things you are going to learn you have
- 3:23:19to do this one practically properly and
- 3:23:21you also have to learn that how you're
- 3:23:22going to apply all those concepts in a
- 3:23:24project where you're going to actually
- 3:23:26build production level API applications.
- 3:23:28Okay, so first we are going to target
- 3:23:30node HTTP module. This is what we are
- 3:23:32going to do after [snorts] this. Okay,
- 3:23:35extremely important. Okay, I'm going to
- 3:23:36repeat once again. Please note it down.
- 3:23:39There will be one section where I'm
- 3:23:41going to add a postgress full course.
- 3:23:42Now this particular part that we will
- 3:23:46have before starting our actual NodeJS
- 3:23:48project, this video is already uploaded
- 3:23:50on YouTube. Okay, but why I'm adding?
- 3:23:52because if I'm not wrong, I think most
- 3:23:54of the people haven't watched because
- 3:23:56that video didn't get that much
- 3:23:57response. Now why I'm telling this is
- 3:23:59absolutely mandatory. Uh this particular
- 3:24:02section, the reason is very simple
- 3:24:04because in this particular section, I
- 3:24:06think this will be around 4 hours. Each
- 3:24:08and every topic I discussed that if
- 3:24:10you're a beginner, this will be
- 3:24:12absolutely perfect video for posgress to
- 3:24:14start. So I'll highly recommend like
- 3:24:16100% recommend to complete this
- 3:24:18particular section before you will jump
- 3:24:20to next part. Okay. So I just wanted to
- 3:24:23repeat once again once we'll complete
- 3:24:25this node HTTP module we are going to
- 3:24:28have have one complete dedicated
- 3:24:29postgress section. Now if you recolct
- 3:24:32right if you watch my previous NodeJS
- 3:24:33part one video there also I have kept
- 3:24:35one section for MongoDB because that
- 3:24:37project uh or that video was completely
- 3:24:39dedicated to MongoDB because this one we
- 3:24:42are going to cover posgress and also in
- 3:24:44NodeJS microservices also we are going
- 3:24:45to have Postgress. Highly highly
- 3:24:47recommended do not skip this particular
- 3:24:49section. Okay. If you skip it's fine if
- 3:24:51you already know but but if you don't
- 3:24:53know like postgrad basic concept then
- 3:24:55you have to watch this. So this will be
- 3:24:57prerequisite. This particular section is
- 3:24:59prerequisite for the next section.
- 3:25:01[snorts] Once we cover this right we are
- 3:25:03going to have our actual practical
- 3:25:05project. Now you can me see that I
- 3:25:06clearly mentioned this will be all in
- 3:25:08one covers everything. So what I mean by
- 3:25:10this? So let's discuss or else let's not
- 3:25:12directly jump to the uh practical part
- 3:25:14because you need to know right that what
- 3:25:16are the things you have to learn. So
- 3:25:18what I mean by this allin-one. So in
- 3:25:20this particular project you are going to
- 3:25:22understand how we are going to use
- 3:25:24posgress with docker. Okay. So I'm going
- 3:25:26to give you some see we are not going to
- 3:25:28have too many things around docker. If
- 3:25:30you're thinking okay I don't know
- 3:25:31docker. So we are going to have one
- 3:25:32single docker compost. If you want you
- 3:25:35can also skip docker but I will highly
- 3:25:37suggest to use it because it is very
- 3:25:39very helpful and it is also production
- 3:25:42oriented right. So you'll understand
- 3:25:44that how we can create posgress pool or
- 3:25:46posgress connection. how you are going
- 3:25:48to implement a proper folder structure
- 3:25:50when you're going to implement any
- 3:25:52production level NodeJS application.
- 3:25:54Third, how you can implement CRUD. CRUD
- 3:25:56means create uh read, update and delete
- 3:26:00which is the pillar of each and every
- 3:26:02project. Whatever project you are going
- 3:26:03to build, whatever complexity of that
- 3:26:05project is at the end everything will be
- 3:26:08CRUD. So CRUD we are going to cover. We
- 3:26:10are going to cover authentication JWT
- 3:26:12authentication. We are not using any
- 3:26:13third party packages. Okay. Then we are
- 3:26:16also going to have file upload. So this
- 3:26:18project is going to cover everything.
- 3:26:19Now you might be thinking okay the same
- 3:26:22thing we have covered in the part one of
- 3:26:24the NodeJS also that I uploaded two
- 3:26:26years back actually yes because see when
- 3:26:29I'm uploading a new video the only
- 3:26:31difference is that this is on posgress
- 3:26:33that one on MongoDB and to be honest I
- 3:26:36will be very honest right now in this
- 3:26:39particular uh time right learning
- 3:26:41posgress will give you too much age
- 3:26:43because most of the products and level
- 3:26:44application will see that they're not
- 3:26:46using MongoDB. So I just wanted to
- 3:26:47mention so if you're thinking that okay
- 3:26:49what is the new thing in this particular
- 3:26:51video or on this particular project to
- 3:26:53be honest that is not much at the end we
- 3:26:55are going to cover all the backend
- 3:26:56responsibilities only but how you can
- 3:26:58create a proper folder structure the
- 3:27:00coding standard working with posgress
- 3:27:02all these things we are going to
- 3:27:03understand after this we are going to
- 3:27:06have one complete dedicated section on
- 3:27:08radius this also another video that I
- 3:27:10have uploaded same video I already
- 3:27:12mentioned that that will be prerequisite
- 3:27:14for node this video again I didn't get
- 3:27:16proper response I'm going to add this
- 3:27:17section. If you want to understand how
- 3:27:20to work with background jobs bmpq then
- 3:27:22this is also prerequisite section. So
- 3:27:25why I'm mentioning all of this so that
- 3:27:27you will be in track. So first we'll
- 3:27:28start with node http then complete your
- 3:27:31posgress section. Once complete please
- 3:27:34do not jump directly from here to here.
- 3:27:36Please do not do this. Okay. I highly
- 3:27:38recommend if you're serious do this.
- 3:27:40First learn posgress then jump to this
- 3:27:43one. Once you'll do that learn radish
- 3:27:46have some patience and then go to the
- 3:27:48practical section. So this will be
- 3:27:49proper sequence. Okay. So with this now
- 3:27:52let's get started. The first thing what
- 3:27:53I have done I created a single folder.
- 3:27:56You can see that we don't have anything
- 3:27:57else here. I think we have already
- 3:27:59covered all these things right. How you
- 3:28:00can create package.js and tsconfig in
- 3:28:03the previous section. So that is the
- 3:28:04reason I haven't uh I'm not showing you
- 3:28:06all of this. So what we are going to do
- 3:28:08in this one we are going to understand
- 3:28:09couple of things. So first we have to
- 3:28:11understand the basic HTTP server and the
- 3:28:13concept how you can implement routing uh
- 3:28:17body reading body and also JSON
- 3:28:19response. These four will be main
- 3:28:21pillar. Why we are covering all of this?
- 3:28:23Because once you start XL before that
- 3:28:26you need to know the core HTTP module or
- 3:28:28else there is no point and also we are
- 3:28:30going to call one very important section
- 3:28:31where we are going to call one external
- 3:28:33API because there is a possibility that
- 3:28:36some section will be uh that not some
- 3:28:38section in some projects let's say
- 3:28:40you're working in a company there is a
- 3:28:42possibility that you will have some
- 3:28:43third party API that you need to call so
- 3:28:46you also need to understand some of the
- 3:28:47concepts there also so with this now
- 3:28:49let's get started so first we'll start
- 3:28:51with understanding the basic HTTP be
- 3:28:53server concept from the next part. Okay.
- 3:28:55So let's start creating the first file.
- 3:28:57Okay. So for this one what we are going
- 3:28:59to do right let's see we don't have to
- 3:29:00create any proper folder structure or
- 3:29:02anything for now because we need to
- 3:29:04understand the concept right. So as
- 3:29:06usual we will create single file like
- 3:29:07this. So I'm just going to give this one
- 3:29:09as basic http server.ts.
- 3:29:13So first what are the things that we
- 3:29:16need to understand. First we need to
- 3:29:17understand that from which package we
- 3:29:19are going to import http right. So first
- 3:29:21let's do this and we I think you know
- 3:29:23very well right. So I'm going to keep
- 3:29:25complete section practical only. There
- 3:29:28is no point of explaining only concept
- 3:29:30then do practical but we are going to do
- 3:29:32vice versa. First we're going to write
- 3:29:33the practical and then write the concept
- 3:29:34in this file only just like we have done
- 3:29:36in the previous section. So input http
- 3:29:39from this module. So from which module I
- 3:29:41need to do. So I'm going to do node and
- 3:29:43then you're going to give colon and this
- 3:29:45will be your http right? You can also do
- 3:29:47something like this. So do http. Okay,
- 3:29:50but I'll do like this. So we are getting
- 3:29:52from node HTTP module. So this will be
- 3:29:55my module. First what I'm going to do
- 3:29:57right, I'm going to take one port that I
- 3:29:59wanted to run this on 30,000 port. So
- 3:30:02first we need to understand that how you
- 3:30:04can create a HTTP server. So to create a
- 3:30:06HTTP server, you need to use something
- 3:30:08called HTTP.create server. So this
- 3:30:10create server is a method that is going
- 3:30:12to do what? It's going to create a
- 3:30:14low-level HTTP server. Okay. So first
- 3:30:17let's create the server. Then we'll
- 3:30:18understand each and everything. So let's
- 3:30:20start with server. So I'm going to take
- 3:30:22HTTP and this is going to give me this
- 3:30:25create server. Okay. Now this create
- 3:30:27server is going to have something. Okay.
- 3:30:29So this is going to have something
- 3:30:30called request and this is going to have
- 3:30:33response. So what this is all about very
- 3:30:35important concept like these are like
- 3:30:37core concept. Okay. You can't skip any
- 3:30:40of these. Request is your request
- 3:30:41object. Response is your response
- 3:30:43object. So what actually this request
- 3:30:46object contains right? So first you are
- 3:30:48going to have something called as
- 3:30:49request and then you're going to have
- 3:30:51something called as response. Now you're
- 3:30:53going to get some error because this is
- 3:30:55going to not able to understand that
- 3:30:56what actually this is. So in this case
- 3:30:58we need to import something from this
- 3:31:00particular model. So I'm going to import
- 3:31:02a type and I'm going to tell that okay
- 3:31:04this is my incoming message that the
- 3:31:07request I'm going to receive and this is
- 3:31:09my outgoing or this is my server
- 3:31:11response not outgoing but this is my
- 3:31:13server response. So I'm going to import
- 3:31:14type and here we're going to have
- 3:31:16something called a server response. So
- 3:31:18first I'm going to tell that okay this
- 3:31:20is my incoming request
- 3:31:24and this is my server response.
- 3:31:28Okay. So now after this what we going to
- 3:31:30do right also we have to take this one
- 3:31:33as a call back and here now we are going
- 3:31:37to do something. So first we need to
- 3:31:39understand here that what is happening.
- 3:31:40So first we understand that so let's
- 3:31:43write it like this. I'll do http dot
- 3:31:45create server. This is going to create
- 3:31:48low-level
- 3:31:50http server. And second we have a call
- 3:31:55back right. So this is our call back.
- 3:31:58Now this call back is going to run for
- 3:32:00every incoming HTTP request. So we have
- 3:32:03a call back. So call
- 3:32:05is going to run for every incoming
- 3:32:09request that you're going to get
- 3:32:13incoming HTTP request. What this request
- 3:32:16means? So this request is nothing but
- 3:32:19this is our request
- 3:32:22object.
- 3:32:24What this request object contains. Now
- 3:32:26this is going to contain couple of
- 3:32:28things, right? So first if I just do
- 3:32:29here something like this. So I'll do
- 3:32:31request dot and you can see that you're
- 3:32:33going to have lot of things here. So
- 3:32:34many things are happening. So some of
- 3:32:36the very important point you have to
- 3:32:37understand. First there will be a
- 3:32:39request method. Okay. Now this request
- 3:32:41method is going to tell the type of
- 3:32:43method that or type of request that we
- 3:32:45are doing. So for example this method
- 3:32:48can be your get this can be your post.
- 3:32:50This can be your put this can be your
- 3:32:52options. This can be your delete
- 3:32:55something like this. Okay. So this is
- 3:32:57your request dot method. Then you're
- 3:32:59going to have something called request
- 3:33:00URL. What this is going to tell this is
- 3:33:02act this is actually your requested
- 3:33:04path. Okay, for example, we are calling
- 3:33:07to this path slash or we are calling to
- 3:33:09this / user. So this is your path or so
- 3:33:12in this case this will be your request
- 3:33:14dot URL. Okay, then we have something
- 3:33:16called very important we have request
- 3:33:18headers. So this is actually these
- 3:33:21headers are nothing but so these headers
- 3:33:24will be the actual metadata.
- 3:33:27actual metadata
- 3:33:30sent by the client. So what I mean by
- 3:33:33client client is so in this case your
- 3:33:35client can be let's say you have a
- 3:33:36reactjs application so you're sending
- 3:33:37the headers okay and next you have
- 3:33:40something called request body right the
- 3:33:42request body is nothing but so this will
- 3:33:44be your data that will be sent by the
- 3:33:46client right whenever we are going to do
- 3:33:48let's say post or put type of request so
- 3:33:51this will be your request object then
- 3:33:53we're going to have our response so
- 3:33:55which is our response
- 3:33:57object right so this is nothing but so
- 3:34:00this particular object will be used by
- 3:34:02our server to send something back to the
- 3:34:06client. Okay. So let's say you're doing
- 3:34:08some API call and once you do the API
- 3:34:11call or whatever DB operations you are
- 3:34:13doing you are doing so you're going to
- 3:34:15get some response back and that response
- 3:34:18you need to render in your UI so that
- 3:34:20your user can see okay so in that case
- 3:34:23that particular response you have to
- 3:34:24send back okay so because you are going
- 3:34:27to have your back end and front end
- 3:34:28right so until unless you're going to
- 3:34:29send the data back from your back end to
- 3:34:32front end there is no way you can render
- 3:34:34the data correct so this response is
- 3:34:37going to help you in that particular
- 3:34:38case. So it's going to send something
- 3:34:40back to your client. So what are the
- 3:34:42things going to send? So you're going to
- 3:34:43have something called status code. So
- 3:34:45this status code is going to tell that
- 3:34:47okay if the response or if the operation
- 3:34:50that we are doing if that is successful
- 3:34:52or not then you're going to also pass
- 3:34:53the response headers and then you're
- 3:34:56going to have your response body.
- 3:34:59Okay. So this is all about the main
- 3:35:02concept. Okay. So first you're going to
- 3:35:04have what? So HTTP create server is
- 3:35:06going to create your actual HTTP server.
- 3:35:09There will be a call back which is going
- 3:35:10to run every time there will be HTTP
- 3:35:12request. This is your request object and
- 3:35:14this is your response object. Okay. So
- 3:35:16now first of all what we are going to do
- 3:35:18right. So in this
- 3:35:21so let's remove this. So first I'm just
- 3:35:23going to do con
- 3:35:26method. So this will be my request do
- 3:35:28method. So this will be the first thing.
- 3:35:32Again I think here we have let's write
- 3:35:34it only once so that you remember. So
- 3:35:36here we are going to have request dot
- 3:35:37method can be get. Okay. So in this case
- 3:35:40get means you are reading data.
- 3:35:43Then you're going to have post. So post
- 3:35:45means you're going to creating some new
- 3:35:47data. So you're going to create data or
- 3:35:50creating or let's do create data. Then
- 3:35:53you're going to have put. So put will be
- 3:35:56you are actually replacing the data.
- 3:35:58Replace data. Then you're going to have
- 3:36:01something called patch. So patch is you
- 3:36:03are updating but you're updating partial
- 3:36:06data or let's update partial data and
- 3:36:11then you're going to have delete. So
- 3:36:13delete is nothing but I think this is
- 3:36:14very easy to understand that delete is
- 3:36:16nothing but you are deleting the data.
- 3:36:18So delete data. So normally these are
- 3:36:21very very important. This is what you're
- 3:36:23going to use most of the times there are
- 3:36:24lot so many but if you're learning
- 3:36:27NodeJS in depth okay in-depth see again
- 3:36:30in depth in the sense I'm not talking
- 3:36:31about you have to be like a node master
- 3:36:34okay but if you're working in a company
- 3:36:36right let's say you're targeting for job
- 3:36:39this is more than enough why I'm telling
- 3:36:41because this is when you're going to
- 3:36:43implement some actual products and level
- 3:36:46application even in your company right
- 3:36:47you'll see that most of the times you
- 3:36:49are going to use all of these so this
- 3:36:52This is about your request dot method.
- 3:36:55So then we're going to have our URL. So
- 3:36:56this will be my request dot URL.
- 3:37:01Okay. So I already mentioned this one.
- 3:37:03So this will be request URL is uh
- 3:37:05nothing but that in which path the
- 3:37:08client is actually requesting. So this
- 3:37:10will be in which path the client
- 3:37:15is actually requesting.
- 3:37:20Okay. So this will be your request dot
- 3:37:21URL and then you're going to have what
- 3:37:24we are going to have something called
- 3:37:25headers. So for example here let's do
- 3:37:29one thing. So I'm just going to do one
- 3:37:30user agent header. So how you can do
- 3:37:32this? You're going to do request and
- 3:37:34you're going to pass a header and then
- 3:37:36here you can pass something called user
- 3:37:38agent. Okay what actually headers are
- 3:37:40see headers are nothing but your
- 3:37:43metadata. Now what how I will explain?
- 3:37:45So headers are nothing but extra
- 3:37:47information that we send along with our
- 3:37:50request. Okay. So this will be our extra
- 3:37:52information. For example, let's say what
- 3:37:53is our browser type. What is our token
- 3:37:56authorization token? What is our content
- 3:37:58type? Whether we are sending JSON
- 3:38:00content or we are sending text normal
- 3:38:03text. All these things you're going to
- 3:38:04send in your uh request headers. Okay.
- 3:38:07And now once this is done then we have
- 3:38:10something called our response. So let's
- 3:38:11say I'm just going to do response. And
- 3:38:13you can see that we're going to have
- 3:38:14response dot status code. If you do
- 3:38:16response dot right, you're going to get
- 3:38:18lot of options here, right? You can see
- 3:38:20that. So do something like request dot
- 3:38:23send date, set header, all of these. So
- 3:38:25for now, let's do request, sorry,
- 3:38:27response status code. And I'm just going
- 3:38:29to do this one as 200. What this is
- 3:38:32going to do? This is going to set your
- 3:38:34HTTP status code. Okay, here 200 means
- 3:38:39status sorry uh request is successful or
- 3:38:42status is successful. Now here you can
- 3:38:45have so many for example 201 when you're
- 3:38:47going to create something you're going
- 3:38:49to have 400 you'll have 500 you have 429
- 3:38:52like there are so many okay you're going
- 3:38:54to have 401 so all these now each and
- 3:38:57every status code we use in a certain
- 3:39:00position or certain situation to
- 3:39:02represent something so then you are
- 3:39:05going to have one more important which
- 3:39:06is nothing but header so let's say I'm
- 3:39:09adding some header here so in this case
- 3:39:11I'm going to add a content type so what
- 3:39:14actually the content that I'm sending.
- 3:39:16So in this case I have to mention that
- 3:39:18okay whether I'm sending a JSON format
- 3:39:21we are sending in a text format all
- 3:39:23these. So for example let's say I wanted
- 3:39:24to send only text. So here this set
- 3:39:28header what this is going to tell this
- 3:39:29is going to tell your client that what
- 3:39:31kind of response we are sending here it
- 3:39:34is normal text. Now one thing you have
- 3:39:36to understand okay that if you don't add
- 3:39:38this particular header the browser okay
- 3:39:42still it can show the response but here
- 3:39:45setting it always help because for
- 3:39:46example let's say we are sending a JSON
- 3:39:48so I have to mention that content type
- 3:39:49will be application/json
- 3:39:51something like this now what I need to
- 3:39:53do right I have to whatever this server
- 3:39:55that I created I have to end this
- 3:39:58particular request so how I'm going to
- 3:40:00do that or not request I'm going to tell
- 3:40:03we have to finish this particular
- 3:40:05response response and to finish this
- 3:40:06what we have to do right we have
- 3:40:07something called response dot end what
- 3:40:10this response do end is going to do this
- 3:40:12is going to finish this particular
- 3:40:14response now this is actually very very
- 3:40:16important because why because every uh
- 3:40:18request we are going to have at the end
- 3:40:20you are going to have some kind of
- 3:40:21response correct that is the reason
- 3:40:23you're creating the request what will
- 3:40:25happen if you don't add this on this
- 3:40:26particular thing if you don't add right
- 3:40:28the browser or the client it's going to
- 3:40:30wait because you it will think that okay
- 3:40:32you are sending something but if you're
- 3:40:34not sending it's going to do it. So now
- 3:40:36for example, let's say maybe let's let's
- 3:40:38do like this. I'm just going to do basic
- 3:40:41HTTP node server.
- 3:40:44Okay. And maybe let's render the method.
- 3:40:47So I'm just going to render the method
- 3:40:49here. Let's render the URL also. Let's
- 3:40:52render the maybe user agent.
- 3:40:57I think these three is fine. Okay. So
- 3:40:58save this. So now is this completed? No.
- 3:41:02Why? Because when we will start a
- 3:41:06particular request or we are going to
- 3:41:08get any response back. So in that case
- 3:41:11uh what we have to do we have to listen
- 3:41:13that server should listen to a certain
- 3:41:15port. Okay. So server.list we have a
- 3:41:18method that is going to do it's going to
- 3:41:20start the actual server. Okay. So first
- 3:41:23what we have to do right? So we have to
- 3:41:25do server and this is going to give you
- 3:41:27this listen. We are going to pass the
- 3:41:28port. Okay. You listen to this 30,000
- 3:41:30port. Now what will happen right? before
- 3:41:33listen this server exist but this server
- 3:41:36exist in your memory okay but it's not
- 3:41:38going to accept any request why because
- 3:41:40this is not running once you add this
- 3:41:43listen right what is going to do right
- 3:41:45so node is going to process this and
- 3:41:47it's going to wait for that HTTP request
- 3:41:49so whenever you are going to do this
- 3:41:51request it's going to run this
- 3:41:52particular method and it's going to do
- 3:41:54its job whatever it's going to do so now
- 3:41:56in this case this is going to get a call
- 3:41:59back and here what you can do right uh
- 3:42:02So I'm just going to do here log and
- 3:42:04maybe let's do server
- 3:42:07is now running on port and let's give
- 3:42:12this one port here right so I'm just
- 3:42:13going to pass a port
- 3:42:18let's say maybe let's change this one
- 3:42:20for because I already have so many
- 3:42:22projects that I have created right maybe
- 3:42:23something some other things are running
- 3:42:25so let's keep 5,000 only for now so
- 3:42:27let's save this so now what we going to
- 3:42:30do right we will just add one script in
- 3:42:33our package dojson and let's go here.
- 3:42:36Let's remove this.
- 3:42:39So I'm just going to do 01.
- 3:42:43So this will be tsx. So you go to
- 3:42:45source/
- 3:42:47and you have this, right? So I'm just
- 3:42:48going to rename copy this and then you
- 3:42:52run it. Paste it here. That's it. So
- 3:42:54save this. So now what you can do? So
- 3:42:56just to check this one. So I'm just
- 3:42:57going to do npm run and then you can do
- 3:43:0001 because why? Because we've given this
- 3:43:02one as 01 right single script this one.
- 3:43:05So now let's run this. You can see that
- 3:43:08server is now running on port 5,000. So
- 3:43:11that means this is actually working
- 3:43:13fine. So now what we can do right so
- 3:43:15let's go here and maybe let's do local
- 3:43:18host and I'm just going to do 5,000
- 3:43:20because we're running on 5,000. What
- 3:43:22you're getting? We're getting basic HTTP
- 3:43:23node server. This is get request right?
- 3:43:26you're getting your user agent right and
- 3:43:28all the details that whatever you have
- 3:43:29added here. Okay. So now I think this is
- 3:43:32completed. So first of all you
- 3:43:34understand some of the things right that
- 3:43:36what actually create server method is
- 3:43:38what is request object response object.
- 3:43:41Okay. And then how you are going to set
- 3:43:43headers and all the things. Now remember
- 3:43:45this status code and everything when
- 3:43:47we'll start our actual project
- 3:43:48implementation this will be easier
- 3:43:50because whatever you're seeing all these
- 3:43:51status code we are going to anyways use
- 3:43:53in our NodeJS project.
- 3:43:55So with this now let's move on to the
- 3:43:57next concept. Okay. So now let's move on
- 3:43:59to the next one. So next one let's
- 3:44:01understand some of the routing concepts.
- 3:44:03Okay. So I'll do routing. DS. So first
- 3:44:06of all let's do one thing. Okay. Let's
- 3:44:08close this one. Okay. Clear this.
- 3:44:13So first uh let's do one thing. Let's
- 3:44:15import this because we need this. We
- 3:44:18have already discussed this one. We
- 3:44:19don't have to again uh discuss. So let's
- 3:44:21take the port as 5,01. this time and now
- 3:44:26what we will do right so let's create
- 3:44:27the server first now if you see that how
- 3:44:30I've actually created see remember
- 3:44:32whatever video that you are seeing right
- 3:44:33this particular course
- 3:44:36you don't have any idea how much time I
- 3:44:38actually spent while creating this video
- 3:44:40the point is not about the video
- 3:44:42recording or the editing the point is
- 3:44:44how I'm going to structure uh this
- 3:44:46particular curriculum that is the most
- 3:44:48important part structuring is very very
- 3:44:50important means this particular concept
- 3:44:52should come after that concept And that
- 3:44:54is where the main teaching will come.
- 3:44:56You understand what I'm talking about?
- 3:44:58For example, let's say in the previous
- 3:44:59section we have already discussed that
- 3:45:01how you can create uh server using HTTP
- 3:45:04create server. Now when in the next part
- 3:45:06I'm going to use the same concept. I
- 3:45:08don't have to explain because from the
- 3:45:10previous section you will be able to
- 3:45:12apply that particular concept in this
- 3:45:14one. Okay. And you'll notice this
- 3:45:16particular pattern throughout this uh
- 3:45:18video. Okay. Till the end. So let's do
- 3:45:20one. So we'll do http.create server. So
- 3:45:24this is going to give us a call back. So
- 3:45:25I'm just going to do request which will
- 3:45:27be my incoming message. Then I'm going
- 3:45:29to have the response which will be my
- 3:45:30server response. Okay. And [snorts] here
- 3:45:32first of all what we are going to do
- 3:45:34right. So let's take our method. So I'm
- 3:45:36just going to take the method and this
- 3:45:38method will be so I'm going to take
- 3:45:39either it will be the request method or
- 3:45:42by default I'm going to take this one as
- 3:45:43get. So this will be by default it will
- 3:45:46be get method. Okay. So now what we are
- 3:45:50going to do right? So first you have to
- 3:45:52remember one thing that what this uh
- 3:45:54okay let's first do one thing that I'll
- 3:45:57do request URL and we already discussed
- 3:46:00that we have something called request
- 3:46:01URL right but what this request do URL
- 3:46:04actually contains now this contains the
- 3:46:06path that requested by the client okay
- 3:46:10so let's say if I'm doing like this so I
- 3:46:13have http and then I will do local host
- 3:46:16and then I'll do 3,00 okay/ users
- 3:46:20something like this. So I'm taking 3,000
- 3:46:22only. That's fine. But let's take 5,0001
- 3:46:24because we're going to run this. In this
- 3:46:26case, what will be your request URL?
- 3:46:29Your request URL will be / users. Okay,
- 3:46:33only this part. Similarly, let's say if
- 3:46:35I just do here / user and then here I'm
- 3:46:38just going to take only for the user ID
- 3:46:40one. So in that case, this will be only
- 3:46:43of this user ID equal to one. Okay. So
- 3:46:46request URL only this part. Okay. So now
- 3:46:49when we are going to implement routing
- 3:46:51or we understand routing we usually want
- 3:46:54only the path part right only the path
- 3:46:57this part okay so now how we are going
- 3:46:59to get this so in that case what we have
- 3:47:01to do right we are going to convert this
- 3:47:03request dot URL into a URL object and
- 3:47:06then we are going to read the path name
- 3:47:08so first we have to convert so how we
- 3:47:10are going to convert so what we have to
- 3:47:13do right let's cut it so we have
- 3:47:14something called new URL okay and this
- 3:47:17is is going to give So you can pass this
- 3:47:19ukulele or this will be
- 3:47:23by default it will be like this and then
- 3:47:26what we going to pass uh pass right. So
- 3:47:28let's take this one as http and here I'm
- 3:47:31simply going to do uh request dot header
- 3:47:34and then I'm going to get something
- 3:47:36called host. Okay and now what we can do
- 3:47:39right we can simply read the path name.
- 3:47:41So I'm just going to do path name and
- 3:47:43this will be a request url dot you have
- 3:47:45something called path name. So this is
- 3:47:47going to give you the actual path name.
- 3:47:49Okay. So now first of all let's do one
- 3:47:51thing. Let's take rest and then I'm just
- 3:47:54going to do a set header. And for now
- 3:47:55I'm just going to pass the content type
- 3:47:57as text only. So the content type will
- 3:48:00be text/plain.
- 3:48:04And then what we have to do right so now
- 3:48:07what I can do right we can simply check
- 3:48:09that if the current method is get. So
- 3:48:12I'm simply going to check if the method
- 3:48:14method is equal to equal to get and if
- 3:48:17the path name if this path name is equal
- 3:48:20to let's say I'm sending request from
- 3:48:22this path from the health route. So this
- 3:48:24is our route. Okay. So you'll understand
- 3:48:26what I'm talking about. So in that case
- 3:48:28what I'm going to do right I'm just
- 3:48:29going to do raise dot status code will
- 3:48:31become 200. I'm just going to do race
- 3:48:34end. So I have to end it right. We have
- 3:48:36already discussed. So I'm just going to
- 3:48:37do server is now or server is healthy.
- 3:48:41And then I'm simply going to do return.
- 3:48:44Okay. So if the path name is health,
- 3:48:46you're going to return this. Now you'll
- 3:48:47understand one thing that even though if
- 3:48:49you don't have any third party packages,
- 3:48:51sorry not packages, framework like X or
- 3:48:53let's say you're using something else
- 3:48:55still using only node HTTP you will be
- 3:48:57able to implement all of these okay
- 3:49:00without using any framework also. So
- 3:49:01that is the reason you have to
- 3:49:02understand the basic concept. Similar
- 3:49:04way what I can do right copy this and
- 3:49:06then I can paste it here. Okay. the
- 3:49:08method is get and now if the route is
- 3:49:10instead of health if this is users so
- 3:49:12I'm just simply going to pass here list
- 3:49:14of users so I'm just going to do list of
- 3:49:17users and then we are going to return
- 3:49:19similarly you can do for other also for
- 3:49:21example let's say if I copy this and now
- 3:49:23I'm simply going to change this one to
- 3:49:25if this is post not get request okay and
- 3:49:28the path name is users so I'm just going
- 3:49:30to change this one to 201
- 3:49:33we usually we already expl sorry we
- 3:49:35already explained this one right if you
- 3:49:37recollect 20 is 201 is when we are going
- 3:49:41to create something. So that is the
- 3:49:43reason I'm taking this one as method is
- 3:49:45post and in that case I'm simply going
- 3:49:47to do user created successfully
- 3:49:51something like this. Now here um you can
- 3:49:53also take one for delete also. Okay. And
- 3:49:56then what I have to do right now see
- 3:49:58there is a possible that when user is
- 3:50:00going to send one particular request
- 3:50:03let's say user can send any random
- 3:50:06request also to any path for example
- 3:50:08some blah blah. Now this path is not
- 3:50:11present. There is a possibility right
- 3:50:12that this route is not present in your
- 3:50:14back end. So in that case you have to
- 3:50:15inform the user okay this route is not
- 3:50:17found. So to do this what you have to do
- 3:50:19is we're going to pass status code will
- 3:50:21be 404. So 404 means this route is not
- 3:50:24present. 404 means not found. So
- 3:50:27something is not found not found here.
- 3:50:31Right? And then in that case you're
- 3:50:32simply going to do race.
- 3:50:35And I'm simply going to do route not
- 3:50:38found. Okay that's it. So now what we
- 3:50:41going to do? Let's go here and then
- 3:50:43we're going to copy this and I'm simply
- 3:50:45going to paste it here. save this. So
- 3:50:48let's go to package dojson, copy,
- 3:50:52paste and then we are going to simply
- 3:50:56copy this
- 3:50:59paste here and change this one to two.
- 3:51:02So save this and now what we going to do
- 3:51:04right? So let's run it. So npm run 02.
- 3:51:07Now you'll see that this is now running
- 3:51:09on 5001. So now what I can do right
- 3:51:11let's go here and then I'm going to
- 3:51:13create new request and then here I'm
- 3:51:16simply going to do what right so let's
- 3:51:18copy this from here
- 3:51:21and then I'm simply going to do 501 and
- 3:51:23then I'm going to pass this one as
- 3:51:25health okay see what will happen so you
- 3:51:26can see that you are getting server is
- 3:51:28now healthy now the reason you are
- 3:51:30getting because it's now matching here
- 3:51:32why because what we have done right this
- 3:51:34request URL is now going to give you /
- 3:51:36health because this is the path name
- 3:51:39right this is is the path name that
- 3:51:40you're going to get and if the path name
- 3:51:42is matching so in that case you're going
- 3:51:43to get server is healthy if you're
- 3:51:45passing this one as users so you're
- 3:51:48going to get that okay list of users
- 3:51:50okay if you're passing what what else so
- 3:51:52if I change this one to post and then if
- 3:51:54I send this one user created
- 3:51:56successfully but in case if I just
- 3:51:58change this one to get and if I give a
- 3:52:00random route which is not present so
- 3:52:02I'll do random route not present
- 3:52:07present
- 3:52:09here and then if you send this right
- 3:52:11you're going to get route not found and
- 3:52:12you're going to get a 404 you're getting
- 3:52:15all of this. So this is where it is very
- 3:52:17important you have to understand couple
- 3:52:18of things here first of all what
- 3:52:19actually method is what is path name
- 3:52:22right how you're going to get the path
- 3:52:24name what is status code what actually
- 3:52:27response you are sending and also that
- 3:52:29you also understand one very important
- 3:52:31point and that is if we don't have any
- 3:52:33route that is not present so in that
- 3:52:34case we pass this one as 404 okay why
- 3:52:37this status code are extremely important
- 3:52:39because when you're going to implement
- 3:52:40actual code right let's a production
- 3:52:42level application you're going to build
- 3:52:44you need to know which status code you
- 3:52:46need to use in which situation and that
- 3:52:49is very important. Okay. So I think with
- 3:52:51this this is now completed uh routing
- 3:52:53section or concept. Now let's move on to
- 3:52:55the next one. Okay. So moving to the
- 3:52:56next one. So let's understand that how
- 3:52:59we can read our request body in plain
- 3:53:01NodeJS because anyways in the next
- 3:53:03section we are going to introduce
- 3:53:05express. So you're going to understand
- 3:53:06all these concept in better way but
- 3:53:08still you have to understand that how
- 3:53:09you can uh do the same thing or do the
- 3:53:13same implementation without using any
- 3:53:16third party like uh frameworks. Okay. So
- 3:53:18first I'm just going to give 03. Let's
- 3:53:20give this one as maybe reading uh body
- 3:53:24ts. So first let's close these two.
- 3:53:28And here also let's do one thing. Let's
- 3:53:31rename this. Copy this. And then let's
- 3:53:34add one more here. Okay. So I'll do 03
- 3:53:39and this will be tsx
- 3:53:42source/ this. Save this. Okay. So first
- 3:53:46of all we need to copy this tool
- 3:53:49from here. I'll copy this.
- 3:53:52Let's keep this on 5002 so that you'll
- 3:53:55understand what is happening also. Let's
- 3:53:56close this. And here first we are going
- 3:53:59to do so we'll create our server.
- 3:54:03So this will be http do.create server
- 3:54:07and here we're going to have the call
- 3:54:08back. So request will be your incoming
- 3:54:10message. Response will be your server
- 3:54:13response.
- 3:54:15And here first of all we going to get
- 3:54:16couple of things. So first what we need
- 3:54:18to get right. So we going to get our
- 3:54:20method. So let's go here. So we'll get
- 3:54:23our method which will be request dot
- 3:54:25method. or else let's keep this one as
- 3:54:27get. Next we are going to get our path
- 3:54:30name. I think this part we've already
- 3:54:32discussed. So let's copy this from here
- 3:54:34and then we'll paste it. So now also
- 3:54:37let's set the uh header as pl uh text
- 3:54:41only. Save this. So now what I going to
- 3:54:43do right? So first of all I'm simply
- 3:54:45going to do if method is equal to get
- 3:54:50and then what you're going to do what
- 3:54:52else? Maybe let's do one thing for get I
- 3:54:54think we've already discussed. So let's
- 3:54:56do this one as post only. This is where
- 3:54:58we are going to do something. So here
- 3:54:59what I'll do I'm simply going to check
- 3:55:01if the path name is equal to equal to
- 3:55:04users then we are going to do something.
- 3:55:06Okay. So now this particular post is now
- 3:55:10a post request. Okay. This particular
- 3:55:12path not post this particular path is
- 3:55:14now a post request. Post means this is
- 3:55:17going to expect some kind of JSON
- 3:55:18request body. Okay. So for example let's
- 3:55:21say when you are sending something from
- 3:55:23your UI you are going to send your name
- 3:55:25and email something like that. So in
- 3:55:27that case what I'll do right is I'm
- 3:55:28going to create a type and this give
- 3:55:31this one as maybe let's say create user
- 3:55:33body that I'm actually creating some uh
- 3:55:36user. So where I'm going to keep the
- 3:55:38name as string and also I'm going to
- 3:55:40pass email which will be string these
- 3:55:42two things. So now what I'm going to do
- 3:55:45right so first of all this particular
- 3:55:47data that I'm going to send from my UI
- 3:55:49side I need to read it in my back end.
- 3:55:51Okay. So for now what I can do right so
- 3:55:53here we can make use of chunks. So here
- 3:55:56first of all I'm going to take chunks
- 3:55:58and these will be buffer and here what
- 3:56:00I'm going to do let's take this one as
- 3:56:02empty for now. What we are simply doing
- 3:56:04right we're actually storing our
- 3:56:05incoming chunks here. Now what these
- 3:56:08chunks will be each and every chunk that
- 3:56:09we are going to store that will be part
- 3:56:12of your request body. Okay. Now what we
- 3:56:16going to do right? So first of all let's
- 3:56:17do one thing. Let's take a request and
- 3:56:19here we are going to take a on uh
- 3:56:21listener. Now what we going to do right
- 3:56:23here we have a method. Oh sorry we have
- 3:56:25a event called data. What this data
- 3:56:28event is going to do? This data event is
- 3:56:31going to run every time node is going to
- 3:56:34receive your body chunk or new body
- 3:56:37chunk. Let me write it or else it will
- 3:56:38be difficult. So this data event is
- 3:56:42going to run is going to run every time
- 3:56:47node is sorry node receives
- 3:56:50a new body chunk. So this event is going
- 3:56:54to be triggered. Okay.
- 3:56:57when you're going to send small JSON
- 3:56:59body. So maybe this will run only once.
- 3:57:02If you're sending larger body, this
- 3:57:04might run multiple times. So now what we
- 3:57:07are going to do right? So this is going
- 3:57:08to receive chunk and chunk type will be
- 3:57:12buffer. Okay. So now what I going to do
- 3:57:14right? So first I'm going to take the
- 3:57:16chunks that we have and then I'm going
- 3:57:18to post this chunk that I'm receiving
- 3:57:21here. So I'm just posting this one to
- 3:57:23this chunk array. Okay. Now what are we
- 3:57:26going to do? We going to take a end uh
- 3:57:28here. So I'm just going to do request
- 3:57:30dot end sorry request dot on and this
- 3:57:34will be my end uh event. What this end
- 3:57:37event is going to do. So this is going
- 3:57:39to run when very very important you can
- 3:57:41note it down. This end event is going to
- 3:57:44run when the full request body arrived.
- 3:57:47Means node already received the complete
- 3:57:49request body. Okay. Now what will be the
- 3:57:52proper way to frame this? All right
- 3:57:55everyone. So I just got a phone call. So
- 3:57:56guys so what I was talking about that
- 3:57:58when this particular event is going to
- 3:58:00run that when the node is going to
- 3:58:02receive the complete request body. So
- 3:58:04this the proper way to frame this one
- 3:58:06will be that combining [snorts] all the
- 3:58:08chunks. Okay. So what this is going to
- 3:58:10happen right until unless this
- 3:58:12particular event will run completely
- 3:58:15node will not have the complete request
- 3:58:17body you're getting. So once it will
- 3:58:19have the complete or full request body
- 3:58:22that time this is going to run this
- 3:58:24particular event. So here what we are
- 3:58:25going to do right. So let's take the
- 3:58:28call back. So first of all I'm going to
- 3:58:29get the raw body and this will be so
- 3:58:32first of all I'm going to take buffer
- 3:58:34dot contact here we're going to pass the
- 3:58:37chunks because are going to receive all
- 3:58:39the chunks because this is going to push
- 3:58:41all the every chunk to this array and
- 3:58:44then I'm simply going to do a string. So
- 3:58:46let's do two string and let's pass a
- 3:58:48UTF8.
- 3:58:50Okay. So now what this buffer do concat
- 3:58:54is going to do? This is going to combine
- 3:58:55all the received chunks into one buffer.
- 3:58:59Okay. I hope this is clear. Now first of
- 3:59:01all let's give a check that if this body
- 3:59:04is not received because there is a
- 3:59:05possibility that user haven't sent
- 3:59:07anything. So in that case what you're
- 3:59:09going to do? So I'm just simply going to
- 3:59:10do race dot status code will now become
- 3:59:12400 because there is a error and message
- 3:59:14we're going to send that okay your
- 3:59:16request body is required or empty and
- 3:59:20then we are going to just do return okay
- 3:59:23and now what we going to do right we're
- 3:59:25going to actually parse this so I'm just
- 3:59:27going to do body and this will be JSON
- 3:59:29dotparse and here we're going to pass
- 3:59:31your raw body and now what will be the
- 3:59:34type now this is very important okay so
- 3:59:36the type will be create user body why
- 3:59:39Because this is the format that you are
- 3:59:40actually expecting. Consider if the user
- 3:59:43is not sending name and email. So in
- 3:59:45that case let's say it's sending instead
- 3:59:47of name it's sending address or instead
- 3:59:49of email it sending age that is a
- 3:59:51possibility right. So we need to
- 3:59:52properly typed the body. So the body
- 3:59:56format will be this. So in that case now
- 3:59:58I can uh clearly check this. Okay. The
- 4:00:00body name is not present or if the body
- 4:00:04do email is also not present. So in that
- 4:00:06case I'm simply going to do race status
- 4:00:08code will be what 400 because 400 is we
- 4:00:11are using for our error okay not emit
- 4:00:14man this will be end. So in that case
- 4:00:16I'm simply going to do both name
- 4:00:19and email is required and then I'm
- 4:00:24simply going to do return. So now in
- 4:00:28this point if you notice in this
- 4:00:30particular point what will happen right
- 4:00:32everything is actually done. So we
- 4:00:33already check all the age cases. Okay.
- 4:00:36So what I mean by H cases because this
- 4:00:37is very important when you are working
- 4:00:39with Okay. One more thing I think we
- 4:00:42missed it. So let's quickly do this one
- 4:00:45after this right. Let's take a try catch
- 4:00:47block. So that is also good practice. So
- 4:00:49put all of this inside a try. Okay. So
- 4:00:52here what I was talking about that when
- 4:00:54you're actually implementing any API
- 4:00:56right the first thing what you have to
- 4:00:59consider that when you're doing the
- 4:01:00planning that whether you are covering
- 4:01:02all the age cases or not because
- 4:01:03covering age cases is very important
- 4:01:05what I'm talking about age cases for
- 4:01:06example this is one age case right age
- 4:01:09case means the error can happen so you
- 4:01:11are actually preventing the error
- 4:01:12similarly this is another age case here
- 4:01:15okay so now but this is the try and here
- 4:01:18I'm just going to take a catch now in
- 4:01:20the catch now This is not yet completed.
- 4:01:22try you need to do so in the catch I'm
- 4:01:24simply going to do rest status code is
- 4:01:26maybe 400 and let's send rest dot end
- 4:01:30and I'm simply going to do invalid
- 4:01:32JSON body something like this and now
- 4:01:35once here once you check all of this
- 4:01:37right what you can do right now
- 4:01:40if there is no error so in that case you
- 4:01:42simply do
- 4:01:44raise dot status code is 201 and then
- 4:01:46you're going to do rest dot end will be
- 4:01:50your user is now created now at the And
- 4:01:52when you're creating actual
- 4:01:53implementation what you're going to do
- 4:01:55right you are going to store all this in
- 4:01:57your DB but for now we'll do body name
- 4:02:00and your email is body email. Okay
- 4:02:04that's it. So this is done. So now so
- 4:02:07this will be your race end event. Here
- 4:02:10you can also take one more. So I'll do
- 4:02:13request do on and here I'm just going to
- 4:02:15pass in case if there is any error
- 4:02:16happen. So in that case what you're
- 4:02:18going to do? So we're going to pass a
- 4:02:20raise dot status code will be 500
- 4:02:23because this can be a any error right?
- 4:02:25So, it's sending a 500 and I'll do raise
- 4:02:27end and I'll simply do fail to
- 4:02:31create
- 4:02:33u uh request body or something or maybe
- 4:02:36let's do this fail to read request body
- 4:02:40read request
- 4:02:43body okay so I think this is done and
- 4:02:46then what we going to do right we are
- 4:02:48going to return
- 4:02:51that's it so this will be if the method
- 4:02:54is post and the path name is users. So
- 4:02:56in that case we're going to do all of
- 4:02:58this. If this is not the case in that
- 4:03:00case what I'll do do raise status code
- 4:03:02will be 404 because you are maybe
- 4:03:04sending to a route which doesn't exist.
- 4:03:07And here also I'm just going to do raise
- 4:03:09end will be route not found. So this is
- 4:03:14done and now what you're going to do
- 4:03:16let's copy this one from here.
- 4:03:20Save this and this is done. Now I hope
- 4:03:22that this part is clear right. So first
- 4:03:24what we doing we are taking a chunks
- 4:03:26then we are taking a event this will be
- 4:03:30the data event listener. So this is
- 4:03:31going to run every time node is going to
- 4:03:33receive the body chunk. We are going to
- 4:03:35push that chunk into our chunks in our
- 4:03:37end. First we are going to read the body
- 4:03:40raw body from. So first what this is
- 4:03:43going to do this is going to I already
- 4:03:45mentioned right what buffer uh concat is
- 4:03:48going to do. So buffer docon is going to
- 4:03:50combine all the receipt chunks into one
- 4:03:52buffer. Now this is very important.
- 4:03:54Okay. And then you're simply checking
- 4:03:56two edge cases. If this is not present
- 4:03:58that means you are not sending your
- 4:03:59request body then you're parsing the
- 4:04:01body then you're telling if the body
- 4:04:03don't have the name and email because
- 4:04:05this is the format or data format we
- 4:04:07need right if this is the case then also
- 4:04:09returning one error and then we are
- 4:04:11sending request dot status code 201
- 4:04:13means this is now successful okay there
- 4:04:15is no error so now what I can do right
- 4:04:18so let's go here clear this and I'm
- 4:04:21simply going to do npm run 03 this is
- 4:04:24now running on 5002 So let's go here.
- 4:04:275002
- 4:04:32okay/ users change this one to get
- 4:04:35because see if I change this one right
- 4:04:36now route not found because this is not
- 4:04:38get so this will be a post try to send
- 4:04:41request body is request fantastic so
- 4:04:43that means what happened so this
- 4:04:45particular part is running because
- 4:04:46you're not sending anything now let's go
- 4:04:48where you have to go to body let's go to
- 4:04:51raw and we have a JSON okay so in that
- 4:04:53case what you're going to do right so
- 4:04:55first I'm just going to send the name.
- 4:04:57So name will be sungum comma and I'm
- 4:05:00just going to say email and email will
- 4:05:03be sung at thegmail.com.
- 4:05:05Okay. So now if I send this user created
- 4:05:09with this and try to give a different
- 4:05:11property. So I'll do name to email one.
- 4:05:13And if you send this you can see that
- 4:05:14both name name and email is required
- 4:05:16because this is not the expected data
- 4:05:18that we need. We need name and email
- 4:05:21only here. So if you do John and let's
- 4:05:24do here John 1gmail.com send this. So
- 4:05:27now user is created. So that means we
- 4:05:30successfully actually read our request
- 4:05:32body without using any third party
- 4:05:34framework. Okay. So this is you are
- 4:05:36using pure node. Okay. So we are not
- 4:05:38using anything rather than node concept.
- 4:05:40This is very very important to
- 4:05:41understand. You see that how things are
- 4:05:43happening behind the scenes. Okay. So
- 4:05:45with this I think this part is
- 4:05:47completed. Now let's move on to the next
- 4:05:48one. Okay. So let's move to the next one
- 4:05:50which will be how you can work with JSON
- 4:05:52responses right because when you're
- 4:05:54creating actual project you will see
- 4:05:55that most of the times you're going to
- 4:05:56send the JSON responses okay back to
- 4:05:59your client. So here I'm simply going to
- 4:06:01do JSON and let's do response. DS. Okay.
- 4:06:05So first of all what I going to do right
- 4:06:07okay let's create the so let's copy this
- 4:06:10from here and let's copy this also paste
- 4:06:13it here and change this one to 5003 so
- 4:06:15that we can understand what is
- 4:06:16happening. We can keep the same code
- 4:06:18also that is also fine. So first I'll do
- 4:06:21http create server. So I think this part
- 4:06:24we have already covered multiple times.
- 4:06:26We have incoming request response will
- 4:06:27be server response and then we are going
- 4:06:30to have our call back. So first of all
- 4:06:31what we going to get? So we're going to
- 4:06:33get three things. We're going to get the
- 4:06:35method. We are going to get the request
- 4:06:36URL and the path name. So copy these
- 4:06:38three because at the end we have to
- 4:06:40check the path name. Right? So first of
- 4:06:42all what we going to do right? So here
- 4:06:44now we are going to create a helper.
- 4:06:46Okay. And this helper will be uh let's
- 4:06:48create this one as send JSON. Okay. Now
- 4:06:53first of all let's do one thing. I cop
- 4:06:55uh I have created some type. Okay. You
- 4:06:57understand what is happening here. So
- 4:06:59first let's go ahead and paste this. So
- 4:07:01first you can see that we have a user
- 4:07:02type, user as ID, name and email. This
- 4:07:06is our API response. So this is the for
- 4:07:07API response format that we want to send
- 4:07:10back to our client. So we want to return
- 4:07:12that if the response is or the request
- 4:07:15is successful. So we're going to return
- 4:07:17success true or false. We also want to
- 4:07:19return a message so that user will
- 4:07:22understand. Okay, for example, let's say
- 4:07:23from the UI side, you are submitting one
- 4:07:26form, right? Or let's say you're
- 4:07:27creating one product data or let's say
- 4:07:29you're creating uh maybe okay what to
- 4:07:32think so let's say you're creating some
- 4:07:34uh admin creating some user information
- 4:07:37okay so in that case once the that
- 4:07:40request is successful so that admin or
- 4:07:43the end user should understand whether
- 4:07:45that is successful or not right so in
- 4:07:47that case what we usually do we'll
- 4:07:49render some kind of message in the toast
- 4:07:51or let's say in a dialogue so that they
- 4:07:53will understand okay user is now created
- 4:07:55successfully why Why I'm repeating all
- 4:07:56of this multiple times? Because this is
- 4:07:58the format we are going to use in our
- 4:08:00next project also. And that time I'm not
- 4:08:01going to repeat the same thing again and
- 4:08:03again and success will be true or false.
- 4:08:05What is the data? Data is the actual
- 4:08:07data that I want to send to our client.
- 4:08:10Okay. And in case there is any error. So
- 4:08:12in that case I'm going to return the
- 4:08:14error. So that is the reason you can see
- 4:08:15that both are optional. So now what I'm
- 4:08:17going to do is I'm going to create a
- 4:08:19send JSON. Let's take a generic here.
- 4:08:21And in this one what I'm going to do? So
- 4:08:23first of all my response will be my
- 4:08:25server response. Then I'm also going to
- 4:08:27take a status code
- 4:08:30and this status code will be number
- 4:08:33and then I'm going to take a body
- 4:08:37and this will be my API response
- 4:08:41this one and you're going to pass this t
- 4:08:44here. Okay, that's it. And let's return
- 4:08:47here void. So first of all what we have
- 4:08:50to do? So first we have to set the
- 4:08:51status code. So first I'll do res status
- 4:08:54code will be the actual status code we
- 4:08:56are receiving here because when we going
- 4:08:58to call this helper the status code can
- 4:09:01be 200 it can be 201 it can be 400 it
- 4:09:04can be 401 it can be 500 also so we
- 4:09:06don't know what actually the status code
- 4:09:08is we have to set it here what this
- 4:09:10status code is going to tell so status
- 4:09:13code is going to tell the client that
- 4:09:14what actually happened for example 200
- 4:09:16means it's successful 201 means
- 4:09:18something we have created that is
- 4:09:20successful 400 Means some error happened
- 4:09:23and that error happened from the client
- 4:09:25side means client sends some invalid
- 4:09:26data then only 400 error we are getting.
- 4:09:29For example, let's say in that form we
- 4:09:31you uh ad sorry not admin the client
- 4:09:34needs to send name age and email but
- 4:09:37client only send name email but client
- 4:09:40didn't send age. So in that case we are
- 4:09:42going to render that okay age you also
- 4:09:44have to send age is a mandatory field.
- 4:09:46Then let's say you're getting 404. So in
- 4:09:48that case route not found 500 500 means
- 4:09:51some internal server header uh error
- 4:09:54happened. So all these things this rest
- 4:09:56do status code is going to mention and
- 4:09:58then what we going to do right I'm
- 4:09:59simply going to do r dot set header. Now
- 4:10:02this set header will now be content type
- 4:10:06extremely important concept because this
- 4:10:08is what we are going to use and this
- 4:10:10will now be not plain text this will be
- 4:10:13a JSON data. So in this case we are
- 4:10:16simply telling that okay content type is
- 4:10:18going to tell the client that how we are
- 4:10:19going to understand your request body.
- 4:10:21So in this case we are simply telling
- 4:10:22that your request body will be JSON so
- 4:10:25that we can parse it. So this is done
- 4:10:27and then what we going to do right I'm
- 4:10:29just going to do rest dot not same man
- 4:10:31rest. And I'm just going to do JSON dot
- 4:10:34stringify.
- 4:10:36This popup is extremely irritating. I
- 4:10:39don't like this. Okay. So this one you
- 4:10:41see and this will be body. Okay. So we
- 4:10:45want to send this uh in the stringify
- 4:10:48format JSON string format. So this will
- 4:10:51be my send JSON helper. Okay. So now
- 4:10:53what we can do we can use this in every
- 4:10:55place. How? So now let's say if we go
- 4:10:57here that okay now if the method is
- 4:10:59equal toal to get
- 4:11:02and the path name is equal to home home
- 4:11:06path. So in that case I'm simply going
- 4:11:08to call the send JSON and here I'm going
- 4:11:11to pass what response. So you to pass
- 4:11:13this response then you're going to pass
- 4:11:15what? You have to pass the status code.
- 4:11:18So in this case this will be 200 and
- 4:11:20then you also have to pass the body.
- 4:11:22Body will be actual data. So in that
- 4:11:24case let's I'm going to pass a success
- 4:11:26which will be true. I wanted to pass a
- 4:11:28message. This will be let's say server
- 4:11:30is running something like this. And then
- 4:11:33we are going to pass a data and this
- 4:11:35data will be let's say any data maybe
- 4:11:37let's say we are sending here routes and
- 4:11:41then maybe I'm just going to do this one
- 4:11:43as get/
- 4:11:45user something like this okay so now
- 4:11:47let's save this so it's simply calling
- 4:11:49this and now this is going to send your
- 4:11:51JSON response back okay so this is one
- 4:11:53so now what you're going to do right
- 4:11:55let's say I wanted to get this user
- 4:11:57information also so in that case what I
- 4:11:59can do I can create one more here so I'm
- 4:12:01just going to do if
- 4:12:03method is equal to get and if the path
- 4:12:08name is equal to / users. So in that
- 4:12:11case what I'm going to do is I'll copy
- 4:12:13this from here
- 4:12:16paste it. So uh this will be same and
- 4:12:19here I'm going to pass message and I
- 4:12:21will do user
- 4:12:23fed successfully and in the data I'm
- 4:12:27going to pass the users.
- 4:12:29this user is nothing but this data. This
- 4:12:33data we going to return. Okay. So now
- 4:12:36let's first of all let's check this and
- 4:12:38also at the end what I have to do right
- 4:12:41here after this I'm simply going to take
- 4:12:43another send JSON and I'm simply going
- 4:12:46to pass this one as null and when we are
- 4:12:49sending any request which route is not
- 4:12:52present. So we're going to pass a 404
- 4:12:54and in this case we're going to pass a
- 4:12:55success will be false. Message will be
- 4:12:58route not found
- 4:13:03and also you're going to do a error and
- 4:13:05this will be let's say this method
- 4:13:10and then you're going to pass this path
- 4:13:11name
- 4:13:13is not exist and then we'll go here and
- 4:13:17then just run this. So copy this,
- 4:13:21paste it, save,
- 4:13:24and then we'll just add it here. So I'm
- 4:13:27going to add one more. So this will be
- 4:13:2804. And this will be tsx source/ this.
- 4:13:33So save this.
- 4:13:35And now close this, clear it. And now
- 4:13:39this will be 04. So server is now
- 4:13:41running. So now let's go here. And I'm
- 4:13:43simply going to do 5003.
- 4:13:46So this will be get. And let's see first
- 4:13:48of all which uh what we are using here
- 4:13:50right. So we have this uh slash this
- 4:13:53path. So in this case we should get
- 4:13:55server is now running. So send this. So
- 4:13:58we're getting true message. We are
- 4:13:59getting the data. And now let's do this
- 4:14:02on / users. So in this case we should
- 4:14:04get the user here. We're getting 500
- 4:14:08error. Something we are doing wrong
- 4:14:10here.
- 4:14:14Oh we have to return from here man. we
- 4:14:16have to return. Okay,
- 4:14:18this is what we have to do. So let's
- 4:14:20save this.
- 4:14:22Clear it and run once again. And now
- 4:14:24let's try to send and you can see that
- 4:14:26you're getting the data here. So you're
- 4:14:28getting success to user is now fed
- 4:14:30successfully and you're getting the
- 4:14:32data. Okay. So now this is how you have
- 4:14:34to work with JSON. This is what we are
- 4:14:36going to use because once we start
- 4:14:38express this will make our life much
- 4:14:40easier because express will give some of
- 4:14:42the out ofthe-box features that we don't
- 4:14:43have to implement manually right. So
- 4:14:45with these four very very important uh
- 4:14:48topic that we have understood first of
- 4:14:51all basic HTTP server KSN routing
- 4:14:53concept reading body and then how you
- 4:14:57can work with JSON responses. So with
- 4:14:58this now let's move to the next one
- 4:14:59which will be our last concept for this
- 4:15:01particular uh section which will be how
- 4:15:04you can call external APIs and what is
- 4:15:06the best way to do that. Okay because
- 4:15:09calling external APIs is very simpler
- 4:15:10but you have to do some of the concepts.
- 4:15:13So let's see how we are going to do that
- 4:15:14in the next part. All right. So now
- 4:15:16let's end this particular section with
- 4:15:18another very important concept that how
- 4:15:20you can work with external API calls
- 4:15:22which is also one of the most frequent
- 4:15:25uh functionality that you can do. I not
- 4:15:27most frequent but yeah some of the times
- 4:15:28you are going to do this. Okay. So now
- 4:15:31so one of the most practical example
- 4:15:33will be let's say you are getting some
- 4:15:35task where you have to do some kind of
- 4:15:37web scrapping. Okay. Now in this case
- 4:15:39what you have to do right? So let's say
- 4:15:40you are going to have some external API
- 4:15:42and in first of all you're going to get
- 4:15:44the content. So you have to call that
- 4:15:46API right? And then let's say whatever
- 4:15:48content you're going to get you have to
- 4:15:49scrap the data something like this. I'm
- 4:15:51just giving a normal example or there
- 4:15:53can be like what we are going to do now.
- 4:15:54So you're going to have normal API call.
- 4:15:56You're going to get the response back
- 4:15:58and then let's say you're going to do
- 4:16:00something with that response and then at
- 4:16:02the end you're going to send the data
- 4:16:03back to your client. Okay. So first of
- 4:16:05all what we are going to do right we
- 4:16:07will be using this JSON placeholder API.
- 4:16:09This is very famous. Okay. And now if
- 4:16:12you notice one thing okay here. So
- 4:16:14notice that what are the format or
- 4:16:16things not format the properties that we
- 4:16:18are receiving here. So we have ID name,
- 4:16:20username, address, phone, website,
- 4:16:22company all these. So first of all what
- 4:16:24we are going to do right so let's say I
- 4:16:26have to actually call the data from this
- 4:16:28particular API only or I have to get the
- 4:16:30data. So what are you going to do right?
- 4:16:32So first I'm going to take this one as
- 4:16:33my API URL. Now one very very important
- 4:16:36concept I'm going to explain. Okay and
- 4:16:38this is going to be a game changanger
- 4:16:40for all of you. I'm not sure if some of
- 4:16:41you actually know this particular
- 4:16:43concept or not. So what I'm trying to do
- 4:16:45right so you notice one thing right when
- 4:16:48you are going to call the external API
- 4:16:50from a API source or from a URL or from
- 4:16:53a source you notice that that particular
- 4:16:56uh is going to give you the data in a
- 4:16:58certain format. So what I mean by this?
- 4:17:00So in this case you have a ID, name,
- 4:17:02username, address and then you're going
- 4:17:04to have street that is inside address.
- 4:17:06Similarly you have company and you see
- 4:17:07that you have name inside this company
- 4:17:10object. So that means this format
- 4:17:12actually provided by this particular
- 4:17:13API. But let's say in our application
- 4:17:17you need the data in a certain format
- 4:17:19and this is what we called as or I
- 4:17:21called as uh uh data trans
- 4:17:25transformation. Okay. So let's do this
- 4:17:27one with example or else this will be a
- 4:17:28little tricky. So what I'm going to do
- 4:17:30right I'm going to create one type and
- 4:17:31I'm just going to give this one as
- 4:17:33placeholder user. Why I'm giving
- 4:17:35placeholder user because this is the
- 4:17:37format that we are getting from this
- 4:17:39URL. So now in this one I'm going to get
- 4:17:42a number. Sorry ID which will be a
- 4:17:43number. Okay. So we have a id then we
- 4:17:45are going to get get a name right. So
- 4:17:47I'm just going to get a name. This will
- 4:17:50be what? String. Then we are going to
- 4:17:52get a email. So this also will be
- 4:17:53string. And now you see that we are
- 4:17:55going to get a company and then inside
- 4:17:57company you're going to get name
- 4:17:59correct. So now I'm just going to take
- 4:18:00this one as company and this particular
- 4:18:02company is going to have a name which
- 4:18:04will be string. Now this is only
- 4:18:06possible if you know the format of that
- 4:18:09external API right from where you are
- 4:18:10fetching the data. If you don't know in
- 4:18:12that case you have to use some unknown
- 4:18:13type and based on that you have to parse
- 4:18:15get the data. you're getting that what
- 4:18:17I'm talking about because in this case
- 4:18:18we actually know that okay this is the
- 4:18:20data format we are going to receive when
- 4:18:22we are going to call this particular API
- 4:18:23URL but now what I'm going to do right
- 4:18:26we are going we are not going to use
- 4:18:28this particular data set instead we are
- 4:18:30going to create our own data set so here
- 4:18:32I'm just going to do this one as public
- 4:18:35user okay and now what I need I need a
- 4:18:38number sorry ID which will be a number I
- 4:18:40need a name which will be another uh
- 4:18:43string I need a email which also will be
- 4:18:46string but in this case I don't need
- 4:18:49name inside company instead what I'm
- 4:18:51going to do right I'm going to get a
- 4:18:53company directly so that means this is
- 4:18:55my data format or this is the
- 4:18:57requirement of this application that I'm
- 4:18:59building and this is the data format I'm
- 4:19:01receiving from that APA okay so that
- 4:19:03means once I'll get the data I need to
- 4:19:05transfer this particular data to this
- 4:19:07particular format so in this case I hope
- 4:19:09this is you're getting right so what I'm
- 4:19:11doing here so now how we are going to do
- 4:19:13that to do this what we going to do
- 4:19:15Right? We need to create a function
- 4:19:16always here. Okay. So I'm just going to
- 4:19:18create a function and this is will be
- 4:19:20your transform data or you can do
- 4:19:22transform user because we are working
- 4:19:23with user here. Right? In this case now
- 4:19:27what will happen right? This will be
- 4:19:28your raw data. Now this raw data is
- 4:19:30nothing but the data that you are
- 4:19:32receiving from this URL. So the type
- 4:19:34will be this because this is the format
- 4:19:36you receiving from that particular data
- 4:19:38source. So this will be my placeholder
- 4:19:40user. But when we are going to return
- 4:19:43it, I need to return this in this
- 4:19:45particular format. So that means we are
- 4:19:47actually transforming the data here. So
- 4:19:49that means when I'm going to do I'll
- 4:19:50return ID which will be my raw data dot
- 4:19:53id. Then I'm going to return a name
- 4:19:55which will be my raw data dot name. Then
- 4:19:58I'm going to return a uh email which
- 4:20:00will be raw data. Sorry if you're
- 4:20:02getting any noise. I think it's coming
- 4:20:03from outside some bike. And then I'm
- 4:20:06going to return the company which will
- 4:20:07be raw data dot company dot name. So
- 4:20:11what we have done based on our
- 4:20:13application requirement we are changing
- 4:20:15the raw data into a certain format. Now
- 4:20:18this format can be anything and this
- 4:20:19format can be anything in case you don't
- 4:20:22know this particular format. So in that
- 4:20:23case you have to use unknown type right.
- 4:20:26So now I think this is done. So first of
- 4:20:28all what we are going to do. So first
- 4:20:30let's see the best way that we can do a
- 4:20:32API call. So here we are going to do
- 4:20:34fetch external user right. This is
- 4:20:36external API call. What will be the data
- 4:20:39uh return type? So return type will be a
- 4:20:41promise and this will be a void. So
- 4:20:43first of all what we have to do here now
- 4:20:46see you can directly use fetch okay but
- 4:20:50we have to use certain functionality
- 4:20:52here and that uh functionality we call
- 4:20:54as about controller. So what actually
- 4:20:56about controller is so first let me
- 4:20:58write it and then I'm going to explain.
- 4:21:00So this will be new about controller.
- 4:21:02Now you have to understand this one in a
- 4:21:04proper way. Now first of all this
- 4:21:06particular thing right whatever this is
- 4:21:09we don't know what actually this is so
- 4:21:10this is going to help us to cancel
- 4:21:14certain fetch request so this will be so
- 4:21:17let me put in a uh proper way so this
- 4:21:20will be about controller
- 4:21:22lets us
- 4:21:24uh cancel and in progress
- 4:21:28fetch request in progress fetch request
- 4:21:33okay Now when you directly use fetch
- 4:21:37right so let's say you are directly
- 4:21:38using fetch what will happen right fetch
- 4:21:40actually doesn't automatically fail
- 4:21:42let's say what will happen right maybe
- 4:21:44you are doing API call and that API call
- 4:21:45didn't give any response in first 10
- 4:21:47seconds that is possible right so in
- 4:21:49that case fetch will not help you to
- 4:21:51cancel that particular request here what
- 4:21:54you're going to do we are going to use
- 4:21:55something called this about controller
- 4:21:57and we'll see that how we are going to
- 4:21:58actually implement this one when your
- 4:22:00external API will become very slow in
- 4:22:03that case this can be very very helpful.
- 4:22:05Okay. So now uh what we are going to do
- 4:22:07right. So first of all what this is
- 4:22:09going to do? So this will gives us a
- 4:22:11signal. Okay. And we pass that signal to
- 4:22:14our fetch and then we use some method.
- 4:22:16So this will give you some method. For
- 4:22:18example about method and using that
- 4:22:21method that particular fetch that we are
- 4:22:23using that gets canled. So how we are
- 4:22:25going to do that? So first of all I'm
- 4:22:27going to get take a set timeout here and
- 4:22:30the oh no not not like this you have to
- 4:22:31take a timer so that so I'll just do a
- 4:22:34timer and here I'm just going to do set
- 4:22:36timeout
- 4:22:37and here what I'm going to do right I'll
- 4:22:39give a 5second time out here so what I'm
- 4:22:41simply doing right so first of all this
- 4:22:43timer is going to do what or set time is
- 4:22:45set timeout is going to do what this is
- 4:22:47going to start a timer now if your
- 4:22:50external API doesn't respond within 5
- 4:22:52seconds because we are giving 5 seconds
- 4:22:54here this will happen right This timer
- 4:22:56is going to do something but what
- 4:22:59actually it's going to do? So this is
- 4:23:00where we are going to use this
- 4:23:02controller.about method. Now now you'll
- 4:23:04see that this will be very clear. So
- 4:23:06what we are doing that okay first we are
- 4:23:08starting this then we are starting a
- 4:23:10timer and we are simply checking that if
- 4:23:12my API is not giving any request or
- 4:23:15response or pin inside 5 seconds I'm
- 4:23:18going to call this controller dotabout
- 4:23:20method. So this is going to do what?
- 4:23:22it's going to cancel that particular uh
- 4:23:25request. Okay, so this will be the first
- 4:23:26thing. After this what I'm going to do
- 4:23:29I'll take a try and here we are going to
- 4:23:31do actual call. So I'm just going to do
- 4:23:33con and I'm simply going to do this one
- 4:23:35as response and this is where I'll do a
- 4:23:38fetch.
- 4:23:40We going to pass the API URL and here we
- 4:23:43going to pass a method. So method will
- 4:23:45be get and here we are going to pass a
- 4:23:47signal and this will be controller dot
- 4:23:50signal. Okay. Now first of all what is
- 4:23:53happening here? What fetch is going to
- 4:23:55do? Fetch is going to send an HTTP
- 4:23:57request to this API URL right method is
- 4:24:00our gate. I I think this is very clear
- 4:24:02because gate means we are reading the
- 4:24:04data right. What this signal is so this
- 4:24:06is going to connect this particular
- 4:24:08request to this about controller above.
- 4:24:11This is going to do that. I hope this is
- 4:24:13clear. Right now what will happen right?
- 4:24:15If you notice that we are using this
- 4:24:17controller.abot method also. Right. In
- 4:24:20case this particular method controller
- 4:24:22that we are using that runs before this
- 4:24:25API response in that case fetch is going
- 4:24:28to throw an about error. Okay. So first
- 4:24:30what we have to do here? So first we are
- 4:24:32going to give a check okay that if this
- 4:24:34response do okay if this is false. So
- 4:24:36I'm just going to log error and let's
- 4:24:39give this one as uh upstream
- 4:24:43API field with HTTP and then we can pass
- 4:24:47the response right. So we do response
- 4:24:49dot status and then we are going to
- 4:24:53return. So this will be the first thing.
- 4:24:56Then we are going to get our data. So
- 4:24:58how we are going to get the data? So
- 4:24:59I'll do raw user and here we are going
- 4:25:02to do a because this is async operation,
- 4:25:06right? I think we are using async here
- 4:25:07if I'm not wrong. We are using async. So
- 4:25:10ait and this will be my response dot
- 4:25:12json. But here you have to give the your
- 4:25:14data format. So what will be your data
- 4:25:16format? how we are going to get the data
- 4:25:18in this particular format. So I'm just
- 4:25:20going to as placeholder user right and
- 4:25:23then we're going to transform the data
- 4:25:26in the format that we want means we want
- 4:25:28in this certain format. So that means we
- 4:25:30need to call this method. So now what
- 4:25:32I'm going to do right so I'm simply
- 4:25:33going to do con
- 4:25:35user and this will be my transform user
- 4:25:38and you're going to pass the raw user.
- 4:25:40That's it. And now what I'm going to do
- 4:25:41right I'll simply just do log and I'm
- 4:25:44just going to log the user and then
- 4:25:46we'll take a catch.
- 4:25:48Let's take a error. Now what are we
- 4:25:51going to do first of all? So first I'm
- 4:25:53going to check if this error is a
- 4:25:55instance
- 4:25:59if this instance of error
- 4:26:02and if the error dot name
- 4:26:06is equal to okay we have to give equal
- 4:26:08to and this we called as about error
- 4:26:12okay and let's just check this one here
- 4:26:16so first of all I'm just going to do
- 4:26:18what is about error
- 4:26:23This is a specific error thrown in
- 4:26:25JavaScript when an ongoing asynchronous
- 4:26:27is deliberately cancelled. Okay. So
- 4:26:30first of all why does it happen? Most
- 4:26:32JavaScript APIs use about controller
- 4:26:34about signal interfaces to manage
- 4:26:36cancellation. When you invoke this about
- 4:26:39that we are doing here right simply we
- 4:26:41are calling this controller.about. So
- 4:26:43that means what will happen right? If
- 4:26:45your time out this particular timeout if
- 4:26:47this is runs and this particular about
- 4:26:49method is canceled sorry not canceled if
- 4:26:51called. So in that case your fetch is
- 4:26:53going to do is going to throw an error
- 4:26:56that error called as about error. Okay.
- 4:26:59So this is the task of your fetch but
- 4:27:01this will only throw in case if you're
- 4:27:03calling this and then you're passing the
- 4:27:05signal here. Okay. This is very very
- 4:27:07important. So now what are you going to
- 4:27:09do right? So first of all I think this
- 4:27:12is clear right or let's see if we know
- 4:27:14okay let's see common operation that
- 4:27:15trigger this canceling an ongoing f
- 4:27:18request stopping a read or write
- 4:27:20operation on streams and some
- 4:27:22interrupting and database query but for
- 4:27:24now we are focusing only on this one and
- 4:27:25this is what actually we are doing here
- 4:27:27right so we are simply telling that okay
- 4:27:29if this timeout fails so in that case
- 4:27:31you're going to call the about method so
- 4:27:33in that case your fetch is going to
- 4:27:34throw that about error so now what are
- 4:27:36you going to do right so first of all if
- 4:27:38this is the case So in that case I'm
- 4:27:40just going to do error again
- 4:27:44and I'm just going to do request failed
- 4:27:47or time out whatever you are going to do
- 4:27:49because
- 4:27:51upstream
- 4:27:52API took so long something like this and
- 4:27:57[snorts] we are going to return okay so
- 4:27:59this is done and then we are going to
- 4:28:02simply do we are also going to take a
- 4:28:04message and I'm simply going to do if
- 4:28:06the error is instance is
- 4:28:10of error. So we are going to get the
- 4:28:13error dot message or else I'm just
- 4:28:15simply going to pass unknown error.
- 4:28:19Okay. And then also again we can log
- 4:28:21error and I'm simply going to do
- 4:28:23external
- 4:28:26API failed
- 4:28:29and then we're going to pass this
- 4:28:30message here and then we are also going
- 4:28:33to take a finally. So in the finally
- 4:28:35what we have to do we have to clear the
- 4:28:37time out because we have a timeout here.
- 4:28:40So I'm just going to do clear time out
- 4:28:41and pass the timer and timer what we are
- 4:28:44using here. We're using timer only
- 4:28:47right? I think let's skip maybe let's
- 4:28:50skip time out. I think time out is
- 4:28:51better.
- 4:28:54And we're going to clear this here.
- 4:28:56Okay. That's it. So now I think this is
- 4:28:58done. So what we have to do right? So
- 4:29:00let's save this. And to call this, we
- 4:29:02are simply going to call this method
- 4:29:03here.
- 4:29:05That's it. So, this is done. So, now
- 4:29:07let's save this. And I'm just going to
- 4:29:10close. Let's clear. And I'll just copy
- 4:29:14this. Let's go to package.json.
- 4:29:17And I'll just paste it here. So, this
- 4:29:19will be 05
- 4:29:21tsx source/
- 4:29:24this. And then we're going to just run
- 4:29:26this. I'll do npm run 05.
- 4:29:30That's it. So you're going to get error
- 4:29:32not get error you're going to get the
- 4:29:33data because definitely it's going to
- 4:29:35pass. Now in this case one thing you
- 4:29:37have to understand that even though your
- 4:29:38response of this particular API is in
- 4:29:41this format that you are having company
- 4:29:43and inside company you are having name
- 4:29:45but when you're getting the actual data
- 4:29:46you're getting company as this directly.
- 4:29:49Okay this is very important why because
- 4:29:51in our code we are actually formatting
- 4:29:53or transforming the data the way we want
- 4:29:56you're getting. So this is what actually
- 4:29:58it's doing. So this is very very helpful
- 4:30:00when you have some certain requirement
- 4:30:02in your project and you want to
- 4:30:04transform the data. So you can use this
- 4:30:05kind of uh uh features. So I this is
- 4:30:09clear. I think this is very important.
- 4:30:10So with this we have completed one very
- 4:30:12important and I'm pretty much sure see
- 4:30:14if you're a beginner or let's see even
- 4:30:15the intermediate level this is more than
- 4:30:17enough in your node HTTP module. Right?
- 4:30:19Again when I'm going to create the part
- 4:30:21three in case if I'm getting good
- 4:30:23response I always mention that or else
- 4:30:24I'm not going to do that. I'll be very
- 4:30:26honest. If I get good response from part
- 4:30:28one and two and you'll see that there
- 4:30:30are certain concepts that normally you
- 4:30:32not find in most of the courses or
- 4:30:34videos those I'm going to cover but for
- 4:30:36now I think this is more than enough. So
- 4:30:38with this what will be the next part see
- 4:30:40I'm going to repeat once again again and
- 4:30:42again and I'm telling you so that you
- 4:30:45find sorry not you find or you actually
- 4:30:48get the most advantage from this
- 4:30:49particular video. What I'm trying to say
- 4:30:51is very very important that I'm going to
- 4:30:53add my posgress full course that I
- 4:30:55already created. This is very recent
- 4:30:57course like I think maybe one month ago
- 4:31:00I uploaded. I'm going to add that in the
- 4:31:02next part. Please do not skip that
- 4:31:04particular section. Okay. You can skip
- 4:31:06the intro part. Why I'm telling because
- 4:31:08that is a video part right? You can skip
- 4:31:10the intro like it will be 1 minute or
- 4:31:12even you can watch the intro because
- 4:31:14you'll understand that what are the
- 4:31:15things you are going to learn. Why this
- 4:31:16is very very important because this next
- 4:31:194 hours is going to give you exact idea
- 4:31:22of posgress every single thing you need
- 4:31:24and remember we are not going to use any
- 4:31:27packages in that so we are not using any
- 4:31:28OM prisma or drizzle okay these kind of
- 4:31:31things we are going to use raw pg and
- 4:31:33this is going to be a game changanger
- 4:31:35because if you really want to learn
- 4:31:36postless and then you start the API
- 4:31:38project that we are going to have after
- 4:31:39that so what I'm trying to say I'm going
- 4:31:42to repeat once again that do not skip
- 4:31:44this particular section okay this part.
- 4:31:46Do not skip. First you learn this one
- 4:31:49and after this you try to complete this
- 4:31:51whole section without skipping any any
- 4:31:53concept and then you do this then you
- 4:31:56jump to this one you already have the
- 4:31:58time stamp right you can directly jump
- 4:32:00in case you already know then you can
- 4:32:03directly jump to this particular section
- 4:32:05but I suggest do not do this one okay so
- 4:32:07with this now let's move on to the next
- 4:32:08one which will be postgress complete
- 4:32:10course and then we will start our actual
- 4:32:12practical project and in this one you're
- 4:32:14going to learn each and everything uh
- 4:32:16that you need to know to start your
- 4:32:18practical journey with node. Okay
- 4:32:21everyone, so now before uh we are going
- 4:32:23to proceed to our next section which
- 4:32:25will be so as I already mentioned that
- 4:32:27in the previous section we have
- 4:32:28completed HTTP and then we are going to
- 4:32:30have a complete dedicated postgress
- 4:32:32section but before that I thought that
- 4:32:34let's uh cover some of the very
- 4:32:35important uh concept and now these will
- 4:32:37be little bit advanced if you want you
- 4:32:39can watch this particular section or
- 4:32:41else you can also skip it's up to you
- 4:32:43because this will be mostly concepts and
- 4:32:45also we going to understand some of the
- 4:32:47nodes internals okay so first of all So
- 4:32:50how we are going to do this one right?
- 4:32:51So we are going to create one one file
- 4:32:53and then we'll go to uh this uh flow and
- 4:32:56then we are going to understand that
- 4:32:58what actually happening here and most of
- 4:33:00the things you can also get in your
- 4:33:01interview also. So first let's start
- 4:33:04with so let's create our first file and
- 4:33:06let's give this one as NodeJS
- 4:33:09internals. Okay. Now in the previous
- 4:33:11part we have already covered that what
- 4:33:13actually NodeJS is right. So we already
- 4:33:15know that NodeJS is a JavaScript
- 4:33:16runtime.
- 4:33:18But what we need to know that node is
- 4:33:21actually combines multiple components.
- 4:33:23So what I mean by multiple components?
- 4:33:25So we have something called V8 right. So
- 4:33:28we have something called V8 engine
- 4:33:30right. So what actually V8 engine is
- 4:33:32actually going to do. So this is what
- 4:33:34actually executes your JavaScript right.
- 4:33:36So this is the very basic things right.
- 4:33:38But we have also covered in the previous
- 4:33:41section that NodeJS also has some code
- 4:33:43modules also. For example let's say
- 4:33:44NodeJS has something called FS file
- 4:33:46system. NodeJS has networking, NodeJS
- 4:33:48has timers and all these things. What
- 4:33:51actually happens right? So there are a
- 4:33:53couple of things you need to understand
- 4:33:54that how actually NodeJS run your code.
- 4:33:56That is the most important part here
- 4:33:58which is what we are going to cover. So
- 4:34:00here we need to know first of all that
- 4:34:02NodeJS actually more than V8 engine is
- 4:34:05there are lot of things that is
- 4:34:06happening behind the scenes. So first of
- 4:34:08all what we going to cover? So some of
- 4:34:10the things you need to know that is
- 4:34:11first of all that what are the main
- 4:34:12NodeJS runtime components that we have.
- 4:34:15So first we have something called very
- 4:34:17very important and that we call as main
- 4:34:20JavaScript. So I'm just going to give
- 4:34:22main JS thread. So what this is going to
- 4:34:24do now first of all you need to know
- 4:34:27that whenever you're going to create a
- 4:34:28normal application right what JavaScript
- 4:34:30is going to do JavaScript execute one
- 4:34:33particular main JavaScript thread or
- 4:34:35JavaScript execute on one main
- 4:34:38JavaScript thread. Sorry I was explained
- 4:34:40that one wrong. So the this will be so
- 4:34:42normal applications. I'm just going to
- 4:34:44do like this. Normal applications
- 4:34:46JavaScript executes on one main
- 4:34:50JavaScript thread. Okay. So what I mean
- 4:34:53by this? So what are the process
- 4:34:55actually? So here you have your top
- 4:34:56level JavaScript, right? So let's say
- 4:34:58you have a function. So you have your
- 4:34:59top level JavaScript, you have your
- 4:35:01function call, you have your request
- 4:35:03handlers, you are going to have your
- 4:35:04timer. So let's you are using set
- 4:35:05timeout, set interval, all these. You
- 4:35:08have your promise call backs, file
- 4:35:09system call backs and all these things,
- 4:35:11right? So that means only one normal
- 4:35:13JavaScript call back execute at a time
- 4:35:15on this particular thread and this we
- 4:35:17call as main JavaScript thread. Right?
- 4:35:19And then the second main component will
- 4:35:22be your V8 engine. So what actually this
- 4:35:24V8 engine uh does. Okay. Now first of
- 4:35:28all you need to know that V8 is the
- 4:35:30JavaScript engine that is used by
- 4:35:32NodeJS. Okay. So V8 will be your
- 4:35:34JavaScript engine that is used by
- 4:35:36NodeJS. What this is going to do or what
- 4:35:38are the main responsibilities? First of
- 4:35:40all, it is going to do parsing your
- 4:35:42JavaScript. So, it is going to parse
- 4:35:43your JavaScript. So, parsing JS, it's
- 4:35:46going to execute your JavaScript. This
- 4:35:48will be the second thing. This is going
- 4:35:50to manage your call stack. So, managing
- 4:35:52call stack.
- 4:35:54Next will be it's also going to manage
- 4:35:56your heap memory also. Heap memory.
- 4:36:00Okay.
- 4:36:02And he memory and performing
- 4:36:06garbage collection.
- 4:36:10Okay.
- 4:36:12And also sometimes it's going to do
- 4:36:14what? It's going to optimize your
- 4:36:15executed code also. But mostly you need
- 4:36:17to know that these three are very very
- 4:36:19important. So first of all it's actually
- 4:36:21going to parse your JavaScript. It's
- 4:36:22going to execute your JavaScript and
- 4:36:23it's going to manage your call stack.
- 4:36:25Okay. So this is another main core
- 4:36:27component. So we have main JavaScript
- 4:36:29thread and then we have your V8 engine.
- 4:36:32Now we have right so we have our NodeJS
- 4:36:34core API. Now you might be thinking okay
- 4:36:36we have already covered but this is
- 4:36:37still very important because now we are
- 4:36:39actually understanding what is happening
- 4:36:40behind the scenes right so we have your
- 4:36:42NodeJS code APIs when I'm talking about
- 4:36:44what actually NodeJS code APIs are the
- 4:36:47things that we have already covered for
- 4:36:48example our file system we have our HTTP
- 4:36:50module we have our path module we have
- 4:36:53our streams right so all these things we
- 4:36:55have covered buffers then you have
- 4:36:57process right process also we have
- 4:36:59covered then you have timers all these
- 4:37:01things now all these core APIs right
- 4:37:03whatever you're seeing these actually
- 4:37:05written in JS only. Okay. Now some of
- 4:37:10these okay some of these core EPS next
- 4:37:13you have something called C++. So I'm
- 4:37:15just going to give it comment. So do C++
- 4:37:18bindings. Okay. Now what this is going
- 4:37:21to do now first of all you have to
- 4:37:22understand that what this is going to
- 4:37:24do. This is going to connect your
- 4:37:25JavaScript facing NodeJS APIs to native
- 4:37:28functionality. Okay. So that means here
- 4:37:30so first of all let's write it. So it's
- 4:37:32going to connect JS facing APIs to
- 4:37:37native functionality.
- 4:37:40So here what this is going to do right
- 4:37:41it's going to allow your JavaScript code
- 4:37:44to communicate to communicate with so we
- 4:37:49have something called leu okay then we
- 4:37:52have something called our operating
- 4:37:53system so OS APIs your native libraries
- 4:37:57okay now if I try to cover this leu only
- 4:38:01right so it's going to take more than 2
- 4:38:03hours okay so there are a lot of things
- 4:38:05so that is the reason I'm just going to
- 4:38:07give you the overview but when I'm going
- 4:38:08to upload the part three possible if I'm
- 4:38:11getting good response from this part one
- 4:38:13then I'm going to upload part part two
- 4:38:14anyways I'm going to upload but part
- 4:38:16three if I upload so I'm going to cover
- 4:38:18all of this in details okay now why
- 4:38:20actually you need all of this because
- 4:38:21what JavaScript is going to do right
- 4:38:23JavaScript doesn't directly call this
- 4:38:25operating system level kernels okay here
- 4:38:28this is one of the most important
- 4:38:30JavaScript core component that you need
- 4:38:31to know if you want to learn NodeJS uh
- 4:38:34properly now this is actually a native
- 4:38:36library that is used by NodeJS okay so
- 4:38:38this is a native library that is uh used
- 4:38:42by NodeJS used by NodeJS. So what this
- 4:38:46is going to provide right so it's going
- 4:38:47to first of all provide your event loop
- 4:38:49then it's going to provide your worker
- 4:38:51thread worker thread pool your timers
- 4:38:56your asynchronous as IO input output
- 4:38:59handling
- 4:39:01and also sometimes uh crossplatform
- 4:39:04abstractions all these things right here
- 4:39:06so first of all four things we have
- 4:39:08understood right so we have something
- 4:39:10called our main JS thread our VS sorry
- 4:39:12V8 engine nodes code APIs we have C++
- 4:39:16bindings and one very important native
- 4:39:19module sorry native library that we call
- 4:39:21as li okay then we have our operating
- 4:39:24system now all these are actually nodes
- 4:39:26internals okay you'll understand why I'm
- 4:39:28explaining all of these so you your
- 4:39:29operating system OS what operating
- 4:39:32system is going to do I think we have
- 4:39:34already covered the OS model remember
- 4:39:35operating system is going to do your
- 4:39:37low-level work so low-level work means
- 4:39:40let's say reading your files reading
- 4:39:41files writing your files
- 4:39:45uh maybe let's say tracking time
- 4:39:48right so all these thing it's going to
- 4:39:50do so now let's say if I go here and if
- 4:39:53you try to understand this one right so
- 4:39:55you have your javascript code this is
- 4:39:57your v8 engine and these all your core
- 4:40:00components so you have your nodejs core
- 4:40:02APIs which interact with your C++
- 4:40:04bindings right and this this is actually
- 4:40:06C++ binding is going to do what so C++
- 4:40:09binding is going to connect your
- 4:40:10JavaScript facing nodejs core APIs this
- 4:40:13on. So you have a JavaScript facing
- 4:40:14NodeJS API to your native functionality.
- 4:40:16So this C++ binding is going to help to
- 4:40:20communicate this particular API to your
- 4:40:23Leo this library or your operating
- 4:40:25system library. Okay. So this is why
- 4:40:26this C++ binding is actually very
- 4:40:29important. So I'm just going to repeat
- 4:40:30once again why this is actually very
- 4:40:32important. So have your JavaScript code.
- 4:40:34So first of all you have to understand
- 4:40:36what VS sorry V8 engine is going to do.
- 4:40:38So V8 engine actually going to parse
- 4:40:41your JavaScript. So that is the first
- 4:40:42thing. It's going to execute your
- 4:40:43JavaScript. It's going to manage your
- 4:40:45call stack. Okay. But then you have your
- 4:40:47some code API. I think we've already
- 4:40:49covered code modules, file system, uh
- 4:40:51HTTP, timers, all these things. But the
- 4:40:53problem is this code some some of these
- 4:40:56core APIs actually written in
- 4:40:57JavaScript. So it's actually can't
- 4:40:59directly interact with your operating
- 4:41:01system, right? So that is the reason we
- 4:41:03need something called or we need some
- 4:41:05mediator and that we call as C++
- 4:41:07bindings. So C++ bindings is going to
- 4:41:10connect your JavaScript facing nodes
- 4:41:12APIs to the native functionality. So
- 4:41:14this native functionality will be your
- 4:41:15native libraries leave and operating
- 4:41:18system. What leop is going to do? So
- 4:41:20this is a native library that is used by
- 4:41:23nodejs. It's going to provide your event
- 4:41:24loop, worker thread, timers and all
- 4:41:26these things. And operating system is
- 4:41:28going to do your low-level work. So
- 4:41:30low-level work means it's going to do
- 4:41:32let's say reading your files or writing
- 4:41:34files uh tracking time let's say it
- 4:41:37needs to needs to schedule some threads
- 4:41:39and all these things is going to do okay
- 4:41:41you need to know one very important
- 4:41:43thing that NodeJS is going to
- 4:41:45coordinates all these work but at the
- 4:41:46end your operating system is going to
- 4:41:48process actual system operations you are
- 4:41:51getting what I'm talking about so these
- 4:41:53all are very very important so if I try
- 4:41:54to explain this one let's say so you
- 4:41:56have your JS code right so this is your
- 4:41:58JS code. Then you have your V8 engine,
- 4:42:01right? And then you have your JavaScript
- 4:42:04code. So your JS code,
- 4:42:07then you have your NodeJS core APIs. So
- 4:42:10do core APIs. You have your
- 4:42:14you have your C++ bindings.
- 4:42:18Then you have your LIV and then you have
- 4:42:22your operating system. So this is how
- 4:42:24it's going to work. Okay. So this is the
- 4:42:26overall flow. Now this is very top level
- 4:42:28but if you try if someone ask you okay
- 4:42:30give me or let's say they will ask you
- 4:42:32in the interview okay give me summary of
- 4:42:34that how actually NodeJS internal flow
- 4:42:36works. So just explain in this way that
- 4:42:38first operating system is going to start
- 4:42:40your NodeJS process right. So NodeJS is
- 4:42:43going to initialize your V8 engine. So
- 4:42:44it's going to initialize your V8 engine.
- 4:42:47NodeJS also initialize your LEU and the
- 4:42:49event loop. Okay. Now let's say you have
- 4:42:52an entry file. I think we already know.
- 4:42:53So entry file is let's say app.js,
- 4:42:55right? So what NodeJS is going to do?
- 4:42:56It's going to load your app.js file.
- 4:42:58Right? Then V8 what it's going to do? V8
- 4:43:02is going to parse and execute your
- 4:43:03JavaScript. Right? V8 is going to parse
- 4:43:06and execute your JavaScript. Then your
- 4:43:08top level JavaScript is going to run on
- 4:43:10your main thread. I already explained
- 4:43:12this main thread, right? Normal
- 4:43:13JavaScript is going to run on the main
- 4:43:14thread. Once this is done, then let's
- 4:43:17say you have some async operation. Maybe
- 4:43:18let's say you're doing some API call. So
- 4:43:20async operation will be registered or
- 4:43:22delegated whatever it's going to do.
- 4:43:24Then the very important point you have
- 4:43:26something called event loop. Now I'm not
- 4:43:28going to go into too much details into
- 4:43:29event loop because it's very vast topic
- 4:43:31and then probably I'm [clears throat]
- 4:43:33going to cover this one in the part
- 4:43:34three okay not this one but event loop
- 4:43:37is going to continue until unless there
- 4:43:38is active work okay and then let's say
- 4:43:40you have some callbacks so comp all the
- 4:43:42callbacks will be completed and those
- 4:43:44call backs will be executed on your main
- 4:43:46thread okay so main thread will be your
- 4:43:48active and this process is going to exit
- 4:43:51when there is no active work remains
- 4:43:53means until unless there is active work
- 4:43:55your main thread will remain active
- 4:43:57Okay. So this will be your overall flow
- 4:43:59or this is how your NodeJS internal
- 4:44:01process works. So what are the things
- 4:44:03you need to know right? Why I'm
- 4:44:04repeating all of these? So first you
- 4:44:05need to know the term called we have
- 4:44:07something called VS sorry main JS
- 4:44:08thread. We have something called V V8
- 4:44:11engine. We have our NodeJS code APIs. We
- 4:44:13have C++ binding. What this is going to
- 4:44:15do? We have something called Leo and
- 4:44:18then we have our operating system. So
- 4:44:20with this now let's move on to the next
- 4:44:22concept. So let's move to the next
- 4:44:23concept and this will be so let's
- 4:44:25understand V8 uh V8 engine in details.
- 4:44:29Okay. So V8 engine MD. So I think we
- 4:44:33already know the basics right that what
- 4:44:34actually V8 engine does. So V8 will be
- 4:44:37your JavaScript engine. So V8 uh is your
- 4:44:40JavaScript engine that is used by used
- 4:44:45by NodeJS. Correct? So we already know
- 4:44:48the responsibility not KS it will be JS
- 4:44:51already know the responsibility it's
- 4:44:52going to parse your JavaScript it's
- 4:44:54going to let's say sorry it's going to
- 4:44:55parse your JavaScript so we are going to
- 4:44:57go into much more details now in this
- 4:44:59particular file and then we understood
- 4:45:01in the previous section that it's going
- 4:45:02to parse your JavaScript it's going to
- 4:45:03execute your code it's going to handle
- 4:45:05your event loop and all these things but
- 4:45:07how actually this whole process works or
- 4:45:10means what actually happens behind the
- 4:45:12scenes so [snorts] first let's
- 4:45:14understand that how V8 actually uh
- 4:45:16execute the JavaScript. Now this is very
- 4:45:18very interesting concept. Okay, what
- 4:45:20this is going to do, right? So first of
- 4:45:22all, it's going to parse your
- 4:45:23JavaScript. Okay, so this is the first
- 4:45:24thing once it will parse your
- 4:45:26JavaScript, right? So what this is going
- 4:45:28to do, right? This is going to create a
- 4:45:29internal representation of your code. So
- 4:45:32what I mean by this? So basically it's
- 4:45:34going to produce something called byte
- 4:45:36code. So it's going to produce byte
- 4:45:38code. So it's going to produce byte code
- 4:45:41and then it's going to execute the code.
- 4:45:43So then it's going to execute the code
- 4:45:45and then I'm just going to let's write
- 4:45:47it here only. So first of all we know
- 4:45:49that it's going to parse your JS. It's
- 4:45:51going to create internal representation
- 4:45:52of your code that what actually that
- 4:45:54code is doing. It's going to produce the
- 4:45:56bite code. It's going to execute the
- 4:45:57code right and then as you already me
- 4:46:00mentioned in the previous section also
- 4:46:01sometime it's going to optimize your
- 4:46:03frequent used code also. Then it's going
- 4:46:06to manage your call stack. So all these
- 4:46:08things it's going to do. This is not uh
- 4:46:11let's keep this one here in the new
- 4:46:13line. So it's going to manage your call
- 4:46:14stack. It's also manage your hip memory
- 4:46:18all these thing it's going to do right
- 4:46:19and also garbage collection also it's
- 4:46:21going to perform garbage collection.
- 4:46:24So all these thing your V8 engine is
- 4:46:26going to do. So now let's understand
- 4:46:29here that what actually it is doing. So
- 4:46:31first you have your JavaScript source
- 4:46:33correct. So this is your JavaScript code
- 4:46:35and then you have some you have
- 4:46:37something called parser. So this is
- 4:46:39where your V8 is going to parse your
- 4:46:41JavaScript right. So that means see
- 4:46:44first of all you need to know that what
- 4:46:45actually mean by parse the code mean V
- 4:46:49V8 engine needs to understand the source
- 4:46:51code right that what actually you're
- 4:46:53writing. So sometimes consider that you
- 4:46:55have something like this you have con
- 4:46:58total equal to price plus tax something
- 4:47:00like that you have written. So V8 is
- 4:47:02going to read that and it's going to
- 4:47:04check the syntax. If the syntax is
- 4:47:07invalid, so it's not going to run the
- 4:47:09code, right? So for example, let's say
- 4:47:11you written const total equals and then
- 4:47:15you don't have anything on the right
- 4:47:16side. So this is wrong. Correct? So that
- 4:47:18means this person is going to check the
- 4:47:20syntax and based on that it's going to
- 4:47:22give a error that if the statement is
- 4:47:24incomplete, right? After this it's going
- 4:47:27to create something called abstract
- 4:47:29syntax tree. Now you might be thinking
- 4:47:31from where I've actually learned all of
- 4:47:33these. So these all the things right
- 4:47:35what you can do right I'll give you the
- 4:47:37best of the best solution. So first
- 4:47:39option will be you search for uh V8
- 4:47:42engine internals or how V8 engine works
- 4:47:44on Google not on chat GP and everything.
- 4:47:46Okay. And then read some blogs. The
- 4:47:49blogs I'm talking about the old blogs.
- 4:47:51Old blogs means when this chat GP and
- 4:47:53all these things uh was not there. Okay.
- 4:47:55and then read some two three blogs and
- 4:47:57try to understand and then [snorts] you
- 4:47:59can ask for some AI tools and
- 4:48:00everything. So this is why I usually
- 4:48:02read multiple blogs and then I came to
- 4:48:04know all these things and based on this
- 4:48:05I actually created and try to simpl
- 4:48:07simplify this particular flow. So what
- 4:48:09this is going to do right after pursing
- 4:48:11it's going to create a abstract syntax
- 4:48:13tree and this we call as a insert. Okay.
- 4:48:16So this is what it's going to do that
- 4:48:18means this is going to represent that
- 4:48:20what actually you have written in the
- 4:48:21code. As simple as that what actually
- 4:48:23you have written in the code means it's
- 4:48:24going to understand your structure and
- 4:48:26the meaning of the program. For example
- 4:48:28let's have a cons total is equal to
- 4:48:30price plus tax. So it's going to
- 4:48:31understand okay you are creating a total
- 4:48:32value from price and tax. So you're
- 4:48:34adding both right. So here it's going to
- 4:48:37understand okay I have a variable
- 4:48:39declaration which is total uh I have a
- 4:48:42addition so I'm adding both price and
- 4:48:44tax so it's going to understand so this
- 4:48:46particular part is going to understand
- 4:48:47that code okay so now what this is going
- 4:48:50to do right and also okay one very
- 4:48:52important point when this is going to
- 4:48:53check right so it actually going to
- 4:48:55remove unnecessary formatting details
- 4:48:57and everything okay for example let's
- 4:48:58say you have spaces indentation all this
- 4:49:01thing is going to remove once this is
- 4:49:02done then it's going to create your bite
- 4:49:04code okay so this will be your bite code
- 4:49:06we also call as bite code and the
- 4:49:09interpreter. Okay. So, V8 is going to
- 4:49:11create the bite code. I already
- 4:49:12mentioned it's going to produce the bite
- 4:49:14code. Why? First of all, you need to
- 4:49:16know what actually bite code. So, it's a
- 4:49:18low-level or lower level internal format
- 4:49:21and this is actually easier to
- 4:49:23understand actually for V8 because at
- 4:49:25the end V8 is going to execute your
- 4:49:27code, right? So, that is the reason what
- 4:49:28this is going to do. It's going to
- 4:49:29actually produce a bite code. Once this
- 4:49:32is done, then we have something called
- 4:49:34interpreter. Okay? So interpreter and
- 4:49:36then you have your optimize. I think
- 4:49:37this part you can understand easily. So
- 4:49:39optimize I mentioned this one right. So
- 4:49:41sometime is going to optimize your
- 4:49:42frequent written code. So it's going to
- 4:49:44optimize your machine code and then at
- 4:49:46the end it's going to execute the code.
- 4:49:47But the main important part actually
- 4:49:49these three line here. So first you have
- 4:49:51a parser. What parser is going to do?
- 4:49:53Parser is going to check your syntax.
- 4:49:54That is very important. If you have a
- 4:49:56wrong wrong syntax it's going to give
- 4:49:58you error. Right? So then you have
- 4:50:00something called abstract syntax tree or
- 4:50:02a So this is going to do what I already
- 4:50:05mentioned. So this is going to create or
- 4:50:06this is going to represent the structure
- 4:50:08or the meaning of your actual code means
- 4:50:10this is going to understand let's have a
- 4:50:12function okay this is a function this is
- 4:50:14the name of the function this function
- 4:50:15is adding two variables and it's going
- 4:50:17to return the sum of these two
- 4:50:18variables. So this is going to
- 4:50:19understand that and based on that it's
- 4:50:21going to produce a bite code and then
- 4:50:23it's going to create uh sorry it's going
- 4:50:25to execute your code. Okay. So this will
- 4:50:28be the overall flow or this is how V8 is
- 4:50:31going to execute your JavaScript uh code
- 4:50:34or NodeJS code. Okay. So I think that's
- 4:50:36all for this particular one. Now with
- 4:50:38this now let's move on to the next one.
- 4:50:39So let's move to the next one. I thought
- 4:50:41that I'm not going to cover but let's
- 4:50:43give you some idea on this particular
- 4:50:44module. Okay. So what actually this
- 4:50:46library does I'm doing TSM.
- 4:50:52Okay. So first of all we already know
- 4:50:54that that Leo
- 4:50:56is a native library that is used by
- 4:51:00NodeJS right so this we already know. So
- 4:51:03first what is the main purpose? So the
- 4:51:06main purpose will be so you have to
- 4:51:08understand this one. So this is actually
- 4:51:09going to help node to handle
- 4:51:12asynchronous or async operations right
- 4:51:15across different OS system also this is
- 4:51:17very important. So when I what I mean by
- 4:51:19this so first let's write it. So it's
- 4:51:21going to help. So it's going to help
- 4:51:23NodeJS to handle
- 4:51:25a sync operations
- 4:51:29across
- 4:51:31different OS
- 4:51:33system. Okay. So here this is going to
- 4:51:36provide first of all in event loop
- 4:51:38worker thread pool
- 4:51:42in your timers right uh async io
- 4:51:46operations.
- 4:51:49So now let's understand that why
- 4:51:51actually NodeJS need this particular
- 4:51:53library. Now what we understood from the
- 4:51:56previous section that what V8 actually
- 4:51:58going to do. So V8 is going to execute
- 4:52:00JavaScript right. So it's going to
- 4:52:02execute a JavaScript code. It's going to
- 4:52:03create you it has some parser. It's
- 4:52:06going to parse your code. It's going to
- 4:52:08create a right as we have already
- 4:52:10covered that one. Then based on that
- 4:52:12it's going to produce produce the bite
- 4:52:13code and based on that it's going to run
- 4:52:15your code. But V8 actually not going to
- 4:52:18provide all these operations. So it's
- 4:52:20not going to provide. So V8 does not
- 4:52:23provide
- 4:52:25file system operations.
- 4:52:29Then you have network
- 4:52:31socket handling all these things
- 4:52:36right and then also
- 4:52:40uh general event loop for node APIs,
- 4:52:43NodeJS APIs, right? So that means in
- 4:52:45this case NodeJS needs something else to
- 4:52:49handle all this. So NodeJS needs
- 4:52:50something else. Correct? So this means
- 4:52:53NodeJS need a separate layer or NodeJS
- 4:52:57needs a another layer that will handle
- 4:53:01uh that will handle not handle but that
- 4:53:03will coordinate
- 4:53:05coordinate
- 4:53:07this runtime because all these are
- 4:53:09runtime features right. all these
- 4:53:10runtime features right and all these
- 4:53:13features are provided by this leuof
- 4:53:15native library okay so this is what it's
- 4:53:17going to do if we try to explain uh
- 4:53:19understand this one so before we'll
- 4:53:21explain this so let's see this one so
- 4:53:22you have something like this you have
- 4:53:24your NodeJS API so I'll do NodeJS APIs
- 4:53:27right and then you have your JavaScript
- 4:53:30code you have NodeJS APIs you have your
- 4:53:32uh then you have your native binding
- 4:53:35then you have your leave and then at the
- 4:53:37end you you're going to have your OS
- 4:53:39right so This is how it's going to do.
- 4:53:41So first of all let's see this
- 4:53:43particular flow and then we understand
- 4:53:44the responsibilities. So you see that we
- 4:53:47have JavaScript uh then you have your
- 4:53:49NodeJS code APIs the code AP we have
- 4:53:51file system HTTP timers and all these
- 4:53:53things then it will go through your CS C
- 4:53:55C++ binding I think this is the same
- 4:53:57flow we covered then we have your leave
- 4:53:59of uh library right so it's going to
- 4:54:01provide all this thread pool event loop
- 4:54:03and everything and at the end it's going
- 4:54:05to interact with your operating system.
- 4:54:07So this is what it's going to do and
- 4:54:08what these uh main responsibilities will
- 4:54:10be. So first of all event loop right? So
- 4:54:12what actually event loop is going to do?
- 4:54:14I'll give you basic overview. So what
- 4:54:16event loop is going to check for? See
- 4:54:17event loop is going to check for a
- 4:54:18couple of things. First it's going to
- 4:54:20check for your completed IO operations.
- 4:54:22It's going to check for a timer. So
- 4:54:24let's say you are using some timers and
- 4:54:25it's going to check whether there is any
- 4:54:27timer which is in ready state. It also
- 4:54:30going to check for your pending
- 4:54:31callbacks and also it socket activity.
- 4:54:33Okay. So socket means let's say you're
- 4:54:35using sockets. So event loop is also
- 4:54:37going to check the activity
- 4:54:38[clears throat] of that particular
- 4:54:38socket. So now I'm just going to write
- 4:54:40here. So what event loop is going to
- 4:54:42check? So it's going to check for your
- 4:54:44completed IO operations.
- 4:54:48It's also going to check for your
- 4:54:49timers. So if you have any timers and if
- 4:54:52some timers
- 4:54:56timers are in ready state
- 4:55:00then you also want to check for your
- 4:55:01pending callbacks. Also it's going to
- 4:55:04check for the socket
- 4:55:06activity. So this is what event loop is
- 4:55:09going to check or this will be one of
- 4:55:12the main responsibility of libu which
- 4:55:14will provide this particular feature.
- 4:55:17Then second will be we have something
- 4:55:18called thread pool. Right? So we have
- 4:55:20something called thread pool. Why
- 4:55:22actually we need thread pool or why liu
- 4:55:24actually provide this particular thing.
- 4:55:26So first of all you need to know that
- 4:55:27libu provides
- 4:55:30provides a shared worker thread pool
- 4:55:35and why actually you need this. Now if
- 4:55:37you go here this particular thing I'm
- 4:55:39talking about this thread pool or this
- 4:55:41pool is is used by those operations that
- 4:55:45cannot be handled efficiently. Now what
- 4:55:47I mean by this? So that means this pool
- 4:55:49that we have is used by only those
- 4:55:54operations
- 4:55:56that cannot be handled efficiently. Now
- 4:56:00the best example of this will be let's
- 4:56:02say you have some project you're
- 4:56:04building some project where you have
- 4:56:05multiple or many file system operations
- 4:56:09many file system operations. Okay then
- 4:56:12let's say you are doing some
- 4:56:14cryptographic operations. So this is
- 4:56:15also where it's needed cryptographic
- 4:56:17operations and then also let's say
- 4:56:19you're using some compression related
- 4:56:21work.
- 4:56:24Okay. So this is where we need this
- 4:56:27thread pool and then also you have
- 4:56:29something called timers right. So timer
- 4:56:30is nothing but so what li is going to do
- 4:56:32it's going to help node to track all
- 4:56:34those timers right and then very very
- 4:56:37important it's going to determine when a
- 4:56:39particular timer is eligible to execute.
- 4:56:41Okay. So that means let's say you have a
- 4:56:43timer with a delay. What will happen?
- 4:56:45Right? It doesn't going to block your
- 4:56:48JavaScript main thread. Okay. So I think
- 4:56:49this part we already covered what this
- 4:56:52runtime is going to do, right? Runtime
- 4:56:54is going to record that particular timer
- 4:56:56and then it's going to continue
- 4:56:57processing the other task. Okay. So this
- 4:57:00is actually very important concept.
- 4:57:01Okay. So let's say you have a timer. So
- 4:57:03li is going to help lies
- 4:57:06to track those timers.
- 4:57:10And then this is important part. So this
- 4:57:12is the important here. So it's going to
- 4:57:14determine when the timer is become
- 4:57:19eligible to run or no not run execute.
- 4:57:23Right? So what will happen right? So
- 4:57:24let's say you have a timer with 5second
- 4:57:26delay. So a 5second delay. So it not
- 4:57:29going to or this doesn't mean so it
- 4:57:32doesn't mean the javascript is going to
- 4:57:35slip on the main thread. It's not going
- 4:57:37to do that. So it's going to do what?
- 4:57:38Right. So this uh timer whatever you
- 4:57:41have runtime is going to runtime is
- 4:57:45going to record this timer that okay I
- 4:57:46have a timer. So runtime is record the
- 4:57:49timer and then it's going to continue
- 4:57:51working other task. So continue
- 4:57:53processing other task and then once this
- 4:57:56timer is become eligible to execute so
- 4:57:58that time li is going to determine and
- 4:58:00based on that it's going to run the
- 4:58:01timer on the main thread. Okay. So this
- 4:58:04is what it's going to do and also li
- 4:58:06also communicate with your operating
- 4:58:08system APIs right. So this part let's
- 4:58:10say file related operations handle uh
- 4:58:12process handling timing as synchronous
- 4:58:14notification and all these things. So
- 4:58:16this is why we need this particular
- 4:58:18module which we call as leave. So with
- 4:58:21this now let's move on to the next
- 4:58:22concept. Okay. So let's move to the next
- 4:58:24one. Uh and these will be so let's
- 4:58:26understand what is uh blocking
- 4:58:31and non-blocking.
- 4:58:34Now you might be thinking I'll tell you
- 4:58:36why actually I'm covering all these uh
- 4:58:37this section will be very simple. So
- 4:58:39this section is going to do what right?
- 4:58:40So whatever you have covered in this
- 4:58:42four files it's going to set your base
- 4:58:45for the part three because what will be
- 4:58:48the part three right again part three
- 4:58:50I'm going to only upload if I get good
- 4:58:52response from part one and two or else
- 4:58:54there is no point okay so I just wanted
- 4:58:55to make this point very very very clear
- 4:58:58so part three will be only NodeJS pure
- 4:59:00concept we are going to cover so there
- 4:59:01will not be any APIs and everything okay
- 4:59:03so only NodeJS internal code modules
- 4:59:06worker threads cluster module uh then
- 4:59:09you have your child processes and all
- 4:59:10these things. Okay. So this is the
- 4:59:11reason we are covering very important uh
- 4:59:13section so that you you will be aware of
- 4:59:15the time. Okay. We have something called
- 4:59:17V8 engine. We have something called
- 4:59:18leave. What is blocking non-blocking?
- 4:59:20For example, in the part three uh the
- 4:59:23event loop section only only will take 1
- 4:59:25hour. Okay. So this is very huge. So
- 4:59:27that is the reason we are doing this
- 4:59:29particular section. So first you need to
- 4:59:31know what actually blocking means and
- 4:59:32what is non-blocking. First of all, you
- 4:59:34need to know blocking means the main
- 4:59:36JavaScript thread that you have, it
- 4:59:39can't continue with other JavaScript
- 4:59:41task until unless your current operation
- 4:59:43that whatever you're doing that
- 4:59:45finishes. So that means it needs to wait
- 4:59:47until unless until the current work
- 4:59:49finishes work finishes. So that will be
- 4:59:52your blocking, right? So it's actually
- 4:59:53block your main thread. Okay. On the
- 4:59:56other side, what will be your
- 4:59:57non-blocking? Non-blocking means
- 4:59:59JavaScript can start operation and then
- 5:00:02it's going to retain or it's going to
- 5:00:04regain the control before the final
- 5:00:06result is available. Okay. So now let's
- 5:00:09see some of the synchronous operation. I
- 5:00:10think this part we've already covered in
- 5:00:12the core module. So synchron synchronous
- 5:00:15operation will be these operation must
- 5:00:16finish before JavaScript continue. So
- 5:00:18you have something called remember we
- 5:00:20have read file sync right? This is we
- 5:00:23have done. So in that case what will
- 5:00:24happen right? So JavaScript let's say
- 5:00:26consider this only JavaScript is going
- 5:00:28to start the operations your main thread
- 5:00:30is going to wait this particular
- 5:00:32operation finishes it's going to return
- 5:00:34the result and then JavaScript is going
- 5:00:36to continue with the other task. So now
- 5:00:38let's say if I go here right so you have
- 5:00:39a blocking it's going to start the
- 5:00:41operation your main thread is going to
- 5:00:43wait until unless let's say this read
- 5:00:45file sync this particular uh logic
- 5:00:47whatever you've written this code will
- 5:00:49execute once this is done then it's
- 5:00:51going to complete that that particular
- 5:00:53operation and then JavaScript is going
- 5:00:54to continue with the other task and this
- 5:00:56is where it's going to block this
- 5:00:58operation until unless your current uh
- 5:01:00task is completed right on the other
- 5:01:03side we have a synchronous API
- 5:01:06asynchronous API so we have read file.
- 5:01:07Now this is this is async operation. So
- 5:01:10in this case what will happen right? So
- 5:01:11very very simple way your work is
- 5:01:14delegated right? So that means
- 5:01:16JavaScript can continue with other task
- 5:01:18right once it will complete the other
- 5:01:20task. Let's say you have a call back and
- 5:01:22that call back becomes ready and then
- 5:01:24call back is going to run on the main
- 5:01:25thread. So it's something like this. So
- 5:01:27you have your start operation and this
- 5:01:29is the delegate work means this is
- 5:01:31non-blocking means your non main thread
- 5:01:34is continue doing other task at the same
- 5:01:36time and then it's going to once let's
- 5:01:38say you're using some callback right
- 5:01:39your callback becomes ready. So in that
- 5:01:41case this particular operations
- 5:01:43completed and then your callbacks
- 5:01:45becomes ready and then it's going to run
- 5:01:46the call back on the main thread. So
- 5:01:48that mean it's not going to block your
- 5:01:50main thread. So this will be the very
- 5:01:51basic definition of non-blocking and the
- 5:01:54blocking. Okay, these can be interview
- 5:01:56questions also. Why? Because they can
- 5:01:58give you okay, you have learned file
- 5:02:00system. Give me some proper example or
- 5:02:03proper method to differentiate between
- 5:02:06blocking and non-blocking operation.
- 5:02:08Okay, so this is the reason I wanted to
- 5:02:09me uh mention this one. [snorts] So I
- 5:02:12think this will be simpler one and I
- 5:02:14think with this we have covered almost
- 5:02:16four things I wanted to cover in this
- 5:02:18particular. Let's do one thing in the
- 5:02:20next section. I'm going to give a very
- 5:02:21basic overview of the cluster module so
- 5:02:24that you'll be aware what actually that
- 5:02:25module is and try to cover that part
- 5:02:28also and then uh we will start with our
- 5:02:32crowd implementation which will be
- 5:02:33postgress section. So let's move on to
- 5:02:35the next one. Okay. So let's create the
- 5:02:37last file and this I'll give you some
- 5:02:39very basic idea of cluster module that
- 5:02:41what actually this module is. Okay. All
- 5:02:43right. So let's uh start understanding
- 5:02:45first that why actually you need this
- 5:02:47cluster module. So first you know that
- 5:02:49normal NodeJS process right so you have
- 5:02:51normal NodeJS process and this will
- 5:02:53always have one main JavaScript thread
- 5:02:55correct so it will have one main JS
- 5:02:58thread so this is the first thing this
- 5:03:01particular process right so this normal
- 5:03:02NodeJS process that you have it can
- 5:03:04handle multiple or many concurrent IO
- 5:03:07operation but at the end this is going
- 5:03:09to execute on one main thread only right
- 5:03:12so now let's say you're running this
- 5:03:14NodeJS code on your machine and that
- 5:03:15machine has multiple CPU nodes. So in
- 5:03:18that case one node just process cannot
- 5:03:20fully use all of them. Correct? So in
- 5:03:22that case you have something called
- 5:03:23cluster module. So what this cluster
- 5:03:26module is going to do this is actually
- 5:03:28going to solve this particular problem
- 5:03:30by starting multiple NodeJS worker
- 5:03:33processes. So by starting multiple
- 5:03:37NodeJS worker processes. So what do we
- 5:03:40mean by this? So first of all you need
- 5:03:42to know each and every worker process
- 5:03:44that we are going to have is going to
- 5:03:46have its own its own NodeJS runtime is
- 5:03:51going to have its own V8 engine.
- 5:03:55It's going to have its own event loop
- 5:03:59and also very very interesting and very
- 5:04:01important it's also going to have its
- 5:04:03own main JS thread right and also its
- 5:04:06own memory. So all these things each and
- 5:04:09every worker process is going to have.
- 5:04:11So what this is going to allow this is
- 5:04:13going to allow several NodeJS processes
- 5:04:15to run in parallel you're getting
- 5:04:16because we have multiple worker process
- 5:04:18right. So uh this is how it's going to
- 5:04:20work. But how actually this is
- 5:04:21happening. So let's see. So you have
- 5:04:23your incoming request. The very key
- 5:04:25point you need to know this cluster
- 5:04:27architecture whatever you seeing this
- 5:04:29will have at least one primary process.
- 5:04:32So it has to be there and then it's
- 5:04:33going to have your multiple worker
- 5:04:35processes. So you have worker process
- 5:04:37one, process two, process three and
- 5:04:38process four. Okay. Now what this
- 5:04:41primary process is going to do? This
- 5:04:43primary process is going to manage all
- 5:04:45these worker processes. Okay. So this is
- 5:04:48going to manage all these worker
- 5:04:49processes. And what will be the
- 5:04:51responsibility of this primary process?
- 5:04:52So first it's going to start all the
- 5:04:54worker processes. It is going to watch
- 5:04:56if all the worker processes are in the
- 5:04:58correct health or not. Health means if
- 5:05:00let's say something is down or it's
- 5:05:02something is failing. So it's going to
- 5:05:03check. It also going to restart the
- 5:05:05failed worker. So for example, let's say
- 5:05:07worker two is not working. It is in
- 5:05:09failed state. So in that case, it's
- 5:05:11going to restart this worker uh this
- 5:05:13particular worker process. Okay. Also,
- 5:05:15it's going to coordinate the process
- 5:05:17life cycle. So this is how it's going to
- 5:05:19work. So that means you have your
- 5:05:21primary process and it's going to have
- 5:05:22your worker process one, process two,
- 5:05:24process three and process four like
- 5:05:26this. Now these workers that whatever
- 5:05:29you're seeing, right? So we have our
- 5:05:30four workers. each and every worker can
- 5:05:34share the same server port. So what I
- 5:05:36mean by this? So let's say I have port
- 5:05:3830,00 right? So in this port 30,000 I
- 5:05:41can run this worker one, worker two,
- 5:05:43worker three and worker four. Okay,
- 5:05:45you're getting and then we have another
- 5:05:48uh concept called request distribution.
- 5:05:51Now what I mean by request distribution
- 5:05:53that means whatever incoming request
- 5:05:54that you are getting that will be
- 5:05:56distributed among these four workers. So
- 5:05:59that means let's say you have a four
- 5:06:00request coming request A, request B,
- 5:06:02request C, request D. So request A will
- 5:06:04be handled by worker process one.
- 5:06:06Request B will be handled by worker
- 5:06:08process two, three and four and so on.
- 5:06:10Right? Now here you need to know one
- 5:06:11thing because each and every workers are
- 5:06:13separate process. Right? In that case
- 5:06:15what let's say you have two requests
- 5:06:17request and request B. These request and
- 5:06:19request B can execute JavaScript at the
- 5:06:22same time on different CPU codes. And
- 5:06:24this is the advantage of using this
- 5:06:26cluster module. Okay. So I hope this is
- 5:06:28clear. Now this will be enough. I wanted
- 5:06:30to give you just very basic idea. These
- 5:06:32are very vast concept right? So if you
- 5:06:34go to NodeJS official documentation and
- 5:06:36just search for cluster module child
- 5:06:38processes worker threads you're going to
- 5:06:40get huge huge so many things. So that is
- 5:06:43the reason I just wanted to cover these
- 5:06:45five very very important concept. NodeJS
- 5:06:47internal V8 engine leave blocking
- 5:06:49non-blocking and cluster model. With
- 5:06:51this now let's move to the next one. So
- 5:06:53next on next part will be uh so we are
- 5:06:56going to cover posgress. So now this is
- 5:06:58actually mandatory section. So next
- 5:07:00section you need to do before we are
- 5:07:02going to start cred. Okay. So with this
- 5:07:04now let's move on to the next one. Hey
- 5:07:06everyone welcome back. So in this
- 5:07:07particular video you are going to learn
- 5:07:09each and everything that you need to
- 5:07:10know to start your journey with
- 5:07:11posgress. Now before I'm going to
- 5:07:13discuss the curriculum one humble
- 5:07:15request that please like this video
- 5:07:17comment and also please share and if
- 5:07:19possible if you can hype this video it
- 5:07:20will be very helpful. See I've given a
- 5:07:22lot of effort to create these kind of
- 5:07:24free videos for all of you so that you
- 5:07:26don't have to buy a paid courses. Okay.
- 5:07:28So this video is a prerequisite of my
- 5:07:31upcoming NodeJS masterclass video. So I
- 5:07:33just wanted to mention in case if you're
- 5:07:35not aware. So I will be uploading a 20
- 5:07:38hours free NodeJS course on next month.
- 5:07:41So in that particular course we are
- 5:07:42going to cover Postgress. So if you
- 5:07:44complete this particular video it will
- 5:07:46be very very helpful. So this video will
- 5:07:48be divided into three sections. So we're
- 5:07:50going to start with foundations where we
- 5:07:52we are going to cover the concept of how
- 5:07:53you can create your database schemas,
- 5:07:55tables, data types, constant and the
- 5:07:58concept of primary keys. Then we'll move
- 5:08:00to the second section which will be very
- 5:08:02very helpful. So we're going to
- 5:08:03understand queries and data operations
- 5:08:05like how you're going to insert rows,
- 5:08:08select specific columns, basic
- 5:08:10filtering, the concept of like, I like
- 5:08:13patterns, null, not null, order by,
- 5:08:16limit, offset and paginance. how we are
- 5:08:18going to update single row, how you can
- 5:08:20update multiple rows and at the end we
- 5:08:22are going to finish with returning after
- 5:08:24insert concept and then we're going to
- 5:08:26move to the last section which will be
- 5:08:28relationship and advanced SQL. So you're
- 5:08:30going to understand the concept of
- 5:08:32foreign keys one to many relationship
- 5:08:34inner join left join many to many
- 5:08:36relationship table allises aggregate
- 5:08:38functions group by count distinct sub
- 5:08:42queries indexes and at the end we are
- 5:08:44going to finish finish this particular
- 5:08:45video with transactions I'm pretty much
- 5:08:48sure that uh if you complete this video
- 5:08:50you don't have to buy any paid courses
- 5:08:52if you're a beginner this will be a
- 5:08:54perfect start I've added the source code
- 5:08:56that is completely free in the
- 5:08:57description you can download from my
- 5:08:59website. So with that now let's get
- 5:09:01started and good luck. All right
- 5:09:03everyone. So now we'll get started. So
- 5:09:05first of all I wanted to mention I think
- 5:09:06I already mentioned this one in the
- 5:09:08intro that this is a practical course.
- 5:09:10So what I mean by this now if you have
- 5:09:11already followed my TypeScript course
- 5:09:13that I've uploaded maybe 2 3 months
- 5:09:15back. So there what I've done right? So
- 5:09:18I will uh create individual file for
- 5:09:22individual concepts and then by writing
- 5:09:24the commands and the ASQL queries you
- 5:09:27are going to learn all those concepts.
- 5:09:28Okay. Now this is actually very very
- 5:09:30important because I just wanted to make
- 5:09:32these things very very clear that this
- 5:09:33is not only a conceptual course. Okay.
- 5:09:36For example, let's say I'm just going to
- 5:09:37spend time on what is poss 10 minutes.
- 5:09:40It's not going to work like that. Okay.
- 5:09:43So this is a completely practical course
- 5:09:45for every uh concepts and commands we
- 5:09:48are going to create individual file and
- 5:09:49then we are going to run those file and
- 5:09:51we'll understand each and everything.
- 5:09:53Now second thing I wanted to mention
- 5:09:55because I'm using Mac here. So I'm going
- 5:09:57to download this one for Mac. In case
- 5:09:59you're using Windows, maybe most of you
- 5:10:01are going to use Windows. What you can
- 5:10:03do right, you can use uh sorry, watch
- 5:10:05these two videos. Both the videos are
- 5:10:07very good. Maybe you can watch the
- 5:10:09second one, right? Second one is the uh
- 5:10:112025 update. So follow this exact one.
- 5:10:14Uh this is going to show you that each
- 5:10:17and everything that you need to install
- 5:10:19and set up posgress. So for this one, we
- 5:10:21are going to use PG admin. Right? So
- 5:10:23here first of all what you're going to
- 5:10:24do right uh you come to this particular
- 5:10:27uh URL from here right and then whatever
- 5:10:31you're using based on that you can just
- 5:10:33download it. So I'm just going to click
- 5:10:34here
- 5:10:36and then it's going to download. So
- 5:10:39let's wait for some time. Okay let's
- 5:10:42open this.
- 5:10:45Let's click here open.
- 5:10:49And here I have to give my password.
- 5:10:54Okay, click on next. So this is where
- 5:10:57it's going to install. Next. So we are
- 5:11:00going to install all of these. So we
- 5:11:01have Postgress, SQL server, PG admin 4,
- 5:11:04stack builder and the command line
- 5:11:05tools. Okay. So I'll do next. Next. And
- 5:11:09this is actually very very important.
- 5:11:11Why? This is where you need to set your
- 5:11:14password. Now try to give the password
- 5:11:16that you will be able to uh like
- 5:11:18recollect. So for this one I'm going to
- 5:11:20use admin. Okay. So I'm going to do
- 5:11:22admin. And here also I'm going to type
- 5:11:25admin.
- 5:11:26Next. This is the port uh that posgress
- 5:11:29is going to listen. So just keep it like
- 5:11:32this. Go to next. Uh this is also I'm
- 5:11:34going to click uh next. And this will be
- 5:11:36all your settings that we are going to
- 5:11:38use for our installation. Do next. And
- 5:11:41now it's going to install. Okay.
- 5:11:45All right. So, I think this is done. So,
- 5:11:47I'm just going to uncheck this one and
- 5:11:48I'm going to do finish. Okay. And this
- 5:11:51is completed. But now what you're going
- 5:11:52to do, right? So, I'm just going to
- 5:11:53close this. And let's see if I just
- 5:11:55search here PG admin. Okay. And you'll
- 5:11:58see that you're not going to get it so
- 5:12:00directly. Why? Because first of all, you
- 5:12:02have to go to your application in case
- 5:12:04you're using uh Mac. And then here you
- 5:12:08are going to get uh Postgress SQL 18. So
- 5:12:11click here and then from here you'll be
- 5:12:13able to open this. So just open this PG
- 5:12:16admin 4.
- 5:12:18All right. So now first of all we need
- 5:12:19to give the password. Okay. So I'm just
- 5:12:21going to give my password here.
- 5:12:25Okay. I'll do always allow. And now what
- 5:12:28we're going to do right. So you can see
- 5:12:29that we have the servers. So here first
- 5:12:32of all we need to connect. So here and
- 5:12:35this is where you have to give your
- 5:12:36password that you have given. So in my
- 5:12:38case I've given what admin. So I'm just
- 5:12:40going to give this one admin and then
- 5:12:42I'm do save password and I'll do okay.
- 5:12:46And now you see that this will be now
- 5:12:47connected. Okay. And you can just open
- 5:12:49this. And this is where you are going to
- 5:12:53see all the things visually. Okay. But
- 5:12:57this is what I will suggest not to focus
- 5:13:00too much at the beginning. Why? The
- 5:13:03reason is when you have something that
- 5:13:05you can learn visually, you will be able
- 5:13:07to understand once you know the uh
- 5:13:10concepts and the basics very very
- 5:13:12clearly. So what I mean by this? So here
- 5:13:14you'll see that if I go to now
- 5:13:16databases, you'll be see you are going
- 5:13:18to see I think this is little slow.
- 5:13:20You'll see that we have one uh default
- 5:13:22database here. Now what you can do right
- 5:13:24you'll be able to create database from
- 5:13:26here. Let's say if I do right click this
- 5:13:29is very slow. Okay. So you can do create
- 5:13:31database and all those things. What we
- 5:13:33are going to do right each and
- 5:13:35everything that how we are going to
- 5:13:37create a table, how we are going to
- 5:13:39insert data, how we are going to read
- 5:13:41our data, how we are going to update our
- 5:13:43data, all those things via command line.
- 5:13:46So for this one we have something called
- 5:13:48psql. Okay. So why I'm actually focusing
- 5:13:51too much on this particular part because
- 5:13:53here if I just focus too much on
- 5:13:55learning all those things definitely we
- 5:13:57can also do that. But why I'm suggesting
- 5:14:00you if you're a complete beginner that
- 5:14:02try to master command line things a much
- 5:14:05better way. So what I'm telling let's
- 5:14:07say you are working in a particular
- 5:14:09project where they are using posgress
- 5:14:11and someone ask you okay give me this
- 5:14:13particular data. So now you don't want
- 5:14:15to go to your DB so you can go to your
- 5:14:17DB and then you can go to your tables
- 5:14:19you can go to rows and you can see the
- 5:14:20data that is fine. But if you are a
- 5:14:22master in Postgress SQL, what you can
- 5:14:24do? You can just simply run a particular
- 5:14:26query and you'll be able to get all the
- 5:14:27data. You're getting you're getting what
- 5:14:29I mean. So this is where you have to
- 5:14:31focus. So these things we are going to
- 5:14:33discuss later. For now, you have to make
- 5:14:35your posgress foundation very very
- 5:14:38strong. And this is exactly how I have
- 5:14:40learned the same thing that how how I'm
- 5:14:42teaching here. You believe me or not?
- 5:14:44Okay. So what we are going to do right
- 5:14:46first of all what we need to do right, I
- 5:14:48already created one blank folder here.
- 5:14:50You can see that I don't have anything
- 5:14:51else. Now we are going to follow a
- 5:14:54certain process and the folder structure
- 5:14:56so that sequentially you can learn all
- 5:14:58those concepts line by line. So what I
- 5:15:01mean by this? So first of all what
- 5:15:02you're going to do right I'm going to
- 5:15:04create two folder here. So I will
- 5:15:06suggest that you also follow the exact
- 5:15:08same process. So one will be part one
- 5:15:10because I've given lot of uh time uh
- 5:15:12deciding all those things right. So
- 5:15:14don't think this is like very random
- 5:15:15that I'm creating. I I will give lot of
- 5:15:17time creating all those curriculum in
- 5:15:19what sequence we are going to learn so
- 5:15:21that it will be easy for you to
- 5:15:22understand. So here I'm going to give
- 5:15:24part one and then there will be part
- 5:15:26two. Okay. So part one we are going to
- 5:15:29learn very very basic things. For
- 5:15:30example, how you can create database,
- 5:15:32how you're going to create schema, how
- 5:15:34you can create your table, what are the
- 5:15:36data types, what are the constants,
- 5:15:38primary keys, all those things. In part
- 5:15:40two we are going to go into little bit
- 5:15:42advanced level. Okay. So you're going to
- 5:15:44learn all the SQL uh concepts for
- 5:15:47example how you can insert a single row
- 5:15:49how you're going to insert multiple rows
- 5:15:51okay you are getting right and then we
- 5:15:53are going to have part three but that we
- 5:15:54are going to do later. So now inside
- 5:15:56this part one what we are going to do we
- 5:15:59will be creating each and every SQL file
- 5:16:01with a sequence. So what I mean by this
- 5:16:03I'm just going to do 01 and then I'm
- 5:16:05going to do first database or DB or
- 5:16:08something. So that mean this will be
- 5:16:09your initial thing that you need to
- 5:16:11understand. I hope this is clear. So
- 5:16:13this will be our folder structure that
- 5:16:14we are going to follow. Now before doing
- 5:16:16that so let's understand very very basic
- 5:16:18things like although I mentioned that we
- 5:16:20are going to not going to cover some of
- 5:16:21the concept but what actually posgress
- 5:16:23is. So posgress is a database system
- 5:16:25right now why uh so now if you're a
- 5:16:29complete beginner what actually a
- 5:16:30database system now see when we have
- 5:16:32some real application we need to store
- 5:16:34our user information let's say our
- 5:16:36product information or the app that you
- 5:16:38are creating based on that posgress is
- 5:16:40going to we are going to use to store
- 5:16:43this kind of application data in a
- 5:16:45structured way okay so in a structured
- 5:16:47way why this is very popular and it's
- 5:16:50extremely popular and the reason is
- 5:16:52because it's it's actually give the
- 5:16:54developers right that we are developers
- 5:16:56we are going to develop so many
- 5:16:58advantages that other DVs actually lacks
- 5:17:01for example first of all will be
- 5:17:03reliability okay now first of all when
- 5:17:06you're creating a products and
- 5:17:08application you can't lose your data
- 5:17:10very very easily right and that will be
- 5:17:12a disaster for your application for
- 5:17:15example let's say I'll give a practical
- 5:17:17example that if your user you have a B2C
- 5:17:20application and your user is placing an
- 5:17:23pays real money okay and let's say or
- 5:17:26maybe updating their profile so what we
- 5:17:28need to do your DB should be responsible
- 5:17:31for updating all those data properly and
- 5:17:34posgress SQL is trusted for this kind of
- 5:17:37data handling like it's extremely
- 5:17:40popular second it's give you the SQL
- 5:17:42support right so possql is usually use
- 5:17:45SQL what uh SQL means structured query
- 5:17:48language right you can also search all
- 5:17:50of these in Google also right So what
- 5:17:52actually structured query language is?
- 5:17:54So this is the language that we talk to
- 5:17:56relational databases. Okay. So that we
- 5:17:59are going to discuss later that what are
- 5:18:01the commands and everything we have and
- 5:18:03one very very popular advantages of
- 5:18:06posgress SQL will be and that is the
- 5:18:08relationship between your data right so
- 5:18:11what will happen right whenever you have
- 5:18:12a backend application your data the real
- 5:18:15data will be connected for example one
- 5:18:18user can create multiple post one order
- 5:18:21belongs to one customer so one order
- 5:18:23cannot belong to multiple customer these
- 5:18:26things are very basic concepts right so
- 5:18:28this is where post SQL is extremely
- 5:18:31strong I repeat extremely strong at
- 5:18:34storing and querying all those relations
- 5:18:37data okay so these things will be the
- 5:18:40advantages some of the very advanced
- 5:18:41features that uh your posgress is going
- 5:18:44to give you that we are going to not
- 5:18:46going to cover that much here let's say
- 5:18:48we have indexes for f queries we have
- 5:18:50transactions we have full text search
- 5:18:52lot of things views triggers stored
- 5:18:55function extension if I'm getting good
- 5:18:57response from this particular video I'm
- 5:18:59going to create one more part two. Okay.
- 5:19:01[snorts] So this will be your postgra
- 5:19:04square with the very definition. Right?
- 5:19:05Now one thing you need to understand and
- 5:19:07that is one of the most important
- 5:19:08concept that you can get. You can also
- 5:19:10note it down that what is the relation
- 5:19:13sorry what is the non-re what is the
- 5:19:15differences between a relational and
- 5:19:17nonreational DB. Okay. Now if you're a
- 5:19:21student you have maybe heard this
- 5:19:23particular term that SQL database NoSQL
- 5:19:26database relational database non
- 5:19:28relational database something like that
- 5:19:29right so what is the actual definition
- 5:19:31of a relational database so relational
- 5:19:33databases actually stores data in a
- 5:19:36table format okay so I'm just going to
- 5:19:39let's do one readme
- 5:19:42so here I'm just going to do that
- 5:19:44relational database
- 5:19:47relational db is going to store the data
- 5:19:49in what? In tables format or in tables,
- 5:19:52right? And these tables these tables can
- 5:19:55be connected using relationships.
- 5:20:00Okay,
- 5:20:02let's say you have a users table,
- 5:20:05you have a post table, you have a
- 5:20:07comments table or whatever blah blah.
- 5:20:11Okay.
- 5:20:14So here what situation can happen that a
- 5:20:16particular post is belong to one user.
- 5:20:19So one post belong belongs to one user
- 5:20:24something like this. Okay.
- 5:20:27And let's say one comment
- 5:20:30belong to one particular post something
- 5:20:34like this. And this connection that we
- 5:20:36have this is called relationship. Okay.
- 5:20:39So now uh this is one more thing we can
- 5:20:42mention that one user okay this is also
- 5:20:44important that one user
- 5:20:47can have many post
- 5:20:50right so this is called one to many
- 5:20:52relationship that we are going to
- 5:20:53discuss later so this will be relational
- 5:20:55DB non- relational DB will be it
- 5:20:58actually doesn't organize the data using
- 5:21:00connected tables right right so we'll do
- 5:21:02non relational
- 5:21:05uh non relations DB
- 5:21:09So it's normally doesn't
- 5:21:13uh organize
- 5:21:17data using connected tables.
- 5:21:22Okay. Now here what actually these kind
- 5:21:25of DVs use? They use documents. So we
- 5:21:27have what MongoDB, right? Or it also
- 5:21:30uses key value pairs. Key value
- 5:21:34pairs. So this will be the very basic
- 5:21:37definition between a relational and non
- 5:21:41relational DB. Okay. Now this is another
- 5:21:45very important interview questions that
- 5:21:46when you're going to use relational DB
- 5:21:48or when relational DBs are a good
- 5:21:51choice. So you have to mention that we
- 5:21:53we are going to use postgress SQL when
- 5:21:55we have our data that has clear
- 5:21:58structure that has proper relationship.
- 5:22:01Right? You also need to implement
- 5:22:03transactions. you need joins right also
- 5:22:07you also need strong validation at the
- 5:22:09database level okay so these things each
- 5:22:12and everything that I'm mentioning very
- 5:22:14very important so first of all will be
- 5:22:15data has clear structure so I'm just
- 5:22:17going to write in short second will be
- 5:22:19data has relationships
- 5:22:22relationships
- 5:22:24then maybe let's say you need to
- 5:22:26implement transactions
- 5:22:29also you need joins and as I already
- 5:22:32mentioned that you also need strong
- 5:22:33valid validation strong validation at DV
- 5:22:37level. Now example will be let's say
- 5:22:39you're creating a banking application
- 5:22:44all right and or maybe let's say you're
- 5:22:46creating a e-commerce something like
- 5:22:48this or let's say you're creating a CRM
- 5:22:50application so here you can use
- 5:22:53relational non-reational you're going to
- 5:22:56use when your data save or data is going
- 5:22:59to change very often right so
- 5:23:02non-reational will be when your data
- 5:23:04changes
- 5:23:06very often. Okay. Documents that you
- 5:23:10have, I already mentioned that
- 5:23:11non-relations DB is going to store your
- 5:23:13documents in sorry store your data in
- 5:23:15documents. So when your docs or
- 5:23:18documents are independent
- 5:23:20so not that much relations are there
- 5:23:22okay you don't need much joins
- 5:23:27right so in this case you have to use or
- 5:23:30you can use not you have to you can use
- 5:23:32non-reational DB. Now why am I
- 5:23:34explaining all of this? Because this is
- 5:23:36actually very very important. This is so
- 5:23:38important right? I can't even explain.
- 5:23:40You go to any company you'll see that
- 5:23:41whenever they're going to start a new
- 5:23:43product there will be a lot of
- 5:23:45discussions with the product business or
- 5:23:47leadership whatever that they what DV
- 5:23:50they're going to use it's extremely
- 5:23:52important why because once you start the
- 5:23:56product let's say you started one
- 5:23:57particular DB and that is a wrong choice
- 5:23:59it can be huge problem because then
- 5:24:01again you need to migrate and there can
- 5:24:03be lot of things right developer cost
- 5:24:06timing and all those things are matter
- 5:24:07so that is the reason these things are
- 5:24:09very important Now if I want to sum up
- 5:24:11that when you are going to implement any
- 5:24:13kind of project right you have to first
- 5:24:15see the requirement but in overall
- 5:24:17posgress SQL is a very very strong very
- 5:24:20strong DB choice okay so this is what I
- 5:24:22wanted to mention so very basic concepts
- 5:24:25so with this now from the next part what
- 5:24:27we're going to mention as I already
- 5:24:30told that for every concepts we are
- 5:24:32going to create one one file and then we
- 5:24:33are going to see in the terminal the
- 5:24:35same thing I'm also going to show you in
- 5:24:36the PG admin but we'll focus more on the
- 5:24:38SQL queries that will be your main
- 5:24:40object. So let's move on to the next
- 5:24:42one. All right. So now we'll start our
- 5:24:44journey of learning Postgress SQL
- 5:24:47concept wise but practically. Before
- 5:24:50that as I already mentioned this
- 5:24:51particular term in case you are not
- 5:24:53aware and that is psql which is the
- 5:24:55official terminal based command line
- 5:24:57interface for postal SQL right. So what
- 5:25:00we are going to uh why we need this or
- 5:25:03when we are going to use this when we
- 5:25:05need to
- 5:25:07run our SQL queries. You can see that it
- 5:25:09allows uh to interactively type SSQL
- 5:25:12queries, issue them to the database
- 5:25:15server and you can also view the result
- 5:25:17directly in your terminal and this is
- 5:25:19the thing I was talking about that
- 5:25:20definitely whenever you are going to
- 5:25:22work with your DB you can definitely see
- 5:25:24the result in your DV right but when you
- 5:25:27can run any kind of commands and then
- 5:25:29you'll be able to get the result in the
- 5:25:31command line. So that means what you
- 5:25:32know your concepts very very well and
- 5:25:35that is very important thing that we are
- 5:25:37going to at least learn in this
- 5:25:39particular video. So first of all let's
- 5:25:41start with I think we have done enough
- 5:25:44talking. So let's start with first file
- 5:25:46which will be 01 first databaseql.
- 5:25:52Okay. So this will be a first file. So
- 5:25:54first what we are going to do it. So
- 5:25:56first we're going to understand that how
- 5:25:58you can create your first DV. Now here
- 5:26:02what I'm going to do right. So first I'm
- 5:26:04just going to write here that okay
- 5:26:06create and then I'm just going to do
- 5:26:08here database and let's give this a name
- 5:26:11as Postgress
- 5:26:13SQL. Okay. So we have post no let's do
- 5:26:16postgra SQL
- 5:26:19part one. So this is my DB name. So what
- 5:26:23actually this command is doing here
- 5:26:25right? So this thing is doing. So this
- 5:26:27create a database is going to create a
- 5:26:29new DB. So this is going to create.
- 5:26:34So this is going to create new DB right
- 5:26:38where inside your postgrade I'm going to
- 5:26:42write insert postgrade server.
- 5:26:45So this is how you can create your first
- 5:26:47DB. So now one thing you need to
- 5:26:50understand that in postg server you can
- 5:26:53have multiple databases right. For the
- 5:26:55part one, what are we going to do? We're
- 5:26:57going to use one single database so that
- 5:26:59we can understand and all the things
- 5:27:00will be in sync. Now when we going to
- 5:27:04run this particular file, what will
- 5:27:06happen? So this file once it will run
- 5:27:09successfully, it is going to connect to
- 5:27:10your new DB, right? And then what we can
- 5:27:14do from our psql command line, we can
- 5:27:16actually connect directly to this
- 5:27:19particular DB. But before that I'm just
- 5:27:21going to give you one more uh command
- 5:27:23here that you need to know. But remember
- 5:27:25I'm going to write it explicitly that
- 5:27:27this is actually not needed and you
- 5:27:28should never do it in your real products
- 5:27:30and application and that is first I'm
- 5:27:32going to do the drop database
- 5:27:35if exists
- 5:27:37and then I'm just going to give this
- 5:27:39name posgressql part one. So what
- 5:27:41actually we are doing here? So you're
- 5:27:43simply telling that you are going to
- 5:27:46drop this particular DB if it is already
- 5:27:48present. drop database is going to
- 5:27:50remove this database completely and I'm
- 5:27:53just going to write it here that you
- 5:27:54should you should never
- 5:27:58do it in prod app and this is so I'm
- 5:28:04just going to write it here so this is
- 5:28:07only for
- 5:28:10learning okay so two things we have
- 5:28:12learned one is how you can drop a
- 5:28:14database and how you can create a new
- 5:28:16database okay so now let's see how we
- 5:28:18are going to connect to this particular
- 5:28:20and how we are going to run this
- 5:28:22particular file that is also important
- 5:28:23as I already mentioned that we are going
- 5:28:25to use command line and for this one you
- 5:28:27can use a individual command line but
- 5:28:29I'm going to use VS code only because it
- 5:28:31will be easy for us to understand
- 5:28:33so first of all I'm going to run this
- 5:28:35particular command okay and I'm going to
- 5:28:37explain that what each and everything is
- 5:28:38doing here this is each and everything
- 5:28:40see we are going to do from this
- 5:28:42particular point you need to understand
- 5:28:44everything you can't memorize these
- 5:28:46things in case you're not able to do it
- 5:28:48will be very difficult for you to
- 5:28:50understand when you will actually build
- 5:28:52your backend applications. So here I'm
- 5:28:55just going to do psql u and I'm just
- 5:28:58going to do this one as posgress
- 5:29:00space - d and I'm just going to do
- 5:29:03posgress and here I'm just going to do
- 5:29:05hyphen f and then I will go to my part
- 5:29:08one which is this particular folder part
- 5:29:10one slash and then I'm just going to
- 5:29:12give this file name. So I'm just going
- 5:29:14to copy this here and then I'm just
- 5:29:16going to paste it. So what actually this
- 5:29:18is doing here? So first our psql is our
- 5:29:21posgressql command line tool hyphen u
- 5:29:24pososegr is nothing but we are simply
- 5:29:26telling that we are logging as our user
- 5:29:28which is posgress hyphen d posgress
- 5:29:31means we are going to connect to our
- 5:29:33database posgress and hyphen af this
- 5:29:36particular means we are going to run
- 5:29:38this commands from this particular file
- 5:29:40as simple as that okay so in sum up you
- 5:29:43it's telling that use psql login as
- 5:29:46posgressql user connect to your posgress
- 5:29:49database and run run this particular SQL
- 5:29:51file. That's it. And we going to do
- 5:29:53enter. You have to give a password and
- 5:29:55I'm just going to give admin enter.
- 5:29:59And you can see that what it's saying.
- 5:30:01So this is fine. So this doesn't exist.
- 5:30:04So it's going to skip which is fine.
- 5:30:05Drop database and create database. So
- 5:30:07that means this is actually created. So
- 5:30:09now if I just go here, right? And let me
- 5:30:11just I don't know why this is very slow.
- 5:30:14So let me refresh this.
- 5:30:17I think I get this particular thing in
- 5:30:20other places also that this is uh little
- 5:30:23slow this issue also reported by other
- 5:30:25users also and you can see that we have
- 5:30:27this postgrad SQL part one and I'm just
- 5:30:30going to do right click not right click
- 5:30:32I think let's open this and you're going
- 5:30:35to see all the details and everything
- 5:30:36schemas and everything but for now let's
- 5:30:39leave it here so that means now you have
- 5:30:41your DB here so what I will suggest let
- 5:30:44me just move this one here So what I'm
- 5:30:47actually suggesting that try to
- 5:30:49understand each and every command very
- 5:30:51very seriously because this is what you
- 5:30:54need to work in your company also when
- 5:30:56you'll be working as a backend developer
- 5:30:58you have to work with your DB you need
- 5:31:00to know each and everything like like
- 5:31:02such a way so that if someone ask you
- 5:31:04okay give me this particular command SQL
- 5:31:06query or everything you should be able
- 5:31:08to do it like this right so first of all
- 5:31:11we learn that how you can create a new
- 5:31:13DB and how we are going to run this okay
- 5:31:15Now I'm going to show you that how you
- 5:31:17can run your psql queries from your
- 5:31:20terminal. So first I'm just going to
- 5:31:22clear this. So first let's do here psql
- 5:31:25- u. So which will be our user which is
- 5:31:27posgress and I'll do hyphen d and this
- 5:31:31is our db and then I'm just going to do
- 5:31:34enter and you'll see that it's asking
- 5:31:35for this password every time. Okay. So
- 5:31:37you have to give the password every
- 5:31:38time. So what we going to do right? So
- 5:31:40let's cancel this and I'm just going to
- 5:31:44add this password here.
- 5:31:48Okay. So now let's clear once again and
- 5:31:50then we are going to just run once again
- 5:31:54here and you can see that now this is
- 5:31:56connected. So now I'm going to show you
- 5:31:58that some of the things that you can
- 5:31:59directly check from here. So first of
- 5:32:02all let's say you want to see which
- 5:32:05current database that you are using. So
- 5:32:07here what you can do right you're going
- 5:32:08to do select and you're going to do
- 5:32:10current underscore and you have to do
- 5:32:12database.
- 5:32:15All right and then you'll do enter and
- 5:32:17you can see that this is our current
- 5:32:18database. And now remember this is we
- 5:32:21are actually writing inside your psql
- 5:32:23command line. Okay. Now you want to see
- 5:32:26the current user. So you're going to do
- 5:32:28select and you'll do current user.
- 5:32:31Enter. This is the current user which is
- 5:32:33posgress. Okay. Now you can also see
- 5:32:37which version of Postgress SQL you're
- 5:32:39using. So I'm just going to do select
- 5:32:41and here if I'm not wrong it will be
- 5:32:44version and this will be the this is
- 5:32:46going to give you the version. Okay you
- 5:32:48can see that this is the version that we
- 5:32:49are using right. So now let's say you
- 5:32:52want to see all your databases that how
- 5:32:55many databases I have. So you have to
- 5:32:56give slash I don't know which what but
- 5:32:59this particular uh icon called u. So I
- 5:33:03think this is called back slash I think
- 5:33:05and then this will be l and here you're
- 5:33:08going to get all these okay all the
- 5:33:11databases. Now let's say you want to see
- 5:33:13all your tables. So for tables you have
- 5:33:15to do dt and here you can see that we
- 5:33:17don't have any tables for now which
- 5:33:19makes sense. Now you want to exit your
- 5:33:21psql command line. So you can also type
- 5:33:24here exit directly. So let's see if I do
- 5:33:26exit it will be it will be uh it will
- 5:33:29exit. Or else you can also do like this.
- 5:33:32You can directly do uh slash and then
- 5:33:35you have to do Q. So this is going to
- 5:33:37also do the exit. Okay. So both the
- 5:33:39things are very important. So you can
- 5:33:42use exit or this particular / Q. So this
- 5:33:45will be the very first thing basic
- 5:33:46things you have learned. So now let's
- 5:33:48move on to the next one which will be
- 5:33:50how we are going to create our schema.
- 5:33:52So here I'm just going to create one
- 5:33:54more file and let's do this one as 02
- 5:33:57and I'm just going to do this one as
- 5:33:59first schema.sql.
- 5:34:02Okay, let's bring it here. So first of
- 5:34:06all, how we are going to create your
- 5:34:08first schema? What actually schema? Now
- 5:34:11schema is like a folder inside your
- 5:34:13database. Okay, now there will be some
- 5:34:16default schema it's going to create,
- 5:34:18right? So you see that you have the
- 5:34:19schema. Let's expand this.
- 5:34:23See that there is a public schema that
- 5:34:25will be created and inside this also
- 5:34:27you're going to have the tables. Okay.
- 5:34:29So this is your public or default schema
- 5:34:31that postsql always has. So now let's
- 5:34:34say we want to create our own schema. So
- 5:34:37we going to have our DB. So it's like
- 5:34:39this. So I'm just going to give a
- 5:34:41comment. Okay. So here I'm just going to
- 5:34:44have the DB. DB is going to have the
- 5:34:46schema. Schema is going to have the
- 5:34:49table. table is going to have the rows
- 5:34:52something like that. So now I want to
- 5:34:53create my own schema. So how I can do
- 5:34:56that and that is very very simple. So
- 5:34:57I'm just going to do create schema and
- 5:35:00then I'm just going to do if not exists
- 5:35:04and then you have to give the name. So
- 5:35:05I'm just going to do basic something
- 5:35:06like this. Right? So create schema is
- 5:35:08going to create a name space for your
- 5:35:10table and your other database objects.
- 5:35:13What actually if not exist means? So
- 5:35:15this is going to prevent an error. Very
- 5:35:17very important. If this schema is
- 5:35:20already created, so this is going to
- 5:35:22this if not exists
- 5:35:26is going to
- 5:35:29prevent an error, right? Uh if the
- 5:35:33schema
- 5:35:36schema
- 5:35:39is already
- 5:35:41created. Okay. So this is how you can
- 5:35:44create a schema. All right. So this is
- 5:35:46how you can create a schema. Now here
- 5:35:48I'm also going to show you that how you
- 5:35:49can create a extension and why actually
- 5:35:51you need it. So I'm just going to do
- 5:35:52here create extension
- 5:35:55if not exist and here I'm just going to
- 5:35:59use pg crypto.
- 5:36:02Okay. So what actually this extension is
- 5:36:04going to do? So in later part we are
- 5:36:06going to use uh something called get
- 5:36:08random UID which will help us to create
- 5:36:12UID primary keys and that is very
- 5:36:14important because in Postgress SQL what
- 5:36:16we can do right we can directly create
- 5:36:18or generate UID. So here this particular
- 5:36:21extension is needed because this is
- 5:36:23going to help us to create that
- 5:36:25particular UID. Okay for later we are
- 5:36:28going to discuss uh this particular
- 5:36:29particular thing in details. So now what
- 5:36:31I'm going to do right so this is going
- 5:36:33to create the schema and this is going
- 5:36:35to create the extension right I think
- 5:36:37extension spelling is also correct all
- 5:36:40right now what I'm going to do right now
- 5:36:42this is where I wanted to explain one
- 5:36:44more thing which is very important so
- 5:36:45here what I'm going to do right I'm
- 5:36:47going to write a query okay so here I'm
- 5:36:49just going to write a query now this
- 5:36:51query I can directly write from my psql
- 5:36:54command line also but now you may ask me
- 5:36:56that why I'm writing this one here the
- 5:36:58reason is this is for the teaching
- 5:37:00purpose this This is not mandatory. This
- 5:37:03is optional. Why I'm doing this? Because
- 5:37:05when you are going to download this
- 5:37:06source code, you will be able to
- 5:37:07directly see this query or you can copy
- 5:37:09and you can run. Okay. So that is the
- 5:37:11reason I'm going to follow this
- 5:37:13particular uh process throughout this
- 5:37:16video, not for this particular file. So
- 5:37:18now let's say what I need to do, right?
- 5:37:20I want to write a particular query which
- 5:37:22is going to give me all the schemas that
- 5:37:24are available in my current DB. Okay. So
- 5:37:27to do that, what we're going to do,
- 5:37:28right? We're going to use a select and
- 5:37:30here I'm just going to use a schema name
- 5:37:33and then I'm just going to do from and
- 5:37:35this we have something called
- 5:37:36information
- 5:37:38schema
- 5:37:40schema schemata. So what actually this
- 5:37:43is doing now this information doss
- 5:37:44schema dos schemata is a system view.
- 5:37:47What actually system view? So system
- 5:37:48view is a built-in view that is going to
- 5:37:51give you the information about your
- 5:37:52database structure. For example, let's
- 5:37:54say you want to see all your database
- 5:37:56schemas that how many schemas are
- 5:37:58available. So in that case, we can use
- 5:38:00this. And here I'm also going to do
- 5:38:01order Y by for now. Let's skip this. We
- 5:38:04are going to have a separate section on
- 5:38:06what actually is order Y. So I'm just
- 5:38:07going to do order Y of schema name.
- 5:38:10That's it. So now what I can do right, I
- 5:38:12can simply directly run this particular
- 5:38:14file which is going to create a schema
- 5:38:17going to create this extension and at
- 5:38:19the end it's going to run this
- 5:38:20particular query. All right. So now
- 5:38:22let's run this file and what we can do
- 5:38:24right. So let me just copy this
- 5:38:28and then we'll paste it here. So what we
- 5:38:30are doing so loopsql u posgress d this
- 5:38:34is the db that we have created in the
- 5:38:36first uh uh file and part one/ Now we
- 5:38:40need to run this file. Okay. So I'm just
- 5:38:42going to copy this and then let's paste
- 5:38:45it here.
- 5:38:47Enter. And you can see that now we have
- 5:38:50the schema is created extension is
- 5:38:52created and you are getting so many
- 5:38:54schemas. Okay. Now this might be a
- 5:38:55little confusing that from where these
- 5:38:57are coming.
- 5:38:59First one so basic schema is your custom
- 5:39:01schema that you have created. What is
- 5:39:03the information schema? This is the
- 5:39:05posgress standard metadata schema. Then
- 5:39:08you have the PG catalog. This is
- 5:39:09posgress internal system schema. PG
- 5:39:12toast. Uh if I'm not wrong this is
- 5:39:15internal storage schema for large
- 5:39:17values. Right? You can also check it
- 5:39:19once not exactly see what's so pg toast
- 5:39:22schema in to place you can just search
- 5:39:24it.
- 5:39:30Okay. So you can see that this is
- 5:39:31internal names we used for the oversiz
- 5:39:33attribute right so it's for large values
- 5:39:36and then you have the public which I
- 5:39:37already mentioned that every posgress is
- 5:39:39going to have a database is going to
- 5:39:41have a default schema uh which will be
- 5:39:43present in every database. Right now why
- 5:39:46this is coming up because in the query
- 5:39:49I'm actually getting all my schemas at
- 5:39:51once. Right? And this information schema
- 5:39:53dots schemata is going to give me all
- 5:39:55the schemas that are available in my uh
- 5:39:57database. All right. So now uh if you go
- 5:40:01here right so so this is our uh DB. So
- 5:40:03now let me just refresh this.
- 5:40:07This is very slow man. In Windows it is
- 5:40:09first. I don't know. I think that is the
- 5:40:11reason so many issues I think they have
- 5:40:13reported on this. I don't know why this
- 5:40:15is this much slow. Even I'm using the
- 5:40:17latest version of Mac but still it is
- 5:40:19very slow. So if I go to schemas and
- 5:40:22let's see what we getting here. So you
- 5:40:24can see that we are already getting two
- 5:40:26schema and you can see that we have the
- 5:40:27basic schema. Fantastic. So that means
- 5:40:29this is now completed. So this is how
- 5:40:31you can create schemas and also
- 5:40:33extension. Now moving to the next one
- 5:40:35which will be very very important. So
- 5:40:37for now what you're going to do right
- 5:40:38I'm just going to do 03
- 5:40:41and let's do this one as first table
- 5:40:44dossql. So now we are going to
- 5:40:46understand that how you can create
- 5:40:47table. Okay. So first of all what
- 5:40:49actually table? So table is going to
- 5:40:51store your data right or store one type
- 5:40:55of data. So here let's create a simple
- 5:40:58students table. So now what we going to
- 5:41:00do right your table is going to live
- 5:41:02inside your schema. And now we have
- 5:41:04created a basic schema here. So what I'm
- 5:41:06going to do right inside my this
- 5:41:08particular schema you wanted to create a
- 5:41:10table. So first of all I'm just going to
- 5:41:11do drop table. Now again don't do this
- 5:41:13in your real project. This is just for
- 5:41:15teaching purpose that this is how you
- 5:41:17can also drop table. Okay. So this will
- 5:41:19be drop table and here this will be if
- 5:41:22exists and I'm just going to do basics
- 5:41:25dot let's do students. Okay. So inside
- 5:41:29my basic schema you wanted to have a
- 5:41:30students table. Simple. And then I'm
- 5:41:32going to create a table. We'll do create
- 5:41:34table and I'm just going to do basics
- 5:41:36dot students and here now we have to
- 5:41:39have all our property. So let's start
- 5:41:41with the first one. First I need to have
- 5:41:42a id which will be serial and this will
- 5:41:46be my primary key. Now what actually we
- 5:41:48are doing here. Now this ID is your
- 5:41:51unique value. Right? So what this serial
- 5:41:54is going to do this is going to create
- 5:41:57an auto incremental
- 5:42:00or auto incrementing
- 5:42:02integer. Okay. So that means if I'm
- 5:42:05inserting one particular student with ID
- 5:42:071 from next time when I'm going to
- 5:42:09insert it it will be like 2, 3 like this
- 5:42:12and so on. Right? so on. So your four
- 5:42:15five something like this right? Primary
- 5:42:18key very very important very important
- 5:42:20concept that this simply means so
- 5:42:22primary key simply means
- 5:42:26that this
- 5:42:28column
- 5:42:29uniquely
- 5:42:32identifies
- 5:42:33each row. Okay. So this will be the
- 5:42:37first property that I wanted to have or
- 5:42:39column. Then I'm going to have a name
- 5:42:41property which will be text and I'm just
- 5:42:43going to make this one as not null. So
- 5:42:46what we are simply telling that this is
- 5:42:48my another property. So text is going to
- 5:42:52represent string data. Then I'm simply
- 5:42:56telling that not null
- 5:43:00uh means
- 5:43:02this column is required. Right? So that
- 5:43:06means
- 5:43:07postgress is going to
- 5:43:10reject if this name value
- 5:43:15is not present. As simple as that. Then
- 5:43:19I'm also going to take a email property.
- 5:43:21So I'm going to take a email which will
- 5:43:22be text. This also will be not null. And
- 5:43:25this will be a unique property. Okay?
- 5:43:27Because every student is going to have
- 5:43:29their unique email. All right? So unique
- 5:43:32means
- 5:43:34unique
- 5:43:37means that um no two student
- 5:43:45is going to have
- 5:43:48uh same email. Okay. Then let's take an
- 5:43:52one more property and then I'm just
- 5:43:54going to take a age property. This will
- 5:43:56be integer. I'm just going to take a
- 5:43:59check. So check is going to add a rule
- 5:44:01that every student age has to be greater
- 5:44:04than or equal 18. So this is one check I
- 5:44:06wanted to give and at the end I'm going
- 5:44:08to take a created at property. So do
- 5:44:11created add which will be timestamp
- 5:44:17and here I'm just going to do this one
- 5:44:18as default. Now so what we are doing
- 5:44:21here so simply telling that this time
- 5:44:23stamp is going to store your date and
- 5:44:26time format. So time stamp is going to
- 5:44:28store your date and time format. Default
- 5:44:33means if you don't give anything it's
- 5:44:34going to take by default.
- 5:44:37Default means if you don't give any
- 5:44:41value
- 5:44:44it will take by default.
- 5:44:49That's it. So now I think this is done.
- 5:44:52So first of all let's create this table
- 5:44:54and let's see if this is creating inside
- 5:44:56that particular schema or not. So first
- 5:44:59of all let's go here. I'm going to clear
- 5:45:03this and then I'm just going to run it
- 5:45:05here. So we are running inside this and
- 5:45:07then I'm just going to copy this. Let's
- 5:45:10do rename and then let's run it here.
- 5:45:16Enter. And you can see that it's created
- 5:45:19the table. So you can go here and let me
- 5:45:21just refresh this.
- 5:45:29So if I go inside this you'll be see
- 5:45:31that we'll be having the tables and
- 5:45:33inside this you're going to have your
- 5:45:34table one table which is my student
- 5:45:36table and you can see the columns and
- 5:45:38everything here right. So you can expand
- 5:45:40this and you can see all your properties
- 5:45:42that you have added. So ID, integer,
- 5:45:44name, email, age and created date. Okay.
- 5:45:47So that mean this is working fine. So
- 5:45:49now [clears throat] what we going to do
- 5:45:50right? So we need to insert some values
- 5:45:53so that we can see that whether we we
- 5:45:55have those values or not. Correct. But
- 5:45:57before that let's do one thing. Let's
- 5:45:59run some of the uh commands here. Okay.
- 5:46:03Inside psql so that you can understand
- 5:46:04what is happening here. So first of all
- 5:46:07inside this. So let's run this command.
- 5:46:10So I'm just going to open this. So do
- 5:46:13psql - u postgress and then I'm going to
- 5:46:17run this db. So inside this and then
- 5:46:19first of all I wanted to see my tables.
- 5:46:21I'll do /dt.
- 5:46:23Okay it's showing no tables.
- 5:46:27Oh I think we need to do /dt and we need
- 5:46:29to go to basics basics dot and then
- 5:46:33we'll do star.
- 5:46:35Okay. So you can see that we have to do
- 5:46:37list of tables. Schema basics name
- 5:46:39students. This is table owner is
- 5:46:41postgress. Fantastic. Now first of all
- 5:46:45before we'll do anything right we need
- 5:46:47to add some data so that you can check
- 5:46:49whether we have any student present or
- 5:46:51not. Correct? So how we are going to do
- 5:46:53that? So see this is what I was talking
- 5:46:56about that these queries that I'm
- 5:46:58writing here these direct queries I can
- 5:47:00directly run here also inside psql. The
- 5:47:03reason why I will be adding here so that
- 5:47:04when you're going to download the source
- 5:47:06code you can take the reference from
- 5:47:07here. I hope you're getting right. Don't
- 5:47:09get confused. These queries you don't
- 5:47:11have to write in the file. This is for
- 5:47:12the teaching purpose. So for now I'm
- 5:47:14just going to exit from here. Clear
- 5:47:16this. And then let's run the first uh
- 5:47:21query here. So I'm just going to do
- 5:47:23what? So I'm going to insert some uh
- 5:47:25data. So insert some data.
- 5:47:28So to do this one, we're going to do
- 5:47:30insert
- 5:47:32and insert we need to do into. So you
- 5:47:34have basics dot students. So this is the
- 5:47:38table and what are the things we need to
- 5:47:39insert. So first of all id will be
- 5:47:41automatically created then we have name
- 5:47:44comma we have email comma we have age
- 5:47:47these three things right. So this is the
- 5:47:50things we want to insert but we need to
- 5:47:52tell right okay what values we wanted to
- 5:47:54insert. So that means we need to give
- 5:47:56here values and these values is going to
- 5:47:59tell me that okay these things I wanted
- 5:48:01to insert. So I'm just going to add sum.
- 5:48:05So let's give one more email. So do
- 5:48:07thegmail.com
- 5:48:10and comma I'm just going to give here 55
- 5:48:13although I'm not 55 just I'm just
- 5:48:14giving. So here I'm just going to copy
- 5:48:16this paste it here. Let's do here John
- 5:48:20and I'll do this one as John and then
- 5:48:23let's do 23. Okay. And we need to give
- 5:48:27this one. So this will be the first uh
- 5:48:30data that I wanted to insert. So now
- 5:48:33what I'm going to do right so let's run
- 5:48:34one second and we'll see that what is
- 5:48:36happening here. Okay. So first of all
- 5:48:38I'm going to run this file again and see
- 5:48:40that we have insert something is
- 5:48:42happening here but we don't know what
- 5:48:43actually happened. So first we need to
- 5:48:45see that whether we have all this data
- 5:48:48present inside our table or not. So
- 5:48:50first I'm going to go inside this and
- 5:48:52then I'm what I'm going to do right. So
- 5:48:54I'm just going to do select
- 5:48:57star and I'm just going to do from. So I
- 5:48:59have to go to basics
- 5:49:02dot students. So it's simply telling
- 5:49:05that select is going to read your data
- 5:49:07from a table. This star means we need
- 5:49:10all the columns. Okay. So this is what
- 5:49:12you need to enter. And you can see that
- 5:49:14we have two data. ID one sum email this
- 5:49:18created at John and this is the data.
- 5:49:20Fantastic. Now this is what I was
- 5:49:22talking about. The same query I can
- 5:49:24write inside this file also so that
- 5:49:27whenever you're going to run this file
- 5:49:28you'll be able to get the result
- 5:49:30immediately. Okay. So let's see if I
- 5:49:33just wanted to write it here. So
- 5:49:34something like select
- 5:49:37from and then you're going to do basics
- 5:49:39dot students it's going to do the exact
- 5:49:41same thing. Okay. So now I think this is
- 5:49:43done. So this is how what you're going
- 5:49:46to do. You're going to create your
- 5:49:47table. You can drop table using drop
- 5:49:49table command. So [snorts] create table
- 5:49:51you have to take all your properties
- 5:49:53that you're going to take and based on
- 5:49:54that if you want to insert some data you
- 5:49:56need to insert and then if you want to
- 5:49:59uh read the data you need to select and
- 5:50:01then you have to tell that okay from
- 5:50:03which place I wanted to read the data.
- 5:50:05So in our case this will be our basic
- 5:50:07schema dot students table. So with this
- 5:50:10now let's move on to the next one. All
- 5:50:12right. So now let's move on to the next
- 5:50:14one which will be going to understand
- 5:50:15some of the data types. Okay. So I'm
- 5:50:17just going to do 04 data types dots sql.
- 5:50:22So I think some of the data types we
- 5:50:23have already covered but still we need
- 5:50:25to understand some more. So first I'm
- 5:50:27just going to do drop table if exists
- 5:50:31and I'm just going to create one more.
- 5:50:33So I'm just going to do here basics dot
- 5:50:35let's give this one as maybe products
- 5:50:38basic. Okay. And then let's create the
- 5:50:40table. So we'll do create table and
- 5:50:44let's give this one as basics.
- 5:50:47dot product
- 5:50:50basic and then
- 5:50:52so we'll start with the first one which
- 5:50:54we've already covered. So this will be
- 5:50:56ID. So we're going to take serial and
- 5:50:58this will be primary
- 5:51:01key. Okay. Then next we are going to
- 5:51:04take our uh let's take a name property
- 5:51:07and here I'm just going to take one more
- 5:51:09uh prop uh thing here and that is called
- 5:51:11vare and I'm just going to give this one
- 5:51:13as 100. Let's do this one as not null.
- 5:51:16Now what actually we mean by this this v
- 5:51:19100 that we have taken here that means
- 5:51:20that we have a string with maximum
- 5:51:23length of 100 characters. Okay, so I'm
- 5:51:25just going to write here that we have a
- 5:51:27string that has a max length of 100
- 5:51:32characters. Okay, you need to use vare
- 5:51:36when you want to enforce a maximum
- 5:51:38length. Okay, so this is one more thing
- 5:51:41that you have learned. Now next thing uh
- 5:51:44let's take a description
- 5:51:47and for description I'm just going to
- 5:51:49take this one as text. Okay. So text is
- 5:51:52normal uh normally as string type. Uh
- 5:51:54what else? So let's take maybe a stock
- 5:51:56count. So I'm just going to take this
- 5:51:58one as stock and let's uh or let's do
- 5:52:00this one as stock. Maybe let's give
- 5:52:03stock only. And I'll do this one as
- 5:52:05integer and I'm just going to give a
- 5:52:07default value. Now we'll be able to also
- 5:52:08give a default value and this will be
- 5:52:10zero. Right? So integer is going to
- 5:52:13store a whole number right. So that
- 5:52:15means your stock should be a whole
- 5:52:17number 0 1 2 300 like that. It should
- 5:52:20not be 10.5 or 5.5. Okay, this is
- 5:52:23actually important when you're using
- 5:52:25integer. Then we have something called
- 5:52:27big int. So let's I'm just going to take
- 5:52:29a total property here or let's do total
- 5:52:31views or something like this. And I'm
- 5:52:33just going to give this one as big int.
- 5:52:35And then here also I'm just going to
- 5:52:36give this one as a default value which
- 5:52:39will be zero. Now what this big int
- 5:52:42means that we want to store larger whole
- 5:52:45number than integer. So we want to store
- 5:52:49larger whole number
- 5:52:53than integer right or we have integer
- 5:52:57something like this. So now in most of
- 5:53:00the times you'll see that your uh
- 5:53:02integer will be more than enough. Then
- 5:53:05we have one more property that you need
- 5:53:06to understand. Let's we have a price
- 5:53:08property and this is called numeric.
- 5:53:10Okay. So I'll do numeric and here I'm
- 5:53:12just going to give 10 2. Now what
- 5:53:14actually this is telling here this
- 5:53:16numeric is actually useful for your
- 5:53:17exact decimal values. So when you are
- 5:53:20going to use decimal values you want to
- 5:53:22take one minute. Okay. So you want to
- 5:53:26take numeric here. So this will be your
- 5:53:27exact decimal
- 5:53:30values. So what I mean by exact? So here
- 5:53:33uh we have 10. So 10 is what? 10 means
- 5:53:37your total digits. So 10 means total
- 5:53:41digits and then you have two. So two
- 5:53:44means here digits
- 5:53:47after the decimal point after the
- 5:53:51decimal
- 5:53:54point. Okay. So let's say you have 999
- 5:53:58something like this 999. Okay. So this
- 5:54:00is where you can take numeric and then
- 5:54:02at the end we have the boolean. So let's
- 5:54:05say I want to take is active something
- 5:54:07like this is active. So let's do is
- 5:54:09active and here I'm just going to take
- 5:54:11this one as boolean and here you can
- 5:54:13also take a default value and default
- 5:54:15value will be true. So this will be my
- 5:54:17default value and this will be a boolean
- 5:54:19property. Okay. So this is how you can
- 5:54:21create. Now here we don't have that much
- 5:54:24things to understand because these are
- 5:54:25very straightforward whatever things we
- 5:54:27have done. Now let's see want to let's
- 5:54:29write the queries also. So I'm just
- 5:54:30going to write the queries that I want
- 5:54:32to add some rows in this particular
- 5:54:36table. So how we can do that? We have
- 5:54:37already learned. So we can do insert and
- 5:54:39let's do into and we are going to do
- 5:54:42basics dot product basic and here let's
- 5:54:46say I wanted to render sorry enter name
- 5:54:50then we have what description then we
- 5:54:52have the stock then we have the total
- 5:54:54views right so I think we have taken the
- 5:54:56total views then we have the price and
- 5:54:59then at the end we have the is active so
- 5:55:02these all the things we want to render
- 5:55:03and what we want to render so we need to
- 5:55:05render the values correct And now let's
- 5:55:08say I wanted to add here. So let's add
- 5:55:12here something like this. Product one
- 5:55:15description. I'm just going to do
- 5:55:16product disk.
- 5:55:19Then we're going to give a uh stock
- 5:55:22count which will be what? So this will
- 5:55:23be whole number. Then we have the total
- 5:55:25views. So what is total views? Total
- 5:55:27views will begin. So you can take
- 5:55:29anything like 1200. Then you have the uh
- 5:55:32so after this so let's take the price.
- 5:55:35So price will be numeric right? So we
- 5:55:37can take something like 2 455.65
- 5:55:41and then we'll take true. Okay. So these
- 5:55:43values I want to insert. And let's say
- 5:55:46now at the end you want to also get the
- 5:55:48values here that how many uh um rows or
- 5:55:53products that we have added. So to do
- 5:55:54this what we can do simply we can do a
- 5:55:56select right and then we'll do start.
- 5:55:59Now remember I already mentioned all of
- 5:56:00this you can directly run for my psql
- 5:56:02command also that I've already showed
- 5:56:04you all of these but here I can I'm
- 5:56:06doing all of this at once. So I'll do
- 5:56:08basics dot product basics. So I'm simply
- 5:56:11telling that okay first create this
- 5:56:12table insert this particular data and
- 5:56:16then I'm going to get the data
- 5:56:17information. Now let's say uh you get
- 5:56:20one more requirement that you need to
- 5:56:22write a query where you are only going
- 5:56:24to get the active products. You have to
- 5:56:27get only the active products not all the
- 5:56:29products. Okay. So how you can do that?
- 5:56:32Now here you'll see that we have a
- 5:56:34boolean column that is is active. Right?
- 5:56:38This one what we can do that we can
- 5:56:39directly use in our filters. So
- 5:56:42something like this that where the field
- 5:56:44name which is is active is equal to true
- 5:56:46that is also valid. So let's see how we
- 5:56:49are going to do that. So here I'm simply
- 5:56:50going to do that. Okay. Select ID.
- 5:56:54Select name. Select price and select is
- 5:56:59active from. So you have to give from
- 5:57:02from where to place. So L basics.p
- 5:57:04product basics this is the table name
- 5:57:06and this is the condition. Then you're
- 5:57:08going to tell where is active is two.
- 5:57:11That's it. So this is going to give you
- 5:57:13all these data where is active will be
- 5:57:15true. Now let's see if this is working
- 5:57:17or not. Okay. So I'm just going to clear
- 5:57:19this and let's run again. And here. So
- 5:57:23first I'm just going to rename copy this
- 5:57:27and then I'm just going to paste it
- 5:57:28here.
- 5:57:32Okay, we are getting one error uh insert
- 5:57:34into basic. Okay, something I think we
- 5:57:37have missed here that is
- 5:57:41okay. We have to give this
- 5:57:44and also we have to give here also.
- 5:57:49So now let's clear this and let's run
- 5:57:52once again.
- 5:57:55All right. So now it's drop table create
- 5:57:57table one product is added and you can
- 5:58:00see that you're getting the information
- 5:58:02here that this is the one product. So
- 5:58:04this is from where you're getting this
- 5:58:05is the query and then you're getting
- 5:58:08only one product where it is is active
- 5:58:10because for now we have only one
- 5:58:11product. If you want you can add one
- 5:58:12more product that I will also give you
- 5:58:14as a home world. do one thing instead of
- 5:58:16adding one product here you can add
- 5:58:18multiple products right what you have to
- 5:58:21do in these values right instead of this
- 5:58:23you have to just give comma and then
- 5:58:25paste it one more time and then you just
- 5:58:26close with the semicolon you'll see that
- 5:58:28and for the second one just make this
- 5:58:30one to false and then when you're going
- 5:58:32to run this right so here you're still
- 5:58:34going to get only one result because
- 5:58:35that is only one product which will be
- 5:58:37true I hope you're getting right so try
- 5:58:38to do this one by yourself but for now
- 5:58:41for the basic data types this is more
- 5:58:43than enough so we have learned the ID
- 5:58:45one is the serial
- 5:58:47which is our primary key one is vare we
- 5:58:50have learned text integer begin numeric
- 5:58:52boolean so with this now let's move on
- 5:58:54to the next one where we are going to
- 5:58:56understand some of the other data types
- 5:58:57that is also very very important that
- 5:58:59you can use uh in some situations based
- 5:59:02on your project requirements so let's
- 5:59:03see how we going to do that in the next
- 5:59:05All right so moving to the next one so
- 5:59:06let's create the next file and this will
- 5:59:09be other
- 5:59:11data typesql
- 5:59:14Okay. So let's first create it. So I'm
- 5:59:16just going to do drop table
- 5:59:19if exists. And I'm just going to do
- 5:59:22again basics. This is our schema. And
- 5:59:24let's give this one as app events or
- 5:59:26something. And I'm just going to do
- 5:59:28create table. And I'll do basics dot app
- 5:59:32events. And here first of all what I
- 5:59:35will do right? So we are going to create
- 5:59:38our id as a uid. Okay. So how we are
- 5:59:41going to do that? So what actually first
- 5:59:43we need to understand what is UU ID
- 5:59:46means so it's a unique identifier type
- 5:59:48okay and here just like we have the
- 5:59:51serial right so this is not a simple 1 2
- 5:59:543 that you added three data and now you
- 5:59:56are adding the fourth data so the ID
- 5:59:58will become four so what will happen
- 6:00:00right it the values will look like a
- 6:00:02certain format okay and how we are going
- 6:00:04to create it so you have something
- 6:00:06called generate random UID okay which
- 6:00:09will going to create your random UID
- 6:00:12automatically. So how we are going to do
- 6:00:13that? So here we have ID and I'm just
- 6:00:16going to give this one as EU id and here
- 6:00:19so we're going to give this one primary
- 6:00:21key and you have to give a default and
- 6:00:23in this default you have to give this
- 6:00:24one as uh gen and then this will be
- 6:00:27random and then you're going to do UID
- 6:00:29and this is going to create a unique UID
- 6:00:32for this particular column. Okay. Now
- 6:00:35next let's take maybe we have a app
- 6:00:37events right? So let's take a event
- 6:00:39name. So I'll do event name and this one
- 6:00:42I'm not explaining. So this is text and
- 6:00:44I'm just going to do not null. And now
- 6:00:46what we're going to do right we'll
- 6:00:48understand one more new uh data type
- 6:00:50which is called JSON B. So what this
- 6:00:53JSON B means right now this is going to
- 6:00:56store your JSON data and this is going
- 6:00:58to store if I'm not wrong it's going to
- 6:01:00store in a binary format or binary
- 6:01:02optimized format. Okay. when you need to
- 6:01:05have some flexible structured metadata
- 6:01:07for that particular column, you should
- 6:01:08take this particular type. Okay. Now
- 6:01:10let's I'm just going to have a metadata
- 6:01:12and this metadata will be my type of
- 6:01:14JSON B and you're going to give a
- 6:01:16default value and in this default value
- 6:01:17I'm going to tell that okay this will be
- 6:01:19my default value for this particular
- 6:01:21metadata and this will be of type JSON
- 6:01:24B. I hope this is clear. Okay. So now
- 6:01:28next also I think time stamp we have
- 6:01:30already discussed still let's take it.
- 6:01:31So I'm just going to do create a and
- 6:01:33this will be a time stamp and I'm just
- 6:01:36going to give a default and this will be
- 6:01:38now. So which will be the current time
- 6:01:40and I think this is done. So now this is
- 6:01:42going to create our table. So this will
- 6:01:44be our this app events table. So if I
- 6:01:47save this and now let's go here. Let's
- 6:01:50run this. And now let's copy this file
- 6:01:54name first. we'll do slowly and then
- 6:02:01okay so it's created the table now what
- 6:02:03I'll do right so let's go here and then
- 6:02:07I'm just going to run this and also I'm
- 6:02:09just going to move this one here and
- 6:02:12then I'll be inside this and here what
- 6:02:14I'll do right so we have the DT and okay
- 6:02:18so we need to I think I always make this
- 6:02:20mistake we need to do DT / DT and we
- 6:02:24have to do basics because this is our
- 6:02:25schema dot start and this is going to
- 6:02:28give you all the uh table. So you can
- 6:02:30see that we have app events just now we
- 6:02:31have created product basics and the
- 6:02:34student and the same thing you can also
- 6:02:36check here also right if you go here and
- 6:02:38let's see if I refresh this I don't know
- 6:02:39why this is this much slow man it's very
- 6:02:41irritating see so if I refresh this
- 6:02:43right you'll be able to see all the
- 6:02:44updated tables you have app events and
- 6:02:46you can go here and you can explore all
- 6:02:48of this whatever columns that you have
- 6:02:50created okay id event name metadata
- 6:02:54created that is fine so now what I'm
- 6:02:56going to do right so first of all let's
- 6:02:58insert one of uh maybe let's insert two
- 6:03:02data here. So what I'm going to do
- 6:03:04simply here only I'm just going to do
- 6:03:05again we can do the same thing from here
- 6:03:07also I'm repeating the same same thing
- 6:03:09again and again the reason why I'm
- 6:03:11adding so that it will be easy for you
- 6:03:12to copy paste or else what you need to
- 6:03:14do right if I write all of these
- 6:03:16directly in command line not in this
- 6:03:18file so when you download the source
- 6:03:20code these queries will not be present
- 6:03:22you're getting uh what I'm explaining
- 6:03:24here so that is the reason I'm just
- 6:03:26writing all of this here so I'm just
- 6:03:28going to do insert into and I'm just
- 6:03:31going to do basics dot app events and
- 6:03:34here I'm just going to do the event name
- 6:03:35because we have the event name and we
- 6:03:37have to add the metadata correct so we
- 6:03:39have the metadata and here I'm just
- 6:03:41going to do values and let's add
- 6:03:44so here I'm just going to do maybe event
- 6:03:46right so maybe let's say sign up is one
- 6:03:48of the method and here now you have to
- 6:03:50add a JSON so this will be JSON property
- 6:03:53and maybe let's I'm just going to do
- 6:03:54sign up and this user sign up from uh
- 6:03:58Chrome browser something like this and
- 6:04:00so this will be browser and And this
- 6:04:02will be Chrome. Okay. So this will be
- 6:04:04your format. This is how we need to take
- 6:04:06it. Now let's see. I'm just going to
- 6:04:07copy this and then copy uh comma paste
- 6:04:11and then I'm just going to do sign in.
- 6:04:13And here I'm just going to do maybe
- 6:04:15browse and install browser. I'm just
- 6:04:17going to do username or whatever you
- 6:04:19want to take user and then I'm just
- 6:04:22going to take this one as someone. Okay,
- 6:04:24it can be anything. So this is now going
- 6:04:27to insert that into that particular uh
- 6:04:30app event table. So now to check it I'm
- 6:04:34just going to do a select query start
- 6:04:36and then I'm just going to do from and
- 6:04:38then I'll do basics do app event. This
- 6:04:40is the table name. So first let's do
- 6:04:42this and let's run it once again.
- 6:04:45So here I'm just going to run this and
- 6:04:48you can see that so we have created this
- 6:04:50inserted and you can see that we getting
- 6:04:52UID. This is the UID. This is the format
- 6:04:54that I'm talking about. This is the
- 6:04:56event name sign up and this is the
- 6:04:57metadata and then you have the time
- 6:04:59stamp. Okay. And this is the second one
- 6:05:02for sign in. So what we are
- 6:05:03understanding that whether it is
- 6:05:05creating the UID not it's actually
- 6:05:06successfully created this UID. Now what
- 6:05:09you can do right you can also query
- 6:05:11these JSON properties also or JSON field
- 6:05:14also. How? So now let's say uh maybe
- 6:05:17what I'm going to do right I have this
- 6:05:19browser. Correct? So now this browser I
- 6:05:22wanted to check that what I wanted to do
- 6:05:25here that I want to open my JSON B
- 6:05:27column named metadata right so we have
- 6:05:30given the metadata right and now here
- 6:05:32let's I wanted to get the value of this
- 6:05:34browser this browser as text so how I'm
- 6:05:38going to do that to do this what you
- 6:05:40have to do right so you have to do
- 6:05:41select and then I'm just going to give
- 6:05:43an event name
- 6:05:46now this is the important part so you
- 6:05:47have to give metadata and here you're
- 6:05:49going to give like this. So now this
- 6:05:52metadata I'm simply telling that okay
- 6:05:53you have to find this browser property.
- 6:05:56Okay so we going to give this one
- 6:05:58browser. So it's going to find and then
- 6:06:00I'm just going to do give me as browser.
- 6:06:05Then this is not it because you have to
- 6:06:07always give your destination that from
- 6:06:09where you are going to find this. So you
- 6:06:10have to give from table name. We'll do
- 6:06:12basics.app events and here we are going
- 6:06:15to simply mention where metadata
- 6:06:20is browser.
- 6:06:23Okay, that's it. So what we are simply
- 6:06:26telling here this metadata uh this arrow
- 6:06:30browser means that you have to open this
- 6:06:32JSON column named as metadata and get
- 6:06:35the value of the browser as text. All
- 6:06:38right. So now let's test it. So first
- 6:06:40let's open this and I'm going to clear
- 6:06:42this and also in the meantime let's do
- 6:06:45one thing let's comment out this one.
- 6:06:47Okay so I only wanted to check this this
- 6:06:49part. So here what I'll do let's uh
- 6:06:53sorry not clear let's run it once again
- 6:06:56and you can see that what you're getting
- 6:06:57we are getting sign up and you're
- 6:06:59getting chrome. Okay so now what is
- 6:07:01happening here you're getting the sign
- 6:07:03up and this is chrome. Why? because we
- 6:07:05are clearly telling here that you have
- 6:07:07to open the JSON B column named metadata
- 6:07:10and you have to get the value of browser
- 6:07:13as text. So this is how you can do this.
- 6:07:15Okay. So this is about the other data
- 6:07:18types. Now next we have another very
- 6:07:20very important and that is how we are
- 6:07:21going to
- 6:07:23work with null, empty, string and zero
- 6:07:26value. Okay. So we have null, empty,
- 6:07:28string and zero value and all three are
- 6:07:31not same. So let's see how uh we are
- 6:07:33going to do that. So first of all I'm
- 6:07:34just going to do 06 maybe and let's do
- 6:07:37null
- 6:07:38empty string
- 6:07:41uh something like this or let's do
- 6:07:45zero also 0.sql is given. Okay. So first
- 6:07:50of all let's clear this.
- 6:07:54I'll close everything else. So first we
- 6:07:57have null, right? So null means unknown
- 6:08:02or missing value. Next we have empty
- 6:08:06string. Empty string will be what? known
- 6:08:11string value but no characters or but it
- 6:08:16contains no characters
- 6:08:20and then at the end you have the zero.
- 6:08:22So zero will be your actual numeric
- 6:08:25value, right? So actual numeric
- 6:08:29value of zero. Okay. So let's create one
- 6:08:34more uh here. So I'm just going to
- 6:08:35copyright instead of rewriting. So copy
- 6:08:37this, paste it here and this I'm just
- 6:08:40going to do value examples something
- 6:08:43like this and then I'm simply going to
- 6:08:45create. So do create table basics dot
- 6:08:49value examples and here I'm just going
- 6:08:52to do so now I'm doing little faster not
- 6:08:55already you have done multiple times. So
- 6:08:56do serial so primary
- 6:09:00key and then after this maybe let's take
- 6:09:04what is it? Let's take a nickname. So
- 6:09:06we'll do nickname which will be text and
- 6:09:09then I'm also going to take a bio which
- 6:09:12also will be text and let's take a score
- 6:09:16and this one I'm just going to do
- 6:09:17integer. Okay. So we have these three
- 6:09:20that's it. So now let's do one thing
- 6:09:22what we are going to do right here. You
- 6:09:24have to understand that when we are
- 6:09:25going to insert the value then you can
- 6:09:27clearly understand what is happening
- 6:09:29here. And I'm going to make these things
- 6:09:30very very simple and easy to understand.
- 6:09:33So I'm just going to do insert. I'll do
- 6:09:34into and I'll do basics dot value
- 6:09:37examples. So we have nickname, we have
- 6:09:40bio should be in that order. Okay. And
- 6:09:42then we have score. Correct? And then
- 6:09:44we're going to add values. Now you see
- 6:09:46what we are doing here. So for the first
- 6:09:48one, notice for the nickname I'm passing
- 6:09:51explicitly as null. Okay. And then I'm
- 6:09:53just going to do here by learning rej
- 6:09:58or maybe learning posgress.
- 6:10:01Right? And then I'm passing the score.
- 6:10:03I'll pass in 10. So this is my first
- 6:10:05one. Now here clearly I'm mentioning
- 6:10:08that nickname is null.
- 6:10:11Nickname is null. Right?
- 6:10:14Uh also let's move it here. So this will
- 6:10:17be the first one. First condition. Next
- 6:10:19what I'm going to do give comma and I'm
- 6:10:22going to add one more property. Sorry uh
- 6:10:24row here. Here I'm explicitly mentioning
- 6:10:27that nickname is empty string. See, I'm
- 6:10:29giving here empty string and I'm just
- 6:10:31going to do empty nickname
- 6:10:34and let's give 20 and then at the end uh
- 6:10:38what I'm going to do right so let's do
- 6:10:41here some names I'm just going to do sum
- 6:10:44and for the second one I'm going to pass
- 6:10:46empty and here for this score I'm giving
- 6:10:49explicitly zero so here what this is
- 6:10:52this means the score is zero this is not
- 6:10:54missing there is a huge difference
- 6:10:56missing means it's a null value. Zero
- 6:10:59means you're telling that okay he got
- 6:11:01zero in the exam. So it's a real number.
- 6:11:03Okay. So this is the difference. And
- 6:11:05then you can also pass multiple as a
- 6:11:07null also. Let's say you are passing
- 6:11:09here uh one more here that this guy is
- 6:11:12John and his bio is null and his score
- 6:11:16is also null. This also you can do.
- 6:11:19Okay. And this is done. So now this is
- 6:11:21going to add these four. Now what I
- 6:11:23going to do right? First of all, let's
- 6:11:24say I'm now from this point, right? We
- 6:11:27have already done all the queries and
- 6:11:29everything multiple times. You should be
- 6:11:31able to write some of the basic queries.
- 6:11:33For example, now I want to query all
- 6:11:35these values from my DB. How I'm going
- 6:11:36to do that or from that column? So, it's
- 6:11:38very simple, right? I'm just going to do
- 6:11:39select
- 6:11:41uh star. So, star and then I'm just
- 6:11:44going to do what? From so to give basics
- 6:11:47dot value examples. So, this will be the
- 6:11:50first thing. So if I save this and then
- 6:11:53let's say I'm just going to run it here.
- 6:11:56So this will be the null empty sorry
- 6:12:03and let's remove this
- 6:12:07and you can see that these two null this
- 6:12:10also whenever you're getting empty or
- 6:12:11null you're getting those columns are
- 6:12:13empty right so that mean this is working
- 6:12:15fine now let's say you only want to
- 6:12:18check that maybe I want to check that
- 6:12:20give me the records where the nickname
- 6:12:23is only null so how you can do that so
- 6:12:25I'm going to comment it out here. So I'm
- 6:12:27just going to tell that okay select star
- 6:12:30from
- 6:12:32basics do app sorry not app value
- 6:12:35examples and then here I'm simply going
- 6:12:37to do that where nickname
- 6:12:42will be what is null simply so this is
- 6:12:46going to give me that okay give me from
- 6:12:47this table where the nickname is null so
- 6:12:49this is going to give me all the
- 6:12:50examples where the nickname is null and
- 6:12:52you can run it once again so let's say
- 6:12:55if I just run it Here you can see that
- 6:12:57we have only one where the nickname is
- 6:12:59null. Okay. Then you can also do maybe
- 6:13:02let's say you want to see where the
- 6:13:04nickname is empty. So I'm just going to
- 6:13:06do this will be a homework that write
- 6:13:08one query
- 6:13:11to find where nickname is empty string.
- 6:13:18So this will be your homework. Okay. Try
- 6:13:20to do it by yourself.
- 6:13:22Now let's say you want to check whe the
- 6:13:24score is only zero. So to do this you
- 6:13:26can do select star. So I do from basics
- 6:13:30dot value examples and then you're going
- 6:13:33to do where score will be equal to what?
- 6:13:36Zero. That's it. So this is going to
- 6:13:37give you where the column the score is
- 6:13:40zero. And if you run this here once
- 6:13:41again
- 6:13:44and you can see that you're getting this
- 6:13:46right. So that means this is working
- 6:13:49fine. And this is how you have to use.
- 6:13:52Now you also can use reverse way that
- 6:13:55you want to check whe the nickname is
- 6:13:57not null. Okay, I'm going to repeat once
- 6:14:00again. You want to check whe the
- 6:14:01nickname is not null. So how you can do
- 6:14:03that? Copy this exact thing. Paste it
- 6:14:06here and you're going to do is not null.
- 6:14:08Okay, so this is where it's going to
- 6:14:10give you the reverse uh result. So here
- 6:14:13I'm just going to do clear and let's run
- 6:14:15once again.
- 6:14:17You can see that you're getting nickname
- 6:14:20is not null. Okay. Now here okay first
- 6:14:23we are getting three but for the first
- 6:14:25one it is null right. How many we have
- 6:14:28added? Let me just check. Okay we have
- 6:14:30added four correct. So it should be
- 6:14:32getting three because first one is
- 6:14:33explicitly null second was not empty. So
- 6:14:37this is different. Okay. So here it's
- 6:14:39going to give you that where it is not
- 6:14:41null. I hope this is clear right? So we
- 6:14:44are not going to use equal to null. we
- 6:14:45are going to use if not null something
- 6:14:47like this. So I think this is all what
- 6:14:50else uh I think I think yeah okay one
- 6:14:55more thing I wanted to mention I think
- 6:14:56that part we have covered right the
- 6:14:58empty one we have covered is null we
- 6:15:00have covered yeah I think we have
- 6:15:01covered almost all the things so try to
- 6:15:04practice this one the reverse one that
- 6:15:06when you want to check that if there is
- 6:15:0810 products or 10 uh examples that rows
- 6:15:12that are added in that particular table
- 6:15:14so there can be two scenarios you can
- 6:15:16check you want to check the products
- 6:15:19where it is null. So you're going to do
- 6:15:20where that particular key name is null.
- 6:15:23If you want to check the reverse, you
- 6:15:24have to give where the key proper key
- 6:15:26name is not null. Okay, it's going to
- 6:15:28give you the reverse order. So with this
- 6:15:30now, let's move on to the next one. Next
- 6:15:31one, we're going to understand what is
- 6:15:33constants. Okay, and this is also very
- 6:15:34very important. All right. So moving to
- 6:15:36the next one. Another important concept
- 6:15:38which will be constants, right?
- 6:15:42Uh let's do this one as SQL.
- 6:15:46All right. So close this. So we have
- 6:15:48different column constants. Okay. So we
- 6:15:51have something like I think we have
- 6:15:52already discussed not null. Then we have
- 6:15:55unique
- 6:15:57then you have default then you have
- 6:15:59check all these right now these all are
- 6:16:03database rules. Now why this is very
- 6:16:05very powerful because it's going to
- 6:16:08protect your data and user will not be
- 6:16:10able to pose wrong data to D. You can do
- 6:16:14backend validation because backend
- 6:16:16validation always you need to do it. But
- 6:16:18remember one thing and this is also very
- 6:16:20important concept that you should know
- 6:16:21if you're a beginner let's you are
- 6:16:23working in sorry you are studying in
- 6:16:24college that even though backend
- 6:16:26validations are good but database
- 6:16:29constants are always always stronger
- 6:16:31than backend validation. Why? Because
- 6:16:33that is going to directly protect the
- 6:16:35data even let's say uh you are writing a
- 6:16:39different app or script or developer
- 6:16:41whatever they are doing. So you're
- 6:16:43you're understanding what I'm saying. So
- 6:16:46you have a back end logic or back end
- 6:16:48validation logic in your back end code.
- 6:16:50Now let's say you don't have any
- 6:16:52constants in your DB. Now if we can
- 6:16:55write in our DB in different ways,
- 6:16:57right? So what I mean by this, we can
- 6:16:59write in a different ways. You can write
- 6:17:00from your app. So let's say you're doing
- 6:17:02some API call and API call is writing
- 6:17:04into your DB. You can also write uh data
- 6:17:07into DB using script or directly
- 6:17:10developer can go to DB and they can
- 6:17:12write makes sense right easily we can do
- 6:17:14it but let's say if you have constant on
- 6:17:17the DB level in all scenarios if you're
- 6:17:20writing a wrong data it's going to fail
- 6:17:23so that is the reason backend validation
- 6:17:25sorry database constants are always
- 6:17:27always always stronger than backend
- 6:17:29validation doesn't mean that I'm telling
- 6:17:31that you don't have to write backend
- 6:17:32validations you have to write but both
- 6:17:34are equally important important but
- 6:17:35database constants are much stronger
- 6:17:37okay whenever you'll be working with
- 6:17:39databases so first let's do one thing so
- 6:17:41I'm just going to do here let me just
- 6:17:42copy from here so I'm just going to copy
- 6:17:45this paste it here and let's give this
- 6:17:47one name as basics dot let's create
- 6:17:49accounts and here I'm just going to do
- 6:17:51create table and I'm just going to do
- 6:17:54basics dot accounts and let's add ID
- 6:18:00let's give this one as primary Okay,
- 6:18:05let's add a full name. So I'm just going
- 6:18:08to do text and this will be not null.
- 6:18:11Okay, so this is one of the constant. So
- 6:18:14not null means we're simply telling that
- 6:18:15full name is required and postgress is
- 6:18:18going to reject whenever we going to
- 6:18:20insert and we'll see that if the full
- 6:18:22name is missing as simple as that. What
- 6:18:24else? Uh I think unique we have already
- 6:18:26discussed right still let's do it. So
- 6:18:28I'll do email. This will be text. And
- 6:18:31I'm just going to do not null. And this
- 6:18:33will be unique. And also we are going to
- 6:18:36give a default value. So I'm just going
- 6:18:38to do isore active. And I'm just going
- 6:18:41to do this one as boolean. And this will
- 6:18:43be default. And I'm just going to give
- 6:18:45this one as true. Okay. And check also
- 6:18:48we have discussed previously. So I'll do
- 6:18:50integer. And I'm just going to take a
- 6:18:52check. And here I'm just going to give
- 6:18:54age should be greater than or equal 80.
- 6:18:58Okay. And let's take the last one which
- 6:19:00will be created at and here I'll do this
- 6:19:05one as time stamp. We also have a time
- 6:19:07stamp Z. There is one more property and
- 6:19:10then we're going to take this one as
- 6:19:12now. Okay. So this is going to create
- 6:19:14our table. All right. Now let's do some
- 6:19:17of the valid insert. Okay. So valid
- 6:19:19cases. So let's I'm just going to do
- 6:19:21insert into
- 6:19:25here into. So I'll do basics dot
- 6:19:28accounts. So we have full name. We have
- 6:19:31email and let's do age is active we
- 6:19:34don't have to do because it's given as
- 6:19:36default. And I'm just going to do
- 6:19:38values. And I'm just going to add my
- 6:19:39name here.
- 6:19:42And let's give the email.
- 6:19:46And I'm just going to give the age.
- 6:19:48Let's do 11. Okay. That's it.
- 6:19:52So now let's save this. And we are going
- 6:19:54to run this file.
- 6:19:56Okay. So, let's copy this
- 6:20:02and we'll go here and then I'm just
- 6:20:05going to replace this.
- 6:20:11Okay. What is telling that no new row
- 6:20:13for relation while check constant
- 6:20:16account? Oh, see we are already getting
- 6:20:17added. Fantastic. Because we have given
- 6:20:19this one as 11, right? So, it should be
- 6:20:21greater than 20 uh sorry 18. So we have
- 6:20:24to give this one as 20. All right. So
- 6:20:26it's already working. You can see that.
- 6:20:29And I didn't give this one
- 6:20:30intentionally. So I missed it. So now it
- 6:20:32should be able to. So you can see that
- 6:20:33it's working, right? And if you want to
- 6:20:35get it the data, what you can do? Simply
- 6:20:38do select start from and you'll do
- 6:20:40basics dot accounts, right? And this is
- 6:20:43going to give you the information.
- 6:20:44Again, I'm repeating the same thing you
- 6:20:46can do from where? From your ESQL uh
- 6:20:49terminal also, right? So if you run
- 6:20:51this, you can see that you're getting
- 6:20:52one data. Fantastic. Right. So now what
- 6:20:55you're going to do, right? So see what I
- 6:20:57mean. Right. So you can directly do from
- 6:20:58your uh this psq alg. So let's see if I
- 6:21:01clear this and then I'm just going to go
- 6:21:04here copy this.
- 6:21:07Okay. Now inside this let's say you want
- 6:21:10to get the data from this accounts
- 6:21:11table. So what you can do? You can do
- 6:21:12directly just select start and then
- 6:21:15you're going to do from and you'll do
- 6:21:17basics,
- 6:21:18right? And then you'll do accounts.
- 6:21:20That's it. And if you enter here, you're
- 6:21:22going to get the same result. So now
- 6:21:24you're understanding why I'm not doing
- 6:21:26here all the things because if I do
- 6:21:28here, you will not be able to get it
- 6:21:30from the source code. So here you'll be
- 6:21:32able to see the exact queries, right? So
- 6:21:33that is the reason. But these are
- 6:21:35optional. These are not needed. I'm
- 6:21:37repeating this thing so many times
- 6:21:38because some of you can be quite
- 6:21:40confused that why he is writing these
- 6:21:42queries inside this file. That is
- 6:21:44actually intentional. So I'll clear
- 6:21:47this. So this is working fine. Now let's
- 6:21:49say I'm going to give some invalid
- 6:21:50scenarios now. So what I mean by invalid
- 6:21:52scenario so that we can check. So first
- 6:21:54one will be let's say I I I I will be
- 6:21:57adding the email and age. I'm not going
- 6:21:59to add the full name. But if you see the
- 6:22:02rule right full name is not null. So
- 6:22:04that means we need to add it. So this
- 6:22:06should fail and you'll see that it is
- 6:22:08going to fail. So I'm just going to
- 6:22:09comment it out from for uh now copy this
- 6:22:12comment it out and then let's paste it
- 6:22:15here and then I'm just going to remove
- 6:22:17the full name. I'm going to keep the
- 6:22:18email and I will just give here missing
- 6:22:23name at thegmail.com 20. And then if you
- 6:22:26try to run this and you'll see that this
- 6:22:28is going to fail for sure. And you can
- 6:22:31see that this full name null value in
- 6:22:33the column full name relation accounts
- 6:22:35violates not null constant. Correct? So
- 6:22:38this is another fail case scenario. What
- 6:22:41else? All right. So let's add one more
- 6:22:43case. So what we can do right maybe I'm
- 6:22:44just going to okay let's copy this
- 6:22:48and I'm going to comment it here. Let's
- 6:22:50remove this comment and I'm going to
- 6:22:52paste it one more time. Okay. And see
- 6:22:54what we are doing here. So I have sa
- 6:22:56mukari sangamgmail.com
- 6:22:58and I'm going to take uh duplicate
- 6:23:02email user and I'm going to keep the
- 6:23:05same email. Maybe let's do this one as
- 6:23:0723. So this also should fail. Why?
- 6:23:10because we have two user with same
- 6:23:12email. Okay. So let's run this and you
- 6:23:16can see that we have duplicate key for
- 6:23:17this unique. So account email key. This
- 6:23:20is also another constant. Okay. So
- 6:23:22similarly you can check multiple
- 6:23:24scenarios here. For example, let's say I
- 6:23:27we have already checked right. So if you
- 6:23:28give the age less than 18 so it's going
- 6:23:31to fail. For example, let's say I'm
- 6:23:32going to do sum one. But at the same
- 6:23:34time, I'm going to make this one as
- 6:23:36three or maybe let's say uh five.
- 6:23:38Whatever you want to do, this is also
- 6:23:40going to fail, right? So let's say clear
- 6:23:42this and try to push. And you can see
- 6:23:46that what you're getting the account age
- 6:23:48check constant. So this is failing. Why?
- 6:23:50Because the age is five and in the
- 6:23:52constant we have clearly given the check
- 6:23:53should the age should be greater than or
- 6:23:55equal to 80.
- 6:23:58So this is the concept of constant. So
- 6:24:00this is how you need to use right. So I
- 6:24:02think we have covered all the things and
- 6:24:04one thing we need to understand and that
- 6:24:06is uh some more about primary keys that
- 6:24:08we are going to discuss in the next part
- 6:24:10and then we'll move to the part two.
- 6:24:12Why? Because for part one we have almost
- 6:24:14covered most of the things and in
- 6:24:16details right I'm pretty sure you will
- 6:24:17not find this kind of explanation in
- 6:24:19paid courses also because I'm explaining
- 6:24:21each and everything slowly and once you
- 6:24:23do the practice right you'll be able to
- 6:24:25understand you'll not going to forget
- 6:24:26you're not going to believe me that when
- 6:24:28I usually learn all these things right I
- 6:24:30took things very very seriously even if
- 6:24:32I learn all these things five years back
- 6:24:34right the way I've learned things I I
- 6:24:36never memorized these things right so
- 6:24:38maybe there is a possibility that I
- 6:24:40won't be able to memor uh like recollect
- 6:24:43the uh let's say the syntax and
- 6:24:45everything because it's not possible for
- 6:24:47everyone to remember everything right
- 6:24:49but once I'll see once right maybe let's
- 6:24:52say 5 years back I've done SQL I'm just
- 6:24:54giving example and after 5 years I'm
- 6:24:55just I need two three days and I'll be
- 6:24:57able to again implement project from the
- 6:25:00next day so this is how you need to
- 6:25:02learn all these things so with this now
- 6:25:03let's move to the next one all right so
- 6:25:05let's finish our part one with little
- 6:25:07more concept that we are going to cover
- 6:25:09that is on primary keys
- 6:25:12So let's create primary is dosql.
- 6:25:17I think this one we've already discussed
- 6:25:18that primary key is going to denotes
- 6:25:20that it's uniquely identifies each row
- 6:25:23in a table. Okay. Now if you don't have
- 6:25:26any primary key what are the things that
- 6:25:28will become difficult. Now these can be
- 6:25:30interview questions also. So if you
- 6:25:32don't primary key it becomes harder to
- 6:25:35update, delete, do any kind of reference
- 6:25:39or even if you want to connect to a
- 6:25:41specific row to rows in other tables.
- 6:25:44All these things can be very very
- 6:25:46tricky. Okay. So that means there has to
- 6:25:48be something unique in each and every
- 6:25:50row so that you'll be able to identify
- 6:25:51that particular row which is what your
- 6:25:54primary keys is going to do. Okay. So
- 6:25:56here now let's do one thing. I'm just
- 6:25:58going to do drop table. I'm just going
- 6:25:59to create a simple table. I'm just going
- 6:26:01to do if exist. So let's do basics dot
- 6:26:04uh what we can do maybe let's do courses
- 6:26:08courses or maybe let's do sales
- 6:26:11then I'm just going to do create table
- 6:26:14and I'm just going to do basics dot
- 6:26:16sales and here I think we have already
- 6:26:19discussed I'm not explaining because we
- 6:26:21have already covered so we have a ID
- 6:26:23which is a serial and this will be my
- 6:26:25primary key okay and then let's say you
- 6:26:29want to do title sorry sales name maybe
- 6:26:32let's do title or whatever you want to
- 6:26:34take. So I'm going to take text. This
- 6:26:36will be not null. And what else you can
- 6:26:39take? Uh maybe let's do one price
- 6:26:42property sales price. And I'm just going
- 6:26:45to do this one as numeric.
- 6:26:49So let's do 10, 2. And here I'm just
- 6:26:52going to do this one as not null. And
- 6:26:54also I'm going to give a default value
- 6:26:55of zero. And let's take one more which
- 6:26:58will be created at. So this will be
- 6:27:00timestamp and I'm going to do default
- 6:27:02and this will be now.
- 6:27:06Okay. Now let's add some value here. So
- 6:27:09I'm just going to do insert into
- 6:27:12basics dot sales. So what are the things
- 6:27:16we have? We have the title and the
- 6:27:17price.
- 6:27:19And let's add here some values.
- 6:27:24So let's add maybe sale one. And I'm
- 6:27:28just going to do cell 200. Let's give
- 6:27:31comma. So I'll do cell two. And let's
- 6:27:35give this one as 500. Something like
- 6:27:37this. Okay, this is done. So save this.
- 6:27:40And let's run it here. So I'm just going
- 6:27:42to clear,
- 6:27:45rename, copy this, and then I'm just
- 6:27:49going to run it. So it's going to create
- 6:27:51the table.
- 6:27:53All right. Fantastic. So now let's say
- 6:27:56first of all I wanted to check that what
- 6:27:58I have here. So I'm just going to do
- 6:27:59select star from sold basics dot cells
- 6:28:06save this and let's run once again.
- 6:28:11So you have two data okay so ID 1 and
- 6:28:14ID2. So till this part this is fine
- 6:28:16right now what we can do right we can
- 6:28:18also query based on the ID. So now let's
- 6:28:21say I only wanted to see the data of
- 6:28:25ID2. So I can simply do your select
- 6:28:27start and you can do from you do basics
- 6:28:30dot sales
- 6:28:32and here you have to give where your ID
- 6:28:34will be equal to two. So this is going
- 6:28:37to give you the second information
- 6:28:38second row information. So if you clear
- 6:28:41and run this you're going to get only
- 6:28:43one. Okay. Now one thing is very
- 6:28:47important. And now let's say I'm going
- 6:28:48to give a invalid scenario here. So
- 6:28:51let's do one thing, right? What we are
- 6:28:53going to do maybe uh okay let's do one
- 6:28:56thing. Let's keep this one like this and
- 6:28:58let's comment it out. And I'm going to
- 6:28:59insert one more data here. Okay. So I'm
- 6:29:02just going to do here. Let's copy this
- 6:29:04only from here. Paste it. And I'm just
- 6:29:07going to do insert into basic uh dot
- 6:29:09quotes. Sorry. And here I'll do ID,
- 6:29:12title, and price. All three things I'm
- 6:29:14adding in the values. Let's I'm going to
- 6:29:16add only one value. But see what I'm
- 6:29:18doing in the ID I'm giving a duplicate
- 6:29:20ID. So here I'm giving one. Why? Because
- 6:29:22see if you notice here this one ID will
- 6:29:25be automatically created before only. So
- 6:29:27here I'm just going to giving it
- 6:29:28duplicate ID and you'll see that this is
- 6:29:30going to fail. Okay. So I'll do
- 6:29:32duplicate
- 6:29:35ID something like this. And if you now
- 6:29:38save this right and see what will
- 6:29:40happen. So I'm just going to clear this
- 6:29:43run it and you can see that your
- 6:29:45duplicate key value violates unique
- 6:29:47constant sales primary key fantastic key
- 6:29:50ID equal to one is already exist. So
- 6:29:52this is a proof that your primary key
- 6:29:55will be always unique and primary key is
- 6:29:57going to denotes that okay this is my
- 6:29:59unique identifier for each row in a
- 6:30:01certain table. So with these the first
- 6:30:04basic things we have covered I think we
- 6:30:06have learned a lot of things these
- 6:30:07things are very very important without
- 6:30:08these we can't move to the next section.
- 6:30:10So with that now let's move on to the
- 6:30:12part two. Okay. All right everyone. So
- 6:30:14now it's time to start on the part two.
- 6:30:16So what actually we are going to cover
- 6:30:17in the part two. So part two will be
- 6:30:19more on more SQL basics and the concepts
- 6:30:21we are going to cover. For example let's
- 6:30:23say what is the concept of so I think
- 6:30:25insert we have already covered in uh
- 6:30:27still we are going to cover insert
- 6:30:28single row multiple rows how you're
- 6:30:31going to select specific columns. Uh
- 6:30:33[snorts] where and basic filters then
- 6:30:35you have and or not right like I like.
- 6:30:39So many things are there. So all those
- 6:30:41things we are going to cover in this
- 6:30:42part two. So for this one what we're
- 6:30:44going to do right we'll be having a
- 6:30:45setup file. So what this setup file is
- 6:30:47going to do this setup file is going to
- 6:30:49create the table and based on this
- 6:30:51particular setup file whatever concept
- 6:30:53that we are going to cover in the next
- 6:30:54section we are going to use this
- 6:30:56particular table as a base table. Okay.
- 6:30:58Now the reason is very simple. So I'm
- 6:31:00going to show you if you search on
- 6:31:03Google right postgress SQL mac and
- 6:31:05everything you'll see that it has lot of
- 6:31:06issues also and it's extremely slow. I
- 6:31:08don't know why I think if you're using
- 6:31:10Windows right you will not face this
- 6:31:12issue. So what I'm trying to explain
- 6:31:13that if you want to visualize all those
- 6:31:15data from your posgress also you can
- 6:31:17easily do that. But as I already
- 6:31:19mentioned that we are going to use a
- 6:31:20terminal only or let's say psql command
- 6:31:23line which is much better. I will say
- 6:31:25which is better because it will give you
- 6:31:27much more better idea how we can write
- 6:31:29commands and everything. So first let's
- 6:31:31start with create. So I'm just going to
- 6:31:33do this one as 0 0 or let's do 01 and
- 6:31:36I'm just going to do SQL concepts
- 6:31:40base file. Okay. So I'll do base files
- 6:31:42SQL. All right. So first let's create
- 6:31:45some of the tables and everything. And
- 6:31:47for this one I'm not going to explain
- 6:31:48each and everything because some of the
- 6:31:50things we have already covered. So let's
- 6:31:52start with first I'm just going to do
- 6:31:53create extension
- 6:31:58extension spelling. Okay. It's correct.
- 6:32:00So I do if not exist and I'm just going
- 6:32:03to take pgto.
- 6:32:06So after this I'm just going to do a
- 6:32:08drop table if exist. Let's give this one
- 6:32:11as products
- 6:32:13and then let's create our table. So I'm
- 6:32:15just going to do create table and I'll
- 6:32:17do this one as products. Okay. Now let's
- 6:32:20see what are the things that we are
- 6:32:21going to take. So first let's take ID
- 6:32:23and remember all these uh properties
- 6:32:25that we are going to take right you have
- 6:32:27to recolct because whenever we are going
- 6:32:29to cover the next concept we are going
- 6:32:31to use this table as a base table. So
- 6:32:33I'm just repeating this thing multiple
- 6:32:34times. This is important. So we are
- 6:32:36going to take this one as UID and I'll
- 6:32:38take primary key. Let's take default and
- 6:32:41for this one I'm just going to do gen
- 6:32:44random UID. I think this we have already
- 6:32:46covered.
- 6:32:48Next let's take a name property. So name
- 6:32:50I'm just going to take this one as text
- 6:32:52not null.
- 6:32:56Next let's take a category. So this also
- 6:32:58I'm going to take as text not null.
- 6:33:03Let's take a price. So for price I'm
- 6:33:05just going to do this one as numeric
- 6:33:08and this I'll do 10A 2 not null and
- 6:33:13let's give a check. So for check I'm
- 6:33:16just going to do price
- 6:33:18has to be greater than or equal zero.
- 6:33:20Okay. So this is one check I'm giving.
- 6:33:22Then let's take a stock.
- 6:33:25I'm just going to do this one as integer
- 6:33:27not null. I'll do default. Let's give
- 6:33:31default as zero and check. So here also
- 6:33:34I'm just going to give the stock also
- 6:33:35has to be positive. So it will be
- 6:33:36greater than or equal zero. Then let's
- 6:33:40take a is active which will be boolean
- 6:33:44not null also. And then I'm just going
- 6:33:46to do a default which will be true.
- 6:33:48Okay. Let's take a skew property and I'm
- 6:33:52just going to do this one as text. But
- 6:33:54for this one I'm going to take this one
- 6:33:55as unique. So for every product there
- 6:33:57has to be one skew which will be unique.
- 6:34:00Then let's take a description.
- 6:34:02This will be text. And at the end we're
- 6:34:04going to take created_.
- 6:34:08And let's take this one as time stamp
- 6:34:10not null and I'll do default. Let's give
- 6:34:13the current time. Okay. So these all the
- 6:34:16properties that we are going to have or
- 6:34:18all the columns for this particular
- 6:34:19table. Okay. So first let's save this
- 6:34:21and let's see if this is working or not.
- 6:34:23So first what I'm going to do right?
- 6:34:25Let's copy this. All right. So now let's
- 6:34:27paste it here. So what we have done the
- 6:34:28same command we just have to go to part
- 6:34:30two and then this is the first file and
- 6:34:32then run. And you see that this is going
- 6:34:34to create the table. And then
- 6:34:35[clears throat] I'll just do clear and
- 6:34:38let's go here and let me quickly refresh
- 6:34:40this.
- 6:34:44See the how much uh slow this is. I
- 6:34:47think they have created this one using
- 6:34:49electron framework. This is what I'm
- 6:34:51thinking. And there must be some issue
- 6:34:52in the math specially. And if you go to
- 6:34:55public
- 6:34:58and then let's go to table. So you can
- 6:35:00see that we have products. And if you go
- 6:35:02to columns
- 6:35:05and you're going to see all these
- 6:35:07columns, right? So this is the columns
- 6:35:08that we have created. Fantastic.
- 6:35:10So now what I'll do, right? So let's
- 6:35:12insert some of the data first. So
- 6:35:16here I'm just going to do insert into
- 6:35:20products.
- 6:35:22So what are the things we have to do? So
- 6:35:23we have the name, we have category, we
- 6:35:25have price, we have uh stock, uh we have
- 6:35:30is active skew and also we have
- 6:35:33description. So these many things we
- 6:35:34need to uh add and then I'm just going
- 6:35:37to take this one as values and then I
- 6:35:40already have some data that I have
- 6:35:41created. Okay, this is the data that I
- 6:35:43wanted to add here. So copy this and
- 6:35:46after this I'm just going to paste it
- 6:35:48here. Okay, so this is what we are going
- 6:35:51to add. So now also let's do one thing.
- 6:35:54Let's run this command so that we can
- 6:35:56see that if these datas are present or
- 6:35:58not. So this is just a simple select
- 6:36:00command. So do select start from
- 6:36:03products.
- 6:36:04Save this and then we going to run once
- 6:36:07again.
- 6:36:09See that we are already getting the data
- 6:36:11here. Right? So this is all the data. So
- 6:36:13now what we need to do right using this
- 6:36:15data now we are going to understand all
- 6:36:16the concepts one by one. So let's clear
- 6:36:19this
- 6:36:22and let's start with the first one. So
- 6:36:24first will be I'm just going to do 0 to
- 6:36:26although with this one we have already
- 6:36:28done but I just wanted to still uh do
- 6:36:30this one more time. So I'm just going to
- 6:36:32do insert single row dosql.
- 6:36:36So how we going to do insert single row?
- 6:36:38I think this you have already know. So
- 6:36:41we just have to do insert into
- 6:36:45the table name. So you have products
- 6:36:46correct and then you have to give all
- 6:36:48the properties. So just now we have
- 6:36:49done. So we have name, we have category,
- 6:36:52we have price, we have stock,
- 6:36:55we have uh skew
- 6:36:58and also let's say I wanted to add
- 6:37:00description. Okay. So these many things
- 6:37:02we have right why is active is not
- 6:37:05needed because we have a default value
- 6:37:06here. Okay. This also have but let's say
- 6:37:08I wanted to so what will happen right?
- 6:37:10If you don't add this is active it will
- 6:37:12automatically take that one as true. So
- 6:37:14this is done. So now what we need to do?
- 6:37:16So let's add values and then let's add
- 6:37:19all the values that we wanted to add. So
- 6:37:22here also one more thing that each value
- 6:37:24that we are going to add that always
- 6:37:26should match or must match the column
- 6:37:29that we have added in the above. This is
- 6:37:31also very very important. So let's I'm
- 6:37:33just going to do here laptop lap laptop
- 6:37:37stand,
- 6:37:38category. Let's give this one as maybe
- 6:37:40electronics.
- 6:37:42Then I'll give comma let's give a price
- 6:37:43of 5,000.
- 6:37:460. Then after this I'm just going to
- 6:37:48give stock of let's say 23. Then I'm
- 6:37:51just going to do skew. Now this will be
- 6:37:53important. Okay. So let's see what are
- 6:37:55the skew that we are using. You see all
- 6:37:56these skew that we are using here.
- 6:37:58Electronics key 001 like this. So here
- 6:38:02maybe I'm just going to do electronics.
- 6:38:04So which one we can do? Electronics key
- 6:38:06002. Something like this. If it is
- 6:38:08you're giving wrong or if it is not
- 6:38:10unique then it's going to fail. So here
- 6:38:12I'm just going to do electronics key 002
- 6:38:17then I'm just going to give a
- 6:38:18description. So let's give this one as
- 6:38:20laptop
- 6:38:22stand description.
- 6:38:25Okay so this is done. So now first of
- 6:38:27all what we going to do right? So I'm
- 6:38:29just going to run this command. So first
- 6:38:31I'll copy this rename this. Sorry not
- 6:38:34rename. I'm going to copy this and then
- 6:38:36I'm going to run this here. Again, I
- 6:38:38already mentioned you can run the same
- 6:38:39thing in in your psql also.
- 6:38:44Okay, we are getting no y.
- 6:38:50Oh man, I have to put this one inside.
- 6:38:54So, let's try once again.
- 6:38:57So, you can see that inserting is done.
- 6:38:59So, now this will be your homework from
- 6:39:01this part because we have already
- 6:39:02covered. Now let's say I only wanted to
- 6:39:05check that if this data is present or
- 6:39:07not. Right? So here what I need to do so
- 6:39:10let's I'm just going to do select star
- 6:39:12then I'm just going to do from I have to
- 6:39:15give this products and then I need to to
- 6:39:17check so I need to give a where where
- 6:39:19the skew will be what the skew that I
- 6:39:21have given here because this is always
- 6:39:22unique. So this is going to give me the
- 6:39:24correct information always right and if
- 6:39:26I save this and let's say if I run it
- 6:39:28here so it should give me that
- 6:39:30particular result right what is syntax
- 6:39:33error oh you have to give this sorry my
- 6:39:36bad save clear and let's run again you
- 6:39:40can see that what you're getting
- 6:39:42duplicate key while let's this one this
- 6:39:44is already exist okay this we have
- 6:39:45already added my bad so here we just
- 6:39:47have to comment it out save it and now
- 6:39:50let's run it again and then you're going
- 6:39:53going to get one result. Okay. So you
- 6:39:54see that you're getting this result.
- 6:39:57So you have this UID laptop stand
- 6:40:00electronics 523. This is true. This is
- 6:40:03your skew 002 laptop stand description.
- 6:40:06This is your time stamp. So that means
- 6:40:08this is working fine. You're getting why
- 6:40:10you got that particular error because
- 6:40:12we've already added this and then we try
- 6:40:14to run this particular command but in
- 6:40:16before it's try to execute this
- 6:40:18particular query once again and then it
- 6:40:21check that okay there is a key that is
- 6:40:23already present because we already done
- 6:40:24this one in the before right so that is
- 6:40:26the reason we have to comment it out and
- 6:40:28then you have to do here now let's say
- 6:40:30you want to do like this let's say you
- 6:40:32want to check that I wanted to check if
- 6:40:34something is present with electronic key
- 6:40:36003 or not right and let's say I'm just
- 6:40:38going to clear this and And you'll have
- 6:40:40to run it once again and you will see
- 6:40:41that we don't have any row. But the
- 6:40:43moment I'm going to execute this one
- 6:40:45here once again. Let's I'm just going to
- 6:40:47change this one to 003 and let's do this
- 6:40:50on a laptop stand 03 and rest all let's
- 6:40:53keep it same and you'll see that what
- 6:40:55will happen right? It's going to do two
- 6:40:56things. First it's going to execute this
- 6:40:58and at the same time it will it will be
- 6:41:00giving me the updated data with this
- 6:41:03particular skew. So let's say now I'm
- 6:41:05just going to run it one second. Okay.
- 6:41:08Why you getting zero rows? We have to
- 6:41:11save this. See this kind of mistake I'll
- 6:41:13do. We have to save this file and let's
- 6:41:15run once again. And you can see that you
- 6:41:17are getting one data. And this is the
- 6:41:19skew just we have created. Okay. So this
- 6:41:21will be inserting a single row. Now
- 6:41:23inserting multiple rows. Now multiple
- 6:41:25rows are again very very simple but
- 6:41:27still I wanted to show one interesting
- 6:41:29thing that how you can query multiple
- 6:41:31data when you're going to add multiple
- 6:41:33uh how you're going to query multiple
- 6:41:36data from DV when you're going to insert
- 6:41:38multiple data into DV. Okay. So here
- 6:41:40let's do one thing. I'm just going to do
- 6:41:4203 and I'll do insert
- 6:41:45multiple
- 6:41:47rows dosql.
- 6:41:50Again this will be the same thing. So
- 6:41:52you have to do insert into products
- 6:41:57and here I'm just going to do this one
- 6:41:58as name category
- 6:42:01price stock
- 6:42:04then you have skew and then you have
- 6:42:06description correct and then I'm just
- 6:42:08going to take values
- 6:42:11and here what I will do right I'm going
- 6:42:13to add multiple here okay so let's I'm
- 6:42:15just going to do laptop stand five let's
- 6:42:18give this one as electronics
- 6:42:22Let's give some data. Let's give this
- 6:42:24one anything. And here I'm just going to
- 6:42:26do electronic
- 6:42:28laptop
- 6:42:30100. Something like this. And then I'm
- 6:42:33just going to do laptop
- 6:42:37stand 5
- 6:42:40desk. So give comma copy this. And then
- 6:42:45I'm just going to paste it here. This
- 6:42:47will be laptop.
- 6:42:52So copy once again and then I'm just
- 6:42:54going to paste it three times. And let's
- 6:42:56do this one as 006. Then I'm just
- 6:42:58[snorts] going to do 101 and this will
- 6:43:01be laptop 6. Let's do this one as laptop
- 6:43:047. And change this one 102. And I'm just
- 6:43:06going to do seven. Okay, I'm just
- 6:43:08keeping all the same which is fine. And
- 6:43:10then I'm just going to close this. And
- 6:43:11now what I'm going to do right, this is
- 6:43:13going to insert all these three data.
- 6:43:15Now I wanted to check all these data
- 6:43:17that I have added just now multiple
- 6:43:19data. So how you can do that right? So
- 6:43:21you do select let's see I'm just going
- 6:43:23to take name I need category I need
- 6:43:25price I need stock and also I need skew.
- 6:43:30Now what you need to do you have to
- 6:43:31always give the uh from destination from
- 6:43:34where and now I wanted to give okay
- 6:43:36where skew. Now we have to give in.
- 6:43:40Okay. So in is going to give give you
- 6:43:42the or allow you to add multiple
- 6:43:46points that you wanted to query. For
- 6:43:47example, I wanted to check whe the skew
- 6:43:50is this. So you can add it here. Then
- 6:43:53you have to give comma. And then you
- 6:43:54wanted to check this data also.
- 6:43:57And then you also wanted to give this
- 6:43:59102 also. So what this is going to do?
- 6:44:02So this is going to insert all these
- 6:44:04three. And at the same time this is
- 6:44:05going to give you the information back.
- 6:44:07Right? So let's see if this is working
- 6:44:08or not. So I'm going to close this,
- 6:44:11clear this, run this here. So copy this
- 6:44:20and then I'm going to paste it here and
- 6:44:22you can see that you are getting all
- 6:44:23three. So you're getting only name,
- 6:44:24category, price and skew. And here
- 6:44:27you're getting all three. Now these
- 6:44:28three are the newly data. How this is
- 6:44:30giving? This is giving the based on skew
- 6:44:32because skew is unique here. Okay. So
- 6:44:34this is the concept of inserting single
- 6:44:37row query, inserting multiple rows and
- 6:44:40how you can query. So with this now
- 6:44:41let's move on to the next one. All
- 6:44:43right. So moving to the next one. So
- 6:44:44let's create one more file and I'm just
- 6:44:46going to give this one as select
- 6:44:50specific
- 6:44:53columns
- 6:44:54versus select start that we have already
- 6:44:57done right. So I'm just going to do
- 6:44:58versus select
- 6:45:01stars.
- 6:45:02Select stars SQL. Okay. So what I mean
- 6:45:06by this? So first of all, what is select
- 6:45:08star? Select stars is going to return
- 6:45:11every column for every product. As
- 6:45:13simple as that, right? Uh now if you're
- 6:45:16a beginner, I think we have already
- 6:45:17covered select star from this select
- 6:45:19star from this particular destination.
- 6:45:21And that is very easy. But when your
- 6:45:24application will grow, let's say you're
- 6:45:26creating actual production level
- 6:45:27application, what actually you should
- 6:45:29do, right? Or maybe there can be some
- 6:45:31specific scenarios where you only want
- 6:45:34to use specific columns or you want to
- 6:45:36get specific columns not you want to
- 6:45:38return each and every column. Consider
- 6:45:40it's possible 100% possible that there
- 6:45:42is a particular table that has 100
- 6:45:45columns. I'm just giving example okay
- 6:45:47and you don't want to return all the 100
- 6:45:49columns every time. So this is where we
- 6:45:52have something called select specific
- 6:45:54columns concept. Okay. So how we are
- 6:45:56going to do do that? So first of all I'm
- 6:45:58just going to write comment here that
- 6:46:01okay select star is going to return
- 6:46:02every column. So it returns every
- 6:46:05columns and then we have something
- 6:46:07called select specific columns. So if
- 6:46:10select specific columns
- 6:46:13is going to return is going to return
- 6:46:17the columns that I want that I want.
- 6:46:22So first of all we have select.
- 6:46:26So I'll do star from and then I'm just
- 6:46:29going to do this one as products right?
- 6:46:31So this is going to give me every single
- 6:46:33information or every single products
- 6:46:35that I have. So if I save this now let's
- 6:46:38run it first we'll do one by one and
- 6:46:40let's
- 6:46:42copy this
- 6:46:47paste it here and then if I enter so
- 6:46:49it's going to give me employee product
- 6:46:50okay whatever that we have added here
- 6:46:53that's fine now what I need to do right
- 6:46:56I only want to get certain columns only
- 6:46:59although this part we've already covered
- 6:47:01so I'm just going to comment it out
- 6:47:02let's do select so I only want to get
- 6:47:05name I only want to get category. I only
- 6:47:08want to get price and I only want to get
- 6:47:11the stock of this part individual
- 6:47:14product. And then you just have to do
- 6:47:16from and then you're going to give
- 6:47:18product. So this is going to give you
- 6:47:20only the four columns that you need. As
- 6:47:23simple as that, right? So again run it
- 6:47:25and you can see that you're going to get
- 6:47:26only four columns and it's very very
- 6:47:28clean, right? You can see that. Now for
- 6:47:30example that can be s possibility that
- 6:47:32you only want price column. So you just
- 6:47:35have to pass price here, right? So for
- 6:47:37example, let's say you only want price.
- 6:47:43Okay. So save this and let's clear this
- 6:47:46and run it once again. And you're only
- 6:47:48going to get price. Makes sense, right?
- 6:47:51So this is how you have to select
- 6:47:53specific columns. Now what I need to do,
- 6:47:55right, you can also add allis. Now allis
- 6:47:58actually are very useful and you'll see
- 6:48:00that in many applications developers
- 6:48:03will use allis. So allis is going to do
- 6:48:05this right. So allis is going to help
- 6:48:08you to understand the output column name
- 6:48:10much better way. So okay. So I'm just
- 6:48:12going to do allis creates.
- 6:48:15No I'm just going to do as so this is we
- 6:48:18have something called as this is sort
- 6:48:21this is in sort as allis. So I'm just
- 6:48:23going to do as creates and allis.
- 6:48:29Okay. for the output
- 6:48:35output of that column name. Output of
- 6:48:38that
- 6:48:40column
- 6:48:42name, right? And what is the advantage?
- 6:48:45No, definitely allis will be makes the
- 6:48:48column name
- 6:48:51easier to read. How we can do that?
- 6:48:55Simply we are going to do select and
- 6:48:57let's say I wanted to get name but I
- 6:48:59wanted to get this name column as
- 6:49:03you can give any name you want product
- 6:49:05name. So now I'll be able to now you
- 6:49:07might counter me okay when we have
- 6:49:09created this table we can definitely
- 6:49:11give this one as product name absolutely
- 6:49:12yes you can you can do that but there
- 6:49:14can be situation where actually want to
- 6:49:16use this as a license okay that can be
- 6:49:18so many situation can occur now let's
- 6:49:20say want to give here price I want to
- 6:49:22get the price but I wanted to get the
- 6:49:24price as my selling price okay or I
- 6:49:28wanted to get my stock and stock will be
- 6:49:30my how my how many available quantity
- 6:49:33quantities are there so I'll do
- 6:49:34available quantity and then always you
- 6:49:37have to give your destination. So you
- 6:49:39have to give from products. So if I save
- 6:49:41this also let's comment it off here.
- 6:49:44Save this one and see that what will
- 6:49:46happen. You're going to get the exact
- 6:49:47details or syntax error as something we
- 6:49:50have done. Maybe I haven't given comma.
- 6:49:52See so I have to give comma here. So
- 6:49:55save this and then if I clear this let's
- 6:49:59run it once again. So you're going to
- 6:50:00get no product name. This column name is
- 6:50:02not changed. This is a selling price and
- 6:50:04this is your available quantity and
- 6:50:05you're going to get as many details
- 6:50:07sorry as many columns you have that is
- 6:50:10that stays same only thing is your
- 6:50:13column name allis now change to the name
- 6:50:15that you want okay so this will be the
- 6:50:17concept of your select specific columns
- 6:50:20that you have already discussed here and
- 6:50:22then also how you can how you can use
- 6:50:23allis so with this now let's move on to
- 6:50:25the next one so next one maybe what we
- 6:50:27can do here I think where we have
- 6:50:29already discussed right where means I'm
- 6:50:31talking about this on this so where is
- 6:50:33actually we usually use for our basic
- 6:50:36filtering but still let's uh do one more
- 6:50:38time so that we will understand this one
- 6:50:40much better way so I'm just going to do
- 6:50:42this one as well basic filter
- 6:50:46dosql okay so first of all let's say I
- 6:50:49wanted to get couple of things that I
- 6:50:51wanted to get the products from one
- 6:50:53particular category and now why this is
- 6:50:55helpful let's say you're creating
- 6:50:56e-commerce website and you want to have
- 6:50:58a API call where these will be products
- 6:51:01and let's say you have category and this
- 6:51:03category will be electronics. You're
- 6:51:05getting what I mean. So I only wanted to
- 6:51:07get all the categories of this
- 6:51:08electronics, right? So all electronics
- 6:51:10category products. This is where your
- 6:51:13where filter is very very helpful. Okay.
- 6:51:16Now what actually where helps where
- 6:51:19helps? So this is going to reduce the
- 6:51:21rows returned by a particular query. Now
- 6:51:23let's how I'm going to do this. Now you
- 6:51:25have to try to do this all by your own.
- 6:51:28For example, let's say I wanted to get
- 6:51:29select. I wanted to get the name. name I
- 6:51:31wanted to get the category I wanted to
- 6:51:33get the price
- 6:51:35and I wanted to get from products
- 6:51:37definitely this is the table but now I'm
- 6:51:39just going to give okay where the
- 6:51:41category should be equal to electronics
- 6:51:45so you don't want all these uh products
- 6:51:50only one products where the category is
- 6:51:52electronics and this is going to give
- 6:51:53you all the electronics products so
- 6:51:55let's copy this the name and let's run
- 6:51:58it here.
- 6:52:01Okay. So now you can see you're getting
- 6:52:03name, category and these all are your
- 6:52:04electronics products. You make also
- 6:52:07confirm this just uh if I go here let's
- 6:52:10say you have furniture you can see the
- 6:52:11office chair and you can see this office
- 6:52:13chair is not coming here. Okay. So that
- 6:52:16means this is working. Now I'm going to
- 6:52:18give you some of the homework or maybe
- 6:52:20I'm going to do here also but try to do
- 6:52:21by yourself that you have to give uh you
- 6:52:24have to find some products
- 6:52:26where the price is greater than 1,000
- 6:52:31and it's very very simple. So what you
- 6:52:33can do you can again do select I want to
- 6:52:35get the name of the product I wanted to
- 6:52:37get the price of the product I want to
- 6:52:39get from my products table but I wanted
- 6:52:41to give a basic filter that where the
- 6:52:44product price has to be what greater
- 6:52:46than 1,000 that's it and if you run this
- 6:52:49you're going to get result and you'll
- 6:52:52see that you're going to get where the
- 6:52:53price is greater than 1,000. So this is
- 6:52:56another situation right and the same way
- 6:52:58you can do other also. Now let's say you
- 6:53:01only wanted to get where the is active
- 6:53:04is false. Correct? So let's see if we
- 6:53:06have an is active false or not. So we
- 6:53:09have two true true we have one false
- 6:53:11item. Okay fine. Then so let's say I'm
- 6:53:14just going to comment it out that I'll
- 6:53:16give that find products which are not
- 6:53:22active. So here I'm just going to do a
- 6:53:24select give name give me is_active
- 6:53:30from products and here you have to give
- 6:53:33what you have to give where e is active
- 6:53:35will be what equal to false right and
- 6:53:38this is going to give you where it is
- 6:53:40false and just run it here
- 6:53:46now it see all monitor and this is the
- 6:53:50product so this is how you can make use
- 6:53:53of your where filter. So I think this
- 6:53:56should be more than enough. Now moving
- 6:53:57to the next one. Next let's understand
- 6:54:00one more. So I'm just going to do 06 and
- 6:54:02let's give this one as and or not. These
- 6:54:06all are very important. Okay. These all
- 6:54:08are also filters all three. So let's
- 6:54:11close everything else. Okay. First one
- 6:54:14what is and I think this is universal
- 6:54:18concept right? Hands mean every
- 6:54:21condition
- 6:54:23must be true. Right? Then you have or at
- 6:54:29least
- 6:54:34one condition
- 6:54:37must be true.
- 6:54:39And then you have not. So not will be
- 6:54:42your reverse condition, right? Reverse
- 6:54:44or exclude. I'm going to say a
- 6:54:47condition. So let's say how this will
- 6:54:50work. Now let's say I wanted to make a
- 6:54:53filter or I wanted to create a filter
- 6:54:55which is going to return me electronics
- 6:54:57product. Okay. But if the price is
- 6:55:00greater than 100. So I I'm going to
- 6:55:03repeat once again that give me the
- 6:55:04products
- 6:55:06where it is electronics product. It is
- 6:55:09electronics
- 6:55:12electronics
- 6:55:14product but the price has to be greater
- 6:55:17than 1,000. So this is the query that I
- 6:55:20need to write. So how I can do that?
- 6:55:22Here you have a and condition. Why?
- 6:55:25Because both these condition has to be
- 6:55:27true. It has to be electronics and at
- 6:55:29the same time it has to be price has to
- 6:55:31be greater than 1,000. Makes sense,
- 6:55:32right? So that means if I try to do here
- 6:55:34select
- 6:55:36give me name give me category
- 6:55:42price from
- 6:55:45products.
- 6:55:46Now I have to give
- 6:55:49where the category is equal to
- 6:55:53electronics.
- 6:55:56And now I have to give here and filter.
- 6:55:59Okay. So I'm just going to do and let's
- 6:56:02do this one as price has to be greater
- 6:56:05than 1,000. Okay. So this is going to
- 6:56:07give you all the data that satisfy this
- 6:56:09condition. Now let's go one by one I
- 6:56:11think. Right. So we have electronics.
- 6:56:13Electronics electronics. So this USBC
- 6:56:15cable this should not come here. Why?
- 6:56:17Because this is electronics but this is
- 6:56:19less than 1,000. So let's see what is
- 6:56:21happening here. So I'm just going to
- 6:56:22save this. Go up. Let's clear this.
- 6:56:28Copy this. And then I'm just going to
- 6:56:31paste it here. And let's say how many
- 6:56:33data we are getting. All right. So you
- 6:56:35can see that you are getting five rows.
- 6:56:37Now if I just do this one as let's do
- 6:56:39this one as 100. And you'll see that
- 6:56:42this will increase for sure because now
- 6:56:44the condition is separate. Right? So I'm
- 6:56:46just going to run here. So you're
- 6:56:47getting six rows. So USB C cable this is
- 6:56:50also coming right. So this is the
- 6:56:52concept of and filter. Now what how you
- 6:56:56can use odd filter. Now see these things
- 6:56:58are not that much difficult to
- 6:57:00understand right you can do only once I
- 6:57:02can guarantee you'll be able to apply in
- 6:57:04a real project also and that is the
- 6:57:06reason you'll think that why these kind
- 6:57:07of videos are very important like if
- 6:57:09you're watching this video you can watch
- 6:57:11any kind of complicated project videos
- 6:57:13but if your basics are clear right you
- 6:57:15don't have to watch those videos also
- 6:57:17you'll be able to create project by
- 6:57:18yourself for example let's say you don't
- 6:57:20know the concept of where and and
- 6:57:22posgrsql and you started a AI project on
- 6:57:25YouTube uh that using posgress skill you
- 6:57:28can do that but learning concepts are
- 6:57:30very much important and how you going to
- 6:57:31apply [clears throat] those concept in a
- 6:57:33real project that is the main skill so
- 6:57:36now here I'm just going to commit it out
- 6:57:37now let's give one more filter here that
- 6:57:40uh what I'm going to do like maybe let's
- 6:57:42do that you have to give me products
- 6:57:45right where
- 6:57:48category is electronics
- 6:57:51electronics and let's see what else we
- 6:57:53have we have electronics and let's do
- 6:57:55furniture both Sorry uh category is
- 6:57:58electronics or this can be furniture
- 6:58:01right so give me the products where the
- 6:58:03category is electronics or it can be
- 6:58:05furniture it can be either so now I'm
- 6:58:08just going to do select so I'll give
- 6:58:10name category price and I'm just going
- 6:58:14to give from the destination products
- 6:58:17now what you'll do so where the category
- 6:58:20has to be either it is electronics
- 6:58:24and now we have to give a or condition
- 6:58:26here Right. So I'm just going to do this
- 6:58:27can be or.
- 6:58:30So or category can be furniture also.
- 6:58:34Okay. That's it. So now this is a or
- 6:58:38condition.
- 6:58:39So now let's run it once again.
- 6:58:43So we're getting both electronics and
- 6:58:45furniture. Fantastic. So this is about
- 6:58:47the and and or. Now let's understand
- 6:58:51some not condition, right? Not is going
- 6:58:53to exclude a particular condition. So
- 6:58:55now maybe let's do one thing that I
- 6:58:57wanted to return some products that are
- 6:59:00not in a maybe let's say stationary
- 6:59:02category something like that. So let's
- 6:59:04say how many categories we have. We have
- 6:59:05a stationary category here. You can see
- 6:59:07two products are there. So what I'm
- 6:59:09trying to do here that I wanted to get
- 6:59:11all the products where it is not
- 6:59:13stationary right so it's exclude
- 6:59:16condition. So here let's I'm just going
- 6:59:18to do okay give me name also give me
- 6:59:20only category. So we using selective fil
- 6:59:23uh column logic here that we have
- 6:59:24discussed here select specific columns
- 6:59:27also if you notice one thing that every
- 6:59:29time we are going forward with different
- 6:59:31concept we are trying to apply the
- 6:59:33previous concept that we have learned
- 6:59:35for example we have just now we have
- 6:59:37learned select specific columns and you
- 6:59:38can see that we are applying that
- 6:59:39concept this is the learning that I
- 6:59:41always mentioned in all my other videos
- 6:59:43that when you're learning incrementally
- 6:59:45right or gradually every time you're
- 6:59:47going to learn a new concept see if any
- 6:59:49of the concept from the previous thing
- 6:59:50you can apply there or not. That is the
- 6:59:52real learning that you can do. Okay. So
- 6:59:55here this will be select name and
- 6:59:56category. So I'm just going to do from
- 6:59:58products. And here what we need to do we
- 7:00:00have to do where and then I'm just going
- 7:00:03to do what not. Okay. So this is negate.
- 7:00:05It's a exclude condition and I'm just
- 7:00:07going to do category is equal to. So
- 7:00:09let's say maybe let's do furniture only.
- 7:00:11So I'm simply telling that where the
- 7:00:14category is not furniture you have to
- 7:00:15return me those products. So now let's
- 7:00:17run this again and you can see that you
- 7:00:19getting electronics stationary but
- 7:00:21you're not getting furniture. If you
- 7:00:23change this one to stationary you'll see
- 7:00:25that you're going to get now you're not
- 7:00:28going to get stationary here. Okay
- 7:00:34see you're getting category and
- 7:00:35furniture.
- 7:00:36So this will be your not condition. Now
- 7:00:40maybe let's do one more right. So let's
- 7:00:42do little complex that what you can do
- 7:00:45right you can also use parenthesis okay
- 7:00:47now for example let's say I want to do
- 7:00:49something like this I wanted to write
- 7:00:50one query let's see if you're able to
- 7:00:52understand or not so I'm just going to
- 7:00:53take the I'm going to get name I wanted
- 7:00:55to get category I also wanted to get
- 7:00:58price I also wanted to get uh stock also
- 7:01:01this time from I think this you know
- 7:01:03from products now I wanted to do right
- 7:01:06so give me where now I have to give a
- 7:01:08parenthesis parenthesis is nothing but
- 7:01:11it's going to If you're writing a
- 7:01:12complex logic, it is much much easier to
- 7:01:15understand and it is also safe. So here
- 7:01:17I'm just going to give okay category is
- 7:01:20equal to electronics
- 7:01:23and here I'm just going to or this can
- 7:01:25be so I'm just going to do category can
- 7:01:27be also furniture.
- 7:01:31Okay. And then I'm going to give another
- 7:01:33and condition here. So I'm just going to
- 7:01:34do okay and and let's give stock has to
- 7:01:38be greater than zero. That's it. Okay.
- 7:01:40So this is another we can apply here. So
- 7:01:44if I run this once again
- 7:01:47and you can see that you're going to get
- 7:01:49all this right. So you're going to get
- 7:01:50either it is electronics or it is
- 7:01:52furniture but the stock is greater than
- 7:01:55zero. Okay. Now I think this is done.
- 7:01:58What else? So maybe I think this
- 7:02:02uh I'm thinking to write one more. No, I
- 7:02:05think this should be enough or let's
- 7:02:06write one more here. So I'm just going
- 7:02:08to give one real world example. Okay. So
- 7:02:10let's do select let's do name price
- 7:02:14stock and I'll do e is active. Now I
- 7:02:17wanted to give is active also. So I
- 7:02:19wanted to give from products
- 7:02:21and now I'm just going to do where is
- 7:02:24active should be equal to true right? So
- 7:02:27it will be true and at the same time so
- 7:02:29I'm just going to do and the price is
- 7:02:33less than 1,000 or the stock can be
- 7:02:38greater than or equal to 100. See I just
- 7:02:40wanted to explain what you know by
- 7:02:42writing all this that you can do
- 7:02:43anything any combination you have to
- 7:02:45understand the concept. So first you
- 7:02:46tell me that you give me all the active
- 7:02:48products where the price can be less
- 7:02:50than 1,000 or stock has to be greater
- 7:02:53than or equal to 100. So this is also
- 7:02:56going to give you the result here. So
- 7:02:57let's see if I run once again
- 7:03:00and you can see that what you're
- 7:03:01getting. You notice in all here stock is
- 7:03:05actually greater than 100 and also it is
- 7:03:08less than 100. Why? Because you're using
- 7:03:10a or condition here the price is
- 7:03:13actually 100. So it's actually
- 7:03:14satisfying this condition. Price has to
- 7:03:17be less than 1,000. Okay. So that is the
- 7:03:19reason you're getting this particular
- 7:03:20combination. But what you need to see
- 7:03:22you have to see that this is active is
- 7:03:24actually true for all. Okay. So this is
- 7:03:27how you can make use of this and or and
- 7:03:30not filter. So with this now let's move
- 7:03:32on to the next one. Okay. So let's move
- 7:03:33on to the next one. Okay. So I'm just
- 7:03:35going to create one more file and let's
- 7:03:36give this one name as like I like and
- 7:03:41also let's give this one as patterns.
- 7:03:44SQL. Okay. So let's understand these
- 7:03:48some of the uh pattern matching that we
- 7:03:50can use. These are also very very
- 7:03:52important. So one we have like
- 7:03:56okay so this like is nothing but this
- 7:03:59will be case sensitive pattern match.
- 7:04:04Okay. Then you have something called I
- 7:04:06like. Okay. This will be case
- 7:04:08insensitive
- 7:04:11pattern
- 7:04:12match. Okay. Then you have something
- 7:04:15called I don't know how to say I think
- 7:04:18percentile I think this will be
- 7:04:20percentile only percentile means this
- 7:04:22will be I think any number of characters
- 7:04:26yes so this will be any number of cars
- 7:04:28okay or characters whatever you want to
- 7:04:29write and then you have uh we also have
- 7:04:32this I think underscore sorry uh this
- 7:04:35underscore this underscore will be means
- 7:04:38exactly one character so this will be
- 7:04:40exactly one character okay so let's see
- 7:04:42so first I wanted to do something like
- 7:04:45this. Okay, let me go here and I'll see
- 7:04:47that I have this wireless uh product.
- 7:04:50Okay, so I wanted to find some product
- 7:04:53where the name will start with wireless.
- 7:04:56Okay, so let's how we are going to do
- 7:04:58that and then I'm going to explain. So
- 7:04:59I'm just going to do here select and
- 7:05:01wanted to get name and price both and
- 7:05:04then first of all we need to do from
- 7:05:06products
- 7:05:08and now I will give that where the name
- 7:05:13like okay so the name like so here I'm
- 7:05:15using a pattern and I wanted to give
- 7:05:18here that this will be wireless okay and
- 7:05:20when I'm going to give this right here
- 7:05:24so this means so this after means
- 7:05:28anything can uh anything can come after
- 7:05:30it. Okay, this percentile after wireless
- 7:05:34means this percentile we have right. So
- 7:05:35this is after wireless text. So this
- 7:05:38means anything can come after that.
- 7:05:41Okay, I'm I will just write it here. So
- 7:05:44the percentile
- 7:05:46percentage whatever after wireless
- 7:05:50means
- 7:05:53anything can come after it. Okay. So
- 7:05:58let's see how this is happening. So save
- 7:05:59this and I'll go here. Let's clear this.
- 7:06:02We'll do one by one and then I'm just
- 7:06:04going to rename copy this.
- 7:06:08Let's remove this.
- 7:06:13Okay. So here you can see that what you
- 7:06:15are getting. Okay. Something we getting
- 7:06:17syntax is there. Okay. Got it.
- 7:06:21To give this Oh, we we have to remove
- 7:06:24this. I've given in the wrong place. So
- 7:06:26let's run once again. Clear.
- 7:06:30Okay. See. So now you're getting
- 7:06:32wireless mouse and it makes sense,
- 7:06:33right? Because we have only one product.
- 7:06:36All right. Now what this line you
- 7:06:38understand that this anything can come
- 7:06:40after it. So this is what I'm talking
- 7:06:41about. So anything can come after
- 7:06:43wireless. But this is going to do that
- 7:06:45matching. Okay. Next. Let's do one more
- 7:06:48example that. Okay. Let's see how many
- 7:06:51products we have. Uh what else? Maybe
- 7:06:53let's do something with this desk. Okay.
- 7:06:56So I wanted to find some product where
- 7:06:58the name contents desk. Now what this I
- 7:07:02like is going to do as I already
- 7:07:04mentioned is this is a case insensitive
- 7:07:06means it's usually going to ignore any
- 7:07:09kind of upper case or lower case
- 7:07:10differences. So what I mean by this what
- 7:07:13is like how we understand that this is
- 7:07:16going to ignore. So for example let's
- 7:07:17say this is going to match with desk.
- 7:07:21This is also going to match with desk.
- 7:07:23So you can see that this is capital this
- 7:07:25is small and this is also going to match
- 7:07:28with disk you're getting right. So this
- 7:07:30is what we are talking about case
- 7:07:32insensitive. So let's have this is
- 7:07:33happening. So I'm just going to do name.
- 7:07:35Let's also take category now and I'm
- 7:07:37also going to do price and here I'm just
- 7:07:40going to do again from products. And now
- 7:07:42I'm just going to do where name now you
- 7:07:45have to do I like. Okay very very
- 7:07:47important. And you just have to give
- 7:07:49this will be desk.
- 7:07:51Okay. So this is going to do this
- 7:07:53particular match. So now let's save this
- 7:07:56and run once again and we'll see what is
- 7:07:58happening here.
- 7:08:02Okay. So you're getting a standing desk
- 7:08:04and I think we have only one. So this is
- 7:08:06the standing desk. Okay. So we have
- 7:08:08learned these two I like. Sorry like and
- 7:08:10I like. Okay. So let's write one more
- 7:08:12here. So maybe uh you can do right. So
- 7:08:15you can also search across multiple
- 7:08:17columns. So for example, let's say let's
- 7:08:19go here and maybe we'll take this
- 7:08:22example. Okay, let me just check. Okay,
- 7:08:24so this is good. So we have chair in
- 7:08:26this name also and let's see have chair
- 7:08:28in this description also. So you can
- 7:08:30check in both the columns. Okay, so for
- 7:08:32example, let me just comment it out
- 7:08:34here. This one and also this one.
- 7:08:38So we'll just do select. Let's do name
- 7:08:41category
- 7:08:43and also I'm just going to take the
- 7:08:44description also. Okay. And here I'm
- 7:08:47just going to do from and I'll do
- 7:08:49products. And now first of all I will do
- 7:08:52where the name. So here I'm just going
- 7:08:55to do I like and I'm just going to give
- 7:08:57this one as so I wanted to take chair
- 7:08:59right. So I'll do chair. And then what
- 7:09:02you can do right you can give a or.
- 7:09:04Okay. Now this one we have already
- 7:09:06discussed in the previous part. So you
- 7:09:07or the description I like and then you
- 7:09:11have to give the pattern that you wanted
- 7:09:12to match. For example I wanted to check
- 7:09:14the chair word only. So this is how you
- 7:09:16need to check with multiple columns.
- 7:09:18Okay. So let's save this and we'll
- 7:09:21[clears throat] go let's run this
- 7:09:24and you can see that you're getting
- 7:09:25office chair and this is also
- 7:09:27comfortable chair. So this both you
- 7:09:29have. So this is how you can make use of
- 7:09:31like I like and percentile. Okay. So now
- 7:09:35maybe let's move on to the next one.
- 7:09:37What what else we have? Let me think.
- 7:09:39Okay. We also do in not I think in we
- 7:09:42have already covered in some uh previous
- 7:09:44uh parts but still let's do it. So I'm
- 7:09:46just going to create one more. So I'm
- 7:09:47just going to do 08.
- 7:09:50So let's do here. Let's give in. We also
- 7:09:52have to not and also sorry we have in
- 7:09:56not in and between this. So we'll do in
- 7:09:59not in and also let's do between this
- 7:10:01also important between also we have to
- 7:10:04understand. Okay. So first let's start
- 7:10:06with in. So in will be in simple way
- 7:10:10value that value you want to check that
- 7:10:13must match one item from the list.
- 7:10:15Simple. So here value must match one
- 7:10:20item from the list. So this will be in.
- 7:10:24Next you have not in. So what actually
- 7:10:27not in? So this will be value must not
- 7:10:30match. By the name only you can
- 7:10:31understand right? So this will be value
- 7:10:33must not match any item from the list
- 7:10:39and then you have something called
- 7:10:41between. Okay, this is also important.
- 7:10:43So between will be value must be inside
- 7:10:46particular range because from the name
- 7:10:47only you can understand. So this will be
- 7:10:49value must be between or I'm just going
- 7:10:52to write in a better way. This will be
- 7:10:54inside a range. Okay. So now let's say
- 7:10:57what we are going to do right. So in the
- 7:10:59previously we have done what that we are
- 7:11:01checking right that if the category is
- 7:11:03electronics or the category is furniture
- 7:11:06here we can use in instead of doing that
- 7:11:08how so now let's I'm just going to do
- 7:11:10select name give me category and also
- 7:11:13give me the price from products right
- 7:11:17now what I'm going to do right I'm going
- 7:11:18to check where the category in so
- 7:11:20previously we have done okay category is
- 7:11:22equal to this or category equal to that
- 7:11:24like that but here I'm just going to do
- 7:11:26give me in so that means these value
- 7:11:28must be one of that value that I wanted
- 7:11:30to check. So value can be either
- 7:11:32electronics or the value can be either
- 7:11:35furniture.
- 7:11:38I hope this is clear. This is not that
- 7:11:39much difficult to understand. Simple.
- 7:11:41Okay. So this is where you can use this
- 7:11:43in.
- 7:11:45So clear this and let's run it here.
- 7:11:51Okay.
- 7:11:54Right. So you're getting electronics and
- 7:11:56furniture stationary. you are not
- 7:11:58getting. Okay. So now you can also
- 7:12:01double check this one. Maybe let's say I
- 7:12:02wanted to check this stationary and I
- 7:12:04wanted to remove this electronics,
- 7:12:05right? Save this and you are only going
- 7:12:08to get now stationary and furniture. So
- 7:12:10you're getting furniture and stationary.
- 7:12:12All right. So this will be in. Very easy
- 7:12:15to understand. Next uh what else we
- 7:12:17have? We have not in. Not in is again
- 7:12:19it's going to exclude multiple values.
- 7:12:21Means I wanted to return all the
- 7:12:24products that are not electronics, not
- 7:12:26furniture. So that means what if it is
- 7:12:28not electronics not furniture means it
- 7:12:30is only going to give me stationary
- 7:12:31products. So how we can do that? So do
- 7:12:33name category from products. And now you
- 7:12:37have to give me where the category
- 7:12:41not in. Okay. And then you're going to
- 7:12:43give okay you want to check that okay
- 7:12:45this has not be has uh has to be only
- 7:12:48stationary. I'm talking opposite. So it
- 7:12:50has to be only stationary. It don't have
- 7:12:53to be electronics and also it should not
- 7:12:55be furniture. So we excluding both. And
- 7:12:59here the part is important that is not
- 7:13:01in. And if you run this, so you're only
- 7:13:03going to get the stationary product,
- 7:13:06right? See, so you're getting only
- 7:13:07stationary product. Simple. So this is
- 7:13:10about the not in. What else we have? We
- 7:13:12also have the between
- 7:13:14how we can check. Now let's say I wanted
- 7:13:16to check on the price that give me all
- 7:13:18the products where the price is between
- 7:13:20100 to 2,000 something like that. So it
- 7:13:23will be in that bit range right. So in
- 7:13:25that case we can check that. So now here
- 7:13:27you can also use and that if it is
- 7:13:29greater than 100 less than this like
- 7:13:31that but this uh this in not in between
- 7:13:34are very very very useful once you start
- 7:13:36using all of these. So I'm just going to
- 7:13:38do this one as price. I'll do from
- 7:13:42products. And what you have to give? You
- 7:13:44have to give where you want to check the
- 7:13:45property. So I have to check the price
- 7:13:47property. And this will be between. And
- 7:13:49now you have to give okay 100 and see we
- 7:13:52using and and 2,00.
- 7:13:55So it will give me in that range only.
- 7:13:59So now
- 7:14:01also it this is also important. This is
- 7:14:03going to include 10 and 2,000 if I'm not
- 7:14:06wrong. So let me just quickly check. So
- 7:14:08include means it's not like from 101 and
- 7:14:111 9. It is also going to add 100 and
- 7:14:142,000 also. So let me just check that.
- 7:14:17Will this
- 7:14:19include 100 and 2,000 also. I think yes.
- 7:14:25Just confirm.
- 7:14:31Okay. Right. So this is see it is like
- 7:14:34where right? It is similar to where
- 7:14:36price is greater than equal to 100 and
- 7:14:38price is less than or equal to 2,000. So
- 7:14:40that means this is also going to add
- 7:14:42sorry this is going to keep 100 and
- 7:14:432,000 also. Makes sense right? So now if
- 7:14:45you run this you can see that you're
- 7:14:46getting 100 see all three two uh three 9
- 7:14:49and 1 2 9. So it will be in that range
- 7:14:51but again I'm repeating this is also
- 7:14:53going to include 100 and 2,000
- 7:14:55important. So I think this is done. Next
- 7:14:58let's comment it out. What else is left?
- 7:15:02Uh
- 7:15:04I think we have covered all three right
- 7:15:06now we can let's do one more
- 7:15:07combination. So let's say I'm just going
- 7:15:09to get name category price and stock
- 7:15:14from products.
- 7:15:17Now I'm just going to do where the
- 7:15:18category in. So give me electronics
- 7:15:24and maybe let's say stationary product.
- 7:15:30And then I'm also going to do here and
- 7:15:33price between 100 and 1,500 or whatever
- 7:15:38you wanted to give 149 something like
- 7:15:40that you're getting how we are combining
- 7:15:43all of these all the concept that we are
- 7:15:44learning so far [snorts] and if you run
- 7:15:46this so clear this and run it here
- 7:15:49you're going to get all this result here
- 7:15:50okay so this is how you have to do it in
- 7:15:53stationary I think we don't have let's
- 7:15:54see if stationary if it is 101,499
- 7:15:58so this is 99 in stationary. So if I
- 7:16:01make this one to 10, so it should give
- 7:16:03me stationary also if I'm not wrong. So
- 7:16:06clear this and run once again. Now
- 7:16:08you're getting stationary also because
- 7:16:09both are less than 100. Okay. So this is
- 7:16:12about in, not in and between. All three
- 7:16:15that you have learned so far in this
- 7:16:17particular file are actually very
- 7:16:18important. So now what else we have? We
- 7:16:20also have something called null
- 7:16:23filtering. And I think we have already
- 7:16:25covered that null and not null. But
- 7:16:26still it is important. So let's do let's
- 7:16:29not skip this. So I'm just going to do
- 7:16:31this one as null. Let's do and uh what
- 7:16:34I'll do is not null something like this
- 7:16:36is not nullsql.
- 7:16:40So here first of all null filtering
- 7:16:42means again null means very important
- 7:16:44means missing value or unknown value
- 7:16:47something like this.
- 7:16:50Okay. So here what we need to understand
- 7:16:55right that when you're going to check
- 7:16:57you should not check with null you
- 7:16:59should not check null using equal to
- 7:17:01null so what I'm talking about that you
- 7:17:03should not you should not check null
- 7:17:09using equal to null you should not do
- 7:17:11that so correct is what we need to do
- 7:17:13either we need to check is null or we
- 7:17:16have to check is not null okay so this
- 7:17:19is the important part here. Now let's
- 7:17:21say [snorts]
- 7:17:22here what we need to check that I wanted
- 7:17:24to get all the products but the
- 7:17:25description is missing. I'm just giving
- 7:17:27example. Okay. Now here we have to see
- 7:17:30that this doesn't mean the empty string
- 7:17:33that mean that no value is present for
- 7:17:35that particular description property. So
- 7:17:38here so I'll do select let's say select
- 7:17:40name comma description
- 7:17:44from products
- 7:17:47and here I'm just going to do where the
- 7:17:50description
- 7:17:51is null okay so simply telling the
- 7:17:54description is not present so let's run
- 7:17:56this okay so clear this and I'm just
- 7:17:59going to copy here and let's run it
- 7:18:03here.
- 7:18:07Okay. So you can see that USBC cable and
- 7:18:10jail pen. So let's see what is happening
- 7:18:11here. So you can see that we have null
- 7:18:12here. Okay. And for this also we have
- 7:18:15null here. So you're directly checking
- 7:18:17that if this is equal to null. Correct.
- 7:18:20Now you want to check that where the
- 7:18:22description is available. So how you're
- 7:18:24going to do that is not null. Okay. So
- 7:18:26here you copy this and then you're going
- 7:18:29to do is not null. That's it. So this is
- 7:18:33the difference and if you now run this
- 7:18:35so you're going to get all the products
- 7:18:36where the description is available.
- 7:18:38Okay. So we need to understand again I'm
- 7:18:40repeating this thing that null means
- 7:18:43missing value or unknown value. But when
- 7:18:45you're going to check null using equal
- 7:18:47to null you can't check. You should
- 7:18:49check with is null or is not null. Now
- 7:18:52let's see a practical example right
- 7:18:53practical back end example. Let's say
- 7:18:55you're creating admin dashboard where
- 7:18:56admin wanted to check something like
- 7:18:58that. Okay me [snorts] all the active
- 7:19:00products right? These products are
- 7:19:01active that means user can see the
- 7:19:03active products from the e-commerce
- 7:19:05website. But for this particular product
- 7:19:07the description is missing. So admin
- 7:19:09admin needs to query this. So how we we
- 7:19:12can do that? So I'm just going to do
- 7:19:13okay give me name give me the category
- 7:19:15of this product. Give me is active
- 7:19:19and also let's say give me description
- 7:19:21also. So from products
- 7:19:25where
- 7:19:26is active is equal to what? True. And
- 7:19:29here you have to give and
- 7:19:31description is null.
- 7:19:34That's it. So this is going to give you
- 7:19:36all the active products but at the same
- 7:19:38time the description is missing. So
- 7:19:40let's see if we have so this product
- 7:19:42both are active. So you can see that
- 7:19:43both are active but description is null.
- 7:19:45So this is how you can make use of null
- 7:19:48and is not null. Okay. So with this I
- 7:19:51think we have covered most of the
- 7:19:53things. Now still we have lot of things
- 7:19:54to cover in this one. So what are the
- 7:19:56things still we need to cover? We need
- 7:19:57to cover order by limit offset all these
- 7:20:00things how we can update right we
- 7:20:02haven't started update yet that how we
- 7:20:03are going to update a single row how you
- 7:20:05can update multiple rows how you can
- 7:20:07delete rows and also the concept of
- 7:20:09returning so all these things we're
- 7:20:11going to understand in the next part all
- 7:20:12right so let's move on to the next one
- 7:20:14which will be so let's understand order
- 7:20:15by
- 7:20:19now this is one of the most used uh
- 7:20:23concept that you're going to use because
- 7:20:25in projects sorting
- 7:20:27that you're going to use is everywhere,
- 7:20:29right? Almost in every functionality or
- 7:20:31most of the functionality I will say you
- 7:20:33have to do some kind of sorting right so
- 7:20:35here uh so let's understand so maybe
- 7:20:37let's say let's do this one practically
- 7:20:39that we wanted to sort our products from
- 7:20:42cheapest to expensive right so that
- 7:20:45means it will be in ascending order now
- 7:20:47these are very simple concepts I'm just
- 7:20:49going to do select and give me name and
- 7:20:51the price of the product from my
- 7:20:54products collection and you just have to
- 7:20:57use order Y okay and then you have to
- 7:21:00give the property and I wanted to do
- 7:21:03what I wanted to sort based on this
- 7:21:05price property right so and also what
- 7:21:07kind of sort you wanted to do that also
- 7:21:09we need to give so I wanted to give
- 7:21:11ascending order so this will be my query
- 7:21:13very very simple and if I just run this
- 7:21:15and we'll see that this is going to give
- 7:21:17me the name and the price in ascending
- 7:21:19order so let's run this
- 7:21:23okay and you can see that this is
- 7:21:25working fine so from 25 to 15,990 29.
- 7:21:29All right. So I think this is done. The
- 7:21:31same thing you can do for descending
- 7:21:33order also. So I'm just going to do
- 7:21:35exact same. So I'll copy this, paste it
- 7:21:38here. And this will be descending order.
- 7:21:41Okay. Nothing else. So it will be from
- 7:21:43expensive to cheapest. So do clear. And
- 7:21:47now this will be opposite. That's it.
- 7:21:49Nothing much to discuss here. Then what
- 7:21:51else you can do? Right? You can also
- 7:21:54sort multiple columns. Okay. So what I
- 7:21:56mean by this? Now let's say I wanted to
- 7:21:59do something like that. Okay, give me
- 7:22:01name, give me the let's do category and
- 7:22:05also give me the price from products and
- 7:22:09now I'm just going to do order by so I
- 7:22:12wanted to do category in ascending order
- 7:22:15and I wanted to do price in descending
- 7:22:17order. So this also you can do. So if I
- 7:22:20now save this and let's see what we are
- 7:22:22getting. So clear this and run it again.
- 7:22:25So you can see that for price this will
- 7:22:27be in the ascending order sorry
- 7:22:29descending order and the category will
- 7:22:30be in the ascending order. Okay.
- 7:22:33So now uh what else I think for this one
- 7:22:35this will be more than enough right? We
- 7:22:36don't have any uh anything else to
- 7:22:38discuss. Let's move to the next one
- 7:22:40which will be limit and offset that we
- 7:22:43need to discuss. So I'm just going to
- 7:22:44create one more here and let's give this
- 7:22:46one as underscore limit offset example.
- 7:22:50Okay.
- 7:22:52And pagination something like this.
- 7:22:55Now these all are very very practical
- 7:22:57thing. Okay. Now these you're going to
- 7:22:58use let's say you're implementing pagin
- 7:23:00essence. You have to implement all this
- 7:23:01logic correct. Now also this is basic
- 7:23:05pagination. I'm not going go into much
- 7:23:07complicated things. Now what actually
- 7:23:09limit and offset means so that you need
- 7:23:11to understand. So limit means how many
- 7:23:14rows you want to return. Okay. So limit
- 7:23:16means how many rows you want to return
- 7:23:22and what will be the offset. So offset
- 7:23:26will be controls so offset is going to
- 7:23:30control how many rows we want to skip.
- 7:23:33So this will be how many rows
- 7:23:37we want to skip. Right? So now let's say
- 7:23:42how it will work. So for example, you
- 7:23:44have a task that you only want to return
- 7:23:46the five products. Okay. So I wanted to
- 7:23:49return five products and I wanted to
- 7:23:51sort the name of the product in
- 7:23:53ascending order. So this is the query
- 7:23:55you have to write. So how you can do
- 7:23:57that? So let's say I wanted to do name,
- 7:23:59give me name, give me the price of that
- 7:24:02product,
- 7:24:04right? And here I'm just going to do
- 7:24:05this one as
- 7:24:07from products.
- 7:24:11Now I first I wanted to apply my order
- 7:24:13by
- 7:24:16name and this will be ascending
- 7:24:18and now what you have to do you have to
- 7:24:20give a limit property sorry this will be
- 7:24:22limit property and then because I wanted
- 7:24:25to return only five products so I wanted
- 7:24:27to give the limit as five so this is
- 7:24:29going to do what it's going to give me
- 7:24:31selective columns all the concept you
- 7:24:32have discussed previously so this is the
- 7:24:35concept of selective columns this is
- 7:24:36from where I wanted to get this is going
- 7:24:38to order by means it's going to do the
- 7:24:40sorting on name based on ascending order
- 7:24:42and this will be the limit when this
- 7:24:43many products I wanted to fetch and if I
- 7:24:46now clear this let's run it here
- 7:24:50I'll copy this
- 7:24:52and you can see that you're getting only
- 7:24:54five products right now remember it's
- 7:24:56also going to do the this order by based
- 7:24:59on name right it's going to do the in
- 7:25:01ascending order and you are only getting
- 7:25:03now let's say if I wanted to do price
- 7:25:04here price I wanted to do in ascending
- 7:25:07order now this will completely change
- 7:25:08now you can see that you're getting in
- 7:25:10ascending order. If you want to do in
- 7:25:12descending order, you can also do that.
- 7:25:14So you can let's say clear this and run
- 7:25:17it again. So you are going to get in
- 7:25:18descending order. But you are always
- 7:25:20going to get only five products because
- 7:25:22you are limiting the query by five
- 7:25:24products. Okay. So this is the use of
- 7:25:26limit. Now also now let's say in
- 7:25:29normally what will happen in the
- 7:25:30pagination. So in the first page you are
- 7:25:32going to get so this is page number one
- 7:25:34you're going to get five products. In
- 7:25:36page number two you're going to get five
- 7:25:37products. Or let's say you're going to
- 7:25:39get the product from zero sorry 6 to 10.
- 7:25:41Why? Because in first page you only have
- 7:25:45get the five products. So you're going
- 7:25:46to skip in third page again we are going
- 7:25:48to skip all 10 products because in the
- 7:25:51previous two pages we have already
- 7:25:52rendered those products. So we don't
- 7:25:54want to render. So this is where the
- 7:25:55concept of offset comes. Okay. So let's
- 7:25:58say what I need to do right I wanted to
- 7:26:01show on page one example that we wanted
- 7:26:03to do offset. Offset how offset is going
- 7:26:06to work. So offset zero means we don't
- 7:26:09want to skip anything. Offset five means
- 7:26:12we wanted to skip five products. You're
- 7:26:14getting what I mean? So let's do page
- 7:26:16one example. So consider you're
- 7:26:17implementing a page incent and I'm
- 7:26:19implementing the page one uh products.
- 7:26:22Okay? Or the query. So I'll do name give
- 7:26:24me price from products.
- 7:26:29Let's do order by only. Okay. So I'm
- 7:26:30just going to do order by based on name
- 7:26:32and I'll do ascending order only. And
- 7:26:34here I'm just going to do limit five.
- 7:26:36And now I need to do offset. So offset I
- 7:26:40wanted to give zero. Why? Because when
- 7:26:42you're going to render the first page
- 7:26:45products, you are not going to skip
- 7:26:47anything, right? Because in the first
- 7:26:48page, you need to render all the first
- 7:26:50products. So your offset will become
- 7:26:52zero. So now if you just do this one,
- 7:26:55right? First of all, clear this.
- 7:26:57[snorts] And if I just run this, so
- 7:26:59you're going to get five products. And
- 7:27:00these will be the five products, the
- 7:27:02first five products. Okay. Now let's see
- 7:27:04how page two will look like. So I'm just
- 7:27:06going to copy exact same thing and then
- 7:27:09I'm just going to paste it here. What
- 7:27:10will be the difference in the page two
- 7:27:13what you have to do right? You have to
- 7:27:14give a offset of five. Why? Because for
- 7:27:16the page one you've already rendered the
- 7:27:19five product. When you're going to
- 7:27:20render the page two you want to skip
- 7:27:22those five products and you want to
- 7:27:23render the next set of products. Okay.
- 7:27:26So if I now save this let's see how this
- 7:27:28is working. And you'll see that [snorts]
- 7:27:29if I clear this right and next time if I
- 7:27:31render you can see that both the page
- 7:27:34record is different because this is a
- 7:27:36first page data this is a second page
- 7:27:38data in the second page we already
- 7:27:40skipped this one because we've already
- 7:27:41rendered this five in the first page so
- 7:27:43this is the concept of offset
- 7:27:46now how the practical API example look
- 7:27:49like right so in the practical example
- 7:27:52what will happen right normally this is
- 7:27:53how it will look so consider you have if
- 7:27:56I'm not wrong so it would be page page
- 7:27:58minus one into limit. So let's say you
- 7:28:02are in the page two. So it will become 2
- 7:28:05minus one. So into let's say you are
- 7:28:08rendering five products. So five. So
- 7:28:10that means how this will work. So let's
- 7:28:12say in the zeroth page you're going to
- 7:28:14render five for the second page. So this
- 7:28:16will become five. For the third page
- 7:28:19this will become 3 - 1 into 5. So this
- 7:28:24will become 10. So when you're going to
- 7:28:26go to the third page, your offset will
- 7:28:28become 10. Correct? Because in the first
- 7:28:30page, you've already rendered five
- 7:28:32products. In the second page, you
- 7:28:33already rendered five products. So total
- 7:28:3510 products. So in the third page, you
- 7:28:37want to skip 10 products. Correct?
- 7:28:39Because you want to render from 11th
- 7:28:41product. So this is how it will look. So
- 7:28:43let's say if I just do same thing,
- 7:28:45right? So this will be my third page
- 7:28:47example here. So this will become 10. So
- 7:28:49I wanted to skip 10 products because I
- 7:28:52already rendered and you are going to
- 7:28:54get the last set of products. So this
- 7:28:57will be your concept of limit offset and
- 7:29:00pagenation. Okay. So now uh I'm going to
- 7:29:04pause here. It's not pause for you but
- 7:29:06I'm going to pause and then we I'm going
- 7:29:08to start in the next part the update
- 7:29:10part and delete part. So let's see how
- 7:29:12we we are going to do that in the next
- 7:29:13section. All right. So moving to the
- 7:29:15next one. So let's create the next file
- 7:29:17which will be how we are going to update
- 7:29:19single row. Okay. So I'll do update
- 7:29:21single row dosql.
- 7:29:24So let's see how we are going to update
- 7:29:25data. This is also very very important.
- 7:29:27Okay. So first of all let's do one
- 7:29:29thing. Let's see that you can see that
- 7:29:32we have this particular product right.
- 7:29:33So we have this mechanical keyboard
- 7:29:35electronics blah blah. So [snorts] what
- 7:29:36I'm going to do right. So first I'm
- 7:29:38going to see that whether this data is
- 7:29:40present or not. So I'm just going to do
- 7:29:41select name. Let's take price. Let's
- 7:29:44take stock and also I'm going to take
- 7:29:46the skew. So I'm just going to do from
- 7:29:49products and then I'm simply going to do
- 7:29:51where skew is equal to. So let's copy
- 7:29:54this. Right? So I'm just going to copy
- 7:29:56this
- 7:29:58and then I'm just going to paste it
- 7:29:59here. Okay. So this is the first thing.
- 7:30:02So now let's go here and let's say I'm
- 7:30:04just going to do clear
- 7:30:07and let's run this file.
- 7:30:14All right. So we have one data fine. Now
- 7:30:17my task is that what I need to do right.
- 7:30:19So let's say I wanted to update the
- 7:30:22price from 349 to something else and
- 7:30:25also I wanted to change the stock from
- 7:30:2715 to 23. So how I can do that? So first
- 7:30:30of all what we need to do right we need
- 7:30:32to do update right? So we are simply
- 7:30:34telling that we need to update this
- 7:30:36particular product. But where we want to
- 7:30:38update? So we wanted to update in a
- 7:30:40table right? So you always need to give
- 7:30:41the table name. So I want to update in
- 7:30:44the products table. Now next will be
- 7:30:46what you want to update. So this will be
- 7:30:48set that okay set means okay this will
- 7:30:50be my new value. So I wanted to set some
- 7:30:52value. So set will be that okay I wanted
- 7:30:54to make the price as 119.00
- 7:30:58comma you can update multiple properties
- 7:31:00also rows also and I wanted to update
- 7:31:02stock from 15 to 23. That's it. But this
- 7:31:06is not the case. Why? Because this is a
- 7:31:08single row update. So you need to tell
- 7:31:10that okay because your SQL doesn't know
- 7:31:12right that which product you wanted to
- 7:31:14update you can have 100 product thousand
- 7:31:16product lots of products. So here we
- 7:31:18need to use where clause not wear clause
- 7:31:20where filter so [snorts] here I'm just
- 7:31:22going to do skew and then I'm going to
- 7:31:24simply tell that okay you are updating
- 7:31:25this particular product here. Okay so
- 7:31:29I'm simply telling that update products
- 7:31:31table change the price from that
- 7:31:33particular value to 119.00 0 0 5 0
- 7:31:36whatever you wanted to give stock I
- 7:31:38wanted to update to 23 where the skew is
- 7:31:41equal to this skew
- 7:31:43I hope this is clear right so now what I
- 7:31:46going to do right let's copy this and
- 7:31:48then I'm just going to run it here so
- 7:31:50what I'm doing that I'm first checking
- 7:31:52if that is present I'm updating the data
- 7:31:54and then I want to see the updated data
- 7:31:57that whether the update is actually
- 7:31:59happened or not so now let's run it once
- 7:32:01again and we'll see that what is
- 7:32:02happening here okay so I'm just going to
- 7:32:04run here all right now you can See that
- 7:32:06we are getting 349. Now it's changed to
- 7:32:0811 1 9 stock changed from 15 to 23. It's
- 7:32:11very simple to understand the key that
- 7:32:13you need to know that how we going to
- 7:32:15set the data. Set data means you need to
- 7:32:17use this set whatever properties wanted
- 7:32:19update where this is the important part
- 7:32:21because you are updating a single row.
- 7:32:23Okay. So this is all about the single
- 7:32:25row update uh updated updates. Now
- 7:32:28[snorts] next will be uh let's take
- 7:32:30multiple rows. So I'm just going to take
- 7:32:3213. Let's do update
- 7:32:35multiple
- 7:32:37rows
- 7:32:38SQL. Okay. Now here first of all let's
- 7:32:43see what we can do. So first of all
- 7:32:45let's do one thing. Let's see we have
- 7:32:47some stationary product right. So this
- 7:32:48stationary product that we have. So
- 7:32:51first I want to see that okay how many
- 7:32:53stationary product I have. Okay. So that
- 7:32:55will be my first step. So let's do one
- 7:32:56thing. So I'm just going to do select.
- 7:32:58So do name. Give me also category. also
- 7:33:01give me price and let's say I wanted to
- 7:33:03also get the is active from where so
- 7:33:06from products table and then I'm simply
- 7:33:08going to do where I wanted to check the
- 7:33:10category is equal to stationary so I'm
- 7:33:12just going to copy this and let's paste
- 7:33:14it here okay so this is the first thing
- 7:33:16copy this sorry save this and then let's
- 7:33:18run it first let's copy here
- 7:33:24and then I'm simply going to paste it
- 7:33:28so we have two data makes sense
- 7:33:31what I need to do right so let's say you
- 7:33:33have some requirement that what you need
- 7:33:34to do you need to update all the
- 7:33:36stationary products prices these prices
- 7:33:39by 10%. So you have to increase that.
- 7:33:41Okay. So how we are going to do that?
- 7:33:44Now here definitely you don't want to do
- 7:33:46let's say you have 1,000 data you you
- 7:33:48are going to do single single update. No
- 7:33:50right. So there has to be some way that
- 7:33:52where you actually update all the
- 7:33:54products price by 10% at once. So to do
- 7:33:58this what we can do right simply I'm
- 7:34:00just going to do update
- 7:34:02and in this update so we are going to
- 7:34:04simply again this is same products. So
- 7:34:06I'm just going to do set and I'm going
- 7:34:08to do okay price now equal to so I'm
- 7:34:10going to take a round and I'm simply
- 7:34:12going to say that okay this price will
- 7:34:14now become 10% more or whatever you want
- 7:34:17to do let's say you want to increase by
- 7:34:18+ 10 whatever you want to do for now I'm
- 7:34:20just going to increase by 10% and I'm
- 7:34:22going to take this one comma 2 right so
- 7:34:24round two means it's going to keep our
- 7:34:26two decimal places sorry this will be
- 7:34:27two and now the important part how you
- 7:34:30are going to update also here you need
- 7:34:32to just pass that okay where the
- 7:34:33category is stationary so where the
- 7:34:35category is stationary simply telling
- 7:34:37that take all the stationary products
- 7:34:40increase the price by 10% and then you
- 7:34:42can also check it once again right so
- 7:34:43I'm just going to copy this and then you
- 7:34:45can run it here save this and you can
- 7:34:47see that what we are getting right so
- 7:34:49let's clear this run it again and you
- 7:34:52can see that this is increased by 10%
- 7:34:54this is also increased by 10% right so
- 7:34:5625 into 10 into 10% 2.5 so this is 27.5
- 7:35:00and you can see this is updated in both
- 7:35:02the rows so this is how you can update
- 7:35:04multiple column s at once. You can also
- 7:35:07do right. So let's say uh what you want
- 7:35:10to do right you want to mark out of
- 7:35:12stock products inactive. So how you can
- 7:35:14do that? So to do this one what you can
- 7:35:16do right you can just do update and
- 7:35:18let's say you want to do products and
- 7:35:20then you want to do set is active equal
- 7:35:23to false. So you're changing all from
- 7:35:25true to false where the stock is equal
- 7:35:29to zero. So this is going to do what?
- 7:35:31whenever there will be any products that
- 7:35:34is out of stock. So zero means it's out
- 7:35:36of stock. So you're setting that product
- 7:35:38as inactive. That's it. And then you can
- 7:35:40if you want to check it. So you do
- 7:35:42select name stock and let's say is
- 7:35:45active from products.
- 7:35:49And then I'm simply going to do where is
- 7:35:52active is equal to false. Okay. So this
- 7:35:55is where you're going to get this one.
- 7:35:57So I'm just going to comment it out
- 7:35:58here. Save this.
- 7:36:01Clear this and let's run it again. So
- 7:36:03you have only one. So this is changed to
- 7:36:05false. So we have old monitor. Let's see
- 7:36:08where we have old monitor and this is
- 7:36:09the stock is zero. So we're changing
- 7:36:11this one to false. Okay. But this is how
- 7:36:14you can update multiple product at once.
- 7:36:17Okay. Okay. So let's move to the next
- 7:36:19one which will be delete functionality.
- 7:36:21So for delete what I will do right. We
- 7:36:22are going to add one particular data and
- 7:36:24then we're going to delete that data
- 7:36:26only. So we we will not delete any of
- 7:36:28the existing all these data. So first
- 7:36:30let's insert one data here. So I'm just
- 7:36:31going to do insert into products and
- 7:36:35what are the things? So we have name, we
- 7:36:37have category, we have price, let's add
- 7:36:41stock. So skew and also we are going to
- 7:36:44have the description. So here we will do
- 7:36:47values. And now let's do here temp
- 7:36:51product
- 7:36:53to be deleted. Means we going to delete
- 7:36:55the same product.
- 7:36:58Then take uh category. So I'm just going
- 7:37:00to take this one as electronics only.
- 7:37:03Let's do here uh price maybe 345.45.
- 7:37:08Stock let's take 45. SK I'm just going
- 7:37:10to do electronic temp 009 or 013
- 7:37:14whatever you wanted to take. And what
- 7:37:17else? I'm just going to do uh temp
- 7:37:20product disk. So this is going to insert
- 7:37:23that product. And now what I wanted to
- 7:37:25do first I wanted to check if the
- 7:37:27insertion is done or not. So I'm just
- 7:37:28going to select name category and skew.
- 7:37:32So I'm just going to do prop from
- 7:37:34products. And here I'm just going to
- 7:37:36check where skew is equal to this skew
- 7:37:41that we have added this one. So copy
- 7:37:43this and I'm going to paste it here.
- 7:37:45Okay. So this will be my first step. So
- 7:37:47now let's run it. So I'm just going to
- 7:37:51copy this.
- 7:37:54Okay. So we have one data that we have.
- 7:37:56Sorry. Yeah. One data that we are going
- 7:37:58to delete. So now we need to delete
- 7:38:00this. Now deleting is very simple. So
- 7:38:02first what we can do right. So let's
- 7:38:04comment it out because we've already
- 7:38:06inserted that into our DB and then I'm
- 7:38:08just going to do here delete. So delete
- 7:38:11word you have to use from where? From
- 7:38:13products. So this is the table. And then
- 7:38:15simply mention that okay where the skew
- 7:38:17is equal to this particular skew simple
- 7:38:20and this will delete and then you want
- 7:38:22to check it. So just copy this and then
- 7:38:25just paste it here again. So simply we
- 7:38:27are first checking if this present then
- 7:38:28we are deleting and then we are checking
- 7:38:30again if that is present. So in that
- 7:38:32case it should not give any result.
- 7:38:34Okay. [snorts] So if I just go here and
- 7:38:36let's run it. So I'll do clear run it
- 7:38:38again. You can see that first time you
- 7:38:40have deleted one and then you don't have
- 7:38:42anything else. Now there are much
- 7:38:44nothing much to discuss here. These
- 7:38:46things are very very simple but this is
- 7:38:48how you need to delete. So moving to the
- 7:38:50next one which will be the last one in
- 7:38:52this particular section which will be
- 7:38:53you need to understand what is returning
- 7:38:56concept. So I'm just going to do 15 and
- 7:38:58let's do this one returning after insert
- 7:39:02dosql. So here what returning is going
- 7:39:05to do? Returning is going to gives back
- 7:39:07the affected rows after you will do
- 7:39:10anytime insert update or delete. So I'm
- 7:39:12just going to do returning usually
- 7:39:15returns back
- 7:39:18the rows returns back the rows
- 7:39:22immediately
- 7:39:24after
- 7:39:26insert or let's say you're doing update
- 7:39:29or whatever even you are doing uh doing
- 7:39:31delete. So here this is actually
- 7:39:34extremely helpful and you'll see that
- 7:39:35even if I'm created I created any videos
- 7:39:37previously on posgress for our DBs I
- 7:39:41have used this one in multiple places.
- 7:39:42Okay so whenever I'm going to insert
- 7:39:44something immediately I wanted to check
- 7:39:46that okay return me that inserted data
- 7:39:48to see that whether it is done or not as
- 7:39:50simple as that. So to do this you have
- 7:39:52to do insert into first you have to give
- 7:39:54the table name and here again pass the
- 7:39:56name category then what we have we have
- 7:39:59the price we have the stock we have the
- 7:40:02skew and also you have the description
- 7:40:04right so all these things and then let's
- 7:40:05say I wanted to do values and then I'm
- 7:40:07just going to do maybe let's take any
- 7:40:09random webcam camera
- 7:40:13uh I'm just going to do this one as
- 7:40:14electronics
- 7:40:17let's give some price
- 7:40:20uh talk and also webcam camera
- 7:40:26description. Okay. So now what I need to
- 7:40:30do I need to return back. So I wanted to
- 7:40:32see that if the insertion is done or
- 7:40:34not. So for this one we need to do
- 7:40:37returning. We have to use returning. So
- 7:40:40what I need to return. So once the
- 7:40:43product is now added. So what will
- 7:40:44happen? Postgress is going to create a
- 7:40:46UID right? So that will be a unique ID.
- 7:40:49So I will be having the ID. Similarly in
- 7:40:51MongoDB you are going to have something
- 7:40:52called uh underscore id. Remember this
- 7:40:55is a document ID. So similarly we are
- 7:40:57going to have the ID. So I wanted to see
- 7:40:59that actually the product is created. If
- 7:41:01the product is created I will be having
- 7:41:03some ID. I'll be having the name. I
- 7:41:05wanted to return the category. I wanted
- 7:41:07[snorts] to return the price. I wanted
- 7:41:09to return the stock and also let's say I
- 7:41:11wanted to return created
- 7:41:14add. So this is what I wanted to do.
- 7:41:16Okay. So now can save this and let's say
- 7:41:20we are going to run this. So let's clear
- 7:41:22this
- 7:41:24rename copy this and let's run it here.
- 7:41:30What you're getting insert has more than
- 7:41:34name 1 2 3 4 5 6
- 7:41:41oh skew we haven't added my bad man. So
- 7:41:44you have to do electronic
- 7:41:46uh let's do webcam 009.
- 7:41:50So now let's save this clear this and
- 7:41:53now we going to run it again and we'll
- 7:41:54see what we're going to get
- 7:41:57and you can see that we have ID name
- 7:41:58category price stock created. So you're
- 7:42:00getting the data right and you see that
- 7:42:02immediately it's returning right so this
- 7:42:04is your unique ID it's created. So this
- 7:42:06is what returning usually does. So it's
- 7:42:09return back the result or the rows
- 7:42:11immediately after your insert. You can
- 7:42:14also use update right. So let's say
- 7:42:16you've done insert right. You can also
- 7:42:18use update and then you can return the
- 7:42:20data also. For example let's say I
- 7:42:21wanted to update products
- 7:42:24and then I wanted to do set
- 7:42:28I wanted to set the stock is equal to
- 7:42:30stock + 10. So here it will become 56.
- 7:42:35Let's do 11. So 67 and then I'm just
- 7:42:37going to do where skew is equal to this
- 7:42:41skew just now I have created. Okay. So
- 7:42:43where the skew is this and then I wanted
- 7:42:45to use returning. So here also you can
- 7:42:47return ID name and then also let's say
- 7:42:50you want to return the stock. So what
- 7:42:51you're doing you're updating the product
- 7:42:53where the SQ is this and then at the end
- 7:42:55you're using returning. So returning
- 7:42:57clause you're using which is going to
- 7:42:58return. So I'm just going to clear and
- 7:43:00then if you run again you can see that
- 7:43:02now this will become 67. Okay. So the
- 7:43:05stock
- 7:43:07all right so I think this is done same
- 7:43:08also you can do for delete also let's
- 7:43:10say you want to delete this you can use
- 7:43:12delete from products where skew is equal
- 7:43:18to this
- 7:43:22and then you are going to use returning
- 7:43:25so you're going to return the ID name
- 7:43:27and the skew so this is going to delete
- 7:43:29and if you want to see that if the
- 7:43:31delete is successful or not you'll see
- 7:43:32that if I use select now right that
- 7:43:35product will not be present because that
- 7:43:36will be deleted. So I'm just going to do
- 7:43:38form. So this will become products where
- 7:43:41the skew is equal to this.
- 7:43:46Save this and see what will happen.
- 7:43:51You can see so this is deleted. So
- 7:43:52you're getting the ID name and once the
- 7:43:55delete is deletion is successful you you
- 7:43:57don't have that product present. So this
- 7:44:00is how you can use the returning
- 7:44:02concept. Okay. So this is very very very
- 7:44:05helpful. So with this I think we have
- 7:44:07completed the part two and now let's
- 7:44:09move on to the next one which will be
- 7:44:10our last part. So there we are going to
- 7:44:12cover some of the little uh I not say
- 7:44:15advanced concept but intermediate to
- 7:44:17advanced level of concept. We need to
- 7:44:19understand [snorts] the concept of
- 7:44:20foreign keys one to many relationship
- 7:44:23many to many inner join left join
- 7:44:26aggregate functions group by the having
- 7:44:29all these things we're going to cover in
- 7:44:31the next section. All right. So now we
- 7:44:33are almost at the end of section which
- 7:44:35will be part three. Not here we have to
- 7:44:38create outside and this also is very
- 7:44:40very important. So we are going to cover
- 7:44:42relationship uh joins aggregate
- 7:44:45functions group by indexes all these
- 7:44:47things. Okay. And first file will be so
- 7:44:49for this one what we going to do right
- 7:44:51we going to create a reset and seed
- 7:44:55relationships.
- 7:44:57So we are going to first add our data
- 7:44:59then only after that we'll be able to
- 7:45:01start uh understanding all the concepts.
- 7:45:04So first what we going to do right? So
- 7:45:05let's create the extension. I'm not
- 7:45:07going to explain most of the things
- 7:45:08because these things we have already
- 7:45:09covered in the previous part. Extension
- 7:45:13if not exist and I'm just going to use
- 7:45:16pg crypto.
- 7:45:19Okay. Now what we going to do right? See
- 7:45:22these will be relationship sections
- 7:45:24right? So what we need to do we can't
- 7:45:26have one single table. We need to have
- 7:45:27multiple tables so that we can
- 7:45:29understand the relationship between
- 7:45:30tables the properties and everything. So
- 7:45:33first I'm going to drop some of the
- 7:45:34tables in case we have all those tables
- 7:45:36so that we can start fresh. So I'm start
- 7:45:39with drop tables if exist. So we going
- 7:45:42to have one post tags table. Copy this.
- 7:45:46We also going to have one more table
- 7:45:48that will be for comments. So comments
- 7:45:50table we also going to have one for post
- 7:45:53table one will be for only tags and at
- 7:45:57the end we are going to have one for
- 7:45:58users. Okay. So these many tables we are
- 7:46:02going to have now let's create all the
- 7:46:04tables one by one. It's very very
- 7:46:06important to understand all these things
- 7:46:07that what are the properties that we are
- 7:46:09taking. Why? Because our entire section
- 7:46:11this part three will be dependent on
- 7:46:13these tables only. Okay. So let's start
- 7:46:15with the first table. So I'm just going
- 7:46:17to do create table and I'm start with
- 7:46:19maybe let's do users. Okay. So in the
- 7:46:21users first I'm going to have a ID u ID
- 7:46:24and this will be my primary key and here
- 7:46:28I'm just going to take a default
- 7:46:29property and I'm just going to take here
- 7:46:31gen random uu id. Okay. So this will be
- 7:46:35the ID of that user. All right. So now
- 7:46:38after this I'm simply going to take one
- 7:46:39more property. Let's take a name. So
- 7:46:41every user is going to have a name and
- 7:46:43this will be not null. Okay. So these
- 7:46:46two things we are going to have for this
- 7:46:47particular table. Now moving to the next
- 7:46:49one. Next will be our second table which
- 7:46:51will be create table posts. Now this
- 7:46:55will become now very very important.
- 7:46:57Okay. So first every post is going to
- 7:46:59have a ID. So I'm just going to copy
- 7:47:00this and then we're going to paste it
- 7:47:02here. And now this is where we are going
- 7:47:05to start our relationship. So every user
- 7:47:08can create multiple post. So that means
- 7:47:09there has to be some relation so that we
- 7:47:11can understand okay this particular post
- 7:47:13is created by which user. So we are
- 7:47:16going to have a user ID property and
- 7:47:18this user ID will be my user ID of this
- 7:47:21particular table. Right? So that let's
- 7:47:23say we have a post five fifth post is
- 7:47:26created by user number two. So from this
- 7:47:28user ID I will be able to understand
- 7:47:30okay this is created by user number two
- 7:47:32whose ID is this. So here we are going
- 7:47:34to have UID which will be not null. And
- 7:47:37now we are going to understand one very
- 7:47:38important concept that is references. So
- 7:47:40how we are going to tell that okay this
- 7:47:41user id belongs to this particular users
- 7:47:44do id. So this is where we are going to
- 7:47:46have something called references. These
- 7:47:49references now I'm going to tell okay
- 7:47:51this is my users table and this will be
- 7:47:53my ID. I hope this is clear. Next let's
- 7:47:56take a title for every post. We going to
- 7:47:58take a title.
- 7:48:01Sorry if you're getting any noise. I
- 7:48:03think it's some bike noise from outside.
- 7:48:05It's coming. These people are crazy.
- 7:48:07Okay, so title I'm just going to take
- 7:48:09text and this will be not null. And then
- 7:48:12I'm also going to take a status for
- 7:48:14every post you're going to have a status
- 7:48:16because later when we going to
- 7:48:18understand the concept of where group by
- 7:48:20having all these things this actually is
- 7:48:23needed this particular property. So for
- 7:48:25this one you're going to take this one
- 7:48:26as text and I'm just going to do not
- 7:48:28null. Let's take a default property and
- 7:48:30for default I'm going to maybe let's
- 7:48:32give this one as wrapped. And here I'm
- 7:48:34also give a check property. So let's
- 7:48:36give a check property for this. So I'm
- 7:48:38just going to do this one as status. And
- 7:48:41status I'm going to give in. So it can
- 7:48:43be either dropped or it can be either
- 7:48:46published.
- 7:48:48Okay. So this is another property. Next
- 7:48:51what else we can take? So let's take one
- 7:48:54views property. So here I'm just going
- 7:48:56to take a views property which will be
- 7:48:58integer. I'm just going to take not
- 7:49:00null. Let's take a default. I'm going to
- 7:49:02do zero. Let's take a check property and
- 7:49:05here I'm just going to take views. These
- 7:49:07views has to be greater than or equal
- 7:49:09zero. So this is the check that we are
- 7:49:11going to have. So these all the
- 7:49:13properties this post table is going to
- 7:49:15have. Then we going to take our comments
- 7:49:18table. Now let's create again. So do
- 7:49:20create table and I'm going to do
- 7:49:22comments. For this one again we are
- 7:49:24going to have the ID. So copy this
- 7:49:27paste it here. Now what else this is
- 7:49:29going to have? Every comment that you're
- 7:49:31going to have will be associated to some
- 7:49:33post, right? Let's say you're using
- 7:49:35Facebook or Insta for a particular post.
- 7:49:37There can be thousands of comments. But
- 7:49:39you need to identify that okay, this
- 7:49:41comment is belongs to which post,
- 7:49:43correct? So that is the reason we need
- 7:49:44to have one more reference set which
- 7:49:46will be post ID. And here you have to
- 7:49:48take UID and this will be again not
- 7:49:50null. And here you have to take
- 7:49:52references. And then I'm just going to
- 7:49:55denote to my post
- 7:49:58reference to my post table and it will
- 7:50:00reference to this post id. So I'm just
- 7:50:02going to give this one as id. Okay. And
- 7:50:04then also let's take a body which will
- 7:50:06be make actual comment. So this will be
- 7:50:08text and I'm just going to give this one
- 7:50:10as not null. So these three things our
- 7:50:12comments table is going to have. Okay.
- 7:50:15So now uh next what else we have? We
- 7:50:17also need to take our tags table. So I'm
- 7:50:19just going to create one more table.
- 7:50:21Let's give this one as tags. Let's take
- 7:50:24our ID.
- 7:50:26Okay. And here I'm just going to take a
- 7:50:28name. I'll do text not null. And for
- 7:50:32every tag it has to be unique. So I'm
- 7:50:34going to take a unique property. Okay.
- 7:50:36And then at the end we are going to have
- 7:50:38our post tags. Uh now this is actually a
- 7:50:41junction table. And why actually you
- 7:50:43need it? Now let's say one particular
- 7:50:45post can have multiple tags and one tag
- 7:50:48can belongs to many posts. Now I I know
- 7:50:50that this might be little tricky for
- 7:50:51someone uh like if you're a beginner to
- 7:50:53understand but if you give little bit of
- 7:50:55time you'll see that everything is
- 7:50:56actually connecting or connected with
- 7:50:58each other and this is actually called
- 7:51:00many to many relationships. So I'm going
- 7:51:02to repeat once again this post tags. So
- 7:51:04one post let's say you have one post
- 7:51:06that can have multiple tags but one tag
- 7:51:09let's say there is a tag ReactJS. Now
- 7:51:11you have one particular post and that
- 7:51:13particular post has a tag reactjs and
- 7:51:16this particular reactjs tag can belong
- 7:51:18to multiple post right so let's say you
- 7:51:20have 10 post and this reactj tag can be
- 7:51:22present in five post so that means one
- 7:51:25post can have many tags one tag can
- 7:51:28belong to multiple post so it's a many
- 7:51:30to many relations so here what I'm going
- 7:51:32to do I'm just going to create one more
- 7:51:33create table and let's give this one as
- 7:51:36post tags and here what we need to do
- 7:51:39right now Remember here we need to have
- 7:51:42a post id because one post can have
- 7:51:45multiple many texts. We need to
- 7:51:46understand that this is belongs to which
- 7:51:47post? This will be UID
- 7:51:50uh not null and I have to give
- 7:51:52references and this will be post table
- 7:51:55id
- 7:51:56and then I'm also going to have a tag
- 7:51:58id. So I'm just going to do tag ID. This
- 7:52:01is also u ID. So I'll do not null. Let's
- 7:52:04do references. And this will be this
- 7:52:06particular tags table that we have
- 7:52:07created. So this will be tags id. Okay.
- 7:52:10So this will be the relations and also
- 7:52:13let's take we need to take a primary key
- 7:52:15here. So what actually we are doing
- 7:52:16here? We are going to take a composite
- 7:52:18primary key. That means these pair that
- 7:52:20whatever we are taking that must be
- 7:52:22unique. Okay. Why? Now you might ask me
- 7:52:25why? Because if you have one particular
- 7:52:27post the same post cannot be connected
- 7:52:30to same tag twice. You are getting what
- 7:52:32I'm telling. So let's say you have a
- 7:52:33particular post and there is a tag
- 7:52:35ReactJS. You can't add that same ReactJS
- 7:52:38tag two times. Okay. So how you we can
- 7:52:41do that? So I'm just going to take a
- 7:52:42primary key here and let's pass this
- 7:52:44post id, right? And then you have the
- 7:52:47tag ID. So this is going to create a
- 7:52:49composite primary key. So these all the
- 7:52:53uh tables that we need. Okay. So we'll
- 7:52:55start with users ID and name. Post ID,
- 7:52:58user ID, reference, title, status and
- 7:53:00views. Comments ID, post ID because
- 7:53:03every comment is belongs to a post.
- 7:53:05Tags, this is just a unique tag. Uh and
- 7:53:08because this will be unique, right? Now
- 7:53:10this is important. This post tag, every
- 7:53:12post can belong to one tag and one tag
- 7:53:14can belong to multiple post. Okay, with
- 7:53:16a composite primary key. So now what I
- 7:53:19have done right, I actually have created
- 7:53:20all the data that we are going to seed.
- 7:53:23Now what is this? So these are the dummy
- 7:53:24data that I'm going to add and based on
- 7:53:26this data we are going to understand all
- 7:53:28the concept one by one. But let's do
- 7:53:30this one in the next part. Till this
- 7:53:31part this is all the tables. Do this you
- 7:53:34can pause this particular part. I will
- 7:53:36highly suggest and try to go through all
- 7:53:39the properties or all the rows that we
- 7:53:41have created for each and every table.
- 7:53:42It's very very important to understand
- 7:53:44all the concepts that we are going to
- 7:53:46discuss in the next part. All right. So
- 7:53:47let's add all the data one by one and
- 7:53:49then we are going to run this particular
- 7:53:51file because until unless we're going to
- 7:53:52seed our data in our DB and create the
- 7:53:54tables we can't start all the concepts
- 7:53:56like foreign keys one one to many
- 7:53:58relations many to many relations inner
- 7:54:00joint lab join so many things are there
- 7:54:02so first [snorts] let's uh copy this one
- 7:54:04so I'm just going to copy this paste it
- 7:54:06here and see what we are doing simply we
- 7:54:08are inserting the two users we have two
- 7:54:11users on and Rahul into our users table
- 7:54:15then add uh these three I'm going to
- 7:54:17copy and I can clearly understand what
- 7:54:20we are doing here. It's not that much
- 7:54:21difficult to understand. So we are
- 7:54:23inserting into post table user id title
- 7:54:27status and views right. So here ID how
- 7:54:31we going to get so from users where the
- 7:54:33name is this particular name because
- 7:54:34we've already added this in our users
- 7:54:36table. So we have added two post here
- 7:54:39and one is for Rahul user one post.
- 7:54:42Okay. Next let's copy these two. We also
- 7:54:46need to add some comment. So I'm just
- 7:54:48going to paste it here. So what actually
- 7:54:50we doing? We are inserting comment post
- 7:54:52body.
- 7:54:54So id ID how we are going to get? So
- 7:54:55from a post you can see that this
- 7:54:57postgress SQL join explained this is one
- 7:54:59of the post that I have added here. So
- 7:55:01this id is belong because we already
- 7:55:03have the relations in the comments
- 7:55:05table. If you go to the comments table,
- 7:55:07you'll see that there is a relations
- 7:55:08with post ID which is your post ID. So
- 7:55:11that means when we we are setting this
- 7:55:14particular data this id we are going to
- 7:55:17get from post where the title is equal
- 7:55:19to this and this will be for this
- 7:55:21particular title. Now you see back end
- 7:55:23API with posgressql this is created by
- 7:55:26Rahul user and this is created by this
- 7:55:28particular postgress SQL join explain is
- 7:55:30created by on my user. Okay and [snorts]
- 7:55:33then I think let's add uh this tag. So
- 7:55:36I'm also going to add two unique tag. So
- 7:55:38let's add this into our tag. So you have
- 7:55:40SQL and back end. That's it. Next, we
- 7:55:44also need to add our post tag. And then
- 7:55:47at the end, we're going to have our uh
- 7:55:49select message. So see what we doing
- 7:55:52here. So for this one, what we doing,
- 7:55:54right? So post ID, we have the post ID
- 7:55:55and the tag ID, remember? So we taking
- 7:55:58some allis. So from post we are taking
- 7:56:00as a T. Tags we're taking as a T. So you
- 7:56:03take the post ID and the tag ID where
- 7:56:05the post title is postgql joint explain
- 7:56:08and the tag ID is SQL because we have
- 7:56:09already added the SQL here. Okay. And
- 7:56:12the one we have added for I think uh
- 7:56:14backend tag. Okay. So this is going to
- 7:56:16insert in our post tag collection. And
- 7:56:19then we just logging this. Now save this
- 7:56:23and then we need to run this. Okay. So
- 7:56:25first let's clear this
- 7:56:29and I'm going to copy this.
- 7:56:32Let's run it here.
- 7:56:35Okay. So now this is going to create all
- 7:56:38of this. It's going to insert the data.
- 7:56:40And if you want you can just see all of
- 7:56:41this here also. If I now refresh this.
- 7:56:46See how slow this is. Man,
- 7:56:52I think this would solve this one very
- 7:56:55quickly. See, we have the commands, post
- 7:56:57tag, post, product, tags, and users. And
- 7:56:59if you right click here,
- 7:57:04so you can see that you can count rows
- 7:57:05and you can also view the data. If I do
- 7:57:07all rows, right, this will load forever.
- 7:57:10So for now, I'm not going to do that. If
- 7:57:12you're using Windows, right, just do
- 7:57:13right click and do view data. you'll be
- 7:57:14able to see all the data that we have
- 7:57:16created here. Okay? Or else you can also
- 7:57:18directly um do from here. Right? So for
- 7:57:21example, let's say uh I'll [snorts] just
- 7:57:23do here only. Right? Maybe let's do
- 7:57:24select
- 7:57:28select star from and then you're going
- 7:57:31to give all the tables and everything.
- 7:57:33For example, let's say post tags, right?
- 7:57:36Something like this. And then you'll be
- 7:57:37able to do that also. I think we have
- 7:57:39already done this one part one and two.
- 7:57:40I'm not going to do that one. It's just
- 7:57:42going to take some time. So what I mean
- 7:57:43by this whatever data we have added or
- 7:57:46we done the sitting in our DB you'll be
- 7:57:48able to see that uh easily in the DB you
- 7:57:50can just do that right click and do view
- 7:57:53data. So go here any of the table do
- 7:57:55right click you see it's very slow for
- 7:57:57me I don't know why and then you do view
- 7:58:00and then you'll be able to see the rows
- 7:58:01directly from here. Okay. So now now we
- 7:58:05need to start uh all the concepts one by
- 7:58:08one because you already have very good
- 7:58:11idea that how many tables we have what
- 7:58:13is the relations okay if you want you
- 7:58:15can just go through one by one more time
- 7:58:17it's very very important to understand
- 7:58:19right that for example user is has a
- 7:58:22relations with post has a relations with
- 7:58:24comments these tags are unique but when
- 7:58:27you have post tags this has relations
- 7:58:30with both post and the tag so now let's
- 7:58:32create our second file which will be
- 7:58:34foreign keysql.
- 7:58:39Okay.
- 7:58:41Now you know one thing these particular
- 7:58:43sections that we are going to do right
- 7:58:44this is these you need to learn the
- 7:58:46concept more than doing unnecessary
- 7:58:49example. Why? Because you if you can
- 7:58:51understand the relations joins all these
- 7:58:53things you'll be able to do any kind of
- 7:58:55queries you will be able to write. But
- 7:58:56if you don't understand let's you do 10
- 7:58:58example also and you don't know the
- 7:59:01basics it will be difficult for you to
- 7:59:02write in a real project. So what
- 7:59:04actually foreign keys are. So foreign
- 7:59:06key will be you can consider that as a
- 7:59:08column that is going to points to the
- 7:59:11primary key of another table. Okay. So
- 7:59:13I'm just going to write once again. So
- 7:59:15foreign key will be foreign key is a
- 7:59:18column in sort that points
- 7:59:22to the primary key of another
- 7:59:27table.
- 7:59:29Okay. So this will be the foreign key.
- 7:59:31Now just take one very simple example.
- 7:59:33So you have users do ID which is a
- 7:59:36parent key and then you have post dot
- 7:59:41user id. Now this is the reason I
- 7:59:43mentioned that this is important. So go
- 7:59:45to post and see that post has a user ID.
- 7:59:48This is a foreign key. Why? Because what
- 7:59:50this is actually doing this is pointing
- 7:59:53to the parent key here. Parent uh sorry
- 7:59:56this is pointing to the another table.
- 7:59:58So this is pointing to the another
- 8:00:00table. So another table is users and
- 8:00:01this is pointing to the another table
- 8:00:03primary key. What is the primary key?
- 8:00:05ID. So ID is a primary key. So this is a
- 8:00:07proper example of a parent key and a
- 8:00:10primary key. Okay. And what actually
- 8:00:12this means? This means that every post
- 8:00:14that you're going to create that must
- 8:00:16belong to one existing users. So that
- 8:00:19means every post you will create
- 8:00:23that belongs
- 8:00:26to a that not belongs that must belong
- 8:00:30that must belongs or belongs to a to an
- 8:00:34existing users.
- 8:00:38Okay. So this is what it will be. So now
- 8:00:41uh maybe let's do one thing right. So
- 8:00:43let's see some simple example but this
- 8:00:45is all about the concept. So first let's
- 8:00:47see our parent table. So I'm just going
- 8:00:48to do here select let's do ID let's do
- 8:00:51name and I'm just going to do here from
- 8:00:54users table because we have the users
- 8:00:56table. Okay. And then what we going to
- 8:00:58do right then we are going to check our
- 8:01:00child table. So child table we will
- 8:01:01select ID. Now we have the user ID and
- 8:01:04then I'm just also going to take the
- 8:01:06title of that particular post. So this
- 8:01:08will be from my posts table. Okay. So
- 8:01:11save this and let's run it here. Okay.
- 8:01:14So let's remove this.
- 8:01:16And you can see that what you're
- 8:01:18getting. So you're getting two users.
- 8:01:20But if you [snorts] notice one thing,
- 8:01:21you see this part here. This part right?
- 8:01:25See there is a match. So this actually
- 8:01:28is belongs to this particular. So these
- 8:01:30two post that we have is belongs to user
- 8:01:33ona. And this particular post that we
- 8:01:36have is belongs to user Rahul. So there
- 8:01:39is a match. So this is a foreign key
- 8:01:41which points to the primary key of a
- 8:01:45different table or another table. Very
- 8:01:47important. So foreign keys points to the
- 8:01:49primary key of another table. This is
- 8:01:51the concept that you need to understand
- 8:01:52as simple as that. Okay. So I think
- 8:01:55that's all. I think we don't have
- 8:01:57anything much to discuss here. So let's
- 8:01:59move on to the next one which now we are
- 8:02:02going into much more important concept
- 8:02:04and that is now this is also important
- 8:02:06concept because this is you need to
- 8:02:08understand now see if you go here right
- 8:02:10this particular file that we have
- 8:02:11created and if you can understand this
- 8:02:13part whatever we are doing how do we are
- 8:02:15doing the differences and everything
- 8:02:17this will be like a piece of cake
- 8:02:19because it will be a very very simple
- 8:02:20for you to understand but if you don't
- 8:02:22know how your table structure is see
- 8:02:25that is the reason you see my teaching
- 8:02:27process or style right I Always focus
- 8:02:29the thing that is important. If you
- 8:02:31don't know this structure and someone
- 8:02:33will tell you this, it will be difficult
- 8:02:35for you to understand. I can 100%
- 8:02:37guarantee. Okay. So this is the thing
- 8:02:38that you need to understand. Now let's
- 8:02:40move to the next one which will be 03
- 8:02:43and I'll do one to many
- 8:02:46one to many
- 8:02:49uh relationship
- 8:02:51now relationship.
- 8:02:54Okay. So let's close now. I'll close
- 8:02:56this file. I'll open this one. So what
- 8:02:58actually one to many listen that means
- 8:03:00in simple sentence one parent row can
- 8:03:04have many child rows simple so we'll do
- 8:03:07one parent rows can have uh can have
- 8:03:12multi no many child rows many child rows
- 8:03:15okay what will be our example in our
- 8:03:18example one user can write many post so
- 8:03:21one user so user Rahul can have 10 post
- 8:03:25but one post is belong to one user. So
- 8:03:29what I mean by this? Let's see user one.
- 8:03:32So user one is Anunna and user two is
- 8:03:34Rahul. So Rahul can have 10 post. Let's
- 8:03:37say 1a 2a 3a 4 like that 10. But every
- 8:03:42post this single post always belong to
- 8:03:45one user. So this post also belong to
- 8:03:47Rahul. This post also belongs to Rahul.
- 8:03:50You're getting what I'm talking about.
- 8:03:52So that means one user can write many
- 8:03:56posts but one post will always belongs
- 8:04:01to
- 8:04:04no belong to one belongs to one user.
- 8:04:09Okay. So this is what it will be. So
- 8:04:12here if I want to explain in much more
- 8:04:14detail way so or much more simpler way.
- 8:04:18So users again is your parent table and
- 8:04:22then posts will be your child table.
- 8:04:28Okay. So this is about the concept. Now
- 8:04:31here although we haven't started our
- 8:04:33join yet but this is the this is all
- 8:04:35about the concept. Okay. This is one to
- 8:04:36many. Now one more thing I wanted to
- 8:04:39explain here that is let me just write
- 8:04:42here comment only. So you see this
- 8:04:45relationship right? This is a
- 8:04:46relationship correct? This is a one to
- 8:04:48many relationship. So here if I try to
- 8:04:51take post dot user id. So this actually
- 8:04:56relate to our user do ID. Right? This is
- 8:04:59what actually happening here. Correct?
- 8:05:01So simple words that this user do ID is
- 8:05:04your original user ID. This post do user
- 8:05:07ID stores this user ID inside the post
- 8:05:10table. Okay. This is also another very
- 8:05:12very important concept. Yeah. Okay. So
- 8:05:15here what I'm talking about that this
- 8:05:17users do ID please note it down this is
- 8:05:19actually important is the original is
- 8:05:22the original user ID and what this post
- 8:05:25dot I don't know I'll [snorts] make this
- 8:05:29control minus and then it will uh zoom
- 8:05:31out okay so this post dot user ID is
- 8:05:35nothing but it's the same user ID but
- 8:05:38this post dot user ID is going to stores
- 8:05:40that user ID only that original user ID
- 8:05:43only I'm draw that use original user ID
- 8:05:46inside the posts table. So this is what
- 8:05:50is happening here. Okay. So now although
- 8:05:53we haven't started our uh join yet,
- 8:05:55right? Because join is one of the most
- 8:05:57important concept. But I'm going to show
- 8:05:58you one example that uh that let's say
- 8:06:01someone tell you that okay give me all
- 8:06:03the post. So you have to give me all
- 8:06:05post with their authors.
- 8:06:07But you might be thinking okay I can
- 8:06:09give all the post very easily but in my
- 8:06:12post table I only have the user ID. How
- 8:06:16I'm going to get the username right? So
- 8:06:18this is the query can come here because
- 8:06:20I can give the user ID but I need to
- 8:06:22somehow get the username. So this is
- 8:06:24where the relations comes into picture
- 8:06:26because this user ID is nothing but this
- 8:06:28user ID. Correct? This ID. So if you
- 8:06:30know this user ID, you know this ID and
- 8:06:33from this ID you'll be able to get this
- 8:06:35name. Simple. So now what you can do
- 8:06:37right although again I'm repeating we
- 8:06:39haven't done the uh join yet but this is
- 8:06:42also another join concept only. So here
- 8:06:44I'm just going to do users okay dot
- 8:06:47name. So I need the username as I'm just
- 8:06:49going to do remember we have done the
- 8:06:51allis so use using as and this I'm going
- 8:06:54to take as author name and also give
- 8:06:56comma and here I'm going to take the
- 8:06:58posts dot title as post title but now
- 8:07:05also let's take the posts dot status I
- 8:07:08think we also have the status right but
- 8:07:10now how we are going to get this so now
- 8:07:12we need to work with two tables because
- 8:07:15this user name is not present in this
- 8:07:18post table. Post table only has the user
- 8:07:20ID. But to get the username, I need to
- 8:07:23query this user table. So that means I
- 8:07:25need to get from users table and at the
- 8:07:28same time I need to do a inner join.
- 8:07:30Okay. So this we are going to discuss
- 8:07:32later. For now just write it. So we're
- 8:07:33going to do a inner join with which
- 8:07:36table? Post table. Why we can do that?
- 8:07:38Because there is a relation between this
- 8:07:40user ID and this user ID here. So this
- 8:07:44will be inner join. And then what we
- 8:07:46need to do how we going to do that? So
- 8:07:48we need to check on so what we are
- 8:07:51asking that we need to give me all post
- 8:07:53right. So we are doing that. So all post
- 8:07:58with their authors right? This is what
- 8:08:02we're doing. So that means this user
- 8:08:05this user ID will be same as this ID. So
- 8:08:08when I'm going to do on so I need to
- 8:08:10check this user do id has to be equal
- 8:08:14post dot user id. So you're getting I'm
- 8:08:17repeating this thing that what you're
- 8:08:18doing simply you're checking that okay
- 8:08:19this user ID and both are same that
- 8:08:21means this is a match okay and then what
- 8:08:24you're going to do simply you can also
- 8:08:25do a order by so let's do order by so
- 8:08:27I'm just going to do order by and I'll
- 8:08:29do users dot name posts
- 8:08:35dot title
- 8:08:38okay so now what is happening here I'm
- 8:08:41going to repeat once again so we are
- 8:08:43simply telling that we are connecting
- 8:08:46ing user do ID with post do ID so that
- 8:08:49means if ona that user has written two
- 8:08:52post that user row will appear with two
- 8:08:55post you're getting what I'm talking
- 8:08:57about so this proves our concept of one
- 8:09:00to many relation so that mean one user
- 8:09:02row can be connected with multiple post
- 8:09:05rows simple and now why we are using
- 8:09:07inner join now that we going to discuss
- 8:09:08later because inner join normally it
- 8:09:11will do right it will going to return
- 8:09:12only rows where the relationship exit
- 8:09:15exist on the both sides. Okay. All
- 8:09:18right. So now let's run it here. Right.
- 8:09:20So I'm going to paste this. Sorry, not
- 8:09:22this one.
- 8:09:25So I'll just paste it here / this. And
- 8:09:28then if I do enter and you can see that
- 8:09:30what you're getting. So you're getting
- 8:09:31the post title author name. But now you
- 8:09:34are able to get the author name also for
- 8:09:36that corresponding post. And why this is
- 8:09:37happening? because we are doing a inner
- 8:09:40join and there is a match that if the
- 8:09:42user id is equal to post dot user id
- 8:09:44then only I'm going to get but this is
- 8:09:46where even though your post table don't
- 8:09:48have the username still using this
- 8:09:50relation you are able to get this
- 8:09:52particular name okay so this is although
- 8:09:54this we have we have discussed about
- 8:09:56inner join but this is the concept of
- 8:09:58one to many relationship so with that
- 8:10:00now let's move on to the next one all
- 8:10:02right so although we have to cover the
- 8:10:04many to many relations right but I
- 8:10:05thought that let's do the inner join
- 8:10:07first and left join and then we going to
- 8:10:09do that. Okay, it will be helpful. So
- 8:10:11I'll do inner joinsql.
- 8:10:14Let's close this one. Okay. So what
- 8:10:18actually inner join is right. So
- 8:10:20remember one thing is the key here that
- 8:10:23is interview questions. See this
- 8:10:25particular video right? Whatever we are
- 8:10:27doing everything can be interview
- 8:10:28questions because this is a conceptual
- 8:10:30and practical posgress course right. So
- 8:10:33whenever you're going for any interview
- 8:10:35let's say backend interview or whatever
- 8:10:36database interview you're giving there
- 8:10:38is a possibility that you're going to
- 8:10:39get a lot of questions from this
- 8:10:41particular video whatever we are
- 8:10:42learning. So inner join is going to
- 8:10:45returns this is important only the
- 8:10:48matching rows
- 8:10:51from both tables. So from both tables
- 8:10:55okay this is the important part here. So
- 8:10:57in our case I think we've already
- 8:10:58discussed that part right that in our
- 8:11:00post there is a user ID that points to
- 8:11:03our user table ID this ID again I'm
- 8:11:06going to repeat the same thing this user
- 8:11:08ID points to this user ID simple right
- 8:11:12so what postgress is going to do so
- 8:11:14postgress now will be able to match each
- 8:11:17post with the user that user actually
- 8:11:20created the post why because there is
- 8:11:22something some relations between these
- 8:11:24two if there is no match it's is not
- 8:11:27going to return anything simple okay so
- 8:11:29now let's create a simple example and
- 8:11:32you're going to understand everything
- 8:11:33that we are doing here okay and then I'm
- 8:11:35going to explain that what actually this
- 8:11:37example is doing and why I've actually
- 8:11:39decided to take this as example so I'm
- 8:11:41just going to do select
- 8:11:43I'll do users do name as I'm going to
- 8:11:46take this one as author name
- 8:11:49comma you have to give comma here and
- 8:11:51I'll do posttitle as post title
- 8:11:57Then I'm also going to take the posts
- 8:11:59dot status
- 8:12:02and also let's take posts dot views
- 8:12:04because we have the views also. So we
- 8:12:06have the views also. Okay. So these four
- 8:12:09things now from where because you have
- 8:12:10to give the destination always. So from
- 8:12:12my post table and now I need to see I
- 8:12:16need the username right because I
- 8:12:17already mentioned this thing multiple
- 8:12:19times that your post table don't have
- 8:12:21the username. So that means you have to
- 8:12:24get the username from your user table.
- 8:12:26So this is where you have to do inner
- 8:12:27join as simple. So you're going to do
- 8:12:29inner join with the users table and then
- 8:12:31you have to give a matching condition.
- 8:12:33So that is the reason I mentioned that
- 8:12:34it is going to return only the matching
- 8:12:36rows. So there has to be something there
- 8:12:38is a match. So what is the match? Post
- 8:12:40dot user ID is a match with user do ID.
- 8:12:43Simple. So here you're going to give
- 8:12:45that on. So we do on posts
- 8:12:49dot user id is a match with users do id.
- 8:12:53So it's a matching condition. So now
- 8:12:55this is going to do the inner join
- 8:12:56successfully. So postgress will be able
- 8:12:58to understand. And now what you can do
- 8:13:00right I'm just going to do a where. So
- 8:13:01let's do posts
- 8:13:04dot status because we have the post dot
- 8:13:07status right see and then while adding
- 8:13:09the post I've added this one right. So
- 8:13:11go here. So we have this published or
- 8:13:14dropped. So I'm just going to get that
- 8:13:16where the post dot status is equal to
- 8:13:19published
- 8:13:22and let's do order by also. So I'm just
- 8:13:24going to do order by and I'll do post
- 8:13:27dot views and I'm just going to do this
- 8:13:30and maybe let's do descending order.
- 8:13:32Okay. So this will be a perfect example
- 8:13:34of inner join. Now how we are going to
- 8:13:37read this if you're a beginner. So you
- 8:13:39start with the beginning right. So first
- 8:13:40from the post table what we are doing we
- 8:13:43are doing inner joining our users table.
- 8:13:46So simply in a simple way we are
- 8:13:48connecting each post to that particular
- 8:13:51user right and this is the matching rule
- 8:13:53I already mentioned this is the matching
- 8:13:55rule here sorry this is the matching
- 8:13:57rule so this is the matching rule
- 8:14:01without this it won't be possible and
- 8:14:03then we giving some condition that where
- 8:14:05it is published and then we're going to
- 8:14:06do order by and now if you run this see
- 8:14:09what will happen. So we're going to do
- 8:14:10clear
- 8:14:12and see what we're going to get.
- 8:14:16All right. So we're getting zero result.
- 8:14:18Published spelling is wrong. I know
- 8:14:20that. Okay. So this is wrong. Save this
- 8:14:23and run again.
- 8:14:28Okay. So now you're getting author name
- 8:14:30also you're getting post title, you're
- 8:14:32getting status published and also views.
- 8:14:34This is also in descending order. So
- 8:14:35you're doing a lot of things but the
- 8:14:37important part what we are doing here we
- 8:14:38are doing a inner join. So the interview
- 8:14:41questions that you might get that what
- 8:14:42actually inner join is what actually it
- 8:14:44will return and how actually it will
- 8:14:46work. So the most important part you
- 8:14:47have to mention that it's written only
- 8:14:49the matching rows from both tables but
- 8:14:52there has to be some matching condition
- 8:14:53because if there is no matching
- 8:14:55condition how posgress is going to
- 8:14:57understand that okay this particular
- 8:14:58post is written by this user you're
- 8:15:00getting. So this is where it is very
- 8:15:02very important. So now I think this is
- 8:15:05done. Let's move on to the next one
- 8:15:07which will be our left join.
- 8:15:13Okay. So close this. Now what actually
- 8:15:15left join will do. Now left join is
- 8:15:19going to keeps all rows from the left
- 8:15:22table. Now this might be a little tricky
- 8:15:24to understand. Okay. So let's write all
- 8:15:26the things first. So I'm just going to
- 8:15:27do left join keeps all rows from the
- 8:15:33left table.
- 8:15:36Okay. Now say that consider you have a
- 8:15:39left table and you have a right table.
- 8:15:41Now what this first condition is telling
- 8:15:43that this statement that it is going to
- 8:15:45keep all your rows from the left side
- 8:15:46table and if the right side table has
- 8:15:49the matching data then postgress is
- 8:15:52going to include that data. If not what
- 8:15:55possess is going to do? It's going to
- 8:15:56return null for the right side column.
- 8:15:59These things are very very important. So
- 8:16:00I'm just going to do so consider you
- 8:16:02have a two table left table right table.
- 8:16:04If the right table has matching data
- 8:16:10so in that case posgress is going to
- 8:16:13include that. Supposedly it's going to
- 8:16:14include that
- 8:16:17includes that if it's don't have any
- 8:16:19matching data.
- 8:16:26So in that case it's going to return
- 8:16:28null. It returns null.
- 8:16:32All right. So now what example we can
- 8:16:34take from our setup. Now here you can
- 8:16:37see that consider we have our post table
- 8:16:39over here.
- 8:16:43So this is our post table. This is our
- 8:16:46left table. Okay.
- 8:16:49So I'm going to take this one as a left
- 8:16:51table.
- 8:16:53And let's take our comments table and
- 8:16:56this I'm going to take as a right table.
- 8:16:58So this is my right table. So this post
- 8:17:01uh this comments. Okay. And why we are
- 8:17:04taking this? Because
- 8:17:06every post can have comments or can't
- 8:17:09have. So it's a possibility. So that
- 8:17:12mean simply telling that because not
- 8:17:15every post is going to have comments.
- 8:17:16You're getting what I'm talking about.
- 8:17:18So not every post is going to have
- 8:17:21comments. Let's say some post some post
- 8:17:27will have 100 comments
- 8:17:33and some
- 8:17:35will have zero.
- 8:17:38You're getting. So now we'll be able to
- 8:17:40see that how left chain is going to show
- 8:17:42the difference very very clearly. So now
- 8:17:45what we going to do right let's write
- 8:17:47one. So what we we will do we'll write a
- 8:17:49query which is going to show all our
- 8:17:51post including the post that do not have
- 8:17:55any comments or don't have any comments.
- 8:17:57Okay. So see how we are going to do
- 8:17:59that. So I'm going to do select and here
- 8:18:01I'm just going to render the post dot
- 8:18:03title and I'm going to take allis and
- 8:18:05I'm just going to do post title comma
- 8:18:09and also I'm going to take this com uh
- 8:18:11comments is going to have the body right
- 8:18:13so I'm just going to do comments dot
- 8:18:16body comments is spelling is wrong dot
- 8:18:20body as comment_body
- 8:18:24now we are going to do a from so from my
- 8:18:27post table this is my left table and now
- 8:18:30we don't have to do inner join you have
- 8:18:31to do a left join
- 8:18:34left join will be right side table so
- 8:18:36which is your commands table okay and
- 8:18:39then I'm just simply going to give a
- 8:18:40check condition matching condition why
- 8:18:42because there is a match right because
- 8:18:44this post do ID is similar to this post
- 8:18:47ID because every comment is going to
- 8:18:50have that post ID associated right so
- 8:18:52that mean there is a match so here I can
- 8:18:54simply check if this post do ID is equal
- 8:18:57to comment commands dot
- 8:19:00post ID. So here I'm just going to do
- 8:19:02comments dot post ID. Okay. And then I'm
- 8:19:05simply going to do a order by and I'll
- 8:19:08do posts
- 8:19:10dot title. So I'm just going to do order
- 8:19:12by title. So how we are doing this? So
- 8:19:15simply here left join comments. What
- 8:19:18this line is simply doing that we try to
- 8:19:20connect each post with its comments
- 8:19:23simply. And this is the matching rule.
- 8:19:26If there is a post that has no comments,
- 8:19:29what it's going to do? The comment body
- 8:19:31will be what? This comment body null.
- 8:19:34This is what I mentioned, right? If
- 8:19:35there is a no matching, so it's return
- 8:19:36null. You're getting what I'm talking
- 8:19:39about. And left join is going to keep
- 8:19:41all our post. Inner join, what it's
- 8:19:44going to do, right? It's going to hide
- 8:19:45the post that have no comments. There is
- 8:19:47a huge difference, big difference. Left
- 8:19:50join is going to keep all our post, but
- 8:19:52it's going to return the comment body as
- 8:19:54null. Inner journey is going to hide the
- 8:19:56post that don't have any commands. So
- 8:19:58here you can simply see this right? You
- 8:20:00can run it and see what is happening
- 8:20:02here.
- 8:20:04See so now we have back end API with
- 8:20:07posgress. Please add more example
- 8:20:09indexes for beginner. We don't have
- 8:20:10anything posgress explained. This is the
- 8:20:12commenter. Now if you go here where we
- 8:20:14have see added the data go below.
- 8:20:19So you can see that for posgress join
- 8:20:22explain we have very clear explanation
- 8:20:24this is the command please add more
- 8:20:26example uh sorry no backend uh APIs with
- 8:20:30posgress for this one we have please add
- 8:20:31more example but for indexes for
- 8:20:34beginner this is another one for this
- 8:20:36particular post we don't have any
- 8:20:38comment so that means right side there
- 8:20:40is no match so what's is going to return
- 8:20:42it's going to return null so that's why
- 8:20:44you're not getting anything see simple
- 8:20:46right or not simple but it's clear if
- 8:20:48you're a beginner It will be a little
- 8:20:50tricky but see with this explanation I
- 8:20:52hope you are able to understand what
- 8:20:53we're talking about. This is the
- 8:20:54important part. The left journey is
- 8:20:56going to always keep all the data from
- 8:20:58the left table. Right uh table has
- 8:21:01matching data then only it will render
- 8:21:02or else it's going to return null. So
- 8:21:04this is the concept of left join. Okay.
- 8:21:07So we have completed inner join and left
- 8:21:09join. Let's create the next file which
- 8:21:11will be many to many relationship. So do
- 8:21:14many to many
- 8:21:19relationships.
- 8:21:23So let's do this one in the next part.
- 8:21:25All right. So now let's move on to the
- 8:21:26next one which will be now man to many
- 8:21:28relationship. Right. So from our example
- 8:21:30what will be the best uh way to explain
- 8:21:33this one. So I think this part we have
- 8:21:35already discussed and that is that in
- 8:21:36our case right where we have added the
- 8:21:38data that the many to many means one
- 8:21:42post can have multiple tags. You
- 8:21:43remember we have the tags uh this one
- 8:21:46right I already mentioned that let's say
- 8:21:47you have one particular tag this can
- 8:21:49have uh sorry one particular post and
- 8:21:51that can have multiple tags. Let's say
- 8:21:53HTML is a tag, CSS is a tag, ReactJS is
- 8:21:56a tag and one tag can belong to many
- 8:21:59post. So that means this reacts tag can
- 8:22:02be uh associated with multiple post. So
- 8:22:06this will be a proper many to many uh
- 8:22:09relationship example. So that means one
- 8:22:11post
- 8:22:13can have multiple uh tags and one tag
- 8:22:19can be associated
- 8:22:22with multiple posts.
- 8:22:26I hope this is clear right. So now in
- 8:22:29this example in our example what
- 8:22:32actually post is doing so it's store
- 8:22:33post data tags is going to store uh tag
- 8:22:37uh tags data and then we have one more
- 8:22:40post tags. Now this is going to connect
- 8:22:43both post and tags. So if you see so if
- 8:22:46you go here right so go to the post so
- 8:22:48do post do ID this post do ID is similar
- 8:22:53to this post id that we have in this
- 8:22:55post tags table. Similarly, this tax do
- 8:22:59ID is similar to this tags id that we
- 8:23:01have in this post tags dot tags id in
- 8:23:04this particular table. So that means
- 8:23:06here in our case we have a post do ID
- 8:23:08which is similar. So I'm just writing
- 8:23:10triple equals which is equal to this
- 8:23:12post tags
- 8:23:15dot this post id and similarly you have
- 8:23:19the tags do ID right? So we do tags do
- 8:23:21id is again is equal to this post tag
- 8:23:25dot tag id.
- 8:23:28All right. So now let's see one simple
- 8:23:31example. So example will be now your
- 8:23:32task is that you have to show every post
- 8:23:36with its tag. So that means show me
- 8:23:41every post
- 8:23:44with its tag. So how we can do that? Now
- 8:23:48let's see. So we'll do select. Now first
- 8:23:50of all what we need to get so I need to
- 8:23:52get the see when I wanted to show the
- 8:23:54post I don't have the post title here
- 8:23:57right so that means if I want to get the
- 8:23:58post title I need to query this
- 8:24:00particular table so that means I need to
- 8:24:02get this post dot title correct so first
- 8:24:05I need to do what I need to get posts
- 8:24:08dot title as let's do this one as post
- 8:24:13title and then also I need to get the
- 8:24:16tags name so what I mean by tags name
- 8:24:18these tags name because you don't have
- 8:24:20the tax name here. So I need to get the
- 8:24:22tags doame. So I'm just going to do tags
- 8:24:25dotname as let's do this one as tag
- 8:24:27name. Okay. So this will be my tag name
- 8:24:30and this I'm going to get from my post
- 8:24:32table. And here what you have to do. So
- 8:24:35you have to do a inner join because you
- 8:24:37need to check in both the sides. Right?
- 8:24:39So I'm just going to do inner join. And
- 8:24:41also if you go to inner join what I
- 8:24:43mentioned inner join is going to return
- 8:24:45only the matching rows from both tables.
- 8:24:48Okay. There is a difference between
- 8:24:49inner join and the left join. So this
- 8:24:51I'm just going to do inner join but
- 8:24:53inner join you have to do with what you
- 8:24:56have to do with this post tag this
- 8:24:58particular uh table. Okay. So I'm going
- 8:25:01to show you how we are going to do that.
- 8:25:02So first we need to do what? So I need
- 8:25:03to get post tag. So I'm doing what? So
- 8:25:06I'm doing in from this post table with
- 8:25:09this post tag. Why? Because there is a
- 8:25:12relation with this post ID. This post ID
- 8:25:15is equal to this post do ID. So that
- 8:25:17means what will be my matching criteria
- 8:25:18here? So I'm going to give here on. So
- 8:25:21I'll do post do id is equal to post tag
- 8:25:26post tag dot post ID. You're getting
- 8:25:29right. So this will be my matching
- 8:25:30criteria. And this is for the first one.
- 8:25:33Similarly I also need to get the tags
- 8:25:36name. So that means I there should be
- 8:25:38some matching criteria between this
- 8:25:40table and that this table and that is
- 8:25:41what tags do id is equal to post tags
- 8:25:44dot tag ID. So these two are matching.
- 8:25:47So so that if there is a matching so
- 8:25:48I'll be able to get the name. So that
- 8:25:50means again what you need to do again
- 8:25:52you need to do another inner join here.
- 8:25:54Now this time this inner join will be
- 8:25:56not with the post tag. This will be from
- 8:25:59your tags table. So you have this tags
- 8:26:02table here. Correct? And now what you're
- 8:26:04going to do? So you have to do the
- 8:26:05matching criteria. So what will be
- 8:26:07matching criteria? So matching criteria
- 8:26:09will be your post tag. So this will be
- 8:26:12post tag dot. And now you have what? tag
- 8:26:15id and this will be equal to what? So
- 8:26:17this will be equal to your tags do id.
- 8:26:20You're getting what you're doing. So I'm
- 8:26:21explaining line by line. So this will be
- 8:26:23a tags do ID. Okay. And then we are
- 8:26:25going to do what we are going to do
- 8:26:27order by. Let's do order by and I'm just
- 8:26:29going to do here posts
- 8:26:32dot title,
- 8:26:35tags dot name. So this is what we need
- 8:26:38to do. Now I think I don't have to
- 8:26:41explain here now because I've already
- 8:26:42explained line by line. Still I'm just
- 8:26:44going to show you sorry explain one more
- 8:26:45time that what actually this is doing.
- 8:26:47So select simply we are going to get
- 8:26:49this post title and the tag name. Right.
- 8:26:51So here first of all what we are doing
- 8:26:53so from the post table we are doing
- 8:26:55inner join with the post tag table
- 8:26:56because there is a matching. So in the
- 8:26:58matching we need to give the matching
- 8:26:59criteria. So post do ID if there is a
- 8:27:02match with the post tag dot post ID. So
- 8:27:04that means there is a matching. So it's
- 8:27:05going to give me that particular data.
- 8:27:07Then we going to do inergen with the
- 8:27:09tags because there is also a matching
- 8:27:11criteria with the tags do ID with the
- 8:27:13post tags dot tag ID. So this is going
- 8:27:15to give me the tags name. This is what
- 8:27:17we're simply doing here. Now let's run
- 8:27:19this and let's see what is happening
- 8:27:20here. Okay. So I'm going to just clear
- 8:27:22this.
- 8:27:24Let's copy the name [clears throat]
- 8:27:33syntax error text name at okay let me
- 8:27:35see what we doing here.
- 8:27:38Oh say I always do this mistake going to
- 8:27:40give comma here. So now let's run one
- 8:27:43second
- 8:27:45and you can see that what you're getting
- 8:27:46you're getting the post title with the
- 8:27:49tag name. Okay. And now this is very
- 8:27:51very important concept. Now this is not
- 8:27:53a simple posgress concept. Okay, this is
- 8:27:56like I'm going to I'm going to say this
- 8:27:58will be little bit intermediate side. So
- 8:28:00you need to know all of these things
- 8:28:01very very clearly so that it's like you
- 8:28:03don't have to go to any other resources
- 8:28:05to see how these things are working. So
- 8:28:07what I mean by this this many to many
- 8:28:09one to many inner join left join you
- 8:28:11have to master all of this if you want
- 8:28:13to become good like if you want to
- 8:28:16become uh good developer I will say in
- 8:28:18writing all these queries okay so this
- 8:28:20is a very very important concept that we
- 8:28:22have learned here okay so with this I
- 8:28:25think that's all for this particular uh
- 8:28:28uh concept now let's move on to the next
- 8:28:30one next I think what we can do right
- 8:28:32let's understand some of the table allis
- 8:28:35now this is also another important
- 8:28:36concept concept. So what I'm going to do
- 8:28:38right, I'm just going to do 07. Let's do
- 8:28:40this one as table allis now
- 8:28:45spelling is wrong I think.
- 8:28:49Okay. So now let's close this. So what
- 8:28:52actually table allis? Allis I think we
- 8:28:54have already discussed that allis is
- 8:28:56going to make your join query shorter
- 8:28:58and easier to read. Right? So allis is
- 8:29:02going to
- 8:29:04make your queries
- 8:29:08shorter and easier to read. So this is
- 8:29:12the first thing. So simply I'm telling
- 8:29:14that okay if I want the title from the
- 8:29:17post table. So I'm just going to do
- 8:29:19what? Post.title. This is what we are
- 8:29:21doing right. What we can do right? No I
- 8:29:23don't want to write this. Instead I want
- 8:29:25to write I want to write like this. So
- 8:29:29instead of this I want to do P.Title.
- 8:29:31Now this P I need to tell Postgress.
- 8:29:33Okay this P means I'm actually talking
- 8:29:36about the post table or let's say want
- 8:29:38to get the uh what you can do maybe
- 8:29:40users do.ame instead you want to do what
- 8:29:44you want to do you do. Right. So these
- 8:29:48will be your allis.
- 8:29:50So now let's see how we are going to do
- 8:29:52that. Now this will be a very simple
- 8:29:53one. So what we can do right maybe let's
- 8:29:55say
- 8:29:57I wanted to select something so I wanted
- 8:29:59to get my
- 8:30:01let's go here. So I wanted to get the
- 8:30:03post title let's say and also post
- 8:30:05status and maybe let's do username also
- 8:30:08I'm going to get and also let's go to
- 8:30:11the comment and also let's say I want to
- 8:30:12get the comment body. So all these
- 8:30:15things I wanted to get. So first of all
- 8:30:16what I will do right I'm just going to
- 8:30:18ptitle. Now notice one thing we are not
- 8:30:20doing posts do. And here I'm just going
- 8:30:23to take a license and I'm just going to
- 8:30:25do this one as post title. We have to
- 8:30:27give comma here. Let's I'm just going to
- 8:30:29give P dot status and also let's take
- 8:30:31the post views also. So I think we have
- 8:30:34the views right? So I'm just going to do
- 8:30:35P dot views. So this is what I'm talking
- 8:30:37about. So I'll do P dot views.
- 8:30:41Also let's take the username. So I'm
- 8:30:43just going to do U dot name as let's do
- 8:30:46this one as author name. And also I'm
- 8:30:48going to get the comment body. So I'm
- 8:30:50just going to do C dot body as comment
- 8:30:55body. Now this is where you have to do
- 8:30:59the analysis because till this part your
- 8:31:02postgrade doesn't know what actually
- 8:31:04this P.title means. Very very important
- 8:31:07because there is no table that named
- 8:31:10exactly as P. So that means when I'm
- 8:31:12going to do here from ports. So now I'm
- 8:31:15simply telling that okay this is the
- 8:31:16table I'm talking about. I need to give
- 8:31:18a allis here and that allis will be that
- 8:31:20allis that you are using here. So you're
- 8:31:22simply telling that okay I'm actually
- 8:31:24quiring from the post table but I want
- 8:31:26to rename this post from posts to P so
- 8:31:30that I don't have to write the post word
- 8:31:33multiple times as simple as that. And
- 8:31:34now let's say I wanted to do a inner
- 8:31:36join here so I need to do inner join uh
- 8:31:39maybe let's do the user because we also
- 8:31:41need to get the inner join of that uh
- 8:31:43sorry we also need to get the username
- 8:31:45right? So we need to do inner journal on
- 8:31:46the users table but you have to give
- 8:31:48another allis as U because you are not
- 8:31:52using users dotname here you are using u
- 8:31:55dotname okay so this is one more now
- 8:31:57what we need to do I think this part
- 8:31:59already discussed so there will be a
- 8:32:01relations why because your user do ID
- 8:32:04will be similar to your post dot user id
- 8:32:07so that means now what you can do right
- 8:32:09you don't have to do post do user ID
- 8:32:11instead you can take only these allises
- 8:32:13here so you'll do p dot user user id
- 8:32:15you're getting. So you'll do P dot user
- 8:32:18id and this will be equal to your user
- 8:32:20do ID. So user do ID this id. So that
- 8:32:24means user will now become u. So you you
- 8:32:26have to do u do ID right and then what I
- 8:32:30need to do right I also wanted to get
- 8:32:32the comments. So I'm just going to do a
- 8:32:33left join and I'll do comments because
- 8:32:36comments is the commands table that I'm
- 8:32:38actually quering this one. But now this
- 8:32:40time I wanted to do allis of C. So
- 8:32:42comments will be as C and then I'm just
- 8:32:45going to do here on. So there will be
- 8:32:48another match. Why? Because comments do
- 8:32:51id right sorry comments.post id will be
- 8:32:54equal to your post do ID. So that means
- 8:32:56here the matching condition will be your
- 8:32:58p dot id because you are taking the post
- 8:33:00as p which will be equal to c which is
- 8:33:04here c for comments and these comments
- 8:33:06will have the post ID. So this will be
- 8:33:10c.post ID you're getting? So this will
- 8:33:12be a matching condition and let's do a
- 8:33:14order by
- 8:33:16maybe let's do which one we can do order
- 8:33:19by
- 8:33:20let's do p dot views as order by okay so
- 8:33:23I'm just going to do disk so this is one
- 8:33:26more query now remember it's this
- 8:33:28particular concept is not about the
- 8:33:30query because these all the things we've
- 8:33:31already discussed this is about how
- 8:33:33you're writing this allis so here post
- 8:33:35we are talking as a p or writing as a p
- 8:33:38user table we are writing as a u
- 8:33:40commands table we are writing as a See?
- 8:33:42Okay. So now if I just let's say clear
- 8:33:44this and let's run it once again
- 8:33:53and you can see that you're going to get
- 8:33:55the result. So you're getting post,
- 8:33:56title, status, views, author name and
- 8:33:59comment body and this is also in the
- 8:34:01descending order. But again I already
- 8:34:03mentioned this is about how we are going
- 8:34:04to write the allis. Okay. So I think
- 8:34:07that's all for this particular concept.
- 8:34:09Now in the next part what we can do what
- 8:34:11else okay we had to do aggregate also
- 8:34:13right I already mentioned so let's
- 8:34:15create one more file here and I'm going
- 8:34:16to do aggregate
- 8:34:19functions
- 8:34:21dossql
- 8:34:24so now what we are talking about the
- 8:34:26aggregate function so simply in a simple
- 8:34:30definition aggregate functions will be
- 8:34:32it always calculates right so it's
- 8:34:35always going to calculate or calculate
- 8:34:38one [snorts] result from many rows. This
- 8:34:42will be your aggregate function. Now
- 8:34:45there can be so many aggregate
- 8:34:46functions. So I'm just going to give
- 8:34:48some of the examples. So I'll do we have
- 8:34:49the count. What count is going to do?
- 8:34:52Count is going to give you the number of
- 8:34:54rows if I'm not wrong. Then you have
- 8:34:57something called sum. Sum I think by the
- 8:34:59name you can understand. This will be
- 8:35:00total value. Then also you have average.
- 8:35:04So look average and this will be give
- 8:35:07the average value.
- 8:35:12Okay. Then what else? We also have the
- 8:35:14mean. So this mean will be the smallest
- 8:35:17value. And then also we have the max
- 8:35:21which will be maximum value. Okay. Or
- 8:35:24let's do largest or max value. Okay.
- 8:35:31All right. Now where where actually you
- 8:35:34can use all of this very very very
- 8:35:36helpful and that will be I'm going to
- 8:35:38give you some real life example for
- 8:35:40example let's say you're creating some
- 8:35:41admin dashboard where you have to
- 8:35:42calculate your sales let's say you're
- 8:35:44creating some reports you're creating
- 8:35:46some analytics dashboard there also
- 8:35:49mostly it will be around your admin
- 8:35:51panels and those related work and you'll
- 8:35:53see that you're going to use this one
- 8:35:54lot of time now let's say I wanted to
- 8:35:57write a very very simple example that
- 8:35:59what I wanted to do right I wanted to
- 8:36:01count all the rows from my post table.
- 8:36:04Okay. So let's do one by one then I'm
- 8:36:06going to explain. So I'm just going to
- 8:36:07do select. Now let's say you want to
- 8:36:08read all the uh rows from a certain
- 8:36:12table. Okay. So how you're going to do
- 8:36:14that? So for now we are only going to
- 8:36:15use the post table only. To to read all
- 8:36:18the rows from a certain table you have
- 8:36:20to use a count here. And this count
- 8:36:22you're simply going to tell okay this
- 8:36:23here I'm just going to count everything
- 8:36:25and this I'm just going to also give
- 8:36:27allis. So this will be my total
- 8:36:29underscore
- 8:36:32posts. Okay. And here what you're going
- 8:36:35to do. So I'm just going to give a from
- 8:36:38post table. So I'm simply telling we'll
- 8:36:40do one by one. Okay. So this is the
- 8:36:42first one. So it's simply telling that
- 8:36:43give me or count all the rows from this
- 8:36:46post table because this is the table
- 8:36:47that we are using. Now let's run this.
- 8:36:59Okay. So, you're getting three.
- 8:37:00Fantastic. Now, now let's say what I
- 8:37:03need to do, right? I [snorts] wanted to
- 8:37:05now count what I wanted to give a
- 8:37:06filter. So, what filter I wanted to
- 8:37:08give? I wanted to give a filter where
- 8:37:10the status is published. If I'm not
- 8:37:12wrong, we have a published status. See,
- 8:37:14published or dropped. So, I only wanted
- 8:37:17to count all the rows where the status
- 8:37:20is published. So that means now you
- 8:37:22can't directly use count you have to use
- 8:37:24a filter. So how you can do that? So you
- 8:37:25have to do count
- 8:37:28and then you have to use a filter here.
- 8:37:30Okay. And this filter will be now I
- 8:37:33think we have already used this one
- 8:37:34multiple times where keyword status will
- 8:37:38be equal to what whatever status you
- 8:37:40wanted to check. So now I wanted to
- 8:37:41check about the published and then let's
- 8:37:43give a license and I'm just going to do
- 8:37:45this one as published
- 8:37:47posts. So what this is going to give
- 8:37:50this is going to count all the rows but
- 8:37:52it's going to apply a filter now. Okay.
- 8:37:54Now let's run it and let's see what
- 8:37:56you're getting. So if I go here but
- 8:37:59telling syntax in the count
- 8:38:02again I forgot to give comma. Okay. So
- 8:38:06let's do
- 8:38:09so you can see that we have three post
- 8:38:11publish post two. Change this one to
- 8:38:13dropped and you'll see that you are
- 8:38:14going to get one because we have only
- 8:38:16one uh dropped post right. So if I just
- 8:38:19go here, run it again, you see that we
- 8:38:22are getting only one. Fantastic.
- 8:38:24Now let's undo here.
- 8:38:26Next, what we want to do, we'll give
- 8:38:29comma here. So now let's say sum. I want
- 8:38:32to do sum or or what I will do, right?
- 8:38:35Maybe let's do this one. Go to post.
- 8:38:38We have these views, right? So I wanted
- 8:38:40to do a sum of all these views of all
- 8:38:43rows. You're getting what I'm talking
- 8:38:45about. So when you're going to do that
- 8:38:48this very very simple so views is I'm
- 8:38:50talking about these views I wanted to
- 8:38:51sum so you have to use sum and then
- 8:38:53you're going to tell that okay calculate
- 8:38:55all the sum of what so you have to give
- 8:38:57here which column so this will be my
- 8:38:59views column simple and then you're
- 8:39:01going to give as and let's giveis so
- 8:39:03this will be my total views okay so that
- 8:39:06mean now this is going to do what this
- 8:39:08is going to add all your view counts
- 8:39:11together from this table okay from this
- 8:39:14particular table so now if I just run it
- 8:39:16and you'll see that you're going to get
- 8:39:18320 and you can also uh like reconfirm.
- 8:39:21So you have what
- 8:39:23140 140 + 180 so 320.
- 8:39:28What else? Let's do the average views
- 8:39:31also. So for average it will be simple
- 8:39:32just use average pass the views and
- 8:39:35you're going to do here average
- 8:39:38views also to give comma here. Save this
- 8:39:42and now run again.
- 8:39:45So this will be your average views. So I
- 8:39:48think this is done. What else? I think
- 8:39:50min minimum and maximum let's do that
- 8:39:51also. So minimum again it will be
- 8:39:53simple. So do minimum views as lowest
- 8:39:57views and also you're going to do a
- 8:40:00maximum. So do max of views as highest
- 8:40:07views.
- 8:40:09So this is going to give the minimum and
- 8:40:11the maximum. So here, let's clear this
- 8:40:13and run it here again. So we can see
- 8:40:16that you're going to get the lowest
- 8:40:17views here. So let me just do one thing.
- 8:40:20I'm just going to remove some of these
- 8:40:22here. Let's comment it out
- 8:40:26this this.
- 8:40:32Okay. So you have the lowest views which
- 8:40:34is 40 and highest view which is 180. So
- 8:40:37you can go here and can confirm 40 and
- 8:40:40180. Okay. So with this I think we have
- 8:40:42covered the many to many table analysis
- 8:40:44aggregate. Now let's move to the next
- 8:40:46one. So let's understand group by having
- 8:40:48count disting and some of the basics of
- 8:40:50indexes. Okay. Okay. So moving to the
- 8:40:52next one which will be group by. Okay.
- 8:40:55So let's create a group by dossql file.
- 8:40:58So what actually group by is right. So
- 8:41:00in simple way to explain group by will
- 8:41:03be or group by creates
- 8:41:06groups
- 8:41:08of rows. Okay. In the previous section
- 8:41:11we discussed aggregate function right
- 8:41:13count sum average mean max all these.
- 8:41:16Now let's say what we are going to do
- 8:41:18right these uh functions calculates
- 8:41:21values inside each group right. So here
- 8:41:24what we are going to understand in this
- 8:41:26particular section in the previous
- 8:41:27multiple times we have used something
- 8:41:29called where. So where actually do what
- 8:41:33where actually filters very very
- 8:41:35important normal rows before grouping.
- 8:41:40Okay. So this will be where keyword I'm
- 8:41:43just talking about I'm just telling
- 8:41:45keyword only so that you can understand
- 8:41:46where actually filters normal rows
- 8:41:49before grouping what having will do
- 8:41:53having filters right we have having
- 8:41:55filters. So having filters groups
- 8:41:59after grouping now this might be a
- 8:42:02little tricky until unless we are going
- 8:42:03to understand the concept. Okay. Now
- 8:42:06when you're going to use having so
- 8:42:08you're going to use having when the
- 8:42:10condition is going to depend or
- 8:42:12condition depends on a aggregate
- 8:42:13function.
- 8:42:15So now the best option will be I've
- 8:42:17already created one proper example for
- 8:42:20this that you have to find the authors.
- 8:42:23Okay. So to find the authors who have
- 8:42:27written
- 8:42:29at least two posts. Okay. So you have to
- 8:42:33find the authors who have written at
- 8:42:35least two post. Okay. Now here you also
- 8:42:38have to do what? You have to use some
- 8:42:40kind of aggregate functions. Correct?
- 8:42:42Because you have to get the count right
- 8:42:44count sorry count of the post. So let's
- 8:42:47how we can do that. So I'm going to do
- 8:42:50select. Now first of all when I'm
- 8:42:52talking about find authors so that means
- 8:42:54what I need to get what the author name
- 8:42:57so what actually author name so author
- 8:42:58name here is either this is Anona or
- 8:43:00Rahul so author name is nothing but your
- 8:43:03username right so this is where so what
- 8:43:05I'm going to do I'm just going to do eu
- 8:43:07dot name as author name
- 8:43:12comma and now what I need to do I
- 8:43:14already mentioned that we need to have
- 8:43:16the count why because we need to see who
- 8:43:18written at least two post so that means
- 8:43:20I need to use aggregate function here
- 8:43:23and how I can see that whether this
- 8:43:25particular author already created two
- 8:43:27post or not and that is from this post
- 8:43:31id. So we have this post id right. So
- 8:43:33I'm just going to do here p do id as
- 8:43:37total
- 8:43:38posts and now what I need to do right
- 8:43:41I'll also give you the sum and let's do
- 8:43:44the p dot views as total views.
- 8:43:49Okay. So now first of all we need to get
- 8:43:51the table. So first table will be my
- 8:43:53user table because I need to get the
- 8:43:55username. So I'm just going to do user.
- 8:43:57But very very important whenever you're
- 8:43:59going to use the table allis you have to
- 8:44:01use allis here or else I already
- 8:44:03mentioned that posgress will not be able
- 8:44:05to understand that this U means the user
- 8:44:08table. Okay. And then what we need to do
- 8:44:10right we need to do a left join. So I'll
- 8:44:13do left join with the post table as P
- 8:44:16because we're using the P here. Okay. So
- 8:44:18this will be small P where so here what
- 8:44:22will be the matching condition multiple
- 8:44:23times I will discuss because your U do
- 8:44:26ID user do ID similar to your post dot
- 8:44:29user id so that means here I'm just
- 8:44:31going to do u do id will be equal to p
- 8:44:34dot user id correct so this will be my
- 8:44:37matching condition and now here what I'm
- 8:44:39going to do right I'm just going to do a
- 8:44:40group by
- 8:44:42u do ID and let's do a uh what else we
- 8:44:45going to do maybe let's do u dot name
- 8:44:47Okay. User do ID user dot name. Now what
- 8:44:50I mentioned here, so let's go here. What
- 8:44:53I mentioned having filters groups after
- 8:44:55grouping filter normal rows before
- 8:44:57grouping. So now we need to use a having
- 8:45:00because we already have done the already
- 8:45:02used the grouping. So here what you have
- 8:45:05to do you have to use having. So this
- 8:45:06having is simply going to tell that okay
- 8:45:08now I need to see if the count of that
- 8:45:11post. So count of p dot id is greater
- 8:45:14than or equal to two or not because that
- 8:45:16is my criteria here that that user at
- 8:45:18least have written two post. So that
- 8:45:20means now I'm going to see having count
- 8:45:23dot count of p do ID should be greater
- 8:45:25than or equal to two simple and then I'm
- 8:45:28also going to do order by simply and
- 8:45:30let's do order by of total post.
- 8:45:34Let's do this one as descending order.
- 8:45:36So now let's see what actually this is
- 8:45:38doing. So first of all we have got from
- 8:45:39user as you start with the user table
- 8:45:43left join post sp means connect each
- 8:45:46user with their post. What actually this
- 8:45:49group by is doing now? So this group by
- 8:45:52I already mentioned this group by is
- 8:45:53going to create one group for each user.
- 8:45:57So group by creates group of rows. So
- 8:45:59this group by is going to do what is
- 8:46:01going to create a group of each user.
- 8:46:04Why? Now you might ask me okay why it
- 8:46:06will create? So whatever post let's say
- 8:46:09we have two users Anona and Rahul. So
- 8:46:12all post written by Anunna become one
- 8:46:14group and all post written by Rahul
- 8:46:17become another group. Okay. So this is
- 8:46:20very very important. So this is this
- 8:46:22line is actually doing that. So it's
- 8:46:24going to create one group for each user.
- 8:46:26Count is going to do what? So it's
- 8:46:28simply going to check the count that how
- 8:46:31many post exist inside each user group.
- 8:46:35Right? This what I'm talking about. Then
- 8:46:36we have the sum. Sum is going to give
- 8:46:38you the sum. And this having what
- 8:46:40actually having is doing? Now this is a
- 8:46:42criteria. This is simply telling that
- 8:46:44keep only those user group where the
- 8:46:48post count is greater than or equal to
- 8:46:50two. You're getting so first we are
- 8:46:53using group by then we are using having
- 8:46:55very very important that is the reason I
- 8:46:57mentioned that having filter groups
- 8:46:59after grouping. So first we created the
- 8:47:01group and then we simply telling and
- 8:47:03sorry first we have created the group
- 8:47:05then we counted all the post then we
- 8:47:08created the sum also then we created one
- 8:47:10group for ana one group for Rahul and
- 8:47:13now I'm doing what now I'm doing the
- 8:47:15filtering on that count that you are
- 8:47:17going to only keep those user group
- 8:47:20where the count is more than two you're
- 8:47:23getting so if the count is less so it's
- 8:47:25not going to give you that result and
- 8:47:26then you're simply doing a order by so
- 8:47:28now let's run this and see what you're
- 8:47:30getting. Okay,
- 8:47:33that's it. So now you're getting Anunna
- 8:47:36total post is two and total views is
- 8:47:40140. Why? Because Anunna created two
- 8:47:42post as much as I remember I added. Let
- 8:47:46me just check. Okay, see this one right?
- 8:47:51So this is how you need to do. Let's say
- 8:47:52you want to do that. Okay, I wanted to
- 8:47:54check whe the count is exactly equal to
- 8:47:56one. So now if you run this, what will
- 8:47:58happen?
- 8:48:00So you're going to get Rahul because
- 8:48:01Rahul created only one post. So this is
- 8:48:04how you have to use group by and having.
- 8:48:07Okay. So [snorts] I think that's all for
- 8:48:10this particular one. Let's do one more
- 8:48:12uh concept here which is also important
- 8:48:14and that is count distinct. This will
- 8:48:16not take much time but let's do this and
- 8:48:19I'll do SQL. So what this count distinct
- 8:48:22is going to do right. So this is going
- 8:48:24to count unique values right? So it's
- 8:48:26going to count
- 8:48:28unique values. So what I mean by this?
- 8:48:32Okay. Now first of all also another
- 8:48:35thing I need to mention and that is when
- 8:48:37this is useful. Now let's say you're
- 8:48:39using some joints, right? And this is
- 8:48:41useful when joins create repeated rows.
- 8:48:44Now example will be okay let me think.
- 8:48:46So okay so let's say you have one post
- 8:48:49right? I already mentioned that one post
- 8:48:50is going to have multiple tags right?
- 8:48:52You have one post. That post has ReactJS
- 8:48:54tag, NexJS tag, SQL tag, DB tag, HTML
- 8:48:57tag. Now when you're going to join this
- 8:49:01post with post tags and the tags table
- 8:49:04that we have, the same post can appear
- 8:49:07multiple times, right? It's possible. So
- 8:49:09this is where what we going to do,
- 8:49:11right? We are going to use something
- 8:49:12called count distinct and the property
- 8:49:14which is going to count only the unique
- 8:49:16post. So the best example will be let's
- 8:49:19say I wanted to count how many
- 8:49:22unique post sorry how many unique posts
- 8:49:26are connected
- 8:49:28to each tag very good example that how
- 8:49:32many unique post are connected to each
- 8:49:34tag this is what I wanted to uh see now
- 8:49:38let's we are going to do that if you
- 8:49:40notice that whenever we are going to
- 8:49:41write this particular uh query right
- 8:49:43you'll notice that how many previous
- 8:49:45concept we are applying here. So what I
- 8:49:47mean by this? So previously we have
- 8:49:48understood the concept of select, the
- 8:49:50concept of allis, the concept of left
- 8:49:53joint, concept of inner joint, concept
- 8:49:55of group by order by. So basically what
- 8:49:57I'm telling that the mode we are
- 8:49:59progressing in this particular video,
- 8:50:00the mode we are applying the previous
- 8:50:01concept. So here for example what I need
- 8:50:04to get I need to get the tag. So first
- 8:50:06I'm just going to get the tag name. So
- 8:50:08I'm just going to do this one as tag
- 8:50:10name. Next give comma. And now what I'm
- 8:50:13going to do right I need to count how
- 8:50:14many unique post how many unique post
- 8:50:16I'm going to give count and here you
- 8:50:18have to give distinct
- 8:50:21okay distinct p dot id okay so post do
- 8:50:24ID as total
- 8:50:28unique
- 8:50:31posts and now what I'm going to do first
- 8:50:34of all what I need to get I need to get
- 8:50:35the tags table so tag the allies as I've
- 8:50:38given t so this will be as t and then we
- 8:50:40are going to do a left join
- 8:50:42So I'm going to do a left join with my
- 8:50:44post tags table because there is a
- 8:50:47relations with the post tag table also.
- 8:50:49So here I'm just going to take this one
- 8:50:51as let's do post tag. So PT and then I'm
- 8:50:54simply oh sorry PT.
- 8:50:57Then I'm simply going to check the
- 8:50:58matching condition that the tag do ID
- 8:51:01should be equal to PT which is the post
- 8:51:04tag dot tag ID because there is a
- 8:51:06matching here right? So your post tag
- 8:51:08has the tag id which is similar to this
- 8:51:10tags do id correct. So I'm simply going
- 8:51:13to do this t dot id which is tag do id
- 8:51:16is equal to post tag dot tag id. Okay so
- 8:51:19this is the first one. Then [snorts] I'm
- 8:51:21going to give another left join and this
- 8:51:24left join will be on my post as p. This
- 8:51:27is what I have given here p. And here
- 8:51:29also you do the same logic. So now this
- 8:51:32time this will become t dot id will be
- 8:51:35your what? Sorry, let's do PT dot PT dot
- 8:51:40post ID. I think PT do.post ID this one.
- 8:51:43So PT dot post ID will be equal to P do
- 8:51:45ID and now I'm going to do a group by.
- 8:51:49So do group by T dot id tag ID and P dot
- 8:51:53name. And maybe let's do a order by
- 8:51:56also. So I'll do order by total unique
- 8:51:59post in descending order.
- 8:52:03the main part that we are simply doing
- 8:52:05for this particular section which is
- 8:52:07this one. So we're simply getting the
- 8:52:09unique post that are connected to each
- 8:52:11tag. Now you might be thinking okay how
- 8:52:13I'm writing all of this. Believe me I've
- 8:52:15written all of this so many times
- 8:52:17previously. So for me it will be easy
- 8:52:19for you. It can can be difficult if
- 8:52:21you're a beginner if you're a student if
- 8:52:23you're very less experienced. You need
- 8:52:25to do lot of practice. These things will
- 8:52:27not come very easily. You can be very
- 8:52:29honest that how where you have to apply
- 8:52:30lab join where you're going to apply
- 8:52:32group by where you going to apply having
- 8:52:35okay these thing where you're going to
- 8:52:37use where so you need to do a lot of
- 8:52:39practice but this is exactly what this
- 8:52:41query is doing see whenever you're going
- 8:52:43to work with relationships right you
- 8:52:45have to see the table that what actually
- 8:52:47your table structure is and what are the
- 8:52:49properties in two tables that are
- 8:52:52connected with each other for example
- 8:52:54tag ID is similar to your tag ID here
- 8:52:57post ID for this table is similar to
- 8:52:59your post do ID. So this is what I'm
- 8:53:01talking about. So now let's run this. If
- 8:53:04I run here, right? So let's see what
- 8:53:05you're getting
- 8:53:12and you're going to get the unique post
- 8:53:14that how many tag. So this SQL is
- 8:53:16associated with two unique post back end
- 8:53:19is associated with one post. And if you
- 8:53:22want we can also verify. So let's see
- 8:53:24what we are getting here.
- 8:53:30we have SQL two times and back end one
- 8:53:34time. All right. So that means this is
- 8:53:36working fine. So with this I think we
- 8:53:38have covered the group by and also count
- 8:53:41distinct. So now let's move on to the
- 8:53:43next one. All right. So moving to the
- 8:53:44next one. Let's understand another very
- 8:53:46very interesting concept and that is
- 8:53:47called sub queries. Okay.
- 8:53:52SQL. So let's see what actually this is
- 8:53:54right. So I'll do sub queries
- 8:53:57in post posgress.
- 8:54:03Let me refresh this
- 8:54:08in postgress
- 8:54:14queries [snorts]
- 8:54:15nested inside another SQL statement.
- 8:54:18Okay. All right. So I just wanted to
- 8:54:19show you because I'm going to explain
- 8:54:21this one in a very simple way. So what
- 8:54:23[snorts] I mean by this? So this is
- 8:54:25simply we are running one query inside
- 8:54:27another query. So we are running one
- 8:54:30query inside another query as simple as
- 8:54:33that. Okay. So what postgress is going
- 8:54:36to do? Postgress is going to run the
- 8:54:38inner query first. So I'll do runs
- 8:54:42the inner query
- 8:54:45first and then it's going to run the
- 8:54:47outer query. Okay. So it's going to run
- 8:54:49the outer query. All right. So now let's
- 8:54:53understand this one in a uh simple
- 8:54:55example. So your task is what we need to
- 8:54:57do right maybe let's do one thing that
- 8:55:00first we are going to have two queries.
- 8:55:02Okay in the inner query we are going to
- 8:55:05calculate some average values and that
- 8:55:08value your outer query is going to use.
- 8:55:11So that means when your inner query is
- 8:55:13going to return sorry calculate
- 8:55:15something right it it's I'm just giving
- 8:55:17as part of this example because we are
- 8:55:19using this file as our data source. But
- 8:55:22for your project it can be anything. So
- 8:55:24for this one I'm just doing right let's
- 8:55:25say I'm going to calculate my average
- 8:55:27views and that average views I wanted to
- 8:55:29return. So that is going to return some
- 8:55:31value and that value I'm going to use in
- 8:55:33my outer query. So that will be two
- 8:55:35query. Okay. So let's how we are going
- 8:55:37to do that. So now uh okay what will be
- 8:55:41the question? The question will be maybe
- 8:55:42let's do this one in this way that which
- 8:55:44post are perform performing better than
- 8:55:50average
- 8:55:52something like this. So here how I can
- 8:55:55do that right. So I'm just going to do
- 8:55:56select so give me the title of that post
- 8:55:59comma give me the status of that post
- 8:56:01and also give me the views but this one
- 8:56:04you wanted to give me from my post
- 8:56:06table.
- 8:56:08Now here what I'm going to do where
- 8:56:11the views now we have our inner query.
- 8:56:15So where the views is what? So views is
- 8:56:18greater than the average value. So that
- 8:56:21means here now I'm going to write my
- 8:56:22inner query. So now I'm just going to do
- 8:56:24a select. So how you can calculate
- 8:56:27average views. What thing you have to
- 8:56:29use here? Means what are the things you
- 8:56:30have learned so far? So to calculate
- 8:56:32average views you have to use aggregate
- 8:56:34function. Right? So what will be my
- 8:56:35aggregate function? I have to do is
- 8:56:36average. So average will be on what on
- 8:56:39views and then you have to give what? So
- 8:56:41from where you have to do this from your
- 8:56:43post table. So now this is your inner
- 8:56:46query. So this is going to give you the
- 8:56:48average views. This is going to return
- 8:56:50the data and then you're going to use or
- 8:56:53run this outer query. So that means
- 8:56:55you're simply telling select title
- 8:56:57status views from post where views is
- 8:57:00greater than 100. Sorry views is greater
- 8:57:02than 20. So why I'm talking about this?
- 8:57:04So consider this particular query result
- 8:57:07is this particular query's result is 20
- 8:57:11right so this
- 8:57:13block is going to return 20 so it will
- 8:57:16become this so that means I'm simply
- 8:57:17telling that where the views is greater
- 8:57:19than 20 you're getting so this is about
- 8:57:21the subqueries and then you can also do
- 8:57:24order by so do order by views
- 8:57:30and then I'm going to do descending
- 8:57:32order so if you save this and run this
- 8:57:35it's going to work. So let's run it
- 8:57:37again.
- 8:57:39Okay. So this is what you're getting
- 8:57:42right. But the point is you have to
- 8:57:43understand this one that how you're
- 8:57:44running queries. And you can also see
- 8:57:46some example. Give me one example.
- 8:57:49See what it's telling here.
- 8:57:54Select name query where the price is
- 8:57:56select average price from products. This
- 8:57:58is almost the same example but this is
- 8:58:00also doing the same thing. This is a
- 8:58:01subquery, right? This is one subquery.
- 8:58:04The point you have to understand that
- 8:58:05it's going to run the subquery first.
- 8:58:07It's going to return the result and then
- 8:58:10the outer query is going to use that
- 8:58:12result. Okay. So I think that's all for
- 8:58:15this particular concept. I think we
- 8:58:17don't have much here. Now also I thought
- 8:58:20that I'm not going to explain or cover
- 8:58:22the indexes but let's give you some idea
- 8:58:24so that if this I already mentioned if
- 8:58:26the video this video will do good I'm
- 8:58:28going to create anyway one more uh part
- 8:58:32two for this particular video but that
- 8:58:35will happen only if this one will do
- 8:58:36good. Okay. So I'm going to cover some
- 8:58:38of the very advanced level of database
- 8:58:40concept that you have to pay to watch
- 8:58:42those courses. Okay. So I'm going to do
- 8:58:44this one for free. So all right. So
- 8:58:46first of all let's understand what are
- 8:58:47indexes. Okay simply indexes help you or
- 8:58:51indexes helps not me indexes helps
- 8:58:53postgress find rows faster. So I'm just
- 8:58:56going to do index helps postgress
- 8:59:01find rows faster. This will be the best
- 8:59:05example. What will happen without an
- 8:59:07index? Now let's say you have 1,000
- 8:59:10data. You have to find something. So
- 8:59:12without if there is no index posgress is
- 8:59:14going to scan many rows to find your
- 8:59:17matching data with an index what it
- 8:59:20usually do right it's going to jump
- 8:59:22closer to the data it needs so how what
- 8:59:25will be the uh how to frame mental model
- 8:59:30consider there is a book now you have a
- 8:59:32book index right so book index means
- 8:59:34number what that will help you so that
- 8:59:37is going to help you find a particular
- 8:59:38topic without reading every page. This
- 8:59:42is the best example, correct? Similarly,
- 8:59:45if you have a database index, it's going
- 8:59:47to find rows without checking every
- 8:59:49data. So, I'm going to repeat once again
- 8:59:52your book index, right? Book will always
- 8:59:54have the index, right? In the first
- 8:59:55page, let's say you have some indexes,
- 8:59:57you can go to a certain page. Similarly,
- 9:00:00in posgress or any DB, if you have
- 9:00:02indexes or database index is going to
- 9:00:04help find rows without checking every
- 9:00:08row. So, that means what? It's going to
- 9:00:10do what? It's going to speed up your
- 9:00:12query reading process. For example,
- 9:00:15let's say your query reading process is
- 9:00:16select, right? So, it's going to speed
- 9:00:19it up this process. This particular
- 9:00:22process, right? So, I do this particular
- 9:00:25process.
- 9:00:27So, now uh what I will do right maybe
- 9:00:30let's do one thing. So, first I will
- 9:00:33just see my data. So, I'm just going to
- 9:00:36select let's take the ID. I'll do a
- 9:00:38title. Let's do a status. I'm going to
- 9:00:41do a views and also we have the user ID.
- 9:00:45Correct. And I'm just going to do this
- 9:00:46one from posts. So this is my post table
- 9:00:51data. Correct? So now I have one
- 9:00:54requirement that I need to filter the
- 9:00:55post by status. So something like this,
- 9:00:58right? So you have / posts and then
- 9:01:00you're going to do status equal to
- 9:01:04published.
- 9:01:05Correct? So this is one filter. So here
- 9:01:08how you can do that. So what we'll do
- 9:01:10select give me ID give me my title give
- 9:01:14me my status and here I'm just going to
- 9:01:18do from posts table and then I'm simply
- 9:01:20going to do where the status is equal to
- 9:01:23published correct. So status will be
- 9:01:26published.
- 9:01:29So this will be my query. Simple.
- 9:01:32So now the problem will occur when this
- 9:01:35post table will become very large. So it
- 9:01:38always needs to do what posgress needs
- 9:01:40to find each and every rows to find this
- 9:01:42particular uh criteria here that okay
- 9:01:46give me these many post maybe there are
- 9:01:48500 post where it is the status is
- 9:01:50published. Now this is where what we can
- 9:01:53do right we can create a index. So let's
- 9:01:56how we are going to do that. So first
- 9:01:57I'm going to do create. So to create
- 9:01:59index you have to do index again. First
- 9:02:01you have to check that if not exist.
- 9:02:05Okay. And let's give this one as idx
- 9:02:10posts. And now see because we need to
- 9:02:12write index for this status column. So
- 9:02:14I'm just giving this one as underscore
- 9:02:16status. Okay. And what you have to do
- 9:02:19you have to give on post and you have to
- 9:02:22tell that okay which column you want to
- 9:02:24do. So we want to do on the status
- 9:02:26column. So now this is actually very
- 9:02:28important. Okay. And you'll see that any
- 9:02:30if you are working in a company or let's
- 9:02:31say any open source you'll see that they
- 9:02:33try to use this particular structure and
- 9:02:35this is the naming style. Now what this
- 9:02:38idx means this is index this is the
- 9:02:40table name and this is the column name
- 9:02:42very very important. Okay. So this will
- 9:02:44be very important. So here
- 9:02:49idx is simply telling this is the id or
- 9:02:53maybe this is the index.
- 9:02:55posts simply telling this is the table
- 9:02:57name and status
- 9:03:01will be your what column name
- 9:03:04okay so now what we are going to do
- 9:03:07right so let's say we want to now filter
- 9:03:12and sort together so now I wanted to
- 9:03:15write a query that is going to maybe
- 9:03:17let's do something like this it's going
- 9:03:18to find the publish post also at the
- 9:03:21same time it's going to show you the
- 9:03:22most viewed post first. Okay. So
- 9:03:26something like this. Now here I want to
- 9:03:28do very very important. Okay. See what
- 9:03:29you're doing. So I'm just going to do
- 9:03:30here title. Give me title. Give me my
- 9:03:33status and give me the views from which
- 9:03:36table. So from posts
- 9:03:39table. I'm just going to do where. So we
- 9:03:42already using status is equal to list
- 9:03:48and I'm just going to do order by and
- 9:03:50I'll do on views and descending order.
- 9:03:54Now here this query although it's may
- 9:03:57look very simple it's easy to understand
- 9:03:59but it has two important parts okay and
- 9:04:01this part we haven't discussed before
- 9:04:03this where status equal to publish right
- 9:04:06this is going to do what it's going to
- 9:04:07filter by status and order by views
- 9:04:10descending it's going to do what it's
- 9:04:12going to sort by views from highest to
- 9:04:13lowest
- 9:04:15okay now here what we can do right now
- 9:04:18let's say I again I'm just talking about
- 9:04:20various way of writing indexes only for
- 9:04:22now so this is one way now here this is
- 9:04:25a composite thing we are doing so we are
- 9:04:27doing two things you getting what I'm
- 9:04:29talking about so that means we are using
- 9:04:32what we are doing two things together so
- 9:04:35that means this is a concept of a
- 9:04:37composite index so we are doing we are
- 9:04:39first filtering and also we doing a
- 9:04:41order by so this will be a composite
- 9:04:45composite index so how you can create
- 9:04:48the index so you can create the index
- 9:04:50like this so you're going to do create
- 9:04:51index
- 9:04:53if not exist now I'm just going to do
- 9:04:56see idx for index post will be my table
- 9:05:00status is one of the column that I'm
- 9:05:02doing and then also I have a views
- 9:05:04column and then what you have to do you
- 9:05:06have to do on posts because that is the
- 9:05:08table name and now you have to give what
- 9:05:10you have to give status and you have to
- 9:05:13give views in descending order so this
- 9:05:15is one of your index you're getting what
- 9:05:18I'm talking about so this is how you can
- 9:05:20write a composite index index. Okay. So
- 9:05:23now maybe let's do one thing. I'll give
- 9:05:26you some little intermediate level of
- 9:05:28concept or how we can write indexes on
- 9:05:30foreign key columns. Now we already have
- 9:05:33discussed the concept of foreign key.
- 9:05:34Right? For example, let's say if I go
- 9:05:37here, your post dot user id is
- 9:05:39referencing the user do ID. This one,
- 9:05:41right? So means every post is belong to
- 9:05:44a valid user. Correct? Now this is the
- 9:05:47concept about this is a concept of
- 9:05:49foreign keys. So something like this.
- 9:05:50Let's say you have one get request
- 9:05:54users and then I'm just going to do id
- 9:05:58and then give me posts.
- 9:06:00So that means your back end needs to
- 9:06:02find all post written by one particular
- 9:06:05user. You're getting all these things
- 9:06:06what I'm talking about very very
- 9:06:07important thing. So that mean this is
- 9:06:09this is a very valid scenario right
- 9:06:11let's say you are creating one
- 9:06:12particular user profile where you need
- 9:06:14to render all the post that is written
- 9:06:16by that particular user. So that means
- 9:06:18how you can do that. So you have to do
- 9:06:20like this right? So we're going to do
- 9:06:21select and give me title, give me
- 9:06:25status, give me views.
- 9:06:28Now what I have to do first from posts
- 9:06:31because this is the table and then what
- 9:06:34I'm going to do here I'm just going to
- 9:06:35do where user ID is equal to. So I'm
- 9:06:38going to write inner query here. User ID
- 9:06:40is going to what? Select ID from where?
- 9:06:44from users table and then I'm simply
- 9:06:48going to do okay give me where name is
- 9:06:51equal to anona now this anona name is
- 9:06:53can be dynamic value because you're not
- 9:06:55going to write a hardcoded value but
- 9:06:57this name will be this id that you are
- 9:07:00getting correct so this will be let's
- 9:07:02say this can be also rahul I'm just
- 9:07:03giving one example
- 9:07:06now here how you can write a index for
- 9:07:09this here if you notice a post do user
- 9:07:12id actually using to find post for a
- 9:07:15particular user. So that is the reason
- 9:07:17this will be good column that to write a
- 9:07:20index. So that means if I want to do
- 9:07:22write index for this so I'm just going
- 9:07:24to do create index
- 9:07:27if not
- 9:07:29if not exist
- 9:07:32and here I'm do again same format I'll
- 9:07:34do id sorry idx post is the table name
- 9:07:37and I'm just going to do user ID because
- 9:07:40this is what we're doing and this will
- 9:07:41be on post table and what will be your
- 9:07:44property your property will be on user
- 9:07:46id because this is a foreign key
- 9:07:47relations right so this is how you can
- 9:07:50yet. So I think we have covered most of
- 9:07:52the things and this now here we don't
- 9:07:54have anything that much to run but this
- 9:07:56is how you can write indexes. The point
- 9:07:58is you can't write indexes blindly. You
- 9:08:01have to see the requirement that where
- 9:08:03you are going to write but the format
- 9:08:04has to be very important and it is also
- 9:08:06very very easier to understand also that
- 9:08:08why you writing all this. Okay. So with
- 9:08:11this I think we have almost covered all
- 9:08:13the things. Let me just think that what
- 9:08:14else we can cover that we going to cover
- 9:08:16in the next part and with that we'll end
- 9:08:18this particular video. So let's move on
- 9:08:20to the next one. All right. So let's end
- 9:08:21this particular video with transactions
- 9:08:24although I'm thought that I'm not going
- 9:08:25to cover but let's uh let me give you
- 9:08:27some of the very little glimpse what
- 9:08:29actually it is because it's very
- 9:08:31important concept and [snorts] then if
- 9:08:33I'm creating any next but uh anytime
- 9:08:35soon then I'll be also covering this one
- 9:08:37later. So this will be transactions.
- 9:08:41Okay. So what actually transaction does
- 9:08:44is it lets you uh multiple or it give
- 9:08:49you the opportunity to run multiple.
- 9:08:54So do multiple
- 9:08:57SQL statements
- 9:09:00run as one safe
- 9:09:05unit. Okay. So simply we are telling
- 9:09:10that whatever we are running either all
- 9:09:12thing or everything is going to succeed
- 9:09:15or everything will be cancelled.
- 9:09:18So now
- 9:09:19okay this where this will be very very
- 9:09:22helpful. Now this is very very helpful.
- 9:09:24Let's say we're creating an e-commerce
- 9:09:26website where user is placing an order
- 9:09:29at the same time when you're placing an
- 9:09:30order right. So what are the things
- 9:09:32you're going to do? So you place a order
- 9:09:34at the same time if the order is
- 9:09:36successful you need to reduce the stock
- 9:09:38count of that particular product right.
- 9:09:40So reduce the stock of that product. So
- 9:09:44this is also comes under transaction. At
- 9:09:46the same time you need to do creating
- 9:09:48payment records,
- 9:09:52transferring money,
- 9:09:55right? Also you need to do what? You
- 9:09:58need to also do creating user
- 9:10:02records
- 9:10:03with related profile data. All right. So
- 9:10:09that means what what actually we are
- 9:10:11doing here if I if I ask you to sum up
- 9:10:14in one single sentence. So this is
- 9:10:16simply we are using a transaction where
- 9:10:17multiple database changes must succeed
- 9:10:20together. So we have to do all these
- 9:10:22things. Okay. So now let's see how we
- 9:10:25are going to do this uh in our example.
- 9:10:28So now first of all what we need to do
- 9:10:30right how we are going to start a
- 9:10:32transaction. So that is the first thing
- 9:10:33you have to understand. So first
- 9:10:35whenever you're going to start a
- 9:10:36transaction you have to do begin. So
- 9:10:39begin is going to start a transaction.
- 9:10:41Now what will happen right once you
- 9:10:44write begin postgress is not going to
- 9:10:46save the changes permanently until
- 9:10:49unless we are going to do something
- 9:10:50called commit. So that we are going to
- 9:10:52discuss later. Now let's say we start a
- 9:10:54transaction. Now in the first step what
- 9:10:56we wanted to do right we wanted to
- 9:10:58update our draft post. So we have a
- 9:11:00draft post right you search here we have
- 9:11:02a draft post correct. So which one is
- 9:11:07that? This is the draft one. Correct. So
- 9:11:08this is a draft post. So here we have
- 9:11:11what indexes for uh beginner. This is
- 9:11:15the title. Maybe let's do one thing.
- 9:11:17Let's change the status from draft to
- 9:11:19published. So this will be my first
- 9:11:21change. So I'm just going to do update.
- 9:11:23So update my post table. Set the status
- 9:11:28to published
- 9:11:33where title is equal to what?
- 9:11:37This one title is indexes for beginner
- 9:11:41correct and then I'm also going to give
- 9:11:43a and because see there can be two title
- 9:11:46also with the same so you always have to
- 9:11:48give status is draft so these small
- 9:11:51small things you have to understand so
- 9:11:52this is the first thing that we are
- 9:11:53doing what you're doing right now in the
- 9:11:56step two this is not the transaction is
- 9:11:58not yet completed I wanted to increase
- 9:12:00the views for the same post okay so
- 9:12:03let's say how many views I have 40 so
- 9:12:05now I wanted to increase the views of
- 9:12:07this post. So again, I'm just going to
- 9:12:08do update posts
- 9:12:11set views equal to views + 50 where
- 9:12:18title is equal to indexes for beginners.
- 9:12:24Okay, so this is the third one and then
- 9:12:27what I'll do right maybe let's do I want
- 9:12:30to check the result that if this is done
- 9:12:31or not. So I'm just going to do give me
- 9:12:33the title, give me the status, give me
- 9:12:36the views
- 9:12:38from posts table where title is equal to
- 9:12:43indexes for beginners.
- 9:12:46Okay. And then at the end what you have
- 9:12:49to do as I already mentioned very very
- 9:12:50important you have to do commit. So what
- 9:12:53commit is going to do? Commit is going
- 9:12:54to permanently save all these changes
- 9:12:56inside the transaction because this is
- 9:12:58whatever we are doing this is a
- 9:12:59transaction. Okay. Now there is a
- 9:13:02concept called roll back also, right?
- 9:13:04Roll back is going to cancel all the
- 9:13:05changes. But for now, if you're
- 9:13:07understanding this thing, it is more
- 9:13:08than enough. Okay. So now let's run this
- 9:13:11first and see what is happening here.
- 9:13:14Run again. I'm doing some mistakement.
- 9:13:17Update post.
- 9:13:22I'm reading update post. Something we
- 9:13:24doing wrong. Let me just check.
- 9:13:27There might be some places I haven't
- 9:13:29given. Okay, this is where I have to
- 9:13:31give this.
- 9:13:35Okay, so let's run again and you can see
- 9:13:37that we do begin update one update one.
- 9:13:39This is the result. This is now publish
- 9:13:41the PUS are also changed and this is
- 9:13:42commit. Okay, why you're seeing for 140?
- 9:13:46Because the previous time also we have
- 9:13:47already updated one time. That's the
- 9:13:48reason. But you can see that from begin
- 9:13:50to commit this transaction is now
- 9:13:52completed. So overall you have to
- 9:13:54understand the concept that what you're
- 9:13:55doing when we are doing multiple DB
- 9:13:56queries at the same time transactions
- 9:13:59are extremely helpful. So I think that's
- 9:14:02all for this particular video. I think
- 9:14:03we have covered almost most of the
- 9:14:05things this is more than enough. Believe
- 9:14:06me or not if you can understand all
- 9:14:08these things and do right you will be in
- 9:14:10a very good position. So if you like
- 9:14:12this video give a like and I will see in
- 9:14:14my next video. Till then good luck and
- 9:14:16peace. All right everyone. So now this
- 9:14:18is the time that we are going to build
- 9:14:20our actual project. So this will be a
- 9:14:22complete practical section and in this
- 9:14:24project we are going to apply all the
- 9:14:26concepts that we have learned so far
- 9:14:28right and I'm pretty much sure that you
- 9:14:29have already covered the previous uh
- 9:14:31posgress uh section I already mentioned
- 9:14:34multiple times that that is actually
- 9:14:35mandatory and prerequisite and it's
- 9:14:37going to give you a proper idea that how
- 9:14:39you can uh write SQL queries right how
- 9:14:42what are the concept of postgress and
- 9:14:44all these things so what are the things
- 9:14:46we're going to understand in this
- 9:14:47particular project and why this project
- 9:14:48is very very important even though I'm
- 9:14:50Pretty much some of you already know
- 9:14:53CRUD, JWT O, Google Earth, all those
- 9:14:55things. But still this project or
- 9:14:57whatever you are going to build will be
- 9:14:59a complete game changer for all of you.
- 9:15:02Why? Because what are the things that we
- 9:15:04are going to learn? First of all, you
- 9:15:05have to understand that this project is
- 9:15:07going to give you complete idea on how
- 9:15:09you can create a proper NodeJS project.
- 9:15:12How you can work with the Node Express
- 9:15:15Postgress with Docker. What is or what
- 9:15:19usually a proper NodeJS project folder
- 9:15:22structure looks like? What will be the
- 9:15:24proper and best practices? How you can
- 9:15:27do proper error handling? Uh form
- 9:15:30validation. Each and everything we are
- 9:15:32going to cover why I've taken this
- 9:15:34particular project little basic to
- 9:15:35intermediate level because this is going
- 9:15:37to help you even though you know lot of
- 9:15:39things in node still I can guarantee
- 9:15:40this is going to give a complete
- 9:15:42different idea that how you can create a
- 9:15:44proper full node project. So first what
- 9:15:47we will do right so you can see that I
- 9:15:49have a package dojson now this is I
- 9:15:50think we have done multiple times so
- 9:15:52that is the reason I've already created
- 9:15:53a package dojson with three dev
- 9:15:55dependencies we have the node types tsx
- 9:15:57and typescript that's it and in the
- 9:16:00tsconig I've created a tsconfig dot uh
- 9:16:03json file now here you'll see that one
- 9:16:06thing that we are using source folder so
- 9:16:09source will be where we are going to
- 9:16:10write all our source code and also we
- 9:16:13are going to have our main env file
- 9:16:15where we going to add all our env
- 9:16:17variables. Okay. But first let's
- 9:16:19understand that what actually we are
- 9:16:21going to build in this particular
- 9:16:22project. Okay. So now consider like the
- 9:16:25so what we'll do right. So let's say
- 9:16:27this is our main capstone project or
- 9:16:30whatever project you are building. You
- 9:16:31are working in a freelance project or
- 9:16:33maybe let's say you are working in a
- 9:16:35company. At the end you have to
- 9:16:36understand one thing is uh very
- 9:16:38important that everything whatever
- 9:16:40you're going to build is nothing but a
- 9:16:42CRUD. Now this is I usually mention in
- 9:16:44all my videos. What I mean by this CRUD
- 9:16:46is nothing but when you're going to
- 9:16:47create something, when you're going to
- 9:16:49read something, when you're going to
- 9:16:50update something and when you're going
- 9:16:52to delete something. So how this
- 9:16:54particular uh project looks like? So
- 9:16:57here we are going to have main two uh
- 9:16:59roles. Okay, one will be normal user and
- 9:17:01one will be our super admin or admin.
- 9:17:04And in this project we are going to have
- 9:17:06only one admin. Why? When you work with
- 9:17:08one admin and you'll create a project,
- 9:17:10right? You will see that no matter how
- 9:17:12complicated the project is, even though
- 9:17:15let's say some project you're building,
- 9:17:17you're going to have a separate role.
- 9:17:19Let's say you're going to have a user,
- 9:17:20you're going to have a admin, you're
- 9:17:21going to have a support role, still it
- 9:17:24will be much easier for you to do all of
- 9:17:26these. So you have to make your basics
- 9:17:27very strong. So that is the reason again
- 9:17:30in my previous NodeJS uh project also
- 9:17:32although that was little basic like we
- 9:17:33have used MongoDB and all and also that
- 9:17:36project we haven't used uh TypeScript
- 9:17:38and postgress but still in that video
- 9:17:40also that I uploaded maybe two years
- 9:17:42back I also taken the same uh uh role
- 9:17:45user and admin I'll see that that video
- 9:17:47also got got pretty good response why
- 9:17:50because we'll try to make things simpler
- 9:17:52and then we'll try to implement this one
- 9:17:54without using any AI tools. Okay. So
- 9:17:57that is another good practice. If you
- 9:17:59try to complete this particular project
- 9:18:00without making use of any AI tools, it
- 9:18:03will be much better. So you're going to
- 9:18:04have a normal user and admin. User is
- 9:18:07going to do what is user is going to
- 9:18:09create support task. Okay. So I'm just
- 9:18:11going to take as short as st support
- 9:18:13task. So user will be able to create
- 9:18:16support task. User will be able to see
- 9:18:19all the support task that is created by
- 9:18:22this user only. Means let's see if I
- 9:18:24have a user A and user B and user A
- 9:18:27created two two support task. User B
- 9:18:29created five support task. User A will
- 9:18:33be able to see only uh task created by
- 9:18:36him. User B will be able to see the
- 9:18:38support task created by user B. But what
- 9:18:41about the admin? So what admin will be
- 9:18:43able to see? Admin will be able to see
- 9:18:45both user A and user B task. So admin
- 9:18:48will be able to see seven task. So admin
- 9:18:50has the capacity to view all the task.
- 9:18:53So that will be one feature. Then so
- 9:18:55user will be able to create task. User
- 9:18:57will be able to see task. User will be
- 9:18:59able to update their task. What I mean
- 9:19:01by this user A only will be update his
- 9:19:05task. User B only will be able to update
- 9:19:07his task. Same for delete also. Okay. So
- 9:19:11this what will happen when you create
- 9:19:13this particular part. This is going to
- 9:19:15give a proper clear understanding of how
- 9:19:17this creation means card usually works.
- 9:19:20Okay. But this is not that much simple.
- 9:19:22Why? Because before this we will have a
- 9:19:24authentication layer. So something like
- 9:19:25this. Let's say you have your user, you
- 9:19:28have your admin and here you are doing
- 9:19:30some activity. But immediately you are
- 9:19:31going to have authentications.
- 9:19:33Authentication means until unless this
- 9:19:35user is logged in they will not be able
- 9:19:38to create anything. Right? So how we are
- 9:19:39going to do the authentication? So here
- 9:19:41what I decided right this is I'm going
- 9:19:44to make little complex. So here we are
- 9:19:45going to have both email and password
- 9:19:47login. Okay. And at the same time we are
- 9:19:51also going to have a Google login. Okay.
- 9:19:53So both together. So if you want user
- 9:19:55will be able to uh login using or
- 9:19:58register using email and password at the
- 9:20:00same time if they want they will be able
- 9:20:01to login using Google. But if this
- 9:20:04particular user is already logged in,
- 9:20:06okay, using that particular email and
- 9:20:08they try to login with the same Google
- 9:20:10email. So in that case they will not be
- 9:20:12able to login. Okay. So this is what we
- 9:20:15are going to do. So this will be our
- 9:20:16authentication. So this is going to give
- 9:20:18you a proper understanding of how JWT
- 9:20:20works. How we are going to implement
- 9:20:22middleware, right? Middleware means
- 9:20:24let's say user A try to see user B task,
- 9:20:28it will not happen, right? So in that
- 9:20:29case we have to have some protection. So
- 9:20:31we're going to have some kind of
- 9:20:32middleware. So that that medial where is
- 9:20:34going to check whether that particular
- 9:20:36user has that permission or not. If that
- 9:20:38user don't have the permission so it
- 9:20:40will not give that user to do that
- 9:20:42particular task. So for example let's
- 9:20:44say admin admin will be able to see all
- 9:20:46his task. So in that case we have to see
- 9:20:49whether that particular user is actually
- 9:20:51admin user or not. If that user is admin
- 9:20:54user so in that case only we are going
- 9:20:55to give that user permission or else we
- 9:20:57will simply say that okay you are not
- 9:20:58admin you will not be able to do this
- 9:21:00particular task. Okay. So these things
- 9:21:03we are going to build. Third, what
- 9:21:04you're going to do do right for admin.
- 9:21:06Admin will be able to do a file upload
- 9:21:08feature. So we are going to have a file
- 9:21:10upload feature. Okay. This file upload
- 9:21:12feature what we going to do? Admin will
- 9:21:14be able to upload some banners. Okay. So
- 9:21:16we are going to have some banners
- 9:21:17feature. We are going to use malter and
- 9:21:20we are going to use cloudinary both
- 9:21:22along with our posgress DB. Okay. So
- 9:21:24admin will be able to upload assets.
- 9:21:27Okay. that assets we are going to store
- 9:21:30in our cloudinary as well as that asset
- 9:21:33URL means the image URL we are going to
- 9:21:35store in our posgress DB okay so this is
- 9:21:38what we are going to do and then at the
- 9:21:40end we are going to have our background
- 9:21:42job feature but remember before we going
- 9:21:44to do background job functionality we
- 9:21:46need to also have a radius uh section so
- 9:21:48that video also have I have in my
- 9:21:51channel I'm pretty much sure I think
- 9:21:52most of you haven't watched that video
- 9:21:54because that video didn't get proper
- 9:21:55response what I'm going to do again
- 9:21:57these This will be mandatory section. So
- 9:21:59this video also I'm going to add and
- 9:22:01then we are going to do the background
- 9:22:03job feature. So this is this will be a
- 9:22:04big one. Okay. Because there are a lot
- 9:22:07of things you are going to understand in
- 9:22:08this particular project. It's going to
- 9:22:09give you overall idea how you can create
- 9:22:12a proper NodeJS project. So with this
- 9:22:14now let's get started. The first thing
- 9:22:15what we're going to do right. So first
- 9:22:17we need to install some of the packages.
- 9:22:19Okay. So first what we are going to
- 9:22:21install. So first let's do one thing. Uh
- 9:22:24if I go to package.json JSON right you
- 9:22:26will see that we have typescript tsx and
- 9:22:29types/ node correct and now what are we
- 9:22:33going to do right in the source we are
- 9:22:35going to have a entry file now as I
- 9:22:37already mentioned what actually entry
- 9:22:39file is this entry file or the server.ts
- 9:22:42TS simp simply will be our first entry
- 9:22:45file where it will simply check that
- 9:22:47okay whatever code we have written each
- 9:22:49and everything should be combined and we
- 9:22:52are going to add all those logic in the
- 9:22:53server.ts file. So what I mean by this
- 9:22:55let's say in this particular project we
- 9:22:57[snorts] are going to have 10 API
- 9:22:59endpoints. So now when we are going to
- 9:23:01build this particular project right so
- 9:23:04this when this code will run right so
- 9:23:06code needs to understand right what is
- 9:23:08our first file that I need to see. So in
- 9:23:10this server.ts ts file. This would be an
- 9:23:12entry file. So whenever code we are
- 9:23:14going to run, it will check this file.
- 9:23:16Okay, then it will say now all these 10
- 9:23:18routes we have added here. So then it it
- 9:23:20can go to that each and every file and
- 9:23:22it it can see that whatever logic we
- 9:23:24have written and based on that it's
- 9:23:26going to do that. So what I simply mean
- 9:23:28by this that this is a entry file which
- 9:23:30is mandatory. This is where we are going
- 9:23:32to run our main code or this is where we
- 9:23:34are going to run our express server.
- 9:23:36Okay. But before that let's not go into
- 9:23:39too much uh uh what is that too much
- 9:23:43concept but let's see what actually
- 9:23:44express is do what is express js okay
- 9:23:48because anyways we are going to
- 9:23:49understand all of this practically so
- 9:23:50express is nothing but it's a framework
- 9:23:53whatever you have learned so far you see
- 9:23:54that in the beginning section you have
- 9:23:56understood all the code uh nodejs
- 9:23:58concept and also the very important http
- 9:24:00section right so what express is going
- 9:24:03to give you right so this is a simple in
- 9:24:05simple way this is going to give you
- 9:24:06some of the features
- 9:24:08If you don't use express those things
- 9:24:10you have to manually write and you have
- 9:24:12to implement again without express also
- 9:24:14you'll be able to do this but this is
- 9:24:16actually going to give you some of your
- 9:24:18see if you uh notice right it's a
- 9:24:20lightweight wrapper on top of NodeJS
- 9:24:22build networking features as simple as
- 9:24:24that means it is using NodeJS in the
- 9:24:26background it's using NodeJS features
- 9:24:28only but this is going to help you or
- 9:24:30it's going to give you it's going to
- 9:24:32give you some of the out ofthe-box
- 9:24:34features that is going to simplify the
- 9:24:36code that's needed to build web server,
- 9:24:38web application and restful APIs. As
- 9:24:40simple as that. So in in definition wise
- 9:24:44express will be a minimal opensource
- 9:24:46framework that built on top of NodeJS
- 9:24:49networking or NodeJS built-in features
- 9:24:51and that is going to give you some out
- 9:24:53ofthe-box features that you need to
- 9:24:56build web servers web application and
- 9:24:58restful API means it's going to give you
- 9:25:00some extra advantage that if you don't
- 9:25:02use it you have to write manually all
- 9:25:04those things but again if you don't use
- 9:25:06this one still you will be able to
- 9:25:08implement those things also. So this is
- 9:25:10what express is. So first what we are
- 9:25:12going to do right. So first we'll do npm
- 9:25:14install express. Okay. So I'm going to
- 9:25:17install express first. So that will be
- 9:25:18our first. So I'll do npm install
- 9:25:20express. So let's install this. If you
- 9:25:23go to package dojson, you can see that
- 9:25:25we have added the express. And also
- 9:25:26let's install the uh types also. So I'll
- 9:25:29do npm install d. And here I'm simply
- 9:25:32going to do add the types
- 9:25:35express.
- 9:25:38Okay. So types also we need because we
- 9:25:41are using typescript. So this is the
- 9:25:42first thing. Second what actually env is
- 9:25:47I think in the core module we have
- 9:25:49already discussed about the env
- 9:25:50variables right. So env is again it's a
- 9:25:52package right? I think we have already
- 9:25:54discussed this one. So it's a package
- 9:25:55that will help us to read our env
- 9:25:57variables because when we are going to
- 9:25:59have all these variables that we need
- 9:26:02for this particular project we can't
- 9:26:04directly hardcode all those things in
- 9:26:06our code. So that is the reason we need
- 9:26:07to keep all of this in a separate env
- 9:26:09file. Second, when we are going to use
- 9:26:11some sensitive information, let's say
- 9:26:12you're using some password or you are
- 9:26:14using some Google uh credentials secrets
- 9:26:18so that you just can't expose in your
- 9:26:20code. Consider this particular project
- 9:26:21is a public source project means you are
- 9:26:24going to post this one to your GitHub
- 9:26:26anyone can see. Now let's say you added
- 9:26:28your Google secret directly in your
- 9:26:29code. Anyone can copy and can misuse,
- 9:26:31right? So that is the reason we need a
- 9:26:34separate file where we are going to add
- 9:26:35all of these. So this is where we need
- 9:26:38file. Okay.
- 9:26:40So now here we are going to do npm
- 9:26:42install env.
- 9:26:46That's it. And then also we need
- 9:26:49something called course. Okay. So this
- 9:26:50we are going to discuss later. For now I
- 9:26:52think this is fine. So first we have
- 9:26:54installed uh express types node types
- 9:26:59tsx that is that is going to help us to
- 9:27:02run our typescript project and then we
- 9:27:04have typescript env and express these
- 9:27:08two we have installed as a dependency
- 9:27:10and these all are dev dependency we have
- 9:27:12already know the difference between
- 9:27:13dependencies and the dev dependencies
- 9:27:15okay so now first of all what we're
- 9:27:17going to do right so first we need to go
- 9:27:19to our source so let's create our basic
- 9:27:22folder structure now these This folder
- 9:27:23structure whatever we are going to
- 9:27:24create will be extremely important. Why
- 9:27:26this is extremely important? Because you
- 9:27:28need to know that how why we are
- 9:27:30creating each and every folder and what
- 9:27:31is the reason or what is the
- 9:27:33responsibility behind creating each and
- 9:27:35every folder. So first we are going to
- 9:27:36create a config folder. So this config
- 9:27:39is going to hold all our config related
- 9:27:40information. Then I'm going to create a
- 9:27:42leave folder. So leave folder is going
- 9:27:45to handle all our utils related. So
- 9:27:48let's we are creating some helpers.
- 9:27:50Okay. Config folder is going to do what?
- 9:27:52configuration related things. Okay, for
- 9:27:53example, let's say we are going to
- 9:27:54manage our env here. Then we are going
- 9:27:57to have our middleware. Now middleware
- 9:27:59concept can be little tricky. We are
- 9:28:01going to understand what actually middle
- 9:28:02words are. But for now, let's create one
- 9:28:04middleware folder and then we are going
- 9:28:07to create a routes folder. This is where
- 9:28:08we are going to create all our routes.
- 9:28:11So for now these fold are fine. But
- 9:28:12before we go forward, right, let's
- 9:28:14understand what middleware is because
- 9:28:16you don't have to know coding for this.
- 9:28:18Let's try to simplify this one. Consider
- 9:28:20like this that you have your user here.
- 9:28:23Okay. And then you are doing some task
- 9:28:27but this task you have to do it only if
- 9:28:31this user is a logged in user. Loged in
- 9:28:34user means if until the user is actually
- 9:28:38logged in. We don't know that actually
- 9:28:40this user is a user or this user is a
- 9:28:42scammer. You don't know right? So that
- 9:28:44means you need something to understand
- 9:28:46this one. So this is where we have
- 9:28:48something called there is a barrier
- 9:28:50right so it's like a middleware so this
- 9:28:52middleware is going to check that if
- 9:28:54this user has certain permission or not
- 9:28:56if this user don't have permission so
- 9:28:58first of all it's it's going to check it
- 9:29:00has a permission if it has permission
- 9:29:02it's going to give you so it's going to
- 9:29:03give you okay you can go and do this if
- 9:29:06it don't have permission it's going to
- 9:29:07block the request here so this is the
- 9:29:09task of a middleware middleware is like
- 9:29:11a middle person it's going to do
- 9:29:13something now it's don't have to uh
- 9:29:15about always this user and permissions
- 9:29:17it can be anything right but the
- 9:29:19important part is that in the node
- 9:29:21middleware you have to understand that
- 9:29:22we have something called next concept
- 9:29:25now you need to know what actually this
- 9:29:27next is until unless we're going to
- 9:29:29implement in the code but you need to
- 9:29:30understand in a very simple way
- 9:29:32middleware is like a middle person
- 9:29:34that's going to do some task or it's
- 9:29:36going to check something and based on
- 9:29:38that it's going to move on to the next
- 9:29:40task. So in this case the next task is
- 9:29:42this one or else it's going to move on
- 9:29:44to the next route handler. So either it
- 9:29:46will move on to the next task or it will
- 9:29:50move to the next route handler and this
- 9:29:52you'll understand once we'll implement
- 9:29:54the code. Right? So for now first let's
- 9:29:56do one thing. Let's go here and then
- 9:29:59first let's go to env. Okay. And in the
- 9:30:01env what I'm going to do so first I'm
- 9:30:03going to take that okay this is my env
- 9:30:06file where we're going to have all our
- 9:30:08variables. So first of all we need to
- 9:30:09understand where we want to run this
- 9:30:11particular code. So now let's say I
- 9:30:13wanted to run this on port and I'm just
- 9:30:15going to give I'll give some random okay
- 9:30:17because I already have maybe some other
- 9:30:19port that are already occupied. So I'm
- 9:30:22just going to give this one as 5010. Now
- 9:30:24this is the port I wanted to run this
- 9:30:26particular project. As simple as that.
- 9:30:28You can use any port you want. For
- 9:30:30example, you can use 3,000, you can use
- 9:30:3230,0001, you can use 4,000. Okay? Or
- 9:30:34maybe let's use 4,000 here. So I'm going
- 9:30:36to use port 4,000 for this. Okay? And
- 9:30:40then I'm just going to use here node
- 9:30:42env. And in this case we are using
- 9:30:44development because we are doing
- 9:30:46development now. So we going to have two
- 9:30:47env now. Okay. So now what we going to
- 9:30:50do right we will go to our config and
- 9:30:53this is where we are going to create our
- 9:30:54base structure first. So we'll create
- 9:30:56env.ts and here what we going to do
- 9:30:59right? So first of all we need to load
- 9:31:01all our env variables right because
- 9:31:03until unless we're going to load the env
- 9:31:04variables we'll not be able to read it.
- 9:31:06So this is where we need the env
- 9:31:08package. So I'm just going to do env
- 9:31:10from
- 9:31:12env. And then how we are going to load
- 9:31:14it. Okay, I'll minimize this one. So to
- 9:31:16load this one, you're going to use
- 9:31:17something called env.config. Now this
- 9:31:20config is going to do what? It's going
- 9:31:21to load your env file containing into
- 9:31:23process. You have discussed remember
- 9:31:25process.default uh process. Default
- 9:31:28process. Env by default. Okay. If key is
- 9:31:32present, it simply attempts to load
- 9:31:33encrypted. Okay. Fine. First first you
- 9:31:35understand this one. loadb file contents
- 9:31:38whatever you have inside this it's going
- 9:31:40to load into process by default okay now
- 9:31:44this is very important because you are
- 9:31:46going to use this one a lot and then I'm
- 9:31:48just going to use export con and I'll do
- 9:31:51env
- 9:31:52here I'm just going to add all these
- 9:31:54whatever I have so first I have a port
- 9:31:56correct so the port will be so I'm going
- 9:31:58to take a number and here I'm just going
- 9:32:00to take process env dot
- 9:32:04so port here so I'll do
- 9:32:07and always give this uh value here.
- 9:32:10Okay, default. That's it. Then I'm
- 9:32:12simply going to check uh that if this is
- 9:32:15the products and environment or not. So
- 9:32:17I'll do is production send here. I'm
- 9:32:19just going to do process.v
- 9:32:21dot. So we have the node env.
- 9:32:26I'm just going to keep this one as
- 9:32:28development by default. So when this
- 9:32:30will be is production when this node
- 9:32:31enval to production that time it will be
- 9:32:35production if this is not that means we
- 9:32:37are doing a development okay so that's
- 9:32:40it I think these two and also let's do
- 9:32:41node envo
- 9:32:43so here also I'm just going to do
- 9:32:45process env
- 9:32:48or else this will be development and
- 9:32:50then I'm just going to keep this one as
- 9:32:51as const that's it so this will be my
- 9:32:54env file you will understand one concept
- 9:32:58remember I already mentioned the idea of
- 9:33:00this project is to give you the proper
- 9:33:02understanding of how you can create
- 9:33:03NodeJS project. Right? Now let's say you
- 9:33:04added two more ENV. You'll come here and
- 9:33:08you'll add two more ENV in this file.
- 9:33:10Now what will happen like when you're
- 9:33:11going to do env dot you're going to get
- 9:33:13this env. Environ
- 9:33:17is production and you can directly use
- 9:33:18this one like this. Okay. So this is the
- 9:33:20idea of this particular file. So this is
- 9:33:23first one. Now what we going to do
- 9:33:25right? So first when we need express so
- 9:33:27express is going to help us to create
- 9:33:29our web server. So that means we need to
- 9:33:31run our server correct. So that means
- 9:33:33first what we going to do right? So
- 9:33:34let's go to uh this app.ts file. I'm
- 9:33:38going to create entry file. So this will
- 9:33:40be my entry file for express. So means
- 9:33:44all express related logic I'm going to
- 9:33:46add here
- 9:33:48and this express related or this app.ts
- 9:33:51I'm going to add in this server.ts. So
- 9:33:53this is our main root file. Okay. And
- 9:33:56this will be entry file for express. So
- 9:33:58first of all what we need to do here. So
- 9:34:00first I'm going to do export function
- 9:34:04and I just do create app or create
- 9:34:07application whatever you're going to do
- 9:34:10and then first of all I need to do I
- 9:34:12need to create my express application
- 9:34:14right because how we are going to use
- 9:34:16express features until unless you're
- 9:34:18going to create the express instance.
- 9:34:20Correct? So in this case we need to
- 9:34:21first import express from express and
- 9:34:26then we are going to simply do express
- 9:34:28that's it. So what this is going to do
- 9:34:30create an express application the
- 9:34:32express function is a top level function
- 9:34:34exported by the express module. So this
- 9:34:36is the root and now if you do app dot
- 9:34:39and this is where you're going to get
- 9:34:41all of these each and every feature
- 9:34:43you're getting. So this is where this
- 9:34:46app is now holding each and every
- 9:34:48feature provided by express. So now we
- 9:34:50are keeping this one in a variable and
- 9:34:52now what we going to do right we are
- 9:34:54simply going to do app dot use and I'm
- 9:34:56simply going to do express dot JSON okay
- 9:35:00this is actually middleware again
- 9:35:03middleware we are going to understand
- 9:35:04once we create little uh more logic or
- 9:35:08once we'll go a little further because
- 9:35:09you need to understand that how this
- 9:35:11middleware work and it will be much
- 9:35:13easier to explain this one practically
- 9:35:14for now you understand this is actually
- 9:35:16a middleware that parses JSON And it's
- 9:35:20only look at the request where the
- 9:35:21content type header matches the type
- 9:35:23option. You'll understand what we are
- 9:35:24doing in this particular case. I'm going
- 9:35:26to explain and then I'm going to simply
- 9:35:29do now let's say here what I can do
- 9:35:32right in this particular file. Now what
- 9:35:34I mention that when you are going to
- 9:35:36have a root file when this code will
- 9:35:38compile this particular file will be
- 9:35:40read at first. So it's going to check
- 9:35:42each and everything. Now let's say you
- 9:35:44are using this create app method here.
- 9:35:47So then it will go to this particular
- 9:35:49file and this is where you have to add
- 9:35:51all your routes, all your middleware and
- 9:35:53everything. What I need to do right
- 9:35:55first of all I need to have some feature
- 9:35:58in this particular uh project so that
- 9:36:00whenever there is a any error happens or
- 9:36:03let's say something crashes
- 9:36:05this particular application should
- 9:36:06automatically pick that error and it
- 9:36:08will notify the user. Okay, there is a
- 9:36:10problem you try after something. So that
- 9:36:12means we need to have a global error
- 9:36:14handler. You're getting global error
- 9:36:17handler. we need to have. Okay. What
- 9:36:19this error handler is going to do? This
- 9:36:21error handler is simply going to give
- 9:36:23you one information. Okay. Something
- 9:36:24happened. There is internal server
- 9:36:26error. There is a network error.
- 9:36:28Whatever. Not network but internal
- 9:36:29server error happened. So that
- 9:36:31middleware we need to create. Every time
- 9:36:33that uh something breaks so that
- 9:36:35middleware will get executed and then
- 9:36:37that user will be notified. Okay. But
- 9:36:40for this before that also we are going
- 9:36:42to use one more package that is called
- 9:36:45Pino. Okay. So let's see what actually
- 9:36:46Pino is. So what is Pino?
- 9:36:50So it's a logger. Okay, you can see that
- 9:36:52it's a highly popular opensource logging
- 9:36:55utility means here whenever we want to
- 9:36:58notify the user. Okay, let's or maybe
- 9:37:00let's consider from a developer point of
- 9:37:02view. When you're developing, let's say
- 9:37:04you're running the code, you want to
- 9:37:05check your logger. This is going to
- 9:37:07generate the logs in a structured JSON
- 9:37:09format. Okay. Now this is also very
- 9:37:12fast. You see that this is also very
- 9:37:13fast. So we going to use this pino
- 9:37:16package. Okay. So here we will use this
- 9:37:18pen and then I'm just going to show you
- 9:37:20I think it's called pino pretty. Okay.
- 9:37:23So let's see this one pen pretty these
- 9:37:25two.
- 9:37:27Okay. So we can directly so this is
- 9:37:29actually uh how we are going to do this.
- 9:37:32So you can see that we have a logger. We
- 9:37:34have to set a label and then you can use
- 9:37:36some transport here. Okay. So colorize
- 9:37:38means it's going to enable your color
- 9:37:40tags. Uh this is the trans. So human
- 9:37:43readable time stamp. So it's going to
- 9:37:44create human readable time stamp and
- 9:37:46also it's going to ignore if you want to
- 9:37:49ignore something. But for now these two
- 9:37:50I think more than enough. And also I
- 9:37:53think we also need to take the target
- 9:37:55also. I think target is also I think
- 9:37:57optional. So what we will do right label
- 9:37:59we need to take and transport we are
- 9:38:02going to check if this is production we
- 9:38:04are going to do this. uh
- 9:38:07no sorry if this is not production we
- 9:38:09are going to do this or else we're going
- 9:38:10to do undefined in production fall back
- 9:38:12to raw altra first JSON string okay so
- 9:38:15this is what we are going to build uh
- 9:38:17now remember one thing I wanted to
- 9:38:19mention see whenever I'll create these
- 9:38:20videos right so you can see that I'm
- 9:38:22doing this on real time means I'm
- 9:38:23actually I don't have all of this
- 9:38:25created so I'm just checking and writing
- 9:38:27what I mean by this whenever I'm going
- 9:38:29to do let's say there is some mistake or
- 9:38:30some slowness in the explanation it's
- 9:38:34your responsibility You have to do your
- 9:38:36own research and you have to learn.
- 9:38:37You're getting what I'm talking about.
- 9:38:38For example, let's say there are
- 9:38:39something I'm not explaining. You have
- 9:38:42to go or in this case you can make use
- 9:38:43of some AI tools also that will be much
- 9:38:46easier. So in this case what you're
- 9:38:47going to do right. So first let's do npm
- 9:38:49install. So we'll go here and we'll do
- 9:38:52npm install pino and then we have pino
- 9:38:55pretty.
- 9:38:59That's it. Now where we going to add
- 9:39:02this pino file? This you are going to
- 9:39:04add in your leave file. Okay. So this
- 9:39:07will be a logger.ts. So first what I'm
- 9:39:09going to do I'll do import
- 9:39:12pino from
- 9:39:15pino and then also I'm just going to I
- 9:39:18think let's do export
- 9:39:20con logger and here I'm simply going to
- 9:39:23take pino.
- 9:39:25So first I'm going to take the label. So
- 9:39:27label will be where uh okay also one
- 9:39:30very important point let's do one thing.
- 9:39:32Let's go to env see what we are doing
- 9:39:34each and everything you have to
- 9:39:35understand. So instead of adding
- 9:39:36directly hard coding there I'm just
- 9:39:38going to do log level here and I'm just
- 9:39:40going to keep by default info. Okay. So
- 9:39:42log level will be info. So now if I'm
- 9:39:44adding a new one so I need to go to env.
- 9:39:46And then I'm just going to add a log
- 9:39:48level and this will be process env dot.
- 9:39:53So log level or else this will be info.
- 9:39:58Okay. So now we'll go here and then I'm
- 9:40:01just going to use env that I need to
- 9:40:03import and I can use this log label here
- 9:40:06you're getting. So this is how you're
- 9:40:07going to do this. So once this is done
- 9:40:09now we are going to also add a
- 9:40:11transport. So here I'm going to add a
- 9:40:12transport. So I'm going to check if this
- 9:40:14EMV do is production. So in that case
- 9:40:17I'm going to keep undefined. And for
- 9:40:19Dave I'm just going to keep a target.
- 9:40:22And here we are simply going to take
- 9:40:24let's take this one target as we know
- 9:40:27pretty
- 9:40:30also we're going to take options. And
- 9:40:32here I'm just going to take colorize.
- 9:40:35Let me just copy from here.
- 9:40:39Colorize will be true.
- 9:40:43And also we need this one. Translate
- 9:40:45time we're going to keep so that we'll
- 9:40:47get human readable time stamp.
- 9:40:52I think this is done. Why getting error?
- 9:40:54Oh, we have to do this. Okay. So I think
- 9:40:57this is done. So we taking a label which
- 9:40:59will be log label. By default it will be
- 9:41:00info because that is what we have added
- 9:41:02here. Transport target we targeting pen
- 9:41:06pretty options color is true and
- 9:41:08translate time sard.
- 9:41:10Okay. So this is what I need. So now
- 9:41:12what we can do right we can use this. So
- 9:41:15maybe let's do one thing. We need to use
- 9:41:17this one in all the places. First let's
- 9:41:20go to middleware and then I'm going to
- 9:41:21create a global error handler. Okay. So
- 9:41:23I'm just going to do error handler
- 9:41:27TS file. And now this I already
- 9:41:30mentioned this is going to give you a
- 9:41:31general idea whenever there is something
- 9:41:33goes wrong we are going to trigger this
- 9:41:35middleware. And this we're going to use
- 9:41:36in our app.ts file. So here I'm simply
- 9:41:39going to do export function and I'll do
- 9:41:41error handler. So first of all here I'm
- 9:41:44going to take a error. This will be
- 9:41:46error. Then I'm going to take a request
- 9:41:50and this will be okay. This particular
- 9:41:53request is the request object from
- 9:41:55express. Okay. So this will be now you
- 9:41:59consider this one as you remember in the
- 9:42:00HTTP model we have discussed about the
- 9:42:02request and response. You consider this
- 9:42:05one similar to this. Okay. I think now
- 9:42:07this will be easier to understand and
- 9:42:09this is the reason in the uh HTTP model
- 9:42:11we have explained all of this in details
- 9:42:13that if you don't use any third party
- 9:42:15framework how this will look. So this is
- 9:42:17consider this one as that only. So this
- 9:42:19request object this is a response
- 9:42:21object. So here I'm going to take this
- 9:42:23one as request then I'm going to take a
- 9:42:25response because I need to return the
- 9:42:26response. And now you have this next
- 9:42:30function here. Okay. Now this next
- 9:42:32function is extremely important concept.
- 9:42:34This we are going to understand little
- 9:42:36later. So for now let's take this one as
- 9:42:38next function from your express. Okay.
- 9:42:40Okay. Let me explain what this next
- 9:42:41function is. So what happen right when
- 9:42:43you are doing let's say you have a
- 9:42:44middleware. Okay. And you are doing one
- 9:42:47user is doing a task and this task is
- 9:42:49here. So this is task one. Now you have
- 9:42:54or maybe let's uh let's take this on
- 9:42:56task one. Let's take this on task two.
- 9:42:59So this is task two. This is task one.
- 9:43:02And then in middle you have a
- 9:43:04middleware. What this middleware is
- 9:43:06going to do? Let's say user already
- 9:43:08completed the task one. So this task is
- 9:43:10completed. Before it will go to T2 which
- 9:43:14is task two. You have a middleware in
- 9:43:16middle. So this middleware is going to
- 9:43:18check something. I already mentioned it.
- 9:43:20It's going to do something. If this
- 9:43:22condition satisfies means this condition
- 9:43:24whatever you given if this satisfies
- 9:43:26it's going to move to next one. Right?
- 9:43:28But how it's going to move? This is
- 9:43:30where this next is actually important.
- 9:43:32So when you're going to use middleware
- 9:43:34we are going to call this next function.
- 9:43:37So next means now we are simply telling
- 9:43:38middleware oh your task is completed now
- 9:43:40you have to go to next task. So that is
- 9:43:43the reason once will move to sorry once
- 9:43:45you call the next it will go to task
- 9:43:47two. What will happen if you don't use
- 9:43:49this next function. Consider user
- 9:43:51completed task one. Task one now called
- 9:43:54middleware. Middleware already check
- 9:43:57condition satisfies. Now it should go to
- 9:43:59task two. But you haven't called the
- 9:44:00next function. What will happen? it will
- 9:44:02be stuck here forever. It will never go
- 9:44:04to the next part. You're getting so this
- 9:44:06is where this next function is very
- 9:44:07important. So whenever you're going to
- 9:44:09do some functionality or using
- 9:44:11middleware you need to call the next
- 9:44:13function only if that needed only if
- 9:44:15that functionality is needed. Okay. So
- 9:44:18now this is our next function. What we
- 9:44:20going to do right? Okay let's take this
- 9:44:22one. We are going to return void. We're
- 9:44:24not doing anything here. And then I'm
- 9:44:25simply going to take my logger from the
- 9:44:27logger that I have created. And here
- 9:44:29this logger is going to give you some
- 9:44:31label. For example, you can use logger
- 9:44:32dot error logger. fatal logger.info. Now
- 9:44:36because we creating error handler. So
- 9:44:38what will happen? We need to do logger
- 9:44:39dot error. Correct? And this is where
- 9:44:41you're going to pass the error. This
- 9:44:43error. Okay. Whatever error will happen.
- 9:44:45And then I'm just going to give a
- 9:44:47general message that unhandled error.
- 9:44:52Okay. Now what we have to do right, we
- 9:44:54need to return a response back to user.
- 9:44:57Correct. See whenever you are going to
- 9:44:58create an application you're going to
- 9:45:00have your back end you're going to have
- 9:45:01your UI correct. So when your back end
- 9:45:04will do ter some task it's going to
- 9:45:06return the response back. This is where
- 9:45:08this response object is helpful. So now
- 9:45:11this is where what we can do right you
- 9:45:12can do race dot status. Now all these
- 9:45:14things now you're seeing this is
- 9:45:16actually provided some of the things
- 9:45:18provided by express out of the box. So
- 9:45:20this is why we need some framework to
- 9:45:22make make our life much easier. You can
- 9:45:24do the exact same thing using node HTTP
- 9:45:27package also. I think we have already
- 9:45:28discussed this on previously. So here
- 9:45:30I'm going to return a 500. And now I'm
- 9:45:32going to return a JSON response back.
- 9:45:34Okay, this JSON response is going to
- 9:45:36return a success. This will be false
- 9:45:39because something goes wrong. And then
- 9:45:40I'm also going to return a message and
- 9:45:42this will tell that okay there is a
- 9:45:44internal
- 9:45:46server error. Okay. So this will be our
- 9:45:49global error handler. But how we are
- 9:45:51going to use this? So to use this one we
- 9:45:53have to tell express that okay you are
- 9:45:55going to use this error handler. So to
- 9:45:57do this one we are going to do
- 9:46:01uh app dot and you have something called
- 9:46:03use which is very important right. So
- 9:46:05this use is going to use something. So
- 9:46:08what you're simply telling that okay
- 9:46:09whenever something goes wrong you're
- 9:46:10going to use this error handler. So it
- 9:46:12will automatically check that and it's
- 9:46:14going to give you the error. It's going
- 9:46:15to throw error. So this is the first
- 9:46:17thing. Now next we need to create one
- 9:46:20more middleware. what actually that med
- 9:46:22and that is not found. Consider you are
- 9:46:25creating some APIs right user is hitting
- 9:46:27this login endpoint correct now let's
- 9:46:30say user it's possible right user can
- 9:46:32give some random value for example let's
- 9:46:34see if I go here and I give some random
- 9:46:36value here whatever you can do here it
- 9:46:39will not work okay here they have added
- 9:46:41the logic that whatever you'll put it
- 9:46:43will always go to here but normally in
- 9:46:44your application let's say this route is
- 9:46:46not even present so what we have to do
- 9:46:48right you have to tell user okay this is
- 9:46:50not found this route that you are trying
- 9:46:53to access is not found you're getting.
- 9:46:55So this is a generic message that you
- 9:46:58should always implement before even
- 9:46:59starting your logic. So that is the
- 9:47:01reason we need one more middleware and
- 9:47:03this will be not found. DS and this is
- 9:47:07very simple. Okay. So do export function
- 9:47:09and I'll do not found and this is where
- 9:47:12we also need the request and response.
- 9:47:17So we're going to get request and
- 9:47:18response. So I'll do not found. So I'll
- 9:47:21have the request and this will be my
- 9:47:24request. I'm going to have the response.
- 9:47:26This will be my response. It's going to
- 9:47:28return v. And what I'll do right, I'll
- 9:47:30simply do raise dot status
- 9:47:33404
- 9:47:35dot json. And here also I'm simply going
- 9:47:37to do success will be false and message
- 9:47:41will be route not found.
- 9:47:49So save this and now we'll go here
- 9:47:50again. We'll do app dot use not found.
- 9:47:54That's it. So it's going to use these
- 9:47:56two middleware. Okay. Now this is see
- 9:47:58whatever we are doing at extremely
- 9:48:00important. These are the base concept
- 9:48:01that you need to do. Now let's do one
- 9:48:03thing. Let's go here and understand what
- 9:48:06actually this is. So I'll do express URL
- 9:48:08encoded.
- 9:48:12Okay. So you can see that express
- 9:48:15encoded is a built-in middleware. Again
- 9:48:16this is also another middleware that
- 9:48:18parses incoming request with URL encoded
- 9:48:21payloads. Okay. Now how we are going to
- 9:48:23understand this one? This we will
- 9:48:25understand once you'll implement our
- 9:48:26CRUD. Remember I already mentioned the
- 9:48:28concept of CRUD create react because
- 9:48:30that time when you're going to pass some
- 9:48:32form data from your UI side. So in that
- 9:48:34case this the browser encoded the data
- 9:48:36into long query string format the middle
- 9:48:39inter blah blah and then it's accessible
- 9:48:41in your request. body. For now this will
- 9:48:43be a little tricky to understand but we
- 9:48:45need to use this one in our main entry
- 9:48:47file. So for now what we are going to do
- 9:48:49right we are simply going to use this.
- 9:48:50So do app dot use express url encoded
- 9:48:53and then we're going to pass extended
- 9:48:54will be true. So this is going to
- 9:48:56middleware that is going to pass your
- 9:48:58incoming HTML form submission. So
- 9:49:00whenever you're going to have some kind
- 9:49:01of form you're going to pass some form
- 9:49:03data. This is where you need this
- 9:49:05particular middleware. Okay. So now
- 9:49:07we'll go here and after this I'm simply
- 9:49:10going to do app dot use and I'll do
- 9:49:11express dot url encoded and you're going
- 9:49:14to pass a extended will be true.
- 9:49:18I hope this is clear. So with this this
- 9:49:20part is completed. So in the next
- 9:49:21section what you're going to understand
- 9:49:22we understand what actually course is
- 9:49:24and then we'll create our first route.
- 9:49:26We are going to create our entry file.
- 9:49:28This is not yet created. We are going to
- 9:49:30run our express server and we'll test in
- 9:49:32postman that if this is working or not.
- 9:49:34So let's do this this one in the next
- 9:49:36part. All right. So what actually course
- 9:49:38is right? So course is nothing but cross
- 9:49:40origin resource sharing. Now you
- 9:49:41understand this one with practical way.
- 9:49:43So consider you have a front end URL you
- 9:49:45have a back end URL. Correct. Now what
- 9:49:47usually happen right? So first of all
- 9:49:49let's understand how you can do this. So
- 9:49:51we're going to install this course
- 9:49:52package and then you simply tell our
- 9:49:54express. So app dot use means what app
- 9:49:57is your express app. So you're going to
- 9:49:58tell express okay you are going to use
- 9:50:00quartz. That means you're going to allow
- 9:50:02all cross origin request. But what will
- 9:50:05happen? Let's say you have a certain
- 9:50:06front end URL and here in this case you
- 9:50:09can see this is actually very dangerous
- 9:50:10right so you should restrict access to
- 9:50:13your specific front- end domain only so
- 9:50:15that means you have a front end domain
- 9:50:17and you want that particular domain only
- 9:50:20can call your backend URL or that
- 9:50:23particular domain only can call your
- 9:50:25backend endpoint or backend API so in
- 9:50:27that case what you have to do right you
- 9:50:29have to explicitly say okay whenever
- 9:50:32you're going to set this course in your
- 9:50:33back end code something like this that
- 9:50:35in know in my course option I have a
- 9:50:37origin this is my front end URL this is
- 9:50:40the method that I want or this is the
- 9:50:42allowed HTTP action and then you can use
- 9:50:45some other options also here okay maybe
- 9:50:47let's see if you're using cookies you
- 9:50:49can from your u you can you have to pass
- 9:50:51credentials equal to true all these
- 9:50:53things but first you have to understand
- 9:50:54that this is how you can uh use a
- 9:50:56filtered course options mean now this
- 9:50:59particular origin only can do a request
- 9:51:02to your backend APIs but in our case
- 9:51:04what we're going to do right so for now
- 9:51:06let's do one thing I think we haven't
- 9:51:07installed the course package I think
- 9:51:09let's do this
- 9:51:12so do npm install course
- 9:51:16clear and then npm install - d
- 9:51:21we also need the course types
- 9:51:24that's it and then first of all what
- 9:51:26we're going to do right so let's go to
- 9:51:28our appts
- 9:51:30and I'm going to import here so I'll do
- 9:51:32import course from course
- 9:51:38and then uh we'll directly use it here.
- 9:51:41So for now I'll just do app dot use and
- 9:51:43then I'll do course if I get time so I'm
- 9:51:45just going to give you some uh advanced
- 9:51:47level of exp or maybe let's do this one.
- 9:51:49Let's keep this one for your node just
- 9:51:51part two because anyways we're going to
- 9:51:52create our microservices and all but
- 9:51:55here you can directly use all of this
- 9:51:56right. So you can pass a option and then
- 9:51:58let's say you can give uh this let's say
- 9:52:00we're going to pass origin you can add
- 9:52:02all of these all these allowed headers
- 9:52:04so many options are there. So for now I
- 9:52:06think this is fine. So this is going to
- 9:52:07handle your chords. Now what we're going
- 9:52:10to do right so let's go to routes and
- 9:52:12then I'm just going to do health dot
- 9:52:15routes.ts.
- 9:52:17Okay so this will be our first route
- 9:52:19that we wanted to create. So first of
- 9:52:21all you have to understand now how you
- 9:52:22can create a express route. So here we
- 9:52:26have something called router. So that we
- 9:52:28are going to import. So we are going to
- 9:52:29import this one from express and this
- 9:52:31will be my router. So now whenever I'm
- 9:52:33going to create a router I need to use
- 9:52:36this router just like in my app.ts when
- 9:52:38I'm creating express application I need
- 9:52:40to use this correct. So that's why first
- 9:52:42of all I'm just going to do export con
- 9:52:44and I'll do health route
- 9:52:47or router and then again I need to use
- 9:52:49this. So that means now this health
- 9:52:52router is going to give me all the
- 9:52:54information that I need for example all
- 9:52:56this method each and every property you
- 9:52:58can use. So what you're doing right
- 9:52:59you're putting all you're taking all the
- 9:53:02functionality or feature that this
- 9:53:04particular thing will give and you're
- 9:53:06assigning this one to a variable. Now
- 9:53:08what I can do right I can do a health
- 9:53:10router and let's say I wanted to create
- 9:53:11a get request. Okay so I can do like
- 9:53:14this get if I wanted to create a post
- 9:53:16request I can do post. If I wanted to
- 9:53:18create a put, I can do put. If I wanted
- 9:53:20to delete, you have options. So many
- 9:53:22things. So here I'm just going to do a
- 9:53:24get. This first of all you have to give
- 9:53:26the path. So this will be your API path.
- 9:53:28Okay, this will be your API path. Now
- 9:53:31this is important concept. So whenever
- 9:53:33let's say you have a front end URL that
- 9:53:35is running on local host 3000 correct
- 9:53:40now from your front end when you're
- 9:53:42going to call this AP endpoint, how
- 9:53:44you're going to call where your back end
- 9:53:46is running? your back end is running on
- 9:53:47localhost 4,000. So now this will become
- 9:53:51local host 4,000 / health. Now let's say
- 9:53:55what I wanted to do right I don't want
- 9:53:57to do like this for [snorts] all my API
- 9:54:00endpoints I wanted to add a prefix API.
- 9:54:03So in that case what you can do so you
- 9:54:04have to do / API/ health/ API/ users/
- 9:54:09API/ product something like this. Okay.
- 9:54:12So now here what I'll do right. So for
- 9:54:14now I'm going to take a health and now
- 9:54:17here what I have to do right you have to
- 9:54:18create the handler. Okay. So here I'm
- 9:54:20going to have my request object.
- 9:54:23I'm going to have my response object.
- 9:54:26And then what I need to do I need to
- 9:54:28return a response back. Response back to
- 9:54:31the user. You think always like this.
- 9:54:33You have to return back something to the
- 9:54:35user because at the end your user is
- 9:54:37going to see something on UI. Correct.
- 9:54:39So you're going to do raise dot status.
- 9:54:41This will be what? 200 because this is a
- 9:54:43success and this will be JSON and here
- 9:54:46you're going to pass a success would be
- 9:54:48true and then I'm going to pass a
- 9:54:50message. I'll do health route is
- 9:54:54working. So this will be the first route
- 9:54:57that I have created. Okay. Now what is
- 9:54:59what will be best approach here? You
- 9:55:01should you can directly use this in
- 9:55:02app.ts but I'll not suggest what you
- 9:55:05should do right you should always create
- 9:55:06a entry file here. So this will be your
- 9:55:09root route file. Okay. So this is where
- 9:55:12you're going to combine all your routes
- 9:55:16of application means every route. Let's
- 9:55:18say you're having a 1,000 route also
- 9:55:21you're going to add it here. Okay. So
- 9:55:23now again we need to create our router.
- 9:55:26So I'll do uh import something from
- 9:55:28express. So I'm just going to take
- 9:55:30router.
- 9:55:32And now what I'm going to do right I'm
- 9:55:34just going to do export con. I'll do API
- 9:55:37router and this will be router. So now
- 9:55:41what I need to do right I need to tell
- 9:55:42that okay this API router now needs to
- 9:55:45use this health router. Okay how we are
- 9:55:47going to do this? So it's simply going
- 9:55:49to do okay API router you have to use
- 9:55:52this health router that's it so now what
- 9:55:55you have done you have actually plugged
- 9:55:57this health router in this component. So
- 9:56:00you're plugging
- 9:56:02all routes
- 9:56:04in one place. Okay. So later let's say I
- 9:56:08created a user router. So you can do app
- 9:56:10API router dot use and you'll do user
- 9:56:13router. So you can have user router. I'm
- 9:56:16just showing okay these things are very
- 9:56:18important. Let's say you're creating a
- 9:56:19post router. You're creating a admin
- 9:56:22router. You're creating let's say
- 9:56:24dashboard router whatever. So you're
- 9:56:25going to combine all of these here in
- 9:56:27this component. Something like this.
- 9:56:29So I think this is done. Save this. So
- 9:56:31now what we have to do right? We have to
- 9:56:33use this in this app dot ts. Now this is
- 9:56:35one of the most important concept that
- 9:56:36we are going to understand. So now what
- 9:56:38I have to do right see this is why we we
- 9:56:41created this. So we don't have to use
- 9:56:42all this individual route in this
- 9:56:44component. What I can do right you can
- 9:56:46simply tell okay app dot use and this
- 9:56:48will be our base API endpoint. Now this
- 9:56:51is very very important. App dot use this
- 9:56:55starts with API. So that means when we
- 9:56:58are going to call this one from our
- 9:56:59front end it will understand okay this
- 9:57:01is the API that we are calling and here
- 9:57:03we are going to simply pass API router
- 9:57:06that's it. So now if you go here what
- 9:57:10will be the final
- 9:57:12uh endpoint here final endpoint will be
- 9:57:15your backend URL which is localhost
- 9:57:194000
- 9:57:20/ API because you are actually adding a
- 9:57:24prefix here. Okay, this is your prefix
- 9:57:26API. Okay, and then you're going to have
- 9:57:29/ health like this. So this will be your
- 9:57:32actual route. So health first API will
- 9:57:36come and then health will come here like
- 9:57:38this. So I think this is completed and I
- 9:57:41think this is done. Now what you're
- 9:57:43going to do and at the end you're going
- 9:57:44to return the app here.
- 9:57:47This app that's it. So now this is
- 9:57:49completed and then we'll go to our
- 9:57:51server.ts. This will be our entry file.
- 9:57:53This is where we are going to do what?
- 9:57:55We are going to call this create app.
- 9:57:58Call this create app means we're going
- 9:57:59to call this method and we're going to
- 9:58:01run our server because we haven't run
- 9:58:03the server yet. Okay. So I'm just going
- 9:58:05to do con app will be create app. This
- 9:58:10is the first thing. And then I'm simply
- 9:58:12going to do app.list.
- 9:58:14So it will listen to a port, right?
- 9:58:16Which port? We have the port 4,000. So
- 9:58:18we are going to do envo
- 9:58:22and then we're going to take a call back
- 9:58:24and here I'm simply going to do logger.
- 9:58:26We already have the logger right and
- 9:58:28we'll do logger.info. And now I'll do
- 9:58:30server is now running on port. And here
- 9:58:36let's do like this on port http
- 9:58:41localhost.
- 9:58:43And then here I'm just going to pass the
- 9:58:44env.
- 9:58:47That's it. Now you'll see that how much
- 9:58:49cleaner this particular file is and that
- 9:58:51is the reason we mentioned that this is
- 9:58:52our entry file. So what will happen when
- 9:58:54the code will run it will come here it
- 9:58:56will check this it will go here it will
- 9:58:59check all of this it will check okay
- 9:59:00this route we have it will go here it
- 9:59:03will check again we have the health
- 9:59:04route it will go to this particular file
- 9:59:08you're getting so simply we are plugging
- 9:59:10each and everything and we are keeping
- 9:59:12in a single file so now we have to go to
- 9:59:15package dojson and then I'm just going
- 9:59:17to add some script here okay let's
- 9:59:19remove this
- 9:59:21uh what are the script we need so Let's
- 9:59:23take a div script. So here I'm just
- 9:59:26going to take a tsx watch. I think this
- 9:59:28you already know we have the
- 9:59:30source/server.ts
- 9:59:31file. So this is our entry file. Okay.
- 9:59:34Then I'm just going to do a build. Build
- 9:59:36will be our ts uh tsc. And then also I'm
- 9:59:41just going to take a start and start
- 9:59:44will be all node dish folder because if
- 9:59:47you go to ts config we have the dish
- 9:59:50folder that where we are going to
- 9:59:51compile and we are going to keep the
- 9:59:52code. So here we will we'll be having
- 9:59:54dish/server.js
- 9:59:56file.
- 9:59:58Save this. And now let's go here. And to
- 10:00:01run this what we have to do we have to
- 10:00:02run the script. Correct? And also here
- 10:00:05we can do like this. So we will do dist
- 10:00:08/ server.js. JS save this and I'm just
- 10:00:12going to do npm run dev
- 10:00:16that's it server is now running on and
- 10:00:18this is the reason we have using
- 10:00:19coloring so you can see that it's coming
- 10:00:21as a color so local host 4,000 correct
- 10:00:23now we'll go here copy this
- 10:00:26and then I'll just paste it here and
- 10:00:28I'll do / health let's see what is
- 10:00:30happening and I will send here route not
- 10:00:33found fantastic now from where it is
- 10:00:35coming I'll see that each and every
- 10:00:37middleware that we have created that are
- 10:00:39getting executed. So now this health
- 10:00:41route is not present correct because
- 10:00:43what you have to do you have to do /
- 10:00:44API/ health. So that is the reason
- 10:00:46you're getting route not found but this
- 10:00:48is actually coming from here not found.
- 10:00:50So this middleware is getting executed.
- 10:00:53You're getting so now this will
- 10:00:55automatically check that okay this route
- 10:00:57is not found. I need to run this one.
- 10:00:59But if I just do localhost 4000/ API/
- 10:01:02health send this you're getting health
- 10:01:04route is working. Fantastic. So in this
- 10:01:06case now it will go check here. it will
- 10:01:08go and okay there is a health route is
- 10:01:11present so that means this is working so
- 10:01:13this will be the base setup that we have
- 10:01:16done so we've understood about the
- 10:01:17course and how we can create express
- 10:01:19route and then express server so with
- 10:01:21this now let's move on to the next one
- 10:01:23okay so what will be our next step now
- 10:01:25remember for this particular project we
- 10:01:27are going to use docker and uh database
- 10:01:30will be definitely possess right now
- 10:01:32what I wanted to mention right uh if you
- 10:01:34are not aware because see for me it's
- 10:01:36not possible to again cover docker and
- 10:01:38docker compose all of these what I'll
- 10:01:40suggest you just go to YouTube and just
- 10:01:41search with my name and uh just type
- 10:01:44DevOps and you'll see that you are going
- 10:01:45to get multiple DevOps videos and I also
- 10:01:49have multiple Docker videos also so if
- 10:01:51you want you can watch this video also
- 10:01:53DevOps full course part two so here I
- 10:01:55have covered the uh docker in details
- 10:01:58same you can also if you want to learn
- 10:02:00with GitLab CI/CD you can watch this one
- 10:02:03or the best will be you can watch the
- 10:02:04first one okay so if you click here and
- 10:02:07you'll see There there will be time
- 10:02:08stamp and where you will be able to see
- 10:02:10docker and docker compose only this part
- 10:02:12you can watch from this uh time stamp
- 10:02:14okay why I'm saying this one because see
- 10:02:17when uh we are going to use or whenever
- 10:02:20we are going to create any products
- 10:02:21level nodejs application right
- 10:02:23especially in the part two we are going
- 10:02:24to cover microservices in part three we
- 10:02:26are going to have some more advanced
- 10:02:28topic docker is one of the best
- 10:02:30absolutely best uh thing that you can
- 10:02:32use okay there is no competition because
- 10:02:35whenever you go to any product based
- 10:02:37company, service bus company doesn't
- 10:02:38matter you'll see that almost all the
- 10:02:41company more than I will say 80 to 90%
- 10:02:43of the projects they are using docker
- 10:02:46okay uh and also it's industry standard
- 10:02:48and that is the main reason I've decided
- 10:02:50to use docker for this particular
- 10:02:52project also and this is very very
- 10:02:54simple what we're going to do right we
- 10:02:55are not going to have multiple services
- 10:02:57for now we are going to have only one
- 10:02:59service and if needed later we are going
- 10:03:00to add more so we are going to have only
- 10:03:02one service and that will be posgress
- 10:03:04and you can see that maybe let's Uh
- 10:03:06first of all let's copy this and then
- 10:03:08I'm just going to change couple of
- 10:03:09things. So what we going to do right in
- 10:03:11this project root we are going to create
- 10:03:14our docker compos file. Okay. So in the
- 10:03:16project root you create docker compose
- 10:03:19do yimml. The reason is very simple
- 10:03:21because in this particular section we
- 10:03:23are going to create all our uh sql
- 10:03:25schemas migration and also we are going
- 10:03:28to create our own custom migration
- 10:03:30script. We are not going to use any
- 10:03:31third party packages. Okay. So here just
- 10:03:33paste it and then I'm just going to
- 10:03:35change this one from support desk
- 10:03:37posgress to let's do this one as nodejs
- 10:03:40capstone
- 10:03:42project. Okay that's it and I think we
- 10:03:46don't have to make any other changes. So
- 10:03:48what we are doing we are using port. So
- 10:03:50if you want you can use a different
- 10:03:51port. Maybe let's say I'll use 5 440.
- 10:03:55Uh and then uh we are using posgress
- 10:03:57image. This is our container name. This
- 10:03:59will be our posgress user posgress
- 10:04:01password and also posgress db. You can
- 10:04:03also change this one to something else.
- 10:04:05Maybe let's do this one as nodejs
- 10:04:07capstone.
- 10:04:10Okay. So this is the first thing that we
- 10:04:12are going to use. Okay. Once we'll do
- 10:04:14that what we are going to do right also
- 10:04:17I'm pretty much sure that you should
- 10:04:18have docker desktop installed on your
- 10:04:22device. You can see that I already have
- 10:04:24this one. So what we have to do? We have
- 10:04:25to start this particular service and
- 10:04:27also we need to go to our renv and we
- 10:04:30need to add our DB URL also. But before
- 10:04:32this first let's do one thing. Let's
- 10:04:34start our uh docker service. So what I'm
- 10:04:36going to do I'm just going to do docker
- 10:04:38compose up - d and let's see what you're
- 10:04:41getting.
- 10:04:45All right. So you can see that this is
- 10:04:46now created and you're going to have
- 10:04:48this capstone project. And if I go here
- 10:04:50you can see that this is running on this
- 10:04:51particular port. So that's it. We don't
- 10:04:54have to make any other changes for now
- 10:04:55because we are going to understand each
- 10:04:57and everything manual way. What I mean
- 10:04:59by manual ways if I want I can I thought
- 10:05:01that I'm going to use some custom sorry
- 10:05:03some packages for migration and
- 10:05:05everything. But I think some of you also
- 10:05:06commented that no we want to learn uh
- 10:05:09our own manual migration. So that is the
- 10:05:11reason we are going to create our own
- 10:05:12SQL migration logic. Okay before this
- 10:05:15what we going to do right we are going
- 10:05:17to add one database URL here. So I'm
- 10:05:19just going to do database URL and let's
- 10:05:21give this one as. So you'll have to do
- 10:05:22postgress because we'll do postgra SQL
- 10:05:25and this will be like this. So posgress
- 10:05:27and then you have to give posgress your
- 10:05:29username your password and then you have
- 10:05:32to give your port. So do local host
- 10:05:36and this is running on 5440 right? So
- 10:05:40you're running on 5440. If you're using
- 10:05:42some different port you can use that.
- 10:05:44And then we are going to give that uh
- 10:05:46what is that name? So you're using more
- 10:05:48JS capstrong. So you're going to use
- 10:05:49this one. Okay,
- 10:05:52that's it. So now let's save this. And
- 10:05:54also it's I think you can also do this,
- 10:05:57right? You can also use underscore here
- 10:06:00if you don't want to use this one. I
- 10:06:01think that would be better. But it's
- 10:06:02fine. Uh so for now I think that's all
- 10:06:05we have to do this particular. So I
- 10:06:07wanted to mention couple of things. So
- 10:06:08the prerequisite for this particular
- 10:06:10section will be better. If you already
- 10:06:12know Docker that's fine then you can
- 10:06:14just continue or else watch that video
- 10:06:16that I mentioned. Second, install docker
- 10:06:18desktop and then you just have to create
- 10:06:20docker compos file and start the
- 10:06:22container. That's it. Now what we're
- 10:06:24going to do right so first let's go to
- 10:06:26our okay we are already here. So we need
- 10:06:28to understand that okay how many users
- 10:06:30we sorry how many tables we need first
- 10:06:32of all I'm pretty much sure you've
- 10:06:34already completed the postgress section
- 10:06:37right in the previous because if you
- 10:06:38don't do this one it will be tricky okay
- 10:06:40I highly recommend you watch that
- 10:06:42complete section do not skip that will
- 10:06:44be a game changanger for you because in
- 10:06:46that whoever completed you'll agree with
- 10:06:48me that I actually covered almost all
- 10:06:49the concepts if you haven't covered I'll
- 10:06:52suggest watch all the posgress topic and
- 10:06:54you'll see that how much easier it will
- 10:06:56be to complete this particular project
- 10:06:57So what we're going to do right we going
- 10:06:59to have one user table. Okay. Second we
- 10:07:02are going to have our support task table
- 10:07:04where we are going to store our support
- 10:07:06related task and then we are going to
- 10:07:08have our banners table. Banner means
- 10:07:10where we are going to do our upload or
- 10:07:12we are going to upload our images. Okay.
- 10:07:14What your uh this particular image uh
- 10:07:17user table is going to consist. User
- 10:07:19table is going to have your ID. So ID of
- 10:07:21that user. It is going to have the email
- 10:07:24of that user. It's going to have
- 10:07:26password hash. We are going to store
- 10:07:28password has. We are also going to have
- 10:07:31our Google ID, right? Because we also
- 10:07:33going to implement our Google login for
- 10:07:36this. And also we are going to have our
- 10:07:38role. Now role is very important because
- 10:07:40for this one we're going to have user as
- 10:07:42a role or this can be admin. Okay. So
- 10:07:45these two will be role and then we're
- 10:07:47going to have created and updated. So
- 10:07:49created will be when this user is
- 10:07:50created. If we are updating that user
- 10:07:52information when the user will be
- 10:07:54updated. So these all the things we
- 10:07:55going to have in our user table. What
- 10:07:57our support table is going to consist.
- 10:08:00This also will have the ID. So ID will
- 10:08:02be your primary key for this particular
- 10:08:04table. Then you're going to have the
- 10:08:05title of that uh support. Then you're
- 10:08:08going to have a status right. And then
- 10:08:10we are also going to have very very
- 10:08:12important we are going to have the user
- 10:08:13ID of that particular. So means which
- 10:08:17user actually creating this particular
- 10:08:18support. So this will be what? This will
- 10:08:20be a I think we have already covered
- 10:08:22this one. So we have primary key and
- 10:08:24foreign key. So this means this
- 10:08:26particular support task is created by
- 10:08:28this particular user. So that is that is
- 10:08:30an amazing if you completed the previous
- 10:08:32uh postgress section it will be way
- 10:08:34easier to understand each and
- 10:08:35everything. Okay. So these all the
- 10:08:36things we are going to have in this
- 10:08:37support table. Next we are going to have
- 10:08:40a banners. Banners is means where we
- 10:08:42going to store our ID. Now you might be
- 10:08:43thinking why we are doing all of these
- 10:08:46schemas at once. There is a huge reason
- 10:08:49and this is how you actually going to
- 10:08:51implement your own project because this
- 10:08:52is you take this one kind of a system
- 10:08:54design means before implementing the
- 10:08:56project you are actually checking your
- 10:08:58project scope how many tables what will
- 10:09:01be your relation all these things okay
- 10:09:03so that is the reason or else if we can
- 10:09:05also do this on step by step but I think
- 10:09:07this will be much easier okay so for
- 10:09:09banners we are also going to have the
- 10:09:10banner ID we are going to have the image
- 10:09:12URL image URL will be what so we are
- 10:09:15going to store the image to our client
- 10:09:17Cloudinary right so that we are going to
- 10:09:19discuss later and then cloudinary cloud
- 10:09:21is going to give you one particular URL
- 10:09:23and that URL we are going to store in
- 10:09:25our postgress we are also going to store
- 10:09:27one cloudinary unique ID and that
- 10:09:30particular thing we call as cloudinary
- 10:09:32public ID and you'll understand why
- 10:09:34actually we need this one and then we're
- 10:09:35going to have created and updated that.
- 10:09:37So these all the tables we are going to
- 10:09:39create. So first what we will do right
- 10:09:42so let's go to our codebase and let's
- 10:09:44create uh maybe so let's do outside only
- 10:09:47so I'm just going to do migrations okay
- 10:09:49now from this point everything will be
- 10:09:51very very important whatever we are
- 10:09:52going to do so first what will be the
- 10:09:54first file so first we're going to
- 10:09:55create our so let's give this one
- 10:09:57sequentially so I'll do enable and here
- 10:09:59we are going to enable our pg crypto um
- 10:10:02extension so I'll do enable pg so let me
- 10:10:05check the spelling so let's c r y p2 I
- 10:10:08think this is correct pg crypto And then
- 10:10:10we going to do let's do this one as um
- 10:10:12maybe let's do pgc crypto.sql. Okay. Uh
- 10:10:16here what we going to do? So we going to
- 10:10:17do create extension.
- 10:10:21Extension spelling is wrong. I think
- 10:10:23extension will be this if not exist
- 10:10:27right. And here we are going to do what?
- 10:10:29So we do pg crypto.
- 10:10:32If those who have already completed my
- 10:10:34posgress they already know why actually
- 10:10:36we need this one. Okay. So I'm not going
- 10:10:38to explain again. So do create extension
- 10:10:40if not exist PG crypto. Next we're going
- 10:10:43to create our user table. So I'm just
- 10:10:44going to do 002
- 10:10:47and I'll do create underscore I'll do
- 10:10:50users
- 10:10:52table.sql.
- 10:10:54So let's create. So I'm just going to do
- 10:10:56create table and here we're going to do
- 10:10:59users. Now first of all what we going to
- 10:11:02have? So we're going to have our ID. ID
- 10:11:04will be UU ID. And here this will be
- 10:11:06your primary key. And then we are going
- 10:11:09to have a default. And what will be a
- 10:11:11default? Default will be generate random
- 10:11:13ID. Remember we have actually covered
- 10:11:15this one also.
- 10:11:17That's it. Then we are going to have
- 10:11:20email. So email will be var. So let's
- 10:11:23give this one as 255
- 10:11:25not [snorts] null. And this also will be
- 10:11:27unique. Very very important. Next we are
- 10:11:30also going to have a password has. So
- 10:11:32I'm just going to take this one as
- 10:11:33password. Why breaking capital? So do
- 10:11:36password has and this will be text. Now
- 10:11:39very very important here when we are
- 10:11:42going to implement our Google login that
- 10:11:43time we don't need password has because
- 10:11:45Google is going to handle all of these.
- 10:11:47So that is the reason we are keeping
- 10:11:48this one as nullable. Okay. [laughter]
- 10:11:50Next we are also going to have our
- 10:11:52Google ID. So I'm just going to do
- 10:11:54Google id. So I'll do vare 255
- 10:11:58and I'm also going to do unique. Next we
- 10:12:01are going to do a role and I'll do vare
- 10:12:04and let's do 20 and I'll do not null.
- 10:12:09So let's take default. So what will be
- 10:12:11the default? By default we're going to
- 10:12:12create this one as user. So do user
- 10:12:15I'm just going to give a check
- 10:12:18and check let's give this one role. So
- 10:12:21I'll do ro in here I'm [snorts] just
- 10:12:23going to give two. So one will be either
- 10:12:25user or else this will be admin. Okay.
- 10:12:28So either it will be user or it will be
- 10:12:30admin. But by default this will be user.
- 10:12:32Then we are also going to have our
- 10:12:34created at. So I'll do created
- 10:12:37at created at time stamp.
- 10:12:41Uh let's do not null
- 10:12:44and what will be default? Default will
- 10:12:46be now. And the same thing we are going
- 10:12:49to do for updated also.
- 10:12:52So do updated at. So updated at this is
- 10:12:56also same. So this will be our first
- 10:12:58table. So you have ID, email of that
- 10:13:01user, password has, Google ID, RO,
- 10:13:03created at, and updated at. Okay. Next,
- 10:13:05we're going to have our third table,
- 10:13:07which will be our support table. So I'll
- 10:13:09do 003 underscore. So I'm just going to
- 10:13:12do create uh maybe let's do support.
- 10:13:15Let's do task. Now tasks and then I'm
- 10:13:19just going to do tablesql.
- 10:13:22So here we are going to do again. So
- 10:13:24I'll do create table.
- 10:13:26Uh let's do support
- 10:13:29tasks
- 10:13:31and here I'm simply going to do id u uu
- 10:13:35id uh this will be primary key this will
- 10:13:38be default and here we also going to do
- 10:13:41generate random key so go here and copy
- 10:13:44this
- 10:13:47next title where care uh how much maybe
- 10:13:51let's do 150 and let's do not null
- 10:13:56Then we're going to have a status. Now
- 10:13:58status we're going to take a var. So
- 10:14:00this will be 20 not null
- 10:14:04default uh let's keep a couple of status
- 10:14:07maybe default we'll keep open or else we
- 10:14:10are going to give a check property here.
- 10:14:14So you check and here what I'll do right
- 10:14:16I'll do status in uh let's give maybe
- 10:14:20open let's give one in progress in
- 10:14:23underscore
- 10:14:25progress and also I'm just going to give
- 10:14:28result so so you have three status okay
- 10:14:32uh what else very very important so
- 10:14:34you're going to have our foreign key
- 10:14:35which will be our user ID UU id so here
- 10:14:39we this will be not null definitely and
- 10:14:41what you have to use we have to use
- 10:14:42references Remember this we have
- 10:14:44actually discussed clearly in that
- 10:14:46posgress see I already mentioned
- 10:14:48everything everything whatever I'm
- 10:14:51mentioning that I'm that whatever you
- 10:14:53are seeing here right all are covered in
- 10:14:55the previous section okay so now here
- 10:14:58what you have to do is you have to give
- 10:15:00the user because see we are using user
- 10:15:02so user will be what will be your
- 10:15:04primary key primary key is ID now this
- 10:15:07is user ID okay so this will be your ID
- 10:15:10now we are going to do on delete
- 10:15:15cusk
- 10:15:17and then uh let's create created add
- 10:15:20timestamp
- 10:15:22not null and I'll do null and then we're
- 10:15:27going to have updated
- 10:15:29add timestamp not null null
- 10:15:35that's it
- 10:15:37so I think this is what we need and at
- 10:15:40the end we need our banners So now we
- 10:15:42will create one more. So I'll do 004
- 10:15:48create banners
- 10:15:51table.sql.
- 10:15:54So here let's create create table and
- 10:15:57here I'm just going to do banners.
- 10:16:00So first of all let's copy couple of
- 10:16:01things right. So this will be same.
- 10:16:04Then we're going to have our image url.
- 10:16:07This will be text and this also will be
- 10:16:09not null. You can't have empty image
- 10:16:11URL. Then we are going to have
- 10:16:13cloudinary public ID. Don't worry, we
- 10:16:16are going to actually cover all of this.
- 10:16:18Even I'll discuss the flow that how all
- 10:16:19all of this is working. For now, you
- 10:16:22consider this one as a unique ID that
- 10:16:24you need to interact with cloudinary
- 10:16:27something like this. And this will be
- 10:16:28cloudinary public ID and this will be
- 10:16:31text not null. Then we also going to
- 10:16:35take these two
- 10:16:38and then paste it here. That's it. So
- 10:16:41these four tables we need first of all
- 10:16:45not four three tables. One is our users,
- 10:16:48one is our support tasks and one is our
- 10:16:51create banner table. Now what important
- 10:16:53thing you are going to understand right?
- 10:16:55Because if you completed the postgress
- 10:16:57video you understand all of these what
- 10:16:58is happening. the thing that we are
- 10:17:00going to understand in the next part
- 10:17:01which will be how you can create your
- 10:17:03own custom migration and we are going to
- 10:17:05migrate the this particular schema in
- 10:17:07our posgress. So let's see how we are
- 10:17:09going to do that in the next part. All
- 10:17:10right. So now let's understand uh
- 10:17:13properly that how you can create your
- 10:17:14own custom migration script. So for this
- 10:17:17one what I'll do right go to source and
- 10:17:20create one more folder here and this I'm
- 10:17:22going to give this one as scripts and
- 10:17:24let's give this one as migrate.ts. Now
- 10:17:26if you want you can also keep this one
- 10:17:27in DB folder uh like DB then you can
- 10:17:30also do migrate.ts that is fine but we
- 10:17:33will use this only. Let's close
- 10:17:35everything else for now. Also before
- 10:17:38that we need to install one very very
- 10:17:40important package and that is we need to
- 10:17:42use something called PG. So PG package
- 10:17:44we are going to actually use to create
- 10:17:46to create our DB connection pool. Okay.
- 10:17:49Now you consider this one if you are
- 10:17:50familiar with right? So for
- 10:17:52what we usually use you use something
- 10:17:54called mongus right and then you'll do
- 10:17:56mongus doconnect and then you'll give
- 10:17:57your mongus mongodbd connection it will
- 10:18:00connect to your db remember in your code
- 10:18:03you need some medium right so that you
- 10:18:05will be able to query from your db so
- 10:18:07this is where we need one particular
- 10:18:09package and that package called pg okay
- 10:18:11because we're using raw posgress right
- 10:18:13raw pg we are not using any like prisma
- 10:18:17all these things right so if you want
- 10:18:19let me just quickly show you what I mean
- 10:18:20by this Chrome. Okay. So if I go here,
- 10:18:24right? So let's refresh this. So I'll do
- 10:18:26explain
- 10:18:28what's pg package is for posgress.
- 10:18:38So it's a official most popular
- 10:18:40low-level posgress client for your
- 10:18:42nodejs. Right? Postgress client is as I
- 10:18:44mentioned that you'll be able to create
- 10:18:46your connection pooling, right? you'll
- 10:18:47be able to query all these things
- 10:18:49whatever you need to do related to your
- 10:18:51uh posgress you'll be able to do that
- 10:18:54right so this is nothing but your client
- 10:18:56for your posgress in NodeJS similarly
- 10:18:58just just like you have Kafka JS for
- 10:19:00your Kafka client for NodeJS something
- 10:19:02like that so what you're going to do
- 10:19:04that we're going to install PG so copy
- 10:19:06this
- 10:19:08and then we will go here clear this and
- 10:19:11let's install this one first
- 10:19:14okay so this is added what I going to do
- 10:19:17right so first of all now you will
- 10:19:20understand another folder structure
- 10:19:22pattern now I already mentioned that how
- 10:19:24we are going to create the folder
- 10:19:26structure remember that whenever you
- 10:19:28will work with any config related thing
- 10:19:31you are going to keep this one inside
- 10:19:33your config folder when you are going to
- 10:19:35do let's say any DB related thing you
- 10:19:37can also keep this one inside your lip
- 10:19:39folder or the better will be you can
- 10:19:41create DB folder and inside this you can
- 10:19:43create something like that but you can
- 10:19:45also keep this one inside leave folder
- 10:19:47only middleware and all these things
- 10:19:49we're going to discuss later so for now
- 10:19:50what I'll do right so for now let's
- 10:19:52create here only so I'll do db ds and
- 10:19:54then first of all what we have to do so
- 10:19:56first I'm going to create on pool here
- 10:19:58const pool and this will be so first we
- 10:20:02need to do new and we are going to use
- 10:20:04this pool and this pool we are going to
- 10:20:06import from pg package so let's go here
- 10:20:09and then I'm going to import something
- 10:20:11from pg
- 10:20:13and this is going to give me I think we
- 10:20:14have to install the types uh Uh I think
- 10:20:16I always miss this. We need to also
- 10:20:18install the PG types also. Let's install
- 10:20:21this. Okay. So this is going to now I
- 10:20:24think it will not give you error. So
- 10:20:25just import and here what you have to
- 10:20:27pass right you have to pass the
- 10:20:28connection string. Okay. Now before that
- 10:20:31we also need to do one very very
- 10:20:33important point. Uh I'm not sure if you
- 10:20:35can recolct or if you can remember I
- 10:20:37mentioned whenever we are going to add a
- 10:20:38new env we also need to update our
- 10:20:41env.ts file that we have created inside
- 10:20:43config. So here what I'm going to do
- 10:20:45right we are going to create a helper
- 10:20:46function and let's do this one as check
- 10:20:49required env uh maybe let's do variables
- 10:20:53something env values or variables is
- 10:20:56better and here I'm going to check the
- 10:20:59uh string and let's return this one as
- 10:21:01string. So what I'll do right I'm just
- 10:21:03going to check the value and this will
- 10:21:05be process env.
- 10:21:09So this is going to give me value if
- 10:21:11there is not value present. So I'm just
- 10:21:13going to throw a new error. Okay. So
- 10:21:15I'll do throw new error and here we are
- 10:21:18going to do missing env variables for
- 10:21:22this variable for or then pass this key
- 10:21:26that this key is not present. And then
- 10:21:27what you have to do you have to note
- 10:21:29here outside return uh value. That's it.
- 10:21:34And then what you can do right because
- 10:21:35we need to add one more remember. So we
- 10:21:37are going to add our database C model.
- 10:21:39So do database C model. This database C
- 10:21:41model is the one that we have added just
- 10:21:43now this one. So do database C model and
- 10:21:45this will be now you can pass this and
- 10:21:47then here what you have to do you have
- 10:21:48to pass the value. So this will be our
- 10:21:51database model. So this is going to
- 10:21:52check this. Okay. So save this. Now
- 10:21:54we'll go to our db.ts and what you have
- 10:21:57to do right very very simple. Just
- 10:21:59import your env right. So you have envy
- 10:22:02cy that's it. So this is going to create
- 10:22:04your pool or connection pool and this is
- 10:22:07where this pool we can basically use to
- 10:22:09interact with our DV. Just think like
- 10:22:11that. So this is the first thing we have
- 10:22:12to do. Now we need to go to our
- 10:22:14migrate.ts and here you need to
- 10:22:16understand quite a few things and it's
- 10:22:18not that much simple. Okay. Uh so let's
- 10:22:21see how much simpler I can do. So first
- 10:22:24of all what you have to do right when
- 10:22:26you're creating a custom migration right
- 10:22:28because when you are a beginner I think
- 10:22:30this is more than enough also. I think
- 10:22:32that you should understand the custom
- 10:22:34migration first then you should use the
- 10:22:37third party packages because you have
- 10:22:39something called node pg migrate if I'm
- 10:22:41not wrong let me just show you right so
- 10:22:42do node pg migrate you'll see we have
- 10:22:44something like this
- 10:22:48uh see we have this node pg migrator so
- 10:22:51this is going to help you to m to
- 10:22:52migrate this one you can do this one
- 10:22:54right and also see here uh whatever blah
- 10:22:57blah but it's fine so you can pass this
- 10:23:00migrate you can do node p bracket and
- 10:23:01it's going to do the migration but we
- 10:23:03don't want to do this. Okay. What we'll
- 10:23:06do right we are going to migrate custom
- 10:23:08way. So how we are going to do that?
- 10:23:09First of all when you're going to do
- 10:23:11migration you need to understand where
- 10:23:12your migration folder uh stays right. So
- 10:23:15here now I'm going to pause this video
- 10:23:18and you have to understand that whether
- 10:23:20you can replay all the concept that we
- 10:23:21have learned so far. Remember in the
- 10:23:24path module in the core module you
- 10:23:25understood that how you're going to join
- 10:23:27your path how you're going to get your
- 10:23:29current working directory. remember so
- 10:23:31that will be your first step. So I'm
- 10:23:33just going to do con migration
- 10:23:35directory and this will be equal to your
- 10:23:38path. You're going to import from path
- 10:23:40node path dot join and you're going to
- 10:23:43get process dot cwd
- 10:23:47remember and then what you have to do
- 10:23:48you have to do migration. So this is
- 10:23:50going to give you the current working
- 10:23:52directory and then you have to go to
- 10:23:53migration. So here this is going to give
- 10:23:55you what? So this will give you your
- 10:23:57migration directory. So now this
- 10:23:59particular script we will understand
- 10:24:00okay this is where all my table stays.
- 10:24:04Okay
- 10:24:05next what we going to do right we are
- 10:24:07also going to create one particular
- 10:24:09table this migration table is going to
- 10:24:11track that which all file we have
- 10:24:14already migrated. So for example we now
- 10:24:17have three table consider later let's
- 10:24:19say we have added seven more tables. So
- 10:24:21it this particular table will understand
- 10:24:24that okay these three tables we have
- 10:24:26already migrated. So these three you
- 10:24:28don't have to do anything. Next seven
- 10:24:30new files we have to migrate. So for
- 10:24:33this one what we're going to do we're
- 10:24:34going to create a new create migration
- 10:24:39create migrations table. Okay. So this
- 10:24:41is what we're going to do the tracking
- 10:24:43now. So we can directly just use raqql
- 10:24:46here. So create table if not exist.
- 10:24:50Right. And I'm going to give migrations.
- 10:24:52Right. This is the name. And inside this
- 10:24:54what we're going to do. So I'm just
- 10:24:55going to use ID. Let's give this one as
- 10:24:58serial. This will be my primary key. As
- 10:25:00I already mentioned, see I've done all
- 10:25:02of these. You might be thinking how I'm
- 10:25:04writing first or this is first. You can
- 10:25:06pause or you can rewatch the same thing.
- 10:25:09I've done this all these things 100
- 10:25:11times. Once you'll do right, you'll
- 10:25:13understand all of these is nothing. It's
- 10:25:14very very simple. Okay. So here we're
- 10:25:16going to take name and I'm just going to
- 10:25:18do V care and I'll do 255
- 10:25:22not null and this name will be unique.
- 10:25:24Okay. So this name will be me and I'm
- 10:25:26also going to track that when this
- 10:25:28particular table is already migrated
- 10:25:30means when this is already executed. So
- 10:25:32do executed at. So we'll take this one
- 10:25:35as time stamp not null and let's take a
- 10:25:39default value and default will be now.
- 10:25:41I'm not sure if there is a spelling
- 10:25:42mistake. If I'm doing a spelling mistake
- 10:25:44don't blindly write. Just do if there is
- 10:25:46any mistake right just do it properly.
- 10:25:49But I think it's correct only. So create
- 10:25:51table if not exist migration ID name
- 10:25:53executed. That's it. So now what we're
- 10:25:56going to do right. So first of all let's
- 10:25:57do one thing. Let's create a function.
- 10:25:59One by one we need to do. So I'm just
- 10:26:00going to create async function. And here
- 10:26:04I'll do migrate. This one is going to
- 10:26:07return a promise of void. So first of
- 10:26:12all what I need to do? So first I need
- 10:26:15to create our uh migration table first
- 10:26:17because we need to create this table
- 10:26:19right. Then only we'll be able to track
- 10:26:20that which all migration we have already
- 10:26:22done. How you can do that? So first
- 10:26:24you're going to do like this. First I'll
- 10:26:26do a so you pull this just now created.
- 10:26:29So you can do query and then you're
- 10:26:31going to pass this create migration
- 10:26:32table SQL. Just now we have created.
- 10:26:35Once we'll do that now we have to find
- 10:26:39all the files inside this particular
- 10:26:41folder that are not yet executed. So
- 10:26:44that means if we can understand okay
- 10:26:46let's say this particular table is not
- 10:26:47executed. So we need to find this
- 10:26:49particular file. Okay. So that means
- 10:26:51what you're going to do right? So I'm
- 10:26:52just going to do con. Let's do executed
- 10:26:55and I'm just going to do new. So I'll
- 10:26:56take a set just just normal concept
- 10:27:00JavaScript concept. So I'll do set and
- 10:27:02then I'm just going to get all my
- 10:27:03executed migration that are already
- 10:27:05executed. So I'll do get executed
- 10:27:08migrations. You're getting what we are
- 10:27:10doing here. See everything we are doing
- 10:27:12I'm doing in that particular sequence so
- 10:27:14that it will be easier. So this get
- 10:27:16executed migration is going to give me
- 10:27:18all the migration that I've already
- 10:27:19done. So I'm simply going to do async
- 10:27:22function get executed migration. Uh
- 10:27:25let's do a promise and maybe let's
- 10:27:27return a string. So I'll do a string and
- 10:27:31of array. And what I'll do right so I'm
- 10:27:34just going to do con result.
- 10:27:37So this will be a so dot query. And here
- 10:27:41what we can do right we can also create
- 10:27:42a type. So just create a type directly
- 10:27:44here. And I'm just going to do migration
- 10:27:47row. And this will be so I'm going to
- 10:27:50take the name which will be string
- 10:27:52because I return string. So here I can
- 10:27:54pass this type. So I do migration log.
- 10:27:57Now what I'm going to do right so I'm
- 10:27:59going to actually uh query. So how we
- 10:28:01going to query? You have to write is SQL
- 10:28:04query. So I'll do select name. We
- 10:28:08already have name here. See we have name
- 10:28:10here. So select name from what is the
- 10:28:14table name. So table name is migration.
- 10:28:15So we'll do select name from migration
- 10:28:18and I'll do order by and order by name.
- 10:28:21Very very simple. We are not doing any
- 10:28:23like very complicated thing. We're
- 10:28:25simply getting the name from the
- 10:28:26migration table. And then what we'll do
- 10:28:28right it's going to give you a row. So
- 10:28:30I'll do result dot rows. So we're going
- 10:28:33to get rows dot map. So we're going to
- 10:28:35get current row which will be our
- 10:28:37migration row. This will this will only
- 10:28:39have name right because we're getting
- 10:28:41name here. And then what we're going to
- 10:28:43do simply going to return the row name.
- 10:28:46So this is going to already give me
- 10:28:48let's consider this that this migration
- 10:28:50table that you created already have this
- 10:28:52create user table. So that means by this
- 10:28:54particular function we'll understand
- 10:28:56that okay this particular table is
- 10:28:57already executed. So that will be our
- 10:28:59second step. Now if we found that okay
- 10:29:02these are executed. So we'll be able to
- 10:29:04see which all are pending. So I'll do
- 10:29:06get uh what I'll do right? We need to
- 10:29:09create one more file here. So let's do
- 10:29:11one thing. We also need to get all the
- 10:29:13migration file. So first I'm just going
- 10:29:15to create one more function and I'll do
- 10:29:17get all migration file that I need to
- 10:29:19migrate. So look get migration files. Uh
- 10:29:22and this will be string of array. And
- 10:29:24what I'll do right I'm going to return.
- 10:29:26So I'll import fs. FS I'm going to
- 10:29:28return from node fs. So I'll do import
- 10:29:32fs from node
- 10:29:36fs.
- 10:29:38So what I can do right so fs dot read
- 10:29:43directory sync and here what I'll do
- 10:29:46right I already have my migration
- 10:29:48directory so it will go to this
- 10:29:49particular path so I'm telling that okay
- 10:29:52go to this particular path no not
- 10:29:54migration migration directory then
- 10:29:56you're going to filter so here I'm
- 10:29:59simply going to take the file and this
- 10:30:01will be file dot and if you go here
- 10:30:05every file is ending with sql so what I
- 10:30:08can do right I'm just going to tell that
- 10:30:09okay if the file actually ends with
- 10:30:13SQL so dossql so that means now we'll
- 10:30:16understand okay for now because we
- 10:30:18haven't migrated anything so it will
- 10:30:20understand these 1 2 3 4 files I have
- 10:30:23correct and then what you can do right
- 10:30:24you can just do a sort here you can do
- 10:30:27sort
- 10:30:28so save this so this is going to give
- 10:30:30you all the migration file that you need
- 10:30:32to migrate now what you can do right you
- 10:30:34can just use this get migration files
- 10:30:37dot filter filter
- 10:30:39and this is going to give you the file
- 10:30:41and then you're going to check let's say
- 10:30:43we have four correct out of these two
- 10:30:46are already executed so now pending will
- 10:30:48be what four minus 2 correct so it will
- 10:30:50be two so we can filter it so it's
- 10:30:52simply going to do if this executed dot
- 10:30:54has this particular file means if this
- 10:30:57particular executed don't have this
- 10:31:00particular file that means that is not
- 10:31:01executed yet correct so that is painting
- 10:31:04it's normal JavaScript there is nothing
- 10:31:05related to any complicated thing we are
- 10:31:08doing if this pending.length length if
- 10:31:10this is zero so that means we don't have
- 10:31:12to do anything so we are going to get
- 10:31:14the logger dot info so I'm going to do
- 10:31:17no pending migration
- 10:31:22and then we going to return then we are
- 10:31:25going to do right now we going to
- 10:31:26actually run the migrations consider two
- 10:31:28are already pending so I'm going to do
- 10:31:30for so do con item no not item let's
- 10:31:33take file name of pending and then what
- 10:31:37we're going to do right here we are
- 10:31:38going to create our actual migration
- 10:31:40file. So we'll do run migration
- 10:31:43and here we're going to pass the file
- 10:31:45name. So this migration actually will be
- 10:31:47our main function where we are going to
- 10:31:49run the migration. So we'll go here and
- 10:31:51let's do uh this will be a sync function
- 10:31:56and I'll do run migration. So this is
- 10:31:58going to give me the file name
- 10:32:01which will be string and here this is
- 10:32:03also going to return a promise of void.
- 10:32:06Okay.
- 10:32:08All right. Right. So I'm going to pause
- 10:32:09this. I'm doing recording from long time
- 10:32:11and then we will resume from here. All
- 10:32:13right. So now let's implement the actual
- 10:32:15migration logic. Okay. So first of all
- 10:32:17what we going to do right? So first
- 10:32:18we'll get our uh SQL. So let's take a
- 10:32:22variable SQL. And then we are going to
- 10:32:23first read. So I'll do fs. Sorry this
- 10:32:26will be fs dot and I'll do read file
- 10:32:30sync. And what we're going to do we need
- 10:32:32to do path dot join. And this is where
- 10:32:35our migration directory will come. And
- 10:32:37then we're going to pass our file name.
- 10:32:39Okay. So this file name and then also we
- 10:32:42have to give the type which will be
- 10:32:44UTF8.
- 10:32:46So now what I'm going to do right so
- 10:32:48let's connect to our client. So have it.
- 10:32:51So we have our pool right. So created
- 10:32:54our pool uh and this will be pool dot
- 10:32:56connect.
- 10:32:58And now we are going to start try catch
- 10:33:03uh in the catch error. So here what I'm
- 10:33:06going to do right client and then here
- 10:33:09you can do a query and we're just going
- 10:33:11to pass a roll back here
- 10:33:15and here I'm just going to throw the
- 10:33:17error. So do throw error finally. So
- 10:33:20once everything is done
- 10:33:23so we are simply going to do what? So
- 10:33:25we'll do client dot release.
- 10:33:28Now in the try now first of all let's
- 10:33:30start. So look have it. So do client do
- 10:33:32query and we'll do begin.
- 10:33:37Now we are going to query the SQL. So do
- 10:33:39client do query create query this SQL.
- 10:33:44We'll do a client dot query and then
- 10:33:47we're going to insert. So do insert. So
- 10:33:49we need to insert each and every file
- 10:33:51once uh in this migration table so that
- 10:33:54we can keep a track. I think this part
- 10:33:55we've already discussed right. Right. So
- 10:33:57we going to do insert into
- 10:34:01insert into migrations
- 10:34:04and then we're going to do what? We're
- 10:34:06going to pass the name
- 10:34:08and here we're going to pass the values.
- 10:34:10So what will be values? So values here
- 10:34:11I'm just going to give. So the values
- 10:34:14will be this uh file name. Okay. So here
- 10:34:17you're going to pass the file name. So
- 10:34:19now this every time we're going to run a
- 10:34:21migration, this is going to actually
- 10:34:23post that particular file name into our
- 10:34:24migration table. Okay. And then we're
- 10:34:27going to just do commit. So do a client
- 10:34:29do query and we'll do commit.
- 10:34:33That's it. And let's take a logger do
- 10:34:36info. And I'm simply going to do
- 10:34:39migration
- 10:34:40completed.
- 10:34:44And let's pass the file name. Okay. So
- 10:34:46this is what we going to do. So this is
- 10:34:48going to actually run your migration.
- 10:34:50Right. So what we doing? So first we are
- 10:34:52checking what all are already executed,
- 10:34:54what all are pending. If nothing is
- 10:34:55pending, we don't have any pending
- 10:34:57migration. And then we are going to just
- 10:34:59uh check get or pending count for every
- 10:35:02file. We are going to run the migration
- 10:35:04and at the end what we can do right
- 10:35:06we'll do logger do info and I'm simply
- 10:35:09going to do all migrations completed
- 10:35:14and then here we're going to run the
- 10:35:16migration. So this migrate this one. So
- 10:35:18this is going to what return a promise
- 10:35:20right? So you can take a catch here. So
- 10:35:22I'll do a error. So there is any error.
- 10:35:26So what I'm going to do is I'll do
- 10:35:27logger dot error. And here we can pass a
- 10:35:31error and let's pass this error here.
- 10:35:33Okay. And also let's do a migration
- 10:35:36field something like this. Migrations
- 10:35:39field.
- 10:35:41And then what we can do we can take a
- 10:35:42process.exit and we can do a
- 10:35:45process.exit of one. This also we have
- 10:35:47already discussed. And let's take a
- 10:35:49finally. And the finally we just have to
- 10:35:53call this pool
- 10:35:55dot end.
- 10:35:58That's it. So this will be what actually
- 10:36:01we are going to uh implement our
- 10:36:03migration script. This is completely
- 10:36:05manual, right? We are not using any
- 10:36:06third party packages or anything. And
- 10:36:08I'll go to package.json. And then here
- 10:36:10we can add one more script. So I'll do
- 10:36:12migrate and then I'm simply going to do
- 10:36:15tsx source/
- 10:36:18So we have a scripts, right?
- 10:36:19webcripts/migrate.
- 10:36:23DS
- 10:36:24also if you want you can add some more
- 10:36:26uh script here. For example, let's say
- 10:36:28you wanted to add a docker up. So you
- 10:36:30can simply do docker compose up hyphen
- 10:36:33d. You want to take a docker down. So
- 10:36:36you can do do a docker down and I'm just
- 10:36:38going to do docker compose down. So we
- 10:36:42can just do all of these. So save this.
- 10:36:44Now let's see what is happening. Right.
- 10:36:46So we are going to do all right. So we
- 10:36:48run simply npm run migrate. Okay. So
- 10:36:50let's do this. I'll do npm run migrate.
- 10:36:55Okay. Getting some error. Syntax error
- 10:36:58near end. All right.
- 10:37:08Oh, we have to pass default here. We
- 10:37:11have to pass default here. And also
- 10:37:14let's give semicolon. And also I have
- 10:37:16checked one more issue is there. So this
- 10:37:17will be gen random id okay not get
- 10:37:21this is also wrong
- 10:37:24uh default we are using here so you need
- 10:37:26to use default let's see here okay see
- 10:37:29here also you're not using default so
- 10:37:30you have to use a default default value
- 10:37:36and also this will be len so now let's
- 10:37:40run it once again
- 10:37:42clear this
- 10:37:45fantastic so my is Now completed
- 10:37:47migration completed all migrations are
- 10:37:49completed. So that means now this is
- 10:37:52migrated this particular
- 10:37:55schemas and whatever table we have this
- 10:37:56is migrated to our posgress. So with
- 10:37:59this what we have understood till this
- 10:38:01part that you can also implement your
- 10:38:03own script or even create your own
- 10:38:05script for migration. You don't have to
- 10:38:07always use your third party packages
- 10:38:09right but again all these whatever you
- 10:38:12have written right these has to be
- 10:38:13precise it means you need to know all of
- 10:38:15these or if you don't have any idea
- 10:38:17right what why I'm mentioning all this
- 10:38:18one because don't learn any tech blindly
- 10:38:21for example let's say you don't have
- 10:38:23that much patience that okay you haven't
- 10:38:24watched the postgress part in the
- 10:38:25previous section and you directly
- 10:38:27watching this video some of the things
- 10:38:29will be very tricky for example let's
- 10:38:31say here only I made this mistake right
- 10:38:34I missed this one while writing but I I
- 10:38:36can guarantee some of you already was
- 10:38:38that party already catch it. Okay, there
- 10:38:40is default part is missing. you're
- 10:38:42getting what I'm talking about or maybe
- 10:38:44let's say this one also gen random ID
- 10:38:46right for example let's say if I wanted
- 10:38:48to ask you okay what actually this gen
- 10:38:50random ID is let me just refresh this so
- 10:38:53I'll do uh
- 10:38:55random ID in SQL
- 10:38:59now these whatever line that we are
- 10:39:01writing so gen random ID is going to
- 10:39:03build in postgress function generate u
- 10:39:05ID right here this also can be interview
- 10:39:08question now if you don't have any idea
- 10:39:09and blindly writing all of these then it
- 10:39:11will be difficult for you to understand.
- 10:39:13So that is the reason first prerequisite
- 10:39:15will be posgress and then we also
- 10:39:18understood that we are using docker
- 10:39:20compose. We created one simple uh
- 10:39:22service where we are using posgress.
- 10:39:24This is our env variables. This is the
- 10:39:26port where we are running. We have added
- 10:39:28this one in our env right. We have our
- 10:39:29database URL. We added the config uh
- 10:39:32this database URL. We have created our
- 10:39:34DB pool. And this is what we are going
- 10:39:36to use to query uh everything. So with
- 10:39:39this our base structure is now
- 10:39:40completed. What will be next step? Next
- 10:39:43will be we are going to actually start
- 10:39:44our CRUD and authentication. Each and
- 10:39:46every flow we are uh we are going to
- 10:39:48understand in very details. So let's get
- 10:39:51started. And also for this particular
- 10:39:52project we are going to use our own uh
- 10:39:55error handling. We are not going to use
- 10:39:57any third party packages like zord and
- 10:39:58everything that I will keep that
- 10:40:00particular part for our microservices
- 10:40:02video. So with this now let's move on to
- 10:40:04the next one. All right. So first let's
- 10:40:06create one more folder here and here I'm
- 10:40:08just going to give this one as errors.
- 10:40:10Okay. And inside this, let's create one
- 10:40:12app error. DS. So this is where we're
- 10:40:16going to actually handle our error. So
- 10:40:18I'm just going to do export class. This
- 10:40:21will be simply app error. And this is
- 10:40:23going to extend our error. Okay, error
- 10:40:26interface. What we going to do? We are
- 10:40:28going to have a status code. And this
- 10:40:30status code will be number. And now I'll
- 10:40:33simply take a constructor. And here what
- 10:40:36we going to do, right? We're going to
- 10:40:37take our status code which will be
- 10:40:39number. And also there is a possibility
- 10:40:41that we are also going to send send one
- 10:40:43uh message whenever it's needed. So now
- 10:40:46we going to p take a super pass the
- 10:40:48message and then I'm simply going to do
- 10:40:50this dot status code will be equal to
- 10:40:52the status code that we have. That's it.
- 10:40:54And we don't have to do anything where
- 10:40:56we are going to use this particular uh
- 10:40:58error class. So we're going to use this
- 10:41:00one in our error handler. Remember we
- 10:41:02have created this error handler. This is
- 10:41:04what we are going to do. Now if you
- 10:41:05notice what we are simply doing, we have
- 10:41:07error here. request response blah blah
- 10:41:09whatever and at the end then we are
- 10:41:10sending this response back. What we need
- 10:41:12to check that if this particular error
- 10:41:15that we have if this is a instance of
- 10:41:18this app error. So in that case what we
- 10:41:20can do right we don't have to pass
- 10:41:22always some success false and message uh
- 10:41:25whatever it is why in this case what we
- 10:41:27can do right we can directly get our
- 10:41:29error dot status code also okay and also
- 10:41:32we going to get our message so what we
- 10:41:34going to do right simply to use this I'm
- 10:41:37simply going to do if this error
- 10:41:38whatever error that we have if this
- 10:41:41error is instance of the app error that
- 10:41:43we have created our custom error so in
- 10:41:45that case I'm simply going to do race
- 10:41:47dot status and this will error dot
- 10:41:50status code. So you can see that you're
- 10:41:51getting error dot status code dot JSON
- 10:41:54and then you can do a success will be
- 10:41:57false
- 10:42:02and we are going to pass message which
- 10:42:05will be error dot message right so now
- 10:42:08this will be a custom way that we are
- 10:42:10handling the error okay so now what you
- 10:42:12can do that whenever we need to use this
- 10:42:14let's say I'm going to pass a new app
- 10:42:15error and then we're going to pass a
- 10:42:16status code correct and the message so
- 10:42:19it's going to let's say something
- 10:42:20breaks. So it will check okay the
- 10:42:22currently the error is the instance of
- 10:42:24app. So it's going to send this one and
- 10:42:26this error handler we are actually using
- 10:42:28in our app. DS okay so this is how it's
- 10:42:31going to work. So this is the first
- 10:42:32thing we have to do. Now what will be
- 10:42:34the second step? Second step will be now
- 10:42:37before we will start writing let's
- 10:42:39understand that how that actual flow
- 10:42:41will work. It will be easier. Uh let's
- 10:42:43use this one. See first of all we have
- 10:42:45to understand understand that okay how
- 10:42:47this flow will work. So let's say you
- 10:42:48have a register and you have a login
- 10:42:51right. So how register will work? So
- 10:42:53let's say you have a email right
- 10:42:54consider you have a UI you have a email
- 10:42:57you have a password correct? So this is
- 10:43:00your password and then you have one more
- 10:43:02button and this is like continue with
- 10:43:06Google. Now you'll see that this
- 10:43:08particular thing you are going to see so
- 10:43:10many places. So that means we are giving
- 10:43:13user options that either you can
- 10:43:14register using email and password or so
- 10:43:18here I'm going to do or you can register
- 10:43:20directly using Google. Now this Google
- 10:43:22part we are not going to do now okay and
- 10:43:24there is a reason for this because this
- 10:43:26will be little tricky because you need
- 10:43:28to know the concept of all three direct
- 10:43:30call back and all those things this one
- 10:43:32we're going to do later. Okay first we
- 10:43:34have to understand that how this flow
- 10:43:37will work. So when we are going to do
- 10:43:38email and password so user will enter a
- 10:43:41email. So first of all we need to check
- 10:43:42if the email format is correct or not.
- 10:43:44If this is a valid email or not. So that
- 10:43:47is the first thing. Second we also going
- 10:43:49to check whether user entered the
- 10:43:51password or not. So that is the very
- 10:43:52basic validation. If user don't enter
- 10:43:56email or password we are going to send a
- 10:43:58error back. Okay. So that is the first
- 10:44:00thing. Second we need to check if the
- 10:44:02email is a valid password. Sorry email
- 10:44:04is a valid email. Second, if the
- 10:44:06password format is valid or not. So
- 10:44:08let's say I need at least six characters
- 10:44:10in the password. But when user is
- 10:44:12giving, user is giving 1 2 3 4 5
- 10:44:15characters. So that means this condition
- 10:44:17is not satisfying. So in that case we
- 10:44:19are also going to throw an error. Now
- 10:44:21once registration is completed, so once
- 10:44:24this is completed, we will not redirect
- 10:44:27the user to our application. Now this is
- 10:44:29actually very important. Why? What we
- 10:44:31are going to do right? Once registration
- 10:44:33is done, we will simply mention user.
- 10:44:35Okay, registration is completed. Okay.
- 10:44:38Now what we have to do? You have to log
- 10:44:40to enter into application. Why? Because
- 10:44:43directly we are not going to do that. Uh
- 10:44:45and now remember one thing, there is so
- 10:44:47many things we can do here. You can
- 10:44:48implement a two-step verification. You
- 10:44:50can send email and know those things.
- 10:44:51But I'm not going to do all of this. I
- 10:44:53already have a separate video for that
- 10:44:55also. If you want you can check all of
- 10:44:56this. I think there is no point of
- 10:44:58adding each and everything in this
- 10:44:59particular project. So if you if you're
- 10:45:02not sure I okay you can go to my channel
- 10:45:05and just search for sangamukhari nodejs
- 10:45:07security and those things and you'll see
- 10:45:08that you are going to get all of these
- 10:45:10okay but what I'm trying to say that
- 10:45:11after register we will not allow the
- 10:45:13user to enter into our application once
- 10:45:16registration is done user needs to login
- 10:45:18and once you're going to do login what
- 10:45:20will happen so user will again give
- 10:45:21email user will again give password if
- 10:45:24user is giving wrong email or wrong
- 10:45:25password we'll simply say that okay your
- 10:45:27email is wrong password is wrong once
- 10:45:30they will give correct credential what
- 10:45:32we're going to do right we're going to
- 10:45:33create a token and this token we call as
- 10:45:36JWT right so this will be our JWT token
- 10:45:39what this JWT token is going to hold
- 10:45:42this token is going to hold this
- 10:45:44particular user information so user
- 10:45:46information what user information so
- 10:45:49here in this using this JWT token we
- 10:45:52will be able to verify whether this user
- 10:45:54is a correct user or not so consider we
- 10:45:56have a user A and user B so the JWT
- 10:46:00information of this user A will be
- 10:46:01different. The JWT token of this user B
- 10:46:04will be different. This user JWT is will
- 10:46:07be holding user A information, this uh
- 10:46:10user BJ JWT is going to hold user B JWT
- 10:46:14information. Right? So that means when
- 10:46:16let's say if user B try to access user A
- 10:46:19account, it's not going to happen.
- 10:46:21Correct? You're getting what I'm talking
- 10:46:22until unless this user know the password
- 10:46:25of this particular user. So that is the
- 10:46:27reason we have something called JWT
- 10:46:29right JWT is going to give our uh
- 10:46:32capability to check whether that
- 10:46:34particular user is a valid user or not.
- 10:46:36So first of all what we're going to do
- 10:46:38is we'll be implementing our register
- 10:46:39flow. Once user will register user will
- 10:46:42go to login. Once user will login then
- 10:46:45we're going to create our JWT token.
- 10:46:48Once they will create the token then
- 10:46:49we're going to create our middleware.
- 10:46:51Now this is where you are actually going
- 10:46:53to implement uh or understand one very
- 10:46:55important concept that is middleware. So
- 10:46:57now what are we going to do right? So
- 10:46:59first let's start with the register
- 10:47:00first and then we'll go to medial
- 10:47:02because this is one of the most
- 10:47:03important thing you have to understand
- 10:47:05right how you can implement uh protect
- 10:47:07routes or how you can protect routes. So
- 10:47:10one will be protection of routes and
- 10:47:11second something called rolebased access
- 10:47:14control. Now this is actually rolebased
- 10:47:16access control. So this is actually very
- 10:47:18important. Okay. So let's uh start with
- 10:47:20the register
- 10:47:22logic in the next part. Okay. So we'll
- 10:47:24start implementing our register and
- 10:47:26login flow. Correct. So first let's go
- 10:47:28here. And now here also you are going to
- 10:47:31understand now how you can create proper
- 10:47:33folder structure practically. Right. We
- 10:47:35will have now more practical code
- 10:47:36implementation rather than only
- 10:47:37concepts. So let's create our first
- 10:47:39folder which will be types. Types will
- 10:47:41be one folder where you're going to keep
- 10:47:43all your TypeScript types. As simple as
- 10:47:45that. In our case because in register
- 10:47:47and login flow we are working with user.
- 10:47:50So we are going to create our first type
- 10:47:52which will be user.ts.
- 10:47:54So now let's close everything else. So
- 10:47:56what this is going to have this is only
- 10:47:58going to have your user type. So we do
- 10:48:00export type and I'm just going to give
- 10:48:02user. So user is going to have your ID
- 10:48:04which will be string. User is going to
- 10:48:06have your email which will be string.
- 10:48:08User will be having your role which will
- 10:48:10be string. And also it's going to have
- 10:48:12your created. Now remember these all we
- 10:48:14already have here, right? say uh id uh
- 10:48:17this id email created at all this. So
- 10:48:20here also we're going to take created
- 10:48:23add and this will be date. Okay. So this
- 10:48:26is the first thing we are going to do.
- 10:48:28Now you have to understand one very
- 10:48:30important point that how the actual
- 10:48:32folder structure works. So need to
- 10:48:33understand this one in three parts.
- 10:48:35Okay. So first one
- 10:48:38or let's do like this. So this is
- 10:48:41consider this is your root file. This is
- 10:48:43your root file. I'm going to keep our
- 10:48:45root. What I mean by root file? Root
- 10:48:47file is your app.ts. Consider like this
- 10:48:49this one. Correct?
- 10:48:51Now this root file is going to hold all
- 10:48:55the information of your code. Correct?
- 10:48:57What you're going to do right? Normally
- 10:48:59we usually divide this one in three main
- 10:49:01files. One file we called as repository.
- 10:49:04So I'm going to call this one as repo.
- 10:49:06Second one we call as our services. So
- 10:49:10services is our where business logic
- 10:49:12we're going to keep. And third will be
- 10:49:13our actual routes and this routes file.
- 10:49:16So this will be your routes and then
- 10:49:18there will be your root routes file.
- 10:49:20Root route root routes file. Now this
- 10:49:25might be little tricky. So let's
- 10:49:26understand first of all what actually
- 10:49:27repository is. Repository is going to
- 10:49:29hold or repository will be responsible
- 10:49:31for writing all your DB related logic.
- 10:49:35DB related logic means whenever you need
- 10:49:37to interact with your post DB. So in
- 10:49:40that case you are going to write all
- 10:49:42those logic in your repo. You are not
- 10:49:45going to write your service logic in
- 10:49:47your repository file. Okay. Repository
- 10:49:49means you will only interact with your
- 10:49:51DB. What your service logic? Service
- 10:49:53logic is going to actually hold your
- 10:49:54business logic. For example, let's say
- 10:49:56you want to implement some validation.
- 10:49:58You want to implement some extra logic.
- 10:50:00You are using some helper function.
- 10:50:01You're creating your JWT token. All
- 10:50:03these things okay that is going to hold
- 10:50:05your service logic. And this is not
- 10:50:07about only register or login flow. This
- 10:50:09will be for all the logic that you're
- 10:50:11going to write. And what your route
- 10:50:13information is going to hold? Route
- 10:50:14information is going to hold your actual
- 10:50:16routes based on that particular feature.
- 10:50:19What I mean by based on that particular
- 10:50:21feature, let's say you have a O feature
- 10:50:23and you have a CRUD feature. So there
- 10:50:25will be two file. One route file will be
- 10:50:27for O, one route file will be for uh
- 10:50:30CRUD. And then these two routes you are
- 10:50:33going to combine. So these two routes
- 10:50:35you're going to combine in your root
- 10:50:37route file. Correct? So root route I
- 10:50:39think we have already created. Root
- 10:50:41route means this route. So this is where
- 10:50:43you are going to have all your routes
- 10:50:45and this route file you're going to use
- 10:50:47in your main entry file. Right? So this
- 10:50:50will be the flow. So that means now in
- 10:50:52our case what you have to create we have
- 10:50:54to create couple of folders. So first
- 10:50:56I'm going to create repository.
- 10:51:00Then we're going to create services.
- 10:51:03Then we have routes which we already
- 10:51:06have. Correct? So first let's start with
- 10:51:08our uh repository. So here I'll go to
- 10:51:11repository.
- 10:51:13Where is repository? So here I'm just
- 10:51:15going to create user dot. So I'll do
- 10:51:18repository
- 10:51:20dot ds. Okay. So this is where we are
- 10:51:24going to actually have all your DB logic
- 10:51:26in this particular file. Then you're
- 10:51:28going to have a services. Services means
- 10:51:30where we are going to add our actual
- 10:51:32services logic. Okay. All right. Sorry I
- 10:51:34need to pause because of some noise.
- 10:51:35Okay. So what I was talking about right
- 10:51:37so in the services we're going to write
- 10:51:38actual business logic. Okay. So first of
- 10:51:40all let's create our o service. So I'm
- 10:51:42just going to do o dot service.
- 10:51:45Yes. Now normally in my other videos
- 10:51:46what I do right I usually come from like
- 10:51:49a [snorts] reverse way means I'll
- 10:51:51implement the repository and services
- 10:51:52and the then routes but let's do in this
- 10:51:55particular video I'm going to do much
- 10:51:57better way. So what are we going to do
- 10:51:58right? You are going to understand the
- 10:51:59these concepts uh very very clearly. So
- 10:52:02now so we already have used the
- 10:52:03repository o service and now we'll go to
- 10:52:06our routes and let's create our o routes
- 10:52:08and you'll see that how much easier it
- 10:52:10will be for you to understand all this.
- 10:52:13So what you're going to do right first
- 10:52:14of all let's import routes okay so sorry
- 10:52:16router
- 10:52:18so I'm going to import router I think
- 10:52:20these we have already discussed and now
- 10:52:22I'll do export const
- 10:52:25router and this will be about this
- 10:52:28router make sense what you're going to
- 10:52:31do right so if you go here right you see
- 10:52:33this part this particular file is going
- 10:52:36to hold all your routes I already
- 10:52:38mentioned so what you're going to do
- 10:52:40first of all let's do reverse way so I'm
- 10:52:42simply going to do here okay app router
- 10:52:45sorry API router dot use and then I'm
- 10:52:47simply going to tell that whenever I
- 10:52:49will go to this o route so this will be
- 10:52:52my parent route okay / will be the
- 10:52:54parent route for all our o related
- 10:52:57routes now this might be little tricky
- 10:52:58so what I mean by this let's say in your
- 10:53:00o routes file you have five routes one
- 10:53:03is /register one is / login right like
- 10:53:08this so what will happen when I'm using
- 10:53:10/ so now this will become/ / o/register/
- 10:53:14o/ login/ o/me whatever right so this is
- 10:53:18my parent route this is my parent route
- 10:53:20for my o and this is where I'm going to
- 10:53:23use my o router now for now this is not
- 10:53:25going to work okay even if I save this
- 10:53:27this is going to give me error uh that
- 10:53:29okay something is breaking and whatever
- 10:53:31that's fine but now let's go here so
- 10:53:33first of all we created our o router we
- 10:53:35have plugged this o router in our root
- 10:53:37router that's it now what I have to do
- 10:53:39right so in this o router first we have
- 10:53:41to decide that how many routes we need
- 10:53:42to have. So for now let's start with
- 10:53:44register only first. Okay, we'll do one
- 10:53:46at a time. So do all route dot post. So
- 10:53:50now this will be my register. So this
- 10:53:51will be my register route. This will be
- 10:53:53my path that I need to give. And then
- 10:53:55I'm going to have my async. So I'll do
- 10:53:58request response. And here I'm simply
- 10:54:01going to have next.
- 10:54:05And inside this now first of all you
- 10:54:07need to do always try and catch. The
- 10:54:10first thing what you have to understand
- 10:54:12here. So I'm just going to take catch
- 10:54:14error and then whenever there is error
- 10:54:16what I'm going to do right I'm going to
- 10:54:17call this next and I'm going to pass
- 10:54:20this error to our next middleware. Okay
- 10:54:23so this is always going to check it
- 10:54:25because we are already using it uh using
- 10:54:27this in our app.ts TS right you'll see
- 10:54:29that we have this error handler and all
- 10:54:31of these you remember that part we have
- 10:54:32checked that even if you try to send any
- 10:54:34route which is not present so this not
- 10:54:37found is always going to catch it and
- 10:54:39it's going to understood okay this route
- 10:54:40is not present so I need to return a 404
- 10:54:42something like this so this will be the
- 10:54:44first thing in this try what I need to
- 10:54:46do now here we have to understood uh
- 10:54:49sorry understand some of the very
- 10:54:50important thing that when user is going
- 10:54:52to send your email right and password so
- 10:54:57this will be where this will be in your
- 10:54:59UI correct UI means where user will be
- 10:55:01actually typing these values but in your
- 10:55:03back end how we going to get these
- 10:55:05values so this is where we have
- 10:55:06something called request so the request
- 10:55:09is going to give you this body so
- 10:55:11request do body so this request do body
- 10:55:14is going to hold your email and password
- 10:55:16information that you are going to
- 10:55:18receive here so I'm just going to do con
- 10:55:20and this will be my request do body so
- 10:55:24you'll see that we have this request do
- 10:55:25body this is going to have all your
- 10:55:27information that you're going to pass
- 10:55:28because you're using a post request and
- 10:55:30this is what I'm going to get email and
- 10:55:32password right because for now we have
- 10:55:34only email and password now let's say
- 10:55:35you have name also so in this case you
- 10:55:37also take name let's say you have age
- 10:55:39also you're going to take this you have
- 10:55:40address also so whatever you are going
- 10:55:42to send from your UI side you're going
- 10:55:44to get it from your request do body so
- 10:55:46for now we have only email and password
- 10:55:49that's it and now what we going to do
- 10:55:50right we are not going to write so do
- 10:55:53not or not writing
- 10:55:56service logic here right service logic
- 10:55:59here so service logic will be in service
- 10:56:02logic in service file right so service
- 10:56:04file and now this is where I'm going to
- 10:56:06create so I'm going to create our first
- 10:56:08service and this will be my register
- 10:56:11user and this is where I'm going to pass
- 10:56:13this email and the password okay and
- 10:56:16then once this service logic is
- 10:56:18completed what we need to do we need to
- 10:56:20send back the response back to our UI
- 10:56:22right because if you don't send back how
- 10:56:24your user is going to understand that
- 10:56:27okay if my register is successful or
- 10:56:29something goes wrong so that means
- 10:56:30always you need to send your status back
- 10:56:32so what will be status because you are
- 10:56:34creating something remember you are
- 10:56:36creating user so in this case you can't
- 10:56:37use two 200 you have to use 201
- 10:56:41status code we usually use for create
- 10:56:43okay and then you'll do JSON if you just
- 10:56:46do write see 200 versus 2010
- 10:56:55okay means request succeeded two 201
- 10:56:57created means and end request succeeded
- 10:56:59and brand new resource was successfully
- 10:57:01generated on the server. So in this case
- 10:57:02we're creating a new user correct so
- 10:57:05it's a create so that's why you're using
- 10:57:07201 and this will be JSON so we going to
- 10:57:10pass a success we'll keep this one very
- 10:57:12simple main idea is to so that every
- 10:57:15student every level of student can
- 10:57:17understand what we are doing here so
- 10:57:18it's simply going to do registration
- 10:57:20successful and then we are going to do
- 10:57:23like this see please login to continue
- 10:57:25that means you can't get into our
- 10:57:27application yet you have to login then
- 10:57:29only you can continue so this is Done.
- 10:57:31Save this. Now this is going to give you
- 10:57:33error. That is fine. Now we'll go to our
- 10:57:36service file. So go to service file and
- 10:57:38this is where we're going to create our
- 10:57:39first service. So what will be our first
- 10:57:41service? I'm just going to do export
- 10:57:42async function and we're going to use
- 10:57:45this register. See I'm going to repeat
- 10:57:47one thing only the register user. I'm
- 10:57:49doing very very slowly from the next
- 10:57:51time I'm not going to explain each and
- 10:57:52every line. This video will be already
- 10:57:54very long, right? Because some of the
- 10:57:57things will be repeated. You'll see
- 10:57:59that. So we have email and this will be
- 10:58:01string and then we have a password this
- 10:58:04also will be a string right because
- 10:58:06these two we are receiving correct where
- 10:58:07is that we're receiving email and
- 10:58:10password what's this is going to return
- 10:58:12so we're going to return a promise and
- 10:58:14this will void now first of all what we
- 10:58:17going to do we going to check simple
- 10:58:18validation do if email is not present or
- 10:58:21if password is not present now you see
- 10:58:24what we're doing throwing new app error
- 10:58:27this is very very important because Now
- 10:58:29everything you're creating manually
- 10:58:31you're not using any third party
- 10:58:32package. So what I'm doing I'm simply
- 10:58:34using this app error here. So I'm
- 10:58:36calling this app error and similarly I'm
- 10:58:37telling that okay I have to pass a
- 10:58:39status code. So my status code will be
- 10:58:41400 because it's something goes wrong
- 10:58:43and then I'm going to pass message that
- 10:58:45either email and password are required.
- 10:58:47Email and password are required. Now
- 10:58:50what will happen? This app error is
- 10:58:51going to catch it and then in your error
- 10:58:54handler you are simply telling that if
- 10:58:55this error is a instance of app error.
- 10:58:57So you're going to return this. you're
- 10:58:58getting what I'm doing. So this message
- 10:59:00error dot message is nothing but this
- 10:59:02message that we are going to receive. So
- 10:59:04this is the first thing then I'm simply
- 10:59:06going to check if my password dot length
- 10:59:09right if this is less than mean password
- 10:59:12length. So for example let's say I'm
- 10:59:14just going to give mean password length
- 10:59:15as six. Okay. Now here I'm going to give
- 10:59:17a homework. Now this is not a homework
- 10:59:19but simpler. What you can do right
- 10:59:22create one more folder
- 10:59:25called constants
- 10:59:28and keep this six value 61 six in a
- 10:59:33constant and use this here you're
- 10:59:37getting what I'm talking about so that
- 10:59:38means you'll create one more folder here
- 10:59:40constant keep this one in a constant
- 10:59:42like maybe minimum password length
- 10:59:44something like this and then use it here
- 10:59:46so that you'll also uh you will be also
- 10:59:49able to understand that okay what we are
- 10:59:50doing and when next time you're going to
- 10:59:53create project you'll know that okay
- 10:59:54what each and every folder means so here
- 10:59:57also I'm going to throw new app error so
- 11:00:00I'll give a 400 and I'll simply do
- 11:00:02password
- 11:00:04must be at least six characters long
- 11:00:11that's it
- 11:00:13going to do right I'm going to do con
- 11:00:15and I'm just going to do normalize email
- 11:00:17so I'm just going to do email whatever
- 11:00:19email and I'm just going to do two lower
- 11:00:21case. So two lower case and then I'm
- 11:00:23going to do a trim here. This is normal
- 11:00:25concept. Now what we have to do first of
- 11:00:29all you'll see that when you are
- 11:00:31registering a particular user let's say
- 11:00:32one user email is John at the
- 11:00:36redgmail.com. This user is already
- 11:00:38registered. Okay. Next time let's say
- 11:00:41there is one more user is coming B and
- 11:00:43this user is using same email this
- 11:00:45Johntheregmail.com. So what will happen?
- 11:00:48should not happen right because there
- 11:00:49will be only one user with unique email.
- 11:00:52Next time if you allow user to register
- 11:00:55with the same email so there will be two
- 11:00:57user with same email and you don't even
- 11:00:59know whether that is a user A or that is
- 11:01:01a user B. Right? So that means first of
- 11:01:04all we we have to check that if this
- 11:01:06current email that whatever user is
- 11:01:08sending if this email is already present
- 11:01:10in our DB or not. So that means you have
- 11:01:13to find the user if it's present if it's
- 11:01:17already present in DB or not. If it is
- 11:01:22present if yes then we'll not allow we
- 11:01:24will not allow to register with same
- 11:01:29email again. That's it. Right? That's
- 11:01:32it. So this is actually very very
- 11:01:34important especially for
- 11:01:37um beginners. Right? So this we have to
- 11:01:39know. So first we need to know here
- 11:01:43existing user correct. So we do existing
- 11:01:45user and now I'm just going to do a and
- 11:01:47this is where now we have to use our
- 11:01:50repository because how you're going to
- 11:01:51understand if this is existing user you
- 11:01:53need to check in your DB correct so that
- 11:01:55means I what I mentioned that whenever
- 11:01:56you need to do something with DB so that
- 11:01:59means you have to write that particular
- 11:02:02logic in your user repository means in
- 11:02:04your repository file. So first of all
- 11:02:06what are we going to do right? So let's
- 11:02:08go to our types and I'm just going to do
- 11:02:11export type and I'm just going to do uh
- 11:02:15DB DB user row. So DB user row is
- 11:02:20nothing but in which format or in which
- 11:02:23format you are actually storing the user
- 11:02:25in your DB. So this will be same only
- 11:02:27with this. Okay. So this DB user row is
- 11:02:30nothing but what all properties that you
- 11:02:32have in your DB. Okay. So we'll go here
- 11:02:35and also we need one more type here that
- 11:02:37we are going to use later. So I'm just
- 11:02:38going to do export type and let's do DB
- 11:02:42user with password
- 11:02:47and this will be your DB user and and
- 11:02:50here we are going to have what when
- 11:02:52you're going to have password so you're
- 11:02:54also going to have this password has
- 11:02:55this we going to use later. So this will
- 11:02:57be either string or null but for now I
- 11:02:59think this is fine. So now what you can
- 11:03:01do that we'll go to our user repository
- 11:03:03and this is where I'm just going to
- 11:03:04first check that find
- 11:03:07user by email because in our o service
- 11:03:10we need to check that if this user is
- 11:03:12present or not you're getting so first
- 11:03:15I'm simply going to check that find
- 11:03:19user
- 11:03:21uh let's keep by email what you have to
- 11:03:24pass you have to pass this normalized
- 11:03:25email this one so this method now we are
- 11:03:28going to create in our user repository
- 11:03:30So I'm simply going to do export as sync
- 11:03:33function
- 11:03:36find user by email. So here we are going
- 11:03:39to get the email which will be string
- 11:03:41and here what we're going to do right we
- 11:03:43are simply going to get promise what
- 11:03:46will be the return type return type will
- 11:03:48be user this user will be in our type
- 11:03:52now okay one thing I I think I should
- 11:03:54mention don't get confused with this so
- 11:03:56you might be thinking if both are same
- 11:03:58why why you are creating two separate
- 11:04:00this is the format how we are storing
- 11:04:01the user in your DB this is the format
- 11:04:04how you want to return the data for
- 11:04:05example let's say you have one more
- 11:04:07something like this first name and this
- 11:04:10first name you want to return like this
- 11:04:12but in your DB this first name is
- 11:04:14present like this you're getting so
- 11:04:16these both are different so that is the
- 11:04:18reason we taking uh different type so
- 11:04:20that for now it is same but in some
- 11:04:23cases this will not be same
- 11:04:26so now there will be some snake case
- 11:04:27camel case all those things right so
- 11:04:29this is how we are going to return so
- 11:04:30either it will be user or it will be
- 11:04:33null and now what I'm going to do right
- 11:04:35I'm simply do const
- 11:04:37get the result. So result will be a we
- 11:04:40need to get our pool because this is
- 11:04:41where we are going to query. So you pull
- 11:04:43dot query and now this is where you have
- 11:04:45to give the type. This type will be your
- 11:04:47DB user row. This is how we are storing
- 11:04:49you're getting. And now we are going to
- 11:04:51write our SQL query. So I'm simply going
- 11:04:53to do select
- 11:04:58ID. And believe me or not, I usually
- 11:05:01like writing these raw queries so much.
- 11:05:03Okay? because I always believe that
- 11:05:05these all are much easier to understand
- 11:05:07and also it will give you very strong
- 11:05:08foundation. So I need ID email or so
- 11:05:11sorry this will be ro and created at
- 11:05:13from where from my user table. So I'm
- 11:05:15just going to do from my user table
- 11:05:17where what where the email is this
- 11:05:19right? So where email is equal to dollar
- 11:05:22one and this is where I'm going to just
- 11:05:24simply pass our email. So this is now is
- 11:05:27going to return this whether this user
- 11:05:30is present or not. And then I'm just
- 11:05:31going to do read result dot rows. I only
- 11:05:34need the first one or else it will be
- 11:05:37null. That's it. So now this is going to
- 11:05:40tell if this user is already present or
- 11:05:42not. So I'm going to import this. That's
- 11:05:44it. So what will be our next step? Once
- 11:05:47this is done, first we also have to give
- 11:05:49that if this existing user is already
- 11:05:51present. So in that case we're going to
- 11:05:52throw a new error. I think I already
- 11:05:54mentioned that we're going to throw a
- 11:05:56new app error and I'm simply going to do
- 11:05:58409 and I'll say that email already
- 11:06:02present. You're getting what are you
- 11:06:03doing that you can't use same email
- 11:06:05again.
- 11:06:07Once we'll do that now next time when
- 11:06:09we're going to give email and password
- 11:06:11right so let's see user given password 1
- 11:06:132 3 4 5 6 7 8 like that you never store
- 11:06:17this raw password in your DB okay should
- 11:06:20not do that what you have to do you have
- 11:06:22to create encryption or we usually call
- 11:06:24that one as has I think this part you
- 11:06:25already discussed in the crypto module
- 11:06:28so we need to create a password hash we
- 11:06:30are not going to store raw password in
- 11:06:33our DB correct but how we are going to
- 11:06:34do that this is where we need to inst
- 11:06:36install our b-rypt package. So brypt is
- 11:06:39a package that will give you that uh
- 11:06:41feature to hash your password. Okay. Uh
- 11:06:45so I'll just do give this one. Give me
- 11:06:46npm install bcrypt give me types uh npm
- 11:06:51also types brypt also.
- 11:06:59Okay. So these two things we need. So
- 11:07:00let's install npm install bcrypt js. You
- 11:07:03can use something called argan. So there
- 11:07:05is argon but brypt is actually very
- 11:07:07popular. So that is the reason uh we are
- 11:07:09going to use this. So npm install bcrypt
- 11:07:12and also need the types. So we'll copy
- 11:07:14this
- 11:07:16and I'm simply going to do types.
- 11:07:19So now what are you going to do right?
- 11:07:20So here I'm going to create a password
- 11:07:22has. So I'm just going to do con
- 11:07:24password
- 11:07:26has
- 11:07:29and this will be ait. So here what I'm
- 11:07:32going to do is let's uh sorry we have to
- 11:07:34import our brypt. So I'll do import
- 11:07:36bcrypt from brypt
- 11:07:40js
- 11:07:44and here I'm just going to do brypt and
- 11:07:46this is going to give you h has and here
- 11:07:48you have to pass the password. So
- 11:07:49password will be which password? The
- 11:07:50password that you are receiving this
- 11:07:52password. So you're going to pass the
- 11:07:53password here and then you have to pass
- 11:07:55a salt. Okay. Now in this case again
- 11:07:58what you can do right for now I'm just
- 11:07:59going to give direct as 10 but what you
- 11:08:01can do you can move this one to a
- 11:08:03constant file just like I mentioned here
- 11:08:05okay and this is done and then what
- 11:08:07you're going to do right now we are
- 11:08:08going to simply create the user now this
- 11:08:11create user is going to do what it's
- 11:08:12going to actually store your user in
- 11:08:14your DV so I'm simply going to have it
- 11:08:16and I'm simply going to do create user
- 11:08:19right create user and this is where what
- 11:08:23you have to pass you have to pass the
- 11:08:24normalized email and your password has
- 11:08:26both and this create user where you have
- 11:08:29to do it pause it and try to guess again
- 11:08:33you are doing something with your DB so
- 11:08:35that means you have to do in your
- 11:08:36repository so now if you go here and
- 11:08:39let's create one more so I'm simply
- 11:08:40going to do export as sync function and
- 11:08:44I'll do create user this is where we're
- 11:08:46going to get the email which will be
- 11:08:48string we're going to get the password
- 11:08:51has
- 11:08:53this also will be string and Now we are
- 11:08:56going to simply uh this will be type
- 11:08:58will be promise of user.
- 11:09:01So now I'm simply going to do con result
- 11:09:04this will be a pool dot query
- 11:09:08pass out db user row and this is where
- 11:09:11you're going to write our uh query.
- 11:09:14Okay. So I'm simply going to do insert
- 11:09:18into what is a table users. What we have
- 11:09:21to insert? You have to insert email and
- 11:09:23password
- 11:09:25has right. So your password has.
- 11:09:30So you have a values
- 11:09:37and then we are also going to do a
- 11:09:39returning here
- 11:09:42email ro and also let's return the
- 11:09:44created at that's it. And what will be
- 11:09:48the values? values will be your email
- 11:09:50and your password has. Right? Because
- 11:09:52we're passing email and password has.
- 11:09:54That's it. And then you're going to
- 11:09:55simply return
- 11:09:58result dot rows of zero. And this is
- 11:10:03done. Okay. So this will be your query.
- 11:10:08I think there is extra that's fine.
- 11:10:10Let's remove this line.
- 11:10:20Okay, let's keep it like this only. Save
- 11:10:22this. So now, [snorts] oh no, I don't
- 11:10:24like this one. Let's keep it like this.
- 11:10:34And we can move this one here
- 11:10:37like this. Okay, so this is fine. And
- 11:10:39now what we can do, right, we'll go to
- 11:10:40our o service and we'll just import it.
- 11:10:45Done. Go to what routes and then import
- 11:10:48your register user. That's it. And now
- 11:10:50save this and you'll see that now you
- 11:10:52should not get any error here.
- 11:10:56Okay, let's save this. Let's run it once
- 11:11:00again.
- 11:11:01I'll clear this and I'll do npm rundev.
- 11:11:05It's running. Okay, so now let's go to
- 11:11:07our postman. So I'm just going to copy
- 11:11:08this and let's run it. Right. So we have
- 11:11:114,000/ API/ o/ we have register correct.
- 11:11:16Now let's go to our body and we have to
- 11:11:18select raw and we have JSON. So now
- 11:11:21let's send uh email.
- 11:11:24So I'll send this email [email protected]
- 11:11:28and then here also uh what we are going
- 11:11:30to do right maybe let's only keep email
- 11:11:34for now and let's send this.
- 11:11:37Okay we are getting route not found. Oh,
- 11:11:40we have to do post I bet. So do send.
- 11:11:42You'll see that we are getting email and
- 11:11:44uh password are required. Let's see what
- 11:11:45we getting here.
- 11:11:48So we're getting app email and password
- 11:11:49and required.
- 11:11:52We getting cannot set headers after they
- 11:11:54say. Okay, these means we are sending
- 11:11:56the response to time sending back. So
- 11:11:58something we are doing wrong here. Uh so
- 11:12:01let's go to our uh service. So we are
- 11:12:03doing email and password require app
- 11:12:05error.
- 11:12:09Okay. So go to error handler. We'll see
- 11:12:11what we doing. How we have to return
- 11:12:13here. That is the reason. So when we
- 11:12:15doing response uh sending this response,
- 11:12:17right? So you have to return from this.
- 11:12:19So now let's save this. So now let's try
- 11:12:22once again. I think that error should
- 11:12:23not come. So we send we'll see that you
- 11:12:26are not getting that right. So getting
- 11:12:27email and password are required. Comma.
- 11:12:29And here we are going to do password.
- 11:12:32And now what we going to do right? So
- 11:12:33for this one 2 3 4. Let's keep it like
- 11:12:36this. send this. You'll see that we're
- 11:12:37getting password at least six characters
- 11:12:39long. Fantastic. So now let's do 5 six
- 11:12:43and then I'm just going to do send. You
- 11:12:44can see the registration is successful
- 11:12:46and please login to continue. That means
- 11:12:48this user is now present in our DB.
- 11:12:51Okay. So that means this uh particular
- 11:12:54code is now ran this create user. Okay.
- 11:12:57So with this this part is completed. The
- 11:13:00only basic register flow. Now you might
- 11:13:02be thinking one very important point.
- 11:13:04I'm not sure if you can uh actually
- 11:13:06check this particular thing that in our
- 11:13:08user table we also have what is that uh
- 11:13:11where is that user table man uh this one
- 11:13:14role so you have by default this is as
- 11:13:17user correct but how we are going to
- 11:13:19create admin then and you notice that we
- 11:13:22are not sending like this role equal to
- 11:13:24user actually you should not do like
- 11:13:25this and this is not a good good
- 11:13:27practice so the point is there are
- 11:13:29multiple ways we can create admin user
- 11:13:31okay so I'm just going to give you some
- 11:13:32very basic overview and some of The
- 11:13:34things are actually very important
- 11:13:35you'll see that in companies also they
- 11:13:36usually use first one is when you are
- 11:13:39going to create any admin user first of
- 11:13:41all you can directly update in your DB
- 11:13:43that is absolutely fine there is no risk
- 11:13:45though no wrong in this particular
- 11:13:47process also so let's say you are
- 11:13:49creating one particular SAS application
- 11:13:51and you are the owner and you want you
- 11:13:53to be admin right so it'll be admin so
- 11:13:55you can go to your DB you can directly
- 11:13:58uh update that particular role you can
- 11:14:00change your role from user to admin
- 11:14:02second And you can have a admin script
- 11:14:05just like we created a migration script.
- 11:14:07We can also have a admin script. Right?
- 11:14:09Third is like this is little more
- 11:14:11production level. So what companies are
- 11:14:12using they they use different tool.
- 11:14:14Okay. Now they have their internal tool
- 11:14:16also. Now in this internal tool they
- 11:14:18have some configuration management.
- 11:14:20Configuration management and there they
- 11:14:22can do right they can have some admin
- 11:14:24emails. Consider like this. And once
- 11:14:26they will change that admin email and
- 11:14:28update the configuration automatically
- 11:14:29in your DB these values will be also
- 11:14:31updated. So they have some mechanism. So
- 11:14:33there are multiple ways we can actually
- 11:14:35create admin user because I'm pretty
- 11:14:37much sure sir this question can come to
- 11:14:39your mind that how we are going to
- 11:14:40create a admin user then because most of
- 11:14:43the user whatever we're going to create
- 11:14:44by default it will be created as a
- 11:14:46normal user and that makes sense. So
- 11:14:48that is the reason I just wanted to send
- 11:14:49this point. So with this so our uh
- 11:14:52register flow is completed. So in the
- 11:14:54register flow we don't have to do
- 11:14:55anything just save the user and save the
- 11:14:58password. That's it. Okay. also uh this
- 11:15:00one also very important. Okay. So just
- 11:15:02add a return here. I think this part we
- 11:15:04have missed. So with this now let's move
- 11:15:05to the next one which will be our login
- 11:15:07flow. Okay. So now let's create our
- 11:15:09login flow and we are going to use exact
- 11:15:11same uh flow that what we have done. So
- 11:15:14let's close everything. We'll start with
- 11:15:15our o routes. Okay. So first let's
- 11:15:19create one more route here and this will
- 11:15:21be so I'm just going to drawouter.post
- 11:15:24and this will be path will be login. So
- 11:15:26it will be SL api/
- 11:15:28login. Here also we're going to do
- 11:15:30async. So do request response next and
- 11:15:36here let's take a try catch. So in the
- 11:15:40catch we'll get error and then we're
- 11:15:42going to call out next and pass the
- 11:15:44error. So in the try first of all what
- 11:15:46you have to do again when you're going
- 11:15:47to login user is going to type email and
- 11:15:49password only. So I'm not repeating the
- 11:15:51same thing it's same only. So do email
- 11:15:53password and you're going to get from
- 11:15:55request dot body. Okay.
- 11:15:59Now first of all what we have to do here
- 11:16:01right remember once user will login now
- 11:16:04this time we are only not only going to
- 11:16:07redirect the user to our application we
- 11:16:09also need to store something related to
- 11:16:11that particular information right and
- 11:16:13what that something that's something we
- 11:16:15called as access token. Now by the name
- 11:16:18only you can understand access means
- 11:16:19this token is going to give user access.
- 11:16:22Right? Without this particular token,
- 11:16:24you will not be able to understand
- 11:16:25whether this particular user is actually
- 11:16:27authenticated user or not. So that means
- 11:16:29this access token is the token that we
- 11:16:32are going to store in our local storage
- 11:16:34session storage or in our cookies. That
- 11:16:36is like front end side logic, right?
- 11:16:38Once we are going to send this but
- 11:16:39because this is a backend tutorial, you
- 11:16:41have to understand that as a backend
- 11:16:42developer, your task is to create access
- 11:16:45token or refresh token. Refresh token is
- 11:16:46a separate concept. But let's understand
- 11:16:48access token because this is very
- 11:16:50important. Also this token is going to
- 11:16:52have some lifetime. Lifetime means let's
- 11:16:53say you want to you are creating very
- 11:16:55sensitive application there you want
- 11:16:58your uh access token to be only lived
- 11:17:01till 15 minutes means after 15 minutes
- 11:17:03if user uh don't do any kind of activity
- 11:17:06you are not doing any kind of activity
- 11:17:08you are going to auto log out that
- 11:17:10particular user from the application or
- 11:17:11maybe let's say you are creating some
- 11:17:13e-commerce application in that case 15
- 11:17:15minutes is not fine so you can keep the
- 11:17:17access token as maybe let's say 7 days
- 11:17:19I'm just giving example or 1 day 24
- 11:17:21hours Okay. So at the end you have to
- 11:17:24understand this access token whatever we
- 11:17:26are going to create is going to hold
- 11:17:28that user information and based on this
- 11:17:30we are going to check if the user is a
- 11:17:32authenticated user or not. So first what
- 11:17:35we going to do right. So let's do one
- 11:17:36thing. So for now I'm just going to do
- 11:17:39XY Z. Okay. Later we'll change this one.
- 11:17:41You'll understand why I'm doing like
- 11:17:42this. And for now let's create a login
- 11:17:45user. So I'll do login user. And here
- 11:17:47I'm going to pass the email and the
- 11:17:49password. Okay. We are not doing
- 11:17:50anything. this login user will be our
- 11:17:54service logic. So we'll go here and
- 11:17:56let's create another here. So I'm just
- 11:17:57going to do not here we have to do
- 11:18:00export as sync function this will be
- 11:18:05login user. So here also I'm going to
- 11:18:07get email this will be string password
- 11:18:09this will be string and here what this
- 11:18:12is going to return so this is going to
- 11:18:14return promise but now what we going to
- 11:18:16return right we are going to return one
- 11:18:18access token here. So I'm just going to
- 11:18:20return a access token. Okay. And this
- 11:18:22access token will be string. So that
- 11:18:24means this access token we are going to
- 11:18:26receive here. So now if I just do import
- 11:18:28and let's see if I just do access token
- 11:18:30here. I'm going to get the access token.
- 11:18:31So for now I'm not returning. Let's keep
- 11:18:33it like this. Okay. I'm also going to
- 11:18:35remove the import. Let's uh keep it like
- 11:18:37this only for now. So first of all we
- 11:18:39are going to simply give a condition and
- 11:18:41also remember because we doing a custom
- 11:18:43validation right? If I get some more
- 11:18:45like if I have time like if this this
- 11:18:48video is not crossing like two maybe 16
- 11:18:51hours or 18 hours something like that
- 11:18:52then it will be tricky or else I'm going
- 11:18:54to give you some idea that how you can
- 11:18:56use zord and everything to implement
- 11:18:57this validation but you might be
- 11:18:59thinking why I'm not using zord
- 11:19:01absolutely zord is industry standard you
- 11:19:03can use other packages but again because
- 11:19:05I when I create this video right I have
- 11:19:07to think from a beginner point of view
- 11:19:09also let's see if a student is watching
- 11:19:11this video and then you they don't have
- 11:19:13any idea that how they can create a
- 11:19:15custom error class and directly jump in
- 11:19:17learning in zod that that doesn't make
- 11:19:19any sense you're getting so you have to
- 11:19:21go from level one to level five not
- 11:19:24directly to level five so first of all
- 11:19:26let's copy this and then we going to
- 11:19:28first keep it like this simple now what
- 11:19:30you have to do so again we have to do
- 11:19:32normalize email and then I'm simply
- 11:19:34going to do email dot to lowerase dot
- 11:19:38trim okay now what you have to do here
- 11:19:41now this is actually very important
- 11:19:43concept Now what will happen right when
- 11:19:45you are actually giving your email
- 11:19:48and also you're giving your password. So
- 11:19:51now this time you're not registering you
- 11:19:53actually doing a login activity. So we
- 11:19:55have to check if this email is present.
- 11:19:57So that is the first thing you also have
- 11:19:58to check if this password is present and
- 11:20:00also if this is not a valid password. If
- 11:20:03this is not a valid password so we are
- 11:20:05we will not allow user to login. So
- 11:20:08let's say during registration I've given
- 11:20:10here 1 2 3 4 5 6. But while login I'm
- 11:20:13giving 1 2 3 4 6 5 and this is wrong
- 11:20:18correct. So that means we need to
- 11:20:19validate the password also when we going
- 11:20:22to login. So that means here what I'm
- 11:20:24going to do first I'm going to check
- 11:20:25this user is present or not. So I'm just
- 11:20:27going to do a find user by email
- 11:20:33with password email with password. Okay.
- 11:20:37And here we're going to pass a normal
- 11:20:38HTML. this method again when we sorry
- 11:20:41where we are going to create this this
- 11:20:42we're going to create in our repository
- 11:20:44so we'll go to our repository first
- 11:20:46let's copy this we copy this and then
- 11:20:49I'm simply going to do export as sync
- 11:20:52function so this will find user email
- 11:20:55with password here we're going to get
- 11:20:57the email which will be string and here
- 11:20:59we are going to return a promise
- 11:21:02so here we have this DB user with
- 11:21:05password row remember we have created
- 11:21:07this in our types or null. So now you
- 11:21:10can simply take our result. So result
- 11:21:12will be ait pool dot query. So here
- 11:21:16we're going to pass DB user with
- 11:21:18password row and here we can simply
- 11:21:21write. So I'll do select
- 11:21:24ID select email uh select email also we
- 11:21:29need the role also role password has and
- 11:21:33also let's take the created at.
- 11:21:37Okay. So this we're going to get from so
- 11:21:39do from so we have our users where email
- 11:21:43will be this
- 11:21:46okay and then we're going to simply pass
- 11:21:48our email and then at the end we're
- 11:21:50going to return result dot rows of zero
- 11:21:54or else null so this is going to give me
- 11:21:58that particular information okay so now
- 11:22:00we can go back to our o service so we're
- 11:22:02going to check so first what you have to
- 11:22:04check right so here I'm just going to
- 11:22:05check if this user dot
- 11:22:08And this is where I'm going to have this
- 11:22:10password has. Oh, first I have to import
- 11:22:12that. So I'm not getting that right. We
- 11:22:14have to import this. So user dot
- 11:22:16password has if this is not present. So
- 11:22:19I'm just going to do no. So in that case
- 11:22:22I'm simply going to throw new app error.
- 11:22:25And we are going to give a 401. Okay.
- 11:22:28And I'm just going to do invalid email
- 11:22:31or password. Okay. That's it.
- 11:22:35Uh what else? Now we are going to check
- 11:22:37if this password is a valid password or
- 11:22:39not. So I'm just going to do con is
- 11:22:40password valid. So then we are going to
- 11:22:44use a and we're going to use this brypt.
- 11:22:48We have to use bcrypt. We are using it
- 11:22:52brypt dot we have compare. So how we
- 11:22:56going to compare? Now this is one of the
- 11:22:57most important concept. How we going to
- 11:22:59compare? So you're going to compare with
- 11:23:01this password. This
- 11:23:04password is the password that you are
- 11:23:05sending from the UI side. Okay, means
- 11:23:08this is the actual password user is
- 11:23:10going to type. But then this password
- 11:23:12you're going to compare with which one?
- 11:23:15So you're going to compare with the one
- 11:23:16that is stored in your DB. Okay. So this
- 11:23:19password has easily stored in your DB.
- 11:23:21Correct? So that means if this password
- 11:23:24and user dot password has both are same
- 11:23:27that means this user given correct
- 11:23:29password 1 2 3 4 5 6. But if this user
- 11:23:31given 1 2 3 4 6 5 so that means this
- 11:23:34password is wrong. Okay. So that means
- 11:23:36there will be no match you are getting.
- 11:23:38So if this is the case so now we can
- 11:23:40simply check that if this password valid
- 11:23:43is false. So is password valid is false.
- 11:23:45So in that case I'm just going to throw
- 11:23:49new app error and I'm simply going to do
- 11:23:52401 and I'll do invalid email or
- 11:23:56password. And you notice one thing we
- 11:23:58are simply not directly telling that
- 11:24:00whether your email is invalid or
- 11:24:02password is invalid. Let user guess
- 11:24:03because there is a possibility that
- 11:24:05let's say some hacker uh hackers are
- 11:24:07trying to access right. So they don't
- 11:24:09know that whether the email is invalid
- 11:24:11or password is invalid. So giving a
- 11:24:12general message here. So if this is not
- 11:24:16invalid so I'm going to throw error if
- 11:24:18this is invalid. So now we are going to
- 11:24:20do what? We're going to create a access
- 11:24:21token. Now this is one of the most
- 11:24:23important concept. Okay. So we'll do
- 11:24:25access token and then we are going to
- 11:24:27create a sign access token
- 11:24:31and then what we're going to pass we're
- 11:24:33going to pass some information but
- 11:24:34before that let's create one more
- 11:24:38file here and that we're going to create
- 11:24:40in our leave and I'm just going to do
- 11:24:41jwt.ds DS. Okay. So this is where we are
- 11:24:45going to create our token. Now what
- 11:24:46actually token will consist now this is
- 11:24:48what we have to understand. See your
- 11:24:50[snorts] token is going to have let's
- 11:24:51say you have a user A. Okay. This token
- 11:24:55is going to have a user ID.
- 11:24:58Why? This user ID will be unique. This
- 11:25:01token is going to have a email
- 11:25:03information of that user and this token
- 11:25:06also going to have the role of that
- 11:25:07user. Either this user is a normal user
- 11:25:10or this user is a admin user. Correct.
- 11:25:12Now when we are going to create the
- 11:25:14token and that particular token is going
- 11:25:16to hold all this information. So we will
- 11:25:18be able to understand right that this is
- 11:25:20which user from the user ID what is the
- 11:25:23user email from this email and what is
- 11:25:25the user role from this role. Correct.
- 11:25:27So that means first of all we have to
- 11:25:28create a type here. So I'm just going to
- 11:25:29do export type and I'm just going to do
- 11:25:32token payload. Okay. Or maybe I'm doing
- 11:25:35wrong. Let's go to our types and then
- 11:25:38we'll keep it here only. token payload
- 11:25:41and this is where I'm just going to use
- 11:25:43a user ID. This will be string. Uh this
- 11:25:46also is going to have a email which also
- 11:25:48will be string. This is also going to
- 11:25:50have a role which will be string. So
- 11:25:52save this. Now we'll go here and we're
- 11:25:54going to create our access token. So I'm
- 11:25:55just going to export function. I'll do
- 11:25:58sign access token and this is where I'm
- 11:26:00going to get my payload. Why you need
- 11:26:02payload? Because you need to get the
- 11:26:03user ID, email and password. Correct?
- 11:26:05And this is what you're going to do
- 11:26:06token payload and this is going to
- 11:26:09return a string. So now what we're going
- 11:26:11to do right we're going to create some
- 11:26:12options but before that we need a
- 11:26:15particular package. So which package we
- 11:26:17need? So now let's search here JSON.
- 11:26:20What is JSON web token
- 11:26:27right? So JSON web token is a security
- 11:26:30blah blah between a code structure. JSON
- 11:26:33is a single uh string split into dots
- 11:26:35headers payload that we are going to
- 11:26:37discuss. So first you can see that we
- 11:26:39have to install JSON web token and we
- 11:26:41have this user payload and then based on
- 11:26:43this you have to create a token here.
- 11:26:45Okay first what we're going to do right
- 11:26:47so we'll go here install this. So I'm
- 11:26:50just going to install this one here. So
- 11:26:52go to this I'm just going to do clear
- 11:26:55install
- 11:26:56and also install the types.
- 11:27:02Okay. Now what you have to do, right? So
- 11:27:04this is going to need your secret JWT
- 11:27:07secret. Okay. So here I'm going to copy
- 11:27:09this. You go to your env.
- 11:27:13And then we are going to add one more
- 11:27:14here. So I'll do JWT secret and this
- 11:27:17will be super secret JWT key. Okay. Now
- 11:27:22we can change this one. For now we're
- 11:27:24going to keep it like this. And this one
- 11:27:26first we have to add this. So we're
- 11:27:28going to add this in our env. So we'll
- 11:27:30go here and go to our env. And then I'm
- 11:27:33simply going to take JWT access
- 11:27:38secret. Okay. And this is where I'm just
- 11:27:40going to use this sorry check required
- 11:27:42variables and I'm going to pass this JWT
- 11:27:44secret. Okay. Then I'm also going to
- 11:27:47take very very important. I'm also going
- 11:27:48to add one more here that okay what will
- 11:27:51be the time that till what time this
- 11:27:53particular secret will be accessible. So
- 11:27:55I'm just going to do here. So let's do
- 11:27:57JWT access expires in and let's give
- 11:28:02this one as 15 minutes. Okay. So till 15
- 11:28:05minutes this will be this particular one
- 11:28:07will be available or maybe let's do this
- 11:28:08one 30 for now you can change this one.
- 11:28:11So now what I can do right I can go here
- 11:28:13and then add one more. So I'll do JWT
- 11:28:15access expires in. So this will be check
- 11:28:19this and then we are going to pass the
- 11:28:21JWT access expires in.
- 11:28:25Okay, this is done. So now we can create
- 11:28:27our options. So how we going to create
- 11:28:29options? So first of all, we need to
- 11:28:30import something from JWT package. So
- 11:28:33I'm just going to do import JWT,
- 11:28:38from JSON web token. And what we need?
- 11:28:41We need sign in options. It's called
- 11:28:42sign options. Okay. So now this will be
- 11:28:45my type. So I'm just going to do sign
- 11:28:47options. And here what I have to pass
- 11:28:49it, you have to pass a expired when this
- 11:28:51will be expired. So here I'm going to
- 11:28:52get my env dot you'll see that we have
- 11:28:55this JSON access expires in and here you
- 11:28:58have to go see this is going to give you
- 11:29:00error okay TypeScript error. So here
- 11:29:02you're going to give as and you're going
- 11:29:03to give sign in options and then you
- 11:29:05have to pass the expires in here. Okay.
- 11:29:07And then you are not going to get the
- 11:29:09error and then how you're going to
- 11:29:10create actual token. So to create actual
- 11:29:12token you're going to return JWT and
- 11:29:14this is going to give you the sign
- 11:29:16method here. What you have to pass very
- 11:29:18very important. This is where you're
- 11:29:19going to actually pass the payload and
- 11:29:21this payload is going to hold your user
- 11:29:24information. You're going to pass the
- 11:29:25payload. You also need to pass your JWT
- 11:29:28secret. So I'll do JWT secret. And then
- 11:29:31you're going to pass the options. So
- 11:29:33these options now is going to hold your
- 11:29:35expiry. This is going to hold your
- 11:29:37token. So based on this token and based
- 11:29:39on this user information, it's going to
- 11:29:41create a uh JWT token. Okay. So now
- 11:29:44let's save this. So we go to all service
- 11:29:47to see how much uh sound is it's coming
- 11:29:49right from outside. Can't do anything.
- 11:29:52How many times I'm going to pause and
- 11:29:54redo. It's very uh difficult. Okay. So
- 11:29:57now we're going to create sign in. So
- 11:29:59this is going to receive the token.
- 11:30:01Correct?
- 11:30:02Uh this one we're going to import first
- 11:30:04and this is going to receive payload. In
- 11:30:07this payload now we going to pass the
- 11:30:08user ID. See we getting now from where
- 11:30:10you're going to get the user ID. This
- 11:30:12user ids you're going to get from this
- 11:30:14user information because this
- 11:30:15information is going to give you this
- 11:30:18user information right here. Select all
- 11:30:20of these. So now you [snorts] can go
- 11:30:21here and then I can simply pass user.
- 11:30:25user is going to have your ID email user
- 11:30:29dot email and then role user dot role
- 11:30:34you notice we are not adding the
- 11:30:35password here okay and then at the end
- 11:30:38we're going to now return the access
- 11:30:39token that's it so that means in the end
- 11:30:42we are [clears throat] doing the login
- 11:30:43and we're returning the access token
- 11:30:45save this and now if you go here and
- 11:30:49import this and now you can get the
- 11:30:51access token directly here so do access
- 11:30:53token
- 11:30:57Okay, we are getting error man.
- 11:31:03Oh, value is not used. That's correct.
- 11:31:06So now what we can do right simply we
- 11:31:08are going to return. So do raise dot
- 11:31:10status. So I'll do 200 dot JSON.
- 11:31:15And here let's pass the success will be
- 11:31:19true. And here we're going to pass a
- 11:31:21data. And here I'm going to call this
- 11:31:23access token.
- 11:31:28Okay, still we getting error. Access
- 11:31:30token doesn't exist on promise access
- 11:31:32token. This how come
- 11:31:41I'm doing some silly mistake only. Oh, I
- 11:31:44have to take a man here. See that's
- 11:31:47what. So now this is done. So that means
- 11:31:50we going to get the access token and
- 11:31:51this flow is now completed. Okay. So now
- 11:31:54what will be the next step? Next we have
- 11:31:56to create our uh what is that called
- 11:31:57middleware. But before that let's do one
- 11:31:59thing. Let's check this login flow
- 11:32:01first. So I'm going to go here
- 11:32:06and let's do post and I'll do login
- 11:32:10and let's do here email.
- 11:32:14So I'll do sum at the rate one but I'll
- 11:32:17change this on this and send this email
- 11:32:20and password are required. So here I'm
- 11:32:22just going to take the password and I'll
- 11:32:24do I'll give correct uh password but
- 11:32:26I'll given wrong email. You can see here
- 11:32:28right? So the email is wrong. Send this
- 11:32:30invalid email or password. So now I'm
- 11:32:32going to give correct email but wrong
- 11:32:34password. Send this invalid email or
- 11:32:38password. I'm going to give correct one.
- 11:32:393 4 5 6. Okay. send this and you're
- 11:32:42going to get your success data and this
- 11:32:44is your access token. Okay, this token
- 11:32:47is going to is actually holding all your
- 11:32:49information. This is holding your user
- 11:32:50ID, user email and user role. Okay, so
- 11:32:53this will be the overall flow. Okay. So
- 11:32:55what you should do right instead of
- 11:32:56directly jumping to middleware try to
- 11:32:58understand that what we have done we
- 11:32:59getting the data from our UI we're going
- 11:33:01to services. Okay. We are then checking
- 11:33:05this one. This is very basic validation.
- 11:33:07That's okay. Then we checking the email
- 11:33:09and then we're checking if the email is
- 11:33:11pres sorry user is present. If user is
- 11:33:13not present throw error. If password is
- 11:33:15not not matching throw error. If this is
- 11:33:18not the case then we're going to create
- 11:33:20the token and we are going to returning
- 11:33:21the token here. Okay. So with this now
- 11:33:23let's move to the next one which will be
- 11:33:26now we're going to create our middleware
- 11:33:28and also how you're going to get your
- 11:33:29current user information. So let's do
- 11:33:31this one in the next part. All right. So
- 11:33:33now let's create our middleware. So we
- 11:33:35have to create two middleware. One will
- 11:33:36be our authentication middleware and
- 11:33:38second one will be your admin middleware
- 11:33:40and both are very important. Okay. So
- 11:33:41first let's do one thing. Let's go here
- 11:33:43and let's create the first one. I'm just
- 11:33:45going to do o dot middleware.ts
- 11:33:49ts and also we going to create admin dot
- 11:33:52middleware
- 11:33:54ts. So first in this o middleware what
- 11:33:58we have to do. So first you have to see
- 11:34:00that when we are going to send the token
- 11:34:02right let's say you have a UI correct.
- 11:34:05Now let's say you're doing a API request
- 11:34:07something like this. So how you usually
- 11:34:09send your token right? So you are going
- 11:34:11to send your token in your request
- 11:34:14headers. Now if you remember in the HTTP
- 11:34:17module we have discussed about the
- 11:34:18headers. So in these headers one of the
- 11:34:20headers property will be called
- 11:34:22authorization. Okay. And in this
- 11:34:24authorization we are going to send our
- 11:34:26token. But how we usually send our
- 11:34:27token? We send this particular token as
- 11:34:30a beer token. So it's like this. So you
- 11:34:32have beer B E A R E R and then there
- 11:34:37will be a space and then there will be
- 11:34:40your token. So it will be beer space
- 11:34:44token. Now this beer is going to hold
- 11:34:46your token. So it's like a it's beering
- 11:34:48your token. So it's like a taking your
- 11:34:50token and this token we are going to
- 11:34:51send from our UI side to our back end
- 11:34:54side and back end is going to validate
- 11:34:55this token and based on that we are
- 11:34:57going to understand whether this user is
- 11:34:58a normally user or admin user whether
- 11:35:01this user is authenticated user or non
- 11:35:03unauthenticated user all these things.
- 11:35:05So if you go here and you'll see that be
- 11:35:08error token. So let's see what is what
- 11:35:10it's telling right. So first you'll see
- 11:35:12that it's a security grant the B
- 11:35:15industry standard to handle your JSON
- 11:35:16web token or JWT right. So you have your
- 11:35:19authorization you have your BR and then
- 11:35:21your actual token and you see that there
- 11:35:22is a space. Now this space is important
- 11:35:24because what you're going to do right
- 11:35:26when we'll split it so we are going to
- 11:35:28split it with space. So left side will
- 11:35:31be beer right side will be your token.
- 11:35:33So how we are going to get the token. So
- 11:35:35once you're going to split and take the
- 11:35:37right side of it, we will be getting our
- 11:35:38actual token. Okay. So this is how we
- 11:35:41going to send. So authorization BL and
- 11:35:43this is your token. So first what we're
- 11:35:45going to do, right? So I'm just going to
- 11:35:46do export function and I'll do
- 11:35:48authentication or authenticate. I think
- 11:35:50let's keep authenticate. Here we're
- 11:35:52going to get our request. Now this
- 11:35:54request will be
- 11:35:56so I'm just going to do import from
- 11:35:59express. So here we're going to get
- 11:36:01request. We're going to get response and
- 11:36:03we are also going to get
- 11:36:06next function. So request will be our
- 11:36:09request. Then we're going to get our
- 11:36:11response which will be our response and
- 11:36:13then we're going to have our next very
- 11:36:15very important which will be our next
- 11:36:17function. And here we're going to have a
- 11:36:19void. So first we are going to get our
- 11:36:21header. So I'm just going to do all
- 11:36:23header and this will be your request dot
- 11:36:26headers and then you're going to have
- 11:36:28your authentication. So you request
- 11:36:30header dot authentication. So you can
- 11:36:32see that right. So not authentication
- 11:36:34authorization request do header dot
- 11:36:36authorization. This is what actually
- 11:36:38it's telling here. Okay. Now what I'm
- 11:36:40going to do? So I'm simply going to do
- 11:36:42if not equals this o header dot starts
- 11:36:46with if this is not started with B
- 11:36:48error. So that means there is a wrong
- 11:36:50because it will always start with B
- 11:36:52error. So you can see that. So it's
- 11:36:53simply checking the if this token is not
- 11:36:55started with B error that means you're
- 11:36:56sending a wrong format from your UI side
- 11:36:59and also this will be space right so B
- 11:37:01error space so in that case what you're
- 11:37:03going to do you're going to call next
- 11:37:04middleware then you're going to pass a
- 11:37:06new app error and then you're going to
- 11:37:09pass a 401 so whenever there is a
- 11:37:11authentication related thing you're
- 11:37:13going to send a 401 and this will be
- 11:37:15access token is required
- 11:37:19so this is the first thing and then
- 11:37:20you're going to return then we're going
- 11:37:23to get our actual token. Okay. So, how
- 11:37:25we going to get our actual token? So,
- 11:37:26we're going to do token and this will be
- 11:37:29so we're going to get o header and now
- 11:37:30we're going to do I already mentioned
- 11:37:32we're going to split. So, we are going
- 11:37:34to split with what space and then we're
- 11:37:36going to take the right side right. So,
- 11:37:38split means zeroth and this is first
- 11:37:40one. So, we're going to do split and
- 11:37:42then we are going to do with space and
- 11:37:44then I'm going to take the first one.
- 11:37:46So, this is going to give me my actual
- 11:37:47token the right side of it. And now what
- 11:37:50we have to do now this is actually very
- 11:37:52important. Okay, now this is extremely
- 11:37:54important. I'm going to show you one
- 11:37:55very interesting thing. You'll see that
- 11:37:56if I just do request dot user, right?
- 11:38:00And now you'll see that this user is not
- 11:38:02present. And this makes sense because
- 11:38:04this request which is a type of request,
- 11:38:07this is not holding any user
- 11:38:09information. So that means we need to
- 11:38:11some inform express that okay in my
- 11:38:14request object, I also need to include
- 11:38:16this user object here. But how we going
- 11:38:19to do this? You're getting what I'm
- 11:38:20talking about means this will give you a
- 11:38:22typescript error because this user
- 11:38:23doesn't exist. So that means to inform
- 11:38:26express that okay you need to add or you
- 11:38:28need to append this user information in
- 11:38:30your request object. So how you going to
- 11:38:32do this? So go to types and then I'm
- 11:38:35just going to create express dot
- 11:38:38uh d.ts. So I'm going to create a global
- 11:38:41express and uh so first I'm going to
- 11:38:44declare uh express and here I'm simply
- 11:38:47going to append that one uh that
- 11:38:49particular
- 11:38:51sorry not express we are going to
- 11:38:52declare global
- 11:38:54and here inside this I'm going to take a
- 11:38:56name space which will be express
- 11:38:59and inside this we're going to take our
- 11:39:01interface and in our interface will be
- 11:39:03our interface of this request right so
- 11:39:06we have this request so simply we're
- 11:39:07telling express that you take your
- 11:39:09request object object and in this
- 11:39:11request object also append this user so
- 11:39:13that I can access this request do user.
- 11:39:15So now here we are going to submit a
- 11:39:17login this request interface I am going
- 11:39:20to add a user here and this will be
- 11:39:21optional and this user will be the token
- 11:39:23payload that we have created. This token
- 11:39:25payload is going to hold your user ID
- 11:39:27mail and role and you'll see that now
- 11:39:28you're not getting this error because
- 11:39:30this express is now holding. So express
- 11:39:32dot request do user equal to token
- 11:39:34payload you're getting. So that means
- 11:39:36this user is now appended in your
- 11:39:37request object. And then you're going to
- 11:39:39just do export this. That's it. So now
- 11:39:43this request do user what this value
- 11:39:45will be. So this will be so this request
- 11:39:47do user will be nothing but here we are
- 11:39:49now going to verify our access token. So
- 11:39:51I'm just going to do verify access token
- 11:39:53and you're going to pass this token here
- 11:39:55because you need to verify it. You just
- 11:39:57can't uh use any random token and then
- 11:40:01you can give user uh access. So we'll go
- 11:40:04to our JWT and then I'm just simply
- 11:40:06going to do export function and I'll do
- 11:40:09verify access token. We're going to get
- 11:40:12the token which will be string and here
- 11:40:14we're going to get our token payload.
- 11:40:16And now to do this we're simply going to
- 11:40:18do a try catch.
- 11:40:21So here I'm simply going to throw a new
- 11:40:23app error 401 and I'll do invalid or
- 11:40:27expired
- 11:40:29access token.
- 11:40:31And in this try now I'm simply going to
- 11:40:33do return.
- 11:40:35So we do JWT dot I think it's called
- 11:40:39verify. And here you're going to pass
- 11:40:41the token. But how you're going to
- 11:40:42verify? So you have remember in the env
- 11:40:45you have this JWT access secret. And
- 11:40:47based on it it's going to verify. Okay.
- 11:40:50And here you're going to simply pass as
- 11:40:52so do token payload. And this is done.
- 11:40:54So now this method is going to verify
- 11:40:56your JWT token. And this is what we
- 11:40:59actually verifying here. Okay. And then
- 11:41:01what you have to do, can you guess what
- 11:41:02you have to do here? Extremely important
- 11:41:05concept. Extremely important. So you
- 11:41:07have to always call your next
- 11:41:08middleware. Remember I already explained
- 11:41:10this one. If you don't call this next
- 11:41:12middleware, it will always stuck in
- 11:41:14middle. Okay, it will never call your
- 11:41:16next handler or it will never call your
- 11:41:18next middleware. So that means now this
- 11:41:20particular authenticate is a middleware
- 11:41:23which is going to check every request
- 11:41:26whenever you're going to use this
- 11:41:27middleware that whether the user is a
- 11:41:29authenticated user or not. Now to check
- 11:41:32the admin uh middleware how we are going
- 11:41:34to do this one. So first of all let's do
- 11:41:36one thing. Go here. I'm going to just
- 11:41:39copy this paste it here. So I'm simply
- 11:41:41going to do export function
- 11:41:44require
- 11:41:45admin.
- 11:41:47So I'm going to take request which will
- 11:41:49be request response which will be
- 11:41:52response
- 11:41:53next which will be next function
- 11:41:57and this will void. So simply I'm just
- 11:41:59going to check if this request do user
- 11:42:02I'm going to have user dot role. If this
- 11:42:05is not admin right if this user is not
- 11:42:08admin user so that means we're simply
- 11:42:09going to return next and this will be
- 11:42:12new app error and I'm going to return
- 11:42:14403 and I'm simply do that admin access
- 11:42:18required
- 11:42:20you don't have do not have uh admin
- 11:42:24access it seems something like this and
- 11:42:28then I'm simply going to return this and
- 11:42:31then what you have to do here again you
- 11:42:33have to call the next Okay. Always you
- 11:42:35have to call the next or else it will be
- 11:42:37stuck. Now I think this is done. So what
- 11:42:40will be the next? Next will be now let's
- 11:42:42do one thing. Let's go to our o routes
- 11:42:45and then I'm going to create one more
- 11:42:47route here. So what I need to do right I
- 11:42:49wanted to get my current user
- 11:42:52information. How you going to get
- 11:42:54current user information? Until unless
- 11:42:55you log in you don't have the token and
- 11:42:57from the token you'll be able to get the
- 11:42:59user information. So I'm simply going to
- 11:43:00do O router dot get
- 11:43:04and then I'm simply going to do me.
- 11:43:06Okay. And here for now I'm just going to
- 11:43:08do request response. Now this is not
- 11:43:10correct. It's not going to work but I
- 11:43:12wanted to show you that only. So ludase
- 11:43:14dot status and I'm simply going to pass
- 11:43:16200 dot JSON and here what I'm going to
- 11:43:19do right I'm simply going to do success
- 11:43:23will be true and here I'm going to pass
- 11:43:25a data and here I'm going to do user and
- 11:43:28this will be request and you're going to
- 11:43:29have this user correct save this and
- 11:43:32you'll see that this is not going to
- 11:43:33work and this is where you're going to
- 11:43:35understand one very important concept
- 11:43:36and that is how you can protect your
- 11:43:39routes
- 11:43:41save this so now I think We don't have
- 11:43:43any error. So let's go here. Uh copy
- 11:43:46this
- 11:43:48get and this will be / API/og/me
- 11:43:51and I'm going to just do send here.
- 11:43:54You're not getting any error. Sorry not
- 11:43:55getting any error but you're not getting
- 11:43:57any data. Why? First of all until unless
- 11:44:00you have the token. So here we don't
- 11:44:02know right if the user is logged in user
- 11:44:04or not. So how you're going to get user
- 11:44:05information? So that means what you have
- 11:44:07to do right in your authorization you
- 11:44:09have to actually pass your token. So
- 11:44:11let's say I'm going to pass a beer token
- 11:44:12here. I have to pass the token and then
- 11:44:14I also need to protect this particular
- 11:44:16router. How I'm going to protect? So
- 11:44:18this is where I need to use middleware.
- 11:44:20So middleware here is going to protect.
- 11:44:22So it's going to check if the user is
- 11:44:24authenticated user or not. So now to
- 11:44:26check this what you have to do in middle
- 11:44:28you just have to call that authenticate
- 11:44:30middleware. That's it. Now this is now
- 11:44:32is going to work as a barrier. So that
- 11:44:34means when it's going to do here it's
- 11:44:36going to check this middleware. If this
- 11:44:38is passed then it's going to run this.
- 11:44:40If this is fail so it's going to return
- 11:44:42error. For example, if you go here right
- 11:44:44and let's say first of all save this and
- 11:44:48now let's say if I run that particular
- 11:44:50from the postman you'll see that you're
- 11:44:52not going to get this response. What
- 11:44:54you'll get right you'll get B token is
- 11:44:56needed or access token is needed. Now
- 11:44:58see what will happen if I go here and
- 11:45:01send this see access token is required.
- 11:45:03So that means now this particular route
- 11:45:06is protected. Okay, protected by whom?
- 11:45:08Protected by this middleware. So if I go
- 11:45:10to middleware, it's checking okay, you
- 11:45:12don't have any beer token. So it's going
- 11:45:15to return this. If it will have beer
- 11:45:17token, then it's going to verify the
- 11:45:18token and then you're going to get your
- 11:45:20user information. I hope this is clear.
- 11:45:22So now what I'll do right, I'll go here
- 11:45:24login. I'm going to send this. I'm going
- 11:45:26to get the token. Copy this token.
- 11:45:29Go to this. Go to uh headers. And then
- 11:45:32I'm going to add one header. Okay. So
- 11:45:34let's add here authorization. And here
- 11:45:36I'm just going to do beer, right? So be
- 11:45:39error and then paste this. And then if I
- 11:45:41send this, that's it. So now you're
- 11:45:43getting your user, user ID, email, role,
- 11:45:46and the rest of the expiry blah blah.
- 11:45:49But this is the important. So you're
- 11:45:50getting user, you're getting what I'm
- 11:45:52talking about. So this is how you're
- 11:45:53going to implement the middleware. Now
- 11:45:55this token is actually hold this
- 11:45:58information here. Why? Because when
- 11:46:00you're doing verifying, right, is
- 11:46:01actually doing the verification here.
- 11:46:03Correct? And based on it, it's going to
- 11:46:05return also another very important
- 11:46:07concept. Uh where is that? Let's go to
- 11:46:10that
- 11:46:11o routes not routes service.
- 11:46:17Uh see this is where we are actually not
- 11:46:20this one uh
- 11:46:28uh this part only I'm talking about. So
- 11:46:29request dot user equal to verify access
- 11:46:32token right and here what we doing we're
- 11:46:34returning this return verify this is
- 11:46:36what I'm talking about. So actually what
- 11:46:37we doing first we are verifying the
- 11:46:39token if that is correct then only we
- 11:46:41are doing what we are attaching that
- 11:46:43user information in our request object.
- 11:46:45So this one in our request object. So
- 11:46:47that is the reason you're getting this
- 11:46:48information. The moment you will just
- 11:46:50change this token. Let's say if I do uh
- 11:46:52maybe let's do P to A. Okay. And send
- 11:46:55this invalid or expired access token.
- 11:46:58See it will work right and then if you
- 11:47:01let's say copy this again from here
- 11:47:03let's say I'm going to copy this and
- 11:47:04then again replace here you'll see that
- 11:47:06you're going to get the result. So send
- 11:47:09this. That's it. So this is how you're
- 11:47:12going to protect. Okay. Now we haven't
- 11:47:14used this admin middleware because this
- 11:47:16we're going to use later but for now
- 11:47:18let's keep it like this with this. Now
- 11:47:19let's move to the next one. What will be
- 11:47:21the next part? Next part will be we are
- 11:47:22going to implement cloud right cloud
- 11:47:25means how we going to create update
- 11:47:26delete and all those things. And
- 11:47:27remember one thing here I wanted to
- 11:47:29mention before I know some of you are
- 11:47:31going to comment that okay in the O
- 11:47:32routes or O service we haven't
- 11:47:34implemented cookies where is log out and
- 11:47:36all of this we're not going to have all
- 11:47:38of this in this particular video and the
- 11:47:39reason is very simple if you want to
- 11:47:41learn all of this I already have a video
- 11:47:43for this or else wait for the next part
- 11:47:45in the next part we are going to have
- 11:47:46the rest of the topics that I already
- 11:47:48mentioned some of the things that I
- 11:47:50already mentioned in the previous core
- 11:47:51modules that we are going to have child
- 11:47:53process worker threads then some of the
- 11:47:55advanced topics uh here we are going to
- 11:47:58implement some advanced level of uh
- 11:48:00middle so not middleware means login
- 11:48:03flow email verification all all those
- 11:48:05things it's not possible to include each
- 11:48:07and everything here so I wanted to
- 11:48:08mention but for a beginner or even to
- 11:48:10complete a project this is more than
- 11:48:12enough so first let's implement our CRUD
- 11:48:14we are going to understand RBSC means
- 11:48:16role access control we are going to
- 11:48:18implement our admin side logic and then
- 11:48:20we'll move to file upload because there
- 11:48:21are still so many things are left so
- 11:48:23with this now let's move on to the next
- 11:48:24one all right so now what I going to do
- 11:48:26right So we start implementing our CRUD.
- 11:48:28CRUD means create, read, update and
- 11:48:31delete. Okay. Now first of all, let's go
- 11:48:33to types and we will start with creating
- 11:48:35our task uh type. So task.ts.
- 11:48:39Let's do this one as export type. Uh
- 11:48:42let's do task. So for your task, you're
- 11:48:44going to have a ID. So this will be
- 11:48:46string. You're also going to have
- 11:48:49task title which also will be string.
- 11:48:52You're also going to have a status. Now
- 11:48:54this will be string. And also let's see
- 11:48:56what else we have. Title status user id
- 11:48:59created and updated at. Right? So we'll
- 11:49:01copy this. So here we are going to have
- 11:49:03user id. This also will be string
- 11:49:06created at. This will be date. And then
- 11:49:09we are also going to have the updated at
- 11:49:12which also will be date. So this will be
- 11:49:14our type. So now first of all let's
- 11:49:16close everything. So let's see how many
- 11:49:19files first of all we need because we
- 11:49:21need to first check the flow and
- 11:49:24everything. So first of all as a user
- 11:49:26the first requirement will be this user
- 11:49:28has to be logged in user. So that means
- 11:49:31if you haven't logged in you will not be
- 11:49:33able to create any task. So that that is
- 11:49:35the first requirement. Then user will be
- 11:49:37able to create task. So this will be
- 11:49:40first requirement. User can only see the
- 11:49:44task created by that user only. I think
- 11:49:46that part we have already covered
- 11:49:48previously. User can update their task
- 11:49:50and user can delete their task. So four
- 11:49:53uh routes. Okay, from admin side what
- 11:49:57are you going to do? Right, admin will
- 11:49:58be able to see all the task created by
- 11:50:01all the users. So that means let's say I
- 11:50:02have two users. First user created 10
- 11:50:05task, second user created five task. So
- 11:50:07admin will be able to see all these 15
- 11:50:09task together. Okay. And second admin
- 11:50:12only will be able to update task status.
- 11:50:16So admin can update task uh status. Uh
- 11:50:19user can update uh their task title
- 11:50:22something like this. So first of all
- 11:50:23what we going to do right we need to see
- 11:50:25how many repository file we need. So
- 11:50:26first let's start with uh repository. So
- 11:50:29here I'm just going to create uh maybe
- 11:50:31let's do this one as either we can
- 11:50:34create only let's do user dot task dot
- 11:50:38repository.
- 11:50:42And then I'm also going to create one
- 11:50:43more. So this will be admin.task
- 11:50:46repository.ts.
- 11:50:48So one for user one for admin. Correct.
- 11:50:52Then we are also going to need our task
- 11:50:54services. So we'll go to our services.
- 11:50:56I'm going to create user dot task dots
- 11:51:00service.ts.
- 11:51:02Then I'll do admin.task dots service dot
- 11:51:06ts. Then we need our routes. Correct. So
- 11:51:10in the routes we are going to have two
- 11:51:11routes only. Uh the first one will be
- 11:51:13same. So we do user.task dot routes. DS
- 11:51:18and I'll do admin.task.outs.
- 11:51:21is. So then first of all let's start
- 11:51:24with again from our routes file. So
- 11:51:26let's close everything else go to index
- 11:51:29and let's create our first uh route
- 11:51:31which will be our task route for user.
- 11:51:34So here the first of all let's import
- 11:51:36okay then we are going to do so first
- 11:51:38import something from
- 11:51:40uh express
- 11:51:42and here we are going to first take I'll
- 11:51:45do export const and this will be our
- 11:51:48user task router. Okay. And this will be
- 11:51:51our router. Now first what we have to
- 11:51:53see right that all the task whatever
- 11:51:56operation that user is going to do so
- 11:51:59that user has to be logged in user. So
- 11:52:01that means whatever routes we are going
- 11:52:02to have in this particular file all
- 11:52:04routes has to be protected by our
- 11:52:06authenticated middleware. Authenticate
- 11:52:08middleware you're getting. So this one
- 11:52:10[snorts] we are going to understand uh
- 11:52:11in a minute. First let's do one thing.
- 11:52:12So I'm just going to do here uh let's go
- 11:52:15here. Right. So we'll go to our index.ts
- 11:52:17and here what are you going to do? So
- 11:52:19first of all you have to use this one.
- 11:52:20So I'll do app router
- 11:52:23sorry API router dot use and here let's
- 11:52:26give this one as uh let's do tasks and
- 11:52:30here I'm just going to do user task
- 11:52:32router. So that is the first thing. Now
- 11:52:34I'll go here. First of all we going to
- 11:52:36create our first route which will be how
- 11:52:38we are going to create one particular
- 11:52:40task. So I'm just going to do task user
- 11:52:43task router dot post. Let's give this
- 11:52:47one slash. So this will be our home
- 11:52:49route and here we're going to get a
- 11:52:51sync. So do request response and next
- 11:52:56and here so let's take our try catch.
- 11:53:01So you error
- 11:53:03and then we are going to call the next
- 11:53:05without error. So that is fine. But here
- 11:53:08what we are going to do right so first
- 11:53:09I'm just going to do con task and I'm
- 11:53:12just going to have it. So now we are
- 11:53:14going to create our service. So we'll go
- 11:53:15to our user service and here we're going
- 11:53:17to have our first service which will be
- 11:53:19create user task and what we are going
- 11:53:23to pass here. So first of all we're
- 11:53:24going to pass two things. Now you need
- 11:53:26to understand a couple of things here.
- 11:53:27Okay as I already mentioned that this
- 11:53:29router this route has to be protected.
- 11:53:31So here there are two ways you can do
- 11:53:34this. Now this is one of the most
- 11:53:35important concept you have to
- 11:53:36understand. First of all let's
- 11:53:37understand you have two routes here. One
- 11:53:40route has to be protected and other
- 11:53:41routes not. So in that case what you can
- 11:53:43do right you can use this authenticate
- 11:53:45middleware that we have on the route
- 11:53:47level only why because you can't use
- 11:53:50this one on the page level sorry on the
- 11:53:51file level because let's say you have
- 11:53:53one more here
- 11:53:55right you have one more and this
- 11:53:57particular route don't need this
- 11:53:58authenticate so in that case what you
- 11:54:00can do you can use this one individually
- 11:54:01you're getting but let's say now you
- 11:54:03have one more route and this route also
- 11:54:05need authenticate middleware so in that
- 11:54:07case instead of doing this one two times
- 11:54:09so one time here again one time here we
- 11:54:12can do something like this. We can use
- 11:54:13this one on the file level. Okay. So
- 11:54:16what I mean by this now if I go here and
- 11:54:18I'm simply going to tell okay use a task
- 11:54:20router dot use this authenticate. So
- 11:54:23simply we are telling that whatever
- 11:54:25routes I'm going to have in this
- 11:54:26particular file it can be two routes 10
- 11:54:28routes 20 routes all the routes will be
- 11:54:31now protected by the middleware. So now
- 11:54:34what you have to do right you have to
- 11:54:35use this one on the page level top top
- 11:54:37file. So not top file on the file on the
- 11:54:39top on the top you have to use. So
- 11:54:42whatever you are going to use after this
- 11:54:44all these routes so for example this
- 11:54:45route this route and you have let's say
- 11:54:4750 more routes all will be protected by
- 11:54:50authenticate authenticate middleware.
- 11:54:52Okay so that means all these routes are
- 11:54:54now protected. So all the routes below
- 11:54:58are now protected as simple as that. So
- 11:55:01this is how you can you can use or you
- 11:55:03can protect routes. As I already
- 11:55:05mentioned, I'm going to repeat once
- 11:55:06again. Let's say the requirement is you
- 11:55:08have to protect this one. You don't have
- 11:55:10to protect then you can't use this one
- 11:55:12because this will apply for all the
- 11:55:13routes. So in that case, you have to use
- 11:55:14this one on the route level. Something
- 11:55:16like this. So for this one, you need
- 11:55:18this for this you don't and then you
- 11:55:19have to remove this one. Okay. So for
- 11:55:21now because we need this particular
- 11:55:23authenticate middleware for all the
- 11:55:25routes, we are not going to use this one
- 11:55:27on the route level. Okay. So now let's
- 11:55:29remove this. So first of all, we are
- 11:55:31going to create our user task. Correct.
- 11:55:33So now we'll go to our service file and
- 11:55:35let's create our first service. So here
- 11:55:37I'm just going to do export async
- 11:55:40function and this will be create user
- 11:55:42task. What we need right? So first of
- 11:55:44all we need to know that which user
- 11:55:46actually creating this particular task
- 11:55:47and that is the reason we have this uh
- 11:55:50user ID right. So we have this user ID.
- 11:55:52So that means this one needs to have the
- 11:55:54user ID correct. So we are going to need
- 11:55:56our user ID. So user ID will be string
- 11:55:58and also we need the title. So title and
- 11:56:01this will be unknown. Now here what's
- 11:56:04this is going to return? So this is
- 11:56:05going to return promise and it's going
- 11:56:07to return task type that we have created
- 11:56:09in our task uh in the types file. Okay,
- 11:56:11remember we have created this one. So
- 11:56:13now what we going to do right? So first
- 11:56:14of all let's do validation. So first
- 11:56:17we're going to do con
- 11:56:19and let's do valid title. We are only
- 11:56:23checking the title here and this will be
- 11:56:25let's create one function. So I'm just
- 11:56:27going to do function and I'll do
- 11:56:28validate or valid title. Now let's do
- 11:56:31validate title and this is going to uh
- 11:56:34receive your title. This will be unknown
- 11:56:37and here you're going to use string. Now
- 11:56:39as I already mentioned we are actually
- 11:56:41going to use zord only but here you also
- 11:56:44need to know how you can implement all
- 11:56:45of this manually. So first I'm going to
- 11:56:48use this validate title. We are going to
- 11:56:50pass this user ID right not user ID
- 11:56:52title. So here what I'm going to do
- 11:56:54simply I'm going to check that if this
- 11:56:56type of title
- 11:56:59if this is not equal to equal to string
- 11:57:03or if this is false so that means if
- 11:57:06title dot trim
- 11:57:08in that case we are going to throw new
- 11:57:11app error and we're going to tell that
- 11:57:13okay this is 400 and I'm simply going to
- 11:57:15do title is required okay and now I'm
- 11:57:20simply going to do con trimmed title
- 11:57:23And this will be title dot trim.
- 11:57:27And then what you can do right that you
- 11:57:28can also give some condition. For
- 11:57:29example, let's say I wanted to check if
- 11:57:31this trimmed title.length
- 11:57:34if this is greater than maybe let's give
- 11:57:36a max title length of let's say 150 or
- 11:57:39100 something like this. So in that case
- 11:57:40also we can throw one more error. So
- 11:57:42I'll do throw new app error and I'm
- 11:57:44simply going to do 400. And I'll do
- 11:57:47title must be 100
- 11:57:50characters or less. Right? Now this also
- 11:57:54you can put this one in a constant file.
- 11:57:56Remember I told you to keep this one as
- 11:57:57a homework. So that you can do and then
- 11:57:59we are going to return the streamed
- 11:58:01title. Okay? So now this is going to
- 11:58:03validate title. Now what we're going to
- 11:58:05do right we are simply going to return
- 11:58:07and we are actually going to now
- 11:58:09interact with our DB. Now when I'm
- 11:58:10talking about interact with our DB so
- 11:58:13that means we need to at the end save
- 11:58:15this particular type support task in our
- 11:58:17DB correct so now we have to go to our
- 11:58:20repository file so we'll go to user.task
- 11:58:23task dot repository and we'll create our
- 11:58:25first function. So I'll do export async
- 11:58:29function and I'll do create task and
- 11:58:32here we are going to do user ID. We need
- 11:58:34the user id that which user is actually
- 11:58:36creating. So this will be string we also
- 11:58:38need the title of that task and this
- 11:58:40also will be string and here what will
- 11:58:43be the return type return type will be
- 11:58:44promise of this task.
- 11:58:47So now we are going to use this here. So
- 11:58:49this will be our return and you can do
- 11:58:51create task and what this is going to
- 11:58:53receive first of all let's import this
- 11:58:55and this is going to receive your user
- 11:58:57ID and title. So I'm just going to pass
- 11:58:59the user ID and I'm going to pass a
- 11:59:01valid title. Okay. So save this. So now
- 11:59:04what we have to do here? So simply going
- 11:59:06to create our SQL uh we have to write
- 11:59:09our query. So cool dot query and here
- 11:59:13you can do this one as task only. If you
- 11:59:16want you can also do like this. So you
- 11:59:18can do a type and let's do task row and
- 11:59:20this will be equal to this task and you
- 11:59:22can pass this also. Remember I told you
- 11:59:23that the DV for format and the actual
- 11:59:26format that you want to to return that
- 11:59:28that can be different but in this case
- 11:59:29it's same. So I'm just simply taking
- 11:59:31this only and here now I'm just going to
- 11:59:33write so what we have to write so we are
- 11:59:36going to insert. So I'm just going to do
- 11:59:38insert into and let's check our uh so
- 11:59:42this will be support task. This is where
- 11:59:44we are inserting this our table. What
- 11:59:46are you going to insert? You're going to
- 11:59:47insert title and also you're going to
- 11:59:49insert user ID. Correct? Then I'm just
- 11:59:52going to do values. So values will be
- 11:59:54one and two. And then I wanted to do a
- 11:59:58returning.
- 12:00:00So I'll return ID. I wanted to return
- 12:00:02the title. I wanted to return the
- 12:00:03status. Why we are not taking the
- 12:00:05status? Because by default status will
- 12:00:07be open. Okay. So you take status. Then
- 12:00:10I wanted to return the user ID.
- 12:00:13Then I want to uh return the create edit
- 12:00:15and update.
- 12:00:20That's it. And then uh what I have to
- 12:00:23do? So here after this I'm going to pass
- 12:00:26the title
- 12:00:29and the user ID.
- 12:00:31So I think this is done. And at the end
- 12:00:33we're going to return result dot rows of
- 12:00:37zero.
- 12:00:38So save this. So I think this is done.
- 12:00:41So now if we go here so what you have to
- 12:00:44use we have to use this create task user
- 12:00:46task here. So I'm going to import from
- 12:00:47our service and this is going to receive
- 12:00:50the user ID and the title. Now from
- 12:00:52where you're going to pass this user ID.
- 12:00:54Now this is where you have to understand
- 12:00:55from this request remember we have this
- 12:00:57request and this request is going to
- 12:00:59have your user information and this is
- 12:01:01going to give you the user ID. Now this
- 12:01:04we are going to actually get remember
- 12:01:05when we have validated the token right
- 12:01:08that time we are attaching this user
- 12:01:09information in our request object. So
- 12:01:11from where we'll be able to get this
- 12:01:12user id and then this will be request
- 12:01:14dot body dot title
- 12:01:18and now what you can do you can simply
- 12:01:20pass a dot status of 201
- 12:01:23JSON and here I'm just going to do
- 12:01:25success
- 12:01:27will be true and let's pass a data. So
- 12:01:30data will be this task that we have
- 12:01:32created. Save this. Now let's run it
- 12:01:35once again. See that this is running. So
- 12:01:38now first of all let's go here. Okay. So
- 12:01:39I'm just going to copy this.
- 12:01:42So I'll do post and I'll paste it. So
- 12:01:45this will be app API/ what is the route
- 12:01:48name that we are using. So let's check
- 12:01:51routes routes index. So we'll do / task.
- 12:01:55So / API/ task. Why? Because if you go
- 12:01:59to user task we are using only slash
- 12:02:01right so this will be / task and this
- 12:02:03will be post request we go to body json
- 12:02:05and let's I'm going to pass the title
- 12:02:08right so we have the title
- 12:02:12and this will be first
- 12:02:15support task correct let's try to send
- 12:02:18this you can see access token is
- 12:02:20required now from where you are actually
- 12:02:22getting you have to understand all this
- 12:02:24see because of this line you are getting
- 12:02:25this this authenticate is now is going
- 12:02:28to protect all the routes. So if you go
- 12:02:29to this authenticate, you'll see that
- 12:02:31it's simply checking whether you have
- 12:02:32the access token or not. So this is
- 12:02:34going to block the request. Correct? So
- 12:02:36now what you have to do right, you have
- 12:02:37to go to our login. Let's send this.
- 12:02:39We're going to get the token. So copy
- 12:02:41this token.
- 12:02:45Let's copy this one from here. And now
- 12:02:48if I go to authorization or let's go to
- 12:02:50headers, add one more. So I'm just going
- 12:02:52to add authorization and let's add a
- 12:02:54beer right space. So now let's try to
- 12:02:58send and that's it. So now you have your
- 12:03:01ID title is created status is open by
- 12:03:03default. This is your user ID. The user
- 12:03:05ID that created now you see this user ID
- 12:03:08and if you go to this user ID that
- 12:03:10particular user ID both will be same.
- 12:03:11Right? So for example if you go check
- 12:03:13this one B 0 A and check this one B 0 A.
- 12:03:18So that means this user actually created
- 12:03:20this particular task. So we'll be able
- 12:03:22to actually track that which support
- 12:03:24task is created by which user you're
- 12:03:26getting this user ID and this particular
- 12:03:28user ID both are same. So this about the
- 12:03:32creating. So creating is actually
- 12:03:33simple. Now remember you might be
- 12:03:35thinking I I know that some of you will
- 12:03:36be thinking oh this is very simple. I'm
- 12:03:38just adding one particular title. It
- 12:03:41doesn't like that. What you can do you
- 12:03:43don't have to exactly do like this. Now
- 12:03:45I'm adding only title. What will be your
- 12:03:48homework? Instead of adding only title,
- 12:03:50you can add 10 more properties here. But
- 12:03:53at the end, you have to understand this
- 12:03:55one that the process will remain same.
- 12:03:57Doesn't matter how many properties you
- 12:03:58are going to have here. At the end, the
- 12:04:00process or the flow will remain same. If
- 12:04:03you understand this flow, you will be
- 12:04:05able to create any kind of project.
- 12:04:06Okay. So with this now, let's move to
- 12:04:08the next one which will be our uh what
- 12:04:10is that? Uh next will be get fetching
- 12:04:13all the um task. So for this one we are
- 12:04:16going to do user task router get. So
- 12:04:19this will be slash and I'll do async.
- 12:04:22Let me just copy this one from here.
- 12:04:26And then we are simply going to take a
- 12:04:28try catch
- 12:04:33and then we're going to simply pass the
- 12:04:35error. And here what are we going to
- 12:04:38get? We are simply going to get all our
- 12:04:39task right? So I'm just going to do con
- 12:04:41tasks and I'll do a and I'll do get user
- 12:04:46tasks. Okay. So now first of all when
- 12:04:50we're going to create this service so
- 12:04:51let's go to service and let's create
- 12:04:53this. So I'm just going to do
- 12:04:55export async function and I'm simply
- 12:04:59going to do get user tasks. What
- 12:05:03actually you need? Now here you have to
- 12:05:05understand all of this. when you are
- 12:05:07going to get the task you need to pass
- 12:05:09this user ID because let's say there are
- 12:05:11two user A and B A created five task B
- 12:05:13created 10 task and you only need the
- 12:05:16created the task that is created by A.
- 12:05:18So in that case you need to know this
- 12:05:19user ID then only you'll be able to get
- 12:05:21it correct. So that means this get user
- 12:05:23task now need the user ID. So you are
- 12:05:26going to get the user ID which will
- 12:05:27string what this is going to return it
- 12:05:29is going to return a promise of task and
- 12:05:31this will be array because user can
- 12:05:33create multiple task also and here now
- 12:05:36we are going to return what we're going
- 12:05:37to create one more repository function
- 12:05:40here right here because we need to get
- 12:05:41the task from our DB only correct. So
- 12:05:44I'm just going to do export as sync
- 12:05:46function and I'm just going to do fetch
- 12:05:48task by user ID by user ID. Okay. So you
- 12:05:53need the user id this will be string and
- 12:05:57here what you're going to do you're
- 12:05:58going to do promise and this will be so
- 12:06:01we are going to get task. So now what
- 12:06:04you're going to do that simply will do
- 12:06:06const result.
- 12:06:08So this will be a pull dot query. So
- 12:06:12we're going to
- 12:06:14uh so this will be task row and now
- 12:06:16we're going to write our query. So I'll
- 12:06:18do select.
- 12:06:20So I'll do select ID title user ID. No
- 12:06:25uh we have a status status user ID
- 12:06:30created at
- 12:06:32and updated at. So all these I need from
- 12:06:35where? From our table. The table name is
- 12:06:38support task. So we'll do support tasks
- 12:06:42correct. So from that this table where
- 12:06:45based on what we are checking based on
- 12:06:47user ID. So where this user ID
- 12:06:50will be equal to this right. And then
- 12:06:53I'm also going to do a order by. So
- 12:06:56let's do order by of created at
- 12:06:59let's do disk. Oh no this will be wrong.
- 12:07:02Create at and this will be disk. And
- 12:07:04here you're going to simply pass a user
- 12:07:06ID. That's it. And then what you going
- 12:07:08to do? You have to return result dot
- 12:07:10rows. Why? Because there can be multiple
- 12:07:13task also. So save this. So now what
- 12:07:16you're going to do, right? We'll go to
- 12:07:17our uh task service.
- 12:07:20So here we're going to call this fetch
- 12:07:21task by user ID
- 12:07:24and we're going to pass the user ID.
- 12:07:26This is done. So now we'll go to our
- 12:07:27routes and then we'll import this one
- 12:07:29from our service. And what this is going
- 12:07:31to receive? This is going to receive the
- 12:07:32user ID. So you do request dot user
- 12:07:37dot user id. That's it. And then you're
- 12:07:40going to simply return. So you do race
- 12:07:42dot status of 200 dot JSON. And here now
- 12:07:48we're going to return success will be
- 12:07:49true. And we're going to return the data
- 12:07:52which will be these tasks. And this is
- 12:07:55done. So now this particular route also
- 12:07:57will be protected. Okay. So let's see
- 12:08:00this one whether this is working or not.
- 12:08:02So let's go here and then I'm going to
- 12:08:04just copy this create one more and this
- 12:08:07will be get and now let's do first of
- 12:08:08all let's do one thing let's try to send
- 12:08:10access token is required correct so we
- 12:08:13go to our login let's create one token
- 12:08:16we're going to copy this we'll go here
- 12:08:19let's go to headers let's add
- 12:08:21authorization
- 12:08:24let's add ber
- 12:08:27paste this and now see what will happen
- 12:08:28if I send this so we're getting the task
- 12:08:31created
- 12:08:32Now you might be thinking how it is
- 12:08:34getting the user ID. Correct. Remember
- 12:08:36this is actually getting the user ID
- 12:08:37from your request object and this
- 12:08:39request object from where you are
- 12:08:40getting we are actually getting from
- 12:08:42this token because at the end you are
- 12:08:44validating the JWT token right. So if
- 12:08:46you go to post here and let's say if I
- 12:08:47try to add one more so let's do second.
- 12:08:51So it's created one and if you try to go
- 12:08:53and send this now you're getting two
- 12:08:55task right. So first support task and
- 12:08:57second support task. Fantastic. Right.
- 12:08:59So that means fetching all the
- 12:09:03what that fetching all the task is also
- 12:09:05working. So now what you're going to do
- 12:09:06right we'll create our third one which
- 12:09:08is how we going to fetch a single task
- 12:09:10because now what is happening right we
- 12:09:12are getting all the tasks. So we need to
- 12:09:13fetch one single task. Okay. So now
- 12:09:16let's go here and let's see how we are
- 12:09:17going to do that. So first of all let's
- 12:09:19do one thing. I'm just going to do user
- 12:09:21task router dot get. Now what you have
- 12:09:24to do right remember here you are going
- 12:09:26to understand how you can create a
- 12:09:27dynamic API route. Dynamic API route we
- 12:09:31have to create like this. So we have to
- 12:09:32do task ID. Now what actually we're
- 12:09:34doing here? We're simply telling this is
- 12:09:35a dynamic parent. Why? Because when this
- 12:09:38particular user let's say created 10
- 12:09:40task each every t each and every task
- 12:09:42will have separate ID. So that means
- 12:09:44this task ID will be different. For
- 12:09:47example, your first task ID is one,
- 12:09:50second task ID is three, 2, 3, four,
- 12:09:52like this. Right? So when I wanted to
- 12:09:54get only fifth task, I need to replace
- 12:09:56this one with / five something like
- 12:09:58this. So that is the reason I'm sending
- 12:10:00a colon and then the param or query
- 12:10:03sorry parameter name here that will be
- 12:10:05dynamic right. So here now what I can do
- 12:10:07right I'll simply do async and this will
- 12:10:10be request response next.
- 12:10:15And now I'll just do try catch.
- 12:10:21So I'll do next call the error. And in
- 12:10:24this try so first of all I'm just going
- 12:10:25to do con task because we are fetching
- 12:10:28single task. So do ait. So do get user
- 12:10:32task by id.
- 12:10:37Okay. So what all we have to pass here.
- 12:10:39So first of all before this also let's
- 12:10:41do one thing. Let's return this. I'll do
- 12:10:42uh race dot status of 200 dot JSON. So
- 12:10:47here I'm just going to do success will
- 12:10:49be true and data I'm just going to pass
- 12:10:51this task here. But this is not yet
- 12:10:53completed. So first we have to go and
- 12:10:55fetch the single task. Correct. So go to
- 12:10:57service file and then we are going to
- 12:10:59create one more. So I'm just going to do
- 12:11:01export async function and I'm simply
- 12:11:04going to do get user task
- 12:11:07task
- 12:11:10by ID. Okay. Get user task or fetch user
- 12:11:14task. What we are using here? Get user
- 12:11:16task. I think it is fine. Get user task.
- 12:11:19That's fine. Get user task by ID. It's
- 12:11:21okay. Now here what are the things you
- 12:11:24need? Can you guess? First of all, you
- 12:11:26need to get the user ID. Second, you
- 12:11:29need to get the task ID. Because let's
- 12:11:31say if I try to get this particular
- 12:11:32task, you need this ID. Also, you need
- 12:11:36which user actually created this because
- 12:11:38it's actually better. Okay, you can
- 12:11:40actually face this use in this also. But
- 12:11:42you need the user ID here also. Okay, so
- 12:11:44that means you need two things. So first
- 12:11:45you need the task ID which will be
- 12:11:48string and also you need the user ID
- 12:11:51which also will be string. What this is
- 12:11:54going to return? So it's going to return
- 12:11:55a promise of the single task. So it will
- 12:11:57be task. And now what I'm can do right I
- 12:12:00can do const and this will be task and
- 12:12:02here I'm just going to do ait and I'll
- 12:12:04do find
- 12:12:06task by
- 12:12:09ID find task by ID and user ID. Okay.
- 12:12:14And here we're going to pass the task ID
- 12:12:16and we are also going to pass the user
- 12:12:17ID. So this one now we are going to
- 12:12:19create where again in our repository
- 12:12:21file because we at the end we need to f
- 12:12:23this one from our
- 12:12:25DB. So I'll do export as sync function.
- 12:12:28So do fetch task uh by ID and user id.
- 12:12:32So you need the task ID. This will be
- 12:12:34string. And also you need the user ID.
- 12:12:38This also will be string. So what this
- 12:12:41is going to return? This is going to
- 12:12:42return a promise of task
- 12:12:44or it can return null because it's
- 12:12:46possible that you are giving some random
- 12:12:48ID and that data is not present. Okay.
- 12:12:50So now I'm just going to do con result.
- 12:12:53So this will be have it pull dot query.
- 12:12:57So we're going to pass the task row and
- 12:13:00here we are going to now write our
- 12:13:02query. So we'll do
- 12:13:04select
- 12:13:07ID, title, status,
- 12:13:10user ID,
- 12:13:12created at and updated at. So all these
- 12:13:15thing we need from where? From our
- 12:13:18support task table. So do from support
- 12:13:20task
- 12:13:22and then we are going to do where id
- 12:13:25will be this. Okay, ID means your actual
- 12:13:30ID, the task ID. And you also need the
- 12:13:33user ID where user ID is equal to this.
- 12:13:36And then you're going to simply pass
- 12:13:37your ID means this will be your task ID.
- 12:13:39And then you're going to have your user
- 12:13:41ID. Very simple. And then you're simply
- 12:13:44going to return
- 12:13:46result dot rows of zero. But here it is
- 12:13:50possible. So you can also return null
- 12:13:51here. Okay, if that is not present.
- 12:13:53That's it you have to do here. So now we
- 12:13:55are going to import this in our service
- 12:13:57file and then we are going to give a
- 12:14:00very very important check because if
- 12:14:02this task is not present so in that case
- 12:14:04what I going to do right will throw new
- 12:14:06app error and I'm just going to do a 404
- 12:14:10and I'll do task not found. Okay and at
- 12:14:14the end we going to return the task.
- 12:14:17That's it. So this about our service.
- 12:14:20Now we go to here. So we need to first
- 12:14:22import this from our service and then
- 12:14:25what you need we need to was the task ID
- 12:14:26and the user ID. So task ID will be your
- 12:14:29this ID. But how you're going to get
- 12:14:31this ID now? So this ID you're going to
- 12:14:33get from request and request is going to
- 12:14:35give you these params and these params
- 12:14:36is going to have your task ID. See this
- 12:14:40is going to have your task ID and this
- 12:14:41this depend on what you're giving here.
- 12:14:43For example, if I do ID, see you're
- 12:14:45getting error. Why? Because now you have
- 12:14:46to give task ID you're getting. So
- 12:14:48that's the reason we just change this
- 12:14:50one to task ID. And here I'm simply
- 12:14:52going to do user ID. You already know.
- 12:14:54So do request dot user dot user ID. And
- 12:14:57this is done. So now let's save this and
- 12:15:00let's say if I just go here and let's
- 12:15:03create one more. So what I'll do right.
- 12:15:04So let's copy this
- 12:15:07first. Let's go here. So we're going to
- 12:15:09do this. And now what I have to do, we
- 12:15:11have to pass the dynamic value, right?
- 12:15:13So / task ID. So maybe let's take this
- 12:15:16task one first one. Okay. So this ID you
- 12:15:18wanted to take and I'll do / this. And
- 12:15:23now what I'm going to do right I'm going
- 12:15:24to send here and you can see that we are
- 12:15:25getting access token is required which
- 12:15:27is uh expected because we need to pass
- 12:15:29our token. So we go to our login
- 12:15:33get our token
- 12:15:36and let's go to headers add
- 12:15:40authorization and I'm simply going to
- 12:15:43add a beer
- 12:15:48and paste and now let's try to send and
- 12:15:52that's it. So you're getting success and
- 12:15:54you're getting this data. Okay. So now
- 12:15:56let's see go here and maybe let's I try
- 12:15:58to add this copy this and if I go here
- 12:16:01let's change this one to this send and
- 12:16:04you're getting second task and let's say
- 12:16:06if I just change this I uh ID to a
- 12:16:08different ID send this you're getting
- 12:16:10task not found that means you're getting
- 12:16:12404 and that is coming from your service
- 12:16:15from this line okay so this is completed
- 12:16:18so now next one will be update right so
- 12:16:20let's say how we are going to do update
- 12:16:22so I'm going to create one more and this
- 12:16:24time we're going to use not get not post
- 12:16:27we are going to use patch because we are
- 12:16:29only going to update a certain uh
- 12:16:31portion or certain property in our DB so
- 12:16:33you don't have to update all the
- 12:16:34properties right so in this case when
- 12:16:36you're going to patch something let's
- 12:16:38say uh if I go here let's say one task
- 12:16:41two task three task and in this
- 12:16:43particular task you wanted to update
- 12:16:45some property let's say you wanted to
- 12:16:46change the name from sum to John so in
- 12:16:49that case you need to also know the
- 12:16:51which ID you wanted to update right so
- 12:16:53that means these al will be a dynamic
- 12:16:55value. So in this case also what we
- 12:16:57going to do? We are actually going to
- 12:16:59tell that okay this ID we wanted to
- 12:17:01update right. So this task ID will be
- 12:17:03what again dynamic value you're getting
- 12:17:05and then we are going to take async.
- 12:17:08So we're going to get request response
- 12:17:11next
- 12:17:13and here we are going to do a try catch
- 12:17:17error and then we're going to do next
- 12:17:19and we're going to call out error. So
- 12:17:21first of all what we have to do. So
- 12:17:23first let's do a const and we're going
- 12:17:25to take task and I'll do a and I'll do
- 12:17:28update user task. Okay. So what are the
- 12:17:32things that you need here? So this is
- 12:17:33the first thing. See first of all you
- 12:17:35need to know which task you wanted to
- 12:17:36update. So that means you need the task
- 12:17:38ID. Second you need which user actually
- 12:17:41created that particular task. So that
- 12:17:43means you also need the user ID. And
- 12:17:45that makes sense. Correct? Let's say I
- 12:17:46wanted to update this task from second
- 12:17:50support task to second support task
- 12:17:52edited. So that means I need to change
- 12:17:54this title. Correct? So that means I
- 12:17:56need sorry I need this title. I need
- 12:17:59this user ID and also I need this title
- 12:18:02also. Why? Because consider you are
- 12:18:04having a UI and from this UI user is
- 12:18:07going to update right? So that means
- 12:18:09this title value you need the updated
- 12:18:11title value. Consider I wanted to change
- 12:18:14this uh second support task to sum
- 12:18:17something like this. So that means I
- 12:18:19need that updated value correct then
- 12:18:20only you'll be able to understand okay I
- 12:18:22need to update the title but what will
- 12:18:24be the value that you don't know so that
- 12:18:26you're going to get from your UI side
- 12:18:28right from your front end so that means
- 12:18:30in this one we need three things but
- 12:18:32before that let's do go to our service
- 12:18:34and let's first create this. So here
- 12:18:36what I'm going to do right I'm simply
- 12:18:37going to do export as sync function and
- 12:18:40here I'm just going to do update user
- 12:18:43task. So we need the task
- 12:18:46we need the task ID which will be
- 12:18:48string. We also need the user ID which
- 12:18:51will be string and also we need the
- 12:18:53title that will be also string. What
- 12:18:56this is going to return again it's going
- 12:18:57to return promise of the task.
- 12:19:00So first of all I want to do valid
- 12:19:02right? I wanted to do valid title and
- 12:19:04this will be our validate title and I'm
- 12:19:06going to pass the title and now what I'm
- 12:19:09going to do first I'm going to do con
- 12:19:11task and I'll do update task
- 12:19:15title and this one we are going to do
- 12:19:18where we are going to write in our
- 12:19:19repository because at the end you have
- 12:19:20to remember you have to interact with
- 12:19:22your DB only right so if you go here and
- 12:19:25let's create one more so I'm just going
- 12:19:26to do export async function and I'm
- 12:19:30simply going to do update task ask title
- 12:19:33this is also going to receive these
- 12:19:35three things I'm not going to write
- 12:19:36let's copy and paste and then here
- 12:19:42we are going to do promise and I'm
- 12:19:43simply going to do this will be either
- 12:19:45task or else this will be null again why
- 12:19:47because let's say you are updating one
- 12:19:48particular task that task is not even
- 12:19:50present so in that case this will return
- 12:19:52null correct so I'm simply going to do
- 12:19:55con result so I do ait pool dot query so
- 12:19:59we're going to pass the task row and
- 12:20:01then try to write the query by yourself
- 12:20:03because by this time I hope that you
- 12:20:05completed the postgress section and also
- 12:20:07you have written multiple uh query also
- 12:20:09so I think you should do all of this how
- 12:20:11you going to update so here you can't
- 12:20:13use select correct so you have to do
- 12:20:14update where you wanted to update we
- 12:20:16wanted to update our support task table
- 12:20:20right so now what we have to update so
- 12:20:23you have to update the title property so
- 12:20:25I'm going to do set which property title
- 12:20:28which will be equal to one and then I'm
- 12:20:31Just going to do updated at this will be
- 12:20:34equal to what now because the when
- 12:20:37you're updating that time you also you
- 12:20:39need right that time stamp now what
- 12:20:41you're going to do where now you're
- 12:20:42going to tell okay because your DB
- 12:20:44doesn't know right okay which item I
- 12:20:46need to update let's say you have 10
- 12:20:48task and you wanted to update the fifth
- 12:20:50one so you need to tell right okay where
- 12:20:52the ID will be this
- 12:20:54and user ID will be this so simply I'm
- 12:20:58telling that okay you wanted to update
- 12:21:00this particular First of all, you wanted
- 12:21:02to update support task table. You wanted
- 12:21:04to update the title property. You wanted
- 12:21:07to update the updated property. But how
- 12:21:08we are going to understand which title
- 12:21:10where the ID is this and user ID is this
- 12:21:13mean this means user ID means this task
- 12:21:16is created by that particular user. And
- 12:21:18this is the title ID where I wanted to
- 12:21:20update. And now what I'm going to do I'm
- 12:21:22going to do a returning and this will be
- 12:21:25ID title status user ID
- 12:21:30create or let's write it man copy this
- 12:21:32from here created and updated at these
- 12:21:34two things so here I need this that's it
- 12:21:37and then you're going to password so
- 12:21:39first of all what is our first one first
- 12:21:40variable so which is title so this will
- 12:21:43be your title then next is your ID so
- 12:21:46this will be your task ID don't make
- 12:21:47this mistake okay all of these and last
- 12:21:49one is your user ID and This will be
- 12:21:51your user ID. And then at the end you're
- 12:21:54going to return result dot rows of zero
- 12:21:58or else
- 12:22:02this will be null.
- 12:22:04So save this. I think this is done. So
- 12:22:06now we'll go to our service means task
- 12:22:10service. And now here what we're going
- 12:22:12to do. So first of all let's import this
- 12:22:14and we're going to pass the so we have
- 12:22:17task ID, user ID and title. So first I'm
- 12:22:19going to pass the task ID, user ID and
- 12:22:21the title. And then I'm going to give a
- 12:22:24check that if this task is not present
- 12:22:26or we have to pass a valid title, not
- 12:22:28this one. Task is not present. So I'm
- 12:22:30just going to throw new app error.
- 12:22:33So 404 and I'll do task not found. And
- 12:22:38also this is going to return null also.
- 12:22:40So in that case I'm simply going to
- 12:22:42return the task here.
- 12:22:45So save this. So now I'll go here and
- 12:22:47then you're going to copy sorry import
- 12:22:49this first and this update task is going
- 12:22:51to receive your task ID. So how you
- 12:22:52going to get the task ID? Task ID will
- 12:22:54be your request dot params dot task ID.
- 12:22:58Next you're going to pass the user ID.
- 12:23:00User ID will be request dot user dot
- 12:23:04user ID and then you're going to pass
- 12:23:06the title. Title will be from where
- 12:23:07you're going to get. So title you're
- 12:23:09going to get from request dot body dot
- 12:23:12title. That's it. Save this. And then at
- 12:23:15the end what we going to do? We're going
- 12:23:16to return race dot status of 200. So in
- 12:23:20this case also we're going to return 200
- 12:23:23dot JSON and we're going to do success
- 12:23:26will be true and data will be task
- 12:23:31this task. Save this. So now if we go
- 12:23:34here right so let's say maybe I wanted
- 12:23:36to update this task only. So I'm just
- 12:23:38going to copy this add one more paste it
- 12:23:42here. This will be patch and let's say
- 12:23:44try to okay first of all I'm just going
- 12:23:46to add one
- 12:23:48here I'm just going to update the title
- 12:23:51so title I have second support task
- 12:23:55correct so let's change this one to
- 12:23:57second support task title edited okay
- 12:24:03try to send first we're getting access
- 12:24:05token is needed so first let's log get
- 12:24:09the token
- 12:24:12And then I'll go to headers add a
- 12:24:15authorization
- 12:24:17and I'm going to do be error
- 12:24:20and then try to send and you can see
- 12:24:23that now you're getting second support
- 12:24:24task is edited. Okay. So that means this
- 12:24:27is working fine. Now try to fetch all
- 12:24:29the task. Okay. So I'll go here send
- 12:24:31this. So it's invalid. So okay let's
- 12:24:33change the token first
- 12:24:36paste here and then try to send and you
- 12:24:39can see that now you're getting second
- 12:24:40support task edited. So that mean this
- 12:24:42is working fine. If you go here right
- 12:24:44try to send this you're getting second
- 12:24:46support task edited. All right. So I
- 12:24:47think this is completed. So the last
- 12:24:49step the last one that is left and that
- 12:24:51will be delete. Okay. So with this crowd
- 12:24:54will be completed. So create update uh
- 12:24:56read and delete. So let's do the delete
- 12:24:58in the next part. Okay. So now let's
- 12:25:00implement our uh delete one right. So
- 12:25:02for delete what we'll do? We'll do use a
- 12:25:04task router and we going to use the
- 12:25:06delete method. Here also when you're
- 12:25:08going to delete so we are going to
- 12:25:09delete a certain support task correct so
- 12:25:12in this case also this will be dynamic
- 12:25:15uh so here uh task ID and then let's
- 12:25:18take a sync and let's copy this
- 12:25:26and here we are going to take a try
- 12:25:29catch
- 12:25:32so call next with error and here in the
- 12:25:35try so simply we're going to do private.
- 12:25:37So we'll do delete user task. So what
- 12:25:41are the things we need? So first of all
- 12:25:42let's create our service, right? So this
- 12:25:44will be simpler. So what I can do is
- 12:25:46I'll do export
- 12:25:49as sync function and this will be delete
- 12:25:53user task. So first of all you need the
- 12:25:55task ID
- 12:25:58and also you need the user ID. both you
- 12:26:00need and what this is going to return
- 12:26:03it's going to return a promise and it's
- 12:26:04going to return void nothing so here I'm
- 12:26:07simply going to do const deleted so do
- 12:26:10ait delete task so here let's go to our
- 12:26:16repository first
- 12:26:18and let's uh delete the task so first
- 12:26:20I'm just going to do export as sync
- 12:26:23function
- 12:26:25so do delete
- 12:26:28task so So you need the task ID which
- 12:26:31will be string and also you need the
- 12:26:34user ID. This also will be string and
- 12:26:38here so let's do a promise and because
- 12:26:40we deleting this one from our DB I'm
- 12:26:42going to return a boolean here. Okay. So
- 12:26:44we'll do con result. So this will have
- 12:26:47it pull dot query. So what are you going
- 12:26:51to do simply? So we're going to tell
- 12:26:54that you are going to delete something.
- 12:26:56So delete from where? So from your
- 12:27:00support task table uh here. Okay. So
- 12:27:03we'll do delete from support task where
- 12:27:07id equal to this and user id is equal to
- 12:27:13this. And then you're going to simply
- 12:27:16pass your task ID and user ID. That's
- 12:27:19it. What do you have to return? So
- 12:27:21you're going to return your result
- 12:27:24dot row count or else zero and this
- 12:27:29we're going to simply do this is greater
- 12:27:30than zero. So then it will be true.
- 12:27:33Okay. So that mean this particular row
- 12:27:34will be deleted. All right. So now let's
- 12:27:37import this. We're going to import
- 12:27:38delete task here. So I'll do delete task
- 12:27:42and we're going to pass the task ID and
- 12:27:44the user ID. Okay. So I'm simply going
- 12:27:46to check if not equal to deleted. So
- 12:27:48just throw one error here. So do new
- 12:27:50error. Sorry, app error 404.
- 12:27:55I'll do task not found.
- 12:27:58Sorry, this will be not deleted. Save
- 12:28:00this. So now we'll go to here and what
- 12:28:03we're going to do, right? So we can
- 12:28:04directly call this delete user task.
- 12:28:06This one, this particular service from
- 12:28:08our route. But we have to send a
- 12:28:10response back. So I'm simply going to do
- 12:28:11rest dot status 200. And let's do a
- 12:28:15JSON. And we are simply going to do
- 12:28:17success will be true.
- 12:28:21And let's pass a message that task
- 12:28:25deleted successfully
- 12:28:28from DB. Okay. So save this. So now I'll
- 12:28:32go here. Maybe let's do one thing. I'm
- 12:28:35going to copy this only. Create one
- 12:28:37more. Uh paste this one. Let's do this
- 12:28:41one. Delete. And we have this ID, right?
- 12:28:43This one. So let's copy the I think this
- 12:28:45only we are using here. That's fine. So
- 12:28:47let's try to send. So access token is
- 12:28:50needed which is correct. So let's get
- 12:28:53one token now. So I'm going to
- 12:28:56take this. We'll go here. Go to headers.
- 12:29:02Uh so let's do authorization. And here
- 12:29:04I'm going to do the error.
- 12:29:07And then let's see send task deleted
- 12:29:11successfully. Right. And if you try to
- 12:29:12send one more time, task not found.
- 12:29:14Fantastic. So let's go here and let's
- 12:29:16try to send. You're getting only one
- 12:29:18task means that is created. Sorry that
- 12:29:20is deleted. Okay. So this will be our
- 12:29:23CRUD. So create, read, update and
- 12:29:26delete. What you have to understand or
- 12:29:27what you have to practice right this
- 12:29:29page. Why this page is very important
- 12:29:31for this is where you are actually going
- 12:29:33to do and believe me uh one thing I'm
- 12:29:35going to mention that whatever project
- 12:29:37you are going to create any project any
- 12:29:39project I'm talking now there can be
- 12:29:41some exception that is fine. whatever
- 12:29:43project whatever complexity at the end
- 12:29:46you are going to do something create
- 12:29:48something you're going to do like
- 12:29:50reading or fetching updating or delete.
- 12:29:52So that is the reason this part is very
- 12:29:54important. What you can do right you go
- 12:29:56to support task instead of title and
- 12:29:58status try to add some three or four
- 12:30:00more properties and then try to
- 12:30:02implement the same thing again and
- 12:30:03again. Okay. So to do more practice. So
- 12:30:05with this now let's move on to the next
- 12:30:06one. So next will be our admin set
- 12:30:08feature. So admin set feature. So what
- 12:30:10admin will be able to do? So for now
- 12:30:12user can only fetch their own task.
- 12:30:15Correct. But admin as admin you will be
- 12:30:17able to fetch your uh fetch all the
- 12:30:19task. All right. So with this now let's
- 12:30:21move on to the next one. All right. So
- 12:30:23now we are going to create our admin
- 12:30:24side routes. Correct. So as I already
- 12:30:26mentioned that admin will be able to do
- 12:30:28what? We'll be able to fetch all the
- 12:30:30task. Second admin will be able to
- 12:30:32update the status. But before that you
- 12:30:33remember that we only have one
- 12:30:35particular user. Correct. So we need to
- 12:30:36create a admin user. So how we can do
- 12:30:38that? We can directly query our DB and
- 12:30:40then we can update one particular user
- 12:30:43role from normal user to admin user.
- 12:30:45Okay. So let's see how we are going to
- 12:30:46do that. So first of all what I'll do
- 12:30:48right? So let's increase this one and
- 12:30:51then okay we'll go here let's clear and
- 12:30:54then first of all we are going to run
- 12:30:56couple of commands. So we're going to
- 12:30:57run this docker execute and then this
- 12:31:00will be what? So this is our container
- 12:31:03name right. So you go to docker compose
- 12:31:05we'll see that we have container name
- 12:31:07and then you have your posgress db. So
- 12:31:10this is your posgress db right? So we
- 12:31:11are going to just copy this and then we
- 12:31:14are going to just run it. Okay. So now
- 12:31:18here we can directly query. So I I need
- 12:31:20the id, email and role from my user
- 12:31:22table normal query. Okay. So just paste
- 12:31:24it. So do paste enter and see that we
- 12:31:27have only one right. What I'm going to
- 12:31:29do? So first let's go to our uh user
- 12:31:32right. So register. So here I'm just
- 12:31:35going to create an admin user. So I'm
- 12:31:36just going to do john at thegmail.com.
- 12:31:38I'll keep the same password and I'm
- 12:31:39going to send registration successful.
- 12:31:42Correct. So now what I'll do right? So
- 12:31:44I'm just going to exit from this. So
- 12:31:45I'll do Q. So let's clear. Let's run
- 12:31:48once again. And then what I'm going to
- 12:31:50do, I'm just going to copy this and
- 12:31:53paste it here. Enter. And now you have
- 12:31:55two user. So what I'll do right, I'm
- 12:31:57going to change this [email protected]
- 12:31:59from normal user or let's say this user
- 12:32:02also. Let's do this only from normal
- 12:32:04user to admin user. So how we can do
- 12:32:06that? So update user, right? So we are
- 12:32:09updating the user table. Set role will
- 12:32:11be admin where email is what? Email is
- 12:32:14this email. So [email protected]
- 12:32:16makes sense. Now this we are going to
- 12:32:18just paste it. So now what I'll do right
- 12:32:21I'll copy this and then I'm just going
- 12:32:23to execute this enter and you can see
- 12:32:26that this is done. So now what we can do
- 12:32:28right if you check this you'll see that
- 12:32:29now this is a admin user correct and
- 12:32:31this is what actually we need. So now we
- 12:32:34have one super admin which is
- 12:32:35[email protected]
- 12:32:37correct? So this is what we are going to
- 12:32:39do first of all. Now let's do one thing.
- 12:32:41Let's go to our admin routes first. So
- 12:32:44we'll go to our admin routes. So we have
- 12:32:46routes routes. Routes we have admin
- 12:32:48routes. So okay. So here first let's
- 12:32:51start with creating. So how many routes
- 12:32:53we need? First we need mainly two
- 12:32:55routes. One where we are going to fetch
- 12:32:57all the task. Second we are going to
- 12:32:58update the status. But before that one
- 12:33:01very important thing you're going to
- 12:33:02understand now and that is so first
- 12:33:04let's import something from uh we do
- 12:33:08express and I'm simply going to do
- 12:33:10export con admin task router and this
- 12:33:16will be router and here first uh let's
- 12:33:20do one thing let's go to our index and
- 12:33:22I'm just going to do copy this and let's
- 12:33:25do /admin / task and here I'm just going
- 12:33:28to do admin task ask router. Okay, save
- 12:33:31this. So now first of all what we have
- 12:33:33to do here we have to protect the
- 12:33:35routes. Now this is important. Now this
- 12:33:37is where this particular page or these
- 12:33:40routes whatever we are going to create
- 12:33:42in this particular file only admin can
- 12:33:44access. So that means if you directly
- 12:33:46just do admin task router
- 12:33:49while I'm doing no let's do this one
- 12:33:51capital man. This is not correct. Let's
- 12:33:54keep capital and I'm going to import
- 12:33:56this and then I'm just going to do if I
- 12:33:59do admin.outer do use let's I'm just
- 12:34:02going to do authenticate. Now what will
- 12:34:04happen now this is actually normal user
- 12:34:06authentication right but we need to add
- 12:34:08one more layer that means first we have
- 12:34:11to check if this user is a normal user
- 12:34:14and if this user is a admin user second
- 12:34:17we also have to check if this user is a
- 12:34:19logged in user or not. So that means
- 12:34:21what I mean uh what I mean by this so
- 12:34:23let's say we have a user we have a admin
- 12:34:25correct for normal user we are simply
- 12:34:28checking if the user is logged in or not
- 12:34:30so if the user is logged in so this is
- 12:34:33fine but for the admin user we have to
- 12:34:35check two things first we have to check
- 12:34:36if the user is logged in plus we also
- 12:34:39have to check if the user role is admin
- 12:34:41user or not. So that is [snorts] two
- 12:34:43check we need to do. So that means this
- 12:34:45is only one check. So this is only going
- 12:34:47to check if the user is logged in. So
- 12:34:49how we are going to check if the user is
- 12:34:50a admin user or not? This is where we
- 12:34:53have this admin middleware. So this
- 12:34:55admin middleware is actually going to
- 12:34:57check if this user is admin user or not.
- 12:34:59So that means what we can do that we can
- 12:35:01use multiple middleware together. So to
- 12:35:03use multiple middleware together we have
- 12:35:05to give comma and we are going to use
- 12:35:07require admin. That's it. So that means
- 12:35:09we are simply telling that there are two
- 12:35:11level of check. First you check if the
- 12:35:13user is logged in user plus if the user
- 12:35:15is a admin user or not. both has to be
- 12:35:18satisfied. Then only we will allow user
- 12:35:21or we'll allow this particular code to
- 12:35:23go to the next middleware or go to the
- 12:35:24next route handler. So now what I can do
- 12:35:27right I'll do admin task router dot get.
- 12:35:30Now let's do slash and here I'll do
- 12:35:32async request
- 12:35:36response next.
- 12:35:39So I'll do try catch
- 12:35:43and we're going to call the next with
- 12:35:45error. So now first of all let's do a
- 12:35:49const and I'll do data and I'll do a get
- 12:35:53admin task.
- 12:35:57Okay. So first what we have to do here?
- 12:36:00So first of all let's do one thing.
- 12:36:02Let's go to our admin service. Right. So
- 12:36:05let's go to admin task service because
- 12:36:07this is where we are going to write our
- 12:36:09service logic. So first of all here we
- 12:36:11are going to uh do couple of changes.
- 12:36:13Okay. Before that we also let's do one
- 12:36:16thing right. So let's also implement uh
- 12:36:18some of the search and filter pattern so
- 12:36:21that you'll understand. So because in
- 12:36:22the previous previous section we have
- 12:36:24understood right that how you can do uh
- 12:36:26create uh CR create update fetch and
- 12:36:28everything. But in this one let's not
- 12:36:30only do normal fetching but as well as
- 12:36:32let's include search query that how you
- 12:36:35can implement search and how also you
- 12:36:37can implement filter. So let's do this
- 12:36:39one in the next part. All right. So
- 12:36:40let's create our uh services. So first
- 12:36:43we have our first one which will be to
- 12:36:45fetch all the task correct. So what are
- 12:36:47we going to do right? So let's create
- 12:36:48the function here. So I'll do export
- 12:36:50async function. Let's do this one as get
- 12:36:53admin task. So here what we are going to
- 12:36:55receive right. So remember now we are
- 12:36:57actually going to implement a search
- 12:36:59feature also. So what will happen like
- 12:37:01whenever we are going to do the API call
- 12:37:03so let's I can pass a query parameter.
- 12:37:05So what I mean by query parameter so
- 12:37:06something like let's say I wanted to
- 12:37:08search all those title of the of those
- 12:37:11task but the title starts with first
- 12:37:14something like this. So when I do this,
- 12:37:16so let's I wanted to call this API with
- 12:37:18local host
- 12:37:21whatever 3,000 or something and then I
- 12:37:23have API and then what I'm going to do
- 12:37:24right I wanted to pass a query params
- 12:37:26here. These query params will be my
- 12:37:28search param and here what I can do
- 12:37:29right I can pass a value. So that means
- 12:37:31what I'm simply doing that I'm actually
- 12:37:33implementing a search sorry function ID
- 12:37:36but this is my key and this is my value.
- 12:37:38So simply I'm telling that okay whenever
- 12:37:40I wanted to do a search I wanted to see
- 12:37:43all those support task where the title
- 12:37:45starts with this title or maybe it start
- 12:37:48with first whatever it is okay so for
- 12:37:50this one what I going to do right so
- 12:37:51let's create the type here only so I'm
- 12:37:53going to create one uh type so I'm just
- 12:37:55going to do this one as admin task task
- 12:37:58list and this will be my query
- 12:38:02okay and here what I'm going to do right
- 12:38:04I let's do a search and this search will
- 12:38:06be string and also I want to do search
- 12:38:08with my status. This also will be
- 12:38:10optional and this will be string. Now
- 12:38:12why optional is actually important?
- 12:38:13Because there is a possibility that
- 12:38:14sometime user want to search or they
- 12:38:16don't want to search. Okay. And then I'm
- 12:38:19also going to take one more type and
- 12:38:20this will be my admin task
- 12:38:23list response
- 12:38:26and this will be task and we are going
- 12:38:29to import task of this
- 12:38:32array. Okay. So what this is going to
- 12:38:34receive right? So this is going to
- 12:38:36receive query. The query will be type of
- 12:38:38this admin task query
- 12:38:41and what this is going to return. So it
- 12:38:43is going to return promise and this will
- 12:38:45be admin task query response. Okay. So
- 12:38:48this is what we are going to return. So
- 12:38:50first of all what you have to do that
- 12:38:51see now let's understand that how you
- 12:38:53can implement a proper search or filter
- 12:38:54functionality right whenever you are
- 12:38:57going to do any search functionality
- 12:38:59right you need to actually first trim
- 12:39:01that uh input that you are receiving.
- 12:39:03Okay, trim means we have to remove some
- 12:39:06unnecessary spaces from our query
- 12:39:08parameters. So first let's start with
- 12:39:10search. So I'm just simply going to do
- 12:39:12search and this will be so I'll do
- 12:39:14search query dot search. So you can see
- 12:39:17that we're getting dot trim. Okay. So
- 12:39:21this is what you're going to do or else
- 12:39:23I'm just going to do this one as
- 12:39:24undefined. Same I wanted to do for my
- 12:39:26status also. So this will be query dot
- 12:39:29search dot trim and then I'm simply
- 12:39:33going to do or else this will be
- 12:39:35undefined. So this is the first thing I
- 12:39:37need to do. Now very important now when
- 12:39:40we are actually going to do a uh filter
- 12:39:43with status let's say I wanted to filter
- 12:39:46all those task where the status is uh
- 12:39:49open right. So in that case what we have
- 12:39:52to do right because in our uh status uh
- 12:39:55logic so you can see that this
- 12:39:56particular property has fixed uh status
- 12:39:59here right so fixed means either it will
- 12:40:01be open or it will be in progress or it
- 12:40:03will be resolved right it will not be
- 12:40:05something else maybe let's say failed
- 12:40:07failed is not there so now let's see if
- 12:40:09user try to give some different value
- 12:40:12which value is not present so you have
- 12:40:13to actually return a error back so
- 12:40:16you're getting so now to do this one
- 12:40:17what you're going to do right I'm simply
- 12:40:19going to do okay if this status is true
- 12:40:22and now what we're going to do right so
- 12:40:24let's maybe
- 12:40:26uh we are going to create one let's do
- 12:40:28here only so I'm just going to do a con
- 12:40:31let's do task status
- 12:40:36and these will be either open right so
- 12:40:39we have either open or it will be either
- 12:40:42in progress
- 12:40:44or it will be after uh either
- 12:40:47resolved and then I'm simply we're going
- 12:40:50to do as constant right so these
- 12:40:52statuses has to be between these three
- 12:40:56options and if it is not there so that
- 12:40:58means we have to mention that and then
- 12:41:00I'm simply going to do if not equal task
- 12:41:02status dot includes the current status
- 12:41:05that we have right as task status
- 12:41:10uh what is that name task status right
- 12:41:16task oh we have to create one more here
- 12:41:18I bet so let's create one type here. So
- 12:41:21I'm just going to do type and I'll do
- 12:41:22task status and this will be so I'm
- 12:41:26simply going to do a type of this task
- 12:41:28status and this will be number.
- 12:41:32Okay. And this one we can use here that
- 12:41:34if this is not as task status. So in
- 12:41:37that case what you're going to do we are
- 12:41:39simply going to throw new error. So I'll
- 12:41:40throw new app error
- 12:41:43and I simply going to tell 400 and I'll
- 12:41:45do status must be
- 12:41:50between
- 12:41:52open in progress
- 12:41:55or result. Okay. So this is the first
- 12:41:59thing. So now what we going to do right?
- 12:42:01So first here after this we are simply
- 12:42:04going to create our task and let's do
- 12:42:07ait and I'm simply going to do find all
- 12:42:10task. Now this is where we are actually
- 12:42:12going to implement our uh DB uh
- 12:42:14property. So not DB properties our
- 12:42:16repository logic and here we are going
- 12:42:18to pass the search and we are going to
- 12:42:19pass the status and at the end what
- 12:42:22we're going to do we're going to return
- 12:42:23this task here
- 12:42:26this task. Okay.
- 12:42:29Okay this is fine. So now we are going
- 12:42:31to create our actual logic where we have
- 12:42:34to do couple of things. So first let's
- 12:42:36go to our admin task repository and here
- 12:42:39first let's create this. I'm simply
- 12:42:40going to do export as sync function and
- 12:42:44this will be find all task and here we
- 12:42:48are going to receive what we are going
- 12:42:49to receive filters. So what will be the
- 12:42:51filters? So we are getting search and
- 12:42:53status right? So we need to create a
- 12:42:54type. So simply I'm going to do type
- 12:42:57let's do admin task list
- 12:43:01filters
- 12:43:04and this will be so either it will be
- 12:43:06search or it will be status correct. So
- 12:43:08copy this
- 12:43:11and oh sorry here we are going to do
- 12:43:14this and now this will be type will be
- 12:43:17this and then what we're going to return
- 12:43:19we going to return a promise of task
- 12:43:22of array. So first we need to do couple
- 12:43:25of things right and also let's see one
- 12:43:27thing you have to return in this one not
- 12:43:30query we have to return the response
- 12:43:32here. So now what we have to do first?
- 12:43:34So first we have to create one
- 12:43:36condition. So I'll do conditions
- 12:43:40and this will be string of array and
- 12:43:42this will be equal to array. Also we
- 12:43:44need the values. So I'm just going to do
- 12:43:46values. Values we don't know what is the
- 12:43:48value. So I'm just going to do unknown
- 12:43:50of array and this will be equal to
- 12:43:52array.
- 12:43:54Now what I'm going to do? So first of
- 12:43:55all what we have to do? So we have to
- 12:43:57check that if currently in our URL
- 12:44:01search is present. We are going to push
- 12:44:02this search into our condition and also
- 12:44:05we are going to push the value into our
- 12:44:07values array. If status is present then
- 12:44:10we are going to also push the status
- 12:44:11into our condition and also we are going
- 12:44:13to push our values here also. So to do
- 12:44:15this one first what we'll do? So I'll
- 12:44:17take a param index and this will be
- 12:44:20equal to one. I'm simply going to check
- 12:44:21that if filter dot search if this is
- 12:44:24actually present. So in that case I'm
- 12:44:26going to do condition dot push and here
- 12:44:28we are going to write a query right. So
- 12:44:30title I like right and this part also I
- 12:44:33have covered in my uh actually uh
- 12:44:36posgress video uh if you have watched
- 12:44:38that particular section and we are going
- 12:44:40to pass the param index and then we are
- 12:44:42also going to push the values so values
- 12:44:44will be what? So values will be here our
- 12:44:48uh filters dot search. So do filter dot
- 12:44:51search
- 12:44:53that's it and then we are going to
- 12:44:55increase the param index by one like
- 12:44:57this. Similarly, we have to do the same
- 12:44:59thing for our status also. So, I'll do
- 12:45:02if filter dot status if this is present.
- 12:45:05So, in that case, I'm simply going to do
- 12:45:07condition dot push. And here we are
- 12:45:09going to check the status property. So,
- 12:45:10I'll do status is equal to
- 12:45:14and this will be our param index. And
- 12:45:16here we are going to pass value dot push
- 12:45:19and d will be filters dot status. Okay.
- 12:45:24So now based on whatever is present it's
- 12:45:27going to whose uh in the values values
- 12:45:30are important also and also in the
- 12:45:32condition you have to check that which
- 12:45:33property actually we are filtering. So
- 12:45:35here we are checking our title for
- 12:45:36status we checking our status and from
- 12:45:38where it is coming this is actually from
- 12:45:40our DB properties that we are checking.
- 12:45:43So now we can simply create a um where
- 12:45:45clause. So I'll do con and I'll do where
- 12:45:48clause
- 12:45:50and this will be the condition length if
- 12:45:52this is greater than zero. So I'm simply
- 12:45:54going to do where
- 12:45:56and here we are going to simply pass
- 12:45:58conditions dot join because if both are
- 12:46:01present what you have to do we are going
- 12:46:02to join with and okay like this. So this
- 12:46:06is done or else this will be empty and
- 12:46:08then at the end you can return the
- 12:46:10result. do res uh result and this will
- 12:46:12be avit. So we have pool dot query and
- 12:46:16here we're going to pass a task
- 12:46:18and now
- 12:46:22okay let's do one thing maybe it will be
- 12:46:24confusing so let's create like this we
- 12:46:26have done remember task row and this
- 12:46:28will be equal to our task like this and
- 12:46:30this task row we going to use here
- 12:46:34task row and now what I'm going to do so
- 12:46:37we'll do select
- 12:46:40so I'll do select ID title
- 12:46:44status
- 12:46:46user ID
- 12:46:49created at and updated data. So all this
- 12:46:52I need and now I'm simply going to do
- 12:46:55from
- 12:46:56support tasks this is my table name and
- 12:46:59you have to pass the web clause here and
- 12:47:02also now let's do order by
- 12:47:05created at disk
- 12:47:08and we are going to pass the values. So
- 12:47:11values will be so this is our condition
- 12:47:14and this will be our actual values the
- 12:47:16values that we have this one. So I think
- 12:47:19this is done and then at the end we are
- 12:47:21going to return result dot rows.
- 12:47:26So this is completed. So this is what we
- 12:47:28are going to do based on what whatever
- 12:47:29is present we add the condition that we
- 12:47:32based on what we are going to filter
- 12:47:33right and then uh we are also going to
- 12:47:36push the values created a wear clause
- 12:47:38and then just create the query. So this
- 12:47:40is done. So now we can go to our
- 12:47:42services. So here let's import this
- 12:47:45first. So we're passing sorry uh this
- 12:47:49will find all task search and status and
- 12:47:51then it's going to return the task
- 12:47:53return. So this is done. Now what we
- 12:47:55have to do right? So you have to go to
- 12:47:56our task route. So first of all this get
- 12:47:59admin out going to import from our
- 12:48:01service. This is our get admin and here
- 12:48:03you see that you have to pass a query.
- 12:48:05So how you're going to pass a query. Now
- 12:48:07to pass the query what you do right
- 12:48:09you're going to pass a request and
- 12:48:11request is going to give you this query
- 12:48:13that's it and then you can simply do
- 12:48:16status
- 12:48:18200
- 12:48:21uh JSON and then you'll pass the success
- 12:48:24it will be true and then
- 12:48:27you're going to pass the data. So this
- 12:48:29is done. So now this is actually the
- 12:48:31important part here. Okay. So now what
- 12:48:34we going to do right we need to do the
- 12:48:35uh check here or we are going to test.
- 12:48:38So first let's go to our okay let me
- 12:48:40just move this one here.
- 12:48:45Okay so first let's go to our login and
- 12:48:49then first I'll try to login. Okay I'm
- 12:48:52not running maybe. Okay I got it because
- 12:48:55I'm actually recording after some time.
- 12:48:58So I have to start again. So now let's
- 12:49:01check what we passing. We are passing
- 12:49:03body sumgmail.com
- 12:49:06send this we're getting the token so
- 12:49:08first check this okay so I'll go here uh
- 12:49:10and then in the headers I'll just check
- 12:49:13that okay what is this uh user
- 12:49:21send this okay this is a normal user I
- 12:49:24think we have created one more user
- 12:49:26right I think that is John I remember
- 12:49:29let's send I think this is the admin
- 12:49:31user because we need to check the admin
- 12:49:33user also and then now let's try to send
- 12:49:36this one
- 12:49:39okay so this is admin right so now first
- 12:49:41of all what I'll do right so let's
- 12:49:43create one here so this will be get
- 12:49:46right so go here copy this API till this
- 12:49:50part
- 12:49:52so API now let's go to our index so we
- 12:49:56have / API/ admin / task correct so /
- 12:50:00admin/ as tasks. Okay. So now first of
- 12:50:04all I try to send access token is
- 12:50:06required. Now what I'll do right I'll go
- 12:50:07to login and I'll try to login with my
- 12:50:10normal user. Okay. This is very
- 12:50:11important. So try to send and you'll see
- 12:50:14that I'm just going to copy this and
- 12:50:17let's go to our headers. So add
- 12:50:19authorization. Let's add our beer.
- 12:50:25And then you see what will happen. Try
- 12:50:26to send admin access is required and you
- 12:50:29do not have admin access. It seems
- 12:50:30fantastic right? So that means this
- 12:50:33particular line now is actually able to
- 12:50:35understand that okay this actually need
- 12:50:37a admin access because this also need
- 12:50:39authenticate as well as require admin.
- 12:50:41So now if I go to login and try to login
- 12:50:44with my admin user get the token
- 12:50:48and now I try to go here and try to
- 12:50:51replace this
- 12:50:53and try to send that's it. So you're
- 12:50:56getting this one. Now what I'll do right
- 12:50:57so maybe
- 12:50:59uh maybe let's do one thing we have a
- 12:51:01title first open task and status is open
- 12:51:04correct so maybe I'm just going to login
- 12:51:07with a normal user okay so I'll try to
- 12:51:09send I'll get the token
- 12:51:12and then go to our uh where is that post
- 12:51:15right so your body second support task
- 12:51:18let's try to add one more here
- 12:51:23try to create so it's created so now if
- 12:51:25I go here and try to send. So you're
- 12:51:27getting two, right? So now let's see how
- 12:51:29we are going to uh test. So first of
- 12:51:32all, let's say I wanted to check the
- 12:51:34title of the second one only. So what I
- 12:51:36can do, right? I can pass a query here.
- 12:51:38So see I'll do this and see here we have
- 12:51:40a query paramedic and I'm just going to
- 12:51:42do a search. So you can see that the key
- 12:51:44is value is search and search is equal
- 12:51:46to I'm going to do second. Okay. And now
- 12:51:48try to send okay status must be okay.
- 12:51:52Why this is coming? Status must be
- 12:51:53between open in progress and result.
- 12:51:55This should not come.
- 12:52:00Oh, this will be status. See, this is
- 12:52:02the problem because you given for both
- 12:52:03sim. So now try to send and you can see
- 12:52:05that you're only getting the second
- 12:52:07support. Now I'll do first. Try to send.
- 12:52:10You're getting only first, right? And
- 12:52:12also you can do with status also. Let's
- 12:52:15say I'm just going to do status and I
- 12:52:17wanted to check the open status. So I'll
- 12:52:19do status and this will be open like
- 12:52:21this. Okay. Try to send you're getting
- 12:52:23get both. And now change this one to
- 12:52:25maybe result
- 12:52:27right and try to send you're getting
- 12:52:29empty right if you try to send in
- 12:52:31progress.
- 12:52:33Try to send you're getting empty. Okay.
- 12:52:36Now let's say you can uh send both. So
- 12:52:38how you going to send both? Let's say
- 12:52:39you wanted to add search. Search will be
- 12:52:41second. And then if you want to add both
- 12:52:44you have to give and here and see that
- 12:52:45one mode will be added here and this
- 12:52:47will be status equal to open. So
- 12:52:49basically I'm telling that give me all
- 12:52:50the support task where the title is
- 12:52:53second and status is open try to send
- 12:52:55you're getting only one fantastic right
- 12:52:58so with this now easily you will be able
- 12:53:01to implement search and filter logic
- 12:53:02also okay so with this now let's move on
- 12:53:04to the next one so next one will be how
- 12:53:07admin can update task status so that we
- 12:53:09are going to do in the next part all
- 12:53:11right so now next one will be so we need
- 12:53:13one more route here that admins should
- 12:53:15be able to update the uh update task
- 12:53:17status
- 12:53:18Okay. So, first let's do admin task
- 12:53:21router patch. I think I'm not explaining
- 12:53:23again because you uh I hope that you
- 12:53:26know that when we are updating only
- 12:53:27certain portion we don't have to do like
- 12:53:29post and everything right. So we need
- 12:53:31only patch. Uh so here I'm simply going
- 12:53:33to take the task ID because we need to
- 12:53:35know which task we are updating and then
- 12:53:38we have to tell look at this will be my
- 12:53:39status of update. So here I'm simply
- 12:53:41going to do a sync. We'll do request
- 12:53:43response
- 12:53:46and next
- 12:53:49correct. So now let's take a try catch
- 12:53:57call the next with error. So in this try
- 12:54:00first now this will be simpler. Okay. So
- 12:54:02it will be const and then I'm simply
- 12:54:04going to d it. Let's give this one as
- 12:54:06update admin task status.
- 12:54:11Okay. and we going to send something
- 12:54:13here. So you'll understand what actually
- 12:54:14we need to send. But first let's create
- 12:54:16our services. So I'm simply going to do
- 12:54:18export async
- 12:54:21function and I'm simply going to do
- 12:54:23update admin task status. So what you
- 12:54:26need? So first of all you need the task
- 12:54:27ID correct which will be what string and
- 12:54:30then you also need to know status. Why
- 12:54:32I'm keeping unknown extremely important
- 12:54:34because we don't know what value user is
- 12:54:36sending from the UI side. Let's say in
- 12:54:38the UI side you have a drop-down then
- 12:54:39user don't have any other option they
- 12:54:41needs to select those but let's say in
- 12:54:43the UI side they have input text and
- 12:54:44they can put any value right so that
- 12:54:46should not happen so in that case we
- 12:54:48have to give a check here so I'm just
- 12:54:49going to give a promise of task
- 12:54:52so I'm going to give this check this one
- 12:54:54okay so copy this
- 12:54:57paste it here somebody is simply going
- 12:54:59to tell that if this type of
- 12:55:02status is not equal to equal to string
- 12:55:07And if not equal to task status includes
- 12:55:10status as task status. So I'm simply
- 12:55:12going to tell that status must be
- 12:55:13between open in progress or result.
- 12:55:16Right? So this is the first thing. Now
- 12:55:19after this I'm simply going to do con
- 12:55:21task and this will be ait update task
- 12:55:25status
- 12:55:28and this will be our task state id and
- 12:55:31status both you have to pass. Okay. if
- 12:55:34this task is not present. So in that
- 12:55:37case we're going to throw
- 12:55:39new app error 404
- 12:55:43and I'll do task not found and then I'm
- 12:55:47simply going to return the task.
- 12:55:52So here now first let's go because from
- 12:55:54our routes you need to import this. So
- 12:55:56import this
- 12:55:58here and then what you have to pass. So
- 12:56:00first you have to pass the task ID. So
- 12:56:02how you're going to get the task id? So
- 12:56:03you're going to get from request
- 12:56:05dotparams dot task id and then what you
- 12:56:08have to you have to pass the actual
- 12:56:10status. This actual status from where
- 12:56:12you're going to get see this part we've
- 12:56:14already done multiple times. So you're
- 12:56:16going to get from your from your request
- 12:56:18body. So request body dot status. Okay.
- 12:56:22And now what you can do right you can
- 12:56:23simply return the responsible race dot
- 12:56:25status of 200
- 12:56:28dot JSON and I'm simply going to do
- 12:56:31success will be to data will be uh the
- 12:56:35task that we are getting. So this is
- 12:56:38fine. Now we have to create our
- 12:56:40repository. Repository means we have to
- 12:56:42create our actual task to update the
- 12:56:44task. Right? So first let's go here and
- 12:56:47then we'll do export
- 12:56:50function this.
- 12:56:53So we'll get the task ID which will be
- 12:56:55string
- 12:56:57and status and this will be string. Why?
- 12:57:00Because here we are already checking if
- 12:57:01it is not string. So that means if it is
- 12:57:03not string in this case so we can
- 12:57:06actually uh like take this one as a
- 12:57:08string here because we already have a
- 12:57:10check. So that is the reason giving all
- 12:57:12or adding all these checks are very
- 12:57:13important. So now what you have to do?
- 12:57:15So you have to return a promise of task
- 12:57:18or this can return null also it's
- 12:57:20possible. So now just write the query
- 12:57:23right. So we'll do con result. So we'll
- 12:57:25do a pull dot query and here I'm simply
- 12:57:29going to pass a task row and now we can
- 12:57:32simply create it. So I'm simply going to
- 12:57:34do select sorry what I'm doing man
- 12:57:38update. So the update support tasks. So
- 12:57:41here we are simply going to tell set
- 12:57:43because we're updating status property
- 12:57:45right. So status will be equal to one.
- 12:57:49Now also I think you already know we
- 12:57:51need to update the updated at property
- 12:57:52also which will be now current time and
- 12:57:55then what we going to do where the id is
- 12:57:57equal to this because based on the ID
- 12:58:00only you have to understand that what
- 12:58:01actually we have to update. So do
- 12:58:04returning we need that id title status
- 12:58:08user id created at and updated at
- 12:58:14okay and then we can simply pass out. So
- 12:58:17first one will be your status status
- 12:58:19will be the status and id will be your
- 12:58:22task id and then we are going to return
- 12:58:26result dot rows of oh I think I have to
- 12:58:30do let's keep result only. So do result
- 12:58:33dot rows of zero or else this will be
- 12:58:36null. So save this and now we can do
- 12:58:39just import this from here.
- 12:58:42What I'm doing man we're getting status
- 12:58:51but we are already giving check here if
- 12:58:53type of status is not equal to string.
- 12:58:57Oh we have to give or
- 12:59:00that is the reason. So if it is not
- 12:59:02equal to string or if this is the case.
- 12:59:04So now I think this should work. Okay.
- 12:59:07So to check this what we can do. So
- 12:59:09first of all let's say let's do one by
- 12:59:11one. Okay. So first I will go to my uh
- 12:59:14let's go here. Let's try to get all the
- 12:59:16task. So we're getting two task. Let's
- 12:59:19create one more. So this will be patch.
- 12:59:23And now what I'm going to do right will
- 12:59:25copy this.
- 12:59:27And let's say I wanted to update this
- 12:59:29particular second task status from open
- 12:59:32to in progress or result. So I'm going
- 12:59:34to copy this
- 12:59:36uh paste it. Then I'll do status. But
- 12:59:38see what I'll do. I'll go to body and
- 12:59:40I'll try to send the status as random
- 12:59:45value. Okay. Let's do one by one. So
- 12:59:47first we'll be getting what token is
- 12:59:49required. Access token is needed.
- 12:59:51Fantastic. Now let's go to login and try
- 12:59:53to login with the normal user. Send
- 12:59:56this. Get the token.
- 12:59:59Go here. Go to headers. Add
- 13:00:01authorization. Add beer. And now see
- 13:00:05that you're going to get admin access is
- 13:00:07required. Fantastic. Now we'll go here.
- 13:00:10And then I'm just going to login with my
- 13:00:12norm uh admin user. Get the token.
- 13:00:17Go here and change the token. So do be
- 13:00:21error
- 13:00:23and see what will happen. send this
- 13:00:25status must be between open in progress
- 13:00:27and result. So that means you just can't
- 13:00:29pass any random value. So now I'm just
- 13:00:31simply going to do in progress. Let's
- 13:00:32say I wanted to do in progress. Send
- 13:00:35this updated this is in progress. Now
- 13:00:37you go to get and try to send this again
- 13:00:40and you're going to get in progress.
- 13:00:42Fantastic. So that means update is now
- 13:00:44working. See the main idea of creating
- 13:00:46this project. You have to understand how
- 13:00:47you can implement CRUD especially what
- 13:00:49is the authentication middleware. What
- 13:00:52is RBC means? ROB based access control.
- 13:00:54For example, just now we have done
- 13:00:55right. So see if you're a normal user,
- 13:00:56you can't update the status only admin
- 13:00:58can update status. Okay. So now see with
- 13:01:01[snorts] this we actually have completed
- 13:01:03our CR section. I think we have done
- 13:01:05almost all the things enough thing that
- 13:01:07you should know. Okay. I know that we
- 13:01:09haven't covered so many complicated
- 13:01:11things like pagination and so many other
- 13:01:13logic there but I think it's not
- 13:01:14possible to include each and everything
- 13:01:15in a single video. But with this so what
- 13:01:18will be the next step? Now this is
- 13:01:21actually now things will be very
- 13:01:23interesting. Okay. So listen carefully
- 13:01:25what I'm going to say. Next part what
- 13:01:28I'm going to do right I will be adding
- 13:01:30my uh I think that is I think recently I
- 13:01:32created I think 2 3 weeks back I created
- 13:01:35a radius video that particular video
- 13:01:38that I'm going to add in the next part.
- 13:01:40So it will be around 2 hours. It is very
- 13:01:42very very important. Again if you really
- 13:01:44want to learn NodeJS properly I will
- 13:01:46highly recommend just like I mentioned
- 13:01:48in the postgress right please watch the
- 13:01:50radius video also because in that
- 13:01:52particular video you are going to
- 13:01:53understand radius concept radius basics
- 13:01:56and also how you can use radius in a
- 13:01:58real NodeJS project. Now why this is
- 13:02:00very important because in this
- 13:02:02particular project now we are going to
- 13:02:03implement our file upload feature. Now
- 13:02:05in the file upload feature we are not
- 13:02:07going to only have file upload we are
- 13:02:08going to have file upload plus radius
- 13:02:11plus we are going to have background
- 13:02:13job. we are going to use boolean q right
- 13:02:15so this is the reason you need to watch
- 13:02:17the next section and there is a reason
- 13:02:20why I'm adding all of these okay so
- 13:02:21don't think like this is just like I'm
- 13:02:23randomly adding all these video if that
- 13:02:25is the case I already have let's say
- 13:02:26five or six nodejs videos I I think I
- 13:02:29should have added each and everything
- 13:02:30and this video will be 50 hours long
- 13:02:32doesn't work like that so that is the
- 13:02:34reason decided to add only postgress and
- 13:02:36the radius part because it's actually
- 13:02:37important if you don't cover or don't
- 13:02:39watch that section you will not be able
- 13:02:41to understand because I'm not going to
- 13:02:42covered basics in this one. Okay, this
- 13:02:44will be only practical and I'm going to
- 13:02:46give you the basic idea that how you can
- 13:02:47implement radius and bul MQ and
- 13:02:49implement background job and background
- 13:02:51workers. So I'm repeating once again
- 13:02:53next part will be radius complete
- 13:02:54dedicated section watch that particular
- 13:02:58section and then you can directly jump
- 13:02:59to the timestamp and you can implement
- 13:03:01the this particular project the rest
- 13:03:03whatever is left. So what are the things
- 13:03:05you have to do? You have to do file
- 13:03:06upload radius implementation and booq in
- 13:03:09the next part. Hey everyone welcome
- 13:03:11back. So in this particular video we are
- 13:03:12going to cover each and everything that
- 13:03:14you need to know to start your journey
- 13:03:16with radius. Now this is a completely
- 13:03:18practical project based video and for
- 13:03:22this one we are going to use a uh NodeJS
- 13:03:24and express application and we are going
- 13:03:26to use docker and posgress as our DB.
- 13:03:28Now before [snorts] I'm going to discuss
- 13:03:30the curriculum as usual I wanted to
- 13:03:32mention one uh important point and that
- 13:03:34is you can see that I clearly mentioned
- 13:03:35that if this particular video gets more
- 13:03:38than 2,000 likes because I'm getting so
- 13:03:39many request I'm going to create uh
- 13:03:42another video on posgress that will be
- 13:03:44advanced part two and also radius part
- 13:03:46two where I'm going to cover some of the
- 13:03:48uh more advanced topics like sessu
- 13:03:51background jobs and all the other
- 13:03:54products and level patterns that you
- 13:03:55need to know and also please like this
- 13:03:58video comment comment any possible if
- 13:03:59you can share with your friends it will
- 13:04:01be very very helpful so what are the
- 13:04:03things that we are going to cover so
- 13:04:04first we'll start with what is radius
- 13:04:07why actually we need it and if I go to
- 13:04:10the details you can see that I added all
- 13:04:11the details that why radius is used as
- 13:04:13first inmemory data store where radius
- 13:04:16fits in a real backend project right the
- 13:04:18difference between radius and the normal
- 13:04:20database and all the other things then
- 13:04:22we are going to start with adding radius
- 13:04:25to our backend project as I already
- 13:04:27mentioned we are going to use a NodeJS
- 13:04:28project. So you're going to understand
- 13:04:30how you can start Radius locally with
- 13:04:32docker install radius client package
- 13:04:35create reusable radius connection file
- 13:04:37and also we are going to understand very
- 13:04:39very important thing that radius as the
- 13:04:41first inmemory layer that we are going
- 13:04:43to use in this backend project. Then we
- 13:04:46are going to understand a complete
- 13:04:48dedicated section on radius data type.
- 13:04:50So you're going to understand hashes,
- 13:04:52list, sets, sorted list at the beginner
- 13:04:55level. How you are going to use counters
- 13:04:58using radius commands and then all these
- 13:05:01concepts we're going to apply in our
- 13:05:03backend project. Okay. So that will be
- 13:05:05another dedicated section where we are
- 13:05:07going to add radius caching to our
- 13:05:08product APIs. Moving to the next one
- 13:05:11which will be another important section.
- 13:05:12We are going to understand cash
- 13:05:13invalidation still cash problem. Right?
- 13:05:16So here we are going to use cast asset
- 13:05:18pattern. understand cash uh issue
- 13:05:21invalidate uh patterns right so there
- 13:05:23will be lot of things we are also going
- 13:05:25to understand or we are going to have a
- 13:05:26dedicated section on TTL okay then uh we
- 13:05:30are going to have another section on uh
- 13:05:31how you can implement rate limiting with
- 13:05:34radius and at the end we are going to
- 13:05:35end this particular video with pubsub or
- 13:05:37how you can actually implement uh
- 13:05:40publish and subscriber pattern using
- 13:05:42radius so this is what we are going to
- 13:05:44understand in this particular video I'm
- 13:05:46pretty sure you're going to learn a lot
- 13:05:49things and as usual the source code will
- 13:05:51will be added in the description so you
- 13:05:53can download from free from my website
- 13:05:56and also I wanted to mention I think
- 13:05:57most of you already aware that I am
- 13:05:59planning to launch a huge course on my
- 13:06:02uh website that is not yet live so if
- 13:06:04you want you can join the weight list
- 13:06:05that is completely free so you can see
- 13:06:07that almost more than 4,000 people
- 13:06:09already joined so with that [snorts] now
- 13:06:11let's get started and good luck all
- 13:06:13right so now let's get started see the
- 13:06:15first thing I wanted to mention that
- 13:06:17this video is not for complete
- 13:06:18beginners. So what I mean by this, you
- 13:06:21should have at least some idea on node
- 13:06:23express. Okay. And also some basic idea
- 13:06:26how usually docker works, how we can
- 13:06:28create docker compos file and some of
- 13:06:30the very basic concepts like how you can
- 13:06:32create controllers, services, express
- 13:06:35routes, some of the postgress queries.
- 13:06:37Now why this is important? Uh so first I
- 13:06:39wanted to mention because see if you
- 13:06:42don't have all of these right, it will
- 13:06:43be very difficult for you to understand
- 13:06:44this particular project. Why? So this is
- 13:06:47a completely practical project. So what
- 13:06:48I mean by practical? So as you can see
- 13:06:50that I have a project created. Okay. So
- 13:06:53here we already have all the routes and
- 13:06:54everything. So we're not going to write
- 13:06:56each and every logic because this course
- 13:06:59is going to only on radius. So what we
- 13:07:02going to do? We are only going to apply
- 13:07:04the radics related concepts practically
- 13:07:07and by implementing practically we are
- 13:07:09going to learn the concepts. I hope
- 13:07:10you're getting and also before we'll
- 13:07:12implement the concepts we are going to
- 13:07:14have some kind of conceptual and some
- 13:07:15very small examples so that you can
- 13:07:18understand that okay what uh that
- 13:07:20particular concept is and where you are
- 13:07:22going to apply that particular uh
- 13:07:23concept in a real project. So first
- 13:07:26before uh we will jump into radius
- 13:07:30concept first you have to understand the
- 13:07:31project structure that what actually we
- 13:07:33are using here. Okay. So as you can see
- 13:07:35that we have a simple uh node express
- 13:07:37project. So for this one we're using
- 13:07:39docker with posgress. So for our DB so
- 13:07:41you can see that in the docker compose
- 13:07:44we just have a service. So this is our
- 13:07:46image container name uh the username
- 13:07:50password and blah blah all the other
- 13:07:51things right. So we have the port
- 13:07:53volumes and you can see that all these
- 13:07:54details also have added in the env right
- 13:07:56so we have a database host database port
- 13:07:59database user database password database
- 13:08:02name. Okay. Now what I'm going to do
- 13:08:04right, I'm going to add this starter
- 13:08:06code in the attachment. Very very
- 13:08:08important. First you have to download
- 13:08:09this and then on top of this starter
- 13:08:12code you are going to implement all the
- 13:08:14logic that we are going to do. Now next
- 13:08:16will be so let's start with the first
- 13:08:18one. So you can see that we have a DB
- 13:08:19folder we have a pool. DS. So here we're
- 13:08:21just creating the connection pool.
- 13:08:24Right? So we have the new pool from the
- 13:08:26pg package. We have av package and
- 13:08:28before that also I want do. And if you
- 13:08:30go to package.js JS and you can see that
- 13:08:32we have chords envir and pg and we have
- 13:08:35a start uh script db migrate that I'm
- 13:08:38going to explain and the db sid uh so
- 13:08:41what this is going to do this is going
- 13:08:43to do the migration right this script
- 13:08:45and then we have a simple dummy data
- 13:08:47that I have created that we are going to
- 13:08:48use a demo products and this is going to
- 13:08:52seed this particular data in your
- 13:08:53posgress db next let's move on to the
- 13:08:56services so what services is going to do
- 13:08:57services is actually going to interact
- 13:08:59with your posgress DB. So that is the
- 13:09:02reason first we have a get all products
- 13:09:04which is going to fetch all the
- 13:09:05products. Then you have get product by
- 13:09:07ID uh and then create product and update
- 13:09:10product. So if you go to the postman so
- 13:09:12you can see that we have all these
- 13:09:13routes. We have a health route products
- 13:09:15to fetch the products right and then we
- 13:09:18have the products uh filter products by
- 13:09:21ID creating a product and updating a
- 13:09:23particular product. Next move on to the
- 13:09:25controllers. controller is nothing but
- 13:09:27your uh logic where we are going to
- 13:09:29actually return our response back. Let's
- 13:09:31say we are having some front end. Uh so
- 13:09:33I'm not explaining all of this. If you
- 13:09:34have some idea, you'll understand what
- 13:09:36actually this thing is doing. And this
- 13:09:37is also going to call the product
- 13:09:39service, right? Then uh we have a simple
- 13:09:41middleware and then the routes file. So
- 13:09:43routes file is going to have your
- 13:09:45products related routes file and one
- 13:09:48index dot ts file which is your root
- 13:09:51route file. And this root route file we
- 13:09:53are going to use where we are going to
- 13:09:54use in the app.ts ts file right so you
- 13:09:56can see that we are using creating the
- 13:09:58express application using the course
- 13:10:00using the express json middleware using
- 13:10:02the routes and then exponing the app and
- 13:10:04this app we are going to use in our main
- 13:10:07file so this is our main root file right
- 13:10:09serverts we are starting the server we
- 13:10:11are creating or testing the DB
- 13:10:13connection and then you can see that we
- 13:10:15are listening to this particular port so
- 13:10:17this is very very important to
- 13:10:19understand this uh project structure or
- 13:10:21else it will be difficult for you to
- 13:10:24understand that what actually we are
- 13:10:25going to do in the next section. Now
- 13:10:26remember as I already mentioned this is
- 13:10:28a radius course. We are not going to
- 13:10:31write too many logic and everything and
- 13:10:33we're not going to go into too much
- 13:10:34advanced details for now but the uh
- 13:10:37information that I'm going to give you
- 13:10:38is going to give you enough information
- 13:10:40to get started with radius. So what will
- 13:10:43be the next step and what structure we
- 13:10:45are going to follow. So first of all
- 13:10:47what we need to do right. So let's uh do
- 13:10:49one thing let's install the radius
- 13:10:51package first we have to do that. So
- 13:10:53what I'm going to do right so let's go
- 13:10:55here and then I'm just going to do we
- 13:10:57haven't started anything that what is
- 13:10:58radius and everything that we're going
- 13:11:00to do. So first download the starter
- 13:11:02code very important okay if you don't do
- 13:11:04it it will be difficult I'm repeating
- 13:11:06again and again download it and then we
- 13:11:08are just going to do npm install radius
- 13:11:11and this you have to do on the root
- 13:11:12that's it okay so this is going to
- 13:11:14install the radius package this is done
- 13:11:17so now first of all we have to
- 13:11:18understand that what actually radius is
- 13:11:21and why actually we need radius right so
- 13:11:23this is important because uh this is a
- 13:11:25very important interview questions also
- 13:11:27why because when you have something
- 13:11:28called post or let's say you're using
- 13:11:30some different DB why actually you need
- 13:11:32radius so first of all you you are going
- 13:11:34to write that radius is nothing but your
- 13:11:37in-memory data store right so this will
- 13:11:39be your in-memory data store okay now
- 13:11:44what is the meaning of this so if you go
- 13:11:45to here right so let's always try to use
- 13:11:48this AI mode okay side by side or you
- 13:11:49can use some chat GP and everything it's
- 13:11:52very helpful okay so if you are watching
- 13:11:54a video and at the same time make use of
- 13:11:55these tools it will be very very easy so
- 13:11:59here you can See the words telling that
- 13:12:00okay it's a inmemory nosql data
- 13:12:02structure something like that but we
- 13:12:03have to understand that what actually in
- 13:12:05memory means so that simply means the
- 13:12:08radius that we have right it's going to
- 13:12:10keeps your data in RAM okay
- 13:12:13[clears throat] you're getting so it's
- 13:12:14going to keeps data mainly in RAM so
- 13:12:19what will happen if we have something in
- 13:12:21RAM so in a simple way the read and
- 13:12:23write operation will be extremely fast
- 13:12:26or very fast right so read and write
- 13:12:28operations questions will be very fast.
- 13:12:32So this is one of the main reason. Now
- 13:12:33why actually you need ready? So what are
- 13:12:35the best use cases interview questions
- 13:12:38right? So for example let's say we want
- 13:12:39to implement some kind of cash. So here
- 13:12:42okay let me just give here. So we want
- 13:12:44to implement some kind of cash. It's
- 13:12:46very important right? Let's say we have
- 13:12:48to implement some kind of rate limiter.
- 13:12:50I'm giving very basic example. Okay
- 13:12:52there are so many advanced use cases
- 13:12:54radius can have. Let's say you want to
- 13:12:56implement some kind of message broker
- 13:13:00right or uh maybe okay let's say you
- 13:13:04have a normal database right so in our
- 13:13:05case we're using postgress now we want
- 13:13:08to implement or we want to keep some
- 13:13:10kind of temporary first layer in front
- 13:13:12of our existing database so it's like
- 13:13:15something like this so we have a temp
- 13:13:16first layer in front of our normal
- 13:13:22database or main database right
- 13:13:25So these all will be the use cases. Now
- 13:13:29now the next question will be why radius
- 13:13:31is used even though we have a normal
- 13:13:33back end right for example in our case
- 13:13:35we're using posgress right extremely
- 13:13:37important concept you can't uh skip all
- 13:13:39of these so even though our normal
- 13:13:43database is what it's actually for your
- 13:13:46permanent structured data. So whatever
- 13:13:48database you're using that is for your
- 13:13:51permanent structured
- 13:13:54data right what radius is going to do
- 13:13:57right so there can be some task that
- 13:14:00whatever application that we are
- 13:14:01building that we need to have some kind
- 13:14:03of speed
- 13:14:05or some kind of temporary data and this
- 13:14:09is where radius can be helpful right so
- 13:14:11this is where radius can be helpful how
- 13:14:13so it can be for example let's say we
- 13:14:15have a product list and I want to c that
- 13:14:18product list. So caching product list.
- 13:14:21So this is where you need radius or
- 13:14:23let's say you want to count some kind of
- 13:14:25product views. How many views are there
- 13:14:28for that particular product or you want
- 13:14:30to limit that how many request
- 13:14:34a particular user can do right or can
- 13:14:37make
- 13:14:39or you can do running background checks
- 13:14:41uh sorry background jobs also running
- 13:14:43background jobs. So this is where you
- 13:14:44need radius. What radius? Radius is
- 13:14:47going to help. So it's going to help
- 13:14:48with this particular kind of task. Why?
- 13:14:51As I already mentioned, it's going to
- 13:14:52store the data in your RAM. It will be
- 13:14:54much faster for your sort lived. Sort
- 13:14:57lived. Okay, this is not permanent data
- 13:15:00or your frequent frequent accessed data.
- 13:15:05So this will be the main reason why we
- 13:15:07need this. Okay. Now what is the
- 13:15:10difference between? So as I already
- 13:15:12mentioned right it is a inmemory data
- 13:15:13store but what posgress usually do? So
- 13:15:15Postgress is our normal DB, right?
- 13:15:18Postgress is going to store the data.
- 13:15:20It's going to store the data where in so
- 13:15:23on disk. Okay. Why? Because this is a
- 13:15:27long-term storage versus radius is going
- 13:15:30to store in memory. Okay. So in RAM so
- 13:15:33it will be much faster. So if someone
- 13:15:36ask you okay what is the one single
- 13:15:37reason of using radius? Now remember the
- 13:15:40there can be so many reasons but number
- 13:15:43one reason will be the speed right. So
- 13:15:45the accessing of data will be much
- 13:15:47faster. So this would be the very basic
- 13:15:51definition what what is radius why
- 13:15:53actually we need radius even though we
- 13:15:55have a normal database right very very
- 13:15:57important concept all these so now what
- 13:16:00we going to do right so first of all
- 13:16:01let's do one thing we are going to do in
- 13:16:02a different way so first we are going to
- 13:16:05understand some of the very basic things
- 13:16:07for example some of the radius data
- 13:16:09types we have to understand so what we
- 13:16:12can do right so directly you can create
- 13:16:14a examples folder so that is the reason
- 13:16:16I mentioned that please create this uh
- 13:16:19uh sorry please for the uh download not
- 13:16:21for download the startup project it's
- 13:16:23very helpful okay so here I'm just going
- 13:16:25to do this one as radius data types and
- 13:16:29I'm not going to spend too much time on
- 13:16:31explaining so many things let's keep
- 13:16:32this whole uh video very very p
- 13:16:35practical and we'll see that it will be
- 13:16:37very easier for you to understand all
- 13:16:39right so first of all what we need to do
- 13:16:41right so we need to do something we need
- 13:16:44to add our uh radius image in for uh
- 13:16:48docker combos correct so first of all
- 13:16:50what are you going to do so let's add it
- 13:16:52here so let's add one more here so I'm
- 13:16:56just going to do this one as radius and
- 13:16:58here what I'm going to do so this will
- 13:17:00be so I'm just going to add a image so
- 13:17:01what will be the image so for this one
- 13:17:03I'm just going to do radius colon 7 so
- 13:17:06let's use radius 7
- 13:17:10and here I'm just going to use the
- 13:17:11container name so let's give this one as
- 13:17:14radius
- 13:17:16uh ports radius something like this
- 13:17:18whatever you want to give I'm going to
- 13:17:20do restart and this will be unless
- 13:17:23stopped
- 13:17:25okay let's use the ports so normally
- 13:17:27radius uses this 63 79 port okay so this
- 13:17:31is what we are going to map and then we
- 13:17:34are also going to have a volumes
- 13:17:37so for volumes this will be my radius
- 13:17:41data and here I'm just going to do /
- 13:17:44data and here we're going to have the
- 13:17:45radius
- 13:17:47data. Okay, so this is what we need.
- 13:17:51Okay, so this will be the first thing.
- 13:17:52So what are you going to do, right? So
- 13:17:54let's go here.
- 13:17:56So I'm just going to do docker compose
- 13:17:59down and then I'll docker compose up -
- 13:18:04d.
- 13:18:07Okay, so what you're getting uh
- 13:18:10networking this is already in used.
- 13:18:12Okay, so you're already using this.
- 13:18:16Maybe let's use this one. Change this
- 13:18:18one to some different code
- 13:18:22and then run once again.
- 13:18:25Okay, so it's running. So let's verify
- 13:18:27once
- 13:18:30go to image. Okay, we have ready. This
- 13:18:33is running. So now what we need to do,
- 13:18:35right, we also need to add this in our
- 13:18:37env. So in our env we don't have. So
- 13:18:39what we're going to do, right, I'm just
- 13:18:40going to add one more. So let's do this
- 13:18:42one as radius_l
- 13:18:44and this will be radius and this is
- 13:18:47running on local host col 6380
- 13:18:52right whatever port you are using that
- 13:18:54you have to use it so using 6380
- 13:18:57so now I think this is done so first of
- 13:18:58all what we are going to do first we
- 13:19:01will understand the basic radius data
- 13:19:03types or else we can't directly jump to
- 13:19:06the practical part okay very important
- 13:19:08all right so I'm just going to explain
- 13:19:10that what are the things we're going to
- 13:19:11learn. Okay. So, first I'm just going to
- 13:19:14So, here I'm just going to write. So,
- 13:19:15we're going to uh learn about string.
- 13:19:18Let's do has also I'm just going to
- 13:19:22let's do list set. There are so many.
- 13:19:25Okay. Then you have something called
- 13:19:27sorted
- 13:19:30sorted set and then you have TTL. Okay.
- 13:19:33So, these all the things we are going to
- 13:19:35understand. At the same time, I'm also
- 13:19:37going to give you the basic concept side
- 13:19:40by side and you'll see that how much
- 13:19:41easier it would be for you to understand
- 13:19:43all of these. So let's do this one in
- 13:19:45the next part. Okay. So let's start with
- 13:19:47string, right? So we have strings. So
- 13:19:49what is a radius string? The definition
- 13:19:52will be radics uh radius string stores
- 13:19:56one value under one key. Okay. So this
- 13:19:59usually stores
- 13:20:02one value under one key. Okay, what are
- 13:20:06the things that it can store? Right. So
- 13:20:08for example, first of all, plain text if
- 13:20:11you want to store. Next, it can also
- 13:20:14stored numbers
- 13:20:16stored as text. Okay, so these two and
- 13:20:20also something like counters.
- 13:20:23So this will be very simple definition.
- 13:20:25Now what will be the example? Let's say
- 13:20:26you have a key, right? So your key is
- 13:20:29something like we are let's say you're
- 13:20:31uh counting some page views that how
- 13:20:34many views this particular page gets
- 13:20:36something like that very simple example.
- 13:20:38So there will be some key okay so let's
- 13:20:40say this key is your page views and then
- 13:20:43there will be one value very very simple
- 13:20:45to understand and this value will be
- 13:20:47let's say it's 100 something like this
- 13:20:50okay so that is the reason I mentioned
- 13:20:52it's going to store one value under one
- 13:20:54key okay so now let's understand this
- 13:20:58one with a very simple example so first
- 13:21:00of all what we going to do we already
- 13:21:02have enval so I'm just going to do env
- 13:21:05from dot
- 13:21:07env and also we are going to import
- 13:21:11something
- 13:21:12from our radius package. So just now we
- 13:21:14have installed radius. So what are the
- 13:21:16thing what actually we need here? So we
- 13:21:18need to create a connection. So we need
- 13:21:20to import create client. Okay. So first
- 13:21:23what are we going to do right in our in
- 13:21:25view this rad whatever code that you are
- 13:21:27running and then what you're going to do
- 13:21:29first. So first we need to load the
- 13:21:31config. So I'll do.config.
- 13:21:33So this will be the first thing. And now
- 13:21:35we need our radius connection URL that
- 13:21:38we are going to use to create the
- 13:21:39connection. So I'm just going to do con.
- 13:21:41So I'll do radius URL and this will be
- 13:21:44equal to. So we'll do process env dot
- 13:21:47and we have our radius url right or else
- 13:21:50you can just simply directly pass this
- 13:21:52one here also. So copy this and then
- 13:21:55we're going to pass it here. So now what
- 13:21:57we need to do we're going to create one
- 13:21:58radius client. Okay. Why actually you
- 13:22:02need radius client if someone ask you.
- 13:22:04So radius client will be our main
- 13:22:06connection that we need so that we will
- 13:22:08be able to connect to radius. So I'm
- 13:22:10just going to do con radius. So this
- 13:22:12will be a create client and this
- 13:22:14actually you need the URL. Okay. So this
- 13:22:16is going to do the connection and what
- 13:22:18will be your URL? Your URL will be rad.
- 13:22:21So this is going to create the
- 13:22:22connection. So first of all what we
- 13:22:24going to do first? I'm just going to do
- 13:22:26a sync function and this will be run.
- 13:22:29And here first we need to see that if we
- 13:22:32are able to open the connection to our
- 13:22:35radius server or not. So I'll do open
- 13:22:37connection
- 13:22:38to radius
- 13:22:41server right. So how we going to do
- 13:22:43that? So this we have it and this radius
- 13:22:46instance that we have created this one
- 13:22:48right so here this is going to give a
- 13:22:50connect method and this is going to do
- 13:22:52the connection and then I'm simply going
- 13:22:54to do a console.log log and I'm simply
- 13:22:57going to do connected to radius because
- 13:23:00this we need to see and also I'm just
- 13:23:03going to do console.log log and let's
- 13:23:05say I'm just going to check the ping and
- 13:23:07this ping will be a so you have the
- 13:23:09radius dot ping right so this is going
- 13:23:13to uh explain us sorry not explain this
- 13:23:16is going to tell us that okay if the
- 13:23:18connection is successful or not so now
- 13:23:21what are going to do so this will be a
- 13:23:22promise so do run dot catch in case any
- 13:23:25error so we're going to get the error
- 13:23:28and then I'm simply going to do console
- 13:23:31dot error and I'm Just going to do demo
- 13:23:35or maybe let's what let's do demo field
- 13:23:37because we doing the demo right here. So
- 13:23:39demo field and this will be error and we
- 13:23:42are just going to exit from this
- 13:23:43process. So do process doexit will be
- 13:23:46one. So let's save this. So we we are
- 13:23:50not doing anything else here. Okay.
- 13:23:51We're not adding the example of string
- 13:23:53that we are going to do. So go to
- 13:23:55package dojson and then I'm just going
- 13:23:57to maybe let's do a example colon and
- 13:24:00I'll do radius types. So how we are
- 13:24:04going to run it. So this will be tsx and
- 13:24:08I'm just going to do so we have to do
- 13:24:10example right? So you have to go to
- 13:24:12examples because this is what we are
- 13:24:14using / the radius data type.ts. So I'll
- 13:24:18do radius data type not types.ts.
- 13:24:23So this will be our command that we are
- 13:24:25going to use directly in the terminal.
- 13:24:27So if I now open this let's clear it and
- 13:24:31I'll do npm run enter and you can see
- 13:24:35that it's now connected to radius and
- 13:24:37you're getting ping and this is the
- 13:24:39pong. So that means the connection is
- 13:24:41now successful and see this is the
- 13:24:43reason I when I usually it is right I
- 13:24:45use use docker because docker is if you
- 13:24:48have ID on docker it's very very easy to
- 13:24:50use and also using docker you can do lot
- 13:24:53of things okay see how much easier it is
- 13:24:55to understand that whether the
- 13:24:56connection is successful and if it is
- 13:24:58working or not but one thing you have to
- 13:24:59be make sure that you using correct
- 13:25:01ready URL here if you're doing any
- 13:25:03mistake it's going to fail so this is
- 13:25:05done so now let's start with uh what we
- 13:25:09have to do Okay, so let's start with
- 13:25:10some very basic string example, right?
- 13:25:12So we'll go here and then I'm just going
- 13:25:14to do strings
- 13:25:17and first let's do one thing. What I
- 13:25:19usually mention that there will be one
- 13:25:21key and for one particular key it's
- 13:25:23going to hold one particular value,
- 13:25:26right? So we're going to take a string
- 13:25:27key. Okay, so I'm just going to do
- 13:25:29string key and this will be maybe let's
- 13:25:32say I wanted to do get some page view.
- 13:25:34So I'm just going to do demo page views
- 13:25:37something like this and then what I'm
- 13:25:40going to do right I'm just going to do a
- 13:25:42and now we are going to use some methods
- 13:25:44that you have to understand. So what
- 13:25:45method we are going to use we're going
- 13:25:46to use a set method. So set method it's
- 13:25:49going to store that particular value
- 13:25:51under this key. Okay. So how we going to
- 13:25:53do that? So it's very simple. So do
- 13:25:55radius dot set
- 13:25:57and here you have to give what you have
- 13:25:59to give the key which is radius
- 13:26:00argument. So key is this and then we'll
- 13:26:03give the value right. So let's give this
- 13:26:05one as 100. So this is how you can set a
- 13:26:08particular value based on one key. Okay.
- 13:26:10I'm repeating this thing again and again
- 13:26:12that set is going to do what? It's going
- 13:26:14to store one value under one key. Okay.
- 13:26:18Now let's say I wanted to get this value
- 13:26:19or I wanted to read this value. So how I
- 13:26:22can do that? So now I'm simply going to
- 13:26:24do okay let's take the page views. So
- 13:26:26this will have it. Now I'll do radius
- 13:26:29dot get. And what it was it was a key.
- 13:26:31It's very simple. So we'll do strings
- 13:26:33key and then let's do a log. So I'll do
- 13:26:35console dot log and let's [snorts] take
- 13:26:38the page views. So now let's save this.
- 13:26:41Now what we can do right uh let's
- 13:26:44close this and I'll clear it and then
- 13:26:48run once again and we'll see what you're
- 13:26:50getting. So we're getting 100. Okay. Now
- 13:26:52what you can do right you can use
- 13:26:54another property uh on strings and that
- 13:26:56is called increment. insert something
- 13:26:58like incr. Okay. So what it's going to
- 13:27:01do? It's going to increase the number by
- 13:27:03one. But here you might be thinking okay
- 13:27:04this is a string. But what happens
- 13:27:07writing radius or normally the radius
- 13:27:09strings can also works work like
- 13:27:11counters. So I'm just going to write it.
- 13:27:13So radius
- 13:27:15strings can also work like counters. So
- 13:27:19now let's say if I wanted to do const
- 13:27:22after increment and I'll do a and I'll
- 13:27:25do radius and then you'll do in cr. So
- 13:27:29here what you have to pass. So you have
- 13:27:31to pass a string key, right? So this
- 13:27:34will be the key that you have to pass
- 13:27:35and then you'll just log it once again.
- 13:27:40Right? You'll save and run it again. So
- 13:27:43you can see that you're getting 101.
- 13:27:45Okay? So this is all about the strings.
- 13:27:48Now what will be that uh next step next
- 13:27:50will be h has. Now has is going to do
- 13:27:52what right? So let's understand here
- 13:27:54only.
- 13:27:55So has is going to so I'm just going to
- 13:27:58take this one as all right. So now let's
- 13:28:00understand h has right. So what actually
- 13:28:01radius has is so radius has is going to
- 13:28:04stores many uh many small fields under
- 13:28:07one key. So it's going to stores
- 13:28:10many small uh fields under one key. Now
- 13:28:15what will be a good example for this? So
- 13:28:18one good example for this will be uh so
- 13:28:21let's [snorts] say we have a small
- 13:28:22object right so this will be a good
- 13:28:24example or something like map inside
- 13:28:28radius so this will be your has right so
- 13:28:32now let's say uh there can be any key
- 13:28:35right so there is an any key so this is
- 13:28:38your key name and then you're going to
- 13:28:40have some fields here for that
- 13:28:42particular key so now let's say you have
- 13:28:44a uh field which is name and the name is
- 13:28:49Sam something like this and you also can
- 13:28:52be having a email and this is your email
- 13:28:56something like this so let's understand
- 13:28:58this one with example so what I'll do
- 13:29:00right so let's do this one as const and
- 13:29:03I'll do has key and what we can do maybe
- 13:29:06let's do demo colon user colon profile
- 13:29:11okay so now what we can do simply so I'm
- 13:29:14just going to do a And here we have a
- 13:29:16radius and we need to understand one
- 13:29:18method and that is called h set. So what
- 13:29:21this is going to do this is going to
- 13:29:23store the many fields that we wanted to
- 13:29:25have under this particular key. It's
- 13:29:27going to store that. Okay. So it's like
- 13:29:29a has like a small object inside radius.
- 13:29:33So this is the method we need to do how
- 13:29:36the structure will be. So here you have
- 13:29:38to pass the key. So this will be your
- 13:29:41key and then you can pass the property.
- 13:29:43So let's say I'm going to take a name
- 13:29:46and this will be I think this is wrong.
- 13:29:48This will be capital right? So here uh
- 13:29:51this will be key and then we have to
- 13:29:52take the property. So now let's say I'm
- 13:29:54just going to take this one as sum and
- 13:29:56I'm also going to take a city and let's
- 13:29:59do something like US. Okay. And then
- 13:30:01what you can do right uh so here uh
- 13:30:04let's say I wanted to get this value
- 13:30:07now. Okay. So this is how we are going
- 13:30:09to set some certain fields inside a uh
- 13:30:13inside a key using h has concept right.
- 13:30:15So we are using h set. Now to get this
- 13:30:19I'm just going to do extract profile
- 13:30:21info whatever you wanted to give here.
- 13:30:24So this will be equal to have it. So do
- 13:30:27radius and you're going to use h get
- 13:30:30all. So this is what you have to use.
- 13:30:33And here also you have to pass the key.
- 13:30:35So I'm just going to pass this one as
- 13:30:36has key. Now let's go here and then I'm
- 13:30:39just going to copy this
- 13:30:41and I'll paste it here and let's see
- 13:30:44what you're getting right. So I'll just
- 13:30:46run this one second. So I'm getting name
- 13:30:48and city. Okay. So this is how you're
- 13:30:50going to make use of uh has. Now all
- 13:30:54they might be thinking okay these are
- 13:30:56very uh simple concept but these simple
- 13:30:58concepts are actually important. Why?
- 13:31:00Because when you are going to learn all
- 13:31:02of these and then you're going to let's
- 13:31:03say you're creating a practical project
- 13:31:05there you have to understand whether you
- 13:31:07actually can apply all these concept
- 13:31:09inside your real project you're getting.
- 13:31:11So let's say you have a particular
- 13:31:13endpoint and you wanted to use radius in
- 13:31:16that. Now the requirement is such a way
- 13:31:18that you have to use string concept and
- 13:31:21has concept both. So this is the
- 13:31:23knowledge that you have to acquire that
- 13:31:26okay in that particular scenario you
- 13:31:28will be able to understand that okay
- 13:31:29which are properties I need to apply or
- 13:31:32what are the concepts so what I mean by
- 13:31:34it's not like we are blindly doing all
- 13:31:35of these at the end when we going to do
- 13:31:38practical you'll see that some of the
- 13:31:39things that we are doing here we will be
- 13:31:41applying in a real project and that will
- 13:31:43be your final outcome okay uh next uh
- 13:31:47what else we have okay let's understand
- 13:31:49list
- 13:31:51so list will be so I'm just going to do
- 13:31:55list
- 13:31:56so radius list will be ordered okay so
- 13:32:01this will be ordered collection of
- 13:32:04values okay so again here also you're
- 13:32:07going to have a key and then there will
- 13:32:08be some values okay so this is the
- 13:32:10simplest example that these will be some
- 13:32:12collections of values but it will be
- 13:32:14ordered so now let's see one simple
- 13:32:17example [snorts] so I'll just do here
- 13:32:19const I will do list
- 13:32:22key and here I'm just going to do maybe
- 13:32:24demo let's say I'm just going to take
- 13:32:26some messages in order and here what I
- 13:32:29will do simply first I'm just going to
- 13:32:30have it so I'll do list key and you're
- 13:32:33going to do l push
- 13:32:37no not loose man so here we are going to
- 13:32:40use a radius
- 13:32:43loose
- 13:32:45okay so this is what you're going to use
- 13:32:47you have to pass a key which will be
- 13:32:48list key and let's say I wanted to give
- 13:32:51uh Hello.
- 13:32:54Okay. And again, let's I wanted to add
- 13:32:56some more. So maybe I I'll do hi radius.
- 13:33:00All right. So now let's say we wanted to
- 13:33:01extract. So what I'll do, right, I'm
- 13:33:03just going to do extract
- 13:33:05messages and this will be avid and here
- 13:33:08we're going to have radius dot we have
- 13:33:11to use something called l range. Here
- 13:33:13we're going to pass the list key and
- 13:33:15then we're going to give the start and
- 13:33:17the stop. And now let's log this. Right?
- 13:33:20So I'm just going to do extract
- 13:33:22messages. Save this. And now let's
- 13:33:26clear. And then I'm just going to run it
- 13:33:28again.
- 13:33:30Okay. So you can see that first you're
- 13:33:31getting high radius and then hello. This
- 13:33:33is why because we using l push. Okay.
- 13:33:36Now if you're getting confused, let me
- 13:33:39just quickly show you that diff between
- 13:33:42lus
- 13:33:44and rpus.
- 13:33:48So you can see that now L pus is going
- 13:33:50to do what? It's going to insert the X
- 13:33:53at the left side. Okay. R push is going
- 13:33:56to insert this one on the right side. So
- 13:33:59now if I'm first adding this one, so
- 13:34:01this will be added. When I'm going to
- 13:34:02add high radius, it will be added on the
- 13:34:04left side. So that's why you're getting
- 13:34:06this. You're getting this one. And that
- 13:34:08is the reason you're getting hello on
- 13:34:10the at the end. Okay? Because we using
- 13:34:13loose. If you want to get in the reverse
- 13:34:16order, what you have to use? you to use
- 13:34:18arpus. Okay. So I'm just going to give
- 13:34:19this one as a homework to all of you. So
- 13:34:22this is about the uh list. Next uh what
- 13:34:26we have I think we also have to discuss
- 13:34:28about. So let's uh discuss about set. So
- 13:34:31now I'm just going to do this one as
- 13:34:32set. So what actually set? So set will
- 13:34:35be or okay let me just quickly write it
- 13:34:37here. It will be easy. So loose right.
- 13:34:39So loose will be adds a new item at
- 13:34:44beginning.
- 13:34:47Then you're going to have l range. L
- 13:34:49range is going to do what? It's going to
- 13:34:51reads item items from the list. Simple.
- 13:34:56And then we going to have uh we also
- 13:34:58going to have what? L arpus. So we also
- 13:35:02have the arpus. Arpus is going to will
- 13:35:04be what adds the item
- 13:35:07at end. Okay, we also have something
- 13:35:10called L stream, right? These all are
- 13:35:13very important one. So it's going to do
- 13:35:15what? It's going to keep
- 13:35:17only part of the list. You can also
- 13:35:21verify. So if you go here and let's do L
- 13:35:24trim.
- 13:35:26So L trim is a command to trim an
- 13:35:29existing list. So it contain only the
- 13:35:31specific range of elements. Okay. So you
- 13:35:33have to give the key the start and the
- 13:35:35stop. So this is about the list. So now
- 13:35:40let's move to the set. So what will be
- 13:35:41set? Set is like if you have have some
- 13:35:44idea on JavaScript sets how it usually
- 13:35:46works. So set is usually stores unique
- 13:35:49values only. Okay. So it is going to
- 13:35:51sets unique unique
- 13:35:54sets of values only. Okay. So now uh
- 13:35:59what will happen? Uh this can be
- 13:36:01interview question but this will be very
- 13:36:02beginner level of questions. What will
- 13:36:04happen if I try to add same element
- 13:36:06multiple times? So it's going to ignore
- 13:36:08the one that you have already added.
- 13:36:11Very very simple concept. So how it will
- 13:36:14work? So let's go here and then I'm just
- 13:36:15going to do con. Let's do a set key. And
- 13:36:18I'm just going to let's say I have some
- 13:36:20tags and I wanted to get the tags only.
- 13:36:23And here I'm just simply going to add
- 13:36:24it. And you're going to do radius. And
- 13:36:26now this will be not loose. This will be
- 13:36:29add. So s add is going to add and it's
- 13:36:33going to create a set and it's going to
- 13:36:36add that unique value. So what I mean by
- 13:36:38unique value here. So now let's first of
- 13:36:40all I'm going to give set key and I'm
- 13:36:41adding here node JS. And now I'll just
- 13:36:45uh let's paste it. Let's do next JS and
- 13:36:49I'm just going to do uh next JS again.
- 13:36:52Okay. So there is a duplicate. Okay. So
- 13:36:54what will happen here? So first of all
- 13:36:56let's say I wanted to get or I wanted to
- 13:37:00uh return how many unique members exist
- 13:37:03in this particular set. So how I can do
- 13:37:06that? So first of all let's say I wanted
- 13:37:07to get the tag count and this will be a
- 13:37:10we have to use something called s card.
- 13:37:13Okay. And here we're going to pass a set
- 13:37:15key. Now let's see what is happening.
- 13:37:17Right. So I'm just going to log this.
- 13:37:23So do tag count save.
- 13:37:30Now you can see that you're getting only
- 13:37:31two. Why? Because even though you've
- 13:37:34added n just two times, it is going to
- 13:37:36consider the nes unique. Okay. The first
- 13:37:39one. Next one it's going to ignore.
- 13:37:41Okay. Then we have something called
- 13:37:43sorted set also. Okay. So let's say uh
- 13:37:46I'm just going to do con rank key
- 13:37:49something like this. Rank key. And then
- 13:37:52I'm I'll do demo and let's I just going
- 13:37:54to take some kind of leader board and
- 13:37:57I'll do a radius dot we have something
- 13:38:01called Z add. Okay. So what J uh Z add
- 13:38:05is going to do is going to store your
- 13:38:07member plus scores. When you have some
- 13:38:10kind of uh condition or feature that you
- 13:38:13have to implement where you need to
- 13:38:15actually get the data based on ranking
- 13:38:17it's very very very important concept.
- 13:38:20Okay. So now what we going to do? We'll
- 13:38:22lose Z at. So you have to pass the key.
- 13:38:24So we'll do rank key and I'm just going
- 13:38:26to give a score here. See score will be
- 13:38:29100. Okay. And you have to give a value
- 13:38:32here also. So now let's say this is a
- 13:38:34player A. Now I'm going to copy this and
- 13:38:37paste it here. So this will be score uh
- 13:38:41200 and this will be my player B. So now
- 13:38:44what we going to do right? So let's say
- 13:38:46uh I wanted to do something. I want to
- 13:38:49increase this particular score that I
- 13:38:52have by a particular amount. So to do
- 13:38:55this one, we have something called Z
- 13:38:57increment by that I'm talking about full
- 13:38:59form but in short we have to do
- 13:39:01something like this. So let's say I
- 13:39:02wanted to get the new score. So first I
- 13:39:06will do a so this will be Z. Sorry again
- 13:39:09I'm doing Z. So this will be radius dot
- 13:39:12Z increment by
- 13:39:17you have to pass the key here and let's
- 13:39:19say you have to pass the increment
- 13:39:20number. So let's say by 50 and also you
- 13:39:22have to pass the member. What will be
- 13:39:24your member? Member will be your player
- 13:39:25A. Okay. So here you have to give this
- 13:39:28one as player A.
- 13:39:31So now let's log this. So this will be
- 13:39:35the new score. So now let's save this
- 13:39:40and if I run it again. So what we'll be
- 13:39:43getting? We'll be getting 150. Very very
- 13:39:45simple. So we are just increasing this
- 13:39:47one by 50. Now if you wanted to get the
- 13:39:50rank from highest score to lowest score
- 13:39:54there you have to use something called Z
- 13:39:56rev rank. So I'll do con rank
- 13:40:03and this will be a radius dot z uh we
- 13:40:08have the rev rank okay so here you have
- 13:40:12to pass the rank key and let's say what
- 13:40:15I wanted to do I wanted to pass the
- 13:40:17player B
- 13:40:20okay and here let's I'm just going to do
- 13:40:22a log and maybe I'll do a rank here so
- 13:40:27if I Save this.
- 13:40:31So you can see that you're getting zero.
- 13:40:33Okay. Now why we are getting zero? The
- 13:40:36reason is because zero will be always
- 13:40:38considered as top rank. Okay. So what
- 13:40:41this is going to do? This is going to
- 13:40:43give the rank from highest score to the
- 13:40:45lowest score. So now I think this is
- 13:40:48done. If you are getting confused, let
- 13:40:49me just uh give you one example. Right?
- 13:40:52So I'm just going to copy this and I'll
- 13:40:54just do here. Okay. Tell me why the last
- 13:40:57rank
- 13:40:59console is giving zero. Okay. So let's
- 13:41:03see
- 13:41:05now see it's printing zero because
- 13:41:07calculate zero to index rank order from
- 13:41:09highest score to lowest score. So this
- 13:41:11is I was talking about. Now this might
- 13:41:12be little confusing because when uh I
- 13:41:16I remember I learned for the first time
- 13:41:18right I was like oh my okay what is
- 13:41:20happening here. So this is the reason I
- 13:41:22mentioned. So if you see okay first of
- 13:41:24all see this is going to add this one so
- 13:41:26it's going to get first time when I'm
- 13:41:28going to do Z add it's going to get C
- 13:41:30score of 100 player B is going to get a
- 13:41:32score of 200 fine I added 50 okay so
- 13:41:35it's become 150 right so player A is
- 13:41:37become 150 so now the final reader will
- 13:41:40be what 200 150 so what Z rank is going
- 13:41:44to do I already mean it's going to give
- 13:41:46you the rank from highest score the
- 13:41:48lowest score so who is the lowest sorry
- 13:41:50highest score here still player Correct.
- 13:41:53So that is the reason you are getting
- 13:41:54this. So now it's going to do what? It's
- 13:41:56going to sort the leaderboard in the
- 13:41:57descending order. So highest order is
- 13:41:59going to get the top uh position. So now
- 13:42:02I what I mentioned here that zero will
- 13:42:04be equal to always what? Highest score.
- 13:42:06Okay. Or top rank. Zero will be
- 13:42:10I'm just going to do like this. So this
- 13:42:12is how I don't I remember this one. So
- 13:42:14zero will be considered as top rank.
- 13:42:16Right? So now this will be your uh rank
- 13:42:19zero, first place highest score, player
- 13:42:21B and rank one because we are passing
- 13:42:24player B has the absolute highest score.
- 13:42:27So their reverse rank is exactly zero.
- 13:42:30So when you're passing player B, so
- 13:42:31you're getting this one as zero. What
- 13:42:33will happen if I pass this one as player
- 13:42:34A? I think you can actually guess it. So
- 13:42:37it will become one because this will be
- 13:42:38the second player. Okay, because B is
- 13:42:41first here. Now I'll give you one
- 13:42:43homework because we're adding by 50
- 13:42:45right here. players could do this. Uh
- 13:42:48you can just add one more time, right?
- 13:42:50Let's say you added 100 or 200. Now see
- 13:42:53that the moment player will be increase
- 13:42:56by player player B. So now let's say
- 13:42:58player A score is 300. You'll see that
- 13:43:00when you're going to uh console the
- 13:43:03rank, player A will become the highest
- 13:43:05rank and then the player B something
- 13:43:06like that. Okay. So this is about the uh
- 13:43:09uh how you can work with sorted set.
- 13:43:12Okay. Very important concept. uh what
- 13:43:14else we have? I think we have already
- 13:43:17complete most of the things. So the last
- 13:43:19I think we have to also understand some
- 13:43:20of the very basic concept of TTL. Okay.
- 13:43:24So first uh what I will do right? So
- 13:43:26let's understand very basic concept here
- 13:43:28what actually TTL means. So TTL will be
- 13:43:31as much I can recolct it will be time to
- 13:43:35leave. Okay. Or you can also consider
- 13:43:38this one as expiry. Right. So this is
- 13:43:40expiry. So we can just verify. So TTL
- 13:43:43means
- 13:43:44time to leave, right?
- 13:43:51Okay. Okay. So let's take one. So this
- 13:43:54will be TTL or XY. So what this uh is
- 13:43:58going to be? Sorry, why this is going to
- 13:44:00be helpful? What this is going to tell
- 13:44:02radius? So it is uh it's going to tells
- 13:44:05radius that how long Okay, you can note
- 13:44:07it down. how long a particular key
- 13:44:09should exist before it's going to
- 13:44:12deleted automatically. This is a very
- 13:44:13important concept. Okay. So, it's going
- 13:44:15to tell it tells radius that how long a
- 13:44:20key how long a key should exist
- 13:44:24before being deleted automatically.
- 13:44:31Okay, you're getting so now [snorts]
- 13:44:33maybe let's say I have a key here,
- 13:44:35right? So I have a key of a and then
- 13:44:37this key has a value of something maybe
- 13:44:40any value you can take and I'm giving a
- 13:44:42ttl of 300 seconds. So what will happen
- 13:44:46that means after 5 minutes radius is
- 13:44:49going to radius is going to delete this
- 13:44:54key automatically.
- 13:44:57Okay, I hope this is clear. So now let's
- 13:45:00understand maybe uh we'll just do like
- 13:45:03this, right? So I'm just going to do
- 13:45:04cons. Let's take a OTP key or whatever
- 13:45:07key you wanted to take. So I'll do demo
- 13:45:09OTP. And here I'm simply going to do a
- 13:45:12I'll do radius. And you can use simply
- 13:45:14set.
- 13:45:16And you're going to pass the OTP key.
- 13:45:18And I'll just do here 1 2 3 4 5 6. We're
- 13:45:21just setting the value here. Okay. Now
- 13:45:24I'm going to add a expired date here. So
- 13:45:26how you can do? So do radius dot expire.
- 13:45:30And you have to pass a key here. OTB
- 13:45:32key. And this will become 60. Okay. So
- 13:45:36once this TTL will end, radius is going
- 13:45:38to delete this automatically. So here
- 13:45:40what will be the expired time? The
- 13:45:42expired time will be 60 seconds. Okay.
- 13:45:45Because we are using 60. Okay. Now what
- 13:45:48you can do right? We can use TTL. So
- 13:45:50what TTL is going to do? TTL is going to
- 13:45:52show you the remaining seconds before
- 13:45:54it's going to expire. You're getting
- 13:45:56time to what I mentioned time to leave.
- 13:45:59Right?
- 13:46:00So now if I simply do con what will be
- 13:46:03the TTL
- 13:46:05so I'll do a radius TTL
- 13:46:09and you'll simply pass OTP key and then
- 13:46:12you'll just do console
- 13:46:14dot log and I can log this DTL okay so
- 13:46:18now if I go here and let's say I wanted
- 13:46:20to run this so it will be showing as 60
- 13:46:23because now we just started so once this
- 13:46:25detail is going to end radius is going
- 13:46:28to delete this particular automatically.
- 13:46:30Okay. So I think this is done. Now one
- 13:46:33last thing we have to understand that's
- 13:46:34I kept this one intentionally and that
- 13:46:37is how we are going to close the radius
- 13:46:38connection because you notice that it's
- 13:46:40actually not closing and to close the
- 13:46:42radius connection it is very very
- 13:46:44simple. I just have to do a and I'll do
- 13:46:46radius dotquit. Okay. And this is done.
- 13:46:49So now the moment I'll save this and
- 13:46:51you'll see that now if I going to run
- 13:46:52this it's closing. So that is the reason
- 13:46:54I kept that part intentional so that you
- 13:46:57can understand. Okay. So now this is how
- 13:46:59you are going to quit or how you're
- 13:47:02going to close your radius connection.
- 13:47:04So with this I think we have covered
- 13:47:06most of the things. These things are
- 13:47:07very very important. So now what will be
- 13:47:09the next step? Next step will be now we
- 13:47:10need to start working on our actual
- 13:47:12project. So now we're going to set up
- 13:47:14our radius and after that we'll move to
- 13:47:16the next section. All right. So moving
- 13:47:18to the next one. So now we have some
- 13:47:19basic understanding of the data types
- 13:47:21and how you can run and see the output
- 13:47:24and everything. Right. So what will be
- 13:47:26the next step? Now remember there are
- 13:47:27some more examples that we need to
- 13:47:29understand. Okay. But this time what we
- 13:47:31going to do right because now we already
- 13:47:33have some very basic idea. So some of
- 13:47:35the practical things now we are going to
- 13:47:37apply in our project. Okay. So this is
- 13:47:39how we are going to understand all the
- 13:47:41things. So what we have to do? So first
- 13:47:43thing we will start with creating the
- 13:47:45radius connection in our project. Now
- 13:47:47remember this is now not example one.
- 13:47:50This is actually how we are going to
- 13:47:52apply in a project. Okay. So first what
- 13:47:54we need to do? So first we need to see
- 13:47:55that some of the things whether we have
- 13:47:57completed or not. So what will be the
- 13:47:58first thing? First thing we need to see
- 13:48:00in your project. Now why I'm explaining
- 13:48:02all of this? Now forget about this
- 13:48:03example folder. This example folder is
- 13:48:05the dummy code snippet kind of thing
- 13:48:07that will give you the basic idea. Now
- 13:48:10when you implement in a project so first
- 13:48:12thing you have to understand that okay
- 13:48:13whether the radius is installed or not.
- 13:48:15So yes we have already installed radius
- 13:48:17right. So we have the radius package
- 13:48:19here. Second because we using docker we
- 13:48:22have the docker uh service is running or
- 13:48:24not. Yes docker service sorry not yeah
- 13:48:26docker radius service is running correct
- 13:48:29also we have the radius volumes or
- 13:48:30radius data. So first thing we need to
- 13:48:33do what? So first we need to create our
- 13:48:35actual radius connection for this
- 13:48:36particular project. Second we are going
- 13:48:38to use that connection where in our main
- 13:48:40serverts file. So these two things we
- 13:48:43are going to do. So first maybe what we
- 13:48:46can do right. So uh let's create one
- 13:48:48radius folder here.
- 13:48:51Inside this let's create a client ds.
- 13:48:55Okay. So now first of all what we going
- 13:48:57to do now you see that once you
- 13:48:59understand some of the concepts how much
- 13:49:00easier it will be for you to know. So
- 13:49:02first of all you need to get something.
- 13:49:05So what I need to get I need to get the
- 13:49:07create client. This is what we have
- 13:49:08learned right in the previous section.
- 13:49:11So first I'm just going to do con radius
- 13:49:14url. So this will be process env dot.
- 13:49:19So rad or so we are going to use that
- 13:49:23whatever we have here. So copy this and
- 13:49:26then paste it here. So now I'm just
- 13:49:28going to do so we need to create a
- 13:49:30radius client. Correct. So I'm just
- 13:49:32going to do export cons. Why we need to
- 13:49:33do export? Because we can or we need to
- 13:49:36use this radius client in our other
- 13:49:38places. So I'm just going to do radius
- 13:49:40client and this will be equal to create
- 13:49:43client. And here we are going to pass
- 13:49:45the URL and this will be your radius
- 13:49:48URL. Sorry not radius URL we have to
- 13:49:51use. Now what we need to do we need to
- 13:49:54fire some of the events. So what are the
- 13:49:56things we need to fire? So first I
- 13:49:58wanted to see that uh whether the
- 13:50:00connection or the TCP connection is
- 13:50:02established or not. Okay. So how we can
- 13:50:04do that? So we have radiusclient do on
- 13:50:07and here we have a method called
- 13:50:08connect. Now this is going to fire when
- 13:50:13the TCP connection to radius is
- 13:50:15established. Okay. And here what we can
- 13:50:18do we don't have to do anything else in
- 13:50:19the call back. We can just log the
- 13:50:22error. So what you can do right you'll
- 13:50:23just do radius client
- 13:50:26connected something like this. So this
- 13:50:28is the first one. Then we need to see so
- 13:50:32this is only the connect. Okay. One we
- 13:50:34need to take for ready. So when the
- 13:50:36connection is ready. So here I'm just
- 13:50:37going to do this one as ready. So this
- 13:50:40will be radius client ready. Okay. Also
- 13:50:43we can take one more which will be
- 13:50:45error. So if there are any kind of
- 13:50:46error. So I'm just going to do this one
- 13:50:48as error. And this will be radius client
- 13:50:51error because this is a call back. You
- 13:50:53can take a error here. And that error
- 13:50:54you can just log here. Okay. That's it.
- 13:50:58And then we are going to take last one
- 13:51:00which will be end. So this will be end.
- 13:51:03And this will be when the connection
- 13:51:05will be closed. We don't need this one.
- 13:51:06We can remove this. and we'll do radits
- 13:51:09client connection closed. So this is
- 13:51:12done. So now what are we going to do
- 13:51:14right? So first of all I'm just going to
- 13:51:16create one more function here. So I'll
- 13:51:18just do export async function and I'll
- 13:51:22just do connect radius.
- 13:51:26So what will be the return? So this will
- 13:51:28be promise and then we just have to take
- 13:51:30a hide here. So for this one, first of
- 13:51:33all, I'm just going to check if the
- 13:51:35radius client that we have is open. If
- 13:51:38this is false, so why? So what we need
- 13:51:40to do here? So we are going to only
- 13:51:42connect if the client is not already
- 13:51:44open. You're getting so this is what
- 13:51:46you're checking. If the client is
- 13:51:47already open, we don't have to do
- 13:51:48anything. So this is not so in that case
- 13:51:51we have our radius client and here you
- 13:51:53have to do connect. Okay. So here now
- 13:51:55only wanted to call connect. So this is
- 13:51:57the first thing and then ping pong that
- 13:51:59concept right ping means we are going to
- 13:52:02give ping and we need to see whether we
- 13:52:04are getting the ping response or not as
- 13:52:06pong. So I'm just going to do here con
- 13:52:09uh let's do pong and I'll do a
- 13:52:12radiusclient dotping
- 13:52:15and here I'm just going to do so let's
- 13:52:18do this one maybe uh let's log it only I
- 13:52:22think that is fine so I'll just do here
- 13:52:25radius
- 13:52:27ping response and this will be and I
- 13:52:31think this is done and then I'm also
- 13:52:33going to take one more here so I'll do
- 13:52:34export as sync function and I'll Just do
- 13:52:37this one as uh disconnect
- 13:52:40radius. So this will be again promise
- 13:52:45void and here we are going to check. Now
- 13:52:48what you have to check if the uh
- 13:52:50connection is already open then you're
- 13:52:51going to close it. You're getting if the
- 13:52:53connection is not open how we are going
- 13:52:55to close it. So to check if the radius
- 13:52:57client dot is open that means if it is
- 13:53:00already open then how I'm going to close
- 13:53:03it. Now see this concept we have learned
- 13:53:04just now. So this is what I'm talking
- 13:53:06about repeating every time not only this
- 13:53:09video in all the videos in last 2 three
- 13:53:11years whatever videos I'm doing right.
- 13:53:13So what I mean by this whenever you
- 13:53:15learn learn any concept you have to see
- 13:53:17whether you are able to apply that
- 13:53:19concept practically in a project or not.
- 13:53:21If you're not able to do it that means
- 13:53:22there is no point of learning that
- 13:53:24concept you're getting. So let's say
- 13:53:25you're learning a nodejs concept called
- 13:53:28file system and you're building a real
- 13:53:31practical project and you have to see
- 13:53:32whether those file system concepts you
- 13:53:34can apply in that project or not. Okay,
- 13:53:37no one in in any project you are not
- 13:53:39going to get like this. Okay, there will
- 13:53:40be some simple thing and then you're
- 13:53:42just going to apply file system read
- 13:53:44this method there will be situational uh
- 13:53:46concept right so in that particular
- 13:53:48situation you have to understand whether
- 13:53:49you can apply that concept or not so
- 13:53:52here if it is opened so I need to close
- 13:53:54this so how I'm going to close it so we
- 13:53:56have to call that quit method that we
- 13:53:58have learned right so this is going to
- 13:54:00close that connection so I think this is
- 13:54:02done right so this is clear now what
- 13:54:04will be the next step next will be now
- 13:54:06this is not yet completed because in the
- 13:54:08serverts we need to use this. So now
- 13:54:11here you can see that we are calling the
- 13:54:13test connection. So first of all I need
- 13:54:14to connect to my radius. Okay. So I'm
- 13:54:17just going to do a and I'll do connect
- 13:54:20radius. So we need to check here on the
- 13:54:22startup and also let's do one more thing
- 13:54:26here. I'm just going to do process dot
- 13:54:28on and what we need to do right see when
- 13:54:31we going to stop the server we have to
- 13:54:34close the credit connection also. So how
- 13:54:36we can do that right? we have the sign
- 13:54:40uh sign s it okay and here I'm just
- 13:54:43going to take async and then I'm simply
- 13:54:46going to do a and we have the disconnect
- 13:54:49radius and then you have to exit so we
- 13:54:51do process doexit
- 13:54:55no process doexit of zero and I think
- 13:54:58this is done so this what we need to do
- 13:55:00so now if I go here I think it's already
- 13:55:03running so you can see it's already
- 13:55:05connected which is fantastic but still
- 13:55:07I'm just going to run once again and if
- 13:55:09you run here you see the postgress is
- 13:55:10connected successfully ready is client
- 13:55:12connected ready and then you're getting
- 13:55:14the response also and uh this is the
- 13:55:17pong response that you're getting right
- 13:55:19so and the server is running so that
- 13:55:20means now we successfully
- 13:55:23uh initiated the radius connection and
- 13:55:26also we are able to we will be able to
- 13:55:29disconnect the radius also whenever
- 13:55:30you'll be stopping the server okay so
- 13:55:33this this is one small section that we
- 13:55:35have to do now again what going to do
- 13:55:37right let's move back to our examples
- 13:55:40we'll understand some more concepts then
- 13:55:41we'll again come back and then we are
- 13:55:42going to apply those concepts in this
- 13:55:44wheel all right so now let's move on to
- 13:55:46the next one which will be so we need to
- 13:55:47understand some of the more concepts
- 13:55:49okay so first so let's understand that
- 13:55:53what actually casting is right so
- 13:55:55because this is one of the main core
- 13:55:57concept that we need to understand and
- 13:55:59also you're going to understand what is
- 13:56:00radius caching that I mentioned and also
- 13:56:02invalidation both seeing means uh like
- 13:56:06If I wanted to explain a very simpler
- 13:56:08way. So let's say you are storing some
- 13:56:11uh data. I will not say data but storing
- 13:56:13the copy of data. Okay. In a fast place
- 13:56:16so that whenever you're going to do
- 13:56:18future request it can read that
- 13:56:21particular data from that place very
- 13:56:23quickly. Okay. So this will be very
- 13:56:24simple uh human like definition not like
- 13:56:27you are checking from charge but but uh
- 13:56:30this is what I will explain. Okay. So
- 13:56:31the casting will be it is like storing
- 13:56:34but the difference is see storing the
- 13:56:36copy of data storing the copy of data uh
- 13:56:40in a first place. So this can be any
- 13:56:42place. Now in this case we are using
- 13:56:44radius. So it will be in memory. So that
- 13:56:47uh so it will be stored in a first
- 13:56:48place. So future request
- 13:56:53can be can be read very quickly. So this
- 13:56:57will be a very basic definition and
- 13:56:59proper definition of caching right. So
- 13:57:01instead of fing the same data again and
- 13:57:03again uh we are actually reading the
- 13:57:06data from a cast. Now I already
- 13:57:09mentioned right that why we using radius
- 13:57:11because radius is in memory. So reads
- 13:57:13will be very very fast right it should
- 13:57:15be very very very fast extremely fast
- 13:57:18right now some of the reason that why we
- 13:57:22need to use casting. Now this is there
- 13:57:23can be so many reasons okay but I'm
- 13:57:26going to give you some of the very uh
- 13:57:27simple ones. So for example let's say
- 13:57:29you want to reduce your database load.
- 13:57:32This is one of the reason want to
- 13:57:34improve the response time. I think this
- 13:57:36will automatically come because uh the
- 13:57:38uh radius is very fast right so it's
- 13:57:40definitely going to improve your
- 13:57:41response time and also it's going to
- 13:57:44handle the repetitive reads properly or
- 13:57:48efficiently. Now there are some patterns
- 13:57:50that you have to understand. So we have
- 13:57:52something called cast aside pattern. So
- 13:57:55how this will work right in a simpler
- 13:57:56way. So first it is going to check the
- 13:57:59cast. So let's see if you have something
- 13:58:01cash if this is stored or not. Two. So
- 13:58:04this will be I'll do one
- 13:58:08here
- 13:58:10and then in the two this will be if the
- 13:58:13cast has already data. So it's going to
- 13:58:16return it if not if the cast doesn't
- 13:58:19have data.
- 13:58:22If the C doesn't have data so what I'll
- 13:58:26do it's going to read from your read
- 13:58:28from main DV okay and then once you read
- 13:58:32it then [clears throat] you're going to
- 13:58:33save it save in cash so what will happen
- 13:58:37from the next time it's going to read
- 13:58:38from the cache because that time cast
- 13:58:40will be having the data okay so this
- 13:58:43will be a cast aside pattern there is
- 13:58:46another concept called cash heat so what
- 13:58:49what is cast Right? So C hit will happen
- 13:58:53whatever data that you were requesting.
- 13:58:54So the requested data is already in C.
- 13:58:57Correct? So now consider you have
- 13:58:59something called a products endpoint,
- 13:59:01right? And here what will happen right?
- 13:59:04So let's say radius already has
- 13:59:06something called products colon all. So
- 13:59:10that means these products data is
- 13:59:12already present in this particular key.
- 13:59:14So what will happen? It's going to
- 13:59:15return the cache data immediately. So
- 13:59:18return the cash data immediately. Now
- 13:59:22one thing uh so let's understand here.
- 13:59:24So I'll do what is cast
- 13:59:27aside and cash hit.
- 13:59:33So see the C the application looks for
- 13:59:36data finds it and return it instantly.
- 13:59:39Right? Uh this is the optimal path
- 13:59:41because it bypasses the slow main
- 13:59:43databases entirely. Correct? Then you
- 13:59:45have cash miss also something cash asset
- 13:59:48pattern. So this is called lazy loading.
- 13:59:50So you can check it or I'll just read it
- 13:59:53properly that in this pattern the
- 13:59:54application logic directly manages both
- 13:59:56the cache and the database. Correct? I
- 13:59:57already mentioned that if it is present
- 13:59:59you give me the data back right you have
- 14:00:01the client request data application
- 14:00:03right and then you have the cash hit so
- 14:00:05it's going to return the data. So you
- 14:00:07have a radius cast right? If there is a
- 14:00:08miss so it's going to take it from DB
- 14:00:10and then it's going to return the data.
- 14:00:12So this will be your cash aside pattern.
- 14:00:14Cash M will be nothing but when your
- 14:00:16cash doesn't have that particular data.
- 14:00:18Okay. So in this case let's say you're
- 14:00:20getting the list of products and this
- 14:00:22cash is not present. So what will
- 14:00:24happen? It needs to return the data
- 14:00:26right. So in that case it's going to get
- 14:00:28the data from your DB and then you have
- 14:00:30something called TTL. TTL I've already
- 14:00:32mentioned that that will be time to
- 14:00:33leave. Right? So it's going to tell that
- 14:00:35how long that C data should stay before
- 14:00:38we are going to delete it automatically.
- 14:00:40So these will be all the concepts. Now
- 14:00:42because we already know all this, right?
- 14:00:44Let's see how much easier it will be for
- 14:00:45us to understand these things
- 14:00:47practically. So what I'll do right so
- 14:00:49we'll go here and let's do this one as
- 14:00:52radius caching. We'll do all at once.
- 14:00:54Okay, all the concepts. So I'll do
- 14:00:56radius caching. Let's go to package.json
- 14:00:59and I'm just going to copy this so that
- 14:01:01we can run it
- 14:01:03and I'll do radius
- 14:01:06caching and this will be radius casting
- 14:01:10right casting.
- 14:01:13Okay. So let's go here. First of all,
- 14:01:15what I'll do? So we'll go to this.
- 14:01:20I'll close this. So let's go here. I'm
- 14:01:23going to copy till this part. Not going
- 14:01:26to write. Okay. So these five things we
- 14:01:28need. So we we are creating this. So now
- 14:01:31first of all, we need to have our cast
- 14:01:33key. So you should always have your cast
- 14:01:35key, right? Cash key is there. Something
- 14:01:37like C key there. So what is your cast
- 14:01:40key? So I'm just going to take this one
- 14:01:41as let's say cash key. So this will be
- 14:01:44your demo colon products. So this is
- 14:01:48where I want to store my list of
- 14:01:49products in my cash. Okay. Then you're
- 14:01:52going to have a cash DTL in seconds. So
- 14:01:58I'm just going to do second. So what
- 14:01:59this means? This means cash is going to
- 14:02:02expire after 60 seconds. cash
- 14:02:07will expire
- 14:02:09uh after 60
- 14:02:13seconds. This is also very important
- 14:02:15because you see you can't give your TTL
- 14:02:19log also you have to invalidate the
- 14:02:21cash. Why? So let's say you have 10
- 14:02:24products [snorts]
- 14:02:25and you're getting the data from cash.
- 14:02:27It's fine. Now let's say there is some
- 14:02:29update in the data or there is a new
- 14:02:31product added. So now your 10 products
- 14:02:34will become 11. So if your data is
- 14:02:37already present in the cash and it's
- 14:02:39getting returned from the cash, it
- 14:02:40always going to return you the 10 data,
- 14:02:42not the 11 data and also not the updated
- 14:02:44data. For example, let's say there is
- 14:02:46one product and the product price is
- 14:02:48changed from 100 to 500. So in that case
- 14:02:51it will always return you to 100. It's
- 14:02:53not going to return the updated data. So
- 14:02:55that is the reason you have concept of
- 14:02:56invalidation. So you are going to
- 14:02:58invalidate that cast so that it's going
- 14:03:00to return the updated data back. So here
- 14:03:03I'm just going to do con products
- 14:03:06and I'm just going to let's take
- 14:03:07something like this right. So I'll do
- 14:03:08keyboard and then mouse
- 14:03:14and laptop something like this. So this
- 14:03:18is my fake database data. Now what I'm
- 14:03:21going to do I'll just do async function
- 14:03:25run and here first we're going to
- 14:03:28connect I'm just doing little first
- 14:03:33and then what I will do also let's do
- 14:03:36this right so I'm just going to close it
- 14:03:38also
- 14:03:40at the end
- 14:03:42so now first of all I uh I'm doing some
- 14:03:46request okay so this is my first
- 14:03:50request request. So what will happen in
- 14:03:53the first request? Pause the video and
- 14:03:55try to understand. See in the first
- 14:03:56request you it will not be cash hit it
- 14:04:00will be cash miss. Why? Because in the
- 14:04:03first request this data is not present
- 14:04:06in your radius cache. Correct? So what
- 14:04:08will happen every time whenever there
- 14:04:10will be first request will be made it is
- 14:04:13going to get the data from your DB. It's
- 14:04:15very simple concept. So that means first
- 14:04:17time it will be cast miss. Second time
- 14:04:21once it is cach means it's going to get
- 14:04:22the data from your DB we are going to
- 14:04:25store the data in your radius. So next
- 14:04:26time it's going to read the data from
- 14:04:28your cache. So now let's I'm just going
- 14:04:30to do let cast
- 14:04:33and this will be my ait and then I'll do
- 14:04:35radius dot get
- 14:04:38and I'm just going to pass the cast key.
- 14:04:40So I'm just going I'm trying to get the
- 14:04:41data from my cast. So first time this
- 14:04:44data will not be present. Okay. So then
- 14:04:46I'm simply checking. Okay. Now see this
- 14:04:48is the cash aside pattern. Okay, this is
- 14:04:50the pattern that we have discussed. So
- 14:04:52this is the cash aside
- 14:04:55pattern. So I'm just going to do if cast
- 14:04:59data is present.
- 14:05:01So you're going to return me that data.
- 14:05:03Okay. So I'm just going to do a
- 14:05:05console.log
- 14:05:06and I'll do C hit.
- 14:05:10Okay. And then log this. And then you
- 14:05:13have to get the data also, right? So I'm
- 14:05:16just going to get the data which will be
- 14:05:17JSON dot par and you're going to get the
- 14:05:21data here cast if this data is not
- 14:05:24present. So what we have to do? We have
- 14:05:26to get the data still right. So in that
- 14:05:29case this will be cast miss.
- 14:05:33So this time it's going to read the data
- 14:05:34from DV read from main DV.
- 14:05:38So in that case first of all what we
- 14:05:40going to do? So I'm simply going to do
- 14:05:41con uh products.
- 14:05:44I'll do DB products. DB products. Now
- 14:05:47let's do one thing. Let's change this
- 14:05:49one to DB products. And I'm just simply
- 14:05:52going to do this one as products. So
- 14:05:54this will be simply my DB products that
- 14:05:56I have. And now I'm going to set this
- 14:05:58one in my C. So now set or save the
- 14:06:04products in radius C.
- 14:06:08So in that case I'm just going to do
- 14:06:10radius dot set
- 14:06:13ex. So what this is going to do right
- 14:06:15the set x is going to also save your
- 14:06:18ttl.
- 14:06:19Why we need this? So that whatever cash
- 14:06:22that you are saving it is going to it
- 14:06:24doesn't leave forever. Okay. So what I
- 14:06:27mean by this? So this say tax is going
- 14:06:29to do like this. So this is also going
- 14:06:32to save also saves
- 14:06:36detail so that your
- 14:06:40cash doesn't
- 14:06:44leave forever. Okay, you can also check
- 14:06:48it here, right? So I always we are
- 14:06:50checking like this and it is actually
- 14:06:51easier.
- 14:06:55Okay, so it sets a value that is fine
- 14:06:59string value to a key assign uh
- 14:07:01expression time TTL in seconds. Okay, so
- 14:07:04it's going to have one uh time here. So
- 14:07:07how we can do that? So we are simply
- 14:07:09going to do what? So first I have to
- 14:07:10give the key. So you can see that key
- 14:07:12seconds and the value. So this will be
- 14:07:14my cast key. This will be my cast DTL in
- 14:07:18seconds. So 60 seconds. And then I'm
- 14:07:20going to store the data. So this will be
- 14:07:22JSON stringify. Okay. And then what data
- 14:07:24you want to store in a C the products
- 14:07:27data. So now this will be stored in your
- 14:07:29C. All right. So now so first of all
- 14:07:31let's run this. Okay. So what I have
- 14:07:33done? So first we are checking that if
- 14:07:35if it is present. So give me the data or
- 14:07:38else we're going to render cash means
- 14:07:40and then we're going to read it from our
- 14:07:41DB and then we're going to set into C.
- 14:07:44So now first of all let's uh go here and
- 14:07:47[snorts] then I'm just going to copy
- 14:07:49this and I'll do npm run and let's see
- 14:07:54what we getting right. So first of all
- 14:07:55you see that we are getting cast miss.
- 14:07:57Okay. So why we are getting castm?
- 14:08:01That's because okay here we have to
- 14:08:03give. So it's coming here. Makes sense
- 14:08:05also if you log the data you'll be
- 14:08:06getting the data from here. But next
- 14:08:09time now we should be getting the data
- 14:08:10from my our cast. So now let's run it
- 14:08:13once again. And we are getting cash hit.
- 14:08:16Okay.
- 14:08:18And you can see that this is the data
- 14:08:19that you're getting. Okay. So that means
- 14:08:22next time this is already present. This
- 14:08:25is very very important. So first time
- 14:08:27what happened? So your data is not
- 14:08:29present in the cache. You're reading
- 14:08:31data from your DB and you're setting the
- 14:08:33data in your radius cache. Next time
- 14:08:36this will be uh data is already present
- 14:08:38in your C. So this will be cast hit. So
- 14:08:40you're going to get the data right?
- 14:08:42Every time you're going to run you're
- 14:08:43going to get from your cast only. Okay.
- 14:08:46See. All right. So now uh what I have to
- 14:08:49do right first of all let's understand
- 14:08:51another problem and that is called I
- 14:08:54will uh that will be like still cash
- 14:08:57problem. So what I mean by this? So now
- 14:09:00consider that I have my DB products and
- 14:09:03this has all these products correct. So
- 14:09:05we have these products. Copy this
- 14:09:09and here I'm just going to give one more
- 14:09:11which will be my desktop. So now what I
- 14:09:15need to do right so now I'm just going
- 14:09:16to do this one as where you're getting
- 14:09:19this one. Oh, I think I've used con,
- 14:09:21right? Maybe. Yes. So to use this one as
- 14:09:24late.
- 14:09:26Okay. So first what I'll do? So let's
- 14:09:28log this. So what I mean by this? So
- 14:09:32consider this right. So why I'm
- 14:09:35explaining because for some of you it
- 14:09:36will be difficult. So this is not like a
- 14:09:38dummy data right. This is actual
- 14:09:39operation that you are doing in your
- 14:09:41project. you're building some fullstack
- 14:09:43application and let's say you have a API
- 14:09:46or you're creating some product actual
- 14:09:47product and that data the new data this
- 14:09:50desktop is the new data is added in your
- 14:09:52DB correct so now what I'm going to do
- 14:09:54right so I'm just going to do a log and
- 14:09:57here I'm just going to do DB products
- 14:09:59okay save this and then I'm just going
- 14:10:01to run again so you can see that we are
- 14:10:03getting four right so cast me and then
- 14:10:05we getting keyboard mouse and all these
- 14:10:07thing so now uh what we need to do right
- 14:10:10and also if you See we are getting this
- 14:10:12log from here.
- 14:10:15Okay. So this is the log that we are
- 14:10:17getting. So now what I going to do
- 14:10:20right? So first of all I'm just going to
- 14:10:23uh do something like this maybe. So
- 14:10:25let's do here cast
- 14:10:29and I have it radius.get get and I'm
- 14:10:33just going to do this one as cast key
- 14:10:36and then I'm simply going to do log
- 14:10:39and here I'm just going to do here cast
- 14:10:43data and this will be JSON dot parts and
- 14:10:48then I'm just going to get the cast
- 14:10:52data. So let's save this and we'll see
- 14:10:55what is happening here. So we'll clear
- 14:10:57this.
- 14:10:59Okay. So what are you getting? You're
- 14:11:01still getting the old data. Now this is
- 14:11:04a huge problem. So this is called stale
- 14:11:06cache problem. So that means even though
- 14:11:08your DB actual DB has more products or
- 14:11:11newly added products or let's say there
- 14:11:13is a update maybe laptop uh spelling is
- 14:11:15changed from laptop to big laptop
- 14:11:18something like this still your cache
- 14:11:19data has the old data right and this
- 14:11:22makes sense right because you haven't
- 14:11:23invalidated or deleted your cash. So in
- 14:11:26that case what will happen even though
- 14:11:27you can have more data you are still
- 14:11:29going to get same cast data every single
- 14:11:32time. So this is a problem. So now to
- 14:11:34solve this we have a concept called cash
- 14:11:36invalidation. This is a extremely
- 14:11:38important concept. Okay. These are the
- 14:11:39concepts that you are going to use
- 14:11:41actually in your project. Okay. So we'll
- 14:11:43do C invalidation.
- 14:11:46So what this is going to do right. So
- 14:11:48this is going to so we are going to have
- 14:11:50some kind of mechanism in our code in
- 14:11:52our actual API that okay whenever I'm
- 14:11:54going to create a new product or
- 14:11:56whenever I'm going to update a new
- 14:11:57product or whenever I'm going to delete
- 14:11:59a product I need to do what I need to
- 14:12:01delete the old cash so I'm just going to
- 14:12:04give a sort that okay whenever I will do
- 14:12:06any so or let's put it in this way when
- 14:12:09DB changes very simple whenever your DB
- 14:12:11changes you're going to delete the cache
- 14:12:13this is the best way to understand now
- 14:12:16for beginners whenever DB changes is
- 14:12:18you're going to delete
- 14:12:20delete your old cach immediately right
- 14:12:24so you're going to so that you'll get
- 14:12:25the uh updated data here so how you're
- 14:12:28going to do that so I'm just going to
- 14:12:29have it radius dot delete
- 14:12:32and here we're going to pass the cast
- 14:12:33key and I'm just going to do a log
- 14:12:37and I'll do cast deleted
- 14:12:40and then spelling this wrong so now
- 14:12:44again I'm just going to check if not
- 14:12:46equal do cast.
- 14:12:49So I'm just going to do a log here. I'll
- 14:12:53do cast data
- 14:12:56after delete. And here what we going to
- 14:13:00do, right? So I'm just going to do con
- 14:13:02fresh
- 14:13:04products.
- 14:13:06So this will be again your DB products.
- 14:13:08But in actual scenario, this will be
- 14:13:09again your data from your DB. So this
- 14:13:12will be again your data from your DB
- 14:13:14after changes, right? After changes
- 14:13:18after data changes and then again I'm
- 14:13:20just going to set it. So I'll do radius
- 14:13:22dot set
- 14:13:24X and here I'm just going to do this one
- 14:13:27as C key I'll do C DTL in seconds and
- 14:13:32again you're going to do JSON dot
- 14:13:33stringify and you're going to pass the
- 14:13:35phrase products you're getting. So now
- 14:13:38again I'm just going to do a log here.
- 14:13:44Okay, after this I'm just going to do a
- 14:13:46log and I'm just going to do phrase data
- 14:13:50and this will be phrase products.
- 14:13:55So now if I save this so now notice what
- 14:13:57we are doing we doing a invalidation
- 14:13:59cash invalidation by deleting the old
- 14:14:01cash and then again we are setting here
- 14:14:03that if the cash is not present now
- 14:14:05again you give me the data from the DB
- 14:14:06and then again you're going to set it in
- 14:14:08your C. So let's run it again. So you
- 14:14:11can see that we are getting what
- 14:14:14cash data and cash deleted and then we
- 14:14:17are not getting this one. Okay, let's
- 14:14:18run it once again
- 14:14:21and you can see that what we are getting
- 14:14:23cash deleted.
- 14:14:26Something we are doing wrong here. If
- 14:14:28not oh okay we have to actually
- 14:14:32get the data man that's why. So first we
- 14:14:34have to get the cache data which will be
- 14:14:35have radius dot get and this will be
- 14:14:38cash key. then only you'll be getting
- 14:14:40that if this is present because we've
- 14:14:41already deleting it. So first we need to
- 14:14:43see if the cache is present. So first we
- 14:14:45need to get the data and this is not
- 14:14:47going to find because we've already
- 14:14:48deleted it. So then it's going to read
- 14:14:50it from the DB. So now I think this will
- 14:14:52work
- 14:14:55that was the mistake huh see so now
- 14:14:57let's go one by one. So first we have
- 14:14:59what? Cast deleted. So now we are
- 14:15:02getting what? So this is not going to
- 14:15:03give you any data. So that mean cash
- 14:15:05data after delete. So this is your
- 14:15:07phrase data. So this phrase data is
- 14:15:09nothing but your four data
- 14:15:11these four because this is the four data
- 14:15:13that we have added. Okay. So this is how
- 14:15:16you are going to use the concept of cash
- 14:15:18aside castl and invalidate cash and to
- 14:15:21solve this still cash problem. Okay.
- 14:15:23These are very important concept and
- 14:15:25most of the times you're going to use
- 14:15:26this.
- 14:15:28So with this now let's move to the next
- 14:15:30we going to uh apply some of the things
- 14:15:32in our actual project in the next
- 14:15:33section. All right. So now let's
- 14:15:34implement some of this concept in our
- 14:15:36project. Okay. So for that what you're
- 14:15:38going to do right if you go to services.
- 14:15:40So let's go to product service and let's
- 14:15:42close everything else. So first of all
- 14:15:44we'll see that what we are doing here.
- 14:15:46So first you can see that we have one
- 14:15:48method which is going to get all the
- 14:15:50products. Now here one thing I wanted to
- 14:15:51mention we are not focusing on filter.
- 14:15:54Okay for now and I will explain the I'll
- 14:15:56explain the reason why. So for now you
- 14:15:59just see that we are getting uh all
- 14:16:01products there is a method uh service.
- 14:16:03So we just creating one on query and at
- 14:16:05the end what simply we are doing we are
- 14:16:07simply getting the result from your DB
- 14:16:09and then we are returning that and if
- 14:16:11you go to your controllers in the
- 14:16:12controllers we are using that uh what is
- 14:16:15that get product so this is the method
- 14:16:17that we are using and if I go here right
- 14:16:19so let's go to products and if I send
- 14:16:21this so we getting the whatever products
- 14:16:23that we have added in the poss DB so now
- 14:16:26what we need to do right we need to
- 14:16:28implement a caching for this particular
- 14:16:30for all these methods that we have so
- 14:16:32first we have the get all products then
- 14:16:34we have get product by ID create product
- 14:16:36and update product all these methods so
- 14:16:39first let's start with uh taking our
- 14:16:41cast key now remember whatever concept
- 14:16:43that we have learned so far that
- 14:16:44whenever you're going to implement some
- 14:16:46kind of cash there has to be some key
- 14:16:48right so that key is going to hold that
- 14:16:50information or hold that cash
- 14:16:51information so here I'm just going to
- 14:16:53con let's give this one as products
- 14:16:57all cash key now we are only focusing on
- 14:17:00getting all products for now and I'm
- 14:17:02just going to give this one as products
- 14:17:04colon all. So [snorts] now what also
- 14:17:07okay let's take one more thing and that
- 14:17:08is the TTL. So I'm just going to do this
- 14:17:10one con and I'll do products
- 14:17:14cash and I'll do TTL seconds
- 14:17:18and this will be 60. Okay. So now what
- 14:17:22we are going to do first of all these
- 14:17:24get all products now we are going to
- 14:17:26change this one from this particular
- 14:17:28method to a different method. Why? Now
- 14:17:31first of all let's do one thing right?
- 14:17:33Uh maybe let's create one more method
- 14:17:36here. Uh we'll create [snorts] one more
- 14:17:38method like get all products here after
- 14:17:40this and I'll explain or else it will be
- 14:17:42little tricky to understand what we are
- 14:17:44doing. So first of all I'm just going to
- 14:17:45do async and I'll do function
- 14:17:49and here I'm just going to do this one
- 14:17:50as get all products. You're going to get
- 14:17:52error which is fine and then here also
- 14:17:55you're going to get the filters. Right?
- 14:17:56So we have all these filters. So we'll
- 14:17:58copy this filters
- 14:18:01and let's paste it here. And what will
- 14:18:04be the return type? So this is going to
- 14:18:07return a promise
- 14:18:10and this will will return the type of
- 14:18:13product.
- 14:18:15Okay.
- 14:18:16So now first of all what we need to do
- 14:18:18right. So first of all we need to see
- 14:18:21whenever I'm going to call this
- 14:18:23particular service whenever I'm going to
- 14:18:25hit that particular endpoint to get all
- 14:18:27the products I need to see that whether
- 14:18:28that product is present in our cash or
- 14:18:30not. So this is again come back to the
- 14:18:32same concept that we have learned so far
- 14:18:34that is the cash aside pattern. If the
- 14:18:36cash is present you're going to return
- 14:18:38the data from your cache immediately. If
- 14:18:40the cash is not present, you're going to
- 14:18:42get the actual data from your postgress
- 14:18:44DB and you're going to return. But there
- 14:18:46can be some exception. For example, the
- 14:18:48stale cache problem that we have
- 14:18:49discussed that let's say now I added a
- 14:18:51new product. Still we are going to get
- 14:18:53the whole product, right? That part I
- 14:18:54have discussed. So that we're going to
- 14:18:56see later. So for now what we going to
- 14:18:58do right first of all I'm simply going
- 14:19:00to check that con and I'll do has
- 14:19:03filters. I'm simply going to check that
- 14:19:05if filter is present. So for now we are
- 14:19:07not focusing on filters and I'll explain
- 14:19:08why. So I'm just going to give boolean
- 14:19:11and I'll do filters dot
- 14:19:13category this is present or we also have
- 14:19:16the filter dot search right so I'll do
- 14:19:18filter dot search if this is present
- 14:19:20that means there is a filter and I'm
- 14:19:22simply going to check that if this has
- 14:19:24filter is present so in that case I'm
- 14:19:26just going to do console.log log and I'm
- 14:19:28going to do cash bypass for now. I'll do
- 14:19:31C bypass and this will be filtered
- 14:19:38product list. Now why why we are doing
- 14:19:41this right? So see when we have filter
- 14:19:44right so every filter combination
- 14:19:46actually we need a different cascade and
- 14:19:48this is a very important thing. So every
- 14:19:51filter combination that we have need a
- 14:19:56separate cast key. Why that is? Now
- 14:19:59consider that you are doing a filter. In
- 14:20:01the first filter you are only passing
- 14:20:03the category. In the second filter you
- 14:20:05are passing the search. In the third
- 14:20:07filter you're passing both category and
- 14:20:09search. So in that case you can't
- 14:20:10implement this using only one key. So
- 14:20:12there has to be a combination. Okay. For
- 14:20:15example, so let's say we have our
- 14:20:17products right? So we need to do
- 14:20:19something like this. We'll do products
- 14:20:21colon all and then here you're going to
- 14:20:24do a search. So let's say I'm applying a
- 14:20:26search and the keyword is keyboard
- 14:20:28something like this. Okay. So and then
- 14:20:30maybe let's say you are doing a category
- 14:20:32also. Let's I'll do products colon all
- 14:20:34colon category. And then here I'm just
- 14:20:38going to do accessories something like
- 14:20:39this.
- 14:20:43Okay. So [snorts] that is the reason for
- 14:20:45now we are bypassing. But what we are
- 14:20:46going to do right so first we are going
- 14:20:48to change this particular method to
- 14:20:50fetch all
- 14:20:53products
- 14:20:54from database. Okay. So that means this
- 14:20:58method will never change because we need
- 14:21:01to have this right because we don't know
- 14:21:02whether we have the cash all the time.
- 14:21:04If the cache is not present it going to
- 14:21:06it's going to fet the data as usual from
- 14:21:08your database all the time. So that
- 14:21:10means if filter is present so we are
- 14:21:12going to simply return the fetch all
- 14:21:15products from DB and then we're going to
- 14:21:17pass the filters here. Okay. But now
- 14:21:20first of all here we have to implement
- 14:21:22the cast. So that means when the filter
- 14:21:24is not present let's say I'm just
- 14:21:26getting the list of products here. This
- 14:21:28is just for uh explanation in your real
- 14:21:30project. You also have to implement for
- 14:21:32this particular
- 14:21:34uh scenario also. So now what we going
- 14:21:36to do right? So first we need to check
- 14:21:39that before we are going to ask our
- 14:21:41postgress for our products we need to
- 14:21:43check radius first that whether that is
- 14:21:45present or not. If radius already has
- 14:21:47the product list we are not going to
- 14:21:50fetch the same product list from our DB.
- 14:21:52Okay, I'm repeating this thing because
- 14:21:54if you understand the core concept and
- 14:21:56even if you do only two three examples
- 14:21:58right most of the things you'll be able
- 14:22:00to do it I can 100% guarantee even
- 14:22:03though if let's say you're not your
- 14:22:04basics are not clear and you're uh doing
- 14:22:07some very complex project some
- 14:22:09microservices and blah blah but you'll
- 14:22:11see that you'll be getting some problem
- 14:22:13so basic has to be very very clear so
- 14:22:15that you can understand what you're
- 14:22:16doing so now here I wanted to uh check
- 14:22:20that okay if the radius cash is present
- 14:22:22You also have to remember radius
- 14:22:24actually is not the source of truth
- 14:22:25here. Okay. So this is a very important
- 14:22:27point and most of the uh like whoever is
- 14:22:30teaching I'm not sure if they're
- 14:22:31covering this or someone may be skipping
- 14:22:33this but this is actually very
- 14:22:34important. Why radius is not the source
- 14:22:38of truth here. Okay. What actually it is
- 14:22:43uh storing? Remember I already mentioned
- 14:22:45that radius actually is going to store a
- 14:22:47temporary copy of data. that data
- 14:22:50originally come from your posgress DB
- 14:22:52only. So what is your source of truth?
- 14:22:54The postgress DB right. So now in this
- 14:22:57case I'm just going to do con cast
- 14:23:01products
- 14:23:03and here I'm just going to do a and I'll
- 14:23:06do radius client dot get. So first I
- 14:23:08need to check if the this is present or
- 14:23:10not and I'll do product all cash. So I'm
- 14:23:13simply checking if that uh product is
- 14:23:16present
- 14:23:18uh in cash radius cash or not. if this
- 14:23:20is present. So I'm simply going to do if
- 14:23:22the cash products is present. So let's
- 14:23:25do
- 14:23:27log
- 14:23:29and I'm just going to do this on a cash
- 14:23:31hit
- 14:23:33and I'm just going to do products
- 14:23:35all okay and then what are you going to
- 14:23:39do right? So here we are simply going to
- 14:23:42return
- 14:23:43JSON dot parts and here I'm just going
- 14:23:47to do C products as so type will be
- 14:23:51product. So this is what we're going to
- 14:23:53return. Okay. So this is radius is
- 14:23:55actually returning your cash data as a
- 14:23:57string. Why you need JSON parse? Because
- 14:23:59it's going to convert the string back to
- 14:24:01your actual array. Right? This is normal
- 14:24:03concept. If this is not the case, if the
- 14:24:06cash product is not present, so that
- 14:24:08means what? There is a cash miss right.
- 14:24:11So we are going to do cash miss
- 14:24:14and here now we are going to first do
- 14:24:16what? Whenever there is a cast me you
- 14:24:19need to fetch the products from where?
- 14:24:20From your DV. So we simply going to do
- 14:24:23con products
- 14:24:25and this will be a fetch all products
- 14:24:29from DV and you're going to pass the
- 14:24:31filters right. So we have to pass the
- 14:24:33filters. So now here we are going to do
- 14:24:37what? We're going to set our set our
- 14:24:40actual products
- 14:24:44data in radius cast.
- 14:24:48So now I'm simply going to divide radius
- 14:24:50client dot set x. We have already
- 14:24:53discussed see how we are doing right. We
- 14:24:55have already understood all of these. So
- 14:24:57it will be easy. So we have cast key. We
- 14:24:59have the cast ttl in seconds. And then
- 14:25:01we're going to store the data. So do
- 14:25:02JSON.stringify and this will be the
- 14:25:04products. Now these products is the
- 14:25:06products that we are getting from our
- 14:25:07DB. And then we are simply going to do a
- 14:25:10log and I'm just going to do cash set
- 14:25:14products colon all and what you have to
- 14:25:18return you have to return the products
- 14:25:19here and this is done. Okay. So now I
- 14:25:23think this should work. I hope this is
- 14:25:26clear. Now at the end your product
- 14:25:28controller doesn't know what is
- 14:25:29happening in the business logic. Right?
- 14:25:30So here it's going to return the product
- 14:25:33as usual. What you're doing is simply
- 14:25:34doing the main logic here that if the C
- 14:25:37is present, return the data. If this is
- 14:25:39not present, you return the data from DB
- 14:25:41first. Set it in your C and from the
- 14:25:43next time you're going to get from your
- 14:25:45C only. So now let's save this. Let's
- 14:25:47see if this is running or not. I think
- 14:25:48it's running.
- 14:25:50So now I'm just going to run it here. So
- 14:25:52let's run it. So let's see what you're
- 14:25:54getting. We're getting cash miss and
- 14:25:56cash set. And now we're going to run
- 14:25:57once again. And you can see that what
- 14:25:59will happen. It's you can see that cash
- 14:26:01hit. So now it's getting from your cash.
- 14:26:03Okay, you see immediately you're getting
- 14:26:05right. So this is from your radius guys.
- 14:26:07This is fantastic. Now see how much
- 14:26:10cleaner it is to understand. So that is
- 14:26:12the reason I always mention and not I
- 14:26:14always mention but that is how I usually
- 14:26:16teach right in my YouTube videos. I
- 14:26:18don't go into too many complicated
- 14:26:20things because at the end if you working
- 14:26:22in a company right let's say you are in
- 14:26:23college or you you have less than 2
- 14:26:26years of experience you'll see that even
- 14:26:28the whatever work you are doing in your
- 14:26:30company let's say product or service at
- 14:26:32the end they're not doing so many
- 14:26:34complicated things they're doing the
- 14:26:36normal basic things only but the concept
- 14:26:38has to be very clear although there can
- 14:26:40be 1,000 line lines of code but at the
- 14:26:43end you you will see that they're doing
- 14:26:44this cash aside cash invalidation as set
- 14:26:48and fetging the data from C or your
- 14:26:50fetch the data from DB and then set it
- 14:26:52in C. Now this [snorts] is a very basic
- 14:26:54level. There are so many advanced things
- 14:26:56is also there in radius but this is how
- 14:26:58this has to be done. Now see that every
- 14:27:00time it's going to hit the C and it's
- 14:27:02going to get the data from your C right.
- 14:27:04So if you run it here it's going to get
- 14:27:06and sometime it's going to miss. Why?
- 14:27:08Okay, this is another see it's working.
- 14:27:10Why? Because we've given 60 seconds
- 14:27:12here. The TTL is 60 seconds, right? You
- 14:27:14remember this one. So after 60 seconds
- 14:27:16it's going to delete that or invalidate
- 14:27:18that cash. So that is the reason you're
- 14:27:19getting. But if I run once again you see
- 14:27:21that it's cast it. So that means this is
- 14:27:24working absolutely fine. So now what
- 14:27:26will be the next step? Next let's do one
- 14:27:28thing. Let's uh do some more important
- 14:27:30things here only in this particular file
- 14:27:32in the next part because we also have to
- 14:27:34see that when we are going to this cash
- 14:27:37data is present and let's added a new
- 14:27:38product this data should be updated.
- 14:27:41Right? you are getting the the problem
- 14:27:43that I discussed the stale cash problem.
- 14:27:45So let's how we are going to do that in
- 14:27:47the next part. All right. So moving to
- 14:27:48the next one. So get all products we
- 14:27:50have done. So now let's do this one. Get
- 14:27:52product by ID. So what I uh what you
- 14:27:55mean by this right. So let's say
- 14:27:56whenever we are going to fetch a single
- 14:27:58product by ID. So here uh what you're
- 14:28:01going to do right? So also I will
- 14:28:04suggest right this one try to implement
- 14:28:06using the same concept. So what I mean
- 14:28:08by this this will be exactly same
- 14:28:09concept where we have discussed about
- 14:28:10the cast asset pattern right. So first
- 14:28:12we are going to see so let's see I'm
- 14:28:14hitting this particular endpoint from my
- 14:28:16UI side. So I need to check that if this
- 14:28:19particular product information let's say
- 14:28:21I have a product ID of product 10. So ID
- 14:28:24is 10. So if that 10 product is
- 14:28:26currently actually present in my C or
- 14:28:28not. Okay. If that is not present in the
- 14:28:32C. So what you're going to do? You're
- 14:28:33going to face the data from my DB as
- 14:28:35usual. If this is present in C, we're
- 14:28:37going to get from our radius C. So first
- 14:28:40uh what we'll do uh as usual. So let's
- 14:28:42create a export as sync function and I'm
- 14:28:45simply going to do same get product by
- 14:28:47ID and this one I'm just going to do
- 14:28:49this one as fetch
- 14:28:51uh let's do single product. It will be
- 14:28:53easy to understand fetch single product
- 14:28:56from DB, right? Or database whatever you
- 14:28:59can do. So first of all what we need to
- 14:29:01do here, right? So okay, these will be
- 14:29:03same. So let's copy this
- 14:29:07and we'll just paste it here.
- 14:29:10So first we need to actually see that if
- 14:29:12that is present in the radius cast or
- 14:29:15not. So how we can do that? So we have
- 14:29:17to take a cast key, right? So I'm just
- 14:29:19going to do cash key and for this one
- 14:29:22let's do one thing. Let's uh create one
- 14:29:24method. Okay. So I'm simply going to do
- 14:29:27so let's go to the top maybe here. So
- 14:29:31here I'm just going to do a function and
- 14:29:33I'll do get product gas key and here
- 14:29:38we're going to get the product id. So
- 14:29:40this will be number and uh we are going
- 14:29:43to return a string. So here I'm simply
- 14:29:46going to create a key which will be
- 14:29:49products
- 14:29:50colon id because this will be single
- 14:29:53product but how we are going to get the
- 14:29:54single product. So this single product
- 14:29:56is nothing but your product ID. So that
- 14:29:59means let's say I have 1,000 products
- 14:30:01and I am storing the uh product with of
- 14:30:05ID 500. So it will become like this,
- 14:30:07right? So the products colon id colon
- 14:30:10500 something like this. So now this is
- 14:30:13done. So what will be the next step? So
- 14:30:15next will be first we need to see that
- 14:30:16okay this is present or not. If this is
- 14:30:18present we're going to return it. So we
- 14:30:20simply going to do get product by
- 14:30:24product cast key right and here we are
- 14:30:26going to pass the id and then I'm simply
- 14:30:28going to do con cast product. So this
- 14:30:32cast product will be a radius client dot
- 14:30:36get and this will be this cast key. So
- 14:30:39we simply checking if that id product.
- 14:30:42So this is a dynamic value right? So
- 14:30:44this can be 1 2 3 4. Consider let's take
- 14:30:47the example of 10. So if the id of 10
- 14:30:51product was not 10 product ID of product
- 14:30:5310 opposite id of product 10 the product
- 14:30:57which id is 10 if that is present in
- 14:31:00cash or not okay we simply checking that
- 14:31:03part if this is present so that means it
- 14:31:05will be a cash hit right so that data is
- 14:31:07present so I'm simply going to do if
- 14:31:09cash product if this is present so I'm
- 14:31:11just going to do a log and this will be
- 14:31:14a cash
- 14:31:16cast cash hit and And let's also pass
- 14:31:19the uh cast key here. Right. So I'm just
- 14:31:21going to do here uh what I'm doing cash
- 14:31:25key. So this will be cash hit. And then
- 14:31:28we are simply going to as usual return
- 14:31:30the response. So I'm just going to do
- 14:31:31return JSON dot parse.
- 14:31:35So this will be my cash product as
- 14:31:38product. This is done. Now if this is
- 14:31:42not present so as usual this will be
- 14:31:44cash miss.
- 14:31:46And then we're going to first get the
- 14:31:48product from our DB. So we have it
- 14:31:53fetch single product from DB
- 14:31:57pass the ID. First we're going to check
- 14:31:59that if the product is not present.
- 14:32:02We're going to return null. Okay. And
- 14:32:04then we're going to set this one in our
- 14:32:06cast. So now I'm just going to direct
- 14:32:08radisclient dot setx. And here we are
- 14:32:11going to pass the cast key. We're going
- 14:32:13to pass the TTL and also then I'm going
- 14:32:16to pass the actual product that I need
- 14:32:18to uh need to store. So this will be the
- 14:32:20product and this is done. And then I'm
- 14:32:22again going to do a log here and this
- 14:32:24will be catch
- 14:32:27set.
- 14:32:29Okay. What you have to return? You have
- 14:32:31to return the product here. And this is
- 14:32:33done. Okay. All right. So now let's run
- 14:32:35this. And also one thing I just paused
- 14:32:37it and I see that we make a mistake and
- 14:32:39that is uh here right if this is cast so
- 14:32:43it will be cast hit or this will be cast
- 14:32:45miss. Okay so this is one change we have
- 14:32:47to do. So now let's go here and then if
- 14:32:50I try to send this so you'll see that
- 14:32:53what you're getting we're getting cash
- 14:32:54missed and cash is now set. Now next
- 14:32:57time let's try to send once again and
- 14:33:00you'll see that now cash is hit. So that
- 14:33:02means the data is now present in your
- 14:33:04radius cast. So this is another example
- 14:33:07that we have done. Now another very very
- 14:33:08important one that you have to
- 14:33:09understand. Okay. So first let's see how
- 14:33:12I'm going to do it. So what I'll do
- 14:33:14right. So let's go to products endpoint
- 14:33:18and you will see that what we are
- 14:33:20getting right. So first of all let's do
- 14:33:21here. So I'm do 1 2 3 4 5 6 7. Okay. So
- 14:33:28we have seven data. So now uh I'll try
- 14:33:30to send this. So you can see that we
- 14:33:33getting this. Now let's go to the
- 14:33:35postman test that I have created and I'm
- 14:33:38just going to copy this. Okay, let's go
- 14:33:40to the uh add here. So I'm going to add
- 14:33:43the post and then I'll go to body and
- 14:33:46then I'm simply going to send this one.
- 14:33:48So this is added ID 8 and if I try to go
- 14:33:51here and try to send this you'll see
- 14:33:53that how many data you are getting.
- 14:33:55You're getting the ID 7. So you're not
- 14:33:57getting the ID
- 14:33:59uh the ID8 data. So what I mean the new
- 14:34:02data that we have added it's not coming
- 14:34:04here and the reason is very simple this
- 14:34:07is the concept of the stale caches you
- 14:34:09remember I just done this one
- 14:34:10practically so that means it is getting
- 14:34:12from a radius cast until unless that
- 14:34:14cash will be invalidated that will not
- 14:34:17come or else until unless the TTL will
- 14:34:19cross then only the new data will come
- 14:34:22but that is not the solution right so
- 14:34:23that means we have to have some kind of
- 14:34:26mechanism that whenever I'm going to
- 14:34:28create the data or whenever I'm going to
- 14:34:30update the data remember I mentioned
- 14:34:32that whenever there will be some update
- 14:34:34in the DB you need to invalidate the
- 14:34:36cast very very important concept so how
- 14:34:38we are going to do that to do this one
- 14:34:40let's go to our create product and you
- 14:34:42can see [snorts] that we are doing all
- 14:34:43of this whatever blah blah that is fine
- 14:34:45we don't have to do anything here so
- 14:34:47first what I'll do right so I'm just
- 14:34:49going to create a const and I'm just
- 14:34:52going to do newly created product
- 14:34:55so I'll do newly created product
- 14:34:58and this will be these whatever whatever
- 14:35:00we are doing here and now what I need to
- 14:35:03do right I need to invalidate the cash
- 14:35:06now remember I mentioned what the
- 14:35:07postgress is the source of truth right
- 14:35:09so whenever we going to create a product
- 14:35:10we need to delete the old list that is
- 14:35:14stored in our cast so that next time
- 14:35:16whenever we are going to face the latest
- 14:35:18data it is going to get the data from
- 14:35:20your postgress it's going to get the
- 14:35:22fresh data from postgress so that means
- 14:35:24we have to have a method which is going
- 14:35:27to delete the products
- 14:35:29from now delete all products or products
- 14:35:32all
- 14:35:36so this is what we have to do and at the
- 14:35:38end we're going to return the newly
- 14:35:39created product here which is fine so
- 14:35:41what this is going to do so this is
- 14:35:43going to actually delete that cash so
- 14:35:46now copy this and maybe let's do it here
- 14:35:49only right so I'm just going to do async
- 14:35:52function and I'll do delete product cash
- 14:35:56what this is going to return so this
- 14:35:57will be a promise
- 14:35:59void and here I'm simply going to do a
- 14:36:03and I'll do radius client dot delete and
- 14:36:07then I'm simply going to delete the
- 14:36:08product
- 14:36:10all cash key and then I'm simply going
- 14:36:13to do a log and you'll see that I'm just
- 14:36:15going to do a cash delete and then I'm
- 14:36:19just going to do products all and you'll
- 14:36:21see that whenever you're going to do
- 14:36:23that right now notice this cash delete
- 14:36:25will be triggered so now let's do in
- 14:36:28different way so First I will go to
- 14:36:30let's start with first one. So go to
- 14:36:31products. I'm going to send here. We are
- 14:36:34getting the new data because cash is now
- 14:36:35automatically invalidated using TTL.
- 14:36:38That is fine. Now I'll [snorts] go to
- 14:36:40post and I'm going to create one more
- 14:36:41data ID9. And you'll see that here it's
- 14:36:44getting cash is actually now deleted.
- 14:36:46And now if you go to immediately here
- 14:36:48right and try to send this you are
- 14:36:50getting the newly created data also.
- 14:36:52Fantastic right? So that means whenever
- 14:36:54you created a data whenever we updated
- 14:36:57the database we actually deleted our old
- 14:36:59radius cache that is present. So what
- 14:37:01will happen next time it's going to read
- 14:37:04the data immediately from your DB. It's
- 14:37:06not going to wait. So this is how you
- 14:37:08have to invalidate cash. So so see this
- 14:37:12part you have to master that when I'm
- 14:37:14going to invalidate the cash. No one is
- 14:37:15going to come and then tell you that
- 14:37:16okay whenever you are going to create
- 14:37:18product every time you have to delete
- 14:37:19cash. It's not like that you are
- 14:37:21getting. So what I'm trying to explain
- 14:37:22that based on that situation that or
- 14:37:25based on the project or based on the
- 14:37:26feature that you are creating you have
- 14:37:28to decide that when I have to delete the
- 14:37:30cash this example I'm just showing you
- 14:37:32based on the project that I have created
- 14:37:34this doesn't mean that every time you're
- 14:37:36going to create the product you have to
- 14:37:37delete the cash okay maybe yes but what
- 14:37:40I'm trying to say that based on project
- 14:37:42to project this will change but at the
- 14:37:44end the main concept will remain same so
- 14:37:47this is one now the same thing will be
- 14:37:50for what for will be for your uh update
- 14:37:52also. So whenever you're going to update
- 14:37:54any data right that time also you need
- 14:37:57to do what you need to actually
- 14:37:59uh delete or the cache okay because we
- 14:38:03have this update product this one right
- 14:38:05this so whenever I'm going to update so
- 14:38:07there is a possibility that this data
- 14:38:09will not be immediately updated so let's
- 14:38:11see if this is happening right so maybe
- 14:38:13I'm trying to send it here and this is
- 14:38:16now we are getting from caset whatever
- 14:38:19and then let's go to patch and what I'm
- 14:38:22going to I will try to update the first
- 14:38:25product stock from 30 to 5,000 and then
- 14:38:28I'll send it here and go to products and
- 14:38:32let's see what we getting. We are
- 14:38:33getting stock as 30 because this is
- 14:38:36going to still taking from your cash
- 14:38:38only. So that means whenever you're
- 14:38:39going to update the data that time also
- 14:38:42you need to invalidate the cash. So that
- 14:38:44means first of all what I need to do
- 14:38:45when I'm going to update so instead of
- 14:38:48get product by id first of all I need to
- 14:38:50get fetch product single product from
- 14:38:52database. So this is the first thing and
- 14:38:54then whenever I'm going to update the
- 14:38:56product that time also I need to do
- 14:38:58that. So I'm just going to do con
- 14:39:00product equal to this. [snorts]
- 14:39:02So first I'm going to do a and here I
- 14:39:05need to first delete the all product
- 14:39:08cash and at the same time I also need to
- 14:39:10delete the single product. Now you might
- 14:39:13be thinking okay what is this single
- 14:39:14product cache? This single product cache
- 14:39:16is nothing but this get product cash
- 14:39:18that we have created. So this also we
- 14:39:21need to uh delete. Now you might be
- 14:39:22asking why.
- 14:39:24Okay. Now consider that when we are
- 14:39:26updating a product this product with ID
- 14:39:29this we are updating from 5,000 stock to
- 14:39:3210,000. And if you try to go to this
- 14:39:35fetch product by ID now let's say this
- 14:39:37data is now currently stored in your H
- 14:39:40sorry in your C. When you are updating
- 14:39:42this product uh stock from 5,000 to
- 14:39:4510,000, there is a possibility that when
- 14:39:46you're going to send this, you are not
- 14:39:48going to get it. Now you're getting but
- 14:39:50immediately you're not going to get it
- 14:39:51because this data will be stored in your
- 14:39:53single product cache you're getting. So
- 14:39:55that means not only all product cash we
- 14:39:58need to delete which is this one single
- 14:40:00product cash also we need to delete
- 14:40:02because update will happen on a single
- 14:40:03product and it doesn't know that this
- 14:40:06product ID data is present in cash or
- 14:40:08not and at the same time you're updating
- 14:40:10the same product uh stock from 5,000 to
- 14:40:1310,000 means this cash also you need to
- 14:40:15delete you are getting what I'm trying
- 14:40:16to say so that mean this also you need
- 14:40:18to delete so to do this one let's go
- 14:40:21here right so I'm just going to do async
- 14:40:24function
- 14:40:25and I'll do delete
- 14:40:28single
- 14:40:30product cash. So here I'm just going to
- 14:40:33get the product ID which will be a
- 14:40:35number and here what will be the uh
- 14:40:38return type. So this also will be
- 14:40:39promise right? So it will be promise
- 14:40:42of void and here first of all what we
- 14:40:46need to check. So I'm just going to
- 14:40:48check the cast key and this will be get
- 14:40:51product cast key pass the product ID
- 14:40:56and then you're going to do a and I'll
- 14:40:58do radiusclient dot delete
- 14:41:01and then you're going to delete the cast
- 14:41:03key and then I'm simply going to do
- 14:41:05console dot log and I'll do cast delete
- 14:41:10and I'm simply going to do here cast key
- 14:41:13and this also we are going to call it
- 14:41:16here. So I'm just going to the a delete
- 14:41:18single product cash and here we need to
- 14:41:21pass the ID right what is the ID id is
- 14:41:24this ID we need to pass and then we
- 14:41:26going to return the product here
- 14:41:30and this is done so now to verify this
- 14:41:33first what I'll do right see what I'm
- 14:41:35doing first I'm going to send this okay
- 14:41:37so I'm going to request this so now it's
- 14:41:40going to first miss the cash next time
- 14:41:42it's going to hit the cash now I'm going
- 14:41:45to go to update and change this product
- 14:41:47ID stock from 5,000 to uh let's do
- 14:41:5115,000
- 14:41:53and I'm going to update this. Now if I
- 14:41:55go here right and try to send this
- 14:41:57you'll see that immediately you are
- 14:41:59getting 15,000. Why? Because this line
- 14:42:01is now deleting that product cash
- 14:42:03immediately. Also if you go to products
- 14:42:06and try to send this you see that you
- 14:42:07are getting immediately 15,000. You're
- 14:42:09getting this one. So that means both the
- 14:42:11whatever in both the endpoints we have
- 14:42:13added this one in cash we are
- 14:42:15invalidating both the cache whenever we
- 14:42:18are going to update this particular
- 14:42:19product in your DB. So this is how you
- 14:42:22have to understand the concept of cash
- 14:42:24aside pattern still cash isue and cash
- 14:42:27invalidation. I think you might not be
- 14:42:30thinking like these are very important
- 14:42:31concept. Okay these things you are going
- 14:42:33to use mostly whenever you're going to
- 14:42:34create any kind of grant. As I already
- 14:42:37mentioned in my previous videos, at the
- 14:42:39end of the day, whatever complex project
- 14:42:41you are creating, everything is CRUD. So
- 14:42:44what I mean by this, either you're going
- 14:42:45to create something or you're going to
- 14:42:47update something or you are going to
- 14:42:48list something or you're going to delete
- 14:42:50something. So all the things is CRUD. If
- 14:42:53you can master all of this right, you
- 14:42:55will be able to tackle any kind of
- 14:42:56complex project. So with this, I think
- 14:42:58we have almost covered most of the
- 14:43:00things. Now let's move to the next one
- 14:43:01which will be I'm going to give you some
- 14:43:02very basic idea how you can implement
- 14:43:04rate limiting in radius. All right. So
- 14:43:06now let's understand how we can create a
- 14:43:08simple rate limiter using radius. Okay.
- 14:43:11So first what I will do right. So let's
- 14:43:13go to middleware and maybe I'm just
- 14:43:15going to do rate uh limit dot middleware
- 14:43:23uh ts. Okay. So first of all what we
- 14:43:27need to uh do here. So first you have to
- 14:43:29decide that okay what will be your rate
- 14:43:31limiting rules. So what I mean by this
- 14:43:33so let's say we have a endpoint consider
- 14:43:36products endpoint or whatever endpoint
- 14:43:37that you're using that as a user that
- 14:43:40whenever your users will be using that
- 14:43:43particular endpoint is going to hit that
- 14:43:45particular endpoint how much or or how
- 14:43:48many request they can do in a certain
- 14:43:51time frame. So for example let's say
- 14:43:53they can do [snorts] five requests only
- 14:43:56within 30 seconds. So that will be your
- 14:43:59configuration and that will depend based
- 14:44:01on the app you're building based on the
- 14:44:03security and then there are lot of other
- 14:44:05factors. Okay. So in this case first we
- 14:44:07are going to take something like this.
- 14:44:09I'm just going to do a rate limit
- 14:44:13rate limit
- 14:44:15window seconds.
- 14:44:19So this will be I'm just going to give
- 14:44:21this one as 60 seconds. And also I'm
- 14:44:22just going to take one more which will
- 14:44:24be rate limit and I'll do this one as
- 14:44:27max requests.
- 14:44:30This will be let's do maximum 10 request
- 14:44:32or maybe five requests whatever you want
- 14:44:34to do. So now I'm just going to create a
- 14:44:36middleware. So I'm just going to do
- 14:44:38export async function and here I'm
- 14:44:41simply going to do this one as product
- 14:44:43rate limiter because we are doing
- 14:44:46something related to product and here we
- 14:44:49are going to have a request. So this
- 14:44:51will be the request from express.
- 14:44:57So let's import this, right? So I'm just
- 14:44:59going to import from
- 14:45:02express
- 14:45:09and then I'm also going to take a
- 14:45:11response which will be response right.
- 14:45:14So let's import the response also and
- 14:45:17also I'm going to import the next
- 14:45:19function
- 14:45:20and then I'm going to have a next
- 14:45:22middleware which is going to call the
- 14:45:23next middleware or next endpoint
- 14:45:26whatever we have. So here I'm simply
- 14:45:28going to do a try. Okay. So first of all
- 14:45:31what we need to do now here couple of
- 14:45:32things I wanted to mention. Okay. So you
- 14:45:34have to understand all of these each and
- 14:45:36every line what we are doing here. Also
- 14:45:38let's do a catch and I'm going to do
- 14:45:40error and I'll just going to log error
- 14:45:42here. Let's do rate limit
- 14:45:46radius error.
- 14:45:49And what we'll do right, I'm going to
- 14:45:51call the next with our error. So it's
- 14:45:54going to call this error middleware
- 14:45:56because if you go to the server.ts,
- 14:45:58we're using the middleware here. I think
- 14:46:00this one. Okay. So the so this is done.
- 14:46:04So now first of all, let's do one thing.
- 14:46:06Let's always at the end what we have to
- 14:46:08do. We have to call the next middleware
- 14:46:09or next route handler whatever we we
- 14:46:12have. So you have to call the next. This
- 14:46:14is important because if you don't do it,
- 14:46:16it will it will be stuck. Okay, this is
- 14:46:18a middle middleware. So it needs to call
- 14:46:20the next middleware or it needs to call
- 14:46:21the next what to say the next route
- 14:46:24handler. [snorts] Okay, first of all we
- 14:46:27need to understand that what we need to
- 14:46:28do right see whenever you're going to
- 14:46:30implement a real products and level of
- 14:46:32application this uh application this is
- 14:46:34what my understanding right or from my
- 14:46:36practical experience whenever you are
- 14:46:38going to implement this kind of rate
- 14:46:39limiting right. So these uh so first of
- 14:46:42all you have to work with IP right
- 14:46:44because whenever you're going to uh
- 14:46:47implement rate [snorts] limiting there
- 14:46:48has to be something with IP. So what I
- 14:46:50mean by IP? So let's say every user is
- 14:46:53going to have some IP, right? And each
- 14:46:54IP is going to get uh its own counter in
- 14:46:58radius because we using radius. And what
- 14:47:00will happen in the actual real
- 14:47:02production level pattern will be that
- 14:47:04will actually go behind a proxy or let's
- 14:47:06say there is a load balancer. So what I
- 14:47:09mean by this? So first of all, let's
- 14:47:10write all of this because this is
- 14:47:11important. So each IP is going to get or
- 14:47:14will get will get its own counter in
- 14:47:19radius. Okay. So this is the first
- 14:47:21thing. So let's say I have a rate limit,
- 14:47:23right? So I'm just going to do rate
- 14:47:25limit. I think this we've already this
- 14:47:26pattern colon products because we are
- 14:47:28creating a products middleware colon and
- 14:47:30let's say there is a IP. So something
- 14:47:32like this 127.0 blah blah whatever. And
- 14:47:35then [snorts]
- 14:47:36I'm also going to have a rate limit
- 14:47:40colon products
- 14:47:44products
- 14:47:46and then we have one something like
- 14:47:48this. So here what will happen right? So
- 14:47:51let's say there are multiple users will
- 14:47:53be using your application at the same
- 14:47:55time. What will happen that if one user
- 14:47:58crossing the limit? So let's say one
- 14:48:00user So let's say one user is crossing
- 14:48:05the limit. It's possible it will not
- 14:48:09going to it will not going to block
- 14:48:12everyone. Okay. So this is the main
- 14:48:14reason it's not going to block
- 14:48:17everyone.
- 14:48:19So that is the reason we need to have
- 14:48:21some it and this in real application or
- 14:48:25real products and pattern I'm going to
- 14:48:27do will be this will go behind a proxy
- 14:48:32or it can be a load balancer. Okay.
- 14:48:36So this IP handling whatever you have to
- 14:48:38do it needs extra configuration also
- 14:48:40it's not that much simple but for now
- 14:48:42what how we are going to do it. So for
- 14:48:44now I'm just going to do IP and I'm just
- 14:48:45going to take the request ip or else
- 14:48:48this will be unknown. Okay but I already
- 14:48:50mentioned this is how you have to do it.
- 14:48:52There can be lot of other configuration
- 14:48:53that you have to make sure that you're
- 14:48:55using also there is a possible that
- 14:48:57maybe that application that you're using
- 14:48:59uh that also has overseas audiences
- 14:49:02maybe the country that application you
- 14:49:04have maybe some from some other country
- 14:49:06you have a user. So lot of other things
- 14:49:08is there. So you have to [snorts]
- 14:49:10consider all of this but at the end the
- 14:49:12main idea is that if one user is
- 14:49:14crossing the limit it should not block
- 14:49:16other users because there is a
- 14:49:17possibility that 100 users thousand or
- 14:49:19lakh users are using your um application
- 14:49:23at the same time and then now I'm just
- 14:49:25going to take as usual rate limiter key.
- 14:49:29So I'm just going to do this one as
- 14:49:31let's do same only. So I'll do rate
- 14:49:33limit colon products
- 14:49:37colon and then here I'm going to pass
- 14:49:39the IP. So this will be my IP. Okay. So
- 14:49:42now first of [snorts] all what I need to
- 14:49:43do right I need to have a request count
- 14:49:45how many requests that particular user
- 14:49:48is doing. So here I'm just going to do a
- 14:49:50radius client dot and then I'm just
- 14:49:53going to do increment and here I'm going
- 14:49:54to have the rate limiter key. Okay. What
- 14:49:57increment is going to do? It's going to
- 14:49:59increase the request counter by one. And
- 14:50:01now this is where radius is very very
- 14:50:03good. Okay. So this is where radius is
- 14:50:06very very good
- 14:50:09here. Why? Because let's say you have
- 14:50:12multiple backend servers right? What
- 14:50:15will happen right? If you have multiple
- 14:50:16backend servers those many backend
- 14:50:19servers can share the same exact radius
- 14:50:22counter. [snorts]
- 14:50:22Okay. So this is actually very important
- 14:50:24concept. So now consider that we are
- 14:50:26storing this particular count in a
- 14:50:28normal JavaScript variable. Okay. not
- 14:50:30like this. So what will happen even
- 14:50:33consider you have multiple back end
- 14:50:34server that part I already mentioned
- 14:50:36each server is going to have its own
- 14:50:39separate count you're getting what I'm
- 14:50:41talking about so what will what is the
- 14:50:43downside so if each server is going to
- 14:50:45have it own separate count it's going to
- 14:50:47break the rate limiting okay because you
- 14:50:50consider your app is running on multiple
- 14:50:52servers and you're using some normal
- 14:50:55JavaScript variable so in that case it
- 14:50:58can actually break why because each
- 14:51:00server Let's say there is one server,
- 14:51:02second server, first server is going to
- 14:51:04have own separate count. Second server
- 14:51:06is going to have own separate count. And
- 14:51:07this is actually extremely helpful
- 14:51:09because radius is going to do what this
- 14:51:11multiple back end servers actually can
- 14:51:12share the same radius counter. So this
- 14:51:15will be the request count that we are
- 14:51:16going to increment. So now what I'm
- 14:51:18going to do right I'm simply going to do
- 14:51:20the request count is equal to equal to
- 14:51:22one and I'm going to explain what we are
- 14:51:24doing. So I'm just going to dive it and
- 14:51:26I'll do radiusclient doxpire.
- 14:51:29So I'm just going to give the rate
- 14:51:31limiter let limiter key and also I'm
- 14:51:34just going to give the rate limiter
- 14:51:36window second. So what we are doing
- 14:51:38here? So first of what expert is going
- 14:51:39to do is going to create a fixed time
- 14:51:41window for this particular counter. So
- 14:51:43we have a counter right. So now we are
- 14:51:46actually setting this expiry when the
- 14:51:48count is one. Okay. Now now think in
- 14:51:51this way right? So let's say if we reset
- 14:51:53this expiry on every request. So every
- 14:51:55time they're going to hit that request
- 14:51:57and we are going to reset this expiry.
- 14:51:59What will happen? This time window that
- 14:52:01we have, it's going to keeps moving. We
- 14:52:03can't do that. Okay? Because we are
- 14:52:05implementing a fixed window limiter. And
- 14:52:07this is also very very simple level,
- 14:52:08right? This is not even complex for this
- 14:52:12kind of simple fixed window limiter.
- 14:52:15What should happen right? Expiry should
- 14:52:17start with the first request. So that is
- 14:52:19the reason when the request count is
- 14:52:20one, we are actually setting this
- 14:52:22expiry. I hope this is clear. What will
- 14:52:24happen? Because this is actually 60. So
- 14:52:26after 60cond radius is going to delete
- 14:52:28this particular key and it's going to
- 14:52:31count again from phrase. Okay. So after
- 14:52:3360 it's going to delete and it's going
- 14:52:35to start counting from phrase. Very
- 14:52:37important. So I'm just going to write it
- 14:52:39here that after 60 seconds
- 14:52:43after 60 seconds
- 14:52:47radius will deletes this key and start
- 14:52:52counting from fresh. So I think this is
- 14:52:56done and then I'm just going to set some
- 14:52:58header. So I'm just going to do race dot
- 14:52:59set header and let's give this one as x
- 14:53:04rate limit
- 14:53:06limit
- 14:53:08limit
- 14:53:11and this will be your rate limiting max
- 14:53:13request. Also we are going to set one
- 14:53:16more. So I'll do set header and this
- 14:53:18will be x
- 14:53:21rate limit. And let's do remaining
- 14:53:24because we also need to check that how
- 14:53:26many is remaining. So I'm just going to
- 14:53:28do remaining. And how we going to get
- 14:53:30the remaining? So this will be very
- 14:53:32simple. So we do math do max. So I'm
- 14:53:34going to take the zero zero. And you
- 14:53:37have the rate limit max request correct
- 14:53:40versus your request count. Simple
- 14:53:43because this request count we are using
- 14:53:44here, right? So you're increasing this
- 14:53:45one. So every time they're going to do a
- 14:53:47request, it's going to increase the uh
- 14:53:49counter. So that means this is going to
- 14:53:51check that how many request is remaining
- 14:53:53within that time frame. If this request
- 14:53:56count that you have is greater than the
- 14:53:59rate limit max request. So what will
- 14:54:01happen? That means you've already
- 14:54:02exceeded the limit. So in that case we
- 14:54:04have to return. So I'm just going to do
- 14:54:06raise status and what will be the return
- 14:54:084 to9 because you're doing too many
- 14:54:10request and you'll see that in so many
- 14:54:12uh websites also you're going to get
- 14:54:14this kind of thing. So I'm going to do
- 14:54:16success will be false and message will
- 14:54:19be I'm just going to do this one as too
- 14:54:22many requests please try and later
- 14:54:26something like this
- 14:54:28right this is done and then if this is
- 14:54:30not the case we anyway is going to call
- 14:54:32the next so this is how you can create I
- 14:54:35think we have uh discussed each and
- 14:54:37every line so how we going to do that so
- 14:54:40go to your products uh sorry routes not
- 14:54:42products go to product router and here
- 14:54:44you simply going to do router dot use
- 14:54:47and for now I'm just going to do product
- 14:54:49rate limiter and this is done. So now
- 14:54:52let's save this and if you go here right
- 14:54:55maybe let's I'm just going to hit this
- 14:54:56endpoint here. So one time and let's do
- 14:54:592 3 4 5 6. So you're getting too many
- 14:55:04request please try again later. So you
- 14:55:07are going to immed uh is going to get it
- 14:55:09right. So until is going to reset again.
- 14:55:12All right. So I hope this is clear. This
- 14:55:13is very important concept. But again one
- 14:55:15thing I wanted to mention for now we are
- 14:55:17actually applying this for all the
- 14:55:18routes. That should not be the case.
- 14:55:20Maybe the uh application that you are
- 14:55:22building. Let's say in your case only
- 14:55:24one particular
- 14:55:26endpoint you have to apply product red
- 14:55:28limiter. So you can also apply in that
- 14:55:30particular uh endpoint label also. But
- 14:55:32for now we are applying on the overall
- 14:55:34but again the concept you have to
- 14:55:35understand. So with this I think that's
- 14:55:37all for this particular section. Now
- 14:55:39let's move on to the next one. All
- 14:55:40right. So now we are going to end this
- 14:55:42particular video with our last concept
- 14:55:44which will be pops up. So what is popsup
- 14:55:46right? So in so in SW we call that one
- 14:55:49as popsup but that actually means
- 14:55:51publish and subscribe. So first let's do
- 14:55:54one thing right. What are you going to
- 14:55:55do? Let's go here and I'm just going to
- 14:55:58do this one as radius popsup.
- 14:56:03DS. So as you uh I already mentioned
- 14:56:06that this will be publish and subscribe.
- 14:56:11So what publisher is going to do?
- 14:56:13Publisher is going to send a particular
- 14:56:15message. So here we are going to do
- 14:56:17publisher sends a message.
- 14:56:21What subscriber is going to do?
- 14:56:23Subscriber is going to listens
- 14:56:27and receives the message
- 14:56:31and there is another concept or you can
- 14:56:33consider this one as a mediator and that
- 14:56:35we call as channel. What's channel?
- 14:56:37Channel is actually the topic name.
- 14:56:39Okay, from both sides
- 14:56:43both sides use both side means publisher
- 14:56:46and the subscriber. So this is a very
- 14:56:48overview. Okay. Now uh it's kind of like
- 14:56:51a live messaging. Okay, it's not like
- 14:56:54any long-term storage or anything like
- 14:56:55that. So let's say in this particular
- 14:56:58project we're going to create a very
- 14:56:59simple popsup where we are going to
- 14:57:01let's say I'm just going to give you a
- 14:57:03very simple example like we're going to
- 14:57:04create a notification. So once uh admin
- 14:57:08or API is going to create the
- 14:57:09notification express is going to publish
- 14:57:11the event to the radius channel and then
- 14:57:14subscriber is going to receive it
- 14:57:16instantly. Okay. And then uh maybe we
- 14:57:19can do something like this. We are going
- 14:57:20to log that notification in another
- 14:57:22terminal. This is like very basic back
- 14:57:24end to back end live event. Uh we are
- 14:57:26going to implement. Okay. Now here uh
- 14:57:29when you're giving some interview right
- 14:57:31there is a possibility that you are
- 14:57:32going to get query from this particular
- 14:57:34concept because this is actually very
- 14:57:35important. Okay. Now what kind of uh
- 14:57:39questions that you might get okay that
- 14:57:40okay you mention that okay P mean
- 14:57:42publish right. So what publisher
- 14:57:45actually means and what publisher is
- 14:57:47going to send a message where. So you
- 14:57:49have to mention that publisher is going
- 14:57:50to send a message to a radius channel.
- 14:57:52Okay, it is going to send a message to a
- 14:57:55radius channel. Subscriber is going to
- 14:57:57listen and receive the message from that
- 14:57:59particular channel. So that is a listen
- 14:58:01channel is like a topic name that both
- 14:58:02side is going to use or what I when I
- 14:58:06learned I remember I usually used to
- 14:58:08remember this one like this the channel
- 14:58:10is like a mediator. So you have one
- 14:58:11person
- 14:58:13within two person and that one person is
- 14:58:15actually taking the message from one
- 14:58:17person and it's going to sending the
- 14:58:19message to another person. The second
- 14:58:20person who is actually receiving the
- 14:58:22message.
- 14:58:24So now uh let's uh do this one with a
- 14:58:26very simple example first. So first
- 14:58:29let's do one thing. I'm just going to
- 14:58:31copy this one from here. Right? So let's
- 14:58:33go
- 14:58:35till this part. We need
- 14:58:39this one and then I'll paste it here. So
- 14:58:42first of all what I will do we going to
- 14:58:44have a channel as I already mentioned.
- 14:58:46So there will be a channel and I'm just
- 14:58:47going to do this one as demo
- 14:58:49notifications.
- 14:58:53Okay. So here I'll do async function
- 14:58:56run.
- 14:58:58So first uh when you're going to
- 14:59:01implement a pub sub right it actually
- 14:59:04needs two clients. So needs two clients.
- 14:59:08One client
- 14:59:10will be to publish
- 14:59:15and second one to subscribe. This is
- 14:59:19also important concept and prerequisite
- 14:59:22like that you need to know all of these.
- 14:59:24So there will be publisher. So publisher
- 14:59:26will be create client.
- 14:59:29This create client you're going to get
- 14:59:30from radius. And here you're going to
- 14:59:32pass the URL which will be your radius
- 14:59:34URL. Then you're going to have a
- 14:59:36subscriber. Right? So this will be your
- 14:59:38subscriber and this also will be your
- 14:59:40create client and this will be your URL
- 14:59:42which will be your radius URL. Okay. So
- 14:59:45now I have two client. Now publisher can
- 14:59:47send a message or publish a message via
- 14:59:50a channel and subscriber is going to
- 14:59:52receive that particular uh message from
- 14:59:54that particular channel. So now here I'm
- 14:59:57just going to have it. So I do publisher
- 14:59:59dot connect. So first we need to
- 15:00:01connect. Same we need to do for
- 15:00:03subscriber. So I'll do subscriber dot
- 15:00:06connect.
- 15:00:08Okay. So now let's do a log here. So I'm
- 15:00:11just going to do a log and I'll do
- 15:00:13publisher
- 15:00:15connected
- 15:00:17and subscriber connected.
- 15:00:22Okay. Also you can check the ping also.
- 15:00:24Right. So let's say you want to check
- 15:00:26the ping. So I'm just going to do ping.
- 15:00:30So, and here I'm simply going to do a
- 15:00:33publisher
- 15:00:34dotping.
- 15:00:38Okay. And then maybe what else? Maybe
- 15:00:40let's do one more here. And I'm just
- 15:00:42going to do subscriber
- 15:00:48subscriber
- 15:00:50listens.
- 15:00:52Okay. So this is the first step that we
- 15:00:54need to do. Now one very very important
- 15:00:57concept when subscriber is going to
- 15:00:58listen that subscriber
- 15:01:03must be
- 15:01:06active
- 15:01:07before publish.
- 15:01:10Okay why this is very important because
- 15:01:12if nobody nobody is listening right so
- 15:01:15let's say there is no one to listen when
- 15:01:18publisher is going to publish that
- 15:01:19message it might be missed. So it's
- 15:01:21actually very important as that whatever
- 15:01:23subscriber that you're using that
- 15:01:25subscriber is actually responsible for
- 15:01:29consuming that message via that channel
- 15:01:31from your publisher. So that subscriber
- 15:01:33must be active and now what we going to
- 15:01:36do right so
- 15:01:38we'll take subscriber and this is going
- 15:01:40to give this subscribe method and it's
- 15:01:43going to receive the channels you can
- 15:01:44see that you have to pass the channel
- 15:01:45and it's going to receive the message
- 15:01:47correct. So now what you're going to do
- 15:01:49simply so I'm just going to do con data
- 15:01:53and I'm simply going to do JSON.parts.
- 15:01:55I'm going to get the message and then
- 15:01:58I'll just do some log here. So do
- 15:02:00subscriber
- 15:02:02received and also let's do some more
- 15:02:05logging here. And I'm just going to log.
- 15:02:08So let's do here title. So I'm just
- 15:02:11going to log a title. And I'm just going
- 15:02:12to do data dot title.
- 15:02:15And here also let's do one more which
- 15:02:17will be data dot message.
- 15:02:22So this will be data dot message. Okay.
- 15:02:26So now what we're going to do so I'm
- 15:02:28just going to do another log here and
- 15:02:30let's do this one as subscribed
- 15:02:34to channel and then we're going to pass
- 15:02:37the channel here. Okay. So now this is
- 15:02:40how subscriber is going to receive. Now
- 15:02:43this is not a done. Why? because
- 15:02:45publisher haven't sent the event yet,
- 15:02:47right? So publisher needs to send the
- 15:02:49event. So now this is what we're going
- 15:02:51to just start. So I'm just going to do
- 15:02:53publisher
- 15:02:54is now sending the event.
- 15:02:59Okay. And then I'm simply going to take
- 15:03:01the event data here. So this will be our
- 15:03:03actual data. So here I'm just going to
- 15:03:05take a title and this will be radius
- 15:03:09course.
- 15:03:10And then I'm just going to have a
- 15:03:12message and I'm going to do pubsub
- 15:03:16demo.
- 15:03:18Okay, so this is the event. So now
- 15:03:20publisher is going to send the message
- 15:03:23to the channel immediately. Okay, how
- 15:03:25it's going to do that? So I'm just going
- 15:03:26to do con. So I'll do receivers and this
- 15:03:30will be have it. So we're going to have
- 15:03:32the publisher. This publisher is going
- 15:03:34to have now publish. Okay. So this will
- 15:03:36be a publish method not subscribe. So
- 15:03:39this will be publish then you're going
- 15:03:41to pass the channel and then you're
- 15:03:43going to do JSON.stify what you have to
- 15:03:46pass you have to pass this event and
- 15:03:48this event this subscriber is going to
- 15:03:50receive via this particular channel
- 15:03:52which is the message that it's receiving
- 15:03:54here. Okay. And then we can also do some
- 15:03:57log here and then I'm simply going to do
- 15:03:59publish event
- 15:04:03and also let's log here. I'm just going
- 15:04:07to do active subscribers.
- 15:04:13Okay. And these will be receivers.
- 15:04:17Now what we are going to do right, we
- 15:04:19are going to so let's do one thing. We
- 15:04:21are going to have some small weight so
- 15:04:24that whenever subscriber is going to
- 15:04:26receive it, it's going to log okay
- 15:04:28before it's going to exit. So I'm just
- 15:04:30going to grab it. So you can simply do a
- 15:04:31new promise here. normal concept. Take a
- 15:04:34resolve and you're simply going to do a
- 15:04:36resolve. Sorry, set time out. Set time
- 15:04:39out. And I'm just going to resolve
- 15:04:42and I'm just going to [clears throat]
- 15:04:43give 300. Okay. So now I think this is
- 15:04:47done also. Uh what else? Maybe also we
- 15:04:51need to unsubscribe now. So I'm just
- 15:04:52going to do subscribe dot unsubscribe.
- 15:04:55We're going to pass the channel
- 15:04:58and then we're going to quit. Quit is
- 15:04:59again the same thing. So you'll do
- 15:05:01subscriber doquit and you're also going
- 15:05:04to do publisher doquit
- 15:05:07and then let's [clears throat] do one
- 15:05:09more log here and this will be pubsub
- 15:05:15demo done. So the main thing you have to
- 15:05:18understand there are a couple of key
- 15:05:20points. First one is that pubs sub is
- 15:05:22nothing but it's like a live messaging.
- 15:05:24Subscriber must be active before publish
- 15:05:28because you see that subscriber is going
- 15:05:29to subscribe. It's going to get the
- 15:05:31event data before publisher is going to
- 15:05:34publish. It has to be active or else
- 15:05:36once it's going to publish if the
- 15:05:37subscriber is not active it's not going
- 15:05:39to work because there is no no one is to
- 15:05:42receive who is going to receive the
- 15:05:44data. Okay. So this is one important
- 15:05:45concept and another important concept
- 15:05:48popsub is going to or popsub doesn't
- 15:05:51store messages permanently. Okay, this
- 15:05:53is another important concept. So go to
- 15:05:55package dojson and I'm just going to
- 15:05:58copy this
- 15:06:00run it. So I'll do radius popsub
- 15:06:04and this will be let's copy this.
- 15:06:10Okay. And here I'm just going to run
- 15:06:14this.
- 15:06:16So npm run
- 15:06:18and then enter.
- 15:06:21See again I've done mistake. We have to
- 15:06:23run this right. So let's go here. We
- 15:06:26have to just run this. We'll copy this.
- 15:06:32I'll do popsub.
- 15:06:36Okay. So let's see what is happening
- 15:06:38here. So I'll do clear.
- 15:06:41All right. So fantastic. So you can see
- 15:06:43that we have publisher connected. Let's
- 15:06:44go one by one. Subscriber connected
- 15:06:49ping pong. You are getting subscriber
- 15:06:51listens. Now here you will notice one
- 15:06:54thing subscriber listen. Now you say
- 15:06:56subscribe to channel which is this this
- 15:06:58one. So before even though this is
- 15:07:00active it's not going to receive the
- 15:07:02data. Why? Publisher needs to publish.
- 15:07:04Correct. So that's why getting
- 15:07:07subscribed to channel publisher is now
- 15:07:09sending event. Now you can see that
- 15:07:11you're going to get subscriber received.
- 15:07:13So title ready course blah blah message
- 15:07:16whatever you're sending publish the
- 15:07:17event active subscriber is one and pops
- 15:07:20up demo is done. The main point is again
- 15:07:22I'm repeating again and again when
- 15:07:23you're going to implement this one in a
- 15:07:25real project your subscriber has to be
- 15:07:27active before publisher is going to
- 15:07:28publish. If this is not the case you're
- 15:07:30not going to work. Okay. So this is one
- 15:07:32important concept. So with this I think
- 15:07:35uh this part is also completed. So let's
- 15:07:38do one thing right in the next section
- 15:07:39I'm going to show you one very simple
- 15:07:41example on this project context that how
- 15:07:44we are going to implement this one
- 15:07:45practically. So let's do this one in the
- 15:07:47next part. All right. So now let's end
- 15:07:49this particular video with the one
- 15:07:51practical example that I'm going to
- 15:07:53implement here. So first of all I think
- 15:07:54we have already discussed the concept
- 15:07:56right subscriber. I'm not going to
- 15:07:57repeat the same thing again and again.
- 15:07:59So first thing let's create one folder
- 15:08:01here and I'm just going to give this one
- 15:08:02as subscribers.
- 15:08:04Okay. So this will be the first thing
- 15:08:06and let's create this one as so do
- 15:08:08notification
- 15:08:13dots subscriber dot ts. So this is the
- 15:08:17first thing and then also let's rename
- 15:08:19copy this go to controllers and I'm just
- 15:08:23going to do notification dot controller
- 15:08:27ts also we'll go to routes I'll do
- 15:08:30notification dot routes dot ts so first
- 15:08:35let's start with the subscriber right so
- 15:08:37first of all what we need so let's go
- 15:08:39here I need to copy some of the things
- 15:08:41we need the create client
- 15:08:43uh also okay let's copy these to first
- 15:08:47I'm going to paste it here also you need
- 15:08:50to have a notification channel okay so
- 15:08:52for this one what we are going to do
- 15:08:54right so let's create here only maybe we
- 15:08:56don't need another file so I'm just
- 15:08:58going to do con and I'll do notification
- 15:09:03channel and this will be notifications
- 15:09:06so let's see what actually we are doing
- 15:09:08here and I'm just going to take export
- 15:09:12not export I think let's take in case in
- 15:09:14other uh file else we need it. So I'll
- 15:09:17do notifications
- 15:09:19payload.
- 15:09:20So for now I'm going to keep very very
- 15:09:22simple. So I'll do id. So this will be a
- 15:09:25string and also let's take a title. This
- 15:09:28also will be a string. Let's take a
- 15:09:31message.
- 15:09:33This will be string. And let's take a
- 15:09:36created
- 15:09:37at this also will be a string. Okay. Now
- 15:09:42uh what we are going to do right first
- 15:09:43of all uh let's create one more method
- 15:09:46here. So I'm just going to do async
- 15:09:48function and I'm just going to do
- 15:09:51publish notification.
- 15:09:55So here I'm just going to get the
- 15:09:56notification. The type will be
- 15:09:58notification payload and this will be
- 15:10:01the promise void.
- 15:10:04What we are going to do simply so
- 15:10:06publisher is going to do what? publisher
- 15:10:08is going to send the live message to
- 15:10:10radius. Okay. So here we are going to do
- 15:10:12ait. So I'm just going to take the
- 15:10:14radius client dot. So it's going to do a
- 15:10:18publish. So it's going to publish to
- 15:10:20your notification channel.
- 15:10:23And here I'm just going to pass the
- 15:10:25JSON.stringify. What notification is
- 15:10:27going to pass this notification. If you
- 15:10:29want you can also do notification.
- 15:10:31Right?
- 15:10:36That's it. So this will be to publish
- 15:10:39it, right? So now let's save this. So
- 15:10:41now we need to work on the subscriber.
- 15:10:45So here uh we already have
- 15:10:49create radius. We haven't
- 15:10:53created uh radius. So we going to use
- 15:10:55now. So first of all, let's do one
- 15:10:57thing. So here we are going to create
- 15:10:58our subscriber client. So I'm just going
- 15:11:00to do subscriber client. So this will be
- 15:11:03here. We need the create client and I'm
- 15:11:05just going to do URL and this will be
- 15:11:07your radius URL. So radius URL we need
- 15:11:10to just copy this from here
- 15:11:20we can do and this will be ready. Okay.
- 15:11:24So first I'm just going to do
- 15:11:25subscriberclient
- 15:11:26dot on let's give one error in case any
- 15:11:30error and here I'm simply going to
- 15:11:34log one error
- 15:11:39okay we'll do subs radius error
- 15:11:43and this will be error so this is the
- 15:11:45first thing now what we're going to do
- 15:11:47right so we are going to start our
- 15:11:49notific sorry subscriber notification so
- 15:11:52I'll do as sync function
- 15:11:53and I'll do start notification
- 15:11:57subscriber.
- 15:12:00So first what we need to do just see
- 15:12:03this whatever we have done here. So what
- 15:12:05we have done we have done the connect.
- 15:12:07So here first [snorts] we need to
- 15:12:08connect. So I'll do a subscriber client
- 15:12:12dot connect.
- 15:12:14So the first thing now what I need to do
- 15:12:17right this subscriber needs to tell
- 15:12:20radius okay I want to listen to this
- 15:12:22particular channel which channel the
- 15:12:23notification channel that we have
- 15:12:25created this is the channel so here I'm
- 15:12:28simply going to do and now it's your
- 15:12:30task is that you have to subscribe to
- 15:12:33this particular notification channel
- 15:12:35right and also you're going to receive a
- 15:12:38message
- 15:12:40and here I'm just going to do a try and
- 15:12:43uh in the try. So let in the catch what
- 15:12:46you can do right in the catch you can
- 15:12:47maybe just uh do something like this log
- 15:12:52and then we can just do new notification
- 15:12:55in sort I'm doing received
- 15:12:59right so this will be new and here we
- 15:13:01can just get the render the message here
- 15:13:04in the try first of all I'm just going
- 15:13:05to do con notification
- 15:13:09and this will be JSON dotparse so here
- 15:13:13we are going to get the message and this
- 15:13:15will be as and we're going to pass this
- 15:13:18right. So you can pass all of these or
- 15:13:20you can pass as notification payload.
- 15:13:25We can directly pass it here.
- 15:13:28Okay. So this is done and then I'm going
- 15:13:31to do a couple of logs so that we can
- 15:13:33understand. So first I'm going to do new
- 15:13:34notification
- 15:13:36received.
- 15:13:38Then I'll log it here. So I simply going
- 15:13:41to do title. So this will be your
- 15:13:46this notification dot title.
- 15:13:50Copy this. We'll do message and created
- 15:13:52at right. So do message and this will be
- 15:13:56created at.
- 15:14:01That's it. Okay. So this is done. So now
- 15:14:04uh what else? I think this should be
- 15:14:06fine. Now we have to call this. So now
- 15:14:09we are going to do start notification
- 15:14:12subscriber dot catch. Here I'm going to
- 15:14:15get error and I'll simply just do
- 15:14:18console dot error. And here I'm just
- 15:14:22going to do fail to start notification
- 15:14:25and I'll do error and then we'll do
- 15:14:27process do.exit
- 15:14:31of one.
- 15:14:33So this is what we need to do. Okay. So
- 15:14:36now not yet completed. Why? We need to
- 15:14:39publish the notification. We we haven't
- 15:14:40the publish. This is what we have
- 15:14:42learned so far, right? Subscriber is
- 15:14:43going to subscribe and subscribe is must
- 15:14:46be active but publisher is going to
- 15:14:48publish. So where we are going to do
- 15:14:50that for this one? We need to implement
- 15:14:52our uh this uh controller. So let's go
- 15:14:54here and then I'm just going to copy
- 15:14:57this
- 15:14:58paste it here and then I'm do export as
- 15:15:01sync function and this will be my
- 15:15:04publish notification
- 15:15:08controller.
- 15:15:09Okay. So here we're going to get the
- 15:15:12request which will be request response.
- 15:15:15This will be response and we also need
- 15:15:17the next which will be your next
- 15:15:22function.
- 15:15:24So now here what we have to do first. So
- 15:15:26we'll do try and this is the place where
- 15:15:30you have to publish it.
- 15:15:33So I'm just going to take the error and
- 15:15:35then we're going to pass in next with
- 15:15:38error. So in the try what are the things
- 15:15:41you have to do? First you're going to
- 15:15:43get the title and body. Let uh title and
- 15:15:45message. Let's say you have a UI and
- 15:15:47from the UI you're going to receive it.
- 15:15:48Correct? So you're going to get what?
- 15:15:50Title and message. these two things from
- 15:15:53request dot body this title dot request
- 15:15:57see title dot message we are having in
- 15:15:59the payload and then if you do some some
- 15:16:02uh check here that if title is not
- 15:16:03missing I'm not doing all of this now so
- 15:16:05here I'm simply going to do now create
- 15:16:07the notification and this notification
- 15:16:10will be id so this will be your date dot
- 15:16:12now
- 15:16:14dot to string
- 15:16:17then you're going to pass the title
- 15:16:18you're going to pass the message and
- 15:16:21also So created at we have to also pass
- 15:16:23the created at this one and this will be
- 15:16:27new date
- 15:16:30to ISO string. So this will be your
- 15:16:33notification and this is the place now
- 15:16:34we are going to publish. So this is
- 15:16:37where publisher is going to publish is
- 15:16:39going to publish. So do have it. So we
- 15:16:44do publisher notification. Sorry publish
- 15:16:46notification. I think we have to do
- 15:16:48publish notification. We have to export
- 15:16:50it. So we'll do export publish
- 15:16:53notification. So this publish
- 15:16:56notification
- 15:16:58is going to
- 15:17:00get the notification. This is the
- 15:17:01notification you're getting the flow. So
- 15:17:04this one you're going to let's say you
- 15:17:05have a UI you're going to get from UI.
- 15:17:06You are publishing the notification.
- 15:17:08This is going to [snorts] do what? It's
- 15:17:09going to publish. See we are using
- 15:17:11latest client to publish this one
- 15:17:14here. Right? This part this this one.
- 15:17:18and then subscriber is going to
- 15:17:19subscribe. So now I think this is done
- 15:17:22and then at the end what you can do you
- 15:17:23can simply just do array dot status of
- 15:17:25201
- 15:17:28dot JSON and you're going to do success
- 15:17:33will be true
- 15:17:35message
- 15:17:37I'm going to do notification
- 15:17:40published
- 15:17:42successfully
- 15:17:45and you can also pass the data. So let's
- 15:17:47say id will be notification do id and
- 15:17:51this is done. Okay. So this is what I
- 15:17:53need. So now what we can do right. So
- 15:17:56let's go to routes.
- 15:17:59So let's copy this. Let's copy this
- 15:18:02part. What notification routes and this
- 15:18:05we don't need. And this will be my
- 15:18:09notification controller. And I'm just
- 15:18:12going to do this one as notification
- 15:18:14controller.
- 15:18:17Now I'm simply going to do router dot
- 15:18:20post
- 15:18:22and this will be notification controller
- 15:18:24dotpublish notification and you're just
- 15:18:27going to do export default and this will
- 15:18:29be your router and this is done but this
- 15:18:32is not yet completed why in your root
- 15:18:34you have to use this. So go here and
- 15:18:37then simply copy this
- 15:18:40and this will be your notifications
- 15:18:45right and this view notification
- 15:18:49what we are using here
- 15:18:53okay let's import this I think we have
- 15:18:54to import
- 15:18:57so these will be your / notification
- 15:19:00routes
- 15:19:04notification and loads.
- 15:19:08That's it. So I think this is done. So
- 15:19:11now how we are going to check it? First
- 15:19:12let's see if we are getting any error or
- 15:19:14not. I think this is working right. It's
- 15:19:16working. So first let me just copy that.
- 15:19:20I'm just going to copy this
- 15:19:23and I'll go to my package.json
- 15:19:28and then I'll just add this here. So
- 15:19:30we'll do uh source/subscribers
- 15:19:34right this is what you're using
- 15:19:37subscribers notification dot subscriber
- 15:19:40ts so this is one and now what I can do
- 15:19:44right so let's run it here so I'm just
- 15:19:46going to do npm run subscriber
- 15:19:54okay so now this is running so first
- 15:19:56what I going to do right so let's copy
- 15:19:58that curl here. So, I'm just going to
- 15:20:00copy this.
- 15:20:02Go to Postman and I'm going to do
- 15:20:05import. Let's copy and then I'm just
- 15:20:08going to run it on my radius full quotes
- 15:20:13and then let's send it here. We're
- 15:20:16getting four. Okay, what we are using?
- 15:20:18Let's see.
- 15:20:20Our API will not be there.
- 15:20:24So, notification published successfully.
- 15:20:26And you can see what you are getting.
- 15:20:28New notification received. Fantastic.
- 15:20:30Right here. So this title order created
- 15:20:33message. This has been created
- 15:20:34successfully. So from where you are
- 15:20:36getting this one.
- 15:20:38So if you go here right new notification
- 15:20:40received title message and created at.
- 15:20:43Okay. So that means once you're
- 15:20:45publishing subscriber is able to
- 15:20:47subscribe that one and it's going to
- 15:20:50actually getting the data from publisher
- 15:20:51via this notification channel. So this
- 15:20:53is how we have to practically implement
- 15:20:55this logic. Okay. This is very very
- 15:20:57simple way I've implemented and if you
- 15:20:59just do this process multiple times you
- 15:21:01see that how much easier it will be for
- 15:21:03you to implement all of these. Okay. So
- 15:21:05with this I think we have completed this
- 15:21:07particular video. So let me know what
- 15:21:09you think. Uh as I already mentioned in
- 15:21:11the intro if I'm getting good response
- 15:21:13as I already mentioned I'm not going to
- 15:21:15do see part two. If I get good response
- 15:21:17from this particular video I'm going to
- 15:21:19create advance part is very very simple.
- 15:21:22If this video let's say getting 5,000 or
- 15:21:2410,000 views, there is no point of
- 15:21:26spending this much effort of creating a
- 15:21:28part two, right? Uh so with that that's
- 15:21:30all for this particular video. I will
- 15:21:33see you in my next video. Till then good
- 15:21:34luck and peace. All right, I hope that
- 15:21:37you have completed the previous section
- 15:21:39uh which was on radics. So with this now
- 15:21:41let's start. So this will be very
- 15:21:43important one. Okay. And in this one
- 15:21:44you're going to actually understand some
- 15:21:45of the very important concept that you
- 15:21:47are going to use in lot of projects. So
- 15:21:49because file upload, image upload and
- 15:21:51all of these right you are actually
- 15:21:52going to use and these are very very
- 15:21:55practical. So what you're going to do
- 15:21:56right consider you are admin right. So
- 15:21:59what we are going to do right so admin
- 15:22:01will be upload some images right now in
- 15:22:03this one we will actually take the
- 15:22:05functionality simple but we are going to
- 15:22:06understand the concept uh better way.
- 15:22:09Concept means the flow. So admin will be
- 15:22:11able to upload images and then admin
- 15:22:14will be able to see all your images.
- 15:22:16Correct? So admin will be create images
- 15:22:18and admin will be able to fetch all your
- 15:22:20images. So these two routes we have and
- 15:22:22then we have something called delete.
- 15:22:24But how this is going to work? Consider
- 15:22:26this is your application right and see
- 15:22:29how easier I will explain this one. So
- 15:22:31this is your application and this is
- 15:22:32your DB. Okay. So this is let's say your
- 15:22:35DB. What we are going to see till this
- 15:22:38time whatever we have done this CR what
- 15:22:40we used to do. So we usually do some
- 15:22:42functionality. we interact with our DB
- 15:22:45and then we directly uh store this
- 15:22:47particular information in our DB. But
- 15:22:49when we work with assets, it doesn't
- 15:22:51work like that. Instead of directly
- 15:22:54storing the image in your uh DB, what
- 15:22:57you're going to do right, we not going
- 15:22:59to do that. So we have something called
- 15:23:00cloud storage CS. I'm just writing this
- 15:23:04cloud storage can be anything. See in
- 15:23:05our case we're using cloud unit because
- 15:23:07it easier and also you can use AWS
- 15:23:10S3 or you can use cloudfare lot of other
- 15:23:13things. So at the end the concept will
- 15:23:15be same. So you have something called a
- 15:23:17storage. What you're going to do right
- 15:23:19you are actually going to store your
- 15:23:21images in your uh storage. So it will be
- 15:23:24like this. So this let me select this
- 15:23:26one. So from your application it is
- 15:23:29going to interact with your storage.
- 15:23:31Storage is going to do what? It's
- 15:23:32actually going to store all your images.
- 15:23:34So storage is going to keep all your
- 15:23:36images and then what storage is going to
- 15:23:39do right storage is going to give you
- 15:23:40some information that we are going to
- 15:23:42store in our DB. So what will be that
- 15:23:43information? That information will be
- 15:23:45our image URL because see at the end you
- 15:23:48need to keep a track of your assets.
- 15:23:50Right? So now what will be uh the
- 15:23:53responsibility of your DB?
- 15:23:54Responsibility of your DB will be to
- 15:23:56hold the information that is provided by
- 15:23:58the cloud storage. So that means once we
- 15:24:01store the image to our cloud storage
- 15:24:03cloud storage is going to give you two
- 15:24:04things. So first one let's say it's
- 15:24:06going to give a URL and this will be
- 15:24:08your actual image URL that we are going
- 15:24:09to store. So this will be your image
- 15:24:11URL. Okay. Second it's going to give you
- 15:24:14a unique ID. Now this unique ID can
- 15:24:16differ. For example in cloudary we call
- 15:24:18that one as cloudinary public ID. In S
- 15:24:20S3 we call something else. In other uh
- 15:24:23storage we call something else. That
- 15:24:24doesn't matter. But at the end it's
- 15:24:26going to give you a unique ID. And this
- 15:24:28unique ID will be attached to this
- 15:24:30particular asset. So that means how why
- 15:24:32actually you need this unique ID and you
- 15:24:34are going to understand this one in a
- 15:24:36minute. So that means this cloud storage
- 15:24:38is going to give you two information.
- 15:24:39First one will be your image URL and
- 15:24:41second one will be one unique ID that is
- 15:24:43related to this particular asset. And
- 15:24:46then what we will do we are going to
- 15:24:48store this URL and this ID to our DB. So
- 15:24:51that means what we are doing that we are
- 15:24:52not directly storing the image or videos
- 15:24:54in our DB. We are storing the image and
- 15:24:56videos in our cloud storage. Cloud
- 15:24:58storage is going to send that
- 15:25:00information and that information we're
- 15:25:01going to store in our DB. Now why
- 15:25:04actually we need this unique ID.
- 15:25:06Consider this is your application and
- 15:25:09this is your DB. In your DB you have 10
- 15:25:11storage uh saved. So you have 10 storage
- 15:25:15and this is let's say your cloud
- 15:25:16storage. Right? Now what actually these
- 15:25:1910 asset is going to consist? So each
- 15:25:21and every asset is going to have your
- 15:25:23URL and it's going to have your ID.
- 15:25:25Right?
- 15:25:27Now let's say what we have to do right
- 15:25:28from this side admin needs to delete one
- 15:25:31particular asset. So when we are going
- 15:25:33to delete the asset we can't just
- 15:25:35directly delete that particular asset
- 15:25:36from our DB. Why? Because at the same
- 15:25:39time we need to delete this particular
- 15:25:41uh asset from our cloud also. But in
- 15:25:44that case how you're going to understand
- 15:25:45okay this particular image URL actually
- 15:25:48stored in which part in which image or
- 15:25:50basically what I'm trying to say. So
- 15:25:52let's say let's uh understand in a
- 15:25:54simpler way. Let's say you have 10
- 15:25:55assets uh stored. Correct. So each and
- 15:25:57every asset is going to have your URL
- 15:25:59and the ID. So you have 10 assets URL 10
- 15:26:03assets ID. Okay. When we are going to
- 15:26:06delete this particular asset from our DB
- 15:26:08which you can directly do this one.
- 15:26:09Let's say you directly deleted this one
- 15:26:10from your DB. So from DB it is not
- 15:26:13present. But see the problem is in your
- 15:26:15cloudinary storage or in your cloud
- 15:26:17storage that asset is still present. So
- 15:26:19now to under identify that okay this
- 15:26:21particular asset is related to this
- 15:26:23particular asset in our DB. We need this
- 15:26:26ID. So we're getting so using this
- 15:26:28particular ID now what we going to do
- 15:26:29right? Two things we going to do. We are
- 15:26:31going to delete this particular asset
- 15:26:32from our DB. At the same time using this
- 15:26:36ID we are going to delete this
- 15:26:37particular asset from our storage also.
- 15:26:39So that means when we're going to
- 15:26:40delete, we are going to delete from two
- 15:26:41places. We are going to delete from our
- 15:26:43DB at the same time we are going to
- 15:26:45delete from our cloud storage and to
- 15:26:46delete from the cloud storage we need
- 15:26:48this particular unique ID. So this will
- 15:26:51be the order flow and radius one you
- 15:26:53will understand once you implement
- 15:26:54because I hope you have complete again
- 15:26:55I'm repeating okay you have to complete
- 15:26:57the previous video that is mandatory
- 15:26:59then only you can understand because I'm
- 15:27:00not going to cover radius concept in
- 15:27:02this one. This will be completely
- 15:27:03practical. So first this will be the
- 15:27:05overall flow. First what we're going to
- 15:27:07do right to go to cloudinary.com
- 15:27:10and then just sign up for free. Okay. So
- 15:27:12I'm just going to sign up using Google.
- 15:27:14I'll select this account and you'll see
- 15:27:15that I already have so many assets uh
- 15:27:18already uploaded here because I usually
- 15:27:20use this one from my YouTube uh videos.
- 15:27:22One thing I wanted to mention please do
- 15:27:24not use my credences because I've seen
- 15:27:25this one in the past. Some of you
- 15:27:27actually used which is not correct.
- 15:27:28Okay. So please create free account and
- 15:27:31then use. So you see that for example
- 15:27:33this one. I haven't even uploaded all of
- 15:27:35this right. I don't know from where it
- 15:27:36is all these are coming. So don't do
- 15:27:39[snorts] not use this. What you're going
- 15:27:40to do, right? You have to go to go to
- 15:27:43home
- 15:27:46and you'll see that you have this API
- 15:27:47key, right? So you have to go to this
- 15:27:49API key and you're going to get three
- 15:27:50things. Okay, the first one will be your
- 15:27:52cloud name. So this will be your cloud
- 15:27:54name. So if you go back, I wanted to
- 15:27:56show this. This is important because you
- 15:27:58need the cloud name. Okay, so this is
- 15:28:00your cloud name. Then you go to API
- 15:28:02keys. You need the API key and the API
- 15:28:05secret. So three things we need. Okay,
- 15:28:07first of all, let's do one thing. Let's
- 15:28:09go to our application and let's do one
- 15:28:10by one. Go to event uh sorry env.
- 15:28:14And I'm just going to create cloudinary
- 15:28:17cloud name
- 15:28:20cloudinary
- 15:28:22cloudinary
- 15:28:24uh what is that? Cloud uh no cloudinary
- 15:28:28API key.
- 15:28:30And then I'm just going to do cloudinary
- 15:28:33API secret. So these three things we
- 15:28:35need. So now let's go here. So I'm just
- 15:28:38going to copy. So this is my cloud name.
- 15:28:40Okay, this will be your cloud name. You
- 15:28:42can also check it. So here you can see
- 15:28:44this is cloud name. So just paste it.
- 15:28:46Copy the API key or I'll copy from here.
- 15:28:49Paste it. And then this will be the API
- 15:28:51secret. So I'm going to use this. So
- 15:28:53I'll copy and then I'm going to paste it
- 15:28:56here. Okay. So now what I'm going to do,
- 15:28:58right? So this will be the prerequisite.
- 15:29:01Now what I'll do, right? So let's close
- 15:29:03this and then I will give you one
- 15:29:05homework and this homework will be uh
- 15:29:08you have to do this go to your env
- 15:29:11here okay cloudinary API key cloudinary
- 15:29:14secret and then try to use using env dot
- 15:29:16this one I'm intentionally not doing
- 15:29:18this one so this one will be for your
- 15:29:20practice so add these three here and
- 15:29:22then we you are going to use this one
- 15:29:23using envi
- 15:29:28cloudname okay so this will be the first
- 15:29:30thing second what you're going to do
- 15:29:32right so Let's go here and we need a
- 15:29:35cloudinary package. So I do npm install
- 15:29:38cloudinary and also we need malter.
- 15:29:41Okay. So molter is a package that we
- 15:29:44need for our uh asset related uh work
- 15:29:46whenever we are going to do. So let me
- 15:29:48just quickly so this is very slow man.
- 15:29:51So I'll do molter in express node
- 15:29:54project.
- 15:29:57R this will be okay. So this is a
- 15:30:00middleware that handles multipart form
- 15:30:02data right it usually used for uploading
- 15:30:04files images PDF videos and all these
- 15:30:06things right so when we are going to use
- 15:30:08this so first of all we need to install
- 15:30:10the malter and also you need the multter
- 15:30:11type so do npm install- d types malter
- 15:30:14so copy this also and then we going to
- 15:30:17install this also
- 15:30:19all right so this is done next what we
- 15:30:22need we also going to need our IO so I'm
- 15:30:25just going to do IO radius.
- 15:30:29Okay. And also we need B MQ. So this
- 15:30:32will be for our background jobs that
- 15:30:34we're going to do later. But let's
- 15:30:35install this one at once. So we're going
- 15:30:37to do this one.
- 15:30:41Okay. So this is done. All right. So now
- 15:30:44what we're going to do right, let's do
- 15:30:46uh uh let's do this particular section,
- 15:30:48right? Uh slowly. Before doing this one,
- 15:30:52let's go to our docker compose. And we
- 15:30:55actually need to add some more uh one
- 15:30:57more service here. So what I'm going to
- 15:30:59do right, let's go here. Enter. And I'll
- 15:31:04add one more. So I'll do radius.
- 15:31:07And here we are going to do this one
- 15:31:09will be image.
- 15:31:11So image will be radius colon 7. And let
- 15:31:14me check the name alpine.
- 15:31:18Okay. Then we need the container name.
- 15:31:20So I'm going to do container name.
- 15:31:22Container name. Let's give this one as
- 15:31:24what I'll do right uh node js capstone
- 15:31:28project radius.
- 15:31:33Okay. And then I'll do restart
- 15:31:36unless stopped
- 15:31:40and also then we're going to do ports,
- 15:31:42right? So we need to do ports and this
- 15:31:44will be let's see. So I'm just going to
- 15:31:46use a different port. I'll do 6385
- 15:31:49and this is going to run on okay this
- 15:31:51will be in this. So do 63 85 and this
- 15:31:54will be 6379. This is the port usually
- 15:31:57radius runs. Save this. Now what I'm
- 15:32:00going to do right, we have to go to our
- 15:32:01env. And then I'm just going to add one
- 15:32:04more here and this will be my radius url
- 15:32:08and this will be radius and then where
- 15:32:11it is going to run. So it will run
- 15:32:13localhost colon. So you're using what?
- 15:32:156385 right? So 6385.
- 15:32:20That's it. So now what I'm going to do
- 15:32:22right, we'll go here and I'm just going
- 15:32:25to do uh I think in the package dojson
- 15:32:28we have doc npm or or let's directly do
- 15:32:30it. So do docker compose down
- 15:32:34and I'll do docker compose upd
- 15:32:40started. So let me just verify once.
- 15:32:42Okay. So I'll do okay. So you have
- 15:32:44radius this one one will be radius and
- 15:32:47one is postress. Fine. So I think this
- 15:32:49is done. So now what will be the next
- 15:32:51step? So next step first let's start
- 15:32:53with creating our cloudinary
- 15:32:56service right. So cloudinary helper we
- 15:32:58need to create and to create this one
- 15:32:59let's uh keep this one in leave folder.
- 15:33:02So I'll do leave and let's create
- 15:33:04cloudinary
- 15:33:07ts. Okay.
- 15:33:09All right. So first of all let's close
- 15:33:11everything. And this is where you are
- 15:33:13going to understand each and everything
- 15:33:15step by step. Okay. Because this is
- 15:33:17actually very important. So now first of
- 15:33:19all I'm just going to import cloudinary
- 15:33:25cloudinary
- 15:33:27from cloudinary
- 15:33:30and here I'm just going to take a upload
- 15:33:32result means the result that we are
- 15:33:34going to receive and this is going to
- 15:33:36give you two things right so one will be
- 15:33:38your url so this will be secure url
- 15:33:40which will be string and second one will
- 15:33:42be your public ID and this will be your
- 15:33:44cloudinary public ID and this also will
- 15:33:46be string so first will be so we are
- 15:33:49going to create export as sync function
- 15:33:52and I'm simply going to do upload banner
- 15:33:57image to cloudinary
- 15:34:01okay so now let's see how we going to do
- 15:34:04this so first of all what actually we
- 15:34:07need here now remember so we have
- 15:34:09covered one code module that is called
- 15:34:11buffer you remember so here what we
- 15:34:12going to use right we are going to use
- 15:34:14our file buffer okay so whenever we
- 15:34:16going to upload the image which we are
- 15:34:17going to receive a buffer and that
- 15:34:19buffer we are going to handle. So first
- 15:34:21of all as input we are going to receive
- 15:34:22a buffer. So buffer type will be buffer
- 15:34:26and then I'm also going to receive one
- 15:34:28more options and this is optional.
- 15:34:29Option means let's say if I go to my
- 15:34:31cloudinary and let's say uh I'll go to
- 15:34:33my assets and I wanted to create this
- 15:34:35particular asset inside a folder. Okay.
- 15:34:38And that folder name you can give here.
- 15:34:39I don't know why this is slow man.
- 15:34:46So you'll see that you have this folder
- 15:34:47here, right? So you can create a couple
- 15:34:49of folders. See, we have multiple
- 15:34:51folders that I have created. Correct? So
- 15:34:53now what I can do, right? So I can pass
- 15:34:55options and this option is going to
- 15:34:57receive one folder that in which folder
- 15:34:59I wanted to store this and let's keep
- 15:35:01this one optional. Okay. So this is the
- 15:35:04first thing what this is going to
- 15:35:05return. So this is going to return a
- 15:35:06promise and this is going to be my
- 15:35:08upload result. So I need the data in
- 15:35:10this particular format. So I need a URL.
- 15:35:12I need a public ID and this what we have
- 15:35:14already covered here, right? that why we
- 15:35:16need the URL and why we need the public
- 15:35:18ID. Public ID is actually very important
- 15:35:19because at the end this public ID is
- 15:35:22going to help us to identify which asset
- 15:35:24and then we can delete that particular
- 15:35:26asset from our cloudinary storage or
- 15:35:28whatever storage you are using doesn't
- 15:35:30matter. So here now I'm just going to
- 15:35:31take three things. So I'm just going to
- 15:35:33do cloud name
- 15:35:35and this will be my process. Again I
- 15:35:38already mentioned I hope that you have
- 15:35:39done this one. So keep this one in your
- 15:35:41and you can use dot like that. Okay, for
- 15:35:43now I'm just going to keep it like this.
- 15:35:45So we'll do env and then I'll do
- 15:35:47cloudinary cloud name. Then I'm just
- 15:35:49going to take con. Let's do API key. So
- 15:35:52this will be process
- 15:35:55process env dot. So you have cloudinary
- 15:35:58API key
- 15:36:00and I'll do con and this will be API
- 15:36:03secret and this will be process dot env
- 15:36:06dot cloudinary API secret. So three
- 15:36:09things we need. So now first of all I'm
- 15:36:11going to give a check that if not equal
- 15:36:13to cloud name or if not equals to API
- 15:36:17key or if not equals to API secret if
- 15:36:21these are not present. So in that case
- 15:36:22I'm going to throw a new app error. So
- 15:36:25I'm going to give a 500 error and I'll
- 15:36:27do cloudinary is not configured
- 15:36:28properly. So cloudinary is not
- 15:36:31configured properly. So this is the
- 15:36:34first thing and now what you can do
- 15:36:36right you can also I think you can
- 15:36:37import like this. You can also use a
- 15:36:39version two but for now it's fine. You
- 15:36:41can import like this and then what I'll
- 15:36:43do right. So now this is where we are
- 15:36:45going to do what we are going to
- 15:36:46configure our cloudinary because see you
- 15:36:48have to configure or else cloudinary
- 15:36:50will not understand what is your cloud
- 15:36:52name what is your API secret you're
- 15:36:55getting. So that is the reason we need
- 15:36:56this one or else maybe let's do this. I
- 15:36:58think better will be we can import
- 15:36:59version two and let's do this one as
- 15:37:01cloudinary. Okay I think this is better.
- 15:37:03And now I can configure this one. So how
- 15:37:05I'm going to configure? So do configure
- 15:37:07cloudinary. So I do cloudinary and if
- 15:37:09you do dot you'll see that you're going
- 15:37:11to get this config
- 15:37:13or sorry why cloud name I need
- 15:37:15cloudinary. So cloudinary doconfig and
- 15:37:17this is where you're going to pass all
- 15:37:19the three things. So you pass the cloud
- 15:37:21name. So this will be your cloud name.
- 15:37:23You have the API key and this will be
- 15:37:24your API key and then you have API
- 15:37:26secret and this will be your API secret.
- 15:37:30Okay. So these three things cloud cloud
- 15:37:32name API key and API secret. So now my
- 15:37:35cloud ML is configured. So what I need
- 15:37:37to do here see before because we are
- 15:37:39working with buffer you remember that
- 15:37:41section that how we have implemented our
- 15:37:44stream you remember readable stream
- 15:37:46writable stream transform stream all
- 15:37:47these things right. So what we need to
- 15:37:50do right whenever we are going to upload
- 15:37:51our image we are receiving a buffer and
- 15:37:54what we are going to send to cloudinary
- 15:37:56we are going to send our raw image bytes
- 15:37:58raw image bytes to cloudinary. Okay. So
- 15:38:01how we are going to do that? Because
- 15:38:02it's returning promise. So we need to
- 15:38:05return promise here. So I'm just going
- 15:38:06to do new promise. And promise is going
- 15:38:08to give you what? So it's going to give
- 15:38:10resolve and reject. Resolve and reject.
- 15:38:13So now first of all, so I do const. So
- 15:38:16we need a stream here. So we have upload
- 15:38:18stream. Now I'm just simply going to do
- 15:38:19cloudinary dot version. What I'm doing
- 15:38:23cloudinary dot not version
- 15:38:25cloudinary.uploader dot upload. And then
- 15:38:29you're going to get this upload stream.
- 15:38:31So we are uploading a stream. Now here
- 15:38:33what you can do right you can pass
- 15:38:34option and there is a call back. Now you
- 15:38:37see each and every module that we have
- 15:38:39done so far. If you have that much
- 15:38:40patience and completed all the section
- 15:38:42previously all these things would be way
- 15:38:45easier because now remember we have
- 15:38:46discussed one particular thing that what
- 15:38:47is our call back what is the call back
- 15:38:50uh format in classic nodej. So call back
- 15:38:53is going to give you error and it's
- 15:38:55going to give you a result. And now see
- 15:38:56this particular concept we are going to
- 15:38:58apply here. So what I mean by this if
- 15:39:00you learn those individual concepts or
- 15:39:02code modules properly you can combine
- 15:39:04all of this and you can create or use
- 15:39:06those concepts in a project. Okay. So
- 15:39:10now what I can do right I can simply
- 15:39:11give a resource type. So resource type
- 15:39:13will be I'm uploading a image here. So
- 15:39:15if you if you uploading image video and
- 15:39:17everything you can do auto or let's say
- 15:39:19you can if you're uploading raw give
- 15:39:21raw. If you're uploading video you can
- 15:39:22give video. We are uploading image. And
- 15:39:24then I'm just going to do a folder and
- 15:39:26folder will be my options dotfolder.
- 15:39:28This options dot ffolder I'm receiving
- 15:39:29from this option. So this is the first
- 15:39:31thing. Now what is this is going to
- 15:39:33give? So this is going to give you a
- 15:39:34call back. Okay. So this call back is
- 15:39:37now going to give you error and result.
- 15:39:39Okay. So this is the classic call back
- 15:39:42uh format. Now what [snorts] I have to
- 15:39:44do? So if error means what this also I
- 15:39:46have covered. You can't say that I
- 15:39:48haven't covered everything I have
- 15:39:49covered. So it will reject your reject
- 15:39:52the error and you're going to return if
- 15:39:54there is no error. So in that case I'm
- 15:39:56going to resolve. So what I'm going to
- 15:39:58resolve so I'm going to resolve my SQL
- 15:40:00URL and this URL you're going to get
- 15:40:02from a result and see that if I do
- 15:40:04result dot you're going to get
- 15:40:05everything. So you're going to get see
- 15:40:06bytes colors uh context created created
- 15:40:10at and here you're going to get public
- 15:40:13ID see and you have security URL. So I
- 15:40:15need this one and I need the public ID.
- 15:40:18So I'll do result
- 15:40:21dot public id.
- 15:40:23So these two things I need and now what
- 15:40:26I'm going to do right. So let me see
- 15:40:28where it is closing here. And this is
- 15:40:30where I need to do upload stream and I'm
- 15:40:33going to end the upload stream. So look
- 15:40:34dot end. And what you have to pass you
- 15:40:36have to pass your buffer here. Buffer is
- 15:40:38nothing but we are sending a raw image
- 15:40:40bytes to the cloud. So we're going to
- 15:40:42pass the buffer and this buffer we going
- 15:40:44to receive. Okay this one. So now we are
- 15:40:47sending
- 15:40:49uh here so we'll do sending
- 15:40:53raw buffer
- 15:40:55not raw buffer sending raw image sending
- 15:40:59raw image bytes to pluginary okay
- 15:41:03and then I think this is done we don't
- 15:41:05have to do anything uh okay let's see so
- 15:41:08we are taking this one checking
- 15:41:09configure returning a promise okay I
- 15:41:12think this is completed okay so this is
- 15:41:14the first thing so first helper function
- 15:41:16that we need. So now what we're going to
- 15:41:18do right so first of all we are going to
- 15:41:21create our routes then we're going to
- 15:41:23create our services create our
- 15:41:25repository we also going to create our
- 15:41:27multter middleware remember I mentioned
- 15:41:29that malter is a middleware that is
- 15:41:30going to help us to handle our multiart
- 15:41:32form data so we're going to create our
- 15:41:34multter middleware and then we're going
- 15:41:35to combine everything and then create
- 15:41:37the upload logic so let's see how we
- 15:41:39going to do that in the next part okay
- 15:41:40so now let's create all the files so
- 15:41:42first I'll go here and let's do admin
- 15:41:45dotbanners
- 15:41:46admin admin.banner.outes.ts.
- 15:41:49So this is the first one. Then go to
- 15:41:51repository. So I'll do admin dotbanner
- 15:41:54dot repository.ts
- 15:41:57and I'll do admin
- 15:42:00dotbanner
- 15:42:02dotservices.ts.
- 15:42:05So first we need to work with these
- 15:42:06three right. So let's start with our
- 15:42:08admin
- 15:42:10routes. So first as usual we are going
- 15:42:12to import something from express.
- 15:42:15And here we are simply going to do
- 15:42:19what name I'll give. So let's give
- 15:42:20simply export
- 15:42:22con and I'll do admin banner route. We
- 15:42:27need to import router.
- 15:42:30So here uh what I'm going to do right
- 15:42:32let's give router. So I'll do router and
- 15:42:34now first of all let's start with admin
- 15:42:36banner router. Okay let's uh do this
- 15:42:39also. to go to index and I'll do api
- 15:42:42router dot
- 15:42:45use / admin/banner
- 15:42:49banners
- 15:42:51uh and then we're going to do admin
- 15:42:53banner router save this for now so now
- 15:42:56what I'll do so I'll do admin router dot
- 15:42:58use and this I think I don't have to uh
- 15:43:01explain now because now we know that we
- 15:43:03need authenticate and require admin both
- 15:43:06right because this is for admin so now
- 15:43:08let's start with first answer admin
- 15:43:10route dot post
- 15:43:13and here what I'm going to do right now
- 15:43:15this is very important so I'll explain
- 15:43:16so first I'll do request response
- 15:43:21next and here first let's take a try
- 15:43:25catch
- 15:43:30so pass the error let's give error like
- 15:43:33this
- 15:43:34and here first let's create our banner
- 15:43:37so do banner this will be have it I'll
- 15:43:39do create admin banner service
- 15:43:45and here what you have to pass so first
- 15:43:48before doing this one also right so
- 15:43:49let's return this as usual we have done
- 15:43:52so now remember again here what what
- 15:43:54you're doing you're creating something
- 15:43:56correct so that means you have to use
- 15:43:582011 so you have to make sure all small
- 15:44:00small things so do success will be true
- 15:44:04and data you're going to return the
- 15:44:06banner okay So what will be next? So
- 15:44:09next let's create our middleware because
- 15:44:11as I already mentioned that we are going
- 15:44:12to use malter middleware to handle our
- 15:44:14multiart found data. So go to middleware
- 15:44:17and let's do here. So I'm just going to
- 15:44:19do banner uh dot middleware
- 15:44:23dot ts. So first of all we need to do
- 15:44:26something. We are going to import malter
- 15:44:27from malar because we need malar. And
- 15:44:30then let's take one max file size that
- 15:44:34what will be your file size that you
- 15:44:36will allow user to upload. So in this
- 15:44:38case I'm just going to keep let's keep
- 15:44:40either 5 MB or 10 MB. Let's keep 10 MB
- 15:44:42only. So I'll do 1024 into 1024. So this
- 15:44:45will be 10 MB. So now we are going to do
- 15:44:47export const and let's do upload banner
- 15:44:51image.
- 15:44:53So we are going to take malt and here
- 15:44:55you're going to pass couple of things.
- 15:44:57So first of all what you need right? So
- 15:44:58we are actually going to store our file
- 15:45:00in memory because at the end you will
- 15:45:03notice that we are actually not storing
- 15:45:04our file in our local right we already
- 15:45:06have a cloud binary cloud. So that is
- 15:45:08the reason we are going to simply store
- 15:45:10this particular file in memory and that
- 15:45:11is more than enough. So we are going to
- 15:45:14take something called storage and this
- 15:45:15is going to give you this multmory
- 15:45:18storage. Okay. Next what we going to do?
- 15:45:20We are going to pass a limits and in
- 15:45:22this limits you're going to pass a file
- 15:45:24size. This file size will be this max
- 15:45:26file size. Okay. And also [snorts] very
- 15:45:29very important. So what will happen,
- 15:45:30right? So you're going to give a file
- 15:45:31filter. Now this file filter is simply
- 15:45:34going to tell that okay what actually
- 15:45:36what what kind of asset you are
- 15:45:37uploading. So this is going to take your
- 15:45:40request and also you're going to pass
- 15:45:42our file. So you can see that we have
- 15:45:44the file and also it is going to give
- 15:45:46you a call back if I'm not wrong. Okay.
- 15:45:48It is going to give you call back. Okay.
- 15:45:50So request then you have the file and
- 15:45:52then it's going to give you a call back.
- 15:45:53So I'm just going to take call back as
- 15:45:55CV. Okay. Or you can take this one as
- 15:45:57call back also. So now what I going to
- 15:46:00do right here? So I'm simply going to
- 15:46:02check if this file not equal dot mine
- 15:46:06type dot starts with if this is not
- 15:46:09image. So in that case we will not allow
- 15:46:11because we only want image to be
- 15:46:12uploaded. So what [snorts] we going to
- 15:46:14do right we're going to call the call
- 15:46:15back and you already know the call back
- 15:46:16pattern. See I think I already mentioned
- 15:46:19this is multiple times and for this
- 15:46:20reason we've actually covered our core
- 15:46:22modules. So here what I have to pass
- 15:46:24we're going to pass a new app error.
- 15:46:25Okay. First we going to do new app
- 15:46:27error. Give a 400 and I'll do only image
- 15:46:31upload is allowed.
- 15:46:34Okay. And then uh don't forget to
- 15:46:36return. So we're going to do return. So
- 15:46:38now what I have to do if this is not the
- 15:46:40case, we're going to call the call back.
- 15:46:41Error will be null. And what will be
- 15:46:43result? Result will be true. That's it.
- 15:46:46Okay. And now what I'm going to do
- 15:46:48right, I'm simply going to do export con
- 15:46:51because for now we are have single uh
- 15:46:54image upload. So I'm just going to do
- 15:46:55single banner image and this will be
- 15:46:59your upload banner image and here you
- 15:47:01can do single and now this is where I'm
- 15:47:03going to pass a field name. Now this is
- 15:47:05very important. Okay, this will be your
- 15:47:07actual field name when you're going to
- 15:47:09send the request. So this is where
- 15:47:10you're going to pass it like this. So
- 15:47:12for example, let's say I wanted to pass
- 15:47:13like this image. So image will be my
- 15:47:15field name and understand when we are
- 15:47:16going to test it. So for now this is
- 15:47:18more than enough. So this is what
- 15:47:20actually we need. Now what will be next
- 15:47:22step? Next step will be first of all
- 15:47:23we'll go to our routes. Now this is
- 15:47:26where okay this will be actually
- 15:47:27important because we are using a
- 15:47:28middleware right. So what you're going
- 15:47:30to do before you're going to run this
- 15:47:31particular thing we are going to use our
- 15:47:33middleware. So this is where you're
- 15:47:34going to use this upload single banner
- 15:47:36image and this will be from your
- 15:47:38middleware. Okay. So this is done. So
- 15:47:40now what we're going to do right now we
- 15:47:42will be creating our service. So service
- 15:47:44what service is going to do? Service is
- 15:47:46going to do a couple of things. So here
- 15:47:47first of all we are going to check or
- 15:47:49give some check. Let's say if file is
- 15:47:51not present so it's going to return some
- 15:47:53error. Then it's going to upload your
- 15:47:55image to cloudinary. Once the upload is
- 15:47:57completed then what we are going to do
- 15:47:58then we are going to simply store that
- 15:48:00URL and the public ID in our uh DB.
- 15:48:04Okay. So that will be the overall flow.
- 15:48:05So now let's copy this and then let's go
- 15:48:07to our ad main banner service. So here
- 15:48:10I'm just going to do export as sync
- 15:48:12function and I'll do create admin banner
- 15:48:15service. So here you need a file. What
- 15:48:17will be your type? type will be your
- 15:48:18express dot maltter dot file or else
- 15:48:21this will be undefined. Okay, what this
- 15:48:24is going to return? So this is going to
- 15:48:25return a promise and here let's do one
- 15:48:27thing. I think we haven't created our uh
- 15:48:29banner type, right? So let's go to the
- 15:48:31types and let's create one more here. So
- 15:48:33I'm just going to do banner.ts.
- 15:48:36So I'm simply going to do [snorts]
- 15:48:37export type banner and this will be so
- 15:48:40first we need the ID. We also need our
- 15:48:42image URL. This will be string. We also
- 15:48:45need our cloudinary public ID. I think
- 15:48:47that is what we have taken here image
- 15:48:49URL and cloudinary public id this also
- 15:48:51will be string then you need the created
- 15:48:54at
- 15:48:56which will be what date and then you're
- 15:48:58going to have your updated at this also
- 15:49:01will be date so this will be your type
- 15:49:04this is what we're going to use so now
- 15:49:06let's go to our service so first what
- 15:49:08this is going to return this is going to
- 15:49:09return this banner type so first we have
- 15:49:11to check that if this file is not
- 15:49:13present so we are going to throw error
- 15:49:15so I'm just going to do throw new app
- 15:49:17error and here I'm going to do 400 and
- 15:49:20I'll do images needed or images required
- 15:49:22something like this so this is the first
- 15:49:24thing next if the file buffer is not
- 15:49:27present we are going to also return so
- 15:49:29I'll do file buffer is not present we
- 15:49:31are going to throw another error
- 15:49:34and this will be invalid image type or
- 15:49:36image type is invalid something like
- 15:49:38this
- 15:49:41so now what are we going to do right so
- 15:49:43now this is the place where we are
- 15:49:44actually going to upload our image to
- 15:49:47cloud. Remember when [snorts] you're
- 15:49:49going to upload the image to cloud,
- 15:49:50right? First you have to do it before
- 15:49:51you're going to set because first you'll
- 15:49:53upload this one to your cloud then
- 15:49:55you're going to get your URL back and
- 15:49:56then you are going to store that URL in
- 15:49:58your DB. Okay, so this is how it's going
- 15:50:00to work. So first what I'm going to do
- 15:50:02so I'm going to first do a and now I'm
- 15:50:05just going to call that upload banner
- 15:50:06image to cloud remember and this is
- 15:50:08where you're going to pass a buffer. So
- 15:50:10buffer will be your file dot buffer and
- 15:50:12then you're going to pass a folder. So
- 15:50:14here I'm going to pass a folder and
- 15:50:15let's give this one as NodeJS capstone
- 15:50:18project.
- 15:50:20So this will be my folder. What we're
- 15:50:22going to get we're going to get our
- 15:50:23secure URL and we're going to get our
- 15:50:24public ID. Now it makes sense, right?
- 15:50:26Because once we're going to upload this,
- 15:50:27we're going to get these two things. Now
- 15:50:30we're going to again we're going to
- 15:50:31check if this secure URL is not present
- 15:50:33or this public ID is not present. So
- 15:50:35again throw a new error. So I'll do
- 15:50:37throw new app error give a 500 and I'll
- 15:50:40do cloudinary error or something like
- 15:50:41this. whatever you wanted to give.
- 15:50:46Now what we have to do right? So now we
- 15:50:47already have our URL public ID. So what
- 15:50:49will be next step? Next step will be to
- 15:50:51store this one in your DB. So DB means
- 15:50:53now we are going to start with our
- 15:50:54repository. So this is where we are
- 15:50:56going to simply do what? So I'm just
- 15:50:57simply going to do con banner
- 15:51:01and this will have it. So I do create
- 15:51:03admin banner and then we are going to
- 15:51:06pass
- 15:51:08secure URL and the uh what is that
- 15:51:10public id these two things and at the
- 15:51:13end we are going to return the banner.
- 15:51:14Now this uh return we are going to
- 15:51:16return to our routes. So if you go to
- 15:51:19your routes where is that here we are
- 15:51:21going to return. Okay so now what you
- 15:51:23can do right so first you can import
- 15:51:24this one from our service and we have to
- 15:51:26pass our uh file. So what will be your
- 15:51:28file? But how we are going to get the
- 15:51:30file? That is the important part here.
- 15:51:32So you're going to get this one from
- 15:51:33request and this request is going to
- 15:51:35give you this file. Okay. So this is
- 15:51:36what you have to do. So now it will go
- 15:51:38to your service. It's going to do all
- 15:51:40this step. It is going to upload to
- 15:51:42cloudinary and then we are going to
- 15:51:44create this in our repository. So this
- 15:51:46is how it will work. So now we'll create
- 15:51:48copy this and go to repository and then
- 15:51:50I'm simply going to do export as sync
- 15:51:52function. [snorts]
- 15:51:54So create this. So here we are going to
- 15:51:56receive what? So what we are sending? We
- 15:51:58are sending the image URL which will be
- 15:52:00string and we're also going to get
- 15:52:02cloudinary public id and this will be
- 15:52:06your string. What this is going to
- 15:52:09return this is going to return a promise
- 15:52:10and it's going to return the banner
- 15:52:12type. So what I can do we can so for
- 15:52:15this one I think we just have to write
- 15:52:16simple query right. So lupool doquery
- 15:52:19here will be your so let's take one
- 15:52:22more. So I'm just going to do type
- 15:52:24banner row and this will be banner and
- 15:52:28we're going to pass this.
- 15:52:32Okay. So first what we have to do we
- 15:52:34have to insert correct.
- 15:52:36So do insert into so what is the name?
- 15:52:41So banners. So banners is the
- 15:52:44uh what is that table name. So do insert
- 15:52:46into banners. So what are you going to
- 15:52:48insert? We're going to insert three
- 15:52:49things right.
- 15:52:52uh image URL cloudinary public id these
- 15:52:55two things I think we have to do so
- 15:52:57let's go to our uh repository we'll do
- 15:53:01insert image URL and also insert the
- 15:53:04cloudinary
- 15:53:06public id
- 15:53:10okay and then we are going to pass the
- 15:53:12values so I'll do values
- 15:53:15so dollar one dollar2 and then we're
- 15:53:18going to do a returning so we're going
- 15:53:20to return the ID
- 15:53:22image URL
- 15:53:24cloudinary public ID
- 15:53:27created ad and also let's copy the
- 15:53:29updated ad also
- 15:53:35okay and then you're going to pass this
- 15:53:37so first what will be the first value
- 15:53:38first value is image URL second will be
- 15:53:41your cloud so here this will be image
- 15:53:42URL and this will be your cloud public
- 15:53:44ID and then you're going to just return
- 15:53:47result dot rows of zero and that's set.
- 15:53:51So save this. So this is going to save.
- 15:53:53Now we can simply import this from our
- 15:53:55repository and this is completed. So now
- 15:53:57before we going to test this one, right?
- 15:53:59So let's do one thing. Let's get all the
- 15:54:00banners also because then we can see
- 15:54:02that whether actually it is getting
- 15:54:03stored in our DB or not. Correct? So
- 15:54:05what I'm going to do, right? So let's
- 15:54:06create admin banner dot get. So I'm
- 15:54:09going to do slash and let's copy this
- 15:54:12from here.
- 15:54:18And then I'm simply going to do a try
- 15:54:20catch
- 15:54:24and I'm just going to do next error. So
- 15:54:27in the try so simply I'm just going to
- 15:54:29do con banners and this will be have it
- 15:54:31fetch
- 15:54:33admin b man b man b man b man b man b
- 15:54:35man b man b man b man b man banners
- 15:54:36and then I'm simply going to raise dot
- 15:54:38status of 200. I'm doing little first
- 15:54:40now I think we have already done so many
- 15:54:42times the same thing. will be success
- 15:54:44will be true and let's pass the data and
- 15:54:47data will be your manners. So now we can
- 15:54:49simply create this where in our service
- 15:54:53same thing right it's like repetative
- 15:54:55same thing you're going to do. So now
- 15:54:57let's go to our services first and then
- 15:55:00I'll do export as sync function and I'll
- 15:55:04do fetch admin banners what this is
- 15:55:07going to return this is going to return
- 15:55:08promise but remember you have to move
- 15:55:11all of this so it's going to return of
- 15:55:12array right keep because you can have
- 15:55:14multiple uh banners. So first what I'll
- 15:55:16do right so I'll do con banners this
- 15:55:19will be ait and I'll do find all admin
- 15:55:23banners
- 15:55:26from div something like this and then at
- 15:55:30the end we're going to return this
- 15:55:32banners so now first copy this and then
- 15:55:35we're going to create this one. So we'll
- 15:55:37go to our repository
- 15:55:40and I'm simply going to do export as
- 15:55:41sync function find on admin banners from
- 15:55:45DB.
- 15:55:47So here we are going to return promise
- 15:55:50of banner
- 15:55:54and then we are simply going to do con
- 15:55:57result and this will be a pull dot
- 15:56:00query. So this will be your banner row
- 15:56:04and what you going to do? So we're going
- 15:56:06to just select all this right. So we are
- 15:56:08going to do select ID
- 15:56:11image URL we have cloudinary
- 15:56:16public ID correct. So public
- 15:56:19id
- 15:56:20created at and updated at and then we're
- 15:56:25going to do from banners table because
- 15:56:27we have the banners table and let's do
- 15:56:30order by
- 15:56:32uh created
- 15:56:34at disk.
- 15:56:37That's it. And then you're simply going
- 15:56:38to do what? Return result
- 15:56:41dot rows.
- 15:56:44What happened man? Oh, we have to do
- 15:56:48outside.
- 15:56:49Okay, save this. So now we going to
- 15:56:52simply import this from here and go to
- 15:56:55our routes and we're going to import
- 15:56:57this one from our service. Save this. So
- 15:57:00I think this is done and I think we've
- 15:57:02already added it. So this one. So now
- 15:57:04we'll see what is actually happening.
- 15:57:07So let's run it again.
- 15:57:10Fantastic. So now I'll go here. Okay. So
- 15:57:14first let's open one more. So I'm just
- 15:57:16going to do copy this.
- 15:57:19So this will be post first and this will
- 15:57:22be admin/banners.
- 15:57:24Correct? And first let's go to body and
- 15:57:27what you have to do right you have to do
- 15:57:29form data. See here we have form data
- 15:57:31and this is the key you have to use the
- 15:57:32key. This key will be your image. Okay.
- 15:57:34This key will be the one that you are
- 15:57:37using here. So you have your middleware.
- 15:57:40See you're passing image here. Right? So
- 15:57:42this is your field name. So this will be
- 15:57:44image. Now what you have to do right you
- 15:57:45have to change this one to file and
- 15:57:47you're going to select file. Okay. So
- 15:57:49I'm just going to select I think I have
- 15:57:51downloaded one simple uh which one I can
- 15:57:55use man. Maybe I think I also have some
- 15:57:57thumbnails designed.
- 15:58:00Okay. Let's do this one. Okay. So I'm
- 15:58:02just going to select this one. So first
- 15:58:04let's try to send access token is
- 15:58:07needed. Fantastic. So we'll go to this
- 15:58:10one. So let's uh log in or maybe first
- 15:58:13I'm going to check. So I'll do sum get
- 15:58:16the token copy this
- 15:58:19and then I'll go to headers add
- 15:58:21authorization
- 15:58:24and I'll do b error
- 15:58:26and you'll see that you're going going
- 15:58:28to get admin access is required.
- 15:58:30Awesome. So that means you have to use
- 15:58:32admin token send.
- 15:58:35Uh we'll copy this. Go here. go to
- 15:58:39headers. So we'll do be error
- 15:58:43and then try to send. All right. So
- 15:58:46happening. So let's see what is
- 15:58:47happening. That's it. So now you have ID
- 15:58:50image URL cloudinary public ID are
- 15:58:52getting created at updated. Now let's go
- 15:58:55here and I'll go to my assets.
- 15:59:00That's it. So you have your image here.
- 15:59:02So that [snorts] means this image is now
- 15:59:04already added here. And at the same time
- 15:59:06you are getting the uh URL and this URL
- 15:59:08you are now saving in your DV. So now
- 15:59:10let's say if I just wanted to copy this
- 15:59:12and if I go here and uh this one or
- 15:59:15let's do one thing. I'm also going to
- 15:59:17add one more. Okay. So I'm just going to
- 15:59:19close this and add one more here. Maybe
- 15:59:21I'll just do this one advanc type script
- 15:59:25and then I'll just send.
- 15:59:31So this is done. So now let's go here.
- 15:59:33Refresh this.
- 15:59:35Save two. And maybe let's do one more
- 15:59:38because we need this one. You will
- 15:59:40understand why I'm doing this. I will
- 15:59:42use this one.
- 15:59:50So this is done also. Let's check once.
- 15:59:53Okay. So this is added now. So now what
- 15:59:56we can do, right, we can go here and
- 15:59:57first I'll go here. Go to headers. I'll
- 16:00:00copy this entire part and then go to
- 16:00:02headers, add authorization
- 16:00:05and paste this one. Try to send and you
- 16:00:08can see that you already have banners 1
- 16:00:102 and three. Fantastic. That's it. So
- 16:00:13that means now what you can do right you
- 16:00:15are actually fetching the banners
- 16:00:17directly from your DB. You don't have to
- 16:00:19interact with your cloud now because
- 16:00:20you're already storing your assets in
- 16:00:22your cloud. But the main point will come
- 16:00:24that when you're going to delete this
- 16:00:25one, right? So you just can't directly
- 16:00:28delete from your DV. you also have to
- 16:00:29delete this one from your cloud. So this
- 16:00:32one you're going to do in the next part.
- 16:00:33All right. So now let's implement some
- 16:00:35caching uh strategy. So what are you
- 16:00:38going to do right? So where I'm going to
- 16:00:39implement caching. So let me show you
- 16:00:41and I'm just going to close those files
- 16:00:43that are not needed. So you see that
- 16:00:45we're feting the admin banners right. So
- 16:00:47what you're going to do right every time
- 16:00:48we are not going to f directly from our
- 16:00:50DB. I think you have already checked
- 16:00:52this one in the radius section. I hope
- 16:00:53that you have covered this one. So what
- 16:00:55you're going to do right? So if I'm not
- 16:00:57wrong, let me just quickly check. That
- 16:00:58is called the cast aside pattern, right?
- 16:01:00So we have the cash
- 16:01:03aside
- 16:01:04pattern.
- 16:01:06I think it's called cash aside only.
- 16:01:11Uh okay, let me just check what is that
- 16:01:13called. Citiz
- 16:01:22going to record database immediately
- 16:01:24save the result. Okay. So this is what
- 16:01:25actually we we need. Okay. So we're
- 16:01:27going to implement right. So if the data
- 16:01:29is already present in the cache. So in
- 16:01:31that case we are not going to fetch from
- 16:01:33our DV or else we are going to fetch
- 16:01:36from our DV. But before that we also
- 16:01:37have to do couple of things. So first
- 16:01:39what we going to do right? We'll go to
- 16:01:40our leave
- 16:01:43folder and then I'm just going to create
- 16:01:44a radius. TS file. And now first of all
- 16:01:47what I'm going to do right also let's do
- 16:01:49one thing. So let's add our radius curl
- 16:01:52also here. So I'll do rad CL.
- 16:01:55So this will be check required NV
- 16:01:58variables and go to our env. And then we
- 16:02:01going to do radius CL.
- 16:02:04Save this. So now first of all what I'll
- 16:02:06do right I'm simply going to import
- 16:02:09radius from my radius. So this is the
- 16:02:12first thing and then I'm simply going to
- 16:02:14do export const. So I'll do radius and
- 16:02:16this will be so I'll do new radius and
- 16:02:20here we going to pass env
- 16:02:22dot radius url. Now because we using
- 16:02:26bool uh sorry we are using bool mq
- 16:02:29right. So for bool mq when we are going
- 16:02:30to implement our background job and
- 16:02:32background worker
- 16:02:33bool [snorts] mq actually needs its own
- 16:02:36connection request okay or own
- 16:02:38connection request opsense object. So in
- 16:02:41that case what we going to do right we
- 16:02:42not going to pass our IO radius
- 16:02:44instance. Okay. So what we going to do
- 16:02:46right we are simply going to do con and
- 16:02:49then I'm simply going going to do a
- 16:02:50radius URL and this will be new and here
- 16:02:53we're going to use the URL and here we
- 16:02:56going to pass the env dot radius URL.
- 16:02:58Okay. So that means we're creating what
- 16:03:00we are creating here our own connection
- 16:03:03object request and then what you can do
- 16:03:05right we are simply going to do export
- 16:03:07con and here you can do bull
- 16:03:10uh mq connection
- 16:03:14and this will be you have to pass a
- 16:03:16host. So host will be your radius url
- 16:03:19dot host name. This part also you have
- 16:03:21covered in the URL code modules in the
- 16:03:23NodeJS code modules on this port you're
- 16:03:25simply going to get your number and
- 16:03:27you'll get is radius URL and this is
- 16:03:30going to give you the port okay and then
- 16:03:32we are also going to take let's take a
- 16:03:34max uh retries max retries per request
- 16:03:42and this will be null for no okay so
- 16:03:44save this so first before we'll
- 16:03:46implement our background job logic what
- 16:03:49are going to do right so Let's implement
- 16:03:50our banner cache. So let's do this one
- 16:03:53in leave only. So I'm just going to uh
- 16:03:56do here banner cache
- 16:03:59dot yes. So first of all what we are
- 16:04:01going to do. So first here uh also we
- 16:04:04need to do couple of things. If you have
- 16:04:06already covered the uh ready section we
- 16:04:08know that we actually need two things.
- 16:04:10We need the cast key right and that cast
- 16:04:13key will be using that cast key will be
- 16:04:15able to identify. Second we need a TTL.
- 16:04:17Correct? So this is what you're going to
- 16:04:19do. So, I'm just going to do a const and
- 16:04:21let's do admin banners
- 16:04:24cask key. You can always uh make sure to
- 16:04:27move all of this in your uh what is that
- 16:04:29called? Uh
- 16:04:31uh constant file. So, admin banners also
- 16:04:34I need con and I'll do admin banners
- 16:04:38cash
- 16:04:40t seconds and this will be 60. So, this
- 16:04:46is the first thing. So, now we are going
- 16:04:48to create our cash. So first we are
- 16:04:49going to create how many methods you
- 16:04:51need. So first of all one will be to
- 16:04:54check that whether the cache is present
- 16:04:56or not because if the cash data is
- 16:04:57present we going to return from the
- 16:04:59cache. Second we need to set the data in
- 16:05:02our cache and third will be to
- 16:05:04invalidate the cache. Remember
- 16:05:05invalidate technique I mentioned. So
- 16:05:07let's say you have two assets added or
- 16:05:09two images added in your cloudinary and
- 16:05:11you already storing in your cash. Now
- 16:05:13next time when you're going to add a new
- 16:05:14asset so now the number will become 2 to
- 16:05:17three. So if you're not invalidating
- 16:05:18your cash, you are always going to
- 16:05:20receive two data until unless the TTL
- 16:05:22expires. Okay. So that is the reason you
- 16:05:24need uh that clear method also. So I'm
- 16:05:28simply going to do function and here I'm
- 16:05:30simply going to do get banners from gas.
- 16:05:34So what this is going to return? So this
- 16:05:36is going to return a promise and the
- 16:05:38type will be what it's going to return
- 16:05:40banner of because it there can be
- 16:05:43multiple banners. So here I'm simply
- 16:05:45going to do con
- 16:05:48data and this will be a so we're going
- 16:05:51to use radius from radius and this is
- 16:05:54going to do get method and this get
- 16:05:56method what you have to pass you have to
- 16:05:57pass the cast key right that's it and if
- 16:06:00this data is not present so what you're
- 16:06:02going to do you're going to return null
- 16:06:06and if this data is present so you're
- 16:06:07going to do a JSON doparts it's like
- 16:06:09simple and then you're going to do what
- 16:06:11cache data and what will be the type
- 16:06:13will be as banner of array. So this will
- 16:06:16simply get the data from your C. You
- 16:06:18don't have to do anything. Next I have
- 16:06:21to set. So I'll do export as sync
- 16:06:23function and I'll do set banner C. This
- 16:06:28also what? So it's going to return the
- 16:06:29banners data. The type will be banner of
- 16:06:32array and this is not going to return
- 16:06:35anything because we don't have to do
- 16:06:36anything here. We're just going to set
- 16:06:38what you're going to set. So a radius we
- 16:06:40have a set method. So you're going to
- 16:06:42set what this is going to receive. This
- 16:06:44is going to receive your key. So this
- 16:06:45will be your admin b maners cast key.
- 16:06:48Then you're going to set the data the
- 16:06:50value. So what will be the value? Value
- 16:06:52will be JSON dot stringify the banners
- 16:06:55data.
- 16:06:57So this banners data that you're going
- 16:06:58to receive. And then uh we are also
- 16:07:01going to pass. So let's pass a x and
- 16:07:04then I'm also going to pass the ttl. TTL
- 16:07:07will be my TTL.
- 16:07:09Save. I think this is done. And then
- 16:07:12last we need to validate invalidate. So
- 16:07:14do export as sync function
- 16:07:16and I do look clear banners
- 16:07:21and this will be promise of
- 16:07:26void. So what are we going to do here?
- 16:07:28We're simply going to do a radius dot
- 16:07:31delete and you're going to pass the cask
- 16:07:33key. That's it. So these three you need.
- 16:07:36Now how you're going to use this? This
- 16:07:37is the important part right. So where
- 16:07:39you are going to use this one or first
- 16:07:41of all where you are going to set and
- 16:07:43when you're going to delete or when
- 16:07:45you're going to receive. So you have to
- 16:07:46do all this in your service. The first
- 16:07:49one very very important when you're
- 16:07:50going to create a new asset. So whenever
- 16:07:53we are going to create a new asset let's
- 16:07:55say this one after this we need to
- 16:07:57invalidate the cash and I think this one
- 16:08:00you're clearly understanding why we are
- 16:08:01doing right. So every time you're going
- 16:08:02to do we need to do what? We're going to
- 16:08:04do a and we're going to do clear
- 16:08:08dash banners cash. So this is the first
- 16:08:11thing. Okay. Now next will be how you're
- 16:08:14going to fetch. Now fetch is actually
- 16:08:15very simple. So first what you'll do? So
- 16:08:17we're going to do con
- 16:08:19do get cast banners. See radius is not
- 16:08:24that much difficult. Okay. If you
- 16:08:25practice you will be able to understand
- 16:08:27very easily. And I'm simply going to do
- 16:08:28get banners from cast. And then we'll
- 16:08:31check if this get banner from cash is
- 16:08:34present. You simply return this data. So
- 16:08:36you're going to return oh sorry get cash
- 16:08:39banners present you return this. If this
- 16:08:42is not so you're going to fetch from
- 16:08:43your DB. So you already have this one
- 16:08:45from DB and then you're going to just
- 16:08:47set the data to cache
- 16:08:50because if your data is already present
- 16:08:52is cast you don't have to set it
- 16:08:54correct. If it is not then you're going
- 16:08:55to set. So here you're going to do a do
- 16:08:58set banner cast and you're going to pass
- 16:09:01these banners. That's it. And this is
- 16:09:03done. So now what will happen right? So
- 16:09:05now this radius functionality should
- 16:09:07work. So every time so let's say if I
- 16:09:09try to send this right and next time I'm
- 16:09:11going to send now see the timing here.
- 16:09:13So it's actually receiving from the
- 16:09:15cash. Okay. So it's actually receiving
- 16:09:17from your cash very very clearly. You
- 16:09:19can see this one right? So also you can
- 16:09:21also verify. Let's say if I try to I'm
- 16:09:24not sure if this will work. So let's do
- 16:09:26one thing. Let's try to check. So I'm
- 16:09:28just going to remove this clear banner
- 16:09:30cast. Okay. And I'll go here. And then
- 16:09:33let's say if I try to send this one.
- 16:09:38So this is now done. And then let's see
- 16:09:41what you're getting.
- 16:09:43So you can see that you are getting 1 2
- 16:09:453. Three only. But if you go here, you
- 16:09:48see that you have one more here.
- 16:09:511 2 3 4. So you're getting only three
- 16:09:53because you are not clearing the cash.
- 16:09:55But now if I remove this right and next
- 16:09:57time let's say better wait to send I'll
- 16:09:59see that you should be getting. So you
- 16:10:00are still getting three only here.
- 16:10:08Okay I have to create I got it. So I
- 16:10:10have to create here one more. So then it
- 16:10:12will be cleared. My bad. So now once I
- 16:10:14create uh clear uh sorry clear right and
- 16:10:16if I go here now try to send. So you'll
- 16:10:18be getting five. I see that 1 2 3 4 5
- 16:10:23simple. So that means when we'll create
- 16:10:26that time we're going to clear the
- 16:10:27cache. So with this now caching is also
- 16:10:30completed. Now try to implement some of
- 16:10:32the caching in other parts if you want
- 16:10:34like in the CL in the previous sections.
- 16:10:36So now what we have to do we are going
- 16:10:37to do very very important section which
- 16:10:38will be how we going to delete. So in
- 16:10:40this case we're going to use bool MQ and
- 16:10:42we are going to implement our background
- 16:10:44job and background worker. So let's see
- 16:10:45how we going to do that in the next
- 16:10:46part. All right. So now this one will be
- 16:10:48very very important section. Okay. So in
- 16:10:50this one you are going to understand
- 16:10:51that how we are going to implement a
- 16:10:53background job feature. So whenever we
- 16:10:55are going to delete a image from our
- 16:10:56cloudinary. So how we are going to
- 16:10:58delete from DB and at the same time the
- 16:11:00delete from cloud or delete from your
- 16:11:02cloudinary will happen in the
- 16:11:04background. Okay. So we are going to
- 16:11:06return our response immediately but in
- 16:11:08the background bool MQ and radius is
- 16:11:10going to take care of deleting these
- 16:11:12assets. Okay. So first let's do one by
- 16:11:14one. Okay. And this will be very very
- 16:11:16interesting because you need to
- 16:11:17understand the flow first. So go to
- 16:11:19[snorts] leave and cloudinary and we
- 16:11:20need one more helper function here and
- 16:11:22that will be to delete. Okay. So first
- 16:11:24I'm just going to do export as sync
- 16:11:26function and this will be my delete
- 16:11:30banner image from cloudinary.
- 16:11:37So first of all what we need we need
- 16:11:39public ID. I think I don't have to
- 16:11:40explain I already mentioned this one
- 16:11:42multiple times that that is the reason
- 16:11:45we are actually keeping the public ID
- 16:11:47because this will help us to delete this
- 16:11:49one from cloudinary
- 16:11:52so we'll do void and now
- 16:11:56let's take these two uh let's take this
- 16:11:59line and then we are also going to I
- 16:12:00think we can take this part
- 16:12:06this will be same so we paste this one
- 16:12:08and Now we can simply do return. So
- 16:12:11we'll have cloudy dot uploader and then
- 16:12:14you have something called destroy and
- 16:12:16then you're simply going to do public
- 16:12:17ID. Now this cloud is going to take care
- 16:12:20of that particular asset deletion based
- 16:12:22on the public ID and it's going to
- 16:12:23delete from here. Okay. So this is the
- 16:12:25first thing. So now what you're going to
- 16:12:27do right? So first okay we also need to
- 16:12:30do one uh important thing and that is we
- 16:12:32are going to create one more route here.
- 16:12:34Correct. So let's go to routes admin
- 16:12:36banner routes and let's create our route
- 16:12:38here. So admin banner route dot delete
- 16:12:41and here I'm simply going to do banner
- 16:12:43ID. Why? Because you need the dynamic
- 16:12:45value because you need to understand
- 16:12:46that which banner you're going to
- 16:12:48delete. Correct? So here
- 16:12:52so this will be next.
- 16:12:54And now I'm simply going to do try catch
- 16:12:58and in this catch you're going to take
- 16:13:00error. And now we're going to do next.
- 16:13:03And then you're going to pass the error.
- 16:13:04Okay. Now this one I think we have
- 16:13:06already covered why we are doing the
- 16:13:07same thing so that it will be uniform.
- 16:13:08But you can do lot of other way also
- 16:13:10here. Okay. So but this is fine. So here
- 16:13:13what we're going to do. First we're
- 16:13:14going to call a service. Okay. This
- 16:13:16service will be delete admin banner
- 16:13:19service. Now let's understand this flow
- 16:13:21one by one. What you're going to pass?
- 16:13:23You're going to pass a request dot
- 16:13:25params and then banner ID. Simple. So
- 16:13:27you're going to get the banner ID.
- 16:13:28That's it. And then you're going to
- 16:13:30return a response. So raise dot status
- 16:13:32200 dot JSON. And now you're going to do
- 16:13:36success will be true. And let's do
- 16:13:39message.
- 16:13:41So this will be banner deleted
- 16:13:44successfully.
- 16:13:47What? Okay. So this will be banner
- 16:13:50deleted successfully.
- 16:13:54Okay. But what will be in this service?
- 16:13:57Now this is where it will be very
- 16:13:58important. Okay. So this is where you
- 16:14:00have to understand that how this will
- 16:14:01work. If you do this one properly one
- 16:14:05you'll be able to create multiple. Okay.
- 16:14:07So if you go to service and let's create
- 16:14:10this service. So first of all I'm just
- 16:14:11going to do export as sync function and
- 16:14:14we have got a delete what is it? Delete
- 16:14:16admin banner. What this is going to
- 16:14:18receive? This is going to receive a
- 16:14:19banner ID. This will be a string. What
- 16:14:23this is going to return nothing. So it's
- 16:14:24going to return promise and then void.
- 16:14:26So now you have to understand the flow
- 16:14:28here. Now what I mean by flow. So first
- 16:14:31as I already mentioned that when we
- 16:14:32going to delete we are going to delete
- 16:14:34from your DB immediately. We are going
- 16:14:37to delete from our postgress first and
- 16:14:39we are going to get our cloud. So do
- 16:14:41delete from our DB
- 16:14:44and get the and get the public
- 16:14:50ID. So now I'm simply going to do con
- 16:14:52public ID. This will be a delete
- 16:14:57admin banner by id. How you going to
- 16:15:01get? We're going to pass the banner ID.
- 16:15:03Okay. So then I'm simply going to check
- 16:15:05if this public ID is not present. So
- 16:15:08I'll do throw new app error 404 and I'll
- 16:15:12do banner not found. And now what I have
- 16:15:16to do here can you guess this one we're
- 16:15:19going to create. So this we just have to
- 16:15:20delete from our posgress DB. Now once
- 16:15:24you delete then what you have to do see
- 16:15:26remember let's say you have a five
- 16:15:27assets added correct once you delete one
- 16:15:30asset it will become four now let's say
- 16:15:32those five assets assets are stored in
- 16:15:35your radius cache so that means when
- 16:15:37you're going to delete so now your DB
- 16:15:39becomes four next time when when you're
- 16:15:41going to fetch we are going to get four
- 16:15:43data correct but if you're not
- 16:15:46invalidating your cast you are always
- 16:15:48going to get five data that we have
- 16:15:50checked this one so that means here also
- 16:15:51you're going to do private clear better
- 16:15:54case now this is extremely important
- 16:15:56okay very important now why I'm
- 16:15:57repeating this thing very important and
- 16:16:00I'm telling this one same thing again
- 16:16:01and again because when you're
- 16:16:03implementing radius and all you need to
- 16:16:04know where you are going to use this
- 16:16:06radius
- 16:16:07methods right you can't just like
- 16:16:10blindly use this based on the feature
- 16:16:11you have to use for example here we are
- 16:16:13using maybe in some other functionality
- 16:16:15we don't have to use this one so you you
- 16:16:16have to understand no one is going to
- 16:16:18come and teach you in every project okay
- 16:16:20so now what you have to do
- 16:16:22This is where we're going to add our BMQ
- 16:16:24job. So I'll do add MQ.
- 16:16:28So this is going to happen in the
- 16:16:29background. So first we're going to
- 16:16:31delete from our posgress get the public
- 16:16:32ID and then add this one to our
- 16:16:34background job. But before this first
- 16:16:37let's delete this one from our DB. So
- 16:16:38I'll go to our repository and let's
- 16:16:42create one more. So I'll do export async
- 16:16:45function. See night time also this uh
- 16:16:49bikes and everything right? sound. Okay.
- 16:16:52So you'll see banner ID we are going to
- 16:16:54get this will be string and what this is
- 16:16:57going to return. So this is going to
- 16:16:58return a promise. Let's do whether it's
- 16:17:00return string or null because we are
- 16:17:02going to return the uh what is that?
- 16:17:05What is that called? Public id. So we do
- 16:17:08con result. So do ait cool dot query.
- 16:17:14So this is going to give you the
- 16:17:15cloudinary
- 16:17:18public ID. you can directly directly
- 16:17:20take here only and this will be a string
- 16:17:23and here what I'm going to do oh
- 16:17:27here we are going to write our qu so now
- 16:17:29I'm simply going to do that delete from
- 16:17:34what what is the name banners where id
- 16:17:38equal to 1 and then I'm going to return
- 16:17:42so I'll do returning cloudinary public
- 16:17:44ID because see we at the end we are
- 16:17:47going to have the cloudinary public ID
- 16:17:49here. Correct? And then you're going to
- 16:17:50pass your value which will be your
- 16:17:52banner ID. And now what are we going to
- 16:17:55do? So do return
- 16:17:58result dot rows of zero
- 16:18:03dot cloud public ID or else this will be
- 16:18:06null. So this is going to delete from
- 16:18:08your DB. It's going to return the public
- 16:18:10ID. Now we are going to add this one to
- 16:18:12our boolean key job. So I'm going to add
- 16:18:14it. We'll do add delete
- 16:18:18cloudinary
- 16:18:21B uh let's do cloudinary image job
- 16:18:25and this will be public ID.
- 16:18:29So first let's import this from our
- 16:18:31repository. So we're going to copy this.
- 16:18:33Now let's create our Q. Okay. So here we
- 16:18:35are going to create our Q's. So we're
- 16:18:37going to create Q's and workers. So I'm
- 16:18:39going to add a Q here. Okay. And let's
- 16:18:42give this one name as delete
- 16:18:44cloudinary. No, let's keep this one
- 16:18:46capsule cloudinary
- 16:18:50image
- 16:18:52dot q
- 16:18:55not this much dot q.ts.
- 16:18:58Okay. So now here we are going to first
- 16:19:02of all we have to take our q name. So
- 16:19:05I'll do a Q name and this will be your
- 16:19:08cloudinary
- 16:19:11jobs. So this will be your preview name
- 16:19:13for handling audio cloudinary related
- 16:19:16job. So first what I'm going to do right
- 16:19:18I'm going to do export as sync function
- 16:19:21and I'll do add delete
- 16:19:24cloudinary image job. What this is going
- 16:19:27to receive? So let's import this one
- 16:19:30from our job. So this is going to
- 16:19:32receive a public ID. Correct? So here
- 16:19:34you're going to receive [snorts] a
- 16:19:35public id and this public id will be
- 16:19:38string and here we're going to return a
- 16:19:40promise of
- 16:19:43void. So now what are we going to do
- 16:19:46first? So first of all let's do one
- 16:19:48thing what you have to do right. So we
- 16:19:50are going to add one job. So this is
- 16:19:51what we're going to do. We going to add
- 16:19:53one job to our que. Okay. So there will
- 16:19:58be two things. So first will be your
- 16:19:59job.name mean this is going to tell that
- 16:20:01okay what kind of job you are going to
- 16:20:03do. Second there will be data means the
- 16:20:05data that you need to pass to your
- 16:20:08worker. So you have a Q and then you're
- 16:20:10going to have a worker two things. Okay.
- 16:20:12So first what I'll do right? So let's do
- 16:20:14Q and then I'm simply going to do type.
- 16:20:17So I'm just going to do a delete
- 16:20:20cloudinary
- 16:20:22image job data and here I'm simply going
- 16:20:26to have a public ID. Correct? So do
- 16:20:28public ID and this will be string.
- 16:20:31So now what I'll do right? So first I'm
- 16:20:32going to do con this will be my job data
- 16:20:36and this will be my this one uh delete
- 16:20:38cloudinary job data and this is where
- 16:20:40you're going to have your public ID all
- 16:20:41right and this public ID is the public
- 16:20:43ID that you're receiving so now we are
- 16:20:45going to add our job to our que how you
- 16:20:47can do that so I'm just going to add it
- 16:20:50and here we are going to create a que so
- 16:20:53how you're going to create a que to
- 16:20:54create a simply going to do const and
- 16:20:57you do delete cloudinary image q and
- 16:21:01this will be a new Q from BM MQ you're
- 16:21:05going to import you have to pass the
- 16:21:07name right and then you have see you
- 16:21:09have a connection type of radius
- 16:21:11connection correct so you need the
- 16:21:12radius connection so here you're going
- 16:21:13to pass a Q name and your connection
- 16:21:15will be your B MQ connection that you
- 16:21:18have created remember in the radius here
- 16:21:20host port and max retries per request so
- 16:21:23this is going to create a Q and this Q
- 16:21:25[snorts] you're going to add here so I'm
- 16:21:27just going to this dot add and now
- 16:21:30you're going to pass that what will be
- 16:21:32name and what will be data data will be
- 16:21:34that what data sorry what work you are
- 16:21:36actually doing so what we we are doing
- 16:21:38here we actually doing a deleting right
- 16:21:40so what you can do right you can create
- 16:21:42one more con here and I'm simply going
- 16:21:44to do delete
- 16:21:46cloudinary
- 16:21:48image job and this will be uh delete
- 16:21:56cloudinary
- 16:21:58image so this would be my job Right. So
- 16:22:01this is what we are doing here. So I'm
- 16:22:03going to pass this job and then you're
- 16:22:04going to pass the job data. So job data
- 16:22:06will be your public ID. This will be
- 16:22:08your job data and then you can pass
- 16:22:11couple of properties. So first one will
- 16:22:12be let's try attempts. So three times we
- 16:22:14are going to check. Let's add some back
- 16:22:17off. So here I'm just going to do type
- 16:22:18and this will be exponential and let's
- 16:22:21do a delay of 3,000. Right? So what will
- 16:22:25happen if this fails by any chance?
- 16:22:27Sorry. Oh not this one. If cloudinary
- 16:22:30fails. So what we're going to do we're
- 16:22:31going to retrace automatically and also
- 16:22:33let's do one thing. So what we're going
- 16:22:34to do right we're going to keep our
- 16:22:36radius connection clean once it is done.
- 16:22:38So we'll do remove on complete. So this
- 16:22:40will be true and let's do remove on
- 16:22:43fail. This will be false.
- 16:22:45So this will be our job that we are
- 16:22:48going to do. So simply important thing
- 16:22:49you have to understand that what data
- 16:22:50you are sending. So we are sending our
- 16:22:52job data which receives the public ID.
- 16:22:55What will be your job right? What
- 16:22:57actually you're doing that you have to
- 16:22:59explain right? And then how we going to
- 16:23:00create the latest connection. So now we
- 16:23:03can save this. Now this is not yet
- 16:23:05completed. Why? Because you need to
- 16:23:06create the worker. Worker is going to
- 16:23:09actually do the things right. So it's
- 16:23:10going to delete your image. How it's
- 16:23:13going to delete your image? Because if
- 16:23:14you go to your cloudinary what we are
- 16:23:16doing here. Okay. Let's import this one
- 16:23:19here. So if you go to your cloudinary uh
- 16:23:23here, right? So we have this delete
- 16:23:25image from cloudinary. This one we are
- 16:23:27not using any place. See we have only
- 16:23:29one place. So this is what we going to
- 16:23:31use in our worker and this is where
- 16:23:33actually we are returning or actually we
- 16:23:35are deleting the asset. So let's see how
- 16:23:37you're going to do that in the worker
- 16:23:38part. Okay. So now let's go to our
- 16:23:40source and let's create our workers. So
- 16:23:43here we are going to do delete
- 16:23:47plinary
- 16:23:49image dotworker
- 16:23:52ds. So first what you have to do right
- 16:23:55uh let's close everything else. We need
- 16:23:57our Q's. Let's go to Q's file. And we
- 16:24:01need the Q name. Now this Q name has to
- 16:24:03be same. Okay. The the Q name that
- 16:24:05you're using in Q and the Q name you're
- 16:24:07using in worker both has to be same. So
- 16:24:10now what also let's take this also
- 16:24:12because we have this type delete
- 16:24:14cloudinary image job data. This is also
- 16:24:16we need the public ID.
- 16:24:18So first let's create our export con and
- 16:24:21this will be our delete cloudinary
- 16:24:24image worker. Here
- 16:24:28what you're going to do right we're
- 16:24:29going to import new worker from our
- 16:24:32boolean MQ. So this will be from your
- 16:24:34boolean MQ what you need? So first of
- 16:24:36all you need see you need the name. Okay
- 16:24:38so this will be the Q name and then
- 16:24:40you're going to do what? It's going we
- 16:24:42are going to create one async job in
- 16:24:45this. So first I'm going to do a Q name
- 16:24:47and then I'm going to take async
- 16:24:49function and this is will be our job. So
- 16:24:52first what I'll do right simply I'm
- 16:24:54going to check if this job dot name if
- 16:24:57this not equal to this delete I think
- 16:25:00let's create this one where is that
- 16:25:02delete uh uh this one delete cloudinary
- 16:25:06image job both has to be same also so if
- 16:25:09this is not the case so in that case
- 16:25:13what I'll do we're going to return so
- 16:25:14this is the first thing and then what I
- 16:25:16going to do I'll do con data and this
- 16:25:19will be my job dot data
- 16:25:21as so what you're sending here so you're
- 16:25:24sending in the data right in the data
- 16:25:26you're sending public ID correct this
- 16:25:28job data so that means this will be the
- 16:25:30this type that you're using so you're
- 16:25:32receiving public ID so this will be as
- 16:25:34delete cloudinary image job data and now
- 16:25:36what you're going to do you'll do a and
- 16:25:38this is where you're going to do delete
- 16:25:40banner image from cloudinary
- 16:25:43now here this is where you are actually
- 16:25:46going to delete this one so that means
- 16:25:47this is the place we are actually
- 16:25:49calling this delete function which is
- 16:25:51actually going to delete the asset from
- 16:25:53your uh cloud ID. Now this one you see
- 16:25:56that we are not doing in our route we
- 16:25:58are doing in the background. Okay. So
- 16:25:59this is what you're going to do and then
- 16:26:00pass the public ID. What will be public
- 16:26:03ID? Public ID will be data dot public
- 16:26:05ID. That's it. So this is done and then
- 16:26:08what you're going to do right you have
- 16:26:09to pass a connection here also. If I go
- 16:26:10over here right so you see that uh we
- 16:26:13have the worker see we have to pass the
- 16:26:15worker option and the connection also.
- 16:26:16So here we have to do what right? We're
- 16:26:18going to pass a connection and this
- 16:26:20connection will be your BMQ
- 16:26:23connection. So this is done but this is
- 16:26:25not yet completed. Now what we're going
- 16:26:27to do right we're going to call this
- 16:26:28delete uh cloudinate image worker and
- 16:26:30we're going to take a on event and this
- 16:26:32will be once this is completed. So you
- 16:26:34see that once this is completed so what
- 16:26:36we're going to do right we're going to
- 16:26:38do something. So let's say I'm going to
- 16:26:40log some data here. So what I'll do
- 16:26:42right, I'm going to take our logger doin
- 16:26:44info and I'm simply going to do
- 16:26:47cloudinary
- 16:26:50delete job completed. Okay. And uh let's
- 16:26:56do assets removed something like this.
- 16:26:59Now let's give a completed.
- 16:27:03And here we are going to do this. Let's
- 16:27:04do job do ID. So this will be when this
- 16:27:08is completed. if this is failed. So I'm
- 16:27:10just going to do cloudinary sorry delete
- 16:27:13cloudinary worker dot on and this will
- 16:27:16be when this is failed. So in this case
- 16:27:19we're going to get uh error also right?
- 16:27:22So we have a failed so we have error
- 16:27:23also. So in this case we're going to get
- 16:27:24a job and error. So in this case I'm
- 16:27:27simply going to do logger dot error.
- 16:27:30So let's pass error. So this will be an
- 16:27:34error and I'm simply going to pass a job
- 16:27:37id which will be job dot id
- 16:27:41and I'm simply going to do cloudinary
- 16:27:47delete
- 16:27:48job field and then at the end we're
- 16:27:52going to do a log.info and I'll do
- 16:27:55delete cloudinary
- 16:27:58image worker started.
- 16:28:02So this is done but this is still not
- 16:28:05yet completed. What we are going to do
- 16:28:06right go to package dojson and then we
- 16:28:10are going to add one more script here.
- 16:28:12So I'm simply going to do a worker and
- 16:28:15also do tsx source/
- 16:28:18So we have workers right? So we have
- 16:28:20workers and then let's run uh copy this.
- 16:28:24So do workers/
- 16:28:26this. So now what we'll do right we'll
- 16:28:29go here open one more and I'm just going
- 16:28:32to do npm run
- 16:28:34worker.
- 16:28:37So this is started now see so first
- 16:28:40we'll go here and then first let's do
- 16:28:43one thing let's go to our login right.
- 16:28:45So I'm just going to go to login.
- 16:28:48Let's take create one uh token. We'll
- 16:28:52copy this. We'll go here. We'll change
- 16:28:54this one to delete. And then uh what
- 16:28:57also you're going to do first also let's
- 16:28:59see one thing man. Okay, let's go to our
- 16:29:02uh where is that? Uh routes.
- 16:29:05Okay, we're using delete. That's fine.
- 16:29:07So, we'll do banners slash and let's say
- 16:29:09I wanted to delete which one we wanted
- 16:29:12to delete. Maybe let's check this image,
- 16:29:14right? So, I wanted to check this image
- 16:29:15backend system and maybe I wanted to
- 16:29:17delete this one. So, what I'll do right,
- 16:29:19I'm going to take the ID
- 16:29:22and then I'm going to paste it here. Go
- 16:29:24to headers, add authorization,
- 16:29:27and then I'm going to take the token.
- 16:29:36Okay. And then I'll just add it here. So
- 16:29:38do beer
- 16:29:41space and see what will happen. Now I
- 16:29:43wanted to wait. I wanted to keep this
- 16:29:46one open. So you'll see that this is
- 16:29:49started. Now I'm just going to delete
- 16:29:50send this. Delete it successfully. And
- 16:29:52you'll see that cloudinary delete job
- 16:29:54completed one immediately. Now if you go
- 16:29:56here we have four and refresh this.
- 16:30:00That's it. And you'll see that uh I
- 16:30:03think we have added four rightes. Okay
- 16:30:05let's do one thing. I think let's delete
- 16:30:09uh I think that is the reason I should
- 16:30:10should not have added all of these here
- 16:30:12man. So maybe what I'm going to do
- 16:30:14right. Okay I'll do a different way. So
- 16:30:16first I'll go to I'll copy this. Okay
- 16:30:19I'll copy this. Let's go to post. I'll
- 16:30:21add one more here. So, paste this one.
- 16:30:24Go to body and let's close this and
- 16:30:27select a different one. Maybe let's add
- 16:30:29this one. And then I'm going to send
- 16:30:31this one.
- 16:30:36Okay. This is done. Let's go here,
- 16:30:38refresh this.
- 16:30:41Okay. So, we have this one correct. Now,
- 16:30:43what I'll do, right, I will copy this
- 16:30:45ID.
- 16:30:46Go to delete. And you'll see that we
- 16:30:48have this one open. Okay. So, one job is
- 16:30:50completed. And now I'm going to just
- 16:30:52paste this one here. Delete this.
- 16:30:55Immediately deleted. And you see
- 16:30:57cloudinary delete job. See how fast this
- 16:30:59is. Okay. Crazy. So this is already
- 16:31:02deleted. And if you go here, you don't
- 16:31:05have that image. That's it. So that
- 16:31:07means this is how you're going to
- 16:31:08implement background fe uh background
- 16:31:10jobs. Okay. So you're going to have a
- 16:31:11queue. You're going to have a worker.
- 16:31:13The important part is that you have to
- 16:31:15understand that how when you're going to
- 16:31:16add the job in your queue because we
- 16:31:19have added this one where in our
- 16:31:20services correct this is where we are
- 16:31:21adding this one and then your worker is
- 16:31:23going to pick it up and based on that
- 16:31:25he's going to do the job in the
- 16:31:26background so with this now this
- 16:31:28particular section is completed okay all
- 16:31:31right so now what will be our next step
- 16:31:33next step now we are going to implement
- 16:31:34our Google o login so first of all see I
- 16:31:38think some of you already have seen in
- 16:31:39some other website that whenever you
- 16:31:41you'll go to their login page or signup
- 16:31:43pages. See that you're going to have
- 16:31:45[snorts] like uh consider like you're
- 16:31:47going to have that you sign in with
- 16:31:50email or password or you just sign in
- 16:31:53with Google directly. So how this
- 16:31:56particular flow work? Now see I'm not
- 16:31:57going to go into too much technical but
- 16:31:59I will try to explain this flow in very
- 16:32:01very simpler way. So what usually happen
- 16:32:03let's see if you have I think some I'm
- 16:32:05pretty much sure almost all of you have
- 16:32:08seen this particular flow that whenever
- 16:32:10you click on this Google button or
- 16:32:12whatever it is there let's say link
- 16:32:14you'll see that it will go to a Google
- 16:32:16login page correct and that makes sense
- 16:32:18because we are actually doing the login
- 16:32:20using Google and it's the task of Google
- 16:32:22who is going to actually do the
- 16:32:23verification
- 16:32:25whether this particular user is a valid
- 16:32:27Google user or not. So in that case what
- 16:32:29you have to do right we don't have to
- 16:32:30again manage our password in this case
- 16:32:33because Google is already taking care of
- 16:32:34all of this. So how this flow will work.
- 16:32:37So let's say so first of all you are
- 16:32:39going to have your application right and
- 16:32:41in this application you're going to have
- 16:32:42a button right? So let's say continue
- 16:32:44with Google once you're going to click
- 16:32:45on Google right. So what will happen
- 16:32:47right? So we are going to create a
- 16:32:48Google project for this. First of all we
- 16:32:50need some Google credentials. Why?
- 16:32:52because to navigate that particular user
- 16:32:54from your application to the Google
- 16:32:56login page, you need to configure
- 16:32:58something in your application and that
- 16:33:00is called we need two things. So first
- 16:33:02of all you need actually three things we
- 16:33:04need uh one Google cloud project which
- 16:33:06is going to give you the Google client
- 16:33:08ID and client secret and then it's also
- 16:33:11going to give you something called
- 16:33:12redirect URL. But why actually we need
- 16:33:14Google client and Google secret. So
- 16:33:16first of all what are you going to do
- 16:33:17right? we need to create a new Google
- 16:33:18instance using that client ID and client
- 16:33:21secret and then once user is going to
- 16:33:23click on this particular button. So now
- 16:33:25we are going to use some packages or you
- 16:33:27can directly use that URL also. So what
- 16:33:28will happen? It will navigate this
- 16:33:30particular user to the Google login
- 16:33:32page. So that will be the first step and
- 16:33:34then in the Google login page what we
- 16:33:36are going to do right. So normally we
- 16:33:37are going to create here uh that uh
- 16:33:40something called we have a uh concept
- 16:33:42called select account. So what will
- 16:33:44happen? Let's say if you go to any
- 16:33:45website right and let's say you have
- 16:33:47click on this Google login page uh
- 16:33:49Google login button you'll see that it
- 16:33:50will go to the Google login page and
- 16:33:52there let's say you have logged in using
- 16:33:54two email accounts so what will happen
- 16:33:56right you'll be able to see that okay
- 16:33:57whatever email accounts that are
- 16:33:59available for you that is going to show
- 16:34:01you in this particular login page
- 16:34:03correct and then let's say I wanted to
- 16:34:04click on the first login uh first email
- 16:34:07so once you click on this first email
- 16:34:09what will happen right so it is going to
- 16:34:11do some kind of authentication right and
- 16:34:13then what will happen right? So you are
- 16:34:15going to have some kind of a call back
- 16:34:17URL. Now you might be thinking okay what
- 16:34:18actually this call back URL or why
- 16:34:20actually you need this call back URL.
- 16:34:22Now to explain this one in very simpler
- 16:34:23way you consider that actually you are
- 16:34:26doing the work in your application but
- 16:34:28when you're clicking on this Google
- 16:34:30button you are actually moving away from
- 16:34:32your application means you are not there
- 16:34:33in this application. So what will happen
- 16:34:35once the Google login is successful or
- 16:34:37let's say it is failed. So this Google
- 16:34:39login right or Google cloud console is
- 16:34:42going to give you a call back URL and
- 16:34:43that call back URL is going to come back
- 16:34:45to your application and this call back
- 16:34:47URL is going to hold some information.
- 16:34:49So what information is going to hold?
- 16:34:51It's actually going to actually verify
- 16:34:52that whether this login is successful or
- 16:34:55not. Okay, if the login is successful,
- 16:34:57so what you're going to do, you will
- 16:34:58then redirect the user back to your
- 16:35:00application and let's redirect the user
- 16:35:02to the success page. If this login is
- 16:35:05failed, so in that case what you're
- 16:35:06going to do, it will come back to your
- 16:35:07application and then you're going to
- 16:35:09redirect the user to the failure page.
- 16:35:11So that means logically you need three
- 16:35:13things. You need a client ID, you need a
- 16:35:14client secret and then you need a
- 16:35:16redirect URL. And that redirect URL we
- 16:35:19are going to configure in our cloud
- 16:35:20console. So to sum up in a simpler way,
- 16:35:23we are going to have a Google login
- 16:35:24button. We click on this Google login
- 16:35:26button. We are going to configure this
- 16:35:28one using Google client ID and client
- 16:35:30secret. it will go to the Google uh
- 16:35:32login page. There you'll be able to see
- 16:35:34all the emails that is available for
- 16:35:36you. Let's say you have three emails
- 16:35:37that's possible and you want to register
- 16:35:39using the first email or second email.
- 16:35:41So you click on the first email. Then
- 16:35:42what is going to do? It's going to do
- 16:35:44your authentication process and then it
- 16:35:46is going to go to your callback URL.
- 16:35:48Your call back URL is going to actually
- 16:35:50verify that authentication if that is
- 16:35:51successful or not. And based on that you
- 16:35:53will be back to your application again.
- 16:35:55And then whatever the result if it is
- 16:35:57success you are going to redirect the
- 16:35:59user to the success page. If it is
- 16:36:01failed you redirect the user to the
- 16:36:03failed page. So this is what you're
- 16:36:05going to do. So first of all what we
- 16:36:06have to do right we have to search
- 16:36:08something like Google cloud console.
- 16:36:09Okay. So this is what you have to search
- 16:36:11Google on Google and then you click on
- 16:36:13this Google cloud console and first of
- 16:36:16all what you have to do right you have
- 16:36:17to create a new project here. Now I
- 16:36:19already have so many projects. I'm going
- 16:36:20to use this for NodeJS. This is I
- 16:36:23usually use for my YouTube demos. So
- 16:36:25what you're going to do right once you
- 16:36:27create a simple project and then you're
- 16:36:28going to go to this API services and
- 16:36:31here what you have to do right. So first
- 16:36:32of all you'll see that you're going to
- 16:36:34get this credentials and in this uh
- 16:36:37credentials what you have to do you have
- 16:36:39to create a you see that you have to
- 16:36:40create credentials and you have to
- 16:36:42create a O client ID this client ID you
- 16:36:44have to do. Okay. So now I already have
- 16:36:46this one. So once you click on this O
- 16:36:49client ID right? So you have to click on
- 16:36:51this web client and here you can see
- 16:36:54that you're going to have your client ID
- 16:36:56and this will be your client secret and
- 16:36:58you'll see that you have your origin. So
- 16:36:59for now because we are actually so you
- 16:37:01see that this is for use with request
- 16:37:03from our browser mean when we're going
- 16:37:05to have our UI and whatever UI you are
- 16:37:07running let's say whatever port and you
- 16:37:09are going to use that but in our case
- 16:37:10because we are creating only back end
- 16:37:12only projects what you can do right you
- 16:37:14can change this one to local host 4,000
- 16:37:16because we are running this one on 4,000
- 16:37:18right so you see that we are running on
- 16:37:204,000 the same we have to change this
- 16:37:22one to 4,000 and this will be our
- 16:37:25callback keyword. So in our callback U
- 16:37:27model when you're going to configure we
- 16:37:28are going to do this one as local host
- 16:37:294,000 let's say if you're running this
- 16:37:32on local host 5,000 so you have to just
- 16:37:34configure based on that. So it will be
- 16:37:36SL API/ Google/ callback. Okay so now
- 16:37:40this will be a redirect URLs. Okay user
- 16:37:42will be redirect to this path after they
- 16:37:44have authenticated with Google and the
- 16:37:45same thing I've already explained.
- 16:37:47Correct. Now let's [snorts] save this.
- 16:37:49So this is the first thing we are going
- 16:37:50to do. So now we'll go here once again
- 16:37:53and then we'll go to our URL sorry env.
- 16:37:56Let's add our Google
- 16:37:59client ID Google
- 16:38:03client
- 16:38:05secret and Google
- 16:38:08call back
- 16:38:10URL. Okay. So now first of all let's
- 16:38:14copy this
- 16:38:16paste it here. Then I'm just going to uh
- 16:38:19disable this
- 16:38:21and this en sorry I have to disable this
- 16:38:24one and I'll delete this one.
- 16:38:28And then let's add one more. And then
- 16:38:30I'm just going to copy this. Okay. And
- 16:38:34then I'll paste it here. And then here
- 16:38:36I'm just going to use this one. So this
- 16:38:39will be my uh redirect UR. So these
- 16:38:42three things we need client ID, client
- 16:38:43secret and callback URL. This I'm going
- 16:38:45to delete later. uh so that you guys
- 16:38:47can't use it but just create once uh
- 16:38:50okay it's not that much difficult
- 16:38:52because once you create it will be
- 16:38:53easier for you to also test right so we
- 16:38:56need client ID client secret and call by
- 16:38:57keyword so now we'll go to our config uh
- 16:39:01let's add all of this right so we'll do
- 16:39:03Google client ID so I'll do check
- 16:39:06require env
- 16:39:09client secret so this this will be this
- 16:39:14and then what we have we have Google
- 16:39:16redirect
- 16:39:18redirect URL.
- 16:39:21So now let's go here and copy this
- 16:39:26uh copy client ID
- 16:39:30and we have copy client secret.
- 16:39:34So save this. So this is the first step.
- 16:39:37So now what I going to do right? So we
- 16:39:39are going to use a package called Google
- 16:39:41O library. So I'm just going to do
- 16:39:43Google O library and let's see.
- 16:39:48Now this is much more easier. Okay. And
- 16:39:50that is the reason and see that it's
- 16:39:51actually official supported official sub
- 16:39:53authentication client library for
- 16:39:55NodeJS. And this is the reason we are
- 16:39:57going to use this and you'll see that
- 16:39:58it's much more easier to use. So I'm
- 16:40:00just going to copy this. Let's go here.
- 16:40:03Right. So do npm
- 16:40:08and this is official. Right. Okay. So,
- 16:40:11we'll copy this and then we'll go and
- 16:40:14let's install this one.
- 16:40:21That's it. So, this is done. So, now
- 16:40:23what are we going to do? So, first we're
- 16:40:25going to create our Google helpers. So,
- 16:40:28do Google dot ds. So first let's close
- 16:40:31everything else and I'm going to import
- 16:40:35something from so Google o library and
- 16:40:39this will be my o to client. So first of
- 16:40:42all what will be our Google user profile
- 16:40:45means what all info we need. So first we
- 16:40:47need a Google ID because see remember
- 16:40:48when you're going to do a login
- 16:40:50definitely Google is going to give you a
- 16:40:51Google ID. Second we need a Google
- 16:40:53email. Google email means the actual
- 16:40:55email that the user is going to
- 16:40:56register. Okay. So that means I'm just
- 16:40:59going to create a type and you'll see
- 16:41:00that this is actually not that much is
- 16:41:03uh difficult to understand. So I'll do
- 16:41:04Google user profile and I'm simply going
- 16:41:07to do here Google ID
- 16:41:11and this will be string and we are also
- 16:41:14going to have a email and this will be
- 16:41:15string. Okay. So next what will be the
- 16:41:19uh step? So next we're going to create a
- 16:41:20Google o client. So do Google o
- 16:41:24client. So this will be new o client and
- 16:41:28you need your uh here you're going to
- 16:41:30pass sorry
- 16:41:32you're simply going to pass directly. So
- 16:41:34here you're going to do env that we have
- 16:41:37dot google client id. So env dot google
- 16:41:42client secret and your env dot google
- 16:41:45client
- 16:41:47google redirect.
- 16:41:49Okay. So now let's save this. So first
- 16:41:53of all what you have to do? So first uh
- 16:41:55let's go to routes. Okay. So we are
- 16:41:57going to come from routes because it
- 16:41:59will be easier. So I'll go to O routes
- 16:42:01because we are using O only here. So
- 16:42:04first what I have to do right? So when
- 16:42:06I'm going to go to this URL. So let's
- 16:42:08I'll do / API/
- 16:42:11Google. Now what will happen? See first
- 16:42:14of all we don't have any UI correct but
- 16:42:16in UI you are going to click on a button
- 16:42:18or you're going to click on a link or
- 16:42:19whatever you're going to do. Okay, let's
- 16:42:21say clicking on any text whatever
- 16:42:22continue with Google. So it will go to
- 16:42:25the Google login page. See in our case
- 16:42:26whenever we'll go to this particular or
- 16:42:28we are going to hit this particular in
- 16:42:30our browser localhost 4000/ API/op/
- 16:42:33Google it should go to your Google login
- 16:42:36page. So that means we are going to
- 16:42:38redirect that particular uh page to the
- 16:42:41Google login page. Correct? So now what
- 16:42:43basically this we call as Google consent
- 16:42:46screen. Right? So this is where you're
- 16:42:48going to see your Google login page. But
- 16:42:49how we are going to do this? So first
- 16:42:51I'm just going to do a router dot get
- 16:42:54and this will be / google and then I'm
- 16:42:57just going to take so do request
- 16:42:59response
- 16:43:01and here we going to do con and I have
- 16:43:03to create my Google or URL. Okay. So
- 16:43:07here I'm just going to do start Google
- 16:43:10login
- 16:43:12and now what I have to do right this is
- 16:43:14important. So we're going to do race and
- 16:43:16it's going to give you this redirect. So
- 16:43:17that means I'm actually redirecting from
- 16:43:19my application to the Google login page
- 16:43:22and this is what we're going to create.
- 16:43:23So first we will copy this. We'll go
- 16:43:25here and let's create this. So we'll do
- 16:43:27export function
- 16:43:29Google
- 16:43:31what I'm doing man. So first we are
- 16:43:34going to create start login. Right? So
- 16:43:35in this one maybe let's do one thing. Uh
- 16:43:38okay let's okay let's keep it here only.
- 16:43:41Maybe I think this only makes sense. So
- 16:43:43here I'm just going to do a string and
- 16:43:45here I'm simply going to do return and
- 16:43:48let's do get Google
- 16:43:50O URL. So I'm going to create this. So
- 16:43:54this is going to return a string and
- 16:43:55here I'm simply going to create this
- 16:43:57now. So what we going to do right? So
- 16:43:59first maybe I'm doing wrong man. No no I
- 16:44:02think this is not correct. Why? We
- 16:44:04should not keep this one in this Google
- 16:44:06page. So let's do one thing. This one I
- 16:44:08think let's keep in the service file.
- 16:44:10Okay. I'll tell you why. because this is
- 16:44:12where you're calling the service right
- 16:44:14so go to service go to all service sorry
- 16:44:16my bad here we are going to do this so
- 16:44:18we are going to keep this one here okay
- 16:44:20so this is my service and now this one
- 16:44:22we going to create in our helper so I'm
- 16:44:25just going to do here export function
- 16:44:28get google o url so here we'll take this
- 16:44:31one as string and now what actually this
- 16:44:33is going to return right so we are going
- 16:44:35to take our Google o client and then
- 16:44:37we're going to use the generate o url
- 16:44:39okay now couple of things we need to uh
- 16:44:42set here. So first we have to set
- 16:44:43something called scope. So what we going
- 16:44:45to set right? So here we are going to
- 16:44:47basically tell that we need email plus
- 16:44:50the profile information to identify the
- 16:44:52user. Okay. And that user we are going
- 16:44:54to store in our DB. Right? So that means
- 16:44:56we are going to simply tell okay this
- 16:44:58will be open ID. We also need email and
- 16:45:00this will be profile.
- 16:45:02Then we also need something called
- 16:45:04access type. In this access type will be
- 16:45:06online.
- 16:45:08And then okay this is also optional.
- 16:45:10Okay. So and this is optional actually
- 16:45:12needed for this sorry this is optional
- 16:45:14for refresh token. Okay now you actually
- 16:45:16don't need in this particular one but
- 16:45:18still you can keep it then we have
- 16:45:19something called prompt. Now this is
- 16:45:21important in this prompt means simply
- 16:45:22you are going to tell this will be
- 16:45:23select account. What this is going to do
- 16:45:25right this is going to enforce the user
- 16:45:27to select your email account. I already
- 16:45:29mentioned it. Let's say you have
- 16:45:30multiple email accounts. So it's going
- 16:45:31to give you that which one you want to
- 16:45:33select. So this is going to generate our
- 16:45:35o URL. So this is the first thing we
- 16:45:37have to do. So now we'll go to our o
- 16:45:39routes right. So we have this start
- 16:45:41google login that we are going to import
- 16:45:42from our service. So from this
- 16:45:44particular file and then in this service
- 16:45:46we're going to get our o URL. So that
- 16:45:49means this particular one we are going
- 16:45:51to do. So this is the first thing we
- 16:45:52have to do here. Now what will be the
- 16:45:54next step? Next step will be we need to
- 16:45:56handle our call back because I already
- 16:45:58mentioned once we're going to click on
- 16:45:59this account it's going to come back to
- 16:46:01your call back URL and it's going to do
- 16:46:03the authentication. Right? But before
- 16:46:04this let's do one thing. Let's go here.
- 16:46:06Right. I think it's already running. So
- 16:46:08I'm just going to close and rerun rerun
- 16:46:10once again. And then I'm going to copy
- 16:46:12this and let's go here and then do this
- 16:46:16slash. So you have API/
- 16:46:20Google.
- 16:46:21That's it. So this is where you're going
- 16:46:23to see your selector means which account
- 16:46:26sorry which account you are going to
- 16:46:27select you're getting. So that means
- 16:46:29this is now redirecting to your Google
- 16:46:32login page. Okay. So now what will be
- 16:46:35the next step? Next step will be we're
- 16:46:36going to create our second route which
- 16:46:39will be to handle our call back and this
- 16:46:40is where actual work we are going to do
- 16:46:42and this is where we are actually going
- 16:46:44to get our access token and based on
- 16:46:46this access token we are going to
- 16:46:47authenticate the user. So now next will
- 16:46:50be so I'm just going to do a router get
- 16:46:53so this will be / google/allback
- 16:46:57right we are using callback and then
- 16:46:59we're going to do async. So this will be
- 16:47:01request response
- 16:47:04next
- 16:47:06and first what we're going to do right
- 16:47:08we're going to do try catch
- 16:47:11so let's take error let's take like this
- 16:47:16and here we're going to do next we're
- 16:47:18going to pass error and now what we'll
- 16:47:20do right so first I'm just going to do
- 16:47:22code and this one you're going to get
- 16:47:24from your query so I'll do request
- 16:47:25doquery doc code so this is going to
- 16:47:27give you a code as string or this will
- 16:47:30be undefined. Okay. So now what we're
- 16:47:33going to do, right? We are going to
- 16:47:35create our another helper function where
- 16:47:38we are actually going to do what? So we
- 16:47:40are going to so this callback URL
- 16:47:42whatever we are implementing this is
- 16:47:45going to use that code that whatever we
- 16:47:47are going to receive right and it's
- 16:47:49going to return your profile data or
- 16:47:50profile information.
- 16:47:52So now let's do this one like this. So
- 16:47:54I'm just going to do export sync
- 16:47:56function and I'll do this one as uh what
- 16:47:59are we going to do? So first of all
- 16:48:01let's do one thing I think again I'm
- 16:48:02doing wrong maybe let's uh first create
- 16:48:05one more service because if first I'll
- 16:48:07create the service and then we'll create
- 16:48:09the helper. So first I'll do export as
- 16:48:12sync
- 16:48:13function and let's do login with Google.
- 16:48:17Okay, login with Google and this is
- 16:48:20going to receive the code which will be
- 16:48:22string and here the return type will be
- 16:48:24this will be promise of so we're going
- 16:48:26to receive access token the actual
- 16:48:28access token we're going to receive. So
- 16:48:29access token and this will be string. So
- 16:48:33now what are we going to do right? So
- 16:48:35first let's do con Google profile and
- 16:48:38this will be so first what are we going
- 16:48:40to do right so first we need to verify
- 16:48:42Google code and we need to get our
- 16:48:44Google profile back right so we need to
- 16:48:46extract the Google profile from our code
- 16:48:48so here I'm just going to do a and let's
- 16:48:51do get Google user
- 16:48:55from o code okay so this will be our
- 16:48:58method where we're going to pass this
- 16:48:59code here now this one we're going to
- 16:49:01create here okay so I was doing wrong
- 16:49:03because first we should create the
- 16:49:04service and then we will create here. So
- 16:49:06this is going to receive the code and
- 16:49:08this will be string and what this is
- 16:49:10going to return now this is going to
- 16:49:12return a promise and here we are going
- 16:49:13to use this Google user profile because
- 16:49:15we need the Google ID and the email.
- 16:49:17Okay. So here we're going to use this
- 16:49:19Google user profile type. So how we are
- 16:49:21going to get this? So first you have to
- 16:49:23give a check that if this code is not
- 16:49:24present. So I'm just going to throw new
- 16:49:27error
- 16:49:29not new error what I'm doing app error.
- 16:49:31So we're going to do an app error and
- 16:49:32here we're going to do 400 and let's do
- 16:49:35Google O code is required. So this will
- 16:49:40be the first step. Okay. So now next
- 16:49:42step will be first we're going to get
- 16:49:44our something from this method. So I'm
- 16:49:46just going to have it. So Google client
- 16:49:49and this is going to give you this get
- 16:49:50token. So you so what will happen right?
- 16:49:52So that is the advantage of using that
- 16:49:54package. So directly use this get token
- 16:49:56method and you just have to pass the
- 16:49:58code right. So here you have to get the
- 16:50:00code right. This is going to get the
- 16:50:01access token for the given code and this
- 16:50:03code is nothing but the authorization
- 16:50:05code. Okay. So here you're going to pass
- 16:50:06this code and this is going to give you
- 16:50:08the access token. So that means here
- 16:50:10you're going to get the tokens. Okay. So
- 16:50:12now what you'll do right this is
- 16:50:13actually very important. This first of
- 16:50:15all you have to check that in this token
- 16:50:18if the [snorts] ID token is sorry if the
- 16:50:19ID token is present or not. If this is
- 16:50:22not present so that that means that
- 16:50:24Google login failed. So we are going to
- 16:50:26do app error.
- 16:50:29So what will be the error code? So this
- 16:50:30will be 401 not because I I thought it
- 16:50:33will be 400. No, it will be 401. And
- 16:50:36here we're going to do Google login
- 16:50:37failed.
- 16:50:39Uh what I can do ID token missing
- 16:50:41something like this.
- 16:50:43So this is the first thing and then
- 16:50:45we're going to do what? So we're going
- 16:50:46to verify the token and we're going to
- 16:50:48read the user information from Google.
- 16:50:50But how we are going to do this? So I'm
- 16:50:52just going to do something like ticket
- 16:50:54info. Okay. So here we'll do a Google O
- 16:50:58client and then we'll do verify ID
- 16:51:00token. Okay. And this is where you're
- 16:51:02going to pass two things. So first will
- 16:51:03be ID token which will be what tokens do
- 16:51:05ID token and you're going to pass
- 16:51:07audience. What will be your audience? So
- 16:51:09audience you're going to verify from
- 16:51:11your env. So in your env you have this
- 16:51:13Google client ID. So from this client
- 16:51:15ID, this will be your actual unique
- 16:51:17client ID from where you're going to
- 16:51:19verify this. So this will be your Google
- 16:51:21client ID. Okay. So this is going to
- 16:51:24give you the ticket information and then
- 16:51:25what you're going to do right you're
- 16:51:26going to do con payload and this will be
- 16:51:30ticket info dot get payload and this is
- 16:51:33is going to give you the information.
- 16:51:35Okay now what we're going to do right
- 16:51:37simply I'm simply going to check if this
- 16:51:39payload dot sub or if this payload do
- 16:51:44both are not present. So I'm simply
- 16:51:46going to do again throw new app error
- 16:51:50401
- 16:51:51and let's do Google login field
- 16:51:56Google login field and this will be
- 16:51:58invalid user profile
- 16:52:02and we're going to return Google ID
- 16:52:05which will be your payload dot sub and
- 16:52:08you're going to pass the email and this
- 16:52:10will be your payload email but what you
- 16:52:12should do right you should do it too
- 16:52:14lower case I usually to do all this and
- 16:52:16then also you're going to pass a claim
- 16:52:18here. So now what will happen right? So
- 16:52:20first of all let's go through. So first
- 16:52:22is checking the code then it's getting
- 16:52:24the token using this particular method.
- 16:52:25It will easily give you the token. We
- 16:52:27are simply checking the ID token present
- 16:52:29or not. If it is present we are
- 16:52:30verifying the token. We getting the
- 16:52:32ticket info the user info. This is
- 16:52:34nothing but your user info. Okay. So
- 16:52:36this will be your user info
- 16:52:39received from Google. And then we're
- 16:52:42going to get our payload. If this
- 16:52:45payload sub and email is not present. So
- 16:52:47we're going to return this or else. So
- 16:52:49here what we so you're going to return
- 16:52:51the Google ID and the email. So save
- 16:52:53this. So now we'll go to our o routes.
- 16:52:56So first of all what we doing right? So
- 16:52:58first so in this callback we're getting
- 16:53:00the code and then what I'll do right. So
- 16:53:02first I'll do con
- 16:53:04something and we're going to call this
- 16:53:06service right. So this login with
- 16:53:08Google. So do a so login with Google and
- 16:53:12here we're going to pass the code or
- 16:53:14else this will be empty and what you're
- 16:53:16going to receive you're going to receive
- 16:53:17the access token right and this access
- 16:53:19token you're going to return at the end
- 16:53:21so do raise dot status so this will be
- 16:53:24200 dot JSON and here we're going to do
- 16:53:28success will be true and data will be
- 16:53:32this access token so this is done but
- 16:53:35not yet done because first of all we
- 16:53:36have to create our service right So
- 16:53:38first we're going to get our Google
- 16:53:40information from this method that we
- 16:53:42created. So to get use Google user from
- 16:53:44O code. So just import this.
- 16:53:48And now first of all let's do one thing.
- 16:53:49So a couple of things you have to do. So
- 16:53:51first see this is where you have to
- 16:53:55understand some of the things. So first
- 16:53:56we have to what we have to do right. So
- 16:53:58first you have to find this particular
- 16:54:00user from uh find this particular user
- 16:54:03by Google ID first. And what I mean by
- 16:54:05this and this particular method we are
- 16:54:07going to write in our repository. So
- 16:54:08first we have to see if that particular
- 16:54:10thing so not particular thing if that
- 16:54:12particular user is already present or
- 16:54:14not. Okay. So first what I'll do right
- 16:54:16simply so let's do one thing. So I'll do
- 16:54:19let user and I'm going to have it I'll
- 16:54:23do find user by Google ID. So the first
- 16:54:28thing and here what I'll do I'll do
- 16:54:30Google profile and see this is going to
- 16:54:32give you the Google ID right. So now
- 16:54:35what I can do right I can copy this and
- 16:54:37let's go to the repository
- 16:54:42and this is where we are going to
- 16:54:44implement this one right so first let's
- 16:54:46start with the so this will be export as
- 16:54:49sync function this will be find user by
- 16:54:53Google ID and you're going to receive
- 16:54:54the Google ID
- 16:54:57this will be string and this is going to
- 16:54:59return promise so either it's going to
- 16:55:01return your user information or else
- 16:55:03it's going to return null. So first I'm
- 16:55:05just going to do con result. So this
- 16:55:07will be a pull dot query.
- 16:55:11So this will be my user row. No, I think
- 16:55:14we have DB user row. Correct? We have DB
- 16:55:16user row. And here we are going to use.
- 16:55:22So we'll do select id email ro
- 16:55:28and let's do created at all these from
- 16:55:32users and now what we'll do where so
- 16:55:35let's go here and let's go to user and
- 16:55:38see that we have this Google ID see
- 16:55:39everything is linked so now I can simply
- 16:55:41check if this Google ID is equal to this
- 16:55:45right and then we going to pass this
- 16:55:47Google ID here so this is going to give
- 16:55:48me the information if that is present or
- 16:55:50not
- 16:55:52So I'm going to return result dot rows
- 16:55:54of zero
- 16:55:56or else null.
- 16:55:59So save this. Now I'll go to our
- 16:56:01service. So this will be first we are
- 16:56:03finding the user right. If this user is
- 16:56:06not present. So what are we going to do?
- 16:56:08So first first let's see if this is not
- 16:56:11present. So in that case what we're
- 16:56:13going to do right. So we are actually
- 16:56:14going to check if this particular email
- 16:56:17is already exist or not. So that means
- 16:56:20what I mean by [snorts] this now this is
- 16:56:21actually very important what I'm trying
- 16:56:23to say. So let's say that is one
- 16:56:25particular user right and if this
- 16:56:28particular user already logged in with a
- 16:56:31certain email and the same user try to
- 16:56:34log in another time using Google login.
- 16:56:37So that means what I'm trying to say. So
- 16:56:39let's say if I go here I'm not sure if
- 16:56:41you're getting so let's say same user a
- 16:56:44user already registered with one
- 16:56:46particular email. Okay. So it's like
- 16:56:49email ABC.com
- 16:56:50whatever it is and the same user is try
- 16:56:54to use or try to use the Google login
- 16:56:57and try to login with the same email. So
- 16:56:59that should not happen. So in that case
- 16:57:01what I'm going to do right. So first
- 16:57:02let's import this. So here I'm just
- 16:57:04going to do user and this will be a and
- 16:57:07I'm just going to do find user by email
- 16:57:10now. So this is the first thing and here
- 16:57:12I'm just going to do Google profile do
- 16:57:14email and this will be another method we
- 16:57:16are going to create where we are going
- 16:57:18to create in our find user by email
- 16:57:20right I think find user by email we
- 16:57:22already have
- 16:57:24find user by email okay this one we can
- 16:57:26use so this is what we going to use here
- 16:57:28so I'm simply going to do user will be
- 16:57:30find user by email and now what I'm
- 16:57:33going to do I'm simply going to check if
- 16:57:34this user is present so in this case
- 16:57:38there are two option so first one will
- 16:57:40be we need to link this Google account
- 16:57:42to the existing user or second if this
- 16:57:46is a first time login so in that case we
- 16:57:48need to create the user in the DB I know
- 16:57:50this is little complicated so what I
- 16:57:51mean by this if this user you are
- 16:57:53already getting so you need to link this
- 16:57:56user because this user is again try to
- 16:57:58login using Google so you need to link
- 16:58:00this user or link this Google account
- 16:58:04to the existing
- 16:58:07email user Okay. And the second scenario
- 16:58:11will be if this is not the case. So in
- 16:58:13that case if it is a first time Google
- 16:58:15login
- 16:58:16so we need to create the user
- 16:58:22in postgress. So that means it's the
- 16:58:24first time login and it say u new email.
- 16:58:28So in that case we have to create this
- 16:58:29user. So that means the first one will
- 16:58:31be user will be equal to a and I'll do
- 16:58:34link Google
- 16:58:36ID to user. And here we're going to pass
- 16:58:40this user do ID and I'm going to pass
- 16:58:42the Google profile dot Google ID both.
- 16:58:46Okay. So this is if the user is pres
- 16:58:48already present. If this is not present
- 16:58:50so else case. So we're going to create a
- 16:58:52new user. So here I'm just going to do
- 16:58:54user will be have it create Google
- 16:58:59user. So here I need the Google profile
- 16:59:02email and Google profile. Google ID.
- 16:59:06Okay. So this is what we going to do. So
- 16:59:08now what you can do right you can create
- 16:59:10both. So I'll do link Google to user.
- 16:59:15So we'll do export as sync function. So
- 16:59:18I'll do link
- 16:59:20uh Google to user. So for this one what
- 16:59:23we going to do right? We're going to
- 16:59:24what we're sending here user ID and
- 16:59:27Google ID. So do user ID. This will be
- 16:59:30string and this will be Google ID. This
- 16:59:33will be also string. And here we're
- 16:59:35going to return promise and this will be
- 16:59:38user.
- 16:59:40So I'll do con this will be result and
- 16:59:43here we're going to do a pool dot query.
- 16:59:47So this will be DB user
- 16:59:51no pool dotquery DV user row and here
- 16:59:55we're going to now do what we going to
- 16:59:57link this user. How we going to link?
- 16:59:59We're going to update. So we're going to
- 17:00:00update the user and then I'm just going
- 17:00:03to do set. So I'll do set Google ID
- 17:00:08equal to this and also I'm just going to
- 17:00:12do updated at. So I'll do updated at
- 17:00:15equal to now
- 17:00:18where we need to check right. So where
- 17:00:20what where the user ID is this. So that
- 17:00:22means where your ID is equal to this. So
- 17:00:25this ID is nothing but your user ID this
- 17:00:28one. Okay. So this is what and then at
- 17:00:31the end I'm just going to do return in
- 17:00:33ID email
- 17:00:36role and here I'm just going to do
- 17:00:38created add
- 17:00:40and now I can simply pass this. So first
- 17:00:42one will be my Google ID and second one
- 17:00:45will be my user ID
- 17:00:47and then we're going to return result
- 17:00:49dot rows of zero. So save this. So now
- 17:00:53this is going to link your existing user
- 17:00:56sorry link this Google account to the
- 17:00:58existing email user. if this user is
- 17:01:00already present. Okay, so this is what
- 17:01:02we're going to import. If this user is
- 17:01:03not present, if this is a first time
- 17:01:05login, we're going to create the Google
- 17:01:06user. Okay, so here this will be. So
- 17:01:09let's go here. So I'm just going to do
- 17:01:11export as sync function. So do create
- 17:01:16Google user. So what are the things we
- 17:01:19are sending? We sending email and Google
- 17:01:22ID. So do email string
- 17:01:26Google id string. What this is going to
- 17:01:30return again it's going to return
- 17:01:32promise user.
- 17:01:34So now so this will be con result.
- 17:01:38So this will be a pull dot query
- 17:01:44and here we are going to simply do.
- 17:01:47So this will be add right. So we are
- 17:01:49going to insert. So insert into users
- 17:01:54uh no sorry insert into users. So you
- 17:01:56need to mention what you want to insert.
- 17:01:58So you need to insert email and the
- 17:02:01Google ID. Right? So you have to do
- 17:02:02Google ID. I'm not sure what is that
- 17:02:05Google ID.
- 17:02:08Okay. And then we're going to pass the
- 17:02:10values. So values will be your 1 comma
- 17:02:122. And then you're going to do
- 17:02:14returning. So we're going to do id email
- 17:02:17role
- 17:02:20and we're going to do created add.
- 17:02:24So I think that's it. And then we're
- 17:02:26going to pass the email
- 17:02:28and the Google ID
- 17:02:31and again the same thing. So do result
- 17:02:34dot rows of zero. So save this. So now
- 17:02:39what we're going to do right we're going
- 17:02:40to simply import this one here. So if
- 17:02:43you can understand all of this right. So
- 17:02:45I think you'll see that it's not that
- 17:02:47much difficult. Okay, what we getting?
- 17:02:48We have to return the token that is
- 17:02:50fine. So that means first we're checking
- 17:02:51if this uh so here first we are finding
- 17:02:54this user right and here if this is this
- 17:02:57not equal to true then we have try to
- 17:02:59find the user from email. So first we
- 17:03:01try to find the user using Google ID. If
- 17:03:04this is not the case so in that case
- 17:03:06we'll try to find the user using email.
- 17:03:08If this user is already present we're
- 17:03:10going to link that user. If that not
- 17:03:12present first time login we're going to
- 17:03:13create the user. Okay. So now what I can
- 17:03:16do right where is that this one. So
- 17:03:18after this I'm just going to do con. So
- 17:03:21do access token and this will be
- 17:03:25so create access token. I think we don't
- 17:03:27have created this one. Right? So we're
- 17:03:29going to create this one. So let's do
- 17:03:31one thing. I'm simply going to do
- 17:03:33function. Let's do create access token
- 17:03:38for user. And here I'm just going to get
- 17:03:41the user. And this will be my user type.
- 17:03:44And this is going to return a string.
- 17:03:46And here I'm just going to do return
- 17:03:49sign access token.
- 17:03:51Sign access token will be
- 17:03:54this one. Right? So here we're going to
- 17:03:55pass the sign access token. And we're
- 17:03:58going to pass your user ID which will be
- 17:04:00your user do ID. Email will be user do
- 17:04:05email and role will be user dot ro.
- 17:04:11Okay. So this will be what we're going
- 17:04:12to do. So this is going to create your
- 17:04:14user information, pass the user and at
- 17:04:17the end you're going to return the
- 17:04:18access token.
- 17:04:21And this is done. Now this is actually a
- 17:04:24long process but if you take some time
- 17:04:26and try to understand everything is
- 17:04:28linked. So if you go here right I try to
- 17:04:30understand. So let's see it will go to
- 17:04:32Google with login sorry Google with
- 17:04:34login with Google first. Try to f the
- 17:04:37Google profile. So here we are simply
- 17:04:40checking based on the authorization code
- 17:04:42we're going to take our tokens get the
- 17:04:44ticket info or the user info uh get the
- 17:04:47payload and return the Google ID and the
- 17:04:49email once it will get the Google uh
- 17:04:52Google ID and the email it will try to
- 17:04:53find the user if this user is not
- 17:04:56present it will try to find the user by
- 17:04:58the email if this user is present with
- 17:05:00the email it's going to link this Google
- 17:05:02account or else it's going to create the
- 17:05:04user for the first step then it will
- 17:05:06create the token and return the token
- 17:05:08And that's it. So now let's see if I try
- 17:05:11to go to let's say localhost 4,000
- 17:05:16uh 4,000/ API/
- 17:05:19Google and let's I try to click on this
- 17:05:22email.
- 17:05:25That's it. And you can see that you're
- 17:05:27getting data and this is your access
- 17:05:29token, right? And what you can do that
- 17:05:31you can verify this also if this is
- 17:05:33working or not. So what I'll do, right?
- 17:05:35So let's see if I go here. Not this.
- 17:05:38Let's clear this. I think I've added
- 17:05:40this one in some place. Let me just
- 17:05:42check. Okay, this one. Let's copy this
- 17:05:45and let's execute this here. And now I'm
- 17:05:48just going to do select ID, email, and
- 17:05:50ro from user. Okay, paste this. Enter.
- 17:05:54And now we'll see that you have a
- 17:05:56samukhargi 2022
- 17:05:58at thegmail.com. So that means these
- 17:06:01user created using Google login. These
- 17:06:03two user created using normal login and
- 17:06:05this is manual admin. So that means with
- 17:06:07this you can understand every possible
- 17:06:10way that how you can create user. Okay.
- 17:06:13So with this now Google login is
- 17:06:15completed and with this we have finally
- 17:06:18completed this particular video. I think
- 17:06:20we don't have that much to cover as I
- 17:06:22already mentioned that we I'm going to
- 17:06:24cover uh so many other things also in
- 17:06:27the part three but see that is not going
- 17:06:28to come anytime soon but that will come
- 17:06:31in the part three. uh I'm going to cover
- 17:06:33very advanced concept like worker
- 17:06:35threads and all these child processes.
- 17:06:37Uh there will be some more advanced
- 17:06:39topic like cookies and everything
- 17:06:40deployment and everything. But for now I
- 17:06:43already have multiple deployment videos.
- 17:06:44You can watch those videos. But with
- 17:06:47this that's all for this particular
- 17:06:49video. I will see you in my next video.
- 17:06:50Till then good luck and peace.
About this transcript
This page contains the full transcript of Node JS Full Course 2026 | Complete Backend Development Course | 17 Hours by Sangam Mukherjee, generated from the public captions YouTube serves with the video. The transcript has 195,134 words across 26,574 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.