YouTube2Text

Node JS Full Course 2026 | Complete Backend Development Course | 17 Hours — Transcript

by Sangam Mukherjee · 195,134 words · 26,574 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Hey everyone, welcome back. So in this
  2. 0:01particular video, you're going to learn
  3. 0:03each and everything that you need to
  4. 0:04know to start your journey with NodeJS.
  5. 0:06And this is a open challenge to each and
  6. 0:08every one of you. Just watch this
  7. 0:10complete video and then show me a better
  8. 0:12course than this one on YouTube even in
  9. 0:15the paid courses. Okay. So let's see
  10. 0:17what are the things we're going to
  11. 0:18cover. So first of all, I wanted to
  12. 0:19mention the very key point and that is
  13. 0:21this complete course is built on
  14. 0:22posgress and this is not a MongoDB
  15. 0:25course that you usually find on other
  16. 0:27YouTube videos. Okay. second. So let's
  17. 0:29see all the sections because this is
  18. 0:30very very important. Uh you need to know
  19. 0:32that what are the things we going we are
  20. 0:34covering and in which order. Okay.
  21. 0:36Before that uh wanted to mention one
  22. 0:38more point and that is this particular
  23. 0:40video took me more than 2 months to
  24. 0:41create. So please like this video,
  25. 0:44comment and also if possible please
  26. 0:46share with your friends and this will be
  27. 0:47very much helpful because this will help
  28. 0:49my channel to grow and it will also help
  29. 0:52to complete 100k as soon as possible. So
  30. 0:54first of all you can see that this is
  31. 0:56actually divided into 11 section. So we
  32. 0:58are going to start with setup. So first
  33. 1:00we will understand how you can install
  34. 1:02NodeJS runtime basics, npm package.json
  35. 1:05and scripts, TypeScript configuration
  36. 1:08and process object. Then we'll move to
  37. 1:10our next module which is very very
  38. 1:12important. We are going to understand
  39. 1:13all the code modules. So we are going to
  40. 1:15understand crypto module, OS module,
  41. 1:17path module. Then we are going to
  42. 1:19understand timers asynchronous flow file
  43. 1:22system APIs, buffers, URL module, event
  44. 1:25emitter and we are going to end this
  45. 1:27particular module with streams. Then we
  46. 1:30have a dedicated section on HTTP module.
  47. 1:32So here we are going to understand
  48. 1:34native HTTP module without express. Here
  49. 1:37we are going to cover native HTTP
  50. 1:39server, manual routing, reading request
  51. 1:41bodies, JSON responses and external API
  52. 1:45implementation. Then we are going to
  53. 1:46have a little more advanced uh section
  54. 1:49we are going we are going to understand
  55. 1:50NodeJS internals. So NodeJS runtime
  56. 1:52internals V8 engine live blocking versus
  57. 1:56non-blocking and cluster module. Okay.
  58. 1:59Then we have a complete dedicated
  59. 2:01section on postgress. Now this is a
  60. 2:03mandatory section. This you need to
  61. 2:05complete before we are going to uh start
  62. 2:07working on the CRUD. So here you're
  63. 2:08going to understand database, schema,
  64. 2:10tables, data types, constants, keys, uh
  65. 2:14complete SQL, CRUD, relationships, join,
  66. 2:17aggregations and indexes and
  67. 2:18transactions. Okay. Then we are going to
  68. 2:20have our main master project that we are
  69. 2:23going to build and this will be huge
  70. 2:24project. You're going to understand a
  71. 2:26lot of things. So first the first module
  72. 2:28will be to understand how you can create
  73. 2:30a proper express project, proper folder
  74. 2:33structure. So you can see that we are
  75. 2:34going to have our application setup with
  76. 2:36express poller. We are going to use
  77. 2:38scalable folder structure routes health
  78. 2:41checks server life cycle docker compost
  79. 2:43posgress setup custom database migration
  80. 2:46because we are not using any third party
  81. 2:48packages and centralized error handling.
  82. 2:50Then we are going to have our o and JWT.
  83. 2:53So we're going to have user registration
  84. 2:54flow login JWT generation authentication
  85. 2:57middleware admin middleware and we are
  86. 2:59also going to implement Google O login.
  87. 3:02Then we'll move to our CRUD and admin
  88. 3:04API. So we're going to have our normal
  89. 3:05crap because this is actually mandatory.
  90. 3:07So we are going to create user task,
  91. 3:09fetch, update, delete and then admin
  92. 3:11operations. Then moving to next one,
  93. 3:13we're going to have a complete dedicated
  94. 3:14section on file upload and we're going
  95. 3:16to use cloudinary. So with this we're
  96. 3:18also going to use mult. So this will be
  97. 3:20how you can handle multipart upload
  98. 3:22flow, multiple middleware, cloudinary
  99. 3:24upload helper, banner creation and fetch
  100. 3:27APIs. Then we're going to move to our
  101. 3:28radius section. So in this particular
  102. 3:30section we're going to cover radius. So
  103. 3:32this is also a mandatory section. So
  104. 3:33here you're going to understand all the
  105. 3:35radius concept how you can create radius
  106. 3:37client cash asset pattern still cast
  107. 3:39invalidation rate limiting and popsup
  108. 3:41concept and then we'll move to our next
  109. 3:43section which will be how we are going
  110. 3:45to integrate radius and bool mq and you
  111. 3:47can implement background jobs for this
  112. 3:48one we're going to implement this one in
  113. 3:50our previous project only so we're going
  114. 3:51to understand how you can create cues
  115. 3:53jobs workers blemq project setup
  116. 3:55cloudinary deletion in the background
  117. 3:57and also how you can implement a
  118. 3:59reliable asynchronous architecture so
  119. 4:01all those things we are going to cover
  120. 4:02in this whole module. All right. So
  121. 4:04before we'll start so two very important
  122. 4:06points I wanted to mention. The first
  123. 4:07one is that I finally created the
  124. 4:09official LinkedIn page for age debop. So
  125. 4:12I highly request everyone to follow this
  126. 4:13particular page and help me to reach at
  127. 4:16least 5,000 followers as soon as
  128. 4:18possible because some of you already
  129. 4:19know that I'm not that much active on
  130. 4:21social media and don't promote all these
  131. 4:23videos and everything. But because my
  132. 4:24channel is almost close to 100K so I
  133. 4:27thought this will be right time to start
  134. 4:29the page. So I think I need help from
  135. 4:31all of you. So I've added the link in
  136. 4:32the description. Second, uh I think some
  137. 4:35of you already know that I have a
  138. 4:36upcoming course on my debub which is my
  139. 4:39uh website and you can see that almost
  140. 4:415,000 people already joined the weight
  141. 4:42list. So in case you are interested,
  142. 4:44please uh join this course. I added the
  143. 4:47link in the description also. Third
  144. 4:49thing uh to get the source code of this
  145. 4:51enter nodejs uh course what you can do
  146. 4:53right I've added the link in the
  147. 4:56description that is also completely
  148. 4:58free. So you can just go to the project
  149. 4:59section and you will be able to download
  150. 5:01the source code for free. So with this
  151. 5:03now let's get started and good luck.
  152. 5:06Okay. So now let's get started. So this
  153. 5:08will be a very long video. I think I
  154. 5:10already explained and also by the length
  155. 5:12of this video you can understand. So
  156. 5:14what I'll suggest right so a couple of
  157. 5:15things I wanted to mention. Uh so first
  158. 5:17one is I think a lot of people already
  159. 5:19asked me that I already have multiple
  160. 5:21NodeJS uh videos that uh after watching
  161. 5:25those videos do I still need to watch
  162. 5:27this particular video? See this answer
  163. 5:29is little tricky the why I'm telling
  164. 5:31this because I already uploaded one
  165. 5:34video that doesn't mean that in this
  166. 5:36particular video I'm going to discover
  167. 5:37some new things and I'm going to explain
  168. 5:39you are getting at the end of the video
  169. 5:41you'll see that we are going to
  170. 5:43understand the almost similar concept
  171. 5:45that I already covered in the previous
  172. 5:46video but then what is the difference
  173. 5:49okay the difference will be first of all
  174. 5:51in this particular video some of the
  175. 5:53concept and some of most of the
  176. 5:55explanation will be much much much more
  177. 5:57detailed Okay. And you'll understand
  178. 5:59this one very very clearly once I start
  179. 6:01this particular video. Second, this
  180. 6:04video we are going to use posgress,
  181. 6:06right? I already mentioned in the
  182. 6:07previous video and almost all videos
  183. 6:09that are available on YouTube, we'll see
  184. 6:10that most of the videos are built on
  185. 6:12MongoDB, right? Uh because it's easy to
  186. 6:15record and most of the uh student want
  187. 6:17to learn MongoDB also because M was very
  188. 6:20popular. I think it's still very popular
  189. 6:22but now nowadays everyone wants to learn
  190. 6:24posgress. So that is the main change in
  191. 6:26this particular video. So there will be
  192. 6:28one dedicated section where you are
  193. 6:30going to understand poseless also. So
  194. 6:31I'm going to add some video that I
  195. 6:33already created very recently. So it
  196. 6:36will be around 4 hours. So that I will
  197. 6:38be adding in middle so that before we
  198. 6:40are going to start the project. So you
  199. 6:42will see that there will be a postgress
  200. 6:43section. Third, there are some core
  201. 6:46modules we are going to understand much
  202. 6:47detail way. Okay. And this is very very
  203. 6:49important. So once I start you'll
  204. 6:51understand what I'm talking about.
  205. 6:53Fourth one the main project that we are
  206. 6:55going to build that will be much more
  207. 6:57detailed. Now in the previous part one
  208. 6:59video whatever I covered so it was like
  209. 7:01a normal CR. Now here also we are going
  210. 7:03to cover normal CRUD but there will be
  211. 7:05some more advanced topic we are going to
  212. 7:07understand. For example how you can work
  213. 7:08with radius how you can implement
  214. 7:10background job. Now the main question
  215. 7:13comes to this that if I already watch
  216. 7:15this video or if I already know NodeJS
  217. 7:18can I still watch this video? The answer
  218. 7:20is absolutely yes. Okay. So you can
  219. 7:22watch this video. This video is going to
  220. 7:24give you fantastic idea about NodeJS.
  221. 7:26How you can work, how you can create
  222. 7:28proper folder structure and it will give
  223. 7:30you complete overview. Okay. So you
  224. 7:32don't have to watch multiple videos.
  225. 7:33Second when I'm going to upload the part
  226. 7:35two of NodeJS which will be NodeJS
  227. 7:38microservices with Postgress and Kafka,
  228. 7:40you'll see that it will be much more
  229. 7:41easier for you to understand. So I hope
  230. 7:43that this will answer some of the query.
  231. 7:45Now with that let's get started. The
  232. 7:47first of all you can see that I have a
  233. 7:49blank folder. We are going to use VS
  234. 7:51code for this and we will take some
  235. 7:52things uh most of the things very very
  236. 7:54slowly but there are some prerequisite
  237. 7:56that you need to know. So first
  238. 7:57prerequisite will be you should know
  239. 8:00JavaScript ES6 that is mandatory. We are
  240. 8:03not going to cover JavaScript ES6 or
  241. 8:05very very basic concepts because I
  242. 8:07already uh I thought that okay you know
  243. 8:09this okay I will consider this you
  244. 8:11already know this concept and then you
  245. 8:13are watching this video. If you haven't
  246. 8:15know all of this there is no point to
  247. 8:16watch this video. So first you you have
  248. 8:18to watch or learn JavaScript ES6
  249. 8:20concept. Second TypeScript basics you
  250. 8:24need to know okay because this video
  251. 8:26actually will be built completely on
  252. 8:27TypeScript. So I already have a
  253. 8:28TypeScript video also. So with that now
  254. 8:31what we going to do right? So how this
  255. 8:33first section will be [snorts] covered.
  256. 8:35So this will be our first section. You
  257. 8:36can see that I created NodeJS basics and
  258. 8:39uh core modules we are going to cover.
  259. 8:41So Node basics and NodeJS code modules.
  260. 8:44So now first what you have to do right?
  261. 8:46So let's go here and just search for
  262. 8:48NodeJS and then you have to download
  263. 8:50NodeJS. Now here one thing you have to
  264. 8:52understand. So first of all before we we
  265. 8:55will understand that okay what actually
  266. 8:56you need to download. First you have to
  267. 8:58understand what actually NodeJS is.
  268. 9:00Okay. So let's do one thing I'm going to
  269. 9:02create one file here and I'm just going
  270. 9:04to give this one file name as concepts
  271. 9:07MD and this is how we are going to do.
  272. 9:09So first let's understand some of the
  273. 9:11very very key point and this is going to
  274. 9:13help you while giving interview also. So
  275. 9:15first of all, NodeJS is
  276. 9:18so I'm just going to give like this uh
  277. 9:20let's give it like this. So I'll do
  278. 9:22NodeJS is JavaScript or JS runtime.
  279. 9:28So what I mean by this? So first of all
  280. 9:29you have to understand what actually
  281. 9:30runtime this particular word means. So
  282. 9:33this runtime means that it's actually an
  283. 9:35environment where you are actually
  284. 9:37running some code. Okay. So runtime is
  285. 9:39the environment where you run some code.
  286. 9:42Now in the browser what happen right
  287. 9:44where JavaScript runs in JavaScript
  288. 9:47runs inside your Chrome Firefox suffer
  289. 9:50you all these browsers but in NodeJS
  290. 9:52your JavaScript runs where on your
  291. 9:54computer or maybe let's say you're
  292. 9:56running NodeJS on a server. So that
  293. 9:58means in NodeJS your JS runs outside
  294. 10:03of the browser. Now this is the first
  295. 10:05three thing you have to understand.
  296. 10:07Okay, this is very very very important.
  297. 10:09Okay. And this single line whatever I
  298. 10:11have added here. This is the main
  299. 10:12foundation of your backend development.
  300. 10:14Whatever you are going to do with
  301. 10:16JavaScript. So that means NodeJS is JS
  302. 10:19runtime. Runtime means we are running
  303. 10:21some code and this code we are not
  304. 10:24running inside the browser. We are
  305. 10:25running outside the browser. And this
  306. 10:27can be let's say you are running on your
  307. 10:29computer or on a server. I hope this is
  308. 10:32clear right. So first of all this is the
  309. 10:34first thing you have to understand what
  310. 10:35actually NodeJS uses. So NodeJS uses
  311. 10:37something called V8. Now what actually
  312. 10:40this V8 now this V8 is the same JS
  313. 10:43engine that Google Chrome also uses.
  314. 10:47Okay. So what actually this is going to
  315. 10:49do right? So this is going to take take
  316. 10:51your JavaScript code and it's going to
  317. 10:53run on your machine. Okay. Now one thing
  318. 10:56I wanted to mention that when you're
  319. 10:58learning NodeJS as a developer you don't
  320. 11:00have to go into depth of learning V8.
  321. 11:02There is no I'm not saying no point but
  322. 11:04you don't have to do that. You just have
  323. 11:06to understand this particular point that
  324. 11:08NodeJS is not a new language. Okay. It
  325. 11:10is actually JavaScript. We are running
  326. 11:12JavaScript only. But this NodeJS has
  327. 11:15some extra server side capabilities. So
  328. 11:17that NodeJS we can run inside a browser
  329. 11:20not inside a browser. Again NodeJS we
  330. 11:23are running outside the browser. So this
  331. 11:26is one very very important point I
  332. 11:27wanted to mention. So first of all what
  333. 11:29we are going to do right. So let's go
  334. 11:30here and first you have to understand
  335. 11:32one very important point and that is
  336. 11:34that which version we have to uh sorry
  337. 11:36we have to install and you'll see that
  338. 11:38here you are going to see something
  339. 11:40called LTS now what actually this LTS
  340. 11:43means this one this LTS now this LTS
  341. 11:46means long-term support now let's say if
  342. 11:48you're a student or let's say in college
  343. 11:50or maybe let's say I will consider like
  344. 11:52less than 3 years of experience right so
  345. 11:54you are completely like I'll say in
  346. 11:56beginner phase so what you should do
  347. 11:58right you should start with this LTS
  348. 12:00version. So what this means nothing very
  349. 12:03very simple. So LTS means this is a
  350. 12:06NodeJS release version and this is a
  351. 12:08stable version. Okay means what will
  352. 12:11happen? Let's say there are some bug
  353. 12:13reported right. So what will happen
  354. 12:14right? So they are going to fix that
  355. 12:16particular bug as soon as possible
  356. 12:18because lot of developers uses NodeJS
  357. 12:20worldwide. So this is uh we called as
  358. 12:23long-term support version. So this is as
  359. 12:26LTS. So what you have to do right
  360. 12:28whatever you are using okay let's say
  361. 12:30you're using Windows you using Linux or
  362. 12:32something else try to download the
  363. 12:35latest version of the NodeJS and latest
  364. 12:38version in the sense I'm talking about
  365. 12:39the LTS version of the NodeJS you don't
  366. 12:41have to do anything just have to install
  367. 12:43this and once this is done you'll see
  368. 12:45that NodeJS will be installed now here
  369. 12:47you will see something as npm so what
  370. 12:50actually this npm is now npm is actually
  371. 12:52the node package managers node package
  372. 12:55manager so Here I'm just going to do so
  373. 12:58your npm is node package manager. So
  374. 13:04what this is going to do? So this is
  375. 13:06going to help you to install various
  376. 13:08packages. It's going to help you to run
  377. 13:10your project scripts. So we are going to
  378. 13:12have some scripts you'll understand and
  379. 13:14also the most important part it will
  380. 13:16help you to manage your dependencies
  381. 13:18using package.json. So we have something
  382. 13:20called as package.json file. Okay. So
  383. 13:23this is why we need this npm. Now you'll
  384. 13:27see that once you do that okay so just
  385. 13:29download it and install it's very very
  386. 13:31simple once you'll do that you go to
  387. 13:33terminal and or maybe you can directly
  388. 13:35check from here. So what you have to do
  389. 13:37right you are going to do node version
  390. 13:39v. So this v basically we wanted to
  391. 13:42understand what is the version of node
  392. 13:43that we are running in this particular
  393. 13:45machine and if I enter you see that you
  394. 13:47are going to have this 24.14.0 you this
  395. 13:51is actually not the LTS version but it's
  396. 13:53fine if you want you can directly
  397. 13:55download this version from here also
  398. 13:57okay so this is the first thing I wanted
  399. 13:59to mention so now next thing will be
  400. 14:01what we going to do right here also
  401. 14:04let's do one thing okay I wanted to show
  402. 14:06you the npm version also so if you do
  403. 14:08npm version B you are also going to get
  404. 14:10the npm version here so now first what
  405. 14:13we have to do right so first you see
  406. 14:15that we don't have anything here right
  407. 14:16so we don't know what file we have to
  408. 14:17create what folder structure you should
  409. 14:19normally
  410. 14:20create while creating any products and
  411. 14:22levelable NodeJS application. But before
  412. 14:23that what we have to do right we have to
  413. 14:24understand some of the concept but
  414. 14:26before doing that first let's do one
  415. 14:28thing let's create our package dojson
  416. 14:30file. So what actually this package
  417. 14:32dojson file means because this is where
  418. 14:34we are going to start and learning each
  419. 14:36and everything. So here what I'm going
  420. 14:37to do right because I already mentioned
  421. 14:39that npm is your node package manager
  422. 14:41which help you to run your uh scripts.
  423. 14:44It's going to help you to create your
  424. 14:45NodeJS project. It is going to help you
  425. 14:48to install packages. Basically it is
  426. 14:50going to help you to manage your
  427. 14:51project. something like that. So here
  428. 14:53what I'm going to do right I am going to
  429. 14:54create a new node project. Okay, new
  430. 14:57node project. So how I'm going to do
  431. 14:59that? So I'm going to do npm in it. So
  432. 15:01init means we going to initialize
  433. 15:03something and I'm just going to give
  434. 15:04minus y. Okay, hyphen y. So here what
  435. 15:07I'm going to do right if I do enter so
  436. 15:10this is going to create a package.json
  437. 15:12on your root. Okay, root means whatever
  438. 15:15folder that you have created on the root
  439. 15:17you are going to get this particular
  440. 15:18file. Now this is one of the most
  441. 15:20important file you have to understand
  442. 15:21here. Okay, that what actually this file
  443. 15:23is doing. But before that what I'm going
  444. 15:25to do right I'm going to install some
  445. 15:28dependencies and uh and dev
  446. 15:30dependencies. So I'm pretty sure if you
  447. 15:32don't know the terms so I'm going to
  448. 15:33explain what actually that is. So what
  449. 15:35dependency I'm going to create and why
  450. 15:37we need those dependency because
  451. 15:39whenever we are going to create any
  452. 15:41project right so we need some packages
  453. 15:43or some out ofthebox things that we
  454. 15:45don't want to write. So those out
  455. 15:47ofthe-box things or features will be
  456. 15:49provided by those packages. Right. So
  457. 15:51what I'll do right for now because we
  458. 15:52are going to use Typescript. So I'm
  459. 15:54going to install TypeScript here. Okay.
  460. 15:56And here what I'm going to do right. So
  461. 15:58first of all or maybe let's do one
  462. 16:00thing. Maybe I'm doing or teaching in a
  463. 16:02different maybe wrong way. First let's
  464. 16:04understand what actually this package
  465. 16:05do.json is and then I'm going to create
  466. 16:07it. It will be easier. [snorts] So first
  467. 16:09you'll see that you are getting quite a
  468. 16:10few things. Okay. First you have to
  469. 16:12understand what actually each and every
  470. 16:14property is. So name name is your
  471. 16:16project identifier. Okay, this is your
  472. 16:18project identifier. Version is your
  473. 16:20current release version. So this is your
  474. 16:22project release version. Description
  475. 16:24whatever description you wanted to give.
  476. 16:26I think I don't have to explain here.
  477. 16:28Then you have the important part you
  478. 16:30have to understand here you have the
  479. 16:32type. So type here is common JS. Okay.
  480. 16:34So this is your module system hint. Then
  481. 16:37we have very very important part which
  482. 16:39is script. Now this script is going to
  483. 16:42help you to do something. Now I'm not
  484. 16:44going to explain what actually we are
  485. 16:46going to do because these when we are
  486. 16:47going to implement a project and run the
  487. 16:49script you'll understand. Now when I'm
  488. 16:51talking about it's going to help you to
  489. 16:52do something means here you can create
  490. 16:54various script. For example let's say I
  491. 16:56wanted to run some test. So I can keep a
  492. 16:58test script. I wanted to run my local
  493. 17:01server. I'm going to create a script
  494. 17:02which is going to help me to run the
  495. 17:04local server. I'm going to build
  496. 17:06something. I can add a build script.
  497. 17:09You're getting right. I wanted to start
  498. 17:11this particular project. So I can add a
  499. 17:13start script something like this. Okay.
  500. 17:16Now why actually we need this particular
  501. 17:18package dojson file. Now simply you have
  502. 17:19to understand that this particular file
  503. 17:23I usually call this one as identity
  504. 17:25card. So that mean this particular file
  505. 17:26is going to tell me that what actually
  506. 17:28this project is and what dependencies
  507. 17:31this project is holding or what
  508. 17:33dependencies this particular project is
  509. 17:35using. Now let's see if I currently do
  510. 17:38this. I have five developers that we are
  511. 17:41working in a team. Now the second
  512. 17:44developer recently joined your team and
  513. 17:46then you wanted to that particular
  514. 17:48developer want to understand okay what
  515. 17:49all things you are using in this
  516. 17:50particular project. If you share this
  517. 17:52particular file right package.json so
  518. 17:54this developer will understand okay
  519. 17:56these all script I have this is my main
  520. 17:59main entry file. Okay this is my root
  521. 18:01file that you are going to understand
  522. 18:03later and okay I have these
  523. 18:04dependencies. Okay, that mean these
  524. 18:06dependencies we are using and I also
  525. 18:08have these dev dependencies. Now here we
  526. 18:10are going to understand what actually
  527. 18:11dependencies versus dev dependencies but
  528. 18:14before that you need to understand that
  529. 18:15this project is nothing but holding the
  530. 18:17information that okay what actually this
  531. 18:20project is doing as simple as that. So
  532. 18:22now what I'm going to do right first I
  533. 18:24am going to install some of the uh
  534. 18:26dependencies that we need. So first I
  535. 18:28need what because we are using
  536. 18:29typescript we need typescript what
  537. 18:31actually typescript is going to do. So
  538. 18:33here I'm just going to do npm and I'll
  539. 18:35do install and I'll do hyphen d. You'll
  540. 18:37understand what actually you're doing.
  541. 18:38So you're installing a dev dependency.
  542. 18:40Here I'm just going to first install
  543. 18:41typescript. What typescript is going to
  544. 18:43do? This is going to compile your
  545. 18:44typescript to your javascript. Then we
  546. 18:47have something called tsx. Very very
  547. 18:49important. Why? When we're going to run
  548. 18:51typescript, what this particular package
  549. 18:53is going to do? This is going to run
  550. 18:54your typescript directly whenever you
  551. 18:57are going to do the development. Okay?
  552. 18:59You don't have to create any manual
  553. 19:00build setup for this. And for this we
  554. 19:03also need a types and we need the node
  555. 19:06types. So because we are using
  556. 19:08typescript so we need to do what we need
  557. 19:10to give type definition right and this
  558. 19:12is what we are going to use for that. So
  559. 19:14we need three packages typescript tsx
  560. 19:17and types/ node. So this is the first
  561. 19:20thing we are going to do. I will install
  562. 19:22this and we'll see that what will happen
  563. 19:24the moment we are going to install there
  564. 19:25will be a node modules will be created
  565. 19:27and these node modules is going to hold
  566. 19:29all this information just now we have
  567. 19:31installed. Okay. So this node modules is
  568. 19:33going to have all this. You see that we
  569. 19:34have typescript tsx types. Now you don't
  570. 19:37have to focus or you don't have to
  571. 19:38change anything inside of this. Okay. Do
  572. 19:40not do anything until unless you're
  573. 19:42creating your own common package. Let's
  574. 19:44say you're creating your own custom
  575. 19:45package and you wanted to check
  576. 19:47something. Right? Normally you should
  577. 19:49not change anything inside the node
  578. 19:51modules. Okay. So you don't have to do
  579. 19:52anything here. You'll see that based on
  580. 19:54whatever packages we are doing or
  581. 19:56installing it will create those folders
  582. 19:58or dependencies and the subd
  583. 20:00dependencies. So you can see that you
  584. 20:01have the subfolder also inside this. So
  585. 20:04this is the first thing that we have to
  586. 20:06do. Now what I will do right now let's
  587. 20:08say I wanted to add some particular
  588. 20:12file here. Okay. Okay. This is the uh
  589. 20:14script I wanted to add. Okay. Now before
  590. 20:16doing that also you have to understand
  591. 20:18one thing that currently we don't have
  592. 20:19any particular file file in the sense
  593. 20:21let's say whenever we are going to write
  594. 20:22some code we need to write code right.
  595. 20:24So in that case we need to have some
  596. 20:26entry file. That means whenever I'm
  597. 20:28going to run this particular project,
  598. 20:30TypeScript will understand okay this
  599. 20:32particular file code I need to see or I
  600. 20:35need to watch and based on that I need
  601. 20:36to run that. So that means we need to
  602. 20:39create some file. But before that also
  603. 20:41we need to understand one very important
  604. 20:42concept which is if you're a beginner
  605. 20:44this can be a very important interview
  606. 20:46question also that what actually
  607. 20:47dependencies and what is the dev
  608. 20:50dependencies extremely important
  609. 20:52interview question. I myself got this
  610. 20:54question so many times. So to uh explain
  611. 20:58in a simpler way dependencies will be
  612. 21:01dependencies are something is needed
  613. 21:04when the app is running. Dev
  614. 21:06dependencies is something needed only
  615. 21:10when we are developing or we are
  616. 21:12building. So let's put in this way. So
  617. 21:14I'll do dependencies
  618. 21:16or something needed when the app is
  619. 21:21running and dev dependencies will be
  620. 21:30something needed when or while
  621. 21:33developing
  622. 21:36or building. Okay. Now here you have to
  623. 21:41understand a couple of things. Okay.
  624. 21:42These are very important. So you have
  625. 21:44dependencies. Dependencies are something
  626. 21:45like runtime package. Extremely
  627. 21:48important concept. Okay. You have to
  628. 21:50understand all of these because if you
  629. 21:51really wanted to learn NodeJS and
  630. 21:53everything, you need to know in details
  631. 21:55so that you don't depend on AI tools all
  632. 21:58the time because as a developer you
  633. 21:59should know all these basic things and
  634. 22:01you are going to believe me or not. Some
  635. 22:04people also lost inter not lost some
  636. 22:07people also failed interview by not
  637. 22:09answering these basic questions also. So
  638. 22:11when we are talking about dependencies
  639. 22:12what actually dependencies are right so
  640. 22:14you have we call this one as runtime
  641. 22:16packages right so whenever you're going
  642. 22:19to write some code right you're going to
  643. 22:20import something okay or maybe let's say
  644. 22:23the your code whatever you're writing it
  645. 22:25imports and those particular packages we
  646. 22:27uses while serving request or we are
  647. 22:30doing something real work for example
  648. 22:32let's say there is a HTTP framework that
  649. 22:35your serverts file imports whenever your
  650. 22:38code will start now this will be your
  651. 22:41runtime packages means you are running
  652. 22:43some actual code development packages
  653. 22:46will be your dev dependencies means
  654. 22:47something you use to write compile or
  655. 22:50check your code okay not something that
  656. 22:54you need when you are going to run that
  657. 22:56particular code in production very very
  658. 22:58important okay so I'm going to repeat
  659. 23:00once again runtime packages will be
  660. 23:02something your code imports and uses
  661. 23:06while doing an actual work development
  662. 23:08related packages or dev dependencies
  663. 23:10will is something something you use to
  664. 23:12write compile or check code but not
  665. 23:15something you need while running that
  666. 23:17particular code in production. Okay. So
  667. 23:19that will be the
  668. 23:21uh difference between dependencies and
  669. 23:22the dev dependencies. Now you can see
  670. 23:24that clearly you will see these dev
  671. 23:26dependencies of this TypeScript compiler
  672. 23:28or dev runner or let's say we have a
  673. 23:30lint package this you need when you're
  674. 23:32going to do your development. Okay. So
  675. 23:33this will be the proper definition.
  676. 23:36Now what will be the dependencies then?
  677. 23:38So I'll take one example let's say
  678. 23:40express. Now express will be your
  679. 23:42dependencies. Now express is your actual
  680. 23:45running API server that you're going to
  681. 23:47import and you're going to use in your
  682. 23:49code. So that means that particular code
  683. 23:51when you're going to run inside in
  684. 23:52production send you need that express
  685. 23:55package right? So whenever you're going
  686. 23:56to run or build that code in production
  687. 23:58first you need to install that express
  688. 24:00package then only you can use that
  689. 24:02right? Let's say what will happen? Let's
  690. 24:04say you're missing some dependencies and
  691. 24:05you try to run. So your build will fail
  692. 24:08and your app can crash also you're
  693. 24:10getting. So that is the definition or
  694. 24:12difference between dependencies versus
  695. 24:14the dev dependencies. So I think this is
  696. 24:17all about this one. Now what else? Maybe
  697. 24:19let's understand some of the npm uh
  698. 24:22script also. Right? Let's do one thing.
  699. 24:24What are we going to do? Right? Maybe
  700. 24:25let's create one folder here. So I'm
  701. 24:28going to create a source folder. Okay.
  702. 24:29Source folder means inside this I'm
  703. 24:31going to write all my code and here I'm
  704. 24:33going to create index.ts file. So
  705. 24:35index.ts means this is where I'm going
  706. 24:37to write some code. Okay, we are not
  707. 24:39doing anything but we are going to do
  708. 24:41something. First you have to understand
  709. 24:42that how we are going to write script
  710. 24:46inside this package dojson and what
  711. 24:48actually this script means? You have to
  712. 24:51understand that npm scripts are nothing
  713. 24:53but that you are doing something. I
  714. 24:54already mentioned that one. Let's say
  715. 24:56this particular file I wanted to run. So
  716. 25:00now for this task what I'll do right I'm
  717. 25:03going to add one particular script and
  718. 25:05whenever I'm going to run that script as
  719. 25:07a shortcut your tsx or your typescript
  720. 25:10will understand I need to run this
  721. 25:11particular file. So let's see how this
  722. 25:13will work. For example let's say
  723. 25:16first of all I'm just going to do like
  724. 25:18this here. I'm just going to add a de
  725. 25:20script. Let's say whenever I'm going
  726. 25:22doing development I need to do what? I
  727. 25:24need to test my changes right that I'm
  728. 25:26doing something if something is
  729. 25:27breaking. So here what I'll do remember
  730. 25:29I already mentioned we installed this
  731. 25:31tsx package right. So this tsx is going
  732. 25:34to do what? It's going to help us to see
  733. 25:36whenever I'm going to run this
  734. 25:38particular script that whether this
  735. 25:40particular file code is running properly
  736. 25:42or not. So how I'm going to do that? So
  737. 25:44I need to watch this file right? I need
  738. 25:46to run this file. So use something
  739. 25:48called watch. So I'll do tsx watch means
  740. 25:50ts I'm telling tsx to watch which file
  741. 25:53source/index.ts
  742. 25:55file. So that means I'm simply telling
  743. 25:57that okay whenever I'm going to do
  744. 25:59right. So whenever I'm going to run this
  745. 26:01particular npm rundev so if I just do
  746. 26:04here nothing will happen. Okay. So maybe
  747. 26:06let's do one thing. We can also console
  748. 26:09log something here. So you'll
  749. 26:11understand. So I'll do right. I'll just
  750. 26:13do a log and I'll do hello NodeJS.
  751. 26:18And now if I just do npm rundev. So
  752. 26:21whenever I'm doing npm. So I'm telling
  753. 26:22npm to run what? Dev script. So npm will
  754. 26:25go here. It will check. Okay dev means I
  755. 26:27need to run this particular file. So
  756. 26:30sour/index.ts. If I run here, I'll see
  757. 26:32that you're getting node hello nodejs.
  758. 26:35Right? So that means tsx is now watching
  759. 26:37this particular file. And here if I
  760. 26:40let's see if I do save and you see that
  761. 26:42now you're getting hello node js2. Now
  762. 26:44this is the important of this particular
  763. 26:46term. watch means every time you're
  764. 26:48going to change something change means
  765. 26:50you are changing the code right let's
  766. 26:51say I change from hello NodeJS to my
  767. 26:54name right so if I save this you'll see
  768. 26:56that this DSX this particular package is
  769. 26:58watching your code real time and
  770. 27:00whenever you're making any change it is
  771. 27:02updating this one so this is what script
  772. 27:04is going to do now for example in this
  773. 27:06particular script what you're doing we
  774. 27:08are doing some development work now
  775. 27:09let's say you're working on some
  776. 27:11database and in the database you need to
  777. 27:13do some migration you can add a
  778. 27:14migration script and you can do whatever
  779. 27:16what it needs to do. Let's say you need
  780. 27:17to do something test, right? You are
  781. 27:20going to write some unit test. So you
  782. 27:22can do also like this. Okay, unit test I
  783. 27:24need to write or maybe unit something
  784. 27:26like this and you can write a command
  785. 27:27here. Let's say you are doing some ite
  786. 27:30test. So you can add a script and you
  787. 27:32can do whatever command you need to run.
  788. 27:33So whenever now next time you will do
  789. 27:35npm run ite so it's going to check this
  790. 27:38particular script and it's going to run
  791. 27:40that. So this is about the npm script.
  792. 27:42Why this is very important? because you
  793. 27:43need to understand all of these before
  794. 27:45we even start writing any code. Okay. So
  795. 27:48now I think we have covered almost all
  796. 27:49the things and we also covered what
  797. 27:52actually package package.json is. Node
  798. 27:55modules will have all your dependencies
  799. 27:57and the subd dependencies. Okay, you
  800. 28:00don't have to change anything here. Now
  801. 28:02let's understand one very important
  802. 28:04concept. Now I I'm not sure if I should
  803. 28:07explain this one now but still I'll give
  804. 28:10you some prop basic idea. Okay, we are
  805. 28:12not covering much and that is that how
  806. 28:14should you create a proper folder
  807. 28:16structure in a nodejs although we
  808. 28:18haven't covered any uh express or
  809. 28:20anything. Okay. So what you should do
  810. 28:22right I'm going to give very basic
  811. 28:24overview. So don't take this one as a
  812. 28:26complete idea. Okay. Because we are not
  813. 28:28going to cover like middleware routes
  814. 28:30and everything. What you should do right
  815. 28:33how I'm going to explain or when I
  816. 28:35actually run this nodes you have to
  817. 28:37understand that whenever you're going to
  818. 28:38create a new folder or you're going to
  819. 28:40create any file you need to understand
  820. 28:42whether that particular file actually
  821. 28:45holding any responsibility or not. And
  822. 28:47this is the best way to put anything.
  823. 28:50what I'm talking about holding any
  824. 28:51responsibility because let's say
  825. 28:53currently I'm creating a util folder.
  826. 28:56Okay. Now in this utils folder you can't
  827. 28:59put any routing related logic. Routing
  828. 29:02means okay whenever my UI is going to do
  829. 29:05a API call and then whenever you will do
  830. 29:07API call it will do what? It's going to
  831. 29:09check that particular route or that
  832. 29:10particular API route is present or not.
  833. 29:12So that means if you're putting your
  834. 29:14routing logic inside utils folder that
  835. 29:16doesn't make any sense right that is
  836. 29:18also bad. Next let's say you are mixing
  837. 29:23your service logic
  838. 29:26and your DB logic. DB logic means I'll
  839. 29:29do DB repository pattern. DP repository
  840. 29:32pattern means where you're going in
  841. 29:33which file you are going to actually do
  842. 29:34some DB queries at the same time there
  843. 29:36also you are doing service logic. Now
  844. 29:38see you can do that but this is not a
  845. 29:40proper folder structure. Okay. Similarly
  846. 29:43let's say you are creating a routing and
  847. 29:45in this routing you are adding service
  848. 29:47logic. This is also bad. Okay. So this
  849. 29:49is also bad practice. So what I mean by
  850. 29:52this, I'm not going to cover any terms
  851. 29:53for now. But you have to understand
  852. 29:55remember this particular line that
  853. 29:57whenever you're creating any folder or
  854. 29:59any file, you need to understand whether
  855. 30:01that file holding any responsibility or
  856. 30:04not. As simple as that. Okay. Second,
  857. 30:07there is one more rule. You should
  858. 30:09always have one always have one entry
  859. 30:13file. Entry file or one root file. Now
  860. 30:16in this case our root file is
  861. 30:18source/index.ts. So whenever I'll go
  862. 30:20here right package.json whenever I'm
  863. 30:23going to run this particular command
  864. 30:24these files should hold each and every
  865. 30:27information of your entire code. What
  866. 30:29else will happen? Let's say I'm just
  867. 30:30going to give that you have two routes
  868. 30:33you are using something like that or two
  869. 30:35utils file helper function you are using
  870. 30:37that will be best way to explain. So
  871. 30:39what I'm talking about now let's say you
  872. 30:42have two more one utils folder and
  873. 30:44inside that you have two files one file
  874. 30:46you are using here in this particular
  875. 30:48root file and the second file you are
  876. 30:50not using okay now in your package.json
  877. 30:52JSON when you are going to run this
  878. 30:54particular command right ntpm rundev
  879. 30:56you'll see that the first file or first
  880. 30:59util file whatever you have imported
  881. 31:01that output you'll be able to see but
  882. 31:03because you are not importing the second
  883. 31:05file so how [snorts] we are going to
  884. 31:06check that you're getting what I'm
  885. 31:08talking about so my point is your root
  886. 31:10file this particular root file should
  887. 31:13contain
  888. 31:15these files should contain the
  889. 31:17information of
  890. 31:20all or information of your entire source
  891. 31:25code at the end what you're going to do
  892. 31:28right you're going to import all of this
  893. 31:29but you need to import let's say you're
  894. 31:31not importing something it's not going
  895. 31:33to work you're getting what I'm talking
  896. 31:35about so something like this so now I
  897. 31:38think this is all about now again this
  898. 31:40is not the folder structure completely
  899. 31:41we have done because until unless we
  900. 31:43start express or we'll do some practical
  901. 31:46project it will be very difficult but
  902. 31:47till this part this is going to give you
  903. 31:49almost all the ideas that what actually
  904. 31:51node J is why we need to install the LTS
  905. 31:54version of NodeJS. We should install
  906. 31:56mean we no need to you can install the
  907. 31:58latest version also but it's always
  908. 32:00better to install the LTS version. Third
  909. 32:02what actually npm is what is
  910. 32:04package.json right and some of the very
  911. 32:06very important concept right uh
  912. 32:08dependencies versus dev dependencies
  913. 32:11what actually npm scripts are and all
  914. 32:13these things. So with this now what will
  915. 32:15be the next step right next step will be
  916. 32:17see we are not going to create any
  917. 32:18project or anything because we need to
  918. 32:20cover the npm uh sorry nodejs core
  919. 32:22modules. Now first of all you need to
  920. 32:24understand what all core modules we are
  921. 32:26going to cover here. So what I've
  922. 32:28decided right I'm going to cover all
  923. 32:29these. So I'm just going to write here
  924. 32:31so that you'll understand. So first of
  925. 32:32all we will start with process object
  926. 32:36process object of nodejs. Second we are
  927. 32:38going to cover the path not path maybe
  928. 32:40let's start with
  929. 32:42env variables I think I can cover
  930. 32:44enviables here also
  931. 32:48or maybe let's do one thing no let's
  932. 32:50cover process object because env
  933. 32:52variables we'll do practically when we
  934. 32:54are going to do project after process
  935. 32:56object let's cover the path module very
  936. 32:58very important then we're going to cover
  937. 33:00the file system or file system module
  938. 33:03then I'll cover the event loop basics
  939. 33:06because these things you need to know
  940. 33:08else like it will be like incomplete.
  941. 33:10Okay. So event loop we are going to
  942. 33:12understand that then we are going to
  943. 33:14understand some call back extremely
  944. 33:16important means like two important
  945. 33:18concept. Okay. Why this is too
  946. 33:20important? Because this is extremely
  947. 33:22important for beginner level of
  948. 33:23interview questions right. So when
  949. 33:25you're giving interview these interview
  950. 33:27questions are very important. What is
  951. 33:28the call back? Why we need promise? Why
  952. 33:31we need asynav? What is the difference?
  953. 33:34Okay, how this is relevant because see
  954. 33:36callbacks are extremely important
  955. 33:38concept. Okay, so whenever you're going
  956. 33:39to write any function let's see that
  957. 33:41you're going to install some third party
  958. 33:42packages there will be too much using of
  959. 33:45call back promise and async coverage. So
  960. 33:46these three are very important
  961. 33:48non-negotiable you have to learn all of
  962. 33:49these we are also going to learn some
  963. 33:51event emitter concept then buffers we
  964. 33:55also buffers what else we also
  965. 33:57understand some of the uh okay crypto
  966. 33:59module we have to understand see that
  967. 34:01that's the reason I have actually taken
  968. 34:04all these important important modules
  969. 34:05also everything we can't cover then very
  970. 34:08important errors how we can do error
  971. 34:10handling properly without using any
  972. 34:12packages or anything proper error
  973. 34:14handling and also you are going to cover
  974. 34:16some of the basics stream concept. Now
  975. 34:19here you'll see that we are not going to
  976. 34:20cover some of the thing for example here
  977. 34:23we are going to not cover worker threads
  978. 34:26and we are not going to cover here child
  979. 34:29processes. Now these two concept plus
  980. 34:33uh some very advanced
  981. 34:36advanced level of concepts
  982. 34:40these will move to NodeJS
  983. 34:45version sorry NodeJS part three now this
  984. 34:49will be pure NodeJS advanced module I'm
  985. 34:51going to create but not this in this one
  986. 34:54because this doesn't make sense because
  987. 34:55this will be only too much okay so all
  988. 34:58these things we're going to cover in the
  989. 34:59next But you can't skip any of these
  990. 35:01because everything whatever you're going
  991. 35:03to learn we are going to actually apply
  992. 35:04in project directly. So first you
  993. 35:08complete all of this and then you move
  994. 35:09to HTTP module. Okay. This is how this
  995. 35:12course will be built. So first we are
  996. 35:14going to cover the core modules and then
  997. 35:15we'll understand node HTTP module that
  998. 35:17will be a completely different section
  999. 35:19and then we'll start with our project.
  1000. 35:21So with this now let's move on to the
  1001. 35:23next one. Okay everyone. So now what we
  1002. 35:25going to do right? So we are going to
  1003. 35:26understand all the core concepts and the
  1004. 35:28core modules uh that uh you need to know
  1005. 35:31before you're going to start the HTTP
  1006. 35:32module. Okay. And each and every file
  1007. 35:34that we are going to cover will be very
  1008. 35:36important. Now one thing I wanted to
  1009. 35:38mention I think I already explained that
  1010. 35:40one that there are two or three core
  1011. 35:42modules that we are not going to cover.
  1012. 35:44Okay. And the first one will be uh
  1013. 35:46worker threads and child process. Now
  1014. 35:48child process worker threads will be
  1015. 35:50little complicated concept and once I'll
  1016. 35:53upload the node microservices I think I
  1017. 35:55haven't created any video on node
  1018. 35:57internals and some very advanced topics
  1019. 35:59so there will be uh there is a
  1020. 36:01possibility that if these two videos uh
  1021. 36:04get good response so I'm going to upload
  1022. 36:06a part three okay and that one will not
  1023. 36:08be that much practical so in that one we
  1024. 36:10are going to only cover node internal
  1025. 36:12systems and the advanced core modules
  1026. 36:14okay so first what we need to do right
  1027. 36:17see uh I think we We have already
  1028. 36:18installed all of these correct but you
  1029. 36:20have to understand that when we are
  1030. 36:21using Typescript right so Typescript
  1031. 36:23needs to know that how it's going to
  1032. 36:25compile the code to JavaScript right so
  1033. 36:27we need a TypeScript configuration
  1034. 36:29related file and that file we call as
  1035. 36:31tsconffigjson
  1036. 36:33so what we are going to do right so
  1037. 36:35first of all we have to create a
  1038. 36:36tsconfig dot JSON file and what you need
  1039. 36:39to do right go to Google and just search
  1040. 36:41for I think you can use any AI tools
  1041. 36:43also tsconfig file NodeJS okay so what
  1042. 36:46this particular file So, TypeScript is
  1043. 36:48going to read this particular block
  1044. 36:50whatever you are seeing here and then
  1045. 36:52it's going to decide that how it's going
  1046. 36:54to compile the project. Okay, this is
  1047. 36:56very simple. So, for now what I'm going
  1048. 36:58to do right, you can copy this exact and
  1049. 37:00then we'll paste it here. So, first you
  1050. 37:01need to understand some of the important
  1051. 37:03parts. Okay, you uh it will take some
  1052. 37:05time. Let's say if you don't have any
  1053. 37:07idea. So, once you do some project
  1054. 37:09you'll see that each and everything will
  1055. 37:10be uh you'll be familiar that what
  1056. 37:12actually each and every property is
  1057. 37:14doing. So, first one is target ES22. So
  1058. 37:17this target uh what this particular
  1059. 37:20thing controls so this is going to
  1060. 37:21controls which JavaScript uh version
  1061. 37:24TypeScript is going to compile once it
  1062. 37:26will start compiling your code. Okay.
  1063. 37:28And in this one we're using uh 20 uh
  1064. 37:312022 version because this is actually
  1065. 37:34modern version. So you can use this one.
  1066. 37:35So you can see that they mentioned that
  1067. 37:37this is the compile JavaScript version.
  1068. 37:39Okay. Then we have module note next. So
  1069. 37:42this is also important. So this is going
  1070. 37:43to tell Typescript that which model
  1071. 37:45system we are going to use in the
  1072. 37:47compiled files. Okay. Now in this case
  1073. 37:50what this node next means? So here this
  1074. 37:53is going to match how NodeJS handles
  1075. 37:56your ES module and your package.json.
  1076. 37:59Okay. Now uh let's move on to the next
  1077. 38:02one which is module resolution. This is
  1078. 38:04almost same. Then you have one very
  1079. 38:06important which is root directory. In
  1080. 38:08this one we are going to use the source.
  1081. 38:10So we can also do like this right. So
  1082. 38:11this will be your source. So this is
  1083. 38:13where Typescript is going to check your
  1084. 38:14source code and based on that it's going
  1085. 38:16to compile. Okay. So this will be our
  1086. 38:18source uh folder. We'll keep it like
  1087. 38:21this. This is our output directory. So
  1088. 38:23this is where TypeScript is going to
  1089. 38:25compile all your all your JavaScript and
  1090. 38:28it's going to keep in this particular
  1091. 38:29folder dish folder. Then we have some
  1092. 38:31TypeScript related functionality. If you
  1093. 38:33already learned my TypeScript video, I
  1094. 38:35think I have a two videos. I already
  1095. 38:37covered each and every point in details.
  1096. 38:39strict uh strict true no implicit any
  1097. 38:42and strict null checks all of these okay
  1098. 38:44and now some of the things you have to
  1099. 38:46understand let's go here okay so you'll
  1100. 38:48see that ES module uh inter op so what
  1101. 38:51this is going to do right so this is
  1102. 38:52going to this going to make default
  1103. 38:54imports from your common JS package and
  1104. 38:57it will be easier to use okay this is
  1105. 38:59when this is helpful so this is going to
  1106. 39:01be very helpful when let's say you have
  1107. 39:03a node package and that particular node
  1108. 39:05package actually written in older module
  1109. 39:08style okay So that is the reason they
  1110. 39:10mention that this is going to support
  1111. 39:12common JS and ES module inter. Uh then
  1112. 39:15what else? Okay, this is important. So
  1113. 39:17include is nothing but this is going to
  1114. 39:20this is where you're going to have your
  1115. 39:21code and exclude means we're not going
  1116. 39:23to include node modules and the bish
  1117. 39:25folder where we are going to have our
  1118. 39:28compiled files. Right? So this is where
  1119. 39:29we are going to have your build files
  1120. 39:31and this is your external packages. So
  1121. 39:32we don't need the node modules. Now why
  1122. 39:34this is very important because you have
  1123. 39:36to understand why we need this
  1124. 39:38particular file and you'll see that
  1125. 39:39whatever project you are going to create
  1126. 39:41if that particular project is using
  1127. 39:43TypeScript you are going to see this
  1128. 39:44TypeScript configuration file. So this
  1129. 39:46is one particular uh section we I wanted
  1130. 39:49to cover. Now with this so what are we
  1131. 39:51going to do right? So in the next part
  1132. 39:52we are going to start learning all the
  1133. 39:55core modules and each and every core
  1134. 39:56modules will be very very important. So
  1135. 39:58let's start with that in the next part.
  1136. 40:01Okay. So let's start with the first file
  1137. 40:03which will be so I'm just going to
  1138. 40:04create here 01 and let's do process
  1139. 40:08object. TS.
  1140. 40:11So let's close all of these. So first of
  1141. 40:14all what actually process module is
  1142. 40:16right. So see process is nothing but
  1143. 40:18process usually represent your current
  1144. 40:21nodej or current running nodejs program.
  1145. 40:23Okay what actually we are going to
  1146. 40:26achieve from this particular module or
  1147. 40:28what actually this is going to give you
  1148. 40:30access. So this is going to give you
  1149. 40:32some of the very important concept some
  1150. 40:33of the runtime information you need. For
  1151. 40:35example let's say you need some env
  1152. 40:37variables that we are going to use. What
  1153. 40:39I mean by env variables? So let's say in
  1154. 40:41your project you are going to use some
  1155. 40:42password or maybe let's say you're going
  1156. 40:44to use some secrets. So those secrets
  1157. 40:46you're not going to hardcode in your
  1158. 40:47code right. So what you need to do you
  1159. 40:49need to keep those particular
  1160. 40:52sensitive information in a certain file
  1161. 40:53and we usually call that one as a env
  1162. 40:56file right. So this is where you are
  1163. 40:57going to keep all your env variables. So
  1164. 40:59this process object is going to give you
  1165. 41:00the access or this is going to give you
  1166. 41:02certain functionality so that you will
  1167. 41:04be able to read those variables and you
  1168. 41:06can use in your code. Okay. This is also
  1169. 41:08going to give you some of your command
  1170. 41:10line arguments.
  1171. 41:12Okay. Also, this is going to give you
  1172. 41:14some of the uh methods that from where
  1173. 41:17you will be able to exit your code.
  1174. 41:18Let's say something goes wrong. So,
  1175. 41:20you'll be able to exit. Also, uh I I'm
  1176. 41:22not sure if some other t uh teachers
  1177. 41:25usually cover this. This is also going
  1178. 41:27to give you some process life cycle
  1179. 41:30events. Okay. This is also very very
  1180. 41:32important. Okay. So now how this is this
  1181. 41:36is going to be very much important in a
  1182. 41:37real backend project. We see at the end
  1183. 41:40you have to understand whatever module
  1184. 41:41you are going to cover whether that is
  1185. 41:43actually useful in a real world or
  1186. 41:45NodeJS project or not. Okay. Uh why?
  1187. 41:48Because why it's very important because
  1188. 41:50there is no point of learning all of
  1189. 41:52this if you're not able to use this one
  1190. 41:53in a real project. So in a backend
  1191. 41:55project this is let's say you're going
  1192. 41:57to read some your backend code you read
  1193. 42:00backend code from your env file. So this
  1194. 42:02is where you need this process or let's
  1195. 42:04say maybe let's say you need to read
  1196. 42:06your secrets
  1197. 42:08correct maybe let's say secrets can be
  1198. 42:10your DB URL means your database URL or
  1199. 42:13your API keys or there can be password
  1200. 42:16or there can be some Google O secret so
  1201. 42:19it can be anything right so you need to
  1202. 42:20read all of these so this will be
  1203. 42:22helpful you need to let's say you need
  1204. 42:23to read some CLI arguments
  1205. 42:27in scripts this is where you also can
  1206. 42:30use this right So how you are going to
  1207. 42:33use this one? So we have something
  1208. 42:34called process.inv.
  1209. 42:36But from where this process actually we
  1210. 42:39are going to get. So you need to import
  1211. 42:41something right. So we are going to
  1212. 42:42import process from. So we need to
  1213. 42:45import from node. But this will be from
  1214. 42:48our process model. And also for this we
  1215. 42:50need to add the types I think. Okay. So
  1216. 42:53we need to go to our types sorry
  1217. 42:55tsconfig.json
  1218. 42:57and let's add here. So we going to add a
  1219. 43:00types and types will be node. So save
  1220. 43:04this and here you can see that now you
  1221. 43:07are going to get this. So this process
  1222. 43:09is going to give you something from
  1223. 43:11where you are going to so you'll see
  1224. 43:13that if I just do process env you'll see
  1225. 43:15that this is where we are going to read
  1226. 43:16our env uh variables. But remember when
  1227. 43:20we are going to start our actual node
  1228. 43:22capstone project you'll see that I'm
  1229. 43:23going to explain one particular package
  1230. 43:25that is called env. So I just wanted to
  1231. 43:27mention this one so that you'll
  1232. 43:29remember. So we have something called NV
  1233. 43:31and this package is going to help us
  1234. 43:32directly to read these enviables in a
  1235. 43:35NodeJS project. Okay, this is very much
  1236. 43:37helpful. But for now I think this is not
  1237. 43:39needed. So first you will understand one
  1238. 43:40thing is that this process environ
  1239. 43:44variables. So this process env is going
  1240. 43:46to contains all your env variables.
  1241. 43:49Right? So now let's say what I need to
  1242. 43:52do right maybe what I'm going to do
  1243. 43:54right here. Let's do something like
  1244. 43:55this. So I'm just going to take a con.
  1245. 43:57Okay. And I'm just going to do a node.
  1246. 43:59And maybe what I can do, let's do this
  1247. 44:01one as node env. And now what I'm going
  1248. 44:04to do, I'll do process env.
  1249. 44:07So I'll do process env. And let's say I
  1250. 44:11wanted to take this from my node env or
  1251. 44:13else I'm just going to take this one by
  1252. 44:15default as development. Okay. Now one
  1253. 44:19thing you have to understand here we are
  1254. 44:20not checking that how you are going to
  1255. 44:23use env and everything that we are going
  1256. 44:24to see actually in the practical project
  1257. 44:26we are understanding the core module and
  1258. 44:29what are the bad practices and what are
  1259. 44:31the good practices that you should
  1260. 44:32follow for example what will be a bad
  1261. 44:34bad practice here so let's say you're
  1262. 44:36doing like this so you'll take a con
  1263. 44:38port and you're doing 3,00 you should
  1264. 44:40never do this one okay you should never
  1265. 44:42hardcode enviables
  1266. 44:44anywhere in your source code you can
  1267. 44:46take a default value right something
  1268. 44:48like this you can take take a default
  1269. 44:50value but you should always read this
  1270. 44:51one from a certain file okay so now what
  1271. 44:55we are going to do right also very very
  1272. 44:57important I forgot this one these
  1273. 44:59process enviables uh sorry values that
  1274. 45:02whatever you'll have these will be are
  1275. 45:04always either string or undefined so I'm
  1276. 45:07just going to repeat this one so this
  1277. 45:08process
  1278. 45:10envi
  1279. 45:16string
  1280. 45:17or undefined. Now why I'm actually
  1281. 45:19mentioning this particular point?
  1282. 45:21Consider you have this right? So you're
  1283. 45:23doing port 4,000 something like this.
  1284. 45:25You have this particular value. When you
  1285. 45:28are going to actually use this one using
  1286. 45:30process envile
  1287. 45:35what you should do right let's say this
  1288. 45:36is a number you should always do like
  1289. 45:38this. So you're going to do port and
  1290. 45:40then you are going to actually convert
  1291. 45:41this one to a number. Okay. So you're
  1292. 45:43going to do like this. So process enven
  1293. 45:48code or else this will be 3,000. Okay.
  1294. 45:51So you should always convert this one to
  1295. 45:53that particular data type that you need.
  1296. 45:55Okay. So this is another thing I wanted
  1297. 45:56to mention. Now let's we understand that
  1298. 45:59how we are going to read this although
  1299. 46:00we haven't added any file or those that
  1300. 46:03you are going to discuss later. So for
  1301. 46:05getting noise I think it's coming from
  1302. 46:06outside. Okay. Uh like people will be
  1303. 46:10people will go crazy sometime with this
  1304. 46:12bikes and everything. So you'll get too
  1305. 46:14much message. Okay. So this one thing we
  1306. 46:17understood that okay this is where
  1307. 46:18you're going to read the enviable.
  1308. 46:20Second thing we understood that you
  1309. 46:22should always you should means not
  1310. 46:25should but use I sometimes you can take
  1311. 46:27some values by default. For example
  1312. 46:30let's say if you don't have this value
  1313. 46:31so it will take always from this uh
  1314. 46:34default value. If I hover here you'll
  1315. 46:35see this is what I mentioned that either
  1316. 46:37it will be string or this will be
  1317. 46:38undefined. So if you have something
  1318. 46:40where you need to use that particular
  1319. 46:42value as a number, so you should always
  1320. 46:44convert this one to a number. So these
  1321. 46:46all the practices that you should
  1322. 46:48follow. All right. So now moving to the
  1323. 46:49next one. Let's understand another
  1324. 46:51concept. So I'm just going to comment it
  1325. 46:53here. So let's do process.org. Okay. So
  1326. 46:56this is actually arg v. So but inside
  1327. 46:58I'm just going to explain this one as
  1328. 47:01process.org. So let's go here. So you'll
  1329. 47:04see that this is the built-in property.
  1330. 47:05Now here why I actually open this one
  1331. 47:07you need to understand one thing that
  1332. 47:09[snorts] this is a built-in property
  1333. 47:11that returns an array containing the
  1334. 47:14command line arguments. The important
  1335. 47:15part is that the actual custom argument
  1336. 47:18that you're going to pass to your script
  1337. 47:20that will start from index two. Why?
  1338. 47:23Because these usually look like this. So
  1339. 47:26first two elements okay you see that the
  1340. 47:28first two elements in the array are
  1341. 47:29always reserved by the node js. So the
  1342. 47:32first one process.org
  1343. 47:34zero will be the absolute file path of
  1344. 47:37the node just executable running the
  1345. 47:38script. The first index will be absolute
  1346. 47:42file path of the JavaScript file that we
  1347. 47:44are going to execute and the third one
  1348. 47:46will be any custom input that we are
  1349. 47:48giving. So consider like this. So you'll
  1350. 47:50see that we have this particular file
  1351. 47:52right now how this process.org V
  1352. 47:54actually looks like. So you'll consider
  1353. 47:56like this. You have this array and here
  1354. 47:58you're going to have the first one. So
  1355. 48:00first one will be the path to the node.
  1356. 48:02So this will be the node path. Second
  1357. 48:05one will be path to your file. So to
  1358. 48:08your file means in this case the path
  1359. 48:10will be this path. So let's say if I do
  1360. 48:12a relative path and then if I just
  1361. 48:14replace it here. So this will be the
  1362. 48:16path that node is going to execute. And
  1363. 48:18the third one will be the custom
  1364. 48:20argument that you are going to give. So
  1365. 48:21let's I'm just going to run a start
  1366. 48:23command. So this will start from here.
  1367. 48:25So that is the reason you'll see that if
  1368. 48:27I'm not sure if seen some nodes as code
  1369. 48:30you'll see that whenever there is a
  1370. 48:31process. Mark V you'll see that it will
  1371. 48:34always start from index number two.
  1372. 48:37Okay, index number two means the third
  1373. 48:38place. So first two always will be
  1374. 48:40reserved by node. Okay, so this is the
  1375. 48:42reason this line is very important and
  1376. 48:44also this is a very important interview
  1377. 48:46question also. So let's see this one
  1378. 48:48with example. So I'm just going to
  1379. 48:49comment it from here also. Let's remove
  1380. 48:51this. What I'm going to do right, I'm
  1381. 48:54going to take a command. Okay, now
  1382. 48:56you're going to understand some of the
  1383. 48:57concept. Normally in other uh projects
  1384. 48:59or other videos you'll see that people
  1385. 49:01will not cover all of these. So here I'm
  1386. 49:03just going to take the uh this process
  1387. 49:06with index 2 and then I'm just going to
  1388. 49:08take by default this will be our start
  1389. 49:10command. Now what you can do right you
  1390. 49:13can also run different flags very very
  1391. 49:15important concept. So let's say I wanted
  1392. 49:17to I wanted to take a fail crash and
  1393. 49:19also uh sorry fail command and also I
  1394. 49:21wanted to take a crash flag not command
  1395. 49:24my bad. So one I'm just going to take a
  1396. 49:26fill
  1397. 49:29uh flag and also I'm just going to take
  1398. 49:31a grass flag. So how we are going to do
  1399. 49:34this? So I'm simp simply going to do con
  1400. 49:37sud fail and this will be process dot
  1401. 49:40arg and this arg includes fail. Okay so
  1402. 49:45this is the first thing and we are going
  1403. 49:47to do like this and then I'm just going
  1404. 49:49to copy this and let's take one for
  1405. 49:51crash. So this will be my crash. So here
  1406. 49:54I'm just going to check if in this
  1407. 49:56command that we are going to run whether
  1408. 49:57we have this crash flag or not. Okay. So
  1409. 50:00now what we're going to do right we
  1410. 50:02going to run this manually and we'll see
  1411. 50:03that what uh the output will be. But
  1412. 50:06before that you also need to understand
  1413. 50:08another very very important uh uh event
  1414. 50:10and that will be process on. Okay. So
  1415. 50:13this is a method and here you can use
  1416. 50:15this exit method. What this exit method
  1417. 50:18is going to do. So this is not method
  1418. 50:20this will be event. Okay. So this exit
  1419. 50:22event is only helpful or only useful for
  1420. 50:26the final synchronous code. Okay, you
  1421. 50:29don't have to start you do not do not
  1422. 50:31start as sync here. Okay, now why you
  1423. 50:34might ask me? Because at this point when
  1424. 50:36you're going to run this process on
  1425. 50:39right node is already setting down. So
  1426. 50:41node is already setting down. So you
  1427. 50:43don't have to do any async work here.
  1428. 50:46Okay. So when you're going to use this,
  1429. 50:47when you're going to use this one, the
  1430. 50:49best use cases will be let's say you're
  1431. 50:51going to use some final log or maybe
  1432. 50:53let's say you are going to do some final
  1433. 50:55cleanup. This is extremely helpful. So
  1434. 50:57what you can do right so we have process
  1435. 50:59do onexit and it's going to take a call
  1436. 51:00back and here I'm just going to give a
  1437. 51:02code and let's say I'm just going to do
  1438. 51:04a log here that let's do a log and
  1439. 51:07[clears throat] let's take so I'll just
  1440. 51:08do process finished
  1441. 51:12uh with exit code and then let's pass
  1442. 51:14the code here. Okay. So now let's save
  1443. 51:17this. So now what you're going to do
  1444. 51:18right and also uh maybe let's take one
  1445. 51:22more uh function here. So let's do
  1446. 51:25function and I'm simply going to do run
  1447. 51:27app
  1448. 51:30and we'll take void.
  1449. 51:33And here what I'll do right I'm simply
  1450. 51:34going to do log. Let's log this uh
  1451. 51:38command also. I'm just going to uh what
  1452. 51:41we can do right? I think let's skip the
  1453. 51:44uh this command only. Okay, save this
  1454. 51:47and also now what you're going to do
  1455. 51:48right. So first of all we have to check
  1456. 51:50if this is failing or not. So I'm just
  1457. 51:52going to do if this fails if should
  1458. 51:55fail. So in that case I'm just going to
  1459. 51:57log error and I'm simply going to do
  1460. 52:00manual
  1461. 52:02uh failure
  1462. 52:04triggered
  1463. 52:05with
  1464. 52:07fail flag. Okay. And then I'm also going
  1465. 52:11to take one more and I'll do if this
  1466. 52:13class. So I'll do suit crash manual
  1467. 52:16crash triggered with crash flag. Okay.
  1468. 52:22And then at the end I'm just going to
  1469. 52:23run this app. So I'll save this. So now
  1470. 52:26let's go to package uh JSON first. Also
  1471. 52:30let's go to our script. And in this uh
  1472. 52:33script maybe first let's take the build.
  1473. 52:35So I'm just going to use the build sts
  1474. 52:39C. And then I'm simply going to take the
  1475. 52:41first one and this will be we are going
  1476. 52:43to use tsx. I'm simply going to do
  1477. 52:46source because we have source inside we
  1478. 52:48have this particular file name. Right?
  1479. 52:50So we'll copy this. So I'll do source/
  1480. 52:54uh 01 process object ds. So now save
  1481. 52:58this. I'll see that what will happen.
  1482. 53:00First I'll go here clear this and then
  1483. 53:04I'm going to do npm run and we have to
  1484. 53:06run the script. So the script will be
  1485. 53:08first 01. This is the script we need to
  1486. 53:11run. Enter. And you'll see that we are
  1487. 53:13getting command start and process
  1488. 53:15[clears throat] finished with exit code
  1489. 53:17zero. Right? And what actually zero
  1490. 53:19means? Now this is also very very
  1491. 53:20important. So zero means success, one
  1492. 53:23means failure. Okay. Now what I'll do
  1493. 53:26right? So I'll go here and see here when
  1494. 53:29this will fail. Now let's say now we are
  1495. 53:31doing a manual failure. That is fine.
  1496. 53:33But consider there is something actually
  1497. 53:34is wrong. And what you need to do right
  1498. 53:37whenever you're going to do any
  1499. 53:38functionality here this is not log right
  1500. 53:40because see when you're going to actual
  1501. 53:42production level application you're
  1502. 53:44going to not going to log something
  1503. 53:45you're going to do actual some
  1504. 53:46functionality then you're going to do
  1505. 53:49process.exit and you'll see that this is
  1506. 53:51the reason in some code you'll see this
  1507. 53:53process.exit
  1508. 53:54one and this one means this is a
  1509. 53:57failure. You're getting the same thing
  1510. 53:58you have to do here also. Do process
  1511. 54:00do.exit exit and then you'll do one. Now
  1512. 54:04I'll save this and you'll see that first
  1513. 54:05of all what I'm going to do I'll clear
  1514. 54:07this and now I can directly do like
  1515. 54:08this. I'll do npm run 01
  1516. 54:1201
  1517. 54:13and then I'm going to do start. Okay, so
  1518. 54:16this is going to start it. So you'll see
  1519. 54:17that we getting start command but the
  1520. 54:19moment I'm just going to do npm run or
  1521. 54:22let's write it. So I'll do npm run 01 -
  1522. 54:27start. And now I'm going to give a flag
  1523. 54:29here. So I'll do hyphen -y fail. Okay.
  1524. 54:32And you see if I do enter and you'll see
  1525. 54:35that now manual failure triggered with
  1526. 54:37fail flag. And there is process finished
  1527. 54:39with exit code one. Okay. The same thing
  1528. 54:42you can do with crash also. So we'll do
  1529. 54:45npm run 01 -
  1530. 54:48start and then I'll do hyphen crash. And
  1531. 54:51if I do enter here, you'll see the
  1532. 54:52manual crash is now triggered and
  1533. 54:54process finished with exit code one. So
  1534. 54:56this is how you to use command line
  1535. 54:58arguments. Okay, this is extremely
  1536. 54:59important concept. You you don't have
  1537. 55:01any idea how important these things are.
  1538. 55:03You'll see that when you're going to do
  1539. 55:04some express level of project, right?
  1540. 55:05Let's say you're using express and
  1541. 55:06you're creating uh some complicated rest
  1542. 55:08API, you'll see that in your code, there
  1543. 55:10is a possibility you're going to have
  1544. 55:12this line or you're going to have this
  1545. 55:14particular line. Maybe you have you have
  1546. 55:16read some other source code, you'll see
  1547. 55:18that this line might be present. And
  1548. 55:19this is the actual reason of using this
  1549. 55:21process.exit of zero and process.exit of
  1550. 55:24one. Okay. So now I think this is uh
  1551. 55:28done. I think we don't have that much
  1552. 55:29practical because but still you need to
  1553. 55:31know all these concept whatever you have
  1554. 55:33covered. So with this now let's move on
  1555. 55:35to the next mod module which will be uh
  1556. 55:38let's cover the crypto module because
  1557. 55:40crypto is one of the most important and
  1558. 55:41you are going to actually use crypto
  1559. 55:43module in a rest API project. So first
  1560. 55:45we're going to cover the crypto module
  1561. 55:46and then we'll move to the some other
  1562. 55:48modules in the next part. So moving to
  1563. 55:51the next one. So let's create one more
  1564. 55:53file. I hope that you all are writing
  1565. 55:55all of this along with me right why
  1566. 55:57because if your basics are not clear
  1567. 55:59what I always mention in all my videos
  1568. 56:01you're not going to be a good developer
  1569. 56:03in any tech whatever you will learn okay
  1570. 56:06so what when I'm talking about writing
  1571. 56:08means try to implement the code that I'm
  1572. 56:11writing and then pause the video or
  1573. 56:12maybe let's say later also you can do
  1574. 56:14and then go and experiment and learn
  1575. 56:16some more things on the same module and
  1576. 56:18then try to write some more code okay so
  1577. 56:20basically what I'm trying to say do not
  1578. 56:22just write what I'm writing write what
  1579. 56:25I'm writing plus write something that
  1580. 56:27I'm not writing you are getting what I'm
  1581. 56:29talking about means that what will
  1582. 56:31happen let's you are doing crypto module
  1583. 56:33you're going to learn so from a uh
  1584. 56:35tutorial point of view what how much I
  1585. 56:37can do I can give you the basic idea
  1586. 56:39this is a module this is the reason this
  1587. 56:41module exist and these all are the
  1588. 56:43methods I'm not going to cover each and
  1589. 56:45everything if you go to the actual note
  1590. 56:47documentation you'll see that this
  1591. 56:48particular module if you try to learn
  1592. 56:50each and everything it will take you
  1593. 56:51seven 7 days but This is not how you're
  1594. 56:54going to learn right when you're going
  1595. 56:56to learn a certain thing you have to
  1596. 56:58understand what are the things that we
  1597. 57:00usually actually use in a real project.
  1598. 57:03So that means what what you need to do
  1599. 57:05you need to learn from a video learn or
  1600. 57:08uh read some of the documentation the
  1601. 57:10best will be read some blogs and then
  1602. 57:12make use some some some AI tools and
  1603. 57:14everything and then try to write some
  1604. 57:16code by yourself. So first what we are
  1605. 57:18going to do right we going to understand
  1606. 57:20one more module which is called crypto
  1607. 57:22module and you'll see that you are
  1608. 57:23actually going to really going to use
  1609. 57:25this one in so many projects. Okay, this
  1610. 57:27is one of the most important module that
  1611. 57:28you should know. Now, this you're going
  1612. 57:31to usually import from again node.
  1613. 57:34Okay, so you can do direct like this.
  1614. 57:36Okay, crypto. But I usually like this
  1615. 57:37node and then I'm simply going to do
  1616. 57:39crypto. So this will be my crypto
  1617. 57:41module. What actually crypto module is
  1618. 57:43again this is a built-in NodeJS module.
  1619. 57:46Okay, so this is already built-in NodeJS
  1620. 57:48module system. Built-in NodeJS module
  1621. 57:51system. Okay. So now what you're going
  1622. 57:54to do, right? So first of all you have
  1623. 57:55to understand that these you're going to
  1624. 57:57do or these you're going to use any
  1625. 57:59security related tasks as simple as that
  1626. 58:02means consider some of the things I'm
  1627. 58:04going to give you are going to use a lot
  1628. 58:07creating random UU ids or you're going
  1629. 58:10to use normal ids you you can also use
  1630. 58:13this one create
  1631. 58:16secure token very important hashing data
  1632. 58:20extremely important okay you are going
  1633. 58:22to use a lot to hash data
  1634. 58:25also very important sometime we going to
  1635. 58:27use this one to verify I'm just going to
  1636. 58:29do to verify if the data was changed or
  1637. 58:35not changed so I'll do data was not
  1638. 58:36changed something like this okay uh what
  1639. 58:39else also encryption and uh decryption
  1640. 58:42so I'll do encrypt and decrypt so all
  1641. 58:46this will be the most used functionality
  1642. 58:48that you're going to do in this one
  1643. 58:50we're going to understand some of the
  1644. 58:51very important methods so first will be
  1645. 58:52crypto dot
  1646. 58:54random I think it's called U ID this one
  1647. 58:58okay random UI ID then random bytes
  1648. 59:02something called create uh has and then
  1649. 59:06also you have create hmax something like
  1650. 59:07this and you'll see that if I do create
  1651. 59:09only you're getting this many things and
  1652. 59:11it's almost next to impossible for me to
  1653. 59:13cover each and everything right so we're
  1654. 59:14going to only cover the things that we
  1655. 59:16usually use most so first let's start
  1656. 59:19with crypto dot random uu U ID. Okay. So
  1657. 59:25why why actually you need this one. So
  1658. 59:27first of all you have to understand what
  1659. 59:28UID means. So this is means uh if I'm
  1660. 59:31not wrong it's called uni universal uh
  1661. 59:33unique identifier something like this.
  1662. 59:35Let's check once. So I'll do UID full
  1663. 59:39form universal or universal versially
  1664. 59:42I'm not universal unique identifier.
  1665. 59:44Okay. So universal unique identifier. So
  1666. 59:47this is nothing but it's a uh 128 bit
  1667. 59:50level. But let's understand how much I
  1668. 59:52know. Now I'll try to explain in a
  1669. 59:53simpler way. So U ID I think we have
  1670. 59:56already seen it's called universal
  1671. 59:57unique identifier. This simply means
  1672. 59:59when you need a unique ID right? So you
  1673. 1:00:01need a unique ID you're going to use
  1674. 1:00:03this. This can be most used will be user
  1675. 1:00:05ID something like this. You have your
  1676. 1:00:07order ID. You're creating some payment
  1677. 1:00:08system session ID. Okay this is also
  1678. 1:00:11important or request ID all these
  1679. 1:00:12things. Okay. Now here uh what we are
  1680. 1:00:16going to do? So let's I'm just going to
  1681. 1:00:18do this one as request ID. And you
  1682. 1:00:20simply going to do crypto dot random uu
  1683. 1:00:25id. That's it. You don't have to do
  1684. 1:00:26anything here. And you just simply do
  1685. 1:00:28log. And I'm simply going to log this
  1686. 1:00:31request ID. Okay. So if I save this. So
  1687. 1:00:34let's run all of these side by side. So
  1688. 1:00:36I'll go to package.json. I'll create one
  1689. 1:00:38more. So I'll do 02. This will be tsx
  1690. 1:00:42source slash
  1691. 1:00:44and let's copy the name
  1692. 1:00:48dot ts. Save this and then we going to
  1693. 1:00:52clear and then I'm just going to run npm
  1694. 1:00:54run 02. Why? Because we have this
  1695. 1:00:57particular script. And if I do enter and
  1696. 1:00:59you'll see that you're getting a unique
  1697. 1:01:01UU ID. Okay. So next one. So let's go
  1698. 1:01:04here. So next will be random bytes.
  1699. 1:01:07Okay. So you're going to do crypto dot
  1700. 1:01:09random bytes.
  1701. 1:01:12Okay. Why actually you need this? You
  1702. 1:01:14also need to do actually use cases,
  1703. 1:01:15right? So this will be when you're going
  1704. 1:01:16to create secure random bytes. This is
  1705. 1:01:19usually useful very useful when you're
  1706. 1:01:21going to do some pass password reset
  1707. 1:01:23feature. So let's say you're going to
  1708. 1:01:25create a password reset token and then
  1709. 1:01:27user is going to validate you are going
  1710. 1:01:29to validate validate the user based on
  1711. 1:01:31this reset token or let's say you wanted
  1712. 1:01:33to pass a email verification token. This
  1713. 1:01:35is also very important, right?
  1714. 1:01:38session secret and also you can also use
  1715. 1:01:42this one in API keys also. Okay. So now
  1716. 1:01:45let's see if I just try to do like this.
  1717. 1:01:47So I do random bytes and then I'm simply
  1718. 1:01:49going to do like this 16. Okay. Okay.
  1719. 1:01:51Let's not import this one directly like
  1720. 1:01:53this for now. Let's keep it. So what
  1721. 1:01:55actually mean by this? So it's simply
  1722. 1:01:57telling that okay give me random bytes
  1723. 1:01:58but give me 16 random bytes. Okay. So
  1724. 1:02:02how this usually works? So first what we
  1725. 1:02:05going to do right? So first I'm just
  1726. 1:02:06going to do a give me reset token. So
  1727. 1:02:09I'm just going to do crypto dot random
  1728. 1:02:11bytes give me 16 random bytes and then
  1729. 1:02:14we are going to convert these bytes to a
  1730. 1:02:16hex format. Okay. So in hex format what
  1731. 1:02:19will happen right? One bite will become
  1732. 1:02:21two characters. Okay. So that means when
  1733. 1:02:24you're going to get a 16 byt mean it
  1734. 1:02:26will become a 32 character string. Very
  1735. 1:02:29important right? So you can just verify.
  1736. 1:02:31So here what I'm going to do so is one
  1737. 1:02:34byte is two characters
  1738. 1:02:38because if I have random bytes
  1739. 1:02:4316 means 32 character string. Let's see.
  1740. 1:02:50Ah my bad. So it's not actually two
  1741. 1:02:52characters but it will become two
  1742. 1:02:54characters string when you convert into
  1743. 1:02:56a hex format. I think this I mentioned
  1744. 1:02:57that you have to convert this one to a
  1745. 1:02:59hex format. So what I mean by this? So
  1746. 1:03:00this is simply telling that you create a
  1747. 1:03:0216 uh bytes but you're going to do a two
  1748. 1:03:05string and then you're going to convert
  1749. 1:03:07this one to hex format. So this time it
  1750. 1:03:09will become I'll show you my bad. Yes.
  1751. 1:03:12In hex format only I'm asking
  1752. 1:03:21yes. So when you're going to format raw
  1753. 1:03:23data as hexadimal every single bite into
  1754. 1:03:26translate into uh exactly two text
  1755. 1:03:29characters means in this case now this
  1756. 1:03:3116 byt will become a 32 character
  1757. 1:03:33string. So this will be a 32 string and
  1758. 1:03:37also we can validate this. So now here
  1759. 1:03:39I'm simply going to do log and I'll do
  1760. 1:03:42reset token save this and let's run once
  1761. 1:03:46again. So I'll do clear
  1762. 1:03:49and you can see that this will be your
  1763. 1:03:51and you can also check this right 1 2 3
  1764. 1:03:534 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  1765. 1:03:5919 20 1 2 3 4 5 6 7 8 9 10 11 12. Okay,
  1766. 1:04:04so this will become 32 characters and we
  1767. 1:04:07can verify is this 32 characters. Why
  1768. 1:04:10I'm explaining you'll understand that
  1769. 1:04:11why I'm going into too much details
  1770. 1:04:13because this is what we are going to
  1771. 1:04:14actually use in a project.
  1772. 1:04:16See this string is exactly 32
  1773. 1:04:18characters. It's a valid MD5 has or a 16
  1774. 1:04:22by hexadimal string. Since 16 byt into
  1775. 1:04:24two characters. So this will be 32
  1776. 1:04:26characters. Okay. And you'll see that
  1777. 1:04:28exactly these particular things you're
  1778. 1:04:30going to use actual project when you're
  1779. 1:04:31going to build this. Let's say password
  1780. 1:04:33reset token feature email verification.
  1781. 1:04:35You're going to use this one. Okay. Then
  1782. 1:04:37next we have something called crypto dot
  1783. 1:04:40I think we let's do create h has because
  1784. 1:04:42this is also important. Okay. Create
  1785. 1:04:43hash. So create h has will be has what
  1786. 1:04:47what actually has is going to do? So h
  1787. 1:04:49is going to convert your data to a fixed
  1788. 1:04:51length string. Okay, fixed length
  1789. 1:04:54string. Very important, right? So now
  1790. 1:04:57consider you have something like this.
  1791. 1:04:58So you have a hello.
  1792. 1:05:01Let's do like this. Hello. And this you
  1793. 1:05:03wanted to convert to a hash. Something
  1794. 1:05:05like this. Now here directly from a hash
  1795. 1:05:09to this hello is not possible. Okay. So
  1796. 1:05:12now what you have to do right hashing
  1797. 1:05:15what whenever you have something called
  1798. 1:05:17hashing it always one way. So that
  1799. 1:05:19simply means let's say you have a input
  1800. 1:05:21that input can become has but you can't
  1801. 1:05:25convert that has directly to the
  1802. 1:05:27original input. You can also verify. So
  1803. 1:05:29if I just do has
  1804. 1:05:32one way means if I if I have a input
  1805. 1:05:38right that become has I can't convert
  1806. 1:05:45that has back to same original
  1807. 1:05:50input correct so let's see
  1808. 1:05:55uh see so it's a strictly one way
  1809. 1:05:57cryptographic operation means you can't
  1810. 1:06:00reverse it. Okay, this is very
  1811. 1:06:02important. So that is the reason I
  1812. 1:06:04mentioned that H is going to convert
  1813. 1:06:05your data to a fixed length string and
  1814. 1:06:08it is one way. One way means if your
  1815. 1:06:10input becomes H has you can't convert
  1816. 1:06:13that has back to the original input.
  1817. 1:06:15It's not possible. But this is actually
  1818. 1:06:17helpful if you wanted to check your data
  1819. 1:06:19whether that particular data changed
  1820. 1:06:21cast key or let's you wanted to compare
  1821. 1:06:23something. So let's say I have a con and
  1822. 1:06:26I'll do text and maybe let's do hello
  1823. 1:06:28node or whatever you wanted to take and
  1824. 1:06:30let's say I wanted to create a has and
  1825. 1:06:32then I'm simply going to do crypto dot
  1826. 1:06:34create has and here I'm simply going to
  1827. 1:06:37give algorithm. So in this case I'm
  1828. 1:06:39going to give sh a uh 256 and here I'm
  1829. 1:06:43simply going to do dot update to text
  1830. 1:06:47and then what you have to do you have to
  1831. 1:06:48digest this one to a hex into hex
  1832. 1:06:52format. Okay. And then you can just log
  1833. 1:06:54this hash here. Okay. So now if I save
  1834. 1:06:59this, I think we have to pass the text
  1835. 1:07:02this one this text. So if I save this,
  1836. 1:07:05you'll see that this will create a hash.
  1837. 1:07:09Okay. So it's going to create a hash
  1838. 1:07:11here. So this is about how you can make
  1839. 1:07:14use of this crypto.re.
  1840. 1:07:16Then we have something called let's do
  1841. 1:07:18this one also, right? Maybe let's create
  1842. 1:07:21uh let's understand one more which is
  1843. 1:07:23also very important that is called
  1844. 1:07:24crypto dot create hmac.
  1845. 1:07:29So what this is going to do? See first
  1846. 1:07:31of all you need to know what actually
  1847. 1:07:33HMAC means. So it means you have a hash
  1848. 1:07:36based message authentication code
  1849. 1:07:39something like this right? It's like a
  1850. 1:07:41hash only. It's similar to h has but
  1851. 1:07:43it's like more like of a think like a
  1852. 1:07:45secret key. You have a secret key. So in
  1853. 1:07:47normal has right if you have a normal
  1854. 1:07:49hash something like this you have a
  1855. 1:07:51normal has
  1856. 1:07:53normal has how it will work. So you have
  1857. 1:07:55a data and you're going to convert this
  1858. 1:07:57one to has something like this in HMAC
  1859. 1:08:00what will happen to explain in a simpler
  1860. 1:08:02way you have a data but at the same time
  1861. 1:08:05let's say this is a secret so you have a
  1862. 1:08:06secret you have a secret okay what I'm
  1863. 1:08:11doing man and this will be your signed h
  1864. 1:08:15okay so this is the difference
  1865. 1:08:18so now okay one of the best use of using
  1866. 1:08:22this create hmac will be whenever you're
  1867. 1:08:24going to do any kind of webbook related
  1868. 1:08:26uh functionality right these can be
  1869. 1:08:27helpful or maybe signed token also
  1870. 1:08:30signed tokens right so let's say I have
  1871. 1:08:33a secret key so I'll do secret key and
  1872. 1:08:36this will be let's say my super secret
  1873. 1:08:42key something like this and then I have
  1874. 1:08:44a message also here so message and let's
  1875. 1:08:47do user
  1876. 1:08:49id equal to 1 and here now you are going
  1877. 1:08:52to create a signature so how We're going
  1878. 1:08:54to create a signature. So, we'll do
  1879. 1:08:56crypto dot create
  1880. 1:09:01crypto dot create
  1881. 1:09:03h uh no create uh hmac here. You're
  1882. 1:09:08going to give so I'll do sh a 256
  1883. 1:09:12and you're going to pass the secret
  1884. 1:09:14here. Okay. So, you have to pass the
  1885. 1:09:15secret here. So, do secret.
  1886. 1:09:19Let's do secret. So, I'll do secret.
  1887. 1:09:24and then you're going to update. So now
  1888. 1:09:25I'm going to update my message and then
  1889. 1:09:27I'm just going to digest this one to X.
  1890. 1:09:32So this is going to create a signature
  1891. 1:09:35has. So this is going to create a
  1892. 1:09:37signature has. Okay. So now you can also
  1893. 1:09:39verify this one. So let's say first
  1894. 1:09:42let's log this
  1895. 1:09:48and let's clear this. And if I run this
  1896. 1:09:50one, you'll see that it's going to
  1897. 1:09:51create this last one. Okay? So it's
  1898. 1:09:54going to create a signature has. Okay?
  1899. 1:09:55Because you're passing a data plus
  1900. 1:09:57secret. Previously, you are only passing
  1901. 1:09:59data. See, you're not passing any
  1902. 1:10:00secret. So that is the difference. So
  1903. 1:10:02now let's say you want to do verify. So
  1904. 1:10:04do con signature
  1905. 1:10:07verify something like this verify. And
  1906. 1:10:11then you can do crypto dot create hmac.
  1907. 1:10:17And here you're going to pass the so
  1908. 1:10:19I'll do sha 256
  1909. 1:10:22pass the secret and here what you can do
  1910. 1:10:25right uh
  1911. 1:10:28same thing you'll do update
  1912. 1:10:31update the message update message mean
  1913. 1:10:34this message and then you're going to
  1914. 1:10:36just do dot digest to x and now what you
  1915. 1:10:41can do right if I do log and you'll see
  1916. 1:10:44so I'll do signature
  1917. 1:10:46is valid and matching and I'm simply
  1918. 1:10:50going to do if this signature that I
  1919. 1:10:51created this one this signature and this
  1920. 1:10:54signature to verify both are same or not
  1921. 1:10:56so you'll understand so do signature to
  1922. 1:10:58verify okay so if I save this and let's
  1923. 1:11:01see what you're getting
  1924. 1:11:05and you can see that is getting
  1925. 1:11:06signature is valid and matching it's
  1926. 1:11:08true and makes sense so this is where
  1927. 1:11:10what you can do right consider this we
  1928. 1:11:11are doing manually but in a real project
  1929. 1:11:13you are going to let's say getting this
  1930. 1:11:15token from some other place And you're
  1931. 1:11:16going to verify. So you'll be able to
  1932. 1:11:18verify this one using this create uh
  1933. 1:11:20HMAC this particular method. Okay. So
  1934. 1:11:23now if you go here right so you
  1935. 1:11:25understood some very important things.
  1936. 1:11:27Random U ID is going to create unique
  1937. 1:11:30ids. Random is going to do what? It's
  1938. 1:11:32going to create random tokens secret
  1939. 1:11:35random tokens. You have create h has
  1940. 1:11:37create is going to do what? It's going
  1941. 1:11:38to create oneway hash. That is very
  1942. 1:11:40important. I already explained. and
  1943. 1:11:42create hmac is going to create h has
  1944. 1:11:44using a secret key using a secret key so
  1945. 1:11:47with this this particular module is now
  1946. 1:11:50completed and now let's move on to the
  1947. 1:11:51next one all right so let's move on to
  1948. 1:11:53the next one which will be uh so let's
  1949. 1:11:55create another file and this will be OS
  1950. 1:11:57module okay now this will be sort but
  1951. 1:12:01still it is actually important so that's
  1952. 1:12:02the reason actually I've decided to keep
  1953. 1:12:04this so first of all OS means I think we
  1954. 1:12:06all know this means operating system
  1955. 1:12:08right so what OS module is going to give
  1956. 1:12:10you information is going to give to give
  1957. 1:12:12you the information about your license.
  1958. 1:12:14Okay. Now, some of the common use cases
  1959. 1:12:16now all those see I'll be very honest
  1960. 1:12:18when you're going to create any rest
  1961. 1:12:19APIs until unless you're going to have
  1962. 1:12:20very specific functionality related to
  1963. 1:12:23that particular project you're not going
  1964. 1:12:25to use that much of this particular
  1965. 1:12:27model. Okay. But some of the very
  1966. 1:12:29important use cases will be let's say
  1967. 1:12:31you wanted to check your operating
  1968. 1:12:32system. So first of all let's say you
  1969. 1:12:34wanted to check your CPU information. So
  1970. 1:12:37in that case this is important. Or let's
  1971. 1:12:38say you wanted to check your memory
  1972. 1:12:40information. This is also uh important
  1973. 1:12:42and also sometimes you also need to find
  1974. 1:12:44your home directories or let's say you
  1975. 1:12:47wanted to check some temp directories.
  1976. 1:12:49So that time also this is important. So
  1977. 1:12:51how we are going to use this? So first
  1978. 1:12:52of all we are going to import something.
  1979. 1:12:54So I'm going to import star as os from
  1980. 1:12:57so we are going to use from node and
  1981. 1:12:59this will be from os module. Okay. So
  1982. 1:13:02first of all let's do one thing. I'm
  1983. 1:13:03just going to create a very simple
  1984. 1:13:04function and we are going to understand
  1985. 1:13:06one by one. So I'll do run os demo. what
  1986. 1:13:10this is going to return let's give void
  1987. 1:13:12and then let's start with the first one
  1988. 1:13:13and I'm going to take this one as let's
  1989. 1:13:15say platform okay and here I'm simply
  1990. 1:13:18going to do osplatform
  1991. 1:13:20now let's do one thing let's uh also
  1992. 1:13:23maybe run this so I'm just going to run
  1993. 1:13:25this here and let's save this go to
  1994. 1:13:28package dojson let's create one more so
  1995. 1:13:31I'll do 03 so do tsx source/
  1996. 1:13:35and this will be rename copy is
  1997. 1:13:40ds save and now first of all let's start
  1998. 1:13:43with the first one which will be what
  1999. 1:13:45you're getting in the platform correct
  2000. 1:13:47so I'll do npm run 03 enter and you can
  2001. 1:13:50see that we are getting platform darwin
  2002. 1:13:52the platform this will defer whatever
  2003. 1:13:53platform you're using and based on that
  2004. 1:13:56this will change uh what will be the
  2005. 1:13:58next things let's check the architecture
  2006. 1:14:00so I'm just going to do console.log log
  2007. 1:14:02and let's do architecture and this will
  2008. 1:14:04be your OS.arch arc and this will give
  2009. 1:14:07you the architecture right return the
  2010. 1:14:09operating system CPU architecture for
  2011. 1:14:11which the NodeJS binary was compiled
  2012. 1:14:13okay so let's save this and if you just
  2013. 1:14:16clear and run one second and you'll see
  2014. 1:14:18that you're going to get the
  2015. 1:14:19architecture okay you can also get your
  2016. 1:14:22OS type now also you might be thinking
  2017. 1:14:25these things are not that much difficult
  2018. 1:14:27but actually these are actually your
  2019. 1:14:29node basics right these is going to make
  2020. 1:14:31you better the all these concepts right
  2021. 1:14:33is going to make you better node
  2022. 1:14:35developer consider let's say one person
  2023. 1:14:36is starting [snorts] learning NodeJS and
  2024. 1:14:38directly jumping to the rest API but on
  2025. 1:14:40the other side what you are doing you
  2026. 1:14:42are learning each and every NodeJS core
  2027. 1:14:43module you know how you can create APIs
  2028. 1:14:46using only HTTP module how you can
  2029. 1:14:48create routes and everything and then
  2030. 1:14:49you jump to express a node it will give
  2031. 1:14:51you way better advantage whenever you'll
  2032. 1:14:53be going for interview especially for
  2033. 1:14:54interview this will be a game changer
  2034. 1:14:56all these modules whatever you are
  2035. 1:14:58seeing and I can guarantee whenever
  2036. 1:15:00you'll go for any very complicated
  2037. 1:15:02interview right where they will test
  2038. 1:15:03your concepts concept conceptual
  2039. 1:15:05concepts especially they are going to
  2040. 1:15:08ask you questions from all these module
  2041. 1:15:09I can guarantee even if you go to lead
  2042. 1:15:11late code discussion and everything
  2043. 1:15:12you'll see that these kind of questions
  2044. 1:15:14so many people usually get so this will
  2045. 1:15:16be your OS type so let's run again and
  2046. 1:15:19you'll see that you're going to get your
  2047. 1:15:20OS type also here what else you can very
  2048. 1:15:23important you can also get your OS
  2049. 1:15:25release also so let's say here you can
  2050. 1:15:27do write OSR release okay and if you
  2051. 1:15:30save this let's see if I run once again
  2052. 1:15:33you're going to get that also Okay, what
  2053. 1:15:35else? As I already mentioned that you
  2054. 1:15:37are also going to get your home
  2055. 1:15:38directory. So let's I'm just going to do
  2056. 1:15:40home directory.
  2057. 1:15:43And what you can do you can do os doome
  2058. 1:15:45directory. Okay. And the same way you
  2059. 1:15:47can also get your temp directory. So
  2060. 1:15:50let's see if I do console.log and let's
  2061. 1:15:52do temp directory
  2062. 1:15:55and this is going to give you this temp
  2063. 1:15:57directory. Save this.
  2064. 1:16:01And you can see that this is my home
  2065. 1:16:03directory and this is my temp directory.
  2066. 1:16:06Now how you're going to get the CPU
  2067. 1:16:08information? Now CPU information you're
  2068. 1:16:10going to get from so do con let's do
  2069. 1:16:12CPUs. So we do OS dot CPU CPUs and here
  2070. 1:16:17you can you will be able to get the
  2071. 1:16:18count also. Let's say if I just do CPUs
  2072. 1:16:21dot uh I think it's called length. So
  2073. 1:16:23you'll be able to get the CPU count also
  2074. 1:16:25here. Maybe let's do one thing or let's
  2075. 1:16:28expand this one little here and then
  2076. 1:16:30clear this and then if I run here you
  2077. 1:16:32are going to get the CPU's count also.
  2078. 1:16:35Okay. So now uh consider that you wanted
  2079. 1:16:38to check your CPU model that is also
  2080. 1:16:41possible. So let's see if I just wanted
  2081. 1:16:42to give the okay my CPUs dot length if
  2082. 1:16:46this is greater than zero. So what I can
  2083. 1:16:49do I can do uh inside this I can do a
  2084. 1:16:51log and I'll do a first CPU model or
  2085. 1:16:54something like this. So do first CPU
  2086. 1:16:56model and I'm simply going to do CPUs
  2087. 1:17:00I think CPU of zero dot model. I think
  2088. 1:17:03this is going to give you the model
  2089. 1:17:04here. Okay. If you do dot you see the
  2090. 1:17:06model speed times all these things
  2091. 1:17:08you're going to get. So I'll do model.
  2092. 1:17:09Let's do the CPUs of 0 dot speed and
  2093. 1:17:14also I'll do CPUs dot of 0 dot times. So
  2094. 1:17:19you will be able to get all of this
  2095. 1:17:20information also. So clear and run this
  2096. 1:17:23and you can see that first CPU and all
  2097. 1:17:25this information the speed and
  2098. 1:17:27everything you're going to get. Okay. So
  2099. 1:17:29this is done. You can also get the
  2100. 1:17:31memory information also from here
  2101. 1:17:32directly. Okay. And let's say for now
  2102. 1:17:35I'm just going to keep it simple. So I'm
  2103. 1:17:37just going to log here and I'll do OS
  2104. 1:17:39dot total memory. See directly you'll be
  2105. 1:17:41able to get return the total memory of
  2106. 1:17:42the system memory in bytes. Okay. What
  2107. 1:17:44you can do, you can also convert these
  2108. 1:17:46bytes to GB if you wanted to check and
  2109. 1:17:48also you'll be able to get the free
  2110. 1:17:50memory also here.
  2111. 1:17:52Save this and let's clear and if I run
  2112. 1:17:56all of these
  2113. 1:17:59so this will be total uh the memory and
  2114. 1:18:02the free memory all these. So everything
  2115. 1:18:04you're going to get from this right
  2116. 1:18:06mostly as I already mentioned OS CPU
  2117. 1:18:08information you're going to get memory
  2118. 1:18:10and home and temp directory all these
  2119. 1:18:12things you you will be able to
  2120. 1:18:14understand from this particular model I
  2121. 1:18:16think that's all we don't have that much
  2122. 1:18:18so what will be the next one next one
  2123. 1:18:20will be path module we are going to
  2124. 1:18:21understand that is also very important
  2125. 1:18:23so let's move on to the next one okay so
  2126. 1:18:25let's go to the next one which will be
  2127. 1:18:27path module uh very very important okay
  2128. 1:18:30this one actually you're going to use in
  2129. 1:18:32real project okay whenever you're going
  2130. 1:18:34to build. So first of all, uh what path
  2131. 1:18:36module actually helps? So this is going
  2132. 1:18:38to help you to build and read the file
  2133. 1:18:41paths. Okay, it will help you to build
  2134. 1:18:44and read file paths first of all. Okay,
  2135. 1:18:49so first how we are going to do this?
  2136. 1:18:50We're going to import path from you can
  2137. 1:18:53see that you're getting the import also.
  2138. 1:18:54So it will be from node and path module.
  2139. 1:18:57So now uh how we are going to do this?
  2140. 1:18:59Now before that we also need to
  2141. 1:19:00understand some of the bad practices
  2142. 1:19:02that you should not do. For example,
  2143. 1:19:04let's say you wanted to get some file
  2144. 1:19:07file path. Okay. Or maybe let's say you
  2145. 1:19:10wanted to create some uh folder
  2146. 1:19:13something like this. Just think of any
  2147. 1:19:15scenario what you're going to do right.
  2148. 1:19:17So let's say here I'm just going to take
  2149. 1:19:19like I have a const and I have a file
  2150. 1:19:21path something like this. And here what
  2151. 1:19:23you're doing right? So let's say you
  2152. 1:19:25have a project root. Okay. This is your
  2153. 1:19:27project root. This is the root path of
  2154. 1:19:29your project and then you're doing like
  2155. 1:19:31this. Okay, I'm just doing uh add
  2156. 1:19:33uploads plus uh add file name something
  2157. 1:19:37like this. Now this is not a good
  2158. 1:19:38practice. So here instead of doing this
  2159. 1:19:40whenever you're going to have this kind
  2160. 1:19:42of situation you are going to use path
  2161. 1:19:44module instead of instead of doing like
  2162. 1:19:46this. Okay. So how you're going to do
  2163. 1:19:48this? So this path module is going to
  2164. 1:19:50give you this very very important
  2165. 1:19:52methods. There are so many but one of
  2166. 1:19:54the most important is join. Okay. This
  2167. 1:19:56path dot join it's going to do what? So
  2168. 1:19:59it's going to first of all it's going to
  2169. 1:20:00use correct separator for the correct
  2170. 1:20:03operating system. This is one of the
  2171. 1:20:05most core concept or core interview
  2172. 1:20:06questions also you can understand that
  2173. 1:20:08why actually you should not do like
  2174. 1:20:09this. Instead why you should use
  2175. 1:20:11something called path dot join. Now here
  2176. 1:20:14you can give that okay this is so or
  2177. 1:20:16this is going to use as the correct
  2178. 1:20:19separator
  2179. 1:20:23for the correct sorry current operating
  2180. 1:20:26system or current OS now you might be
  2181. 1:20:28thinking what actually you mean by this
  2182. 1:20:30now sometimes see whenever we have path
  2183. 1:20:32let's say we're using Mac it can be like
  2184. 1:20:34this so let's say have a users/
  2185. 1:20:37maybe my name let's I have a project
  2186. 1:20:39folder and let's have a file something
  2187. 1:20:41like this
  2188. 1:20:44okay but the same in Windows it can
  2189. 1:20:46happen like this so let's say you have a
  2190. 1:20:47C directory something like this and then
  2191. 1:20:49you have users then you have maybe let's
  2192. 1:20:51say my name then you have project and
  2193. 1:20:54then you you have filet txt you're
  2194. 1:20:56getting so what this path dojo join is
  2195. 1:20:59going to do it's going to use the
  2196. 1:21:00correct separator for the current OS and
  2197. 1:21:03very very important then this is the
  2198. 1:21:05first thing then you have something
  2199. 1:21:06called uh here we have something called
  2200. 1:21:09process
  2201. 1:21:11CW WD what this is going to do this is
  2202. 1:21:14going to give you the current working
  2203. 1:21:15directory. Current working directory
  2204. 1:21:17means again this can also be interview
  2205. 1:21:20question that what we mean by current
  2206. 1:21:22working directory CWD. So current
  2207. 1:21:25working directory means the folder from
  2208. 1:21:27where your NodeJS process actually start
  2209. 1:21:30uh started the path or the folder I
  2210. 1:21:33think the folder will be better way to
  2211. 1:21:34put. So the folder from where
  2212. 1:21:38the NodeJS
  2213. 1:21:40process uh was started. Okay. So this
  2214. 1:21:44will be your process CWD. So now let's
  2215. 1:21:47say what I need to do do right. So first
  2216. 1:21:49of all I need to get my project root. So
  2217. 1:21:52what I will do is I'll do con and first
  2218. 1:21:54of all I wanted to get a project root.
  2219. 1:21:56So I'll do project root. Give me my
  2220. 1:21:57project root. So I'm just going to do
  2221. 1:21:59process and then I'm just going to do
  2222. 1:22:00CWD. So this is going to give me my
  2223. 1:22:03project root. Okay. So now let's see if
  2224. 1:22:05I just do log here and let's log this
  2225. 1:22:08project root. Save this. Go here. And
  2226. 1:22:11let's add one more.
  2227. 1:22:14And I'm just going to do tsx source/
  2228. 1:22:22paste it here. Save this.
  2229. 1:22:26So I'll do npm run 04. And you can see
  2230. 1:22:29that you are going to get your project
  2231. 1:22:31directory. And see this path, right?
  2232. 1:22:33this path or the separator can change.
  2233. 1:22:35If you're using Windows, you can check
  2234. 1:22:36this. So, because I'm using Mac, this is
  2235. 1:22:38coming like this, right? So, YouTube
  2236. 1:22:402026 projects NodeJS 26 one NodeJS
  2237. 1:22:44basics NodeJS code modules. So, this is
  2238. 1:22:46my current working directory. This is
  2239. 1:22:49where I'm actually my NodeJS process
  2240. 1:22:51actually started previously. So, this is
  2241. 1:22:53going to give you my project loop. Okay.
  2242. 1:22:56So, this is the first thing. So, now
  2243. 1:22:58let's say what we are going to do right
  2244. 1:23:00now. I'm just going to create some uh
  2245. 1:23:03hypothetical sees and because we're not
  2246. 1:23:04creating a proper project right here. So
  2247. 1:23:07because we need to understand the
  2248. 1:23:08concept so what I need to do right let's
  2249. 1:23:10say you're creating some project where
  2250. 1:23:12user actually uploads some file okay and
  2251. 1:23:16that we wanted to store that particular
  2252. 1:23:18file under this particular folder I'm
  2253. 1:23:20just giving okay we have uploads/ users
  2254. 1:23:23and let's say the this is my user ID
  2255. 1:23:25which is 42 or whatever you can take and
  2256. 1:23:27then I wanted to add this profile.png
  2257. 1:23:30png. This is what I wanted to upload
  2258. 1:23:33something like this or I wanted to store
  2259. 1:23:35right now as I already mentioned that
  2260. 1:23:38instead of manually you are going to do
  2261. 1:23:39this what we going to use we already
  2262. 1:23:41mentioned we're going to use proc path
  2263. 1:23:43dot join okay because I'm repeating
  2264. 1:23:45again this is going to use the correct
  2265. 1:23:47separator based on the current OS or
  2266. 1:23:50operating system that you you are using.
  2267. 1:23:52So now what I'm going to do right I'm
  2268. 1:23:54simply going to take a const and I'll do
  2269. 1:23:56user ID and let's say I'll do 42 or 43
  2270. 1:23:5923 whatever you wanted to take and I'll
  2271. 1:24:01do a original file name this is the one
  2272. 1:24:03that I wanted to store okay and this can
  2273. 1:24:05be let's say I have a profile dot let's
  2274. 1:24:07take the same only okay this one so copy
  2275. 1:24:10this and then I wanted to take this one
  2276. 1:24:11I wanted to store this right [snorts]
  2277. 1:24:14how this will work so first of all I'm
  2278. 1:24:16just going to do con and I'll do upload
  2279. 1:24:18file path okay now I wanted to take path
  2280. 1:24:22and I wanted to take join. So now how
  2281. 1:24:25how it will work. So first of all I
  2282. 1:24:27wanted to take my project root. Okay. So
  2283. 1:24:29this will be my project root and then I
  2284. 1:24:32wanted to take all the folders or path
  2285. 1:24:35that I wanted to store in. So in this
  2286. 1:24:37case I wanted to store in /uploads/
  2287. 1:24:41users/ user ID which is the dynamic
  2288. 1:24:43value and then the original file name.
  2289. 1:24:45So in this case this will be uploads.
  2290. 1:24:47This is the parent. This is the child.
  2291. 1:24:50So that means user is child of uploads.
  2292. 1:24:53So then user will come. So it will go
  2293. 1:24:55sequentially. You're getting it's not
  2294. 1:24:57that much difficult to understand. It's
  2295. 1:24:59simple. And then 42 will come under user
  2296. 1:25:02folder. That means 42 is dynamic value
  2297. 1:25:05which is the parent of sorry which is
  2298. 1:25:06the child of uh users. Okay. So now uh
  2299. 1:25:10what you have to do let's take the user
  2300. 1:25:12ID here and then I'm just going to take
  2301. 1:25:14the original file name. That's it. Okay.
  2302. 1:25:17What path dojo join is going to do? It's
  2303. 1:25:19going to create a path string. It is not
  2304. 1:25:22going to create folders or it will not
  2305. 1:25:25check whether the file exist or not.
  2306. 1:25:27This is very very important concept. So
  2307. 1:25:29we are going to understand one very
  2308. 1:25:30important concept that path dot join.
  2309. 1:25:33This is going to create
  2310. 1:25:36create a path string. It is actually not
  2311. 1:25:39going to create the folder. It will not
  2312. 1:25:41create it will not create the folder.
  2313. 1:25:44And you'll understand which module we
  2314. 1:25:46are going to use to create the folder
  2315. 1:25:47later all of this. But this is very
  2316. 1:25:49important part. So path dot join
  2317. 1:25:51actually not going to create the folder.
  2318. 1:25:52It only going to create a path. Okay.
  2319. 1:25:55And that path will be a string. So path
  2320. 1:25:57string it's going to create also it
  2321. 1:25:59doesn't check if I'm not wrong. It's
  2322. 1:26:01usually not check. It does does not
  2323. 1:26:03check the uh no it does not check
  2324. 1:26:06whether the file exist or not. Right? So
  2325. 1:26:09file exist or not. It's not going to
  2326. 1:26:11check. So now let's say if I just log
  2327. 1:26:13this upload file path. So I'll upload
  2328. 1:26:16file path save this and you'll see that
  2329. 1:26:19it's going to create a path string.
  2330. 1:26:21Okay.
  2331. 1:26:23And you can see that now notice
  2332. 1:26:25carefully. So user this and that and
  2333. 1:26:27then we have um one nodejs and you can
  2334. 1:26:30see this is a string. Okay. So we have
  2335. 1:26:31one nodejs node js core modules. This is
  2336. 1:26:34the folder. Inside this we have uploads.
  2337. 1:26:38We don't have any folder. See it not
  2338. 1:26:39created any folder. Then we have a
  2339. 1:26:41users. Then we have 42. And then we have
  2340. 1:26:43profile.photo.png.
  2341. 1:26:45So then you might be asking when we can
  2342. 1:26:47use this. So you can use this when you
  2343. 1:26:48have to use when you have to actually
  2344. 1:26:50create the path string. So that it is
  2345. 1:26:52very very important. It is actually not
  2346. 1:26:54going to create your uh folder that
  2347. 1:26:56you're going to understand later.
  2348. 1:26:57[snorts] Uh what else? I think this will
  2349. 1:27:00be I think that's all. Okay. Let's
  2350. 1:27:02understand some of the very important
  2351. 1:27:04point that how you're going to get some
  2352. 1:27:05of the properties from this. So let's
  2353. 1:27:07say I wanted to get the file name. What
  2354. 1:27:10you can do right? You can do something
  2355. 1:27:12like path dot base name and then you
  2356. 1:27:15pass this upload
  2357. 1:27:17file path. What this is going to do?
  2358. 1:27:19Path dot base name is going to give you
  2359. 1:27:22it's going to return the final part of a
  2360. 1:27:24path. Final part final part of a path.
  2361. 1:27:31So now in this case whatever we have
  2362. 1:27:33created right this one. Notice this one.
  2363. 1:27:35So what this is going to return? So this
  2364. 1:27:38will be your base name. So this is the
  2365. 1:27:39final path last one. Okay. So this will
  2366. 1:27:42be your base name. Base name here. Why
  2367. 1:27:44this is helpful? Let's say you want to
  2368. 1:27:45only wanted to extract your visible file
  2369. 1:27:47name. I don't want the full thing. I
  2370. 1:27:50only want the last the visible file
  2371. 1:27:51name. So in that case what you can do
  2372. 1:27:53that from your full path you will be
  2373. 1:27:55able to get your base name or file name.
  2374. 1:27:58So now let's see if I just do log file
  2375. 1:28:00name here.
  2376. 1:28:02Save this and you'll see that you're
  2377. 1:28:04going to get that profile.png.
  2378. 1:28:07Say you're getting profile.photo.png.
  2379. 1:28:10Okay. Next, you have something called
  2380. 1:28:11path extension. So, this is going to
  2381. 1:28:13give you the extension name. So, let's
  2382. 1:28:15say if I just going to do okay, I'll do
  2383. 1:28:17con
  2384. 1:28:20uh let's do file extension.
  2385. 1:28:23So, do f path dot extension name. And
  2386. 1:28:26you can again pass the upload file path.
  2387. 1:28:29So now save this and let's see if I
  2388. 1:28:33paste it here. So what you're going to
  2389. 1:28:34get? You're going to get the extension
  2390. 1:28:36which will be png. So this is my
  2391. 1:28:38extension. Okay. Also you will be able
  2392. 1:28:41to see I'm only discussing the important
  2393. 1:28:44one and you you'll see that most of the
  2394. 1:28:46times you are going to use all these
  2395. 1:28:48properties only in a project. And here
  2396. 1:28:49you also need to understand that you can
  2397. 1:28:51also get your parent folder from this.
  2398. 1:28:53So let's see if I just do path dot
  2399. 1:28:55directory name and pass the upload this
  2400. 1:28:58one. So this is going to give you what
  2401. 1:29:00it's going to give you this one. So it's
  2402. 1:29:02going to give you uh this uh / project
  2403. 1:29:05sorry not project this part uploads user
  2404. 1:29:08and 42 means it's going to give you the
  2405. 1:29:10parent path here. Okay. So if I just log
  2406. 1:29:12this
  2407. 1:29:19uh notice here see users from here. So
  2408. 1:29:22from here till this part. So till this
  2409. 1:29:24part notice carefully. So let me just
  2410. 1:29:26remove this or just complicated. Save
  2411. 1:29:28this
  2412. 1:29:31and you notice this one. So this is your
  2413. 1:29:33full path right and this is your parent
  2414. 1:29:36path means it makes sense right because
  2415. 1:29:39see notice carefully the one that I
  2416. 1:29:42explained that upload upload folder is
  2417. 1:29:44child of this user folder is child of
  2418. 1:29:48this user ID which is a dynamical value
  2419. 1:29:51is a child of this so profile.photo
  2420. 1:29:53photo.png will be child of this because
  2421. 1:29:56this is actually inside this particular
  2422. 1:29:58folder. So that is the reason you're
  2423. 1:29:59getting this one as your parent folder.
  2424. 1:30:02Okay. So all these things you'll be able
  2425. 1:30:03to do. So the most important thing you
  2426. 1:30:06know what you have to understand that
  2427. 1:30:07what path dojo join usually does. Second
  2428. 1:30:11very important that path dojo join is
  2429. 1:30:13going to create a path string. It will
  2430. 1:30:15not create the actual folder. See that's
  2431. 1:30:17why you're not seeing the folder here.
  2432. 1:30:18So that is also important. And also you
  2433. 1:30:21are going to get file name, file
  2434. 1:30:22extension, parent, folder and all these
  2435. 1:30:24things from using this path dot and all
  2436. 1:30:27the properties that whatever is
  2437. 1:30:28available. For example, if you just do
  2438. 1:30:30here path dot and you'll see that you're
  2439. 1:30:33going to base name delimiter all this
  2440. 1:30:35extension format is absolute join all
  2441. 1:30:37this most of the times I also don't know
  2442. 1:30:40each and everything right what will
  2443. 1:30:42happen whenever you're going to create
  2444. 1:30:43actual project right let's say you have
  2445. 1:30:44a certain requirement you'll understand
  2446. 1:30:46what I'm talking about and in that
  2447. 1:30:48particular project you need to use this
  2448. 1:30:50let's say matches glove this particular
  2449. 1:30:52method so you do the web search right
  2450. 1:30:55and then you'll came to know okay this
  2451. 1:30:56is what you need to do and that is how
  2452. 1:30:58you're going to learn thing. You can't
  2453. 1:31:00memorize each and everything. It's
  2454. 1:31:01absolutely not possible. And this is not
  2455. 1:31:03how you should learn all these things.
  2456. 1:31:05Learn what is absolutely needed because
  2457. 1:31:06what will happen at most of the times
  2458. 1:31:08you're going to use path dot join uh and
  2459. 1:31:11this one uh path.base name and
  2460. 1:31:14path.extension name. This is also very
  2461. 1:31:15very important. So that's all about the
  2462. 1:31:18path module. Very important. Now with
  2463. 1:31:19this now let's move on to the next one.
  2464. 1:31:21Okay. So let's move on to the next one.
  2465. 1:31:23So we'll understand uh timers module.
  2466. 1:31:25Okay. So again this is another very
  2467. 1:31:28important and you are frequently uh
  2468. 1:31:30going to use this one in various
  2469. 1:31:31project. So what are the things we are
  2470. 1:31:33going to understand? First of all you
  2471. 1:31:35have to know that why actually need
  2472. 1:31:37timers right. So first of all we usually
  2473. 1:31:40use timers when we want to run code
  2474. 1:31:42after some delay. So let's say we want
  2475. 1:31:44to run something after some delay. Uh
  2476. 1:31:48second would be let's say maybe you want
  2477. 1:31:50to run some particular code repeatedly
  2478. 1:31:55uh repeatedly after some interval after
  2479. 1:31:58some interval maybe let's say you wanted
  2480. 1:31:59to do something in after every 2 seconds
  2481. 1:32:02something like this right
  2482. 1:32:05uh so these two will be the most
  2483. 1:32:07frequent ones and uh here what are the
  2484. 1:32:10timer functions that we usually use so
  2485. 1:32:12first of all we have something called
  2486. 1:32:13set timeout we have something called set
  2487. 1:32:16interval
  2488. 1:32:18We have something called clear timeout.
  2489. 1:32:22We have something called clear interval.
  2490. 1:32:25And also there is one more set
  2491. 1:32:28immediate. Okay. So mostly you are going
  2492. 1:32:30to use all of these and believe me this
  2493. 1:32:32is very very important concept like
  2494. 1:32:34there are lot of places you are going to
  2495. 1:32:36use set timeout, set interval or even
  2496. 1:32:38set immediate also. Okay. So first let's
  2497. 1:32:42do one thing. What are you going to do
  2498. 1:32:43right? So let's start with a simple set
  2499. 1:32:45timeout example. So what I'm going to do
  2500. 1:32:47right, so first let's take a function
  2501. 1:32:50and I'm just going to do run set
  2502. 1:32:54time out example. Okay. And here let's
  2503. 1:32:58return a void. So first of all I'm just
  2504. 1:33:01going to do log and I'm simply going to
  2505. 1:33:04give a sequence or else it will be
  2506. 1:33:05tricky. So I'll do set timeout
  2507. 1:33:09example started and then we are going to
  2508. 1:33:13take set timeout. So you're going to
  2509. 1:33:15take set time out like this and then
  2510. 1:33:17there will be a call back. Okay. And
  2511. 1:33:20then here we are going to understand uh
  2512. 1:33:22sorry not understand we're going to give
  2513. 1:33:24a delay. So that means we are simply
  2514. 1:33:26telling that these particular set
  2515. 1:33:28timeout will run after 1 second because
  2516. 1:33:31we're giving 1,000 millisecond which is
  2517. 1:33:331 second. So here we are simply going to
  2518. 1:33:35do log and here I'm simply going to do
  2519. 1:33:38that this runs after 1
  2520. 1:33:44second. Okay. And then after this I'm
  2521. 1:33:47simply going to do a log one more. And
  2522. 1:33:49here I'm simply going to do three and
  2523. 1:33:51I'll do this run immediately.
  2524. 1:33:54So what will happen right? Node is not
  2525. 1:33:57going to wait. Node doesn't wait.
  2526. 1:34:03Okay, that's it. So now let's save this.
  2527. 1:34:07So first of all, this is one very basic
  2528. 1:34:09example. So simply what we are doing,
  2529. 1:34:12we're simply telling that okay, we are
  2530. 1:34:14running a particular function where we
  2531. 1:34:15have a set time out. We have given that
  2532. 1:34:17okay, after 1 second, this is going to
  2533. 1:34:19run. Okay. So now what we will do,
  2534. 1:34:21right, maybe let's take one um let's
  2535. 1:34:24take one simple function first and then
  2536. 1:34:25we'll do one by one. So I'll do async
  2537. 1:34:28not async I think we can directly take a
  2538. 1:34:30function and I'm simply going to do run
  2539. 1:34:32timer demo and here this is going to
  2540. 1:34:35return void and here I'm simply going to
  2541. 1:34:37do run set timeout example okay and here
  2542. 1:34:41I'm simply going to call this so I'll do
  2543. 1:34:42run timer demo so for now save this and
  2544. 1:34:46we'll go here let's go to package dojson
  2545. 1:34:49so I'm going to 05
  2546. 1:34:51uh which will be tsx source/
  2547. 1:34:54and then we have this timers module.
  2548. 1:34:59Okay. So let's clear this and then I'm
  2549. 1:35:02simply going to do npm run 05 enter. And
  2550. 1:35:05you can see that so set timeout example
  2551. 1:35:08started. This is started. I already
  2552. 1:35:11mentioned that this particular console
  2553. 1:35:13here node is not going to wait for this
  2554. 1:35:16that okay this will run and then we have
  2555. 1:35:18this. So this one will run and then this
  2556. 1:35:20one will run. No first it's going to run
  2557. 1:35:22and then you can see that this runs
  2558. 1:35:24immediately. node doesn't wait and then
  2559. 1:35:26after 1 second node is going to run this
  2560. 1:35:28particular set timeout. Okay. Now this
  2561. 1:35:30is very important because set time set
  2562. 1:35:32timeout is one of the most important
  2563. 1:35:34feature that you are not only need in
  2564. 1:35:36back end but also there are various use
  2565. 1:35:38cases you're going to use this one in
  2566. 1:35:40front side also. So this will be the
  2567. 1:35:43first one. Now let's say uh how you can
  2568. 1:35:45clear uh timeout. Okay. So how you can
  2569. 1:35:48do that? So first of all you're going to
  2570. 1:35:50take a function and you're going to do
  2571. 1:35:52run clear time out
  2572. 1:35:56example
  2573. 1:35:58and here you're going to do void. So so
  2574. 1:36:02first of all you're going to take a
  2575. 1:36:03timer ID and here let's I'm just going
  2576. 1:36:06to take a set time mode and here let's
  2577. 1:36:09do let's take this one 2cond timer.
  2578. 1:36:11Okay. And then I'm simply going to tell
  2579. 1:36:13that okay this message uh let's do one
  2580. 1:36:17thing. Let's log here first
  2581. 1:36:21and then I'm simply going to do this
  2582. 1:36:23message will not run. Okay. Now why? So
  2583. 1:36:29first of all you have to do something
  2584. 1:36:31like clear time out and then I'm simply
  2585. 1:36:34going to pass this time timer ID. What
  2586. 1:36:36actually we are doing right? We are
  2587. 1:36:37actually canceling the timer. We are
  2588. 1:36:40canceling this particular timer before
  2589. 1:36:41even it will get a chance to run. And
  2590. 1:36:44then I'm simply going to do a log here.
  2591. 1:36:47And I'll do a log. And I'll do uh four.
  2592. 1:36:50Let's give four because we have this on
  2593. 1:36:52uh third. So I'll do four. And I'll do
  2594. 1:36:54clear timeout.
  2595. 1:36:57Cancelled
  2596. 1:36:59the 2C timer means it will cancel this
  2597. 1:37:02particular timer. This will never run.
  2598. 1:37:04Okay, we are immediately canceling this.
  2599. 1:37:06So that mean this timer is going to
  2600. 1:37:08cancel before it will even get chance to
  2601. 1:37:10run. Okay. So this is why whenever you
  2602. 1:37:12need to clear some timer if you're using
  2603. 1:37:15set timeout you can use clear time out
  2604. 1:37:16and you have to pass that okay that
  2605. 1:37:18timer okay here which is your set time
  2606. 1:37:21out that you are keeping in a variable.
  2607. 1:37:23Okay just try to explain this one in a
  2608. 1:37:25simpler way and then let's run it here
  2609. 1:37:28and then we can understand this one very
  2610. 1:37:29very simple way. So save this
  2611. 1:37:32and I'll just clear and then run once
  2612. 1:37:36again.
  2613. 1:37:37Okay. So you see that set timeout
  2614. 1:37:39example started this run immediately
  2615. 1:37:41node doesn't wait clear timeout
  2616. 1:37:43cancelled 2 second timer means here you
  2617. 1:37:48can see I already mentioned that clear
  2618. 1:37:50timeout is going to cancel this
  2619. 1:37:51particular timer here okay and this
  2620. 1:37:53message will not run if you wanted to
  2621. 1:37:56check this just remove this save this
  2622. 1:37:59and clear once again and you'll see that
  2623. 1:38:01what will happen
  2624. 1:38:04okay this message will not run now we
  2625. 1:38:06are getting this But or else you can do
  2626. 1:38:08that this message will run after this
  2627. 1:38:11message will run after 2 seconds. I'm
  2628. 1:38:12just giving example. Okay. So 2 seconds
  2629. 1:38:14something like this. Save this. And then
  2630. 1:38:17here I'll do this one. Let's run once
  2631. 1:38:19again.
  2632. 1:38:22Okay. So that means here clear timeout
  2633. 1:38:24is the respon is actually responsible so
  2634. 1:38:27that this part will not run. So here
  2635. 1:38:29I'll just go revert this. So this one
  2636. 1:38:31will not run.
  2637. 1:38:35Okay. So this will never run. So this
  2638. 1:38:37will be how you can use clear timeout.
  2639. 1:38:40Uh what else is left? Okay. So let's
  2640. 1:38:42see. Uh set interval. Okay. Set interval
  2641. 1:38:44and set timeout is actually different.
  2642. 1:38:47Set interval is going to run the same
  2643. 1:38:49call back again and again after a
  2644. 1:38:51certain time after a fixed delay. So set
  2645. 1:38:54interval is going to is going to
  2646. 1:38:58run the call back
  2647. 1:39:01again and again after the fixed delay.
  2648. 1:39:07Okay, very important and this I think I
  2649. 1:39:09don't have to explain here also these
  2650. 1:39:10can be extremely important interview
  2651. 1:39:12questions also right so what is the
  2652. 1:39:14difference between set timeout and set
  2653. 1:39:16interval mostly you're going to get this
  2654. 1:39:17even they will not ask set immediate but
  2655. 1:39:19definitely you are going to get if
  2656. 1:39:21you're giving for beginner level of
  2657. 1:39:22interview questions right so I'll do
  2658. 1:39:24function and I'll do run set
  2659. 1:39:28interval example
  2660. 1:39:32and here we'll do void so first I'm just
  2661. 1:39:34going to take a count okay see how we
  2662. 1:39:36doing this. So I'll do do a count and
  2663. 1:39:38I'm just going to take an interval ID.
  2664. 1:39:40So I'll do interval
  2665. 1:39:42id here. What you have to do? You have
  2666. 1:39:44to take a set interval. Set interval
  2667. 1:39:46you're going to take this is also going
  2668. 1:39:48to take a call back. And here what I'm
  2669. 1:39:50doing nothing. We are just simply doing
  2670. 1:39:52count ++. Okay. Just notice what you're
  2671. 1:39:55doing. You have to understand each and
  2672. 1:39:56every line. And then I'm simply doing
  2673. 1:39:58I'm simply doing a log here. And let's
  2674. 1:40:01do this one as maybe uh last we have
  2675. 1:40:03given four, right? So let's give five
  2676. 1:40:05and I'll do set interval.
  2677. 1:40:09Set interval tick and here you're going
  2678. 1:40:13to give the count. Okay. And then what
  2679. 1:40:16I'll do right if see what you're doing
  2680. 1:40:19if the count is equal to three means the
  2681. 1:40:21moment count will become three. So in
  2682. 1:40:24that case what you're going to do you're
  2683. 1:40:25going to clear this interval. So how
  2684. 1:40:27you're going to clear the interval? Just
  2685. 1:40:28like we have done clear time out to
  2686. 1:40:30clear a interval you're going to do
  2687. 1:40:31clear interval that okay now I I don't
  2688. 1:40:33need this interval anymore but if you
  2689. 1:40:35wanted to do this you have to pass the
  2690. 1:40:37time out timeout is your interval ID so
  2691. 1:40:39that means you have to pass this
  2692. 1:40:40interval ID or else node doesn't know
  2693. 1:40:42that which interval or which timer you
  2694. 1:40:45wanted to close or you wanted to clear
  2695. 1:40:47okay and then you're simply going to do
  2696. 1:40:49one more log here and let's give six and
  2697. 1:40:51I'll do set interval stopped so I'll do
  2698. 1:40:54set interval stopped
  2699. 1:40:57how we have to give this man. So I'll do
  2700. 1:41:00six set interval stopped. That's it. Now
  2701. 1:41:04I think okay this part okay here I'm
  2702. 1:41:07just going to give maybe let's give this
  2703. 1:41:08one a 500 uh millisecond or.5 second. So
  2704. 1:41:12now we save this and then we going to
  2705. 1:41:14run this set um time interval example
  2706. 1:41:18and I'm just going to comment this one
  2707. 1:41:20okay so that you'll not get confused.
  2708. 1:41:22Save this. Now let's go here clear and
  2709. 1:41:26let's run one second.
  2710. 1:41:29That's it. So 1 2 3 stopped. So every
  2711. 1:41:345 seconds if you do this one let's say
  2712. 1:41:371.5. Okay. So see what will happen. So
  2713. 1:41:39it will take some more time but the
  2714. 1:41:41result will be same. Okay. So let's see.
  2715. 1:41:43So 1
  2716. 1:41:462 3 and it will stop. So that means
  2717. 1:41:50every 1.5 seconds this particular
  2718. 1:41:53interval will run. It will increase the
  2719. 1:41:55count. The moment it will become three,
  2720. 1:41:57it's going to clear the interval and
  2721. 1:41:58you're going you are going to get this.
  2722. 1:42:00As I already mentioned, I'm going to
  2723. 1:42:01make things very simple and see that if
  2724. 1:42:03you do this on multiple times, do the
  2725. 1:42:05same example even multiple times, you'll
  2726. 1:42:07see that you'll be able to do any kind
  2727. 1:42:08of example. Whatever project you're
  2728. 1:42:10going to have, you're not going to have
  2729. 1:42:11any kind of difficulty that what
  2730. 1:42:13actually you're going to do and then you
  2731. 1:42:15are going to have one more. Right? So,
  2732. 1:42:16I'm just going to comment it here. Save
  2733. 1:42:18this. So, let's take one more which is
  2734. 1:42:20called set immediate. Now the difference
  2735. 1:42:22is set immediate is going to run a
  2736. 1:42:25particular call back after the current
  2737. 1:42:27synchronous code finishes. Okay. Now set
  2738. 1:42:30immediate is not going to wait when you
  2739. 1:42:33have something called set time out. So
  2740. 1:42:35it's not going to wait for a fixed time
  2741. 1:42:36like set time out. So it's going to it's
  2742. 1:42:39it's something like this. So it will say
  2743. 1:42:41that I'm going to run as soon as the
  2744. 1:42:44current script is done. So current
  2745. 1:42:45synchronous code is done. Okay. So if
  2746. 1:42:47you go here just do this one that what
  2747. 1:42:49is set immediate right so you have set
  2748. 1:42:52immediate
  2749. 1:42:54what's set time out see here it will say
  2750. 1:42:57that set immediate is going to run after
  2751. 1:42:58current current runs immediately after
  2752. 1:43:01the current IO polling phase finishes
  2753. 1:43:04it's giving the term difficult like for
  2754. 1:43:07beginner it will be difficult to
  2755. 1:43:08understand so it's simply telling that
  2756. 1:43:10okay I'm going to run this particular
  2757. 1:43:12call back after my current synchronous
  2758. 1:43:14code finishes immediately but set
  2759. 1:43:15timeout is going to do what it's going
  2760. 1:43:17to wait for that time that if you're
  2761. 1:43:18giving 5 second timer it's going to run
  2762. 1:43:20after 5 seconds only. So that is the
  2763. 1:43:22difference. Okay. So now let's see what
  2764. 1:43:24is happening. So I'm just going to do
  2765. 1:43:26function and I'll do set im uh immediate
  2766. 1:43:29right. So I'll do set immediate example
  2767. 1:43:32and let's give this one a void.
  2768. 1:43:35So I'll do set immediate.
  2769. 1:43:39And now uh I'm just going to do here log
  2770. 1:43:42this.
  2771. 1:43:45So last we have six. So I'll do seven.
  2772. 1:43:49So I'll do here set immediate
  2773. 1:43:54call back. Okay. And then I'm simply
  2774. 1:43:57going to do log and I am going to do a
  2775. 1:44:01let's say synchronous
  2776. 1:44:04synchronous. I need to take the
  2777. 1:44:06spelling. Okay. You don't have a
  2778. 1:44:08synchronous here.
  2779. 1:44:10I think S Y N C H R O N you know US I
  2780. 1:44:15think this is correct. I don't know.
  2781. 1:44:17Okay. So the synchronous code after set
  2782. 1:44:19immediate.
  2783. 1:44:22Okay. So now let's run this and we'll
  2784. 1:44:26run all of this. Okay. We going to run
  2785. 1:44:28this also. And let's run all these also.
  2786. 1:44:31And I'm going to make this one 1,000.
  2787. 1:44:34Let's save this. And let's run once
  2788. 1:44:36again all of this and we'll see what is
  2789. 1:44:38happening.
  2790. 1:44:43All right. Set timeout example started.
  2791. 1:44:45This runs immediately. Node doesn't
  2792. 1:44:47wait. So that means makes sense. So this
  2793. 1:44:50is going to run. This is going to run
  2794. 1:44:52immediately. Okay. Next you're getting
  2795. 1:44:55clear timeout cancel this one 2 second
  2796. 1:44:57later. This part and then if you notice
  2797. 1:45:01one thing very very important. See this
  2798. 1:45:03part right? So this one inside set
  2799. 1:45:05timeout you see the uh where it is
  2800. 1:45:08running. It's running here but before
  2801. 1:45:10that synchronous code after set
  2802. 1:45:12immediate means this is actually
  2803. 1:45:13finished means all your synchronous code
  2804. 1:45:15already ran and then it will run
  2805. 1:45:17immediately. See that is the reason
  2806. 1:45:19after the synchronous code after set
  2807. 1:45:20immediate you're getting the set
  2808. 1:45:22immediate call back. So now this will
  2809. 1:45:24already satisfies what I mentioned right
  2810. 1:45:26that set immediate is going to run once
  2811. 1:45:28all your current synchronous code
  2812. 1:45:30finishes. So once this will run right so
  2813. 1:45:32you're getting set immediate call back
  2814. 1:45:33now the set timeout will run. So set
  2815. 1:45:36time out means this one will run you're
  2816. 1:45:37getting this will run after 1 second
  2817. 1:45:39correct and then you're getting your set
  2818. 1:45:41immediate sorry set interval. So set
  2819. 1:45:43interval means you're getting count one,
  2820. 1:45:45count two, count three which is this and
  2821. 1:45:49then set uh interval stopped and it
  2822. 1:45:51makes sense right. So this will be the
  2823. 1:45:53whole flow. Okay. So this will be the
  2824. 1:45:55difference. Now you can also do like
  2825. 1:45:57this right? You can also uh implement a
  2826. 1:46:00promise based timer. Okay. So let's see
  2827. 1:46:02that also. I thought I'm not going to
  2828. 1:46:04cover but let's do this one. So I'll do
  2829. 1:46:05let's say I have a async function and
  2830. 1:46:08I'm just going to do run promise
  2831. 1:46:12promise timer
  2832. 1:46:15example
  2833. 1:46:16and here this is going to return a
  2834. 1:46:19promise and this will be void. So how we
  2835. 1:46:22going to do this? I'm just going to do a
  2836. 1:46:23log and I'm just going to do a line and
  2837. 1:46:26let's do this one as waiting for promise
  2838. 1:46:31best timer.
  2839. 1:46:33What you can do that let's say you don't
  2840. 1:46:35want to use set time. What you can do?
  2841. 1:46:37You can use something called slip. So
  2842. 1:46:39from where you're going to import this.
  2843. 1:46:41So let's go to the top and then I'm
  2844. 1:46:43going to import something from this
  2845. 1:46:45particular package. So node and then you
  2846. 1:46:48have something called timers. And then
  2847. 1:46:50you see you're getting promises. We'll
  2848. 1:46:51do / promises. And here you're going to
  2849. 1:46:53get a set time out.
  2850. 1:46:55And then let's say I just wanted to do
  2851. 1:46:57as slip something like this.
  2852. 1:47:01Okay. And this one you're going to use
  2853. 1:47:03here. So what I can do let's say
  2854. 1:47:05directly I can use like this this is
  2855. 1:47:07running and then I can do a because we
  2856. 1:47:08have a sync and I can use this slip and
  2857. 1:47:11I can give this 1,000 whatever time you
  2858. 1:47:13wanted to give the delay okay and then
  2859. 1:47:16you can simply just do console.log log
  2860. 1:47:18and then we'll do dot promise
  2861. 1:47:23promise based timer
  2862. 1:47:27finishes
  2863. 1:47:29after 1.5 seconds something like this
  2864. 1:47:32okay or maybe let's wanted to give 5
  2865. 1:47:34seconds just to check so let's give 5.5
  2866. 1:47:37seconds that's it and then how you can
  2867. 1:47:39run this you just can't directly run
  2868. 1:47:40like this okay this is the thing you
  2869. 1:47:42have to understand so this is a promise
  2870. 1:47:44and this is going to give a catch and
  2871. 1:47:47Here I'll do error. Let's do unknown
  2872. 1:47:50because we don't know what is the error.
  2873. 1:47:52Okay. What I'm doing wrong? Okay.
  2874. 1:47:58And then uh let's do error here
  2875. 1:48:02and I'll do timer
  2876. 1:48:05demo field.
  2877. 1:48:07And here I'm going to pass the error. So
  2878. 1:48:10now save this and let's run all of this
  2879. 1:48:12once again and you'll see clearly what
  2880. 1:48:14is happening. Enter.
  2881. 1:48:18three it will stop and see that it will
  2882. 1:48:20wait 5 seconds and then it's going to
  2883. 1:48:23run okay so promise base timer runs
  2884. 1:48:26after 1.5 seconds I think I've given
  2885. 1:48:28this one change okay change this one to
  2886. 1:48:30not 1.5 5.5 okay so I think this is done
  2887. 1:48:33also you can change this one let's say
  2888. 1:48:35if I change this one to 500 or 1,500
  2889. 1:48:40let's see what will happen so save this
  2890. 1:48:44and if you clear this and run once Once
  2891. 1:48:46again
  2892. 1:48:48see it run here and then set time out
  2893. 1:48:51set interval this will can differ like
  2894. 1:48:54maybe let's say it will run and then
  2895. 1:48:55this promise based uh this timer will
  2896. 1:48:57run but at the concept will remain same.
  2897. 1:48:59So I hope that this will give a clear
  2898. 1:49:01picture that when set timeout will run
  2899. 1:49:03when you're going to use set timeout
  2900. 1:49:04when you're going to use set interval
  2901. 1:49:06what is the difference between set
  2902. 1:49:07timeout and set immediate and what is
  2903. 1:49:10promise based timer. So with this now
  2904. 1:49:12let's move on to the next one. Okay, so
  2905. 1:49:14moving to the next one. One of the
  2906. 1:49:16almost I will say this one is one of the
  2907. 1:49:18most important concept that everyone
  2908. 1:49:19should master and that is call back
  2909. 1:49:22promises and as I'm pretty much sure
  2910. 1:49:24some of you already know this one but
  2911. 1:49:25still I will suggest don't skip this
  2912. 1:49:27because there is a possibility that you
  2913. 1:49:29are going to get lot of interview
  2914. 1:49:30queries from this particular section.
  2915. 1:49:32Okay and this is really important. Uh so
  2916. 1:49:35we will create so let's do call back
  2917. 1:49:39promises and async right so we have all
  2918. 1:49:42these uh let's give asynav module ts
  2919. 1:49:47okay so first of all let's do one thing
  2920. 1:49:49let's take a type and we'll make this
  2921. 1:49:51one as practical as possible and this
  2922. 1:49:54user okay so this is a type of user and
  2923. 1:49:57you're doing a API call and this user is
  2924. 1:49:59going to have a ID number it's going to
  2925. 1:50:01have a name let's say take this one as a
  2926. 1:50:03string and let's say this also will have
  2927. 1:50:05a role. This role can be either this
  2928. 1:50:07will be a student or
  2929. 1:50:10let's say normal user or there will be
  2930. 1:50:11super admin.
  2931. 1:50:13You can make this one as super admin
  2932. 1:50:14also. Let's do this one as Z user.
  2933. 1:50:17So now I'm simply going to do con users
  2934. 1:50:20and type will be this user of array. So
  2935. 1:50:23we're going to have arrow of users
  2936. 1:50:24correct. So you're going to have ID
  2937. 1:50:26which will be one. Sorry, this will be
  2938. 1:50:28number. You're going to have a name.
  2939. 1:50:29Let's take this one as my name. And then
  2940. 1:50:31I'm simply going to take a role and this
  2941. 1:50:34role will be
  2942. 1:50:36so let's take this one as super admin.
  2943. 1:50:38I'll take one more user or let's say two
  2944. 1:50:40more user. Okay. So I'll do ID2. Let's
  2945. 1:50:43do John and let's make this one as user.
  2946. 1:50:46Okay. And then I'll just paste this one.
  2947. 1:50:50So, id three. Uh let's do I don't know
  2948. 1:50:53what uh I uh John or uh let's do maybe
  2949. 1:51:00Roman
  2950. 1:51:02and let's keep this one as user.
  2951. 1:51:05So now what you have to do right the
  2952. 1:51:07task is uh your interviewer or whatever
  2953. 1:51:10you're doing your task is you have to
  2954. 1:51:12actually fetch the list of user or let's
  2955. 1:51:14say you have to fetch a single user. You
  2956. 1:51:16have to fetch a single user using
  2957. 1:51:19callback style using promises using
  2958. 1:51:22asynchron and both will be different.
  2959. 1:51:24Okay. Now this is why this is very
  2960. 1:51:26important because callbacks are one of
  2961. 1:51:28the most important features that you are
  2962. 1:51:30going to use in almost all the places
  2963. 1:51:32and whatever packages even you are using
  2964. 1:51:34you'll see that there will be uses of
  2965. 1:51:36callbacks. Okay. So first what we are
  2966. 1:51:38going to do right uh in this case. So
  2967. 1:51:41first let's do one thing. Let's start
  2968. 1:51:43with the uh with the call back example.
  2969. 1:51:46What actually is a call back? Now that
  2970. 1:51:47is one of the most important thing. See
  2971. 1:51:49call back is nothing but in a simple way
  2972. 1:51:51call back is a function that you're
  2973. 1:51:54going to pass into another function. Now
  2974. 1:51:56this is the proper definition. So call
  2975. 1:51:57back is itself it's a function. Okay.
  2976. 1:52:00Callback is a function and this function
  2977. 1:52:04this function you are passing to a
  2978. 1:52:08different function
  2979. 1:52:10as a parameter something like this right
  2980. 1:52:13so now what will happen right so in this
  2981. 1:52:15case in when you're implementing this
  2982. 1:52:18one let's say in a nodej project the
  2983. 1:52:20classic nodej pattern is like this so
  2984. 1:52:22you have your call back okay this call
  2985. 1:52:25back either is going to have a error and
  2986. 1:52:27this is or else this is going to have a
  2987. 1:52:29result this This is probably one of the
  2988. 1:52:31most important concept that you should
  2989. 1:52:33know. Okay, I'm really uh explaining all
  2990. 1:52:35of these because whatever you're going
  2991. 1:52:36to implement let's say you're
  2992. 1:52:37implementing cloudinary file upload and
  2993. 1:52:39you'll see that there is structure the
  2994. 1:52:40package structure that whenever you're
  2995. 1:52:42going to use this you'll see that there
  2996. 1:52:43will be a call back let's say and there
  2997. 1:52:45can be a error. So if there is a error
  2998. 1:52:47you're going to reject if there is a
  2999. 1:52:49success you're going to get the result.
  3000. 1:52:50So this is a classic NodeJS call back
  3001. 1:52:53pattern. Okay. So this is
  3002. 1:52:57this is a classic NodeJS
  3003. 1:53:01pattern. So this is how it will work. So
  3004. 1:53:03look call back pattern
  3005. 1:53:06how this will work. So let's say I'm
  3006. 1:53:08just going to create a function and I'm
  3007. 1:53:10not sure see I can guarantee so this is
  3008. 1:53:13the challenge pause the video and try to
  3009. 1:53:15implement what you have to do right
  3010. 1:53:16let's say you are going to create a
  3011. 1:53:19function that function is going to
  3012. 1:53:20receive a call back and that function
  3013. 1:53:22also is going to receive a user ID so
  3014. 1:53:25let's see if I'm going to pass the user
  3015. 1:53:26ID to so it's going to give me this user
  3016. 1:53:29so let's see if you can do it without
  3017. 1:53:31watching this part okay then you can
  3018. 1:53:33resume and you'll see that you will some
  3019. 1:53:35of you will find this difficulty Because
  3020. 1:53:37implementing using pure call back let's
  3021. 1:53:40say you're giving interview is very
  3022. 1:53:41tricky using asyn you can do it but
  3023. 1:53:45check whether you can do it using call
  3024. 1:53:46back or not you're getting so now let's
  3025. 1:53:48I'm just going to do find user and I
  3026. 1:53:51will give you one uh practical
  3027. 1:53:53experience okay this is not just about
  3028. 1:53:55teaching actually giving proper
  3029. 1:53:56experience uh that I faced from uh in
  3030. 1:54:00past I've given more than 100 plus
  3031. 1:54:02interviews and there is a 100% I
  3032. 1:54:04remember that I actually got this call
  3033. 1:54:07back and promise related example in more
  3034. 1:54:09than 25 to 30 interviews I think even
  3035. 1:54:13more okay so there is a huge possibility
  3036. 1:54:15that you're going to get questions from
  3037. 1:54:16promises implement custom promise you're
  3038. 1:54:19not going to use the native promise
  3039. 1:54:21features you're going to implement
  3040. 1:54:23promise chain all of these you are going
  3041. 1:54:26to fetch API call using callback you're
  3042. 1:54:28not going to use any promises or async
  3043. 1:54:30so these things are very important so to
  3044. 1:54:32find user and actually I'm telling all
  3045. 1:54:34of these for those if you're currently
  3046. 1:54:36Let's say in college, third year, second
  3047. 1:54:38year, fourth year or let's say you have
  3048. 1:54:39a less than five years of experience.
  3049. 1:54:41Okay, consider five years of experience
  3050. 1:54:42and you're preparing for interview. This
  3051. 1:54:44can be very important all these things.
  3052. 1:54:47Uh so this will be a function. So now
  3053. 1:54:50here what I mentioned that this is going
  3054. 1:54:51to receive a user ID. So this will be my
  3055. 1:54:54user ID which will be my uh what is that
  3056. 1:54:56input and then I'm going to receive a
  3057. 1:54:58call back. Okay, this is the my call
  3058. 1:55:00back function. I already mentioned the
  3059. 1:55:02call back is itself it's a function that
  3060. 1:55:04we are passing in another function. So
  3061. 1:55:06this is my another function I'm passing
  3062. 1:55:07call back as a input and here this
  3063. 1:55:10callback is going to have either there
  3064. 1:55:12can be error which will be either error
  3065. 1:55:15or it can be null okay and then it's
  3066. 1:55:19going to give me a user okay so it's
  3067. 1:55:20going to give me user information so
  3068. 1:55:23here what I'm going to do right so this
  3069. 1:55:25user will be my this user so I have this
  3070. 1:55:27user type correct so this is what it's
  3071. 1:55:30going to receive now you see this
  3072. 1:55:32pattern this is matching either it's
  3073. 1:55:34going to give me error or it's going to
  3074. 1:55:35give give me result okay and then return
  3075. 1:55:38type will be void that's it
  3076. 1:55:42return type so this function return type
  3077. 1:55:43will be this will be void and here I'm
  3078. 1:55:45simply going to take a set time out now
  3079. 1:55:48see whatever we have learned so far in
  3080. 1:55:50the previous part we are applying all
  3081. 1:55:51these things and here what we can do
  3082. 1:55:53right let's take maybe what we can do
  3083. 1:55:57let's take 0.5 second now what I'm going
  3084. 1:56:00to do right you are actually doing API
  3085. 1:56:03call think you are doing a actual API
  3086. 1:56:05got something like this right normally
  3087. 1:56:07you are not going to use callback okay
  3088. 1:56:08you are mostly going to use async cover
  3089. 1:56:10promise but again you should know all of
  3090. 1:56:12this and this is where you're going to
  3091. 1:56:13do okay I have my user so I'll do users
  3092. 1:56:16dotfind
  3093. 1:56:19so this user is nothing but these users
  3094. 1:56:21you're going to have so do users do find
  3095. 1:56:24so do current user
  3096. 1:56:27and we do current user do ID will be
  3097. 1:56:30equal to equal to this user ID that
  3098. 1:56:31you're going to receive this one so Now
  3099. 1:56:35this is the most important part. What
  3100. 1:56:37will happen if you don't get this user?
  3101. 1:56:40Consider that I'm passing user ID 100
  3102. 1:56:42and that user ID 100 is not present. So
  3103. 1:56:45what will happen? So this user is not
  3104. 1:56:47found. Correct? So this is error. So if
  3105. 1:56:50the user is not found, you need to call
  3106. 1:56:52this call back. Now this is where
  3107. 1:56:54student find it uh this one difficulty.
  3108. 1:56:56So you have to call this callback and
  3109. 1:56:58what you have to you have to pass error.
  3110. 1:57:00So here you are going to simply pass
  3111. 1:57:01okay you have to pass a new error and
  3112. 1:57:04then I'm simply going to take user with
  3113. 1:57:06ID this user ID is not present or not
  3114. 1:57:10found you're getting that's it and then
  3115. 1:57:13what you have to do you have to return
  3116. 1:57:14from here immediately so you have to
  3117. 1:57:16return from this that's it if this is
  3118. 1:57:20present so that means you are getting
  3119. 1:57:21result here so let's say you're passing
  3120. 1:57:23user ID3 so you're going to get what ID
  3121. 1:57:263 name Roman role user so in this case
  3122. 1:57:29you're going to all the call back what
  3123. 1:57:30will be error error will be null because
  3124. 1:57:33you don't have any error and what will
  3125. 1:57:36be result will be user so this will be
  3126. 1:57:39your result now you see that how much
  3127. 1:57:40easier it is to understand right so this
  3128. 1:57:43is your uh function where you're passing
  3129. 1:57:46call back as a input consider you
  3130. 1:57:48passing in a different function and then
  3131. 1:57:50you're simply checking we are doing
  3132. 1:57:51something here you don't know what we
  3133. 1:57:52are doing something we are doing if we
  3134. 1:57:54are not getting result we are calling
  3135. 1:57:55the call back passing the error if we
  3136. 1:57:58are getting result
  3137. 1:57:59passing the error as null and user
  3138. 1:58:02information we are passing but how we
  3139. 1:58:03are going to call this to call this you
  3140. 1:58:05can simply just do right so to find user
  3141. 1:58:07with call back we're going to pass a
  3142. 1:58:10user ID let's say I'm going to pass
  3143. 1:58:11three and here what you have to do you
  3144. 1:58:13have to pass a call back function see
  3145. 1:58:15it's call back is going to receive error
  3146. 1:58:17or it's going to receive user correct so
  3147. 1:58:19here what you have to pass you to pass
  3148. 1:58:21error
  3149. 1:58:23and you're going to pass user and now
  3150. 1:58:26what you're going to get if error you
  3151. 1:58:28are getting so let's say we're passing
  3152. 1:58:29error here right so if we are getting
  3153. 1:58:31error
  3154. 1:58:34so I'm simply going to log and I'll do
  3155. 1:58:37call back
  3156. 1:58:39error and you're going to pass error dot
  3157. 1:58:42message that's it right and what you
  3158. 1:58:44have to do again you have to return from
  3159. 1:58:46here if this is not the case so you're
  3160. 1:58:49going to do what you're going to log so
  3161. 1:58:50you're going to get call back result so
  3162. 1:58:53what will be your call back result so
  3163. 1:58:55this user this user is going to give
  3164. 1:58:58give you what ID. This user is going to
  3165. 1:59:00give you the name and this user is going
  3166. 1:59:02to give you the role. That's it. So if
  3167. 1:59:05you save this now, let's I'm going to
  3168. 1:59:07run this here.
  3169. 1:59:10Uh let's run copy this.
  3170. 1:59:18Okay. So do tsx
  3171. 1:59:22source/ this set this and let's I'm
  3172. 1:59:25going to run npm ram 06.
  3173. 1:59:28That's it. So you're getting call back
  3174. 1:59:30result three Roman user. Why? Because we
  3175. 1:59:33are passing three. Let's I'm going to
  3176. 1:59:34pass 30. So 30 is not present. So it's
  3177. 1:59:36going to give me what error. So here if
  3178. 1:59:38I just do you see that call back uh with
  3179. 1:59:41ID 30 was not found. Call back sorry
  3180. 1:59:44user with ID 30 was not found. So user
  3181. 1:59:47with ID 30 was not found. So this is
  3182. 1:59:50calling here and returning the result.
  3183. 1:59:52It's very much simpler to understand. So
  3184. 1:59:54this is all about what call back. So now
  3185. 1:59:57the same example we're going to do using
  3186. 2:00:00promise and async in the next part. All
  3187. 2:00:02right. So now let's do the same example
  3188. 2:00:04using promise and asyncate. Okay.
  3189. 2:00:06Because at the end you have to remember
  3190. 2:00:08that when you're going to implement your
  3191. 2:00:09project, you will be using mostly async
  3192. 2:00:11because of the syntax. It's much easier
  3193. 2:00:13to uh use. But again we need to
  3194. 2:00:16understand this call back that we have
  3195. 2:00:18call back and then we have something
  3196. 2:00:20called call back hill. I'll not go into
  3197. 2:00:22that much details and that is the reason
  3198. 2:00:24we have something called promises and
  3199. 2:00:25from promises we have something called
  3200. 2:00:27async. Okay. So first let's create one
  3201. 2:00:30more function and let's try to implement
  3202. 2:00:33this also. So fetch user with promise.
  3203. 2:00:37So what this is going to receive? It's
  3204. 2:00:38going to receive the user ID. Again user
  3205. 2:00:40ID will be type number. And what will be
  3206. 2:00:43the return type? See remember the return
  3207. 2:00:45type you have to be very very careful
  3208. 2:00:46when you're going to use promises. So
  3209. 2:00:48you have to return a promise. Okay, this
  3210. 2:00:50is going to return a promise and the
  3211. 2:00:51type of this promise will be user. So
  3212. 2:00:54that means we are actually at the end
  3213. 2:00:57returning a user information that user
  3214. 2:01:00will have a ID, name and role. Okay. So
  3215. 2:01:02this is the important line whatever we
  3216. 2:01:04have done here because what will happen
  3217. 2:01:07until unless you're going to return that
  3218. 2:01:08you'll see that you are going to get
  3219. 2:01:10error because you will not return. So
  3220. 2:01:12let's say you return a string value,
  3221. 2:01:14you're going to get error. Okay, because
  3222. 2:01:15you need to return in this certain
  3223. 2:01:17format only. So now what will be the
  3224. 2:01:20next step? Uh these see what I I've seen
  3225. 2:01:24from my own experience right or
  3226. 2:01:26professional experience or giving
  3227. 2:01:28interview that interviewers will not ask
  3228. 2:01:30you more questions on as they can ask
  3229. 2:01:32you but in the implementation they are
  3230. 2:01:34going to ask you that okay you implement
  3231. 2:01:36this one using promises. Why I'm going
  3232. 2:01:38to tell you because promises you need to
  3233. 2:01:40first see first of all to understand
  3234. 2:01:42asyn better you need to know promises
  3235. 2:01:44very well to understand promises very
  3236. 2:01:47well you need to know call backs very
  3237. 2:01:48well so from callbacks and promises only
  3238. 2:01:51you are going to get questions in
  3239. 2:01:52interview I can 100% guarantee and
  3240. 2:01:55sometimes they will go little like
  3241. 2:01:58advanc level for example let's say they
  3242. 2:01:59can simply give you that okay create a
  3243. 2:02:01promise polyfield or create a promise
  3244. 2:02:03all polyfield but don't use any native
  3245. 2:02:05promise features okay so understanding
  3246. 2:02:08promises. So for example, let's say you
  3247. 2:02:09are actually returning promise here. So
  3248. 2:02:11how you going to return a promise? You
  3249. 2:02:13need to know that okay, I'm going to
  3250. 2:02:14return a promise. So this will be my new
  3251. 2:02:16promise. Something like this. Promise is
  3252. 2:02:18going to have a resolve and a reject
  3253. 2:02:21property.
  3254. 2:02:22And this now here what you can do that
  3255. 2:02:25you can simply take a set time out.
  3256. 2:02:26Let's I'm just going to take a set time
  3257. 2:02:27out here because we're not doing actual
  3258. 2:02:29API call here. And now here let's give
  3259. 2:02:32this one as 1,000 milliseconds. And
  3260. 2:02:35inside this we can do like that, right?
  3261. 2:02:37So this line only we can copy. Now what
  3262. 2:02:40is the important part? Important part is
  3263. 2:02:42what will happen if this user is not
  3264. 2:02:43found. Now in this case now let's see
  3265. 2:02:46user is not found. What you have to do
  3266. 2:02:48you you don't have any callable right?
  3267. 2:02:50So you need to reject. So that is the
  3268. 2:02:52reason you have something called reject.
  3269. 2:02:53So promise will give you this reject uh
  3270. 2:02:56here functionality and here you can
  3271. 2:02:58actually reject and throw one error.
  3272. 2:03:00Okay that this particular user ID is not
  3273. 2:03:03present. user with this ID data was not
  3274. 2:03:08found. Uh let's do this one. This will
  3275. 2:03:11be user ID. Correct? So we have user ID.
  3276. 2:03:14This one this is the case. Now again you
  3277. 2:03:17have to return. But if this is not the
  3278. 2:03:19case that means you are actually getting
  3279. 2:03:20the result. So in that case you have to
  3280. 2:03:22resolve it. Means now I already have the
  3281. 2:03:24result. So I'm going to resolve the
  3282. 2:03:25result and the type of result will be
  3283. 2:03:27user. That's it. And this is done. Okay.
  3284. 2:03:31So this will be how we are going to
  3285. 2:03:33write using promises. Now how we going
  3286. 2:03:35to call this one? Okay. Now see calling
  3287. 2:03:38it's very simple. So what we can do
  3288. 2:03:39right you can just comment out this one
  3289. 2:03:41for now. And now let's say after this I
  3290. 2:03:44simply do find uh user
  3291. 2:03:47what name I have given fetch user I have
  3292. 2:03:50given right. So let's do find user. Find
  3293. 2:03:52user with promise. So I'll do find user.
  3294. 2:03:57Find user with promise. We have to pass
  3295. 2:03:59the user ID. So let's give I'll give the
  3296. 2:04:00user ID one and then what this will
  3297. 2:04:02give. So this is going to give me dot
  3298. 2:04:04then dot then simply means once promise
  3299. 2:04:08will run promise will either resolve the
  3300. 2:04:11value or promise is going to reject the
  3301. 2:04:12value. If promise resolve the value. So
  3302. 2:04:14in that case we're going to get that
  3303. 2:04:16value in our then block. If promise is
  3304. 2:04:19going to reject the value we are going
  3305. 2:04:20to catch that particular error. So in
  3306. 2:04:22this case in the resolve uh block what
  3307. 2:04:26we'll be getting in the resolve block we
  3308. 2:04:27are going to get the user information
  3309. 2:04:29because the user information we are
  3310. 2:04:30sending. So in this case we're going to
  3311. 2:04:32get the user here and then you can
  3312. 2:04:34simply just do like this. So here I
  3313. 2:04:37we'll go here. Let's say I'm just going
  3314. 2:04:39to log this only. Copy this and paste it
  3315. 2:04:43here. And let's make this one as promise
  3316. 2:04:45result. Okay. And if there is any error
  3317. 2:04:48so we'll do catch and this will be my
  3318. 2:04:51error
  3319. 2:04:52type will be error and here we are going
  3320. 2:04:55to simply tell okay that means promise
  3321. 2:04:57is actually catching some error and we
  3322. 2:05:00don't know what is the error is so I'm
  3323. 2:05:01just going to do promise error so this
  3324. 2:05:03will give me error dot message that's it
  3325. 2:05:06so now let's save this and then we going
  3326. 2:05:08to run the same thing and we'll see what
  3327. 2:05:10is happening here so let's clear first
  3328. 2:05:12run
  3329. 2:05:14okay so you're getting promise result
  3330. 2:05:15one sum super admin that is correct what
  3331. 2:05:18will happen if I give here 100 right now
  3332. 2:05:20remember this 100 is not present so it's
  3333. 2:05:22going to throw error correct so if you
  3334. 2:05:24go here so you'll see that we are
  3335. 2:05:26getting promise error user with ID sorry
  3336. 2:05:29user with 100 data means this ID data
  3337. 2:05:32was not found so this is how we going to
  3338. 2:05:34write the same thing using promises
  3339. 2:05:36correct now let's keep keep this one
  3340. 2:05:38like this now what how we going to do
  3341. 2:05:40using async async is actually simple so
  3342. 2:05:43here first of all I'm going to take
  3343. 2:05:44async now remember you have to know also
  3344. 2:05:46So that async it actually built on top
  3345. 2:05:48of promises only. So it's like a
  3346. 2:05:50promises. The reason we use as because
  3347. 2:05:52it's much more easier syntax wise and
  3348. 2:05:54it's also easily you can read it. Okay.
  3349. 2:05:57So let's say you have written some
  3350. 2:05:58particular code and other developer is
  3351. 2:06:00reading your code. They will be able to
  3352. 2:06:02understand it's not like they they will
  3353. 2:06:04not be able to understand promise code
  3354. 2:06:05but it's like much more cleaner. Okay.
  3355. 2:06:07That is the main reason. Uh so here so
  3356. 2:06:11first of all you have to do async. So
  3357. 2:06:13this will be function and I'll do find
  3358. 2:06:16user with async a now. So I'll do async
  3359. 2:06:19a. So again this is going to receive the
  3360. 2:06:22user id which will be number what this
  3361. 2:06:25is going to return is will be promise
  3362. 2:06:28and here I'm for now I'm just going to
  3363. 2:06:30do void and then what you have to do you
  3364. 2:06:32always have to take a try and catch
  3365. 2:06:34block right. So if something goes wrong
  3366. 2:06:36your catch block is going to catch that
  3367. 2:06:39particular error. So this is where
  3368. 2:06:40you're going to catch the error and in
  3369. 2:06:42the try you are going to try to get your
  3370. 2:06:45result. So in this case let's have a
  3371. 2:06:46user and I'll do a and then what you can
  3372. 2:06:49do right you can call this method only
  3373. 2:06:51find user with promise. So I can here I
  3374. 2:06:53can simply do find user with promise and
  3375. 2:06:55you can pass this user id at the end
  3376. 2:06:58this is promise only admin right uh now
  3377. 2:07:00here you can just log this I'll do log
  3378. 2:07:04and simply let's do sync ait
  3379. 2:07:10and here you can just log this one I'll
  3380. 2:07:12just do user dot name I think this that
  3381. 2:07:16is fine only that's it and here what you
  3382. 2:07:18can do right you can simp message so you
  3383. 2:07:21have to check if this error is a
  3384. 2:07:23instance of error. So in that case we're
  3385. 2:07:26going to render error dot message or
  3386. 2:07:28else I'm just going to do unknown error.
  3387. 2:07:30Okay.
  3388. 2:07:36Okay. That's it. Also you can just log
  3389. 2:07:39this here. So I'll do log. So I'll do a
  3390. 2:07:42sync error
  3391. 2:07:45and then you're going to do a message
  3392. 2:07:48here. That's it. So save this. Now what
  3393. 2:07:51you have to do you just have to call
  3394. 2:07:53this method. So you go here comment it
  3395. 2:07:55this one and then you can directly call
  3396. 2:07:57this and then you have to pass the uh
  3397. 2:07:59ID. So I pass one. So now this is going
  3398. 2:08:02to go here and it's going to check okay
  3399. 2:08:05this is async. It's going to wait for
  3400. 2:08:08this promise to run. It's going to get
  3401. 2:08:10the result. So it's going to try to get
  3402. 2:08:12the result. If the result is present
  3403. 2:08:14it's going to render the result. If
  3404. 2:08:15there is a error so it's going to catch
  3405. 2:08:17the error. And if you go here and let's
  3406. 2:08:19try to run this.
  3407. 2:08:22So you are going to get async sum. But
  3408. 2:08:25if I go here and let's say I'll give 100
  3409. 2:08:28and this is not present. So in that case
  3410. 2:08:30this will give you a error that this
  3411. 2:08:33data is not found. So this promise is
  3412. 2:08:35actually throwing error here. Right? So
  3413. 2:08:37this promise is going to reject this
  3414. 2:08:38error and this block whatever it's going
  3415. 2:08:42to catch the error here. So that's the
  3416. 2:08:44reason I check in if this instance of
  3417. 2:08:46error. So we're going to render error
  3418. 2:08:47dot message. This is actually instance
  3419. 2:08:49of error. So we are sending the error
  3420. 2:08:51here and this is getting catched here
  3421. 2:08:54and that is the reason you're getting
  3422. 2:08:55the result here. Okay. So this will be a
  3423. 2:08:59proper introduction or proper way to
  3424. 2:09:01explain that what actually call backs
  3425. 2:09:03are. Callbacks are actually most
  3426. 2:09:04important that why we need call back
  3427. 2:09:06functions and everything. Uh then you
  3428. 2:09:08have promises then you have async but at
  3429. 2:09:10the end as are nothing but it's promises
  3430. 2:09:13only. But from syntax wise it's much
  3431. 2:09:15much more easier and readable. So that
  3432. 2:09:16is the reason we use async. So with this
  3433. 2:09:18now let's move on to the next one. Okay.
  3434. 2:09:20So moving to the next one. One of the
  3435. 2:09:22most important core module that we are
  3436. 2:09:24going to understand and that is file
  3437. 2:09:25system module. Okay. And this is another
  3438. 2:09:28very very important. So let's close
  3439. 2:09:29everything.
  3440. 2:09:32Let's open the package.json.
  3441. 2:09:35Okay. So first of all we have to
  3442. 2:09:37understand that what actually file
  3443. 2:09:38system is right. So or why we need file
  3444. 2:09:41system. So first of all fs means file
  3445. 2:09:43system. Okay. Now file system is going
  3446. 2:09:46to allow you to work with files and
  3447. 2:09:48folders. So simply you think like that
  3448. 2:09:50you have to create a folder or you have
  3449. 2:09:52to write some files, you have to read
  3450. 2:09:54some files, you're going to delete some
  3451. 2:09:56files, you're going to check your file
  3452. 2:09:58information. So all those things we can
  3453. 2:10:00do using file system. This is one of the
  3454. 2:10:03uh core modules that we need to know
  3455. 2:10:05before we will start implementing our
  3456. 2:10:07rest APIs and everything. Okay. So this
  3457. 2:10:09is going to help you to create folders.
  3458. 2:10:11It's going to help you to write files.
  3459. 2:10:13It's going to help you to read files.
  3460. 2:10:16It's going to help you to check file
  3461. 2:10:18information.
  3462. 2:10:20Also going to help you to delete files.
  3463. 2:10:22So all these things you can do using
  3464. 2:10:23file system. Okay. Now in node there are
  3465. 2:10:26three ways we can do this or we can work
  3466. 2:10:28with file system. The first one is using
  3467. 2:10:30synchronous APIs. Okay. So we have first
  3468. 2:10:33sync APIs. Okay. So we have something
  3469. 2:10:36called uh so FS for file system and then
  3470. 2:10:39we have read file sync something like
  3471. 2:10:41this. Okay. So this is where you are
  3472. 2:10:43going to read the file synchronous way.
  3473. 2:10:45Then we have call back APIs. Okay. So
  3474. 2:10:48there are callback APIs that you can
  3475. 2:10:49use. For example, FS read file. So that
  3476. 2:10:52is a callback API. Okay. And the third
  3477. 2:10:54one is promise APIs. And now understand
  3478. 2:10:56that why in the previous section we have
  3479. 2:10:57discussed about the callbacks, promises,
  3480. 2:10:59async all of these. Now why this is very
  3481. 2:11:01important because see whenever you're
  3482. 2:11:03going to create project now here in this
  3483. 2:11:05particular file or whatever things we're
  3484. 2:11:07going to discuss I can't give you each
  3485. 2:11:08and every information on file system. So
  3486. 2:11:10what I mean by this so let's you know
  3487. 2:11:12this is sync APIs or this is callback
  3488. 2:11:15API this is promise API based on the
  3489. 2:11:18project that you are building you need
  3490. 2:11:20to know or you need to understand okay
  3491. 2:11:22which method you need to use in that
  3492. 2:11:24certain position. So let's say you have
  3493. 2:11:26a particular project you are building
  3494. 2:11:28and there are two places you need to use
  3495. 2:11:30file system in feature one you need to
  3496. 2:11:32use promise API in the feature two you
  3497. 2:11:34need to use sync API now no one is going
  3498. 2:11:36to tell you okay that okay you have to
  3499. 2:11:38do this or no tutorial video is going to
  3500. 2:11:40teach you all of this okay they can give
  3501. 2:11:42you the uh path okay this is where you
  3502. 2:11:44have to use but by practice you need to
  3503. 2:11:47understand all all of this by yourself
  3504. 2:11:49and then only you can create your
  3505. 2:11:50project or else what will happen I'm
  3506. 2:11:51going to tell you you watch videos after
  3507. 2:11:54videos you watch blogs blogs use AI
  3508. 2:11:56tools. You're not going to create
  3509. 2:11:58project by yourself. Especially if
  3510. 2:11:59you're let's working in a project or
  3511. 2:12:01let's say where project not project I
  3512. 2:12:05will say even I'll say let's say you're
  3513. 2:12:06working in a company and let's say there
  3514. 2:12:08is some uh project which is already
  3515. 2:12:10built and you are only working on the
  3516. 2:12:13fixes so that the new fixes comes. So
  3517. 2:12:15you need to understand all of this or
  3518. 2:12:16say it will be very difficult for you to
  3519. 2:12:18do all of this. Second, if you're
  3520. 2:12:20working in some uh freelancing uh work,
  3521. 2:12:23then this will be much more difficult
  3522. 2:12:24because in freelancing the requirement
  3523. 2:12:26can be very very tricky, right? Your
  3524. 2:12:28client requirement. So why I mentioning
  3525. 2:12:30all of this means the simple way to
  3526. 2:12:33explain all of this that don't blindly
  3527. 2:12:35uh learn [clears throat]
  3528. 2:12:37whatever I'm teaching here. Okay, try to
  3529. 2:12:39learn whatever I'm teaching plus try to
  3530. 2:12:42apply some of the things you should
  3531. 2:12:44learn by yourself and then create
  3532. 2:12:46project. This is why or else I'm I'm
  3533. 2:12:48noticing that some people are actually
  3534. 2:12:50commenting you don't explain this much
  3535. 2:12:52you first explain you don't expl do not
  3536. 2:12:54explain in the file go to whiteboard
  3537. 2:12:56explain there come back and then explain
  3538. 2:12:59even if I do that I still what you are
  3539. 2:13:01going to do you're going to memorize
  3540. 2:13:02each and everything no one can memorize
  3541. 2:13:04okay every method you need to know the
  3542. 2:13:07process and then based on that you have
  3543. 2:13:09to create projects as much as possible
  3544. 2:13:12okay so we have sync APIs then we have
  3545. 2:13:14call back APIs it [snorts] and then you
  3546. 2:13:16have something called promise APIs. So
  3547. 2:13:18all these three we have to understand
  3548. 2:13:20and this is very important and what in
  3549. 2:13:23modern [clears throat] NodeJS
  3550. 2:13:24application what you're going to use
  3551. 2:13:25definitely this is what you have learned
  3552. 2:13:26so far we are going to use promise APIs
  3553. 2:13:28correct because promise you see
  3554. 2:13:29callbacks also you can use but you what
  3555. 2:13:32will happen if your code is very
  3556. 2:13:33complicated or the feature is very
  3557. 2:13:34complicated that will end up in call
  3558. 2:13:37back help so you're going to write call
  3559. 2:13:38back inside call back inside callback
  3560. 2:13:40like that so you're not going to do that
  3561. 2:13:42so first of all let's do one thing maybe
  3562. 2:13:44let's uh create first one so I'm just
  3563. 2:13:47going to create the sync example. Okay.
  3564. 2:13:50So I'm just going to do function and you
  3565. 2:13:52do run sync
  3566. 2:13:55example.
  3567. 2:13:57Okay. Let's understand this one uh
  3568. 2:13:59properly. Okay. Because uh this is one
  3569. 2:14:02of the most important concept you don't
  3570. 2:14:04understand.
  3571. 2:14:05So first of all sync APIs are easier to
  3572. 2:14:08understand. Okay. This is very easy. So
  3573. 2:14:10it's like a synchronous APIs. Now here
  3574. 2:14:13what we going to do right? See first of
  3575. 2:14:15all you have to understand that what
  3576. 2:14:17actually synchronous uh no not what
  3577. 2:14:20where actually you should use this this
  3578. 2:14:21is actually very important right now
  3579. 2:14:23first of all you need to use this
  3580. 2:14:24synchronous API when you have some let's
  3581. 2:14:27say you're creating some small scripts
  3582. 2:14:29you're creating some small startup
  3583. 2:14:32startup scripts there you can use this
  3584. 2:14:34or maybe you're going to build some
  3585. 2:14:36scripts there also you can use this one
  3586. 2:14:39uh maybe you're using some local demos
  3587. 2:14:41something like this you can do all of
  3588. 2:14:43this right where it is not good, not
  3589. 2:14:46good or even bad or even bad practice.
  3590. 2:14:49So where you should not should not use
  3591. 2:14:51this. So let's say you are uh
  3592. 2:14:53implementing some HTTP request and you
  3593. 2:14:55are using this one in HTTP request
  3594. 2:14:57handlers. This is not good, right?
  3595. 2:14:59Because by the name you understand this
  3596. 2:15:00is a synchronous API. Okay. Also let's
  3597. 2:15:04say you're creating some uh very
  3598. 2:15:06scalable backend application, your
  3599. 2:15:07traffic is huge. So when there will be
  3600. 2:15:10lot of traffic, never use this one. So
  3601. 2:15:12high traffic APIs do not use this. Okay.
  3602. 2:15:14[snorts] Or maybe I will suggest this is
  3603. 2:15:16what I believe that if you're
  3604. 2:15:17implementing some background jobs and
  3605. 2:15:19everything right you should not use this
  3606. 2:15:20sync API also. So in BG or background so
  3607. 2:15:23do background jobs and everything you
  3608. 2:15:25should not use this sync APIs. Okay. But
  3609. 2:15:27first of all let's see that uh how we
  3610. 2:15:29are going to uh do this one right or how
  3611. 2:15:32you're going to implement this. So first
  3612. 2:15:33of all let's do one thing here right? uh
  3613. 2:15:37let's go maybe here or you can do no not
  3614. 2:15:39here because this will help you to
  3615. 2:15:41understand
  3616. 2:15:43maybe on top
  3617. 2:15:46so first let's do one thing I'm just
  3618. 2:15:47going to take a folder okay that I'm
  3619. 2:15:49going to create so I'm just going to do
  3620. 2:15:50a demo folder path so I'm going to
  3621. 2:15:53create a path so how we are going to get
  3622. 2:15:55the path current directory see we have
  3623. 2:15:58already discussed in this path module
  3624. 2:16:00whatever we have learned in the previous
  3625. 2:16:01section we are implementing the same
  3626. 2:16:03concept again and again so we need to do
  3627. 2:16:05path so you're going import node uh path
  3628. 2:16:08you're going to do a join and then how
  3629. 2:16:11we going to get the current directory so
  3630. 2:16:13we have something called process cwd you
  3631. 2:16:16remember and then I'm just going to
  3632. 2:16:17create one uh path here so I'm just
  3633. 2:16:19going to give file system
  3634. 2:16:22and then let's give this one as file
  3635. 2:16:24system demo so I'm just going to do fs
  3636. 2:16:26demo so this will be my folder where I
  3637. 2:16:28wanted to create this okay and now after
  3638. 2:16:31this I'm just going to do con sync file
  3639. 2:16:34path so I need to create a file actual
  3640. 2:16:37file. Remember in the path module I
  3641. 2:16:39mentioned that this will actually not
  3642. 2:16:41create the folder.
  3643. 2:16:42[clears throat and snorts]
  3644. 2:16:43It will not create the folder. It
  3645. 2:16:44doesn't check whether the file exist or
  3646. 2:16:46not. But this file system is where we
  3647. 2:16:48are going to actually check all of this.
  3648. 2:16:49Okay, this is where we are going to
  3649. 2:16:51actually create the file or folder. So
  3650. 2:16:53here I'm just going to do path dot join.
  3651. 2:16:56I'm going to take the demo folder path
  3652. 2:16:58and let's do this one as sync note txt.
  3653. 2:17:03So this is where I wanted to create this
  3654. 2:17:05particular file. Okay. So this will be
  3655. 2:17:07your project directory file system fs
  3656. 2:17:10demo and then we're going to have the
  3657. 2:17:11sync node txt. Okay. So now uh maybe
  3658. 2:17:15also let's take a another type here over
  3659. 2:17:17here.
  3660. 2:17:19So I'm just going to do type. So I do
  3661. 2:17:21file result. So I just let's take a
  3662. 2:17:24style. Uh this will be string. Uh file
  3663. 2:17:27name also I'm just going to take which
  3664. 2:17:29will be string. Let's take a content
  3665. 2:17:32which will be string. Let's take a size
  3666. 2:17:34in bytes and this will be number. So
  3667. 2:17:38this will be the uh result that I wanted
  3668. 2:17:41to return. So this will be my return
  3669. 2:17:43type. So here I'm just going to return
  3670. 2:17:45this file result. Okay.
  3671. 2:17:48So now uh how we going to first of all
  3672. 2:17:52write something in a file. So we need
  3673. 2:17:55some method right. So this where we are
  3674. 2:17:57going to use this sync example. So we
  3675. 2:17:59have something called fs. So fs from
  3676. 2:18:01node fs. So first of all let's go and
  3677. 2:18:02import the fs module because we haven't
  3678. 2:18:05imported the fs module only. So I'm just
  3679. 2:18:07going to do import file system from. So
  3680. 2:18:10I'll do node file system. Okay. So now
  3681. 2:18:13this is where you're going to get a
  3682. 2:18:15method. So now I'm just going to do
  3683. 2:18:17what? I'm going to write something. I'll
  3684. 2:18:19do write content to a file. How I can do
  3685. 2:18:23that? So I'll do fs dot write and see
  3686. 2:18:26that we have write file sync. So write
  3687. 2:18:29file sync is going to write something in
  3688. 2:18:30a file. Okay. Now here one very very
  3689. 2:18:33important point if this particular file
  3690. 2:18:35doesn't exist node is going to create
  3691. 2:18:38it. Okay. And if the file is already
  3692. 2:18:41present so whatever node is going to
  3693. 2:18:43replace the content. So consider here
  3694. 2:18:45I'm just going to take this and here
  3695. 2:18:48what it will take it will take the file
  3696. 2:18:50okay uh fs.path or the file descriptor
  3697. 2:18:54okay and the actual data. So now I'm
  3698. 2:18:56just going to give my sync file path.
  3699. 2:18:58Sync file path mean this is where I
  3700. 2:19:00wanted to uh save this. I'll do sync
  3701. 2:19:03file path and then I'm just going to do
  3702. 2:19:06a container. I'll do created using sync
  3703. 2:19:10file system and then here what I'll do
  3704. 2:19:13right I'll give a type. So I'll do u uh
  3705. 2:19:15UTF8. So this is nothing but this is
  3706. 2:19:18simply we are telling note that we are
  3707. 2:19:20writing a simple text. Okay. And now
  3708. 2:19:23what will happen right? So this is the
  3709. 2:19:24first thing. Next we have one more
  3710. 2:19:27method that also you need to understand.
  3711. 2:19:28So first you understand that fs do.
  3712. 2:19:30Write file sync is going to write
  3713. 2:19:32content to a file. If the file doesn't
  3714. 2:19:34exist you can note it down. Then node is
  3715. 2:19:36going to create it if the file already
  3716. 2:19:38exist. So node is going to replace the
  3717. 2:19:40content. Okay. And what this is going to
  3718. 2:19:43do this is going to tell node that okay
  3719. 2:19:44we are writing normal text. Then we have
  3720. 2:19:46something called fs.append file sync.
  3721. 2:19:49What this is going to do? This is going
  3722. 2:19:51to add content at the end of the file at
  3723. 2:19:54the end of this file. Okay. And it's not
  3724. 2:19:56going to replace your existing content.
  3725. 2:19:58So here what you can do, right? You can
  3726. 2:20:00use something called
  3727. 2:20:02because by the name you understand,
  3728. 2:20:03right? So you append file sync and
  3729. 2:20:06you'll do sync file path and I'm just
  3730. 2:20:08going to do here give a space. I'll do
  3731. 2:20:10appended maybe appended using sync fs
  3732. 2:20:14like this. And here also I'm just going
  3733. 2:20:16to do UTF8.
  3734. 2:20:18That's it. So this is two things we
  3735. 2:20:21going to do.
  3736. 2:20:24Oh, we have to give comma man. Okay. So
  3737. 2:20:28now this will to write and this is to
  3738. 2:20:30append. Now how we are going to read
  3739. 2:20:32file means read file content. Okay. And
  3740. 2:20:36for this one we have something called
  3741. 2:20:38this is also simple. We have to use fs
  3742. 2:20:40dot read file sync mean we're going to
  3743. 2:20:42read something. So here we are going to
  3744. 2:20:45do. So let's say I wanted to read the
  3745. 2:20:46content of this particular file. con
  3746. 2:20:50content and we do fs dot read file sync
  3747. 2:20:55and here you're going to give the path
  3748. 2:20:57and give the type so we'll do UTF8
  3749. 2:21:00that's it now remember what will be the
  3750. 2:21:03type so we have the style file name
  3751. 2:21:05content and size in bytes so how you're
  3752. 2:21:07going to get the size in bytes so here
  3753. 2:21:09you have something called uh fs dot stat
  3754. 2:21:12uh stats sync if I'm not wrong so here
  3755. 2:21:15you'll be you'll be getting your stats
  3756. 2:21:16so you do if you do stats here. So fs
  3757. 2:21:19do.stats sync. So fs.stat sync is going
  3758. 2:21:23to give you the information about the
  3759. 2:21:25file. Okay. Now this is also going to
  3760. 2:21:27give you the about the file or also
  3761. 2:21:29about a folder also. And here in this
  3762. 2:21:32particular variable that will be a
  3763. 2:21:34property that is called stats dot size
  3764. 2:21:37and that is going to give you the file
  3765. 2:21:38size in bytes. So here what you have to
  3766. 2:21:41do you have to simply pass the sync file
  3767. 2:21:43path. That's it. So now we are going to
  3768. 2:21:45return the result. Okay. So first of all
  3769. 2:21:46what we going to do? So let's start with
  3770. 2:21:48first we have a style. So this will be
  3771. 2:21:50sync. Uh then we are going to return the
  3772. 2:21:53content. Then we also need to return the
  3773. 2:21:55file. Now here how we going to return
  3774. 2:21:56the file. Now see this is where you have
  3775. 2:21:58to see that how uh the previous concept
  3776. 2:22:01that we have learned so far. We are
  3777. 2:22:03applying here in this path module we
  3778. 2:22:05have already covered. This path dobbase
  3779. 2:22:07name is going to give you the file name.
  3780. 2:22:09Similarly here also we're going to do
  3781. 2:22:10path dot base name and here we're going
  3782. 2:22:12to pass the sync file path. And then
  3783. 2:22:15you're going to pass the size. How you
  3784. 2:22:16going to get the size? So this stats is
  3785. 2:22:18going to give you see here you're going
  3786. 2:22:20to get a lot of options. So first we
  3787. 2:22:22need stats dot size. That's it. So we'll
  3788. 2:22:24save this. Now let's go here and create
  3789. 2:22:26a main function. So as sync function
  3790. 2:22:30main and what will be the because we are
  3791. 2:22:32going to work with promises also. So
  3792. 2:22:34let's keep promise only here. And then
  3793. 2:22:36I'm simply going to take a try catch. Uh
  3794. 2:22:40here this will be error. And I'm simply
  3795. 2:22:42going to take the same thing that we
  3796. 2:22:44have learned so far. So if this error is
  3797. 2:22:46instance of error. So we're going to
  3798. 2:22:48render error dot message
  3799. 2:22:51or else I'm just going to do unknown
  3800. 2:22:53error.
  3801. 2:22:56And now let's uh log one error.
  3802. 2:23:00So I'll do file system error and render
  3803. 2:23:02the message.
  3804. 2:23:04Okay. And then we're going to call this
  3805. 2:23:06main. Okay. So this is done. See first
  3806. 2:23:08of all we have to understand another
  3807. 2:23:10thing that when you have this demo
  3808. 2:23:11folder path so we have to check that if
  3809. 2:23:14this path is actually present or not
  3810. 2:23:16you're getting what I'm talking about.
  3811. 2:23:17So how we going to check and if this is
  3812. 2:23:19not present so we're going to create
  3813. 2:23:20this particular folder. So that also
  3814. 2:23:22we're going to understand that how you
  3815. 2:23:24can create folder. So first let's do
  3816. 2:23:26like this do insure file no sorry insure
  3817. 2:23:30demo folder present or exists. So this
  3818. 2:23:34is what we're going to check. So now
  3819. 2:23:35copy this and let's go here and I'm
  3820. 2:23:38simply going to do function and NC demo
  3821. 2:23:40folder exist. I'm going to return void
  3822. 2:23:43and now we are going to check that if
  3823. 2:23:45this is exist or not. So how we going to
  3824. 2:23:47check exist or not? So we have one
  3825. 2:23:49method that is called a do.exist sync
  3826. 2:23:52and [snorts] this is going to tell that
  3827. 2:23:54okay if this is present. So if this is
  3828. 2:23:55false so that means this folder is not
  3829. 2:23:58present. So here we're going to pass the
  3830. 2:23:59demo folder path. So if this demo folder
  3831. 2:24:01path is not present, so we need to
  3832. 2:24:03create a uh demo folder. Correct? How we
  3833. 2:24:06going to create? So we have something
  3834. 2:24:07called FS. MKDIR sync. Now this MKDI
  3835. 2:24:12sync is going to create a particular
  3836. 2:24:14folder synchronously. Okay, this not
  3837. 2:24:16will be a sync. This will be
  3838. 2:24:17synchronously it's going to create.
  3839. 2:24:19Okay, so it's simply going to fs.m MKDIR
  3840. 2:24:22sync. Okay, because we are working with
  3841. 2:24:24sync. And here you're going to pass the
  3842. 2:24:26demo folder path. And you can pass one
  3843. 2:24:28more option here and that is called
  3844. 2:24:29recursive will be true. Now this
  3845. 2:24:31recursive is nothing but simply telling
  3846. 2:24:33that you are going to create the full
  3847. 2:24:35folder path if the parts of it doesn't
  3848. 2:24:38exist. So it's going to create the
  3849. 2:24:40complete folder path here. Okay. Now
  3850. 2:24:43okay now you might be thinking okay why
  3851. 2:24:45actually you're using this recursive
  3852. 2:24:46equal to true. So let me explain in a
  3853. 2:24:48simpler way. So what will happen right?
  3854. 2:24:50If you don't give this sometime this can
  3855. 2:24:51fail if your parent folder doesn't
  3856. 2:24:54exist. So it's simply telling that even
  3857. 2:24:55if the parent folder is not exist still
  3858. 2:24:58you are going to create this. So first
  3859. 2:24:59we're going to use this. So this is
  3860. 2:25:01going to check if this present or not.
  3861. 2:25:03And now what I can do right I will go
  3862. 2:25:05here and I'm simply going to do con sync
  3863. 2:25:08sync result and this will be my run sync
  3864. 2:25:12example and then I'm simply going to do
  3865. 2:25:14log and for now I'll just do like this.
  3866. 2:25:17I'll do sync result. So save this. Now
  3867. 2:25:20we'll go here and create one more. So
  3868. 2:25:24I'll do 07
  3869. 2:25:26and this will be my tsx source slash and
  3870. 2:25:29then I'm just going to take rename copy
  3871. 2:25:31this
  3872. 2:25:33paste it here and then let's run this
  3873. 2:25:36and see we'll see what will happen. So
  3874. 2:25:37first I'm just going to npm run 07.
  3875. 2:25:39Enter. And you can see that now we
  3876. 2:25:41already have a file system folder
  3877. 2:25:43created. Fantastic. And you see that we
  3878. 2:25:45have a style sync created using this
  3879. 2:25:47file name is this size in bytes is 45
  3880. 2:25:49bytes. If you go here you have fs demo.
  3881. 2:25:52This is what you have created. So now
  3882. 2:25:54[snorts] you have this fs demo this
  3883. 2:25:56folder right file system fs demo and
  3884. 2:25:59then you have sync note text. And if you
  3885. 2:26:01go here you have this created using sync
  3886. 2:26:04uh fs and appended using sync fs. Okay.
  3887. 2:26:07So this is how you're going to do what
  3888. 2:26:09you're going to create folder and file
  3889. 2:26:11synchronously. So this is one only one
  3890. 2:26:13of the section. Now in the next section
  3891. 2:26:14we are going to understand using call
  3892. 2:26:16back and promises. See promises one is
  3893. 2:26:18more important. So let's see how we
  3894. 2:26:19going to do that in the next part. All
  3895. 2:26:21right. So now let's understand how we
  3896. 2:26:23can use call back based APIs. Right? So
  3897. 2:26:25here first of all we need to know I
  3898. 2:26:26think this pattern you remember that we
  3899. 2:26:28have call back and in the call back we
  3900. 2:26:30usually have error or the result. Right?
  3901. 2:26:33So now let's create the function and we
  3902. 2:26:35understand practically. So I'm just
  3903. 2:26:36going to do run call back example
  3904. 2:26:40here. This will be returning a promise
  3905. 2:26:43and here I'm just going to return a file
  3906. 2:26:46result also. Let's go to the top and
  3907. 2:26:49create two more here. So one will be
  3908. 2:26:51callback
  3909. 2:26:53file path and also one will be the
  3910. 2:26:55promise file path. So for this one let's
  3911. 2:26:59keep this one as callback note and this
  3912. 2:27:02will be my promise note. Okay.
  3913. 2:27:06So first of all what we're going to do
  3914. 2:27:07right so for this one we are going to
  3915. 2:27:09wrap this call back example in a promise
  3916. 2:27:12so that we can actually run this from
  3917. 2:27:14this our runner okay from this main
  3918. 2:27:16function but inside we are actually
  3919. 2:27:18going to use our callback style APIs
  3920. 2:27:20only. So first what we going to do right
  3921. 2:27:22so first let's I'm just going to return
  3922. 2:27:24a new promise and here I'll do a resolve
  3923. 2:27:28reject.
  3924. 2:27:30So now let's understand what actually we
  3925. 2:27:32are doing here. So first of all uh just
  3926. 2:27:34like we have uh understood that we have
  3927. 2:27:36something called fs.right file sync
  3928. 2:27:39correct for synchronous APIs. So what we
  3929. 2:27:41need to use? So here we are going to use
  3930. 2:27:43something called a do.t write file only.
  3931. 2:27:46Okay. So here now we are going to pass
  3932. 2:27:48our path. So our path will be our
  3933. 2:27:51callback file path. And here uh sorry
  3934. 2:27:55and uh call back uh file path comma
  3935. 2:28:00let's do created
  3936. 2:28:03using call back fs and we are going to
  3937. 2:28:08pass our type which will be UTF8.
  3938. 2:28:11Okay. So now here what we are going to
  3939. 2:28:13do right you getting error. Okay. So
  3940. 2:28:16first of all let's take the call back
  3941. 2:28:18here. So we going to take the uh call
  3942. 2:28:20back. So first I'm just going to do here
  3943. 2:28:22write error.
  3944. 2:28:27Okay. And then inside this first we need
  3945. 2:28:30to understand that if we have error so
  3946. 2:28:31we are going to reject the error. Right.
  3947. 2:28:33So we are going to simply do if we have
  3948. 2:28:35write error we're going to do reject. We
  3949. 2:28:38are going to pass the error and then we
  3950. 2:28:40are going to return. Okay. So this is
  3951. 2:28:43the first thing that we are going to do.
  3952. 2:28:45So this is about how we are going to
  3953. 2:28:46write. Okay. So now what I have to do
  3954. 2:28:50right uh also let's do one thing I think
  3955. 2:28:52this will be 1 minute I think we are
  3956. 2:28:55closing this one here
  3957. 2:28:57after this okay here we are going to do
  3958. 2:28:59append
  3959. 2:29:01same only instead of append file sync
  3960. 2:29:04you're going to use append file and then
  3961. 2:29:06again you're going to I don't like this
  3962. 2:29:08popup very limiting okay so here we are
  3963. 2:29:11going to use this and then I'm simply
  3964. 2:29:13going to do append
  3965. 2:29:15using call back fs give space and then
  3966. 2:29:20you're going to give the type which will
  3967. 2:29:21be UTF
  3968. 2:29:248
  3969. 2:29:26that's it and then again also you're
  3970. 2:29:28going to take another call back right so
  3971. 2:29:30here you are going to have the call back
  3972. 2:29:31so here we are going to do if there is
  3973. 2:29:33any append error
  3974. 2:29:36and then inside this we're going to
  3975. 2:29:37again check if there is append error so
  3976. 2:29:40we're going to reject the error and
  3977. 2:29:41you'll see that this is actually what we
  3978. 2:29:43call about call back hell because you
  3979. 2:29:46are actually writing call back after
  3980. 2:29:47call back Okay. And this will be return.
  3981. 2:29:50So this about your append inside this
  3982. 2:29:52again we are going to now read the file
  3983. 2:29:54information. Right. So just like we have
  3984. 2:29:56done uh write append read. So similarly
  3985. 2:30:00here also now we're going to do fs dot
  3986. 2:30:02read file not sync read file only. Here
  3987. 2:30:05also you're going to pass callback file
  3988. 2:30:07path and then we're going to pass the
  3989. 2:30:10type which will be UTF8. And then what
  3990. 2:30:12this is going to give? So here we are
  3991. 2:30:13going to give uh so first of all we
  3992. 2:30:16going to get the error. So let's take
  3993. 2:30:18read error or else we're going to get
  3994. 2:30:20the result which will be content the
  3995. 2:30:23actual file content and then inside this
  3996. 2:30:26we're going to again check if we have
  3997. 2:30:27read error we're going to reject
  3998. 2:30:31read error and then we're going to
  3999. 2:30:33return okay and now what we can do right
  4000. 2:30:36so inside this again we have to read the
  4001. 2:30:39stat so simply if I just go here you can
  4002. 2:30:41see that we are using a method inside
  4003. 2:30:45call back first we are checking we are
  4004. 2:30:46writing another call back inside that we
  4005. 2:30:48are writing another call back. Now here
  4006. 2:30:50we need to write another call back. Why
  4007. 2:30:51we need to read the stat. So we going to
  4008. 2:30:53use not stat sync but instead we going
  4009. 2:30:56to use stat and here we're going to pass
  4010. 2:30:58the call back file path and then this is
  4011. 2:31:01going to give you either error. So I'm
  4012. 2:31:02just going to do stat error
  4013. 2:31:05and or else this will be stats array
  4014. 2:31:10this popup man. So and then we are going
  4015. 2:31:14to simply check if this stat error is
  4016. 2:31:15present. We are going to reject
  4017. 2:31:18the stat error or else and then we are
  4018. 2:31:20going to return and at the end now we
  4019. 2:31:23have to return the result but you can't
  4020. 2:31:25return the result directly. You have to
  4021. 2:31:27resolve it. So you are going to resolve
  4022. 2:31:29it. So you're going to pass a style. So
  4023. 2:31:31this will be your call back. You're
  4024. 2:31:33going to pass the content. This content
  4025. 2:31:36will be your uh this content will be we
  4026. 2:31:38have created the content this content.
  4027. 2:31:40Okay. This content that you have. Then
  4028. 2:31:42you're going to pass the size in bytes.
  4029. 2:31:44So size and bytes will be your stats and
  4030. 2:31:46this stats is going to give you size and
  4031. 2:31:50also you're going to have the file name.
  4032. 2:31:52File name will be your path dot base
  4033. 2:31:55name and you're going to pass a call
  4034. 2:31:57file path. That's it. So this is done.
  4035. 2:31:59Now you'll see if you go here and you
  4036. 2:32:01notice that we are writing fs dot this
  4037. 2:32:03is one our method right and inside call
  4038. 2:32:07back. So inside this call back we have
  4039. 2:32:09everything inside this we have append
  4040. 2:32:11file inside append file call back we are
  4041. 2:32:14having the read file inside read file we
  4042. 2:32:16are having the stat and at the end we
  4043. 2:32:18are resolving the result so this is a
  4044. 2:32:20classic example of call back help so
  4045. 2:32:22you're using call back after call back
  4046. 2:32:24after call back like this and then what
  4047. 2:32:26you can do right you can just do con and
  4048. 2:32:28I'll do call back result so this will be
  4049. 2:32:32a so I'll do call back what is the name
  4050. 2:32:34we going to run call back example and
  4051. 2:32:37then we're going to pass it here.
  4052. 2:32:40Save this and let's run it.
  4053. 2:32:44Enter. And you can see that. Fantastic.
  4054. 2:32:46So now inside FS demo we have another.
  4055. 2:32:49So now this time you're getting call
  4056. 2:32:51back created using callback size by is
  4057. 2:32:5451. And this is the file name because
  4058. 2:32:56this is the file name we have created.
  4059. 2:32:58Correct. So scroll down and let's go
  4060. 2:33:00here. You can see that created using
  4061. 2:33:01callback file system. So this will be a
  4062. 2:33:04classic example of how you can implement
  4063. 2:33:06using callback. Okay. So with this now
  4064. 2:33:08let's move to the next one which will be
  4065. 2:33:10how you can work with promises. This is
  4066. 2:33:11exactly what you usually will be doing
  4067. 2:33:14in your project. So let's do this one in
  4068. 2:33:16the next part. Okay. So now let's create
  4069. 2:33:18using promises, right? Promises one. Uh
  4070. 2:33:20so first of all how we going to use
  4071. 2:33:21promises? So we have we need to import
  4072. 2:33:23something right. So we going to import
  4073. 2:33:25fs promises
  4074. 2:33:27from. So we have this module right? So
  4075. 2:33:29we have mode file system / promises. So
  4076. 2:33:32this is what we going to use. Okay. And
  4077. 2:33:34this is much more easier and you'll
  4078. 2:33:36understand what actually we are doing
  4079. 2:33:37here. So let's do the promise API right.
  4080. 2:33:40So here I'm simply going to uh take
  4081. 2:33:42async. So I'll do function and this will
  4082. 2:33:45be run promise
  4083. 2:33:47example.
  4084. 2:33:50What this is going to return again
  4085. 2:33:51promise and the type will be file
  4086. 2:33:54result. So first what we going to do? So
  4087. 2:33:57first we need to write right. So to
  4088. 2:34:00write we are going to use fs promises
  4089. 2:34:02and this is going to give you this write
  4090. 2:34:03file method. Okay. So this is where we
  4091. 2:34:06are going to use our promise file path
  4092. 2:34:10comma. So I'm just going to do this one
  4093. 2:34:11as created using promise APIs. See again
  4094. 2:34:16at the end it is the same thing right
  4095. 2:34:18whatever we are doing. So this is going
  4096. 2:34:19to write same way we are going to have.
  4097. 2:34:23So we'll copy this and then we'll paste
  4098. 2:34:25it here. You're going to have append
  4099. 2:34:26file. So this will be appended using
  4100. 2:34:29promise APIs. Okay.
  4101. 2:34:33And then you're going to have the
  4102. 2:34:35content. So to get the content remember
  4103. 2:34:38content is the file content, right? So
  4104. 2:34:40we going to do a so. So you have fs
  4105. 2:34:41promises dot read file
  4106. 2:34:46pass the promise file path
  4107. 2:34:49and we are going to tell okay we are
  4108. 2:34:50using text and similarly we're going to
  4109. 2:34:52use stats. I'm not explaining because
  4110. 2:34:54it's repeating. So you're going to do fs
  4111. 2:34:56promises dot stats
  4112. 2:34:59not stat uh this will be stat not stats
  4113. 2:35:02and path and then we are going to
  4114. 2:35:04return. So this will be style. So this
  4115. 2:35:06will be promises right? So we're using
  4116. 2:35:08promises. Uh then we have the content.
  4117. 2:35:12Then file name. So this will be your
  4118. 2:35:14path dot base name. This will be the
  4119. 2:35:17promise file path. And then you're going
  4120. 2:35:19to do size in bytes which will be stats
  4121. 2:35:22dot size. That's it. And now we are
  4122. 2:35:26going to simply use this in our uh here.
  4123. 2:35:28So I'll do con.
  4124. 2:35:31Let's do promise result.
  4125. 2:35:34And this will be run promise example.
  4126. 2:35:37And we're going to just log it here.
  4127. 2:35:40The only difference is you are importing
  4128. 2:35:42this one from file system promises
  4129. 2:35:44module. And then if I just run it here.
  4130. 2:35:46So we're going to get promises and one
  4131. 2:35:48mode will be created, right? So if you
  4132. 2:35:51go here sorry if you go here created
  4133. 2:35:53using promise APIs appended using
  4134. 2:35:55promise APIs. So I think this is going
  4135. 2:35:58to give a proper understanding of how
  4136. 2:35:59you can work with file system. How you
  4137. 2:36:01can create file folder how you can check
  4138. 2:36:03if that is exist or not. How you can
  4139. 2:36:05create path and very very important how
  4140. 2:36:07you're going to make use of both path
  4141. 2:36:10module and file system together to
  4142. 2:36:12implement this. Okay this is very
  4143. 2:36:14important because consider you don't
  4144. 2:36:15know that what is path.base name. So you
  4145. 2:36:18will not be able to use right. So that
  4146. 2:36:19is the reason we've discussed the path
  4147. 2:36:21module previously and then we discuss
  4148. 2:36:23the file system uh module. Okay. So with
  4149. 2:36:27this now let's move on to the next
  4150. 2:36:29module which will be another very
  4151. 2:36:30important module and this will be
  4152. 2:36:32buffers. Okay. So we will create one
  4153. 2:36:34more file here. So I'm just going to do
  4154. 2:36:36buffers.ts.
  4155. 2:36:38Okay. Now this is another important. So
  4156. 2:36:41first of all you need to understand that
  4157. 2:36:42what actually buffers are and why we
  4158. 2:36:44need buffers. Right. So first you
  4159. 2:36:46whenever you need to work with raw
  4160. 2:36:49binary data raw binary data and that
  4161. 2:36:54that is where you are going to use
  4162. 2:36:55buffers right now what actually binary
  4163. 2:36:58data means now I'm not sure if some of
  4164. 2:37:00you know means see binary data means
  4165. 2:37:02when you have your data stored in bytes
  4166. 2:37:06so your binary data means
  4167. 2:37:09when you have
  4168. 2:37:11your data stored in bytes as simple as
  4169. 2:37:15that Right? Now you need to understand
  4170. 2:37:17that when uh in JavaScript, right?
  4171. 2:37:19Whenever you use something called
  4172. 2:37:21strings, now these strings are good for
  4173. 2:37:24normal text. But when you work with
  4174. 2:37:26bytes or especially raw bytes, this is
  4175. 2:37:29where you can't use strings that much.
  4176. 2:37:31So you need to use something called
  4177. 2:37:32buffers. Now one of the most important
  4178. 2:37:34interview questions can be that where
  4179. 2:37:37where all the places okay that you can
  4180. 2:37:39use buffers. So first of all, one will
  4181. 2:37:41be reading files. very important uh
  4182. 2:37:45receiving very like there can be believe
  4183. 2:37:48me or not there can be lot of
  4184. 2:37:50complicated and very advanced level of
  4185. 2:37:52places where you can or you need to use
  4186. 2:37:54buffers okay so don't take this
  4187. 2:37:56particular module lightly this is a very
  4188. 2:37:58important module so when you're going to
  4189. 2:38:01receive HTTP request bodies then also
  4190. 2:38:03you can use buffers okay working with
  4191. 2:38:06streams very very important you'll see
  4192. 2:38:08that whenever you're going to work with
  4193. 2:38:09streams right you are going to use or
  4194. 2:38:10you need buffers uh
  4195. 2:38:13handling images, PDFs, files uh and
  4196. 2:38:18videos there also you need buffers also
  4197. 2:38:21sometime you need to use encrypt and
  4198. 2:38:23maybe let's say hashing then also you
  4199. 2:38:25need buffers so these all will be very
  4200. 2:38:28very frequently you are going to use
  4201. 2:38:29buffers so first see if I I'm going to
  4202. 2:38:32repeat one thing that what actually
  4203. 2:38:34buffers and when you need to use buffer
  4204. 2:38:37so first we need to mention that okay
  4205. 2:38:38buffer we are going to use whenever we
  4206. 2:38:40need to work with raw binary data data.
  4207. 2:38:43Okay, raw binary data and raw why
  4208. 2:38:45because JavaScript so they can ask okay
  4209. 2:38:47why not JavaScript uh strings because
  4210. 2:38:50strings are actually good only for
  4211. 2:38:52normal text when you need to use raw
  4212. 2:38:55binary data the binary data that is
  4213. 2:38:58stored in bytes this is where you need
  4214. 2:39:00buffers okay so just like this you have
  4215. 2:39:03string so string means your human
  4216. 2:39:06readable text then you have buffer means
  4217. 2:39:11your raw byes
  4218. 2:39:13like this. Okay. Now, first of all, you
  4219. 2:39:15need to know how you can create normal
  4220. 2:39:17or simple buffer. So, we have something
  4221. 2:39:19called buffer dot from method. So, what
  4222. 2:39:22you can do right, you can simply do
  4223. 2:39:23let's I'm just going to create a text
  4224. 2:39:25buffer. So, I'll do con text buffer. So,
  4225. 2:39:28I I'll directly use buffer
  4226. 2:39:31dot from and see that if I just go here,
  4227. 2:39:34this has crazy amount of method. Okay,
  4228. 2:39:36there is no way I can cover each and
  4229. 2:39:38everything. But I'll try to cover only
  4230. 2:39:40the best one. For example, how you can
  4231. 2:39:42create buffer. So we give a buffer dot
  4232. 2:39:44from and let's say I wanted to pass a
  4233. 2:39:47text here. So this would be my text node
  4234. 2:39:49something like this, right? So this is
  4235. 2:39:51going to do what? This is going to
  4236. 2:39:53create a buffer from a normal text
  4237. 2:39:56because this is a text correct. So what
  4238. 2:39:59will happen? This buff uh sorry this
  4239. 2:40:02normal text will become bytes internally
  4240. 2:40:06you're getting. So this is where we need
  4241. 2:40:08or we are creating buffer from a normal
  4242. 2:40:10text. Now consider if I'm just going to
  4243. 2:40:12log this text buffer what will happen?
  4244. 2:40:15So if you log this text buffer you are
  4245. 2:40:18going to see
  4246. 2:40:20can you guess? So you're going to see
  4247. 2:40:22bytes and this will be hexa decimal
  4248. 2:40:25format. Okay. So this is also very
  4249. 2:40:26important. So let's test it. So I'm just
  4250. 2:40:28going to save it and let's go to
  4251. 2:40:30package.json JSON.
  4252. 2:40:33So 08
  4253. 2:40:35and I'll do tsx source/. So we have 08
  4254. 2:40:39buffers.
  4255. 2:40:41TS save.
  4256. 2:40:45And then if I do clear and let's run
  4257. 2:40:47again and you can see that you're
  4258. 2:40:49getting buffer and this is you're
  4259. 2:40:51getting actually this is not normal text
  4260. 2:40:53right you can clearly see it. So this is
  4261. 2:40:55simply bytes in hexadimal format. Okay.
  4262. 2:40:59What will happen? Each uh value is one
  4263. 2:41:01by. So here each value is one by. So n
  4264. 2:41:06is actually uh sorry n is actually this
  4265. 2:41:094 e right you are seeing here. Then you
  4266. 2:41:12have o equal to this 6f then you have
  4267. 2:41:16what is node n o d right? So d is
  4268. 2:41:20becoming 64 like this right? So this is
  4269. 2:41:23how you can create buffer. So now let's
  4270. 2:41:25say what I need to do right I wanted to
  4271. 2:41:28convert this particular raw buffer into
  4272. 2:41:30my readable text only. So you have
  4273. 2:41:32something called two string method and
  4274. 2:41:34this two string method is going to
  4275. 2:41:35convert that bite into the normal text.
  4276. 2:41:37So for example
  4277. 2:41:40uh if I go here and let's say I'll do
  4278. 2:41:42log and I'm just going to do text buffer
  4279. 2:41:46dot to string another very important and
  4280. 2:41:48here you have to give the the type I
  4281. 2:41:50think we already explained UTF- 8. So
  4282. 2:41:53node you'll understand we are actually
  4283. 2:41:54working with normal text or we are
  4284. 2:41:56converting something to a normal text
  4285. 2:41:58and if I just enter here you can see
  4286. 2:42:00that you're getting node back right
  4287. 2:42:01makes sense. So that means technically
  4288. 2:42:04this text buffer whatever you have
  4289. 2:42:06created from this and this particular
  4290. 2:42:08line both are same. So this is in
  4291. 2:42:10readable view and not readable uh
  4292. 2:42:12readable means readable view only means
  4293. 2:42:14we can human readable view and this is
  4294. 2:42:17in buffer in bytes. Okay. So these are
  4295. 2:42:20one of the most important two concept.
  4296. 2:42:22Now let's understand some more uh
  4297. 2:42:24important methods that mostly we're
  4298. 2:42:26going to use. So something called buffer
  4299. 2:42:28length. So now let's I'm just going to
  4300. 2:42:30do here. So let's take a uh English
  4301. 2:42:32buffer. I'm just going to give any
  4302. 2:42:34random name whatever. So do buffer dot
  4303. 2:42:37from and maybe I'll do hello. Okay. And
  4304. 2:42:41now what I can do right I can simply
  4305. 2:42:43just do here English buffer.length.
  4306. 2:42:46And this is going to give you the length
  4307. 2:42:47here. So save this. And if I just run
  4308. 2:42:50this here. So we're going to get five.
  4309. 2:42:53Okay. Also uh here what actually five
  4310. 2:42:57means? So hello we have what? Five
  4311. 2:42:59characters. Correct. 1 2 3 4 5. Five
  4312. 2:43:02characters equal to five bytes. Okay. So
  4313. 2:43:05this will be your buff uh buffer.length.
  4314. 2:43:08So now you can also use another very
  4315. 2:43:10important method that is called
  4316. 2:43:12buffer.alloc.
  4317. 2:43:13This buffer.alloc is going to do what?
  4318. 2:43:15It's going to create a buffer with a
  4319. 2:43:17fixed number of bytes. Very important.
  4320. 2:43:19So what I'm talking about? also have
  4321. 2:43:21something called allocation. So in short
  4322. 2:43:24it's called alloc. So it's simply
  4323. 2:43:26allocation like this. So it's like con
  4324. 2:43:28I'm just going to do a fixed buffer. So
  4325. 2:43:30do fixed buffer.
  4326. 2:43:32So do buffer dot alloc alloc and then uh
  4327. 2:43:37you're going to pass let's say five. So
  4328. 2:43:39this is going to create a fixed number
  4329. 2:43:40of buffer. So here if I just log this
  4330. 2:43:44and let's say I'm just going to do empty
  4331. 2:43:45fixed empty fixed. This is empty buffer
  4332. 2:43:48but fixed buffer and this will be fixed
  4333. 2:43:51buffer here and if you just log it right
  4334. 2:43:53see what you're getting.
  4335. 2:43:57So you can see that it's going to create
  4336. 2:43:58empty buffer but it's going to create
  4337. 2:44:00fixed buffer means we have given five.
  4338. 2:44:02So it's going to create five.
  4339. 2:44:05Now what you can do right we have
  4340. 2:44:06another important see I already
  4341. 2:44:07mentioned whatever methods that I have
  4342. 2:44:09actually decided to cover. Remember
  4343. 2:44:11there is a reason I'm not doing all of
  4344. 2:44:13this blindly because you'll see whenever
  4345. 2:44:15you're going to work in a project mostly
  4346. 2:44:17you're going to use all of these only.
  4347. 2:44:18So buffer from buffer uh uh that buffer
  4348. 2:44:22dot to string buffer dot uh length
  4349. 2:44:25buffer dotalloc all of this and then we
  4350. 2:44:27have something called write. What write
  4351. 2:44:30is going to do? So what you can do right
  4352. 2:44:31you can write text into a existing
  4353. 2:44:33buffer also. Okay. Now consider what we
  4354. 2:44:36can do right. This fixed buffer is
  4355. 2:44:39currently having what five bytes. And
  4356. 2:44:42now what we are going to do right I'm
  4357. 2:44:43going to add let's say three more bytes.
  4358. 2:44:45So what I can do right I can take this
  4359. 2:44:47fixed buffer and I can do something like
  4360. 2:44:49write and this right is going to I'm
  4361. 2:44:51just going to add API or whatever you're
  4362. 2:44:53going to give. So what will happen this
  4363. 2:44:55API actually need three bytes. Okay. So
  4364. 2:44:57it's going to fit you're getting what
  4365. 2:44:59I'm talking about and the remaining
  4366. 2:45:00bytes because it has already has five.
  4367. 2:45:02the remaining bytes will remain zero.
  4368. 2:45:04It's very important. So what we are
  4369. 2:45:06simply doing, I'm going to repeat once
  4370. 2:45:07again. So we created fixed buffer of
  4371. 2:45:09five bytes, right? All are empty, right?
  4372. 2:45:12And now we are writing this API into
  4373. 2:45:14this buffer. Fixed buffer. So this is
  4374. 2:45:17going to take three bytes and this has
  4375. 2:45:18five. So two will remain empty. So now
  4376. 2:45:21if I just log here. So let's do fixed
  4377. 2:45:24buffer.
  4378. 2:45:27Fixed buffer up right.
  4379. 2:45:31And I'm simply going to do fixed buffer.
  4380. 2:45:36And I'll do log. And let's do fixed
  4381. 2:45:39buffer.
  4382. 2:45:41I'll do fixed buffer as text. So how we
  4383. 2:45:46going to do text? So do fixed buffer dot
  4384. 2:45:48two string.
  4385. 2:45:50And then we're going to use UTF. Right?
  4386. 2:45:53Save this and we'll see what is
  4387. 2:45:55happening. Right?
  4388. 2:46:00So you see we have see first one is
  4389. 2:46:03empty fixed buffer after right is three
  4390. 2:46:05because API is going to take three bytes
  4391. 2:46:06and rest two will remain empty and then
  4392. 2:46:09this is our fixed buffer as text is API
  4393. 2:46:11okay fantastic so that means this is how
  4394. 2:46:13you're going to do this also you can do
  4395. 2:46:16something like this you can use let's
  4396. 2:46:18discuss buffer concat method also
  4397. 2:46:20because what will happen right so
  4398. 2:46:22whenever you're going to work let's say
  4399. 2:46:23you're working in a real project and
  4400. 2:46:25you're getting data in chunks because
  4401. 2:46:27that is possible that when your data is
  4402. 2:46:29huge you are not going to get the data
  4403. 2:46:31at once. So what will happen? You're
  4404. 2:46:33going to get in chunks. So this will
  4405. 2:46:35called as chunk. Chunk one, chunk two
  4406. 2:46:37like this. What you can do right you can
  4407. 2:46:39actually
  4408. 2:46:40uh combine all of these into one buffer.
  4409. 2:46:42So let's say I'm just going to get
  4410. 2:46:44chunks. So I'm going to getting three
  4411. 2:46:46chunks right. So how how I'm getting? So
  4412. 2:46:48I have a buffer dot from. So this is my
  4413. 2:46:51first chunk and this is hello. Then I'm
  4414. 2:46:54going to give comma and I'm getting my
  4415. 2:46:56second chunk. So I do buffer. And I'll
  4416. 2:46:58do hello node.
  4417. 2:47:00And then I'm just going to do buffer dot
  4418. 2:47:03from and I'm just going to do hello
  4419. 2:47:05nodejs something like so I have three
  4420. 2:47:07chunks. What I can do right I can
  4421. 2:47:09combine all of these into one. So I do
  4422. 2:47:11combine buffer
  4423. 2:47:14and this will be chunks not chunk what
  4424. 2:47:17I'm doing. So buffer doconet
  4425. 2:47:20and you're going to use this chunks. So
  4426. 2:47:22what this is going to do this is going
  4427. 2:47:24to combine all of this into one and then
  4428. 2:47:27you're going to simply log this. If I
  4429. 2:47:29just do log here, combine buffer and
  4430. 2:47:32also let's do combine buffer.2 string
  4431. 2:47:35and I'm just going to do UTF8. So you'll
  4432. 2:47:37understand clearly what is happening
  4433. 2:47:39here. So let's log this [screaming]
  4434. 2:47:42and you can see that what you're
  4435. 2:47:44getting. So you're getting hello NodeJS
  4436. 2:47:46and this is the combined buffer. So
  4437. 2:47:47you're getting hello NodeJS. Makes
  4438. 2:47:49sense, right? So that means this
  4439. 2:47:51actually combined and created one
  4440. 2:47:53buffer. Okay. So I think this is done.
  4441. 2:47:56uh also I think I think this should be
  4442. 2:47:58enough right so what we have covered so
  4443. 2:48:00far so covered that buffer is going to
  4444. 2:48:01store raw bytes buffer from is going to
  4445. 2:48:04create buffer from data buffer tworing
  4446. 2:48:07is going to convert your text from your
  4447. 2:48:09buffer bufferlength is going to give you
  4448. 2:48:12the length buffer allocation is going to
  4449. 2:48:14do what it's going to create empty
  4450. 2:48:15buffer buffer write is going to write
  4451. 2:48:19your data into a buffer and buffer
  4452. 2:48:21concat is going to combine multiple
  4453. 2:48:23buffer chunks believe me if you can
  4454. 2:48:25master all of this right most of the
  4455. 2:48:27time you'll be able to work with buffers
  4456. 2:48:29okay so with this now let's move on to
  4457. 2:48:31the next one all right so moving to the
  4458. 2:48:33next one so let's create and normally
  4459. 2:48:36other videos I'm not sure I think they
  4460. 2:48:38usually not cover this but but I'm going
  4461. 2:48:40to cover this one okay this is another
  4462. 2:48:41important one and this will be URL
  4463. 2:48:43module okay so let's see in the next
  4464. 2:48:46part we going to have the HTTP module
  4465. 2:48:48right and there it will be actually
  4466. 2:48:49helpful and also when we are going to
  4467. 2:48:51actually implement our APIs and
  4468. 2:48:52everything so what we're going to do
  4469. 2:48:54right So this URL module is going to
  4470. 2:48:57help you to work with URLs uh safely
  4471. 2:49:00something like that. Okay. So because
  4472. 2:49:02see what will happen whenever you're
  4473. 2:49:04going to do any back end development
  4474. 2:49:05maybe you need some protocol information
  4475. 2:49:07or you need which host name you're using
  4476. 2:49:09or maybe you need the path or maybe you
  4477. 2:49:11need the query parameters mostly query
  4478. 2:49:13parameters and all you are going to use
  4479. 2:49:15a lot. So consider maybe I'm just going
  4480. 2:49:18to take this example right. So I'm just
  4481. 2:49:19going to do HTTP
  4482. 2:49:21HTTPS. Let's take API.ample example.com
  4483. 2:49:25whatever you want to take.example.com
  4484. 2:49:27example.com then I have / users and then
  4485. 2:49:29here I'm going to have let's I'm going
  4486. 2:49:30to use a paginess and page two and then
  4487. 2:49:33I'm using limit will be 20 or 10
  4488. 2:49:35whatever you wanted to take right so in
  4489. 2:49:36this case what will be your path name
  4490. 2:49:38your path name is this one so/ user page
  4491. 2:49:41is actually two and limit is 10 which is
  4492. 2:49:43what this is your query patterns that
  4493. 2:49:44you are actually doing right so let's uh
  4494. 2:49:47implement this one in a simpler way so
  4495. 2:49:48what I'm going to do right I'm going to
  4496. 2:49:50take a function and then I'm just going
  4497. 2:49:52to do run url demo uh let's return void
  4498. 2:49:56only we going to return and things will
  4499. 2:49:57do void and then I'm simply going to do
  4500. 2:49:59con API URL. So how we are going to do
  4501. 2:50:02here? We are going to create a API sorry
  4502. 2:50:04we are going to create a not API man we
  4503. 2:50:06are going to create a URL object. How
  4504. 2:50:08you going to create a URL object? So the
  4505. 2:50:10question is how you can create how to
  4506. 2:50:12create
  4507. 2:50:14URL object from URL string. Now this is
  4508. 2:50:18very important. Okay you need to know
  4509. 2:50:20all of this. You see that in most
  4510. 2:50:21project we usually use this. we have to
  4511. 2:50:24do new URL and here you have to pass the
  4512. 2:50:27uh string. So for example let's I'm just
  4513. 2:50:29going to take https colon and I'm do
  4514. 2:50:32let's say apias devob I'm just going to
  4515. 2:50:35take a debub as example and here I'm
  4516. 2:50:37just going to do user information I'm
  4517. 2:50:39getting and here I'll do page equal to
  4518. 2:50:41two and then let's say let's take a
  4519. 2:50:44limit the limit will be 10 and also I'm
  4520. 2:50:46just going to take a sort sort will be
  4521. 2:50:48latest something like this
  4522. 2:50:52okay so now what I'm doing don't need
  4523. 2:50:55this okay so now what we going to do
  4524. 2:50:57Right. Oh, I think it was correct only.
  4525. 2:51:00So here I thought I've taken this one in
  4526. 2:51:02middle. So here I'm just going to log
  4527. 2:51:03couple of things. Okay. So I'll do log
  4528. 2:51:06and let's do full URL. So how you are
  4529. 2:51:08going to get the full URL. So you have
  4530. 2:51:10to do something like this. So do API
  4531. 2:51:11URL. Here. So this is going to give you
  4532. 2:51:14the full URL. Okay. And [snorts] before
  4533. 2:51:16that also let's do one thing. We are
  4534. 2:51:18going to run this. Okay. So I'm just
  4535. 2:51:20going to run this here. So I'll do run
  4536. 2:51:22URL demo. Save this. Uh let's copy this.
  4537. 2:51:26go to package dojson and here I'll do 09
  4538. 2:51:31and I'll do tsx source/ this
  4539. 2:51:36and let's run one by one. So I'll do 09
  4540. 2:51:40and I'll see that you're going to get
  4541. 2:51:41the full URL. Okay, so this is going to
  4542. 2:51:43give you the full URL. Now what I have
  4543. 2:51:45to get right so a couple of things I
  4544. 2:51:47need to get. So I need to get the
  4545. 2:51:48protocol. So you can do API URL
  4546. 2:51:50dotprotocol.
  4547. 2:51:52You're going to get API URL dot host
  4548. 2:51:55name. This is going to give you the host
  4549. 2:51:56name. You're going to get the path name
  4550. 2:51:58API URL.path name and very very
  4551. 2:52:02important you are also going to get your
  4552. 2:52:04search uh string or query string. How?
  4553. 2:52:07So you'll do AP URL dot search. And if
  4554. 2:52:10you save all of these and let's run all
  4555. 2:52:12of these and see what you're getting.
  4556. 2:52:18Okay. So you're getting this. This is
  4557. 2:52:20the https your protocol. Then you're
  4558. 2:52:22going to have your host name
  4559. 2:52:23api.hdep.com. debugab.com this is your
  4560. 2:52:25path name and this is your search or
  4561. 2:52:27query string right and this is your
  4562. 2:52:29complete one complete means your href so
  4563. 2:52:32this all the information you're going to
  4564. 2:52:34get now what actually search param
  4565. 2:52:36usually do right so there is a method
  4566. 2:52:38method called search panam one of the
  4567. 2:52:40most important concept that you need to
  4568. 2:52:42know whenever you'll be working with
  4569. 2:52:43mostly you are going to work with filter
  4570. 2:52:45search sort all these things okay so
  4571. 2:52:48what is search pam so search param is
  4572. 2:52:51going to give you the capacity to read
  4573. 2:52:53query parameters. Okay, so query
  4574. 2:52:55parameters means where it will start
  4575. 2:52:58with normally with this. So you have
  4576. 2:52:59your uh URL and then you have something
  4577. 2:53:01starting with this question mark and
  4578. 2:53:03after right side whatever you're going
  4579. 2:53:04to have usually you'll call this one as
  4580. 2:53:06query parameters. Okay. So now let's say
  4581. 2:53:09I wanted to get my page information. So
  4582. 2:53:11what I can do right I will do API URL
  4583. 2:53:14and this is going to give me my search
  4584. 2:53:15params and this search param is going to
  4585. 2:53:17give you this. See we have get append
  4586. 2:53:19delete entries blah blah lot of things
  4587. 2:53:21you're going to get get because I need
  4588. 2:53:23to get the information of page. So this
  4589. 2:53:25will be your key and this will be your
  4590. 2:53:27value. So my page is the key value is
  4591. 2:53:29will be two. Limit is key. 10 is value.
  4592. 2:53:32Sort is key. Latest is value. So here I
  4593. 2:53:34wanted to get page. So I'll do page
  4594. 2:53:36here. So this is going to give me page
  4595. 2:53:38information. Copy this. And now let's
  4596. 2:53:40say I wanted to get my limit.
  4597. 2:53:43So this will be my limit.
  4598. 2:53:46And also I'm just going to get a sort.
  4599. 2:53:48This will be my sort. And then if I just
  4600. 2:53:50log all of this and you're going to get
  4601. 2:53:52page
  4602. 2:53:54limit and sort, right? You save all of
  4603. 2:53:57these. For now, I'll comment it out.
  4604. 2:53:59This one here
  4605. 2:54:02and then run it. So you're going to get
  4606. 2:54:042 10 and latest. So you're getting 2 10
  4607. 2:54:07and latest. So this is where you're
  4608. 2:54:09going to use called search params. Okay.
  4609. 2:54:12You can also update the search params.
  4610. 2:54:15how you're going to update. So let's I
  4611. 2:54:16wanted to update my page from two from
  4612. 2:54:19two to three, right? I can do that. So
  4613. 2:54:22I'll do API URL dot search param and
  4614. 2:54:24then you're going to do set and set is
  4615. 2:54:26will be your key value. So key is your
  4616. 2:54:28page value will be your let's say 10. So
  4617. 2:54:32from uh this will be string. Okay. So
  4618. 2:54:35this from two it will be changed to 10.
  4619. 2:54:38Let's say you wanted to change your
  4620. 2:54:39limit from 10 to 20. So I can do like
  4621. 2:54:42this. So I'll do apurl searchparence dot
  4622. 2:54:45set and this will be what is that limit
  4623. 2:54:48limit will be maybe 20. Okay. So now
  4624. 2:54:52what I can do right I can just log it
  4625. 2:54:54here. So I do log and I'll do apur.h
  4626. 2:54:58and see what will happen. Okay. And if I
  4627. 2:55:00just run it.
  4628. 2:55:03So you see that previously you have 210
  4629. 2:55:05latest. Now your page is 10 limit is 20.
  4630. 2:55:08Sort is latest. Means using this set you
  4631. 2:55:11will be able to update these values
  4632. 2:55:13also. Okay. Now you have also another
  4633. 2:55:16very very important something called URL
  4634. 2:55:18search params. This also you can use
  4635. 2:55:20separately. Now when this is very
  4636. 2:55:22helpful let's say wanted to build some
  4637. 2:55:24query string. So in that case URL search
  4638. 2:55:27param is very very very important
  4639. 2:55:29concept very important mean like too
  4640. 2:55:31important because you are actually going
  4641. 2:55:33to use this one in a real project. So
  4642. 2:55:35how you can do this? So let's I wanted
  4643. 2:55:36to create some query params. So I'll do
  4644. 2:55:39new URL search params and this is where
  4645. 2:55:42you can actually give let's say my
  4646. 2:55:43search value is nodej
  4647. 2:55:46my page is one and my limit is
  4648. 2:55:52my limit is let's say five and now what
  4649. 2:55:54I can do right I can simply log this so
  4650. 2:55:57I do log and you can do query params and
  4651. 2:56:00you can do two string
  4652. 2:56:02query params dot two string okay so now
  4653. 2:56:07let's save this And if I just log it
  4654. 2:56:09here, you'll see that what you're going
  4655. 2:56:10to get.
  4656. 2:56:13See, you're getting search nodejs and
  4657. 2:56:16page equal to 1 and limit equal to 5.
  4658. 2:56:19That means this is going to create a
  4659. 2:56:20search param which you can also append
  4660. 2:56:22in your URL. Okay. So this is all the
  4661. 2:56:25things that you are going to uh or you
  4662. 2:56:28can use from your URL module. Okay. So
  4663. 2:56:30these all are very important and
  4664. 2:56:32whatever we have discussed in this
  4665. 2:56:33particular module that actually you're
  4666. 2:56:35going to use in a project. So with this
  4667. 2:56:37now let's move on to the next one. Okay.
  4668. 2:56:38So moving to the next one. So let's
  4669. 2:56:40understand one more and this will be so
  4670. 2:56:42we are going to understand event
  4671. 2:56:44emitters. Okay. So let's do event
  4672. 2:56:47emitter.ts.
  4673. 2:56:49So first we'll understand that what
  4674. 2:56:51actually event emitter is. Now event
  4675. 2:56:53emitter will be or say event emitter
  4676. 2:56:55you're going to use when let's say you
  4677. 2:56:57have
  4678. 2:56:59consider this one that you have one part
  4679. 2:57:01of application that want to announce
  4680. 2:57:02that something happened and other part
  4681. 2:57:05of that application that part want to
  4682. 2:57:07listen to that particular that okay this
  4683. 2:57:09this is something happened and based on
  4684. 2:57:11this I wanted to do something okay so
  4685. 2:57:14simple example will be let's say uh you
  4686. 2:57:17are implementing a functionality okay
  4687. 2:57:19and the functionality is that user
  4688. 2:57:20actually registered
  4689. 2:57:22So once user will comes to your
  4690. 2:57:24application they're going to give your
  4691. 2:57:25email password or whatever right uh name
  4692. 2:57:27blah blah once user registered so
  4693. 2:57:30consider this one as a one part of your
  4694. 2:57:32app so it's doing something right so
  4695. 2:57:35once user will register after this we
  4696. 2:57:38are going to do something okay so for
  4697. 2:57:40example let's say there can be
  4698. 2:57:42possibility that okay we have a feature
  4699. 2:57:44that once user will register we need to
  4700. 2:57:46send a welcome email correct or maybe
  4701. 2:57:50let's say we need to write a log or
  4702. 2:57:54maybe let's say we need to notify some
  4703. 2:57:56other service some other service that
  4704. 2:57:57you have
  4705. 2:58:00okay notify some other service so now if
  4706. 2:58:04you see that this is like a two-way
  4707. 2:58:06thing is happening two-way in the sense
  4708. 2:58:07means I'm trying to explain in a simpler
  4709. 2:58:09way that you are doing something so this
  4710. 2:58:11is what you're doing and based on that
  4711. 2:58:13you want to do something so based on
  4712. 2:58:15user registration you want to do some
  4713. 2:58:17other activity okay so this is where we
  4714. 2:58:20have something called event right so
  4715. 2:58:22this is like event so it's like I'm not
  4716. 2:58:25sure if you're familiar with Kafka so
  4717. 2:58:27it's like a you have a producer so
  4718. 2:58:29something is producing right something
  4719. 2:58:32and that producer is sending that
  4720. 2:58:35information to a uh mediator and then
  4721. 2:58:38consumer is going to consume it so it's
  4722. 2:58:40like this right so you have a producer
  4723. 2:58:42consumer so consider like this so here
  4724. 2:58:45what we have to do right so event
  4725. 2:58:47emitter is going to let's say emit one
  4726. 2:58:49particular event and listener what
  4727. 2:58:52they're going to do? So listeners is
  4728. 2:58:54going to handle that particular listener
  4729. 2:58:56is going to listen to that event and
  4730. 2:58:58based on this is going to do something.
  4731. 2:59:00So for example, let's say here we have
  4732. 2:59:02something like this. So event emitter is
  4733. 2:59:04going to emit one event and then you
  4734. 2:59:07have listeners. Listeners is going to
  4735. 2:59:10listen to this event
  4736. 2:59:13and then it's going to do something.
  4737. 2:59:14It's going to do something based on
  4738. 2:59:16this.
  4739. 2:59:18So here we have some method that you
  4740. 2:59:19need to know which are very important.
  4741. 2:59:21So first one will be
  4742. 2:59:24do on. So what this is going to do? So
  4743. 2:59:26this is going to register
  4744. 2:59:28one listener. So this is the first
  4745. 2:59:30thing. Okay. So every time let's say
  4746. 2:59:34there is a event and that event is
  4747. 2:59:36emitted it's going to reg it's going to
  4748. 2:59:39run and register one listener. Okay. So
  4749. 2:59:41dot on method. Then you have something
  4750. 2:59:44called dot once. Now this is also
  4751. 2:59:47important. Now what this is going to do
  4752. 2:59:48this is going to register a particular
  4753. 2:59:50listener. So this is also register one
  4754. 2:59:52listener but the difference is it's
  4755. 2:59:54going to run only once. That run that
  4756. 2:59:57runs only one time. Okay. And then you
  4757. 3:00:02have something called dot emit. So dot
  4758. 3:00:04emit is going to do what? It's going to
  4759. 3:00:06triggers an event and it's going to send
  4760. 3:00:10that event to the listeners.
  4761. 3:00:13So mostly you need to master all these
  4762. 3:00:15three things. Okay. So first of all
  4763. 3:00:17let's say we will create uh so let's
  4764. 3:00:19take con and I'll do app events. Maybe
  4765. 3:00:22let's do app events only and then I'm
  4766. 3:00:24simply going to do new and here I'm just
  4767. 3:00:26going to do event emitter. This event
  4768. 3:00:28emitter you're going to import from node
  4769. 3:00:31events. Okay. So this will be from node
  4770. 3:00:34colon events and this event emitter. So
  4771. 3:00:37this will be your event emitter. Right.
  4772. 3:00:39So here now what we going to do right?
  4773. 3:00:41So let's say we are let's take this
  4774. 3:00:44example only user registration. So I'm
  4775. 3:00:46just going to take one type and I'll do
  4776. 3:00:49user register payload
  4777. 3:00:54and here we're going to have a num ID
  4778. 3:00:56which will be number and also we're
  4779. 3:00:58going to have a email which will be
  4780. 3:01:00string. Okay.
  4781. 3:01:03Now what I'm going to do right so here
  4782. 3:01:05let's say I have a listener one first
  4783. 3:01:07listener. So when this listener will run
  4784. 3:01:10whatever we are going to write. So
  4785. 3:01:11whenever let's say we have a event and
  4786. 3:01:14that event is going to do something. So
  4787. 3:01:16whenever user will register we are going
  4788. 3:01:18to emit one particular event and once
  4789. 3:01:20we're going to emit that event listener
  4790. 3:01:23is going to listen to that particular
  4791. 3:01:25event and listener is going to run.
  4792. 3:01:27Okay. So in this case in real example
  4793. 3:01:29will be once you'll see that once you
  4794. 3:01:31register any particular website you'll
  4795. 3:01:33see that you are going to get some
  4796. 3:01:35welcome email. Correct. So it's
  4797. 3:01:36something like this. So here what I'm
  4798. 3:01:38going to do right so I will be having so
  4799. 3:01:40I'm just going to app events do on and
  4800. 3:01:43here I'm going to pass a event name this
  4801. 3:01:45event name will be once user will
  4802. 3:01:48register so this is my event name
  4803. 3:01:50consider okay user registered and once
  4804. 3:01:53user will register so whenever this
  4805. 3:01:55particular we are going to emit this
  4806. 3:01:58particular event we are going to pass
  4807. 3:01:59some event okay sorry pass some payload
  4808. 3:02:01and that payload we are going to receive
  4809. 3:02:03here but before doing this what we going
  4810. 3:02:05to do like maybe let's say uh what I'm
  4811. 3:02:08going to do right let's keep it like
  4812. 3:02:09this okay I'm just going to create one
  4813. 3:02:12function and let's do register user so
  4814. 3:02:15this is my actual function where you're
  4815. 3:02:17going to do something so here for now
  4816. 3:02:19I'm just going to give void and I'll do
  4817. 3:02:21con user and user is going to have ID
  4818. 3:02:24one and let's do user is going to have a
  4819. 3:02:26email and I'll take my email here
  4820. 3:02:28whatever you want to take now what I'm
  4821. 3:02:30going to do right so first of all I'm
  4822. 3:02:32just going to do log and I'll do user
  4823. 3:02:34saved so in n in actual application what
  4824. 3:02:36you're going to do you're going to do
  4825. 3:02:37some DB operation and you are going to
  4826. 3:02:39save the user to your DB something like
  4827. 3:02:41this correct and now this is where you
  4828. 3:02:43are going to emit the event see remember
  4829. 3:02:45you need to emit so once you emit the
  4830. 3:02:47event your listener is going to listen
  4831. 3:02:49to that event so here I'm just going to
  4832. 3:02:51do app events do emit and here we are
  4833. 3:02:53going to give the event name okay now
  4834. 3:02:55remember whatever you're emitting the
  4835. 3:02:57event name and the listener event name
  4836. 3:02:59should remain same so here we are
  4837. 3:03:02actually emitting uh that okay user is
  4838. 3:03:04now registered And then we are going to
  4839. 3:03:06pass a user payload. So this will be my
  4840. 3:03:09payload or this will be my data this
  4841. 3:03:11particular event emitter uh event
  4842. 3:03:12emitter is emitting correct. [snorts] So
  4843. 3:03:15now what will happen when I'm going to
  4844. 3:03:16listen to this particular event this
  4845. 3:03:19data whatever I'm sending here this
  4846. 3:03:22listener will be able to access and
  4847. 3:03:24based on this you can do whatever you
  4848. 3:03:25want. For example let's say wanted to
  4849. 3:03:26send a welcome email you can do this
  4850. 3:03:28right. For example, in this user, you
  4851. 3:03:31have the email information. So, we'll be
  4852. 3:03:32able to send that uh email to that
  4853. 3:03:34particular user. So, this is how it will
  4854. 3:03:36work. So, now what you can do, right?
  4855. 3:03:38When this listener is going to listen,
  4856. 3:03:40it will actually match this particular
  4857. 3:03:42event, whatever event we have. And now,
  4858. 3:03:45this is where we are going to get our
  4859. 3:03:46payload. So, here I'm just going to do
  4860. 3:03:48us a register payload.
  4861. 3:03:51And here what we can do can let's say
  4862. 3:03:52I'm just going to log here and I'm just
  4863. 3:03:54going to do email
  4864. 3:03:57listener
  4865. 3:03:59and this email listener is going to do
  4866. 3:04:01let's I'm just going to do a welcome
  4867. 3:04:02email send to
  4868. 3:04:05this user
  4869. 3:04:08and here you're going to pass user do
  4870. 3:04:10email okay so this is going to do that
  4871. 3:04:13now what you can do right this is a very
  4872. 3:04:15important concept that you can have
  4873. 3:04:17multiple listener for the same event
  4874. 3:04:19okay so let's say you wanted to create
  4875. 3:04:21one more listener here. Okay. So maybe
  4876. 3:04:23I'm just going to do app events do on
  4877. 3:04:26and I'm going to give same uh event name
  4878. 3:04:28user register.
  4879. 3:04:32Okay. And then let's say I'm just going
  4880. 3:04:34to take the same thing. So I'll do user
  4881. 3:04:36will be user uh payload. And this is
  4882. 3:04:39where I'm simply going to do what maybe
  4883. 3:04:41let's say I'm just going to log event
  4884. 3:04:43right. So something like this. And here
  4885. 3:04:45I'm just going to do log listener. And
  4886. 3:04:48then I'm just going to do user. Username
  4887. 3:04:50is user do ID and email is and here I'm
  4888. 3:04:55just going to do user do email. Okay. So
  4889. 3:04:58that means you can do what right? You
  4890. 3:05:00can actually register multiple listener
  4891. 3:05:02for the same event also. Okay. So this
  4892. 3:05:06is the first thing that we need to
  4893. 3:05:07understand. Now also let's do one thing.
  4894. 3:05:09After this what we can do right? I'm
  4895. 3:05:10just going to do a log and I'm simply
  4896. 3:05:13going to do register user. And here I'll
  4897. 3:05:17do event listener sorry event listener
  4898. 3:05:22completed something like this okay and
  4899. 3:05:25now let's say first of all I'm just
  4900. 3:05:27going to log this register user here and
  4901. 3:05:29then we're going to understand one more
  4902. 3:05:30concept but this is how it will work
  4903. 3:05:32okay this is how actually in the project
  4904. 3:05:35also we're going to implement so at the
  4905. 3:05:37end it's going to emit event and on the
  4906. 3:05:39other side the listener is going to
  4907. 3:05:41listen to that event and it's going to
  4908. 3:05:42do a real job okay so now let's go here
  4909. 3:05:45I'm just going to copy this and I'll do
  4910. 3:05:4810 ts source/ this and if I go here
  4911. 3:05:56let's run this
  4912. 3:05:59so user saved email listener welcome
  4913. 3:06:01email sent to user sum log listener so
  4914. 3:06:04that means you can see we emitate the
  4915. 3:06:06event only once but we have a two
  4916. 3:06:08listener so both listener will work okay
  4917. 3:06:10and you have log listener and register
  4918. 3:06:13user event listener completed so this is
  4919. 3:06:15how it's going to work. So now we have
  4920. 3:06:17one more concept and that is that how
  4921. 3:06:18you can use once. Once means this one
  4922. 3:06:21register one listener that run only one
  4923. 3:06:23time. So now let's say what I can do
  4924. 3:06:26right I can create one more here. So I
  4925. 3:06:27do app events do on let's have one more
  4926. 3:06:30event here and that is app dot start
  4927. 3:06:32means once our app will start we wanted
  4928. 3:06:34to do something. Okay and here I'm
  4929. 3:06:38simply going to do log and let's do once
  4930. 3:06:41listener. So I'll do once listener and
  4931. 3:06:44now uh app
  4932. 3:06:48app started let's move here. So app
  4933. 3:06:51started something like this. So whenever
  4934. 3:06:53our app will run that time I wanted to
  4935. 3:06:55run this and now what I'm going to do
  4936. 3:06:57right see notice if I go here and I'll
  4937. 3:07:00do app events do emit
  4938. 3:07:02and I have this app dot started this
  4939. 3:07:05event.
  4940. 3:07:07So I'm going to emit this and then I'm
  4941. 3:07:09also going to emit this one more time.
  4942. 3:07:12But what will happen right? Because we
  4943. 3:07:13are using once even though we are
  4944. 3:07:15emitting twice listener will be
  4945. 3:07:18registered with only once because we
  4946. 3:07:20have once here. Oh we need to do once
  4947. 3:07:22here we need to do once. Okay. So this
  4948. 3:07:24will be once. So it will only run first
  4949. 3:07:26thing and if I go here let's run this
  4950. 3:07:29again.
  4951. 3:07:31So you see that once listener app
  4952. 3:07:33started user saved email listener and
  4953. 3:07:35all this. So this will be the uh concept
  4954. 3:07:38of uh event listener that what actually
  4955. 3:07:41event is the very important method how
  4956. 3:07:43we are going to emit event and how
  4957. 3:07:45listener is going to listen to that
  4958. 3:07:46event. So with this now let's move on to
  4959. 3:07:48the next one. So with the core modules I
  4960. 3:07:50think we are almost at the end and now
  4961. 3:07:52let's finish this particular section
  4962. 3:07:54with so I'm just going to 11 and we are
  4963. 3:07:56going to understand streams okay streams
  4964. 3:07:58are also very important. So first of all
  4965. 3:08:02you have to understand again as usual
  4966. 3:08:03right that when you're going to use
  4967. 3:08:05stream and why actually you need it. So
  4968. 3:08:07first of all streams are used when the
  4969. 3:08:09data uh you want to handle. So whatever
  4970. 3:08:11data you're handling piece by piece.
  4971. 3:08:14Okay. So you want to handle the data
  4972. 3:08:15piece by piece. Okay. Now what I mean by
  4973. 3:08:18this? So here what you're doing right
  4974. 3:08:20you are not loading the data not loading
  4975. 3:08:22the data not loading the data like
  4976. 3:08:26everything at once. Everything
  4977. 3:08:29at once. So this will be better way to
  4978. 3:08:31put it. So you're not loading the data
  4979. 3:08:32all at once. You are handling it piece
  4980. 3:08:35by piece. Right? Now the best example of
  4981. 3:08:38you uh stream using will be let's say
  4982. 3:08:40you wanted to read some large files.
  4983. 3:08:43Okay. Or you want to upload files. This
  4984. 3:08:46is also very very important. This is
  4985. 3:08:48where you can actually use streams.
  4986. 3:08:50Okay. Or same will be downloading. So
  4987. 3:08:52you want to download files. Let's say
  4988. 3:08:54you're downloading very big files. So
  4989. 3:08:56instead of downloading all at once,
  4990. 3:08:57you're downloading piece by piece.
  4991. 3:08:58Right? Some sometime you are also going
  4992. 3:09:01to use let's say you're improving audio
  4993. 3:09:02video uh processing that also you can
  4994. 3:09:06use. Now see whatever this uh we are
  4995. 3:09:09discussing that all these use cases are
  4996. 3:09:10actually real and you are actually going
  4997. 3:09:12to use this in projects also you need
  4998. 3:09:14this one sometimes in compressions also.
  4999. 3:09:18So this is will be where you're going to
  5000. 3:09:20use uh streams. Okay. Now uh first of
  5001. 3:09:23all what will happen right the main idea
  5002. 3:09:25or the core concept of stream will be
  5003. 3:09:27that stream doesn't give you the data
  5004. 3:09:29immediately. So it's not going to give
  5005. 3:09:30you the data immediately. It's going to
  5006. 3:09:32give you the data is in small and we
  5007. 3:09:35already know this particular term that
  5008. 3:09:37we have discussed and that is called
  5009. 3:09:39chunk. Right? So remember I already
  5010. 3:09:41mentioned this one chunks. So here it's
  5011. 3:09:43going to give you the data in small
  5012. 3:09:45pieces that we call as chunks. Okay.
  5013. 3:09:48So now how it's going to do? So let's
  5014. 3:09:51say I have this that okay here is here
  5015. 3:09:54is my full 500 MB file something like
  5016. 3:09:59this stream what it's going to do it's
  5017. 3:10:01going to do this okay so look here is
  5018. 3:10:03chunk one
  5019. 3:10:06then it's it will do here is chunk two 3
  5020. 3:10:114 5 something like this okay so it's
  5021. 3:10:14going to give you in chunk why now first
  5022. 3:10:16of all one of the most advantage would
  5023. 3:10:18be this will be memory efficient because
  5024. 3:10:20you can Right? We are not handling all
  5025. 3:10:22the data at once. So there will be not
  5026. 3:10:25too much overloading on memory. Right?
  5027. 3:10:26So this will be memory efficient. Okay.
  5028. 3:10:29Now u also you also need to understand
  5029. 3:10:32some of the stream types. Okay. I I
  5030. 3:10:34forgot to mention this. So for example,
  5031. 3:10:35let's say we have something called uh
  5032. 3:10:38stream types. Stream types are actually
  5033. 3:10:40important. So we have readable stream,
  5034. 3:10:42right? So you have readable readable
  5035. 3:10:44streams. I'm not sure if the spelling is
  5036. 3:10:46wrong. If it is wrong, I think readable.
  5037. 3:10:48Okay. So readable stream. Readable
  5038. 3:10:50streams will be or readable stream will
  5039. 3:10:52be like where you have some data. So you
  5040. 3:10:54have some source of data something like
  5041. 3:10:56this.
  5042. 3:10:57Okay. Now here maybe let's say example
  5043. 3:11:00will be uh what maybe HTTP request body
  5044. 3:11:04or file you're going to read some data
  5045. 3:11:06or read some data from a file big file
  5046. 3:11:08right so this will be readable then you
  5047. 3:11:11have something called writable stream.
  5048. 3:11:14Writable stem will be like uh there will
  5049. 3:11:16be some place or some uh I don't know
  5050. 3:11:19the English term man okay uh destination
  5051. 3:11:22so there will be some destination where
  5052. 3:11:23you're going to data you want to write
  5053. 3:11:25the data so there will be destination
  5054. 3:11:28where the data where the data is written
  5055. 3:11:32okay and then you have something called
  5056. 3:11:35transform transform transform stream now
  5057. 3:11:38this is also very important transform
  5058. 3:11:40means it's kind of a stream that's going
  5059. 3:11:42to read your data it's is going to
  5060. 3:11:44change it also. Okay. And then very
  5061. 3:11:47important, it's going to pass that
  5062. 3:11:48stream to forward. So pass it forward.
  5063. 3:11:50So it's going to read the data.
  5064. 3:11:53Read the data if needed. It's going to
  5065. 3:11:55change it. So it's going to change the
  5066. 3:11:56data and then it's going to pass the
  5067. 3:11:58data. Pass that pass that forward. Okay.
  5068. 3:12:04So example will be let's say this one
  5069. 3:12:06only. Compression will be a proper
  5070. 3:12:07example for this, right? Or maybe let's
  5071. 3:12:09say you want to the basic example. Let's
  5072. 3:12:11say you wanted to convert your text to
  5073. 3:12:13upper case. So we are going to see some
  5074. 3:12:14example but from a definition wise
  5075. 3:12:16right. So all these key points whatever
  5076. 3:12:18I have explained are actually very
  5077. 3:12:19important. So let's start with readable.
  5078. 3:12:21Readable means I already mentioned. So
  5079. 3:12:23there are some method you need to also
  5080. 3:12:25understand. So let's create a readable
  5081. 3:12:26stream. So I'm just going to do readable
  5082. 3:12:29and I'll do string. Okay. What you're
  5083. 3:12:31going to do right you're going to use
  5084. 3:12:32something called readable. And this
  5085. 3:12:34readable from where you're going to use
  5086. 3:12:36this readable you're going to import
  5087. 3:12:38from this node uh stream. So you can see
  5088. 3:12:40that you have this one. So vable you're
  5089. 3:12:42going to use from your stream package.
  5090. 3:12:44So you have node colon stream.
  5091. 3:12:47Okay. So what this is going to do right.
  5092. 3:12:49So first of all let's create this. So
  5093. 3:12:50I'm just going to readable and you have
  5094. 3:12:52to use this form. So here what this is
  5095. 3:12:55going to do this is going to create a
  5096. 3:12:56readable string but it's going to create
  5097. 3:12:58from an array. Each item that you're
  5098. 3:13:00going to have in this particular array
  5099. 3:13:02will become what will become chunks.
  5100. 3:13:04Chunks are very important. Okay. So
  5101. 3:13:06whatever array you are going to have is
  5102. 3:13:08it will become chunks. How you're going
  5103. 3:13:10to do this? So we're going to do like
  5104. 3:13:11this. So every data let's have a hello
  5105. 3:13:14give space uh space let's say maybe I'll
  5106. 3:13:17do from let's do nodejs node js and I'm
  5107. 3:13:22just going to do streams something like
  5108. 3:13:24this right. So this will be readable
  5109. 3:13:28stream. So very simple one. Now let's
  5110. 3:13:31understand transform one. So what you're
  5111. 3:13:33going to do right maybe let's uh take
  5112. 3:13:35that example that we are going to
  5113. 3:13:36convert some text to upper case. So what
  5114. 3:13:38I'll do right I'm just going to create a
  5115. 3:13:40const and I'll do uppercase
  5116. 3:13:43transform.
  5117. 3:13:46So here we're going to use something
  5118. 3:13:48called new transform and this transform
  5119. 3:13:50you're going to import from node stream
  5120. 3:13:51only. So what this is going to do right.
  5121. 3:13:54So here you are going to use a transform
  5122. 3:13:56method. So you can see that you have a
  5123. 3:13:57transform. Transform is going to take a
  5124. 3:13:59chunk encoding and there will be a call
  5125. 3:14:01back. Okay. Chunk. What is chunk? Chunk
  5126. 3:14:04is usually buffer in the buffer concept
  5127. 3:14:06we have discussed right. So chunk will
  5128. 3:14:07be buffer. What we are going to do right
  5129. 3:14:09here? Let's say we have some uh string
  5130. 3:14:12and then we are going to convert that
  5131. 3:14:14chunk to a string that also discussed
  5132. 3:14:16that you can directly do chunk dot to
  5133. 3:14:18string and based on that you can do
  5134. 3:14:19certain operation and at the end what
  5135. 3:14:21you can do you can pass that result to
  5136. 3:14:23the call back. Call back format also you
  5137. 3:14:25have discussed the classic callback
  5138. 3:14:27format call back and then you're going
  5139. 3:14:29to have error and then you're going to
  5140. 3:14:30have result. You remember this part I
  5141. 3:14:32discussed. So consider so here first of
  5142. 3:14:34all let's take this one at transform. So
  5143. 3:14:36in this transform this is going to take
  5144. 3:14:38chunk this is also going to take the
  5145. 3:14:41encoding and this is also going to take
  5146. 3:14:43a callback which is very very important.
  5147. 3:14:45Okay. So now let's say I wanted to do
  5148. 3:14:47something here. So first of all I will
  5149. 3:14:49be having a text and then I'm just going
  5150. 3:14:51to do chunk dot twing.
  5151. 3:14:54Okay. And here so let's take the call
  5152. 3:14:58back. So error will be null and I'm just
  5153. 3:15:00going to do so this is what I'm just
  5154. 3:15:01going to do. Text dot two uppercase.
  5155. 3:15:05So we're going to do text dot two
  5156. 3:15:07uppercase. So what we are doing right we
  5157. 3:15:09are actually sending this transform data
  5158. 3:15:12this transform data where so it's going
  5159. 3:15:14to send this data forward. Okay. Null
  5160. 3:15:17means we don't have any error. Okay. And
  5161. 3:15:19this result is nothing but it will be
  5162. 3:15:21the new chunk that is going to move to
  5163. 3:15:24the next string. Now I know that this
  5164. 3:15:26might be little tricky but you need to
  5165. 3:15:28understand all of this right that how
  5166. 3:15:29readable uh transform and writable all
  5167. 3:15:32these string works or that there is no
  5168. 3:15:33point because these are very very basic
  5169. 3:15:35concept. Now what we're going to do
  5170. 3:15:37right now let's write one writable
  5171. 3:15:39string. So writable string is going to
  5172. 3:15:40do it's going to receive the chunk and
  5173. 3:15:43then it's going to do something we don't
  5174. 3:15:44know what is why because that is going
  5175. 3:15:46to depend uh based on the project or
  5176. 3:15:48based on the feature you are doing.
  5177. 3:15:50Okay. So what this is going to do right
  5178. 3:15:52simple way just remember or you can note
  5179. 3:15:54it down writable is going to write data
  5180. 3:15:56to a particular file or it's possible
  5181. 3:15:59it's going to send data to HTTP server
  5182. 3:16:01also or maybe let's say it's going to
  5183. 3:16:02upload data to some cloud that is also
  5184. 3:16:06possible. Okay. So now
  5185. 3:16:09uh let's do writable stream. So how you
  5186. 3:16:12can do write you are going to use
  5187. 3:16:13something called writable. So you'll see
  5188. 3:16:15that we have writable this one. Okay.
  5189. 3:16:18And now here you're going to do
  5190. 3:16:19something called write. See, write also
  5191. 3:16:21is going to receive what? Chunk,
  5192. 3:16:22encoding, call back. So, I'm just going
  5193. 3:16:24to take right. So, I'll do chunk. Uh, we
  5194. 3:16:27have the encoding and the call back. So,
  5195. 3:16:29do encoding and the call back. So, first
  5196. 3:16:33I'm just going to log something here and
  5197. 3:16:35let's do received
  5198. 3:16:38chunk and here I'm simply going to do
  5199. 3:16:40chunk dot to string. So, this is the
  5200. 3:16:44first thing. So, now what we're going to
  5201. 3:16:46do right, we are simply going to call
  5202. 3:16:48this callback. You have to understand
  5203. 3:16:50till this time we have already discussed
  5204. 3:16:52that multiple times what actually call
  5205. 3:16:54back is but why we need this call back
  5206. 3:16:56here. What this call back is going to
  5207. 3:16:57do. So this call back is going to tell
  5208. 3:16:59node that okay I already have handled
  5209. 3:17:01this particular chunk. Now you can send
  5210. 3:17:05this one to next one. Simple. Okay. So
  5211. 3:17:08this means this call back is simply
  5212. 3:17:10telling that okay I'm done with my job.
  5213. 3:17:11Now you do the rest or move this one to
  5214. 3:17:14next one. And now you are going to we
  5215. 3:17:16are going to combine all of these to a
  5216. 3:17:18certain not certain we are going to
  5217. 3:17:20combine all these readable transform
  5218. 3:17:22writable into one particular concept and
  5219. 3:17:24that we call as pipeline. Now pipeline
  5220. 3:17:27is very very important. What pipeline is
  5221. 3:17:29going to do? Now you see that each and
  5222. 3:17:30everything is individual right? So it's
  5223. 3:17:32going to read something. It's going to
  5224. 3:17:33create a readable stream. It's going to
  5225. 3:17:35do some action here. It's going to uh
  5226. 3:17:38convert this one text to upper case and
  5227. 3:17:40it's going to write this data to some
  5228. 3:17:41place whatever it is. But all these are
  5229. 3:17:44independent. There is no connection. So
  5230. 3:17:46that means what we're going to do? We
  5231. 3:17:47need to use something that is called
  5232. 3:17:48pipeline. Pipeline is going to connect
  5233. 3:17:50all these stream together. Okay. So now
  5234. 3:17:53we are going to create a uh let's do a
  5235. 3:17:56function and I'm just going to do a main
  5236. 3:17:58and here I'm just going to have a
  5237. 3:18:00promise.
  5238. 3:18:02Let's do void. So how this will work? So
  5239. 3:18:05first of all we're going to take a try
  5240. 3:18:06catch. So in the catch we're going to do
  5241. 3:18:09error. So first let's handle the error.
  5242. 3:18:11So we'll do the same thing. So I'll do
  5243. 3:18:13message if this error is instance of
  5244. 3:18:15error. So I'm going to do error dot
  5245. 3:18:19message or else I'm just going to do
  5246. 3:18:21unknown error.
  5247. 3:18:24And here I'm just going to do
  5248. 3:18:27stream
  5249. 3:18:29failed and here I will do message.
  5250. 3:18:33Okay. Now in the try so we going to use
  5251. 3:18:36something called a and we going to use
  5252. 3:18:38something called pipeline and this
  5253. 3:18:39pipeline we are going to import from
  5254. 3:18:41node string promises okay so from here
  5255. 3:18:44we are going to import so from node
  5256. 3:18:45streams has promises you're going to
  5257. 3:18:47have so what this pipeline is going to
  5258. 3:18:49do now this is actually very important
  5259. 3:18:50concept okay and this can be if you know
  5260. 3:18:52all this right and let's see if you can
  5261. 3:18:54answer all of this in interview you'll
  5262. 3:18:55be like okay your interview will be
  5263. 3:18:57actually impressed okay if you know
  5264. 3:18:59because these are pure node concept we
  5265. 3:19:01are not using any framework or anything
  5266. 3:19:03like Right pipeline is going to connect
  5267. 3:19:05all your stream together. Okay. So we
  5268. 3:19:08have first we have a readable string
  5269. 3:19:10correct and then readable string we
  5270. 3:19:12going to do what? We are going to upper
  5271. 3:19:13case. So we going to convert that text
  5272. 3:19:15to upper case and then we going to use
  5273. 3:19:17the rightable. So now what you can do
  5274. 3:19:19right you can actually directly pass all
  5275. 3:19:21your source here. Source means you can
  5276. 3:19:22pass your readable. Then I'm just going
  5277. 3:19:25to do my uppercase. So this is my
  5278. 3:19:28transform. So we are transforming
  5279. 3:19:29something. We are doing some action and
  5280. 3:19:30then we going to do a writable. That's
  5281. 3:19:32it. And then what you can do, you can
  5282. 3:19:34just do a log and this stream sorry do
  5283. 3:19:38stream completed something like this.
  5284. 3:19:41That's it. And then what you're going to
  5285. 3:19:42do right, you're going to just uh not
  5286. 3:19:45here outside. We're going to call this
  5287. 3:19:47main.
  5288. 3:19:49So save this.
  5289. 3:19:51Now let's copy this. Go here.
  5290. 3:19:57So we'll do tsx source/ this
  5291. 3:20:03and let's do here. We'll see what you're
  5292. 3:20:05getting. So you're getting received
  5293. 3:20:06chunk hello from nodejs uh streams and
  5294. 3:20:11uh string completed. So you can see that
  5295. 3:20:14from where you are getting this one
  5296. 3:20:17receipt chunk means that means you are
  5297. 3:20:19writing. Now if you notice one thing
  5298. 3:20:22when you have created the readable we
  5299. 3:20:24have this one in lower case but this
  5300. 3:20:26transform stream is going to do what is
  5301. 3:20:28actually transforming this stream and
  5302. 3:20:30then it actually passing this one to a
  5303. 3:20:32writable stream and then it's going to
  5304. 3:20:34simply saying that okay now I've
  5305. 3:20:36received it for for example here we are
  5306. 3:20:38simply logging right but in actual
  5307. 3:20:39project maybe you're writing this one to
  5308. 3:20:41a file system or maybe you're sending
  5309. 3:20:43this data to some place we are storing
  5310. 3:20:45the data to our cloud whatever it is but
  5311. 3:20:47you can see that this is actually
  5312. 3:20:48received the data Right. So chunk we are
  5313. 3:20:51getting capital and who is doing this
  5314. 3:20:53capital? This uppercase transform this
  5315. 3:20:55stream and then we simply telling that
  5316. 3:20:57now this call back is telling okay
  5317. 3:20:59writeable stream task is done. You go
  5318. 3:21:01next. Now it will check here we don't
  5319. 3:21:03have any other stream. So now stream
  5320. 3:21:05will be completed. So this is how you
  5321. 3:21:07can make use of this stream. So couple
  5322. 3:21:10of key points you have to understand
  5323. 3:21:11that stream work with chunks. Streams
  5324. 3:21:13work with chunks. Readable stream what
  5325. 3:21:16it's going to do? It's going to produce
  5326. 3:21:17the data. Writable stream is going to
  5327. 3:21:19consume the data and it's going to do
  5328. 3:21:21something. And transform stream is
  5329. 3:21:22actually very important. Transform
  5330. 3:21:24actually doing something. It's actually
  5331. 3:21:26modifying the data in the middle. See,
  5332. 3:21:28it's actually converting this one to
  5333. 3:21:29uppercase before it will pass to the
  5334. 3:21:31right angle and pipeline is going to
  5335. 3:21:33combine all your stream together. Okay.
  5336. 3:21:36One thing you don't have to use stream
  5337. 3:21:38everywhere. This is also another
  5338. 3:21:39important concept and this is also a bad
  5339. 3:21:41practice if you're using streams
  5340. 3:21:42everywhere. So, so it's like oh I know
  5341. 3:21:45stream and I have to show off my skill
  5342. 3:21:46and then I'm using. Don't do like this.
  5343. 3:21:48If your data is very large then only you
  5344. 3:21:50have to use streams. Okay. So with this
  5345. 3:21:53now our core module is completed. So I
  5346. 3:21:57think we have almost covered most of the
  5347. 3:21:58things and some of the things are very
  5348. 3:22:00important. I think in other videos you
  5349. 3:22:01will not find in this much details. So
  5350. 3:22:04with this now let's move on to the next
  5351. 3:22:06section. All right. So till this part
  5352. 3:22:08what we have completed. So first section
  5353. 3:22:10is completed right. So you have learned
  5354. 3:22:12that what actually NodeJS is setup
  5355. 3:22:15installation and all the code modules
  5356. 3:22:17and the concepts and I think this is
  5357. 3:22:19more than enough. See whatever concept
  5358. 3:22:20that I mentioned uh that are a little
  5359. 3:22:23more advanced. So for that one I will be
  5360. 3:22:25creating a part three. Now you might be
  5361. 3:22:27thinking that okay I already have a part
  5362. 3:22:28three that I have uploaded last year but
  5363. 3:22:30this is just to mention that part three
  5364. 3:22:32is completely different because in that
  5365. 3:22:34part three we have discussed uh
  5366. 3:22:35different tech like uh I think if I'm
  5367. 3:22:38able to recolct I've discussed with uh
  5368. 3:22:40something like burn hono and then I've
  5369. 3:22:42covered NJS but this part three will be
  5370. 3:22:45completely different all right so here
  5371. 3:22:46we are only going to cover NodeJS
  5372. 3:22:48advanced concept why part three because
  5373. 3:22:50in the part two we are going to have
  5374. 3:22:52NodeJS microservices okay if you're
  5375. 3:22:54following this channel you already know
  5376. 3:22:55what I'm talking about so I think
  5377. 3:22:57whatever we have discussed in the
  5378. 3:22:58previous section that is more than
  5379. 3:23:00enough okay but now the real uh concepts
  5380. 3:23:02that we will start learning so first of
  5381. 3:23:05all what we are going to learn so first
  5382. 3:23:06we'll start with learning node HTTP
  5383. 3:23:09module because this is where it will
  5384. 3:23:11become more and more practical okay and
  5385. 3:23:13from this particular section whatever
  5386. 3:23:15things I'm going to repeat whatever
  5387. 3:23:17things you are going to learn you have
  5388. 3:23:19to do this one practically properly and
  5389. 3:23:21you also have to learn that how you're
  5390. 3:23:22going to apply all those concepts in a
  5391. 3:23:24project where you're going to actually
  5392. 3:23:26build production level API applications.
  5393. 3:23:28Okay, so first we are going to target
  5394. 3:23:30node HTTP module. This is what we are
  5395. 3:23:32going to do after [snorts] this. Okay,
  5396. 3:23:35extremely important. Okay, I'm going to
  5397. 3:23:36repeat once again. Please note it down.
  5398. 3:23:39There will be one section where I'm
  5399. 3:23:41going to add a postgress full course.
  5400. 3:23:42Now this particular part that we will
  5401. 3:23:46have before starting our actual NodeJS
  5402. 3:23:48project, this video is already uploaded
  5403. 3:23:50on YouTube. Okay, but why I'm adding?
  5404. 3:23:52because if I'm not wrong, I think most
  5405. 3:23:54of the people haven't watched because
  5406. 3:23:56that video didn't get that much
  5407. 3:23:57response. Now why I'm telling this is
  5408. 3:23:59absolutely mandatory. Uh this particular
  5409. 3:24:02section, the reason is very simple
  5410. 3:24:04because in this particular section, I
  5411. 3:24:06think this will be around 4 hours. Each
  5412. 3:24:08and every topic I discussed that if
  5413. 3:24:10you're a beginner, this will be
  5414. 3:24:12absolutely perfect video for posgress to
  5415. 3:24:14start. So I'll highly recommend like
  5416. 3:24:16100% recommend to complete this
  5417. 3:24:18particular section before you will jump
  5418. 3:24:20to next part. Okay. So I just wanted to
  5419. 3:24:23repeat once again once we'll complete
  5420. 3:24:25this node HTTP module we are going to
  5421. 3:24:28have have one complete dedicated
  5422. 3:24:29postgress section. Now if you recolct
  5423. 3:24:32right if you watch my previous NodeJS
  5424. 3:24:33part one video there also I have kept
  5425. 3:24:35one section for MongoDB because that
  5426. 3:24:37project uh or that video was completely
  5427. 3:24:39dedicated to MongoDB because this one we
  5428. 3:24:42are going to cover posgress and also in
  5429. 3:24:44NodeJS microservices also we are going
  5430. 3:24:45to have Postgress. Highly highly
  5431. 3:24:47recommended do not skip this particular
  5432. 3:24:49section. Okay. If you skip it's fine if
  5433. 3:24:51you already know but but if you don't
  5434. 3:24:53know like postgrad basic concept then
  5435. 3:24:55you have to watch this. So this will be
  5436. 3:24:57prerequisite. This particular section is
  5437. 3:24:59prerequisite for the next section.
  5438. 3:25:01[snorts] Once we cover this right we are
  5439. 3:25:03going to have our actual practical
  5440. 3:25:05project. Now you can me see that I
  5441. 3:25:06clearly mentioned this will be all in
  5442. 3:25:08one covers everything. So what I mean by
  5443. 3:25:10this? So let's discuss or else let's not
  5444. 3:25:12directly jump to the uh practical part
  5445. 3:25:14because you need to know right that what
  5446. 3:25:16are the things you have to learn. So
  5447. 3:25:18what I mean by this allin-one. So in
  5448. 3:25:20this particular project you are going to
  5449. 3:25:22understand how we are going to use
  5450. 3:25:24posgress with docker. Okay. So I'm going
  5451. 3:25:26to give you some see we are not going to
  5452. 3:25:28have too many things around docker. If
  5453. 3:25:30you're thinking okay I don't know
  5454. 3:25:31docker. So we are going to have one
  5455. 3:25:32single docker compost. If you want you
  5456. 3:25:35can also skip docker but I will highly
  5457. 3:25:37suggest to use it because it is very
  5458. 3:25:39very helpful and it is also production
  5459. 3:25:42oriented right. So you'll understand
  5460. 3:25:44that how we can create posgress pool or
  5461. 3:25:46posgress connection. how you are going
  5462. 3:25:48to implement a proper folder structure
  5463. 3:25:50when you're going to implement any
  5464. 3:25:52production level NodeJS application.
  5465. 3:25:54Third, how you can implement CRUD. CRUD
  5466. 3:25:56means create uh read, update and delete
  5467. 3:26:00which is the pillar of each and every
  5468. 3:26:02project. Whatever project you are going
  5469. 3:26:03to build, whatever complexity of that
  5470. 3:26:05project is at the end everything will be
  5471. 3:26:08CRUD. So CRUD we are going to cover. We
  5472. 3:26:10are going to cover authentication JWT
  5473. 3:26:12authentication. We are not using any
  5474. 3:26:13third party packages. Okay. Then we are
  5475. 3:26:16also going to have file upload. So this
  5476. 3:26:18project is going to cover everything.
  5477. 3:26:19Now you might be thinking okay the same
  5478. 3:26:22thing we have covered in the part one of
  5479. 3:26:24the NodeJS also that I uploaded two
  5480. 3:26:26years back actually yes because see when
  5481. 3:26:29I'm uploading a new video the only
  5482. 3:26:31difference is that this is on posgress
  5483. 3:26:33that one on MongoDB and to be honest I
  5484. 3:26:36will be very honest right now in this
  5485. 3:26:39particular uh time right learning
  5486. 3:26:41posgress will give you too much age
  5487. 3:26:43because most of the products and level
  5488. 3:26:44application will see that they're not
  5489. 3:26:46using MongoDB. So I just wanted to
  5490. 3:26:47mention so if you're thinking that okay
  5491. 3:26:49what is the new thing in this particular
  5492. 3:26:51video or on this particular project to
  5493. 3:26:53be honest that is not much at the end we
  5494. 3:26:55are going to cover all the backend
  5495. 3:26:56responsibilities only but how you can
  5496. 3:26:58create a proper folder structure the
  5497. 3:27:00coding standard working with posgress
  5498. 3:27:02all these things we are going to
  5499. 3:27:03understand after this we are going to
  5500. 3:27:06have one complete dedicated section on
  5501. 3:27:08radius this also another video that I
  5502. 3:27:10have uploaded same video I already
  5503. 3:27:12mentioned that that will be prerequisite
  5504. 3:27:14for node this video again I didn't get
  5505. 3:27:16proper response I'm going to add this
  5506. 3:27:17section. If you want to understand how
  5507. 3:27:20to work with background jobs bmpq then
  5508. 3:27:22this is also prerequisite section. So
  5509. 3:27:25why I'm mentioning all of this so that
  5510. 3:27:27you will be in track. So first we'll
  5511. 3:27:28start with node http then complete your
  5512. 3:27:31posgress section. Once complete please
  5513. 3:27:34do not jump directly from here to here.
  5514. 3:27:36Please do not do this. Okay. I highly
  5515. 3:27:38recommend if you're serious do this.
  5516. 3:27:40First learn posgress then jump to this
  5517. 3:27:43one. Once you'll do that learn radish
  5518. 3:27:46have some patience and then go to the
  5519. 3:27:48practical section. So this will be
  5520. 3:27:49proper sequence. Okay. So with this now
  5521. 3:27:52let's get started. The first thing what
  5522. 3:27:53I have done I created a single folder.
  5523. 3:27:56You can see that we don't have anything
  5524. 3:27:57else here. I think we have already
  5525. 3:27:59covered all these things right. How you
  5526. 3:28:00can create package.js and tsconfig in
  5527. 3:28:03the previous section. So that is the
  5528. 3:28:04reason I haven't uh I'm not showing you
  5529. 3:28:06all of this. So what we are going to do
  5530. 3:28:08in this one we are going to understand
  5531. 3:28:09couple of things. So first we have to
  5532. 3:28:11understand the basic HTTP server and the
  5533. 3:28:13concept how you can implement routing uh
  5534. 3:28:17body reading body and also JSON
  5535. 3:28:19response. These four will be main
  5536. 3:28:21pillar. Why we are covering all of this?
  5537. 3:28:23Because once you start XL before that
  5538. 3:28:26you need to know the core HTTP module or
  5539. 3:28:28else there is no point and also we are
  5540. 3:28:30going to call one very important section
  5541. 3:28:31where we are going to call one external
  5542. 3:28:33API because there is a possibility that
  5543. 3:28:36some section will be uh that not some
  5544. 3:28:38section in some projects let's say
  5545. 3:28:40you're working in a company there is a
  5546. 3:28:42possibility that you will have some
  5547. 3:28:43third party API that you need to call so
  5548. 3:28:46you also need to understand some of the
  5549. 3:28:47concepts there also so with this now
  5550. 3:28:49let's get started so first we'll start
  5551. 3:28:51with understanding the basic HTTP be
  5552. 3:28:53server concept from the next part. Okay.
  5553. 3:28:55So let's start creating the first file.
  5554. 3:28:57Okay. So for this one what we are going
  5555. 3:28:59to do right let's see we don't have to
  5556. 3:29:00create any proper folder structure or
  5557. 3:29:02anything for now because we need to
  5558. 3:29:04understand the concept right. So as
  5559. 3:29:06usual we will create single file like
  5560. 3:29:07this. So I'm just going to give this one
  5561. 3:29:09as basic http server.ts.
  5562. 3:29:13So first what are the things that we
  5563. 3:29:16need to understand. First we need to
  5564. 3:29:17understand that from which package we
  5565. 3:29:19are going to import http right. So first
  5566. 3:29:21let's do this and we I think you know
  5567. 3:29:23very well right. So I'm going to keep
  5568. 3:29:25complete section practical only. There
  5569. 3:29:28is no point of explaining only concept
  5570. 3:29:30then do practical but we are going to do
  5571. 3:29:32vice versa. First we're going to write
  5572. 3:29:33the practical and then write the concept
  5573. 3:29:34in this file only just like we have done
  5574. 3:29:36in the previous section. So input http
  5575. 3:29:39from this module. So from which module I
  5576. 3:29:41need to do. So I'm going to do node and
  5577. 3:29:43then you're going to give colon and this
  5578. 3:29:45will be your http right? You can also do
  5579. 3:29:47something like this. So do http. Okay,
  5580. 3:29:50but I'll do like this. So we are getting
  5581. 3:29:52from node HTTP module. So this will be
  5582. 3:29:55my module. First what I'm going to do
  5583. 3:29:57right, I'm going to take one port that I
  5584. 3:29:59wanted to run this on 30,000 port. So
  5585. 3:30:02first we need to understand that how you
  5586. 3:30:04can create a HTTP server. So to create a
  5587. 3:30:06HTTP server, you need to use something
  5588. 3:30:08called HTTP.create server. So this
  5589. 3:30:10create server is a method that is going
  5590. 3:30:12to do what? It's going to create a
  5591. 3:30:14low-level HTTP server. Okay. So first
  5592. 3:30:17let's create the server. Then we'll
  5593. 3:30:18understand each and everything. So let's
  5594. 3:30:20start with server. So I'm going to take
  5595. 3:30:22HTTP and this is going to give me this
  5596. 3:30:25create server. Okay. Now this create
  5597. 3:30:27server is going to have something. Okay.
  5598. 3:30:29So this is going to have something
  5599. 3:30:30called request and this is going to have
  5600. 3:30:33response. So what this is all about very
  5601. 3:30:35important concept like these are like
  5602. 3:30:37core concept. Okay. You can't skip any
  5603. 3:30:40of these. Request is your request
  5604. 3:30:41object. Response is your response
  5605. 3:30:43object. So what actually this request
  5606. 3:30:46object contains right? So first you are
  5607. 3:30:48going to have something called as
  5608. 3:30:49request and then you're going to have
  5609. 3:30:51something called as response. Now you're
  5610. 3:30:53going to get some error because this is
  5611. 3:30:55going to not able to understand that
  5612. 3:30:56what actually this is. So in this case
  5613. 3:30:58we need to import something from this
  5614. 3:31:00particular model. So I'm going to import
  5615. 3:31:02a type and I'm going to tell that okay
  5616. 3:31:04this is my incoming message that the
  5617. 3:31:07request I'm going to receive and this is
  5618. 3:31:09my outgoing or this is my server
  5619. 3:31:11response not outgoing but this is my
  5620. 3:31:13server response. So I'm going to import
  5621. 3:31:14type and here we're going to have
  5622. 3:31:16something called a server response. So
  5623. 3:31:18first I'm going to tell that okay this
  5624. 3:31:20is my incoming request
  5625. 3:31:24and this is my server response.
  5626. 3:31:28Okay. So now after this what we going to
  5627. 3:31:30do right also we have to take this one
  5628. 3:31:33as a call back and here now we are going
  5629. 3:31:37to do something. So first we need to
  5630. 3:31:39understand here that what is happening.
  5631. 3:31:40So first we understand that so let's
  5632. 3:31:43write it like this. I'll do http dot
  5633. 3:31:45create server. This is going to create
  5634. 3:31:48low-level
  5635. 3:31:50http server. And second we have a call
  5636. 3:31:55back right. So this is our call back.
  5637. 3:31:58Now this call back is going to run for
  5638. 3:32:00every incoming HTTP request. So we have
  5639. 3:32:03a call back. So call
  5640. 3:32:05is going to run for every incoming
  5641. 3:32:09request that you're going to get
  5642. 3:32:13incoming HTTP request. What this request
  5643. 3:32:16means? So this request is nothing but
  5644. 3:32:19this is our request
  5645. 3:32:22object.
  5646. 3:32:24What this request object contains. Now
  5647. 3:32:26this is going to contain couple of
  5648. 3:32:28things, right? So first if I just do
  5649. 3:32:29here something like this. So I'll do
  5650. 3:32:31request dot and you can see that you're
  5651. 3:32:33going to have lot of things here. So
  5652. 3:32:34many things are happening. So some of
  5653. 3:32:36the very important point you have to
  5654. 3:32:37understand. First there will be a
  5655. 3:32:39request method. Okay. Now this request
  5656. 3:32:41method is going to tell the type of
  5657. 3:32:43method that or type of request that we
  5658. 3:32:45are doing. So for example this method
  5659. 3:32:48can be your get this can be your post.
  5660. 3:32:50This can be your put this can be your
  5661. 3:32:52options. This can be your delete
  5662. 3:32:55something like this. Okay. So this is
  5663. 3:32:57your request dot method. Then you're
  5664. 3:32:59going to have something called request
  5665. 3:33:00URL. What this is going to tell this is
  5666. 3:33:02act this is actually your requested
  5667. 3:33:04path. Okay, for example, we are calling
  5668. 3:33:07to this path slash or we are calling to
  5669. 3:33:09this / user. So this is your path or so
  5670. 3:33:12in this case this will be your request
  5671. 3:33:14dot URL. Okay, then we have something
  5672. 3:33:16called very important we have request
  5673. 3:33:18headers. So this is actually these
  5674. 3:33:21headers are nothing but so these headers
  5675. 3:33:24will be the actual metadata.
  5676. 3:33:27actual metadata
  5677. 3:33:30sent by the client. So what I mean by
  5678. 3:33:33client client is so in this case your
  5679. 3:33:35client can be let's say you have a
  5680. 3:33:36reactjs application so you're sending
  5681. 3:33:37the headers okay and next you have
  5682. 3:33:40something called request body right the
  5683. 3:33:42request body is nothing but so this will
  5684. 3:33:44be your data that will be sent by the
  5685. 3:33:46client right whenever we are going to do
  5686. 3:33:48let's say post or put type of request so
  5687. 3:33:51this will be your request object then
  5688. 3:33:53we're going to have our response so
  5689. 3:33:55which is our response
  5690. 3:33:57object right so this is nothing but so
  5691. 3:34:00this particular object will be used by
  5692. 3:34:02our server to send something back to the
  5693. 3:34:06client. Okay. So let's say you're doing
  5694. 3:34:08some API call and once you do the API
  5695. 3:34:11call or whatever DB operations you are
  5696. 3:34:13doing you are doing so you're going to
  5697. 3:34:15get some response back and that response
  5698. 3:34:18you need to render in your UI so that
  5699. 3:34:20your user can see okay so in that case
  5700. 3:34:23that particular response you have to
  5701. 3:34:24send back okay so because you are going
  5702. 3:34:27to have your back end and front end
  5703. 3:34:28right so until unless you're going to
  5704. 3:34:29send the data back from your back end to
  5705. 3:34:32front end there is no way you can render
  5706. 3:34:34the data correct so this response is
  5707. 3:34:37going to help you in that particular
  5708. 3:34:38case. So it's going to send something
  5709. 3:34:40back to your client. So what are the
  5710. 3:34:42things going to send? So you're going to
  5711. 3:34:43have something called status code. So
  5712. 3:34:45this status code is going to tell that
  5713. 3:34:47okay if the response or if the operation
  5714. 3:34:50that we are doing if that is successful
  5715. 3:34:52or not then you're going to also pass
  5716. 3:34:53the response headers and then you're
  5717. 3:34:56going to have your response body.
  5718. 3:34:59Okay. So this is all about the main
  5719. 3:35:02concept. Okay. So first you're going to
  5720. 3:35:04have what? So HTTP create server is
  5721. 3:35:06going to create your actual HTTP server.
  5722. 3:35:09There will be a call back which is going
  5723. 3:35:10to run every time there will be HTTP
  5724. 3:35:12request. This is your request object and
  5725. 3:35:14this is your response object. Okay. So
  5726. 3:35:16now first of all what we are going to do
  5727. 3:35:18right. So in this
  5728. 3:35:21so let's remove this. So first I'm just
  5729. 3:35:23going to do con
  5730. 3:35:26method. So this will be my request do
  5731. 3:35:28method. So this will be the first thing.
  5732. 3:35:32Again I think here we have let's write
  5733. 3:35:34it only once so that you remember. So
  5734. 3:35:36here we are going to have request dot
  5735. 3:35:37method can be get. Okay. So in this case
  5736. 3:35:40get means you are reading data.
  5737. 3:35:43Then you're going to have post. So post
  5738. 3:35:45means you're going to creating some new
  5739. 3:35:47data. So you're going to create data or
  5740. 3:35:50creating or let's do create data. Then
  5741. 3:35:53you're going to have put. So put will be
  5742. 3:35:56you are actually replacing the data.
  5743. 3:35:58Replace data. Then you're going to have
  5744. 3:36:01something called patch. So patch is you
  5745. 3:36:03are updating but you're updating partial
  5746. 3:36:06data or let's update partial data and
  5747. 3:36:11then you're going to have delete. So
  5748. 3:36:13delete is nothing but I think this is
  5749. 3:36:14very easy to understand that delete is
  5750. 3:36:16nothing but you are deleting the data.
  5751. 3:36:18So delete data. So normally these are
  5752. 3:36:21very very important. This is what you're
  5753. 3:36:23going to use most of the times there are
  5754. 3:36:24lot so many but if you're learning
  5755. 3:36:27NodeJS in depth okay in-depth see again
  5756. 3:36:30in depth in the sense I'm not talking
  5757. 3:36:31about you have to be like a node master
  5758. 3:36:34okay but if you're working in a company
  5759. 3:36:36right let's say you're targeting for job
  5760. 3:36:39this is more than enough why I'm telling
  5761. 3:36:41because this is when you're going to
  5762. 3:36:43implement some actual products and level
  5763. 3:36:46application even in your company right
  5764. 3:36:47you'll see that most of the times you
  5765. 3:36:49are going to use all of these so this
  5766. 3:36:52This is about your request dot method.
  5767. 3:36:55So then we're going to have our URL. So
  5768. 3:36:56this will be my request dot URL.
  5769. 3:37:01Okay. So I already mentioned this one.
  5770. 3:37:03So this will be request URL is uh
  5771. 3:37:05nothing but that in which path the
  5772. 3:37:08client is actually requesting. So this
  5773. 3:37:10will be in which path the client
  5774. 3:37:15is actually requesting.
  5775. 3:37:20Okay. So this will be your request dot
  5776. 3:37:21URL and then you're going to have what
  5777. 3:37:24we are going to have something called
  5778. 3:37:25headers. So for example here let's do
  5779. 3:37:29one thing. So I'm just going to do one
  5780. 3:37:30user agent header. So how you can do
  5781. 3:37:32this? You're going to do request and
  5782. 3:37:34you're going to pass a header and then
  5783. 3:37:36here you can pass something called user
  5784. 3:37:38agent. Okay what actually headers are
  5785. 3:37:40see headers are nothing but your
  5786. 3:37:43metadata. Now what how I will explain?
  5787. 3:37:45So headers are nothing but extra
  5788. 3:37:47information that we send along with our
  5789. 3:37:50request. Okay. So this will be our extra
  5790. 3:37:52information. For example, let's say what
  5791. 3:37:53is our browser type. What is our token
  5792. 3:37:56authorization token? What is our content
  5793. 3:37:58type? Whether we are sending JSON
  5794. 3:38:00content or we are sending text normal
  5795. 3:38:03text. All these things you're going to
  5796. 3:38:04send in your uh request headers. Okay.
  5797. 3:38:07And now once this is done then we have
  5798. 3:38:10something called our response. So let's
  5799. 3:38:11say I'm just going to do response. And
  5800. 3:38:13you can see that we're going to have
  5801. 3:38:14response dot status code. If you do
  5802. 3:38:16response dot right, you're going to get
  5803. 3:38:18lot of options here, right? You can see
  5804. 3:38:20that. So do something like request dot
  5805. 3:38:23send date, set header, all of these. So
  5806. 3:38:25for now, let's do request, sorry,
  5807. 3:38:27response status code. And I'm just going
  5808. 3:38:29to do this one as 200. What this is
  5809. 3:38:32going to do? This is going to set your
  5810. 3:38:34HTTP status code. Okay, here 200 means
  5811. 3:38:39status sorry uh request is successful or
  5812. 3:38:42status is successful. Now here you can
  5813. 3:38:45have so many for example 201 when you're
  5814. 3:38:47going to create something you're going
  5815. 3:38:49to have 400 you'll have 500 you have 429
  5816. 3:38:52like there are so many okay you're going
  5817. 3:38:54to have 401 so all these now each and
  5818. 3:38:57every status code we use in a certain
  5819. 3:39:00position or certain situation to
  5820. 3:39:02represent something so then you are
  5821. 3:39:05going to have one more important which
  5822. 3:39:06is nothing but header so let's say I'm
  5823. 3:39:09adding some header here so in this case
  5824. 3:39:11I'm going to add a content type so what
  5825. 3:39:14actually the content that I'm sending.
  5826. 3:39:16So in this case I have to mention that
  5827. 3:39:18okay whether I'm sending a JSON format
  5828. 3:39:21we are sending in a text format all
  5829. 3:39:23these. So for example let's say I wanted
  5830. 3:39:24to send only text. So here this set
  5831. 3:39:28header what this is going to tell this
  5832. 3:39:29is going to tell your client that what
  5833. 3:39:31kind of response we are sending here it
  5834. 3:39:34is normal text. Now one thing you have
  5835. 3:39:36to understand okay that if you don't add
  5836. 3:39:38this particular header the browser okay
  5837. 3:39:42still it can show the response but here
  5838. 3:39:45setting it always help because for
  5839. 3:39:46example let's say we are sending a JSON
  5840. 3:39:48so I have to mention that content type
  5841. 3:39:49will be application/json
  5842. 3:39:51something like this now what I need to
  5843. 3:39:53do right I have to whatever this server
  5844. 3:39:55that I created I have to end this
  5845. 3:39:58particular request so how I'm going to
  5846. 3:40:00do that or not request I'm going to tell
  5847. 3:40:03we have to finish this particular
  5848. 3:40:05response response and to finish this
  5849. 3:40:06what we have to do right we have
  5850. 3:40:07something called response dot end what
  5851. 3:40:10this response do end is going to do this
  5852. 3:40:12is going to finish this particular
  5853. 3:40:14response now this is actually very very
  5854. 3:40:16important because why because every uh
  5855. 3:40:18request we are going to have at the end
  5856. 3:40:20you are going to have some kind of
  5857. 3:40:21response correct that is the reason
  5858. 3:40:23you're creating the request what will
  5859. 3:40:25happen if you don't add this on this
  5860. 3:40:26particular thing if you don't add right
  5861. 3:40:28the browser or the client it's going to
  5862. 3:40:30wait because you it will think that okay
  5863. 3:40:32you are sending something but if you're
  5864. 3:40:34not sending it's going to do it. So now
  5865. 3:40:36for example, let's say maybe let's let's
  5866. 3:40:38do like this. I'm just going to do basic
  5867. 3:40:41HTTP node server.
  5868. 3:40:44Okay. And maybe let's render the method.
  5869. 3:40:47So I'm just going to render the method
  5870. 3:40:49here. Let's render the URL also. Let's
  5871. 3:40:52render the maybe user agent.
  5872. 3:40:57I think these three is fine. Okay. So
  5873. 3:40:58save this. So now is this completed? No.
  5874. 3:41:02Why? Because when we will start a
  5875. 3:41:06particular request or we are going to
  5876. 3:41:08get any response back. So in that case
  5877. 3:41:11uh what we have to do we have to listen
  5878. 3:41:13that server should listen to a certain
  5879. 3:41:15port. Okay. So server.list we have a
  5880. 3:41:18method that is going to do it's going to
  5881. 3:41:20start the actual server. Okay. So first
  5882. 3:41:23what we have to do right? So we have to
  5883. 3:41:25do server and this is going to give you
  5884. 3:41:27this listen. We are going to pass the
  5885. 3:41:28port. Okay. You listen to this 30,000
  5886. 3:41:30port. Now what will happen right? before
  5887. 3:41:33listen this server exist but this server
  5888. 3:41:36exist in your memory okay but it's not
  5889. 3:41:38going to accept any request why because
  5890. 3:41:40this is not running once you add this
  5891. 3:41:43listen right what is going to do right
  5892. 3:41:45so node is going to process this and
  5893. 3:41:47it's going to wait for that HTTP request
  5894. 3:41:49so whenever you are going to do this
  5895. 3:41:51request it's going to run this
  5896. 3:41:52particular method and it's going to do
  5897. 3:41:54its job whatever it's going to do so now
  5898. 3:41:56in this case this is going to get a call
  5899. 3:41:59back and here what you can do right uh
  5900. 3:42:02So I'm just going to do here log and
  5901. 3:42:04maybe let's do server
  5902. 3:42:07is now running on port and let's give
  5903. 3:42:12this one port here right so I'm just
  5904. 3:42:13going to pass a port
  5905. 3:42:18let's say maybe let's change this one
  5906. 3:42:20for because I already have so many
  5907. 3:42:22projects that I have created right maybe
  5908. 3:42:23something some other things are running
  5909. 3:42:25so let's keep 5,000 only for now so
  5910. 3:42:27let's save this so now what we going to
  5911. 3:42:30do right we will just add one script in
  5912. 3:42:33our package dojson and let's go here.
  5913. 3:42:36Let's remove this.
  5914. 3:42:39So I'm just going to do 01.
  5915. 3:42:43So this will be tsx. So you go to
  5916. 3:42:45source/
  5917. 3:42:47and you have this, right? So I'm just
  5918. 3:42:48going to rename copy this and then you
  5919. 3:42:52run it. Paste it here. That's it. So
  5920. 3:42:54save this. So now what you can do? So
  5921. 3:42:56just to check this one. So I'm just
  5922. 3:42:57going to do npm run and then you can do
  5923. 3:43:0001 because why? Because we've given this
  5924. 3:43:02one as 01 right single script this one.
  5925. 3:43:05So now let's run this. You can see that
  5926. 3:43:08server is now running on port 5,000. So
  5927. 3:43:11that means this is actually working
  5928. 3:43:13fine. So now what we can do right so
  5929. 3:43:15let's go here and maybe let's do local
  5930. 3:43:18host and I'm just going to do 5,000
  5931. 3:43:20because we're running on 5,000. What
  5932. 3:43:22you're getting? We're getting basic HTTP
  5933. 3:43:23node server. This is get request right?
  5934. 3:43:26you're getting your user agent right and
  5935. 3:43:28all the details that whatever you have
  5936. 3:43:29added here. Okay. So now I think this is
  5937. 3:43:32completed. So first of all you
  5938. 3:43:34understand some of the things right that
  5939. 3:43:36what actually create server method is
  5940. 3:43:38what is request object response object.
  5941. 3:43:41Okay. And then how you are going to set
  5942. 3:43:43headers and all the things. Now remember
  5943. 3:43:45this status code and everything when
  5944. 3:43:47we'll start our actual project
  5945. 3:43:48implementation this will be easier
  5946. 3:43:50because whatever you're seeing all these
  5947. 3:43:51status code we are going to anyways use
  5948. 3:43:53in our NodeJS project.
  5949. 3:43:55So with this now let's move on to the
  5950. 3:43:57next concept. Okay. So now let's move on
  5951. 3:43:59to the next one. So next one let's
  5952. 3:44:01understand some of the routing concepts.
  5953. 3:44:03Okay. So I'll do routing. DS. So first
  5954. 3:44:06of all let's do one thing. Okay. Let's
  5955. 3:44:08close this one. Okay. Clear this.
  5956. 3:44:13So first uh let's do one thing. Let's
  5957. 3:44:15import this because we need this. We
  5958. 3:44:18have already discussed this one. We
  5959. 3:44:19don't have to again uh discuss. So let's
  5960. 3:44:21take the port as 5,01. this time and now
  5961. 3:44:26what we will do right so let's create
  5962. 3:44:27the server first now if you see that how
  5963. 3:44:30I've actually created see remember
  5964. 3:44:32whatever video that you are seeing right
  5965. 3:44:33this particular course
  5966. 3:44:36you don't have any idea how much time I
  5967. 3:44:38actually spent while creating this video
  5968. 3:44:40the point is not about the video
  5969. 3:44:42recording or the editing the point is
  5970. 3:44:44how I'm going to structure uh this
  5971. 3:44:46particular curriculum that is the most
  5972. 3:44:48important part structuring is very very
  5973. 3:44:50important means this particular concept
  5974. 3:44:52should come after that concept And that
  5975. 3:44:54is where the main teaching will come.
  5976. 3:44:56You understand what I'm talking about?
  5977. 3:44:58For example, let's say in the previous
  5978. 3:44:59section we have already discussed that
  5979. 3:45:01how you can create uh server using HTTP
  5980. 3:45:04create server. Now when in the next part
  5981. 3:45:06I'm going to use the same concept. I
  5982. 3:45:08don't have to explain because from the
  5983. 3:45:10previous section you will be able to
  5984. 3:45:12apply that particular concept in this
  5985. 3:45:14one. Okay. And you'll notice this
  5986. 3:45:16particular pattern throughout this uh
  5987. 3:45:18video. Okay. Till the end. So let's do
  5988. 3:45:20one. So we'll do http.create server. So
  5989. 3:45:24this is going to give us a call back. So
  5990. 3:45:25I'm just going to do request which will
  5991. 3:45:27be my incoming message. Then I'm going
  5992. 3:45:29to have the response which will be my
  5993. 3:45:30server response. Okay. And [snorts] here
  5994. 3:45:32first of all what we are going to do
  5995. 3:45:34right. So let's take our method. So I'm
  5996. 3:45:36just going to take the method and this
  5997. 3:45:38method will be so I'm going to take
  5998. 3:45:39either it will be the request method or
  5999. 3:45:42by default I'm going to take this one as
  6000. 3:45:43get. So this will be by default it will
  6001. 3:45:46be get method. Okay. So now what we are
  6002. 3:45:50going to do right? So first you have to
  6003. 3:45:52remember one thing that what this uh
  6004. 3:45:54okay let's first do one thing that I'll
  6005. 3:45:57do request URL and we already discussed
  6006. 3:46:00that we have something called request
  6007. 3:46:01URL right but what this request do URL
  6008. 3:46:04actually contains now this contains the
  6009. 3:46:06path that requested by the client okay
  6010. 3:46:10so let's say if I'm doing like this so I
  6011. 3:46:13have http and then I will do local host
  6012. 3:46:16and then I'll do 3,00 okay/ users
  6013. 3:46:20something like this. So I'm taking 3,000
  6014. 3:46:22only. That's fine. But let's take 5,0001
  6015. 3:46:24because we're going to run this. In this
  6016. 3:46:26case, what will be your request URL?
  6017. 3:46:29Your request URL will be / users. Okay,
  6018. 3:46:33only this part. Similarly, let's say if
  6019. 3:46:35I just do here / user and then here I'm
  6020. 3:46:38just going to take only for the user ID
  6021. 3:46:40one. So in that case, this will be only
  6022. 3:46:43of this user ID equal to one. Okay. So
  6023. 3:46:46request URL only this part. Okay. So now
  6024. 3:46:49when we are going to implement routing
  6025. 3:46:51or we understand routing we usually want
  6026. 3:46:54only the path part right only the path
  6027. 3:46:57this part okay so now how we are going
  6028. 3:46:59to get this so in that case what we have
  6029. 3:47:01to do right we are going to convert this
  6030. 3:47:03request dot URL into a URL object and
  6031. 3:47:06then we are going to read the path name
  6032. 3:47:08so first we have to convert so how we
  6033. 3:47:10are going to convert so what we have to
  6034. 3:47:13do right let's cut it so we have
  6035. 3:47:14something called new URL okay and this
  6036. 3:47:17is is going to give So you can pass this
  6037. 3:47:19ukulele or this will be
  6038. 3:47:23by default it will be like this and then
  6039. 3:47:26what we going to pass uh pass right. So
  6040. 3:47:28let's take this one as http and here I'm
  6041. 3:47:31simply going to do uh request dot header
  6042. 3:47:34and then I'm going to get something
  6043. 3:47:36called host. Okay and now what we can do
  6044. 3:47:39right we can simply read the path name.
  6045. 3:47:41So I'm just going to do path name and
  6046. 3:47:43this will be a request url dot you have
  6047. 3:47:45something called path name. So this is
  6048. 3:47:47going to give you the actual path name.
  6049. 3:47:49Okay. So now first of all let's do one
  6050. 3:47:51thing. Let's take rest and then I'm just
  6051. 3:47:54going to do a set header. And for now
  6052. 3:47:55I'm just going to pass the content type
  6053. 3:47:57as text only. So the content type will
  6054. 3:48:00be text/plain.
  6055. 3:48:04And then what we have to do right so now
  6056. 3:48:07what I can do right we can simply check
  6057. 3:48:09that if the current method is get. So
  6058. 3:48:12I'm simply going to check if the method
  6059. 3:48:14method is equal to equal to get and if
  6060. 3:48:17the path name if this path name is equal
  6061. 3:48:20to let's say I'm sending request from
  6062. 3:48:22this path from the health route. So this
  6063. 3:48:24is our route. Okay. So you'll understand
  6064. 3:48:26what I'm talking about. So in that case
  6065. 3:48:28what I'm going to do right I'm just
  6066. 3:48:29going to do raise dot status code will
  6067. 3:48:31become 200. I'm just going to do race
  6068. 3:48:34end. So I have to end it right. We have
  6069. 3:48:36already discussed. So I'm just going to
  6070. 3:48:37do server is now or server is healthy.
  6071. 3:48:41And then I'm simply going to do return.
  6072. 3:48:44Okay. So if the path name is health,
  6073. 3:48:46you're going to return this. Now you'll
  6074. 3:48:47understand one thing that even though if
  6075. 3:48:49you don't have any third party packages,
  6076. 3:48:51sorry not packages, framework like X or
  6077. 3:48:53let's say you're using something else
  6078. 3:48:55still using only node HTTP you will be
  6079. 3:48:57able to implement all of these okay
  6080. 3:49:00without using any framework also. So
  6081. 3:49:01that is the reason you have to
  6082. 3:49:02understand the basic concept. Similar
  6083. 3:49:04way what I can do right copy this and
  6084. 3:49:06then I can paste it here. Okay. the
  6085. 3:49:08method is get and now if the route is
  6086. 3:49:10instead of health if this is users so
  6087. 3:49:12I'm just simply going to pass here list
  6088. 3:49:14of users so I'm just going to do list of
  6089. 3:49:17users and then we are going to return
  6090. 3:49:19similarly you can do for other also for
  6091. 3:49:21example let's say if I copy this and now
  6092. 3:49:23I'm simply going to change this one to
  6093. 3:49:25if this is post not get request okay and
  6094. 3:49:28the path name is users so I'm just going
  6095. 3:49:30to change this one to 201
  6096. 3:49:33we usually we already expl sorry we
  6097. 3:49:35already explained this one right if you
  6098. 3:49:37recollect 20 is 201 is when we are going
  6099. 3:49:41to create something. So that is the
  6100. 3:49:43reason I'm taking this one as method is
  6101. 3:49:45post and in that case I'm simply going
  6102. 3:49:47to do user created successfully
  6103. 3:49:51something like this. Now here um you can
  6104. 3:49:53also take one for delete also. Okay. And
  6105. 3:49:56then what I have to do right now see
  6106. 3:49:58there is a possible that when user is
  6107. 3:50:00going to send one particular request
  6108. 3:50:03let's say user can send any random
  6109. 3:50:06request also to any path for example
  6110. 3:50:08some blah blah. Now this path is not
  6111. 3:50:11present. There is a possibility right
  6112. 3:50:12that this route is not present in your
  6113. 3:50:14back end. So in that case you have to
  6114. 3:50:15inform the user okay this route is not
  6115. 3:50:17found. So to do this what you have to do
  6116. 3:50:19is we're going to pass status code will
  6117. 3:50:21be 404. So 404 means this route is not
  6118. 3:50:24present. 404 means not found. So
  6119. 3:50:27something is not found not found here.
  6120. 3:50:31Right? And then in that case you're
  6121. 3:50:32simply going to do race.
  6122. 3:50:35And I'm simply going to do route not
  6123. 3:50:38found. Okay that's it. So now what we
  6124. 3:50:41going to do? Let's go here and then
  6125. 3:50:43we're going to copy this and I'm simply
  6126. 3:50:45going to paste it here. save this. So
  6127. 3:50:48let's go to package dojson, copy,
  6128. 3:50:52paste and then we are going to simply
  6129. 3:50:56copy this
  6130. 3:50:59paste here and change this one to two.
  6131. 3:51:02So save this and now what we going to do
  6132. 3:51:04right? So let's run it. So npm run 02.
  6133. 3:51:07Now you'll see that this is now running
  6134. 3:51:09on 5001. So now what I can do right
  6135. 3:51:11let's go here and then I'm going to
  6136. 3:51:13create new request and then here I'm
  6137. 3:51:16simply going to do what right so let's
  6138. 3:51:18copy this from here
  6139. 3:51:21and then I'm simply going to do 501 and
  6140. 3:51:23then I'm going to pass this one as
  6141. 3:51:25health okay see what will happen so you
  6142. 3:51:26can see that you are getting server is
  6143. 3:51:28now healthy now the reason you are
  6144. 3:51:30getting because it's now matching here
  6145. 3:51:32why because what we have done right this
  6146. 3:51:34request URL is now going to give you /
  6147. 3:51:36health because this is the path name
  6148. 3:51:39right this is is the path name that
  6149. 3:51:40you're going to get and if the path name
  6150. 3:51:42is matching so in that case you're going
  6151. 3:51:43to get server is healthy if you're
  6152. 3:51:45passing this one as users so you're
  6153. 3:51:48going to get that okay list of users
  6154. 3:51:50okay if you're passing what what else so
  6155. 3:51:52if I change this one to post and then if
  6156. 3:51:54I send this one user created
  6157. 3:51:56successfully but in case if I just
  6158. 3:51:58change this one to get and if I give a
  6159. 3:52:00random route which is not present so
  6160. 3:52:02I'll do random route not present
  6161. 3:52:07present
  6162. 3:52:09here and then if you send this right
  6163. 3:52:11you're going to get route not found and
  6164. 3:52:12you're going to get a 404 you're getting
  6165. 3:52:15all of this. So this is where it is very
  6166. 3:52:17important you have to understand couple
  6167. 3:52:18of things here first of all what
  6168. 3:52:19actually method is what is path name
  6169. 3:52:22right how you're going to get the path
  6170. 3:52:24name what is status code what actually
  6171. 3:52:27response you are sending and also that
  6172. 3:52:29you also understand one very important
  6173. 3:52:31point and that is if we don't have any
  6174. 3:52:33route that is not present so in that
  6175. 3:52:34case we pass this one as 404 okay why
  6176. 3:52:37this status code are extremely important
  6177. 3:52:39because when you're going to implement
  6178. 3:52:40actual code right let's a production
  6179. 3:52:42level application you're going to build
  6180. 3:52:44you need to know which status code you
  6181. 3:52:46need to use in which situation and that
  6182. 3:52:49is very important. Okay. So I think with
  6183. 3:52:51this this is now completed uh routing
  6184. 3:52:53section or concept. Now let's move on to
  6185. 3:52:55the next one. Okay. So moving to the
  6186. 3:52:56next one. So let's understand that how
  6187. 3:52:59we can read our request body in plain
  6188. 3:53:01NodeJS because anyways in the next
  6189. 3:53:03section we are going to introduce
  6190. 3:53:05express. So you're going to understand
  6191. 3:53:06all these concept in better way but
  6192. 3:53:08still you have to understand that how
  6193. 3:53:09you can uh do the same thing or do the
  6194. 3:53:13same implementation without using any
  6195. 3:53:16third party like uh frameworks. Okay. So
  6196. 3:53:18first I'm just going to give 03. Let's
  6197. 3:53:20give this one as maybe reading uh body
  6198. 3:53:24ts. So first let's close these two.
  6199. 3:53:28And here also let's do one thing. Let's
  6200. 3:53:31rename this. Copy this. And then let's
  6201. 3:53:34add one more here. Okay. So I'll do 03
  6202. 3:53:39and this will be tsx
  6203. 3:53:42source/ this. Save this. Okay. So first
  6204. 3:53:46of all we need to copy this tool
  6205. 3:53:49from here. I'll copy this.
  6206. 3:53:52Let's keep this on 5002 so that you'll
  6207. 3:53:55understand what is happening also. Let's
  6208. 3:53:56close this. And here first we are going
  6209. 3:53:59to do so we'll create our server.
  6210. 3:54:03So this will be http do.create server
  6211. 3:54:07and here we're going to have the call
  6212. 3:54:08back. So request will be your incoming
  6213. 3:54:10message. Response will be your server
  6214. 3:54:13response.
  6215. 3:54:15And here first of all we going to get
  6216. 3:54:16couple of things. So first what we need
  6217. 3:54:18to get right. So we going to get our
  6218. 3:54:20method. So let's go here. So we'll get
  6219. 3:54:23our method which will be request dot
  6220. 3:54:25method. or else let's keep this one as
  6221. 3:54:27get. Next we are going to get our path
  6222. 3:54:30name. I think this part we've already
  6223. 3:54:32discussed. So let's copy this from here
  6224. 3:54:34and then we'll paste it. So now also
  6225. 3:54:37let's set the uh header as pl uh text
  6226. 3:54:41only. Save this. So now what I going to
  6227. 3:54:43do right? So first of all I'm simply
  6228. 3:54:45going to do if method is equal to get
  6229. 3:54:50and then what you're going to do what
  6230. 3:54:52else? Maybe let's do one thing for get I
  6231. 3:54:54think we've already discussed. So let's
  6232. 3:54:56do this one as post only. This is where
  6233. 3:54:58we are going to do something. So here
  6234. 3:54:59what I'll do I'm simply going to check
  6235. 3:55:01if the path name is equal to equal to
  6236. 3:55:04users then we are going to do something.
  6237. 3:55:06Okay. So now this particular post is now
  6238. 3:55:10a post request. Okay. This particular
  6239. 3:55:12path not post this particular path is
  6240. 3:55:14now a post request. Post means this is
  6241. 3:55:17going to expect some kind of JSON
  6242. 3:55:18request body. Okay. So for example let's
  6243. 3:55:21say when you are sending something from
  6244. 3:55:23your UI you are going to send your name
  6245. 3:55:25and email something like that. So in
  6246. 3:55:27that case what I'll do right is I'm
  6247. 3:55:28going to create a type and this give
  6248. 3:55:31this one as maybe let's say create user
  6249. 3:55:33body that I'm actually creating some uh
  6250. 3:55:36user. So where I'm going to keep the
  6251. 3:55:38name as string and also I'm going to
  6252. 3:55:40pass email which will be string these
  6253. 3:55:42two things. So now what I'm going to do
  6254. 3:55:45right so first of all this particular
  6255. 3:55:47data that I'm going to send from my UI
  6256. 3:55:49side I need to read it in my back end.
  6257. 3:55:51Okay. So for now what I can do right so
  6258. 3:55:53here we can make use of chunks. So here
  6259. 3:55:56first of all I'm going to take chunks
  6260. 3:55:58and these will be buffer and here what
  6261. 3:56:00I'm going to do let's take this one as
  6262. 3:56:02empty for now. What we are simply doing
  6263. 3:56:04right we're actually storing our
  6264. 3:56:05incoming chunks here. Now what these
  6265. 3:56:08chunks will be each and every chunk that
  6266. 3:56:09we are going to store that will be part
  6267. 3:56:12of your request body. Okay. Now what we
  6268. 3:56:16going to do right? So first of all let's
  6269. 3:56:17do one thing. Let's take a request and
  6270. 3:56:19here we are going to take a on uh
  6271. 3:56:21listener. Now what we going to do right
  6272. 3:56:23here we have a method. Oh sorry we have
  6273. 3:56:25a event called data. What this data
  6274. 3:56:28event is going to do? This data event is
  6275. 3:56:31going to run every time node is going to
  6276. 3:56:34receive your body chunk or new body
  6277. 3:56:37chunk. Let me write it or else it will
  6278. 3:56:38be difficult. So this data event is
  6279. 3:56:42going to run is going to run every time
  6280. 3:56:47node is sorry node receives
  6281. 3:56:50a new body chunk. So this event is going
  6282. 3:56:54to be triggered. Okay.
  6283. 3:56:57when you're going to send small JSON
  6284. 3:56:59body. So maybe this will run only once.
  6285. 3:57:02If you're sending larger body, this
  6286. 3:57:04might run multiple times. So now what we
  6287. 3:57:07are going to do right? So this is going
  6288. 3:57:08to receive chunk and chunk type will be
  6289. 3:57:12buffer. Okay. So now what I going to do
  6290. 3:57:14right? So first I'm going to take the
  6291. 3:57:16chunks that we have and then I'm going
  6292. 3:57:18to post this chunk that I'm receiving
  6293. 3:57:21here. So I'm just posting this one to
  6294. 3:57:23this chunk array. Okay. Now what are we
  6295. 3:57:26going to do? We going to take a end uh
  6296. 3:57:28here. So I'm just going to do request
  6297. 3:57:30dot end sorry request dot on and this
  6298. 3:57:34will be my end uh event. What this end
  6299. 3:57:37event is going to do. So this is going
  6300. 3:57:39to run when very very important you can
  6301. 3:57:41note it down. This end event is going to
  6302. 3:57:44run when the full request body arrived.
  6303. 3:57:47Means node already received the complete
  6304. 3:57:49request body. Okay. Now what will be the
  6305. 3:57:52proper way to frame this? All right
  6306. 3:57:55everyone. So I just got a phone call. So
  6307. 3:57:56guys so what I was talking about that
  6308. 3:57:58when this particular event is going to
  6309. 3:58:00run that when the node is going to
  6310. 3:58:02receive the complete request body. So
  6311. 3:58:04this the proper way to frame this one
  6312. 3:58:06will be that combining [snorts] all the
  6313. 3:58:08chunks. Okay. So what this is going to
  6314. 3:58:10happen right until unless this
  6315. 3:58:12particular event will run completely
  6316. 3:58:15node will not have the complete request
  6317. 3:58:17body you're getting. So once it will
  6318. 3:58:19have the complete or full request body
  6319. 3:58:22that time this is going to run this
  6320. 3:58:24particular event. So here what we are
  6321. 3:58:25going to do right. So let's take the
  6322. 3:58:28call back. So first of all I'm going to
  6323. 3:58:29get the raw body and this will be so
  6324. 3:58:32first of all I'm going to take buffer
  6325. 3:58:34dot contact here we're going to pass the
  6326. 3:58:37chunks because are going to receive all
  6327. 3:58:39the chunks because this is going to push
  6328. 3:58:41all the every chunk to this array and
  6329. 3:58:44then I'm simply going to do a string. So
  6330. 3:58:46let's do two string and let's pass a
  6331. 3:58:48UTF8.
  6332. 3:58:50Okay. So now what this buffer do concat
  6333. 3:58:54is going to do? This is going to combine
  6334. 3:58:55all the received chunks into one buffer.
  6335. 3:58:59Okay. I hope this is clear. Now first of
  6336. 3:59:01all let's give a check that if this body
  6337. 3:59:04is not received because there is a
  6338. 3:59:05possibility that user haven't sent
  6339. 3:59:07anything. So in that case what you're
  6340. 3:59:09going to do? So I'm just simply going to
  6341. 3:59:10do race dot status code will now become
  6342. 3:59:12400 because there is a error and message
  6343. 3:59:14we're going to send that okay your
  6344. 3:59:16request body is required or empty and
  6345. 3:59:20then we are going to just do return okay
  6346. 3:59:23and now what we going to do right we're
  6347. 3:59:25going to actually parse this so I'm just
  6348. 3:59:27going to do body and this will be JSON
  6349. 3:59:29dotparse and here we're going to pass
  6350. 3:59:31your raw body and now what will be the
  6351. 3:59:34type now this is very important okay so
  6352. 3:59:36the type will be create user body why
  6353. 3:59:39Because this is the format that you are
  6354. 3:59:40actually expecting. Consider if the user
  6355. 3:59:43is not sending name and email. So in
  6356. 3:59:45that case let's say it's sending instead
  6357. 3:59:47of name it's sending address or instead
  6358. 3:59:49of email it sending age that is a
  6359. 3:59:51possibility right. So we need to
  6360. 3:59:52properly typed the body. So the body
  6361. 3:59:56format will be this. So in that case now
  6362. 3:59:58I can uh clearly check this. Okay. The
  6363. 4:00:00body name is not present or if the body
  6364. 4:00:04do email is also not present. So in that
  6365. 4:00:06case I'm simply going to do race status
  6366. 4:00:08code will be what 400 because 400 is we
  6367. 4:00:11are using for our error okay not emit
  6368. 4:00:14man this will be end. So in that case
  6369. 4:00:16I'm simply going to do both name
  6370. 4:00:19and email is required and then I'm
  6371. 4:00:24simply going to do return. So now in
  6372. 4:00:28this point if you notice in this
  6373. 4:00:30particular point what will happen right
  6374. 4:00:32everything is actually done. So we
  6375. 4:00:33already check all the age cases. Okay.
  6376. 4:00:36So what I mean by H cases because this
  6377. 4:00:37is very important when you are working
  6378. 4:00:39with Okay. One more thing I think we
  6379. 4:00:42missed it. So let's quickly do this one
  6380. 4:00:45after this right. Let's take a try catch
  6381. 4:00:47block. So that is also good practice. So
  6382. 4:00:49put all of this inside a try. Okay. So
  6383. 4:00:52here what I was talking about that when
  6384. 4:00:54you're actually implementing any API
  6385. 4:00:56right the first thing what you have to
  6386. 4:00:59consider that when you're doing the
  6387. 4:01:00planning that whether you are covering
  6388. 4:01:02all the age cases or not because
  6389. 4:01:03covering age cases is very important
  6390. 4:01:05what I'm talking about age cases for
  6391. 4:01:06example this is one age case right age
  6392. 4:01:09case means the error can happen so you
  6393. 4:01:11are actually preventing the error
  6394. 4:01:12similarly this is another age case here
  6395. 4:01:15okay so now but this is the try and here
  6396. 4:01:18I'm just going to take a catch now in
  6397. 4:01:20the catch now This is not yet completed.
  6398. 4:01:22try you need to do so in the catch I'm
  6399. 4:01:24simply going to do rest status code is
  6400. 4:01:26maybe 400 and let's send rest dot end
  6401. 4:01:30and I'm simply going to do invalid
  6402. 4:01:32JSON body something like this and now
  6403. 4:01:35once here once you check all of this
  6404. 4:01:37right what you can do right now
  6405. 4:01:40if there is no error so in that case you
  6406. 4:01:42simply do
  6407. 4:01:44raise dot status code is 201 and then
  6408. 4:01:46you're going to do rest dot end will be
  6409. 4:01:50your user is now created now at the And
  6410. 4:01:52when you're creating actual
  6411. 4:01:53implementation what you're going to do
  6412. 4:01:55right you are going to store all this in
  6413. 4:01:57your DB but for now we'll do body name
  6414. 4:02:00and your email is body email. Okay
  6415. 4:02:04that's it. So this is done. So now so
  6416. 4:02:07this will be your race end event. Here
  6417. 4:02:10you can also take one more. So I'll do
  6418. 4:02:13request do on and here I'm just going to
  6419. 4:02:15pass in case if there is any error
  6420. 4:02:16happen. So in that case what you're
  6421. 4:02:18going to do? So we're going to pass a
  6422. 4:02:20raise dot status code will be 500
  6423. 4:02:23because this can be a any error right?
  6424. 4:02:25So, it's sending a 500 and I'll do raise
  6425. 4:02:27end and I'll simply do fail to
  6426. 4:02:31create
  6427. 4:02:33u uh request body or something or maybe
  6428. 4:02:36let's do this fail to read request body
  6429. 4:02:40read request
  6430. 4:02:43body okay so I think this is done and
  6431. 4:02:46then what we going to do right we are
  6432. 4:02:48going to return
  6433. 4:02:51that's it so this will be if the method
  6434. 4:02:54is post and the path name is users. So
  6435. 4:02:56in that case we're going to do all of
  6436. 4:02:58this. If this is not the case in that
  6437. 4:03:00case what I'll do do raise status code
  6438. 4:03:02will be 404 because you are maybe
  6439. 4:03:04sending to a route which doesn't exist.
  6440. 4:03:07And here also I'm just going to do raise
  6441. 4:03:09end will be route not found. So this is
  6442. 4:03:14done and now what you're going to do
  6443. 4:03:16let's copy this one from here.
  6444. 4:03:20Save this and this is done. Now I hope
  6445. 4:03:22that this part is clear right. So first
  6446. 4:03:24what we doing we are taking a chunks
  6447. 4:03:26then we are taking a event this will be
  6448. 4:03:30the data event listener. So this is
  6449. 4:03:31going to run every time node is going to
  6450. 4:03:33receive the body chunk. We are going to
  6451. 4:03:35push that chunk into our chunks in our
  6452. 4:03:37end. First we are going to read the body
  6453. 4:03:40raw body from. So first what this is
  6454. 4:03:43going to do this is going to I already
  6455. 4:03:45mentioned right what buffer uh concat is
  6456. 4:03:48going to do. So buffer docon is going to
  6457. 4:03:50combine all the receipt chunks into one
  6458. 4:03:52buffer. Now this is very important.
  6459. 4:03:54Okay. And then you're simply checking
  6460. 4:03:56two edge cases. If this is not present
  6461. 4:03:58that means you are not sending your
  6462. 4:03:59request body then you're parsing the
  6463. 4:04:01body then you're telling if the body
  6464. 4:04:03don't have the name and email because
  6465. 4:04:05this is the format or data format we
  6466. 4:04:07need right if this is the case then also
  6467. 4:04:09returning one error and then we are
  6468. 4:04:11sending request dot status code 201
  6469. 4:04:13means this is now successful okay there
  6470. 4:04:15is no error so now what I can do right
  6471. 4:04:18so let's go here clear this and I'm
  6472. 4:04:21simply going to do npm run 03 this is
  6473. 4:04:24now running on 5002 So let's go here.
  6474. 4:04:275002
  6475. 4:04:32okay/ users change this one to get
  6476. 4:04:35because see if I change this one right
  6477. 4:04:36now route not found because this is not
  6478. 4:04:38get so this will be a post try to send
  6479. 4:04:41request body is request fantastic so
  6480. 4:04:43that means what happened so this
  6481. 4:04:45particular part is running because
  6482. 4:04:46you're not sending anything now let's go
  6483. 4:04:48where you have to go to body let's go to
  6484. 4:04:51raw and we have a JSON okay so in that
  6485. 4:04:53case what you're going to do right so
  6486. 4:04:55first I'm just going to send the name.
  6487. 4:04:57So name will be sungum comma and I'm
  6488. 4:05:00just going to say email and email will
  6489. 4:05:03be sung at thegmail.com.
  6490. 4:05:05Okay. So now if I send this user created
  6491. 4:05:09with this and try to give a different
  6492. 4:05:11property. So I'll do name to email one.
  6493. 4:05:13And if you send this you can see that
  6494. 4:05:14both name name and email is required
  6495. 4:05:16because this is not the expected data
  6496. 4:05:18that we need. We need name and email
  6497. 4:05:21only here. So if you do John and let's
  6498. 4:05:24do here John 1gmail.com send this. So
  6499. 4:05:27now user is created. So that means we
  6500. 4:05:30successfully actually read our request
  6501. 4:05:32body without using any third party
  6502. 4:05:34framework. Okay. So this is you are
  6503. 4:05:36using pure node. Okay. So we are not
  6504. 4:05:38using anything rather than node concept.
  6505. 4:05:40This is very very important to
  6506. 4:05:41understand. You see that how things are
  6507. 4:05:43happening behind the scenes. Okay. So
  6508. 4:05:45with this I think this part is
  6509. 4:05:47completed. Now let's move on to the next
  6510. 4:05:48one. Okay. So let's move to the next one
  6511. 4:05:50which will be how you can work with JSON
  6512. 4:05:52responses right because when you're
  6513. 4:05:54creating actual project you will see
  6514. 4:05:55that most of the times you're going to
  6515. 4:05:56send the JSON responses okay back to
  6516. 4:05:59your client. So here I'm simply going to
  6517. 4:06:01do JSON and let's do response. DS. Okay.
  6518. 4:06:05So first of all what I going to do right
  6519. 4:06:07okay let's create the so let's copy this
  6520. 4:06:10from here and let's copy this also paste
  6521. 4:06:13it here and change this one to 5003 so
  6522. 4:06:15that we can understand what is
  6523. 4:06:16happening. We can keep the same code
  6524. 4:06:18also that is also fine. So first I'll do
  6525. 4:06:21http create server. So I think this part
  6526. 4:06:24we have already covered multiple times.
  6527. 4:06:26We have incoming request response will
  6528. 4:06:27be server response and then we are going
  6529. 4:06:30to have our call back. So first of all
  6530. 4:06:31what we going to get? So we're going to
  6531. 4:06:33get three things. We're going to get the
  6532. 4:06:35method. We are going to get the request
  6533. 4:06:36URL and the path name. So copy these
  6534. 4:06:38three because at the end we have to
  6535. 4:06:40check the path name. Right? So first of
  6536. 4:06:42all what we going to do right? So here
  6537. 4:06:44now we are going to create a helper.
  6538. 4:06:46Okay. And this helper will be uh let's
  6539. 4:06:48create this one as send JSON. Okay. Now
  6540. 4:06:53first of all let's do one thing. I cop
  6541. 4:06:55uh I have created some type. Okay. You
  6542. 4:06:57understand what is happening here. So
  6543. 4:06:59first let's go ahead and paste this. So
  6544. 4:07:01first you can see that we have a user
  6545. 4:07:02type, user as ID, name and email. This
  6546. 4:07:06is our API response. So this is the for
  6547. 4:07:07API response format that we want to send
  6548. 4:07:10back to our client. So we want to return
  6549. 4:07:12that if the response is or the request
  6550. 4:07:15is successful. So we're going to return
  6551. 4:07:17success true or false. We also want to
  6552. 4:07:19return a message so that user will
  6553. 4:07:22understand. Okay, for example, let's say
  6554. 4:07:23from the UI side, you are submitting one
  6555. 4:07:26form, right? Or let's say you're
  6556. 4:07:27creating one product data or let's say
  6557. 4:07:29you're creating uh maybe okay what to
  6558. 4:07:32think so let's say you're creating some
  6559. 4:07:34uh admin creating some user information
  6560. 4:07:37okay so in that case once the that
  6561. 4:07:40request is successful so that admin or
  6562. 4:07:43the end user should understand whether
  6563. 4:07:45that is successful or not right so in
  6564. 4:07:47that case what we usually do we'll
  6565. 4:07:49render some kind of message in the toast
  6566. 4:07:51or let's say in a dialogue so that they
  6567. 4:07:53will understand okay user is now created
  6568. 4:07:55successfully why Why I'm repeating all
  6569. 4:07:56of this multiple times? Because this is
  6570. 4:07:58the format we are going to use in our
  6571. 4:08:00next project also. And that time I'm not
  6572. 4:08:01going to repeat the same thing again and
  6573. 4:08:03again and success will be true or false.
  6574. 4:08:05What is the data? Data is the actual
  6575. 4:08:07data that I want to send to our client.
  6576. 4:08:10Okay. And in case there is any error. So
  6577. 4:08:12in that case I'm going to return the
  6578. 4:08:14error. So that is the reason you can see
  6579. 4:08:15that both are optional. So now what I'm
  6580. 4:08:17going to do is I'm going to create a
  6581. 4:08:19send JSON. Let's take a generic here.
  6582. 4:08:21And in this one what I'm going to do? So
  6583. 4:08:23first of all my response will be my
  6584. 4:08:25server response. Then I'm also going to
  6585. 4:08:27take a status code
  6586. 4:08:30and this status code will be number
  6587. 4:08:33and then I'm going to take a body
  6588. 4:08:37and this will be my API response
  6589. 4:08:41this one and you're going to pass this t
  6590. 4:08:44here. Okay, that's it. And let's return
  6591. 4:08:47here void. So first of all what we have
  6592. 4:08:50to do? So first we have to set the
  6593. 4:08:51status code. So first I'll do res status
  6594. 4:08:54code will be the actual status code we
  6595. 4:08:56are receiving here because when we going
  6596. 4:08:58to call this helper the status code can
  6597. 4:09:01be 200 it can be 201 it can be 400 it
  6598. 4:09:04can be 401 it can be 500 also so we
  6599. 4:09:06don't know what actually the status code
  6600. 4:09:08is we have to set it here what this
  6601. 4:09:10status code is going to tell so status
  6602. 4:09:13code is going to tell the client that
  6603. 4:09:14what actually happened for example 200
  6604. 4:09:16means it's successful 201 means
  6605. 4:09:18something we have created that is
  6606. 4:09:20successful 400 Means some error happened
  6607. 4:09:23and that error happened from the client
  6608. 4:09:25side means client sends some invalid
  6609. 4:09:26data then only 400 error we are getting.
  6610. 4:09:29For example, let's say in that form we
  6611. 4:09:31you uh ad sorry not admin the client
  6612. 4:09:34needs to send name age and email but
  6613. 4:09:37client only send name email but client
  6614. 4:09:40didn't send age. So in that case we are
  6615. 4:09:42going to render that okay age you also
  6616. 4:09:44have to send age is a mandatory field.
  6617. 4:09:46Then let's say you're getting 404. So in
  6618. 4:09:48that case route not found 500 500 means
  6619. 4:09:51some internal server header uh error
  6620. 4:09:54happened. So all these things this rest
  6621. 4:09:56do status code is going to mention and
  6622. 4:09:58then what we going to do right I'm
  6623. 4:09:59simply going to do r dot set header. Now
  6624. 4:10:02this set header will now be content type
  6625. 4:10:06extremely important concept because this
  6626. 4:10:08is what we are going to use and this
  6627. 4:10:10will now be not plain text this will be
  6628. 4:10:13a JSON data. So in this case we are
  6629. 4:10:16simply telling that okay content type is
  6630. 4:10:18going to tell the client that how we are
  6631. 4:10:19going to understand your request body.
  6632. 4:10:21So in this case we are simply telling
  6633. 4:10:22that your request body will be JSON so
  6634. 4:10:25that we can parse it. So this is done
  6635. 4:10:27and then what we going to do right I'm
  6636. 4:10:29just going to do rest dot not same man
  6637. 4:10:31rest. And I'm just going to do JSON dot
  6638. 4:10:34stringify.
  6639. 4:10:36This popup is extremely irritating. I
  6640. 4:10:39don't like this. Okay. So this one you
  6641. 4:10:41see and this will be body. Okay. So we
  6642. 4:10:45want to send this uh in the stringify
  6643. 4:10:48format JSON string format. So this will
  6644. 4:10:51be my send JSON helper. Okay. So now
  6645. 4:10:53what we can do we can use this in every
  6646. 4:10:55place. How? So now let's say if we go
  6647. 4:10:57here that okay now if the method is
  6648. 4:10:59equal toal to get
  6649. 4:11:02and the path name is equal to home home
  6650. 4:11:06path. So in that case I'm simply going
  6651. 4:11:08to call the send JSON and here I'm going
  6652. 4:11:11to pass what response. So you to pass
  6653. 4:11:13this response then you're going to pass
  6654. 4:11:15what? You have to pass the status code.
  6655. 4:11:18So in this case this will be 200 and
  6656. 4:11:20then you also have to pass the body.
  6657. 4:11:22Body will be actual data. So in that
  6658. 4:11:24case let's I'm going to pass a success
  6659. 4:11:26which will be true. I wanted to pass a
  6660. 4:11:28message. This will be let's say server
  6661. 4:11:30is running something like this. And then
  6662. 4:11:33we are going to pass a data and this
  6663. 4:11:35data will be let's say any data maybe
  6664. 4:11:37let's say we are sending here routes and
  6665. 4:11:41then maybe I'm just going to do this one
  6666. 4:11:43as get/
  6667. 4:11:45user something like this okay so now
  6668. 4:11:47let's save this so it's simply calling
  6669. 4:11:49this and now this is going to send your
  6670. 4:11:51JSON response back okay so this is one
  6671. 4:11:53so now what you're going to do right
  6672. 4:11:55let's say I wanted to get this user
  6673. 4:11:57information also so in that case what I
  6674. 4:11:59can do I can create one more here so I'm
  6675. 4:12:01just going to do if
  6676. 4:12:03method is equal to get and if the path
  6677. 4:12:08name is equal to / users. So in that
  6678. 4:12:11case what I'm going to do is I'll copy
  6679. 4:12:13this from here
  6680. 4:12:16paste it. So uh this will be same and
  6681. 4:12:19here I'm going to pass message and I
  6682. 4:12:21will do user
  6683. 4:12:23fed successfully and in the data I'm
  6684. 4:12:27going to pass the users.
  6685. 4:12:29this user is nothing but this data. This
  6686. 4:12:33data we going to return. Okay. So now
  6687. 4:12:36let's first of all let's check this and
  6688. 4:12:38also at the end what I have to do right
  6689. 4:12:41here after this I'm simply going to take
  6690. 4:12:43another send JSON and I'm simply going
  6691. 4:12:46to pass this one as null and when we are
  6692. 4:12:49sending any request which route is not
  6693. 4:12:52present. So we're going to pass a 404
  6694. 4:12:54and in this case we're going to pass a
  6695. 4:12:55success will be false. Message will be
  6696. 4:12:58route not found
  6697. 4:13:03and also you're going to do a error and
  6698. 4:13:05this will be let's say this method
  6699. 4:13:10and then you're going to pass this path
  6700. 4:13:11name
  6701. 4:13:13is not exist and then we'll go here and
  6702. 4:13:17then just run this. So copy this,
  6703. 4:13:21paste it, save,
  6704. 4:13:24and then we'll just add it here. So I'm
  6705. 4:13:27going to add one more. So this will be
  6706. 4:13:2804. And this will be tsx source/ this.
  6707. 4:13:33So save this.
  6708. 4:13:35And now close this, clear it. And now
  6709. 4:13:39this will be 04. So server is now
  6710. 4:13:41running. So now let's go here. And I'm
  6711. 4:13:43simply going to do 5003.
  6712. 4:13:46So this will be get. And let's see first
  6713. 4:13:48of all which uh what we are using here
  6714. 4:13:50right. So we have this uh slash this
  6715. 4:13:53path. So in this case we should get
  6716. 4:13:55server is now running. So send this. So
  6717. 4:13:58we're getting true message. We are
  6718. 4:13:59getting the data. And now let's do this
  6719. 4:14:02on / users. So in this case we should
  6720. 4:14:04get the user here. We're getting 500
  6721. 4:14:08error. Something we are doing wrong
  6722. 4:14:10here.
  6723. 4:14:14Oh we have to return from here man. we
  6724. 4:14:16have to return. Okay,
  6725. 4:14:18this is what we have to do. So let's
  6726. 4:14:20save this.
  6727. 4:14:22Clear it and run once again. And now
  6728. 4:14:24let's try to send and you can see that
  6729. 4:14:26you're getting the data here. So you're
  6730. 4:14:28getting success to user is now fed
  6731. 4:14:30successfully and you're getting the
  6732. 4:14:32data. Okay. So now this is how you have
  6733. 4:14:34to work with JSON. This is what we are
  6734. 4:14:36going to use because once we start
  6735. 4:14:38express this will make our life much
  6736. 4:14:40easier because express will give some of
  6737. 4:14:42the out ofthe-box features that we don't
  6738. 4:14:43have to implement manually right. So
  6739. 4:14:45with these four very very important uh
  6740. 4:14:48topic that we have understood first of
  6741. 4:14:51all basic HTTP server KSN routing
  6742. 4:14:53concept reading body and then how you
  6743. 4:14:57can work with JSON responses. So with
  6744. 4:14:58this now let's move to the next one
  6745. 4:14:59which will be our last concept for this
  6746. 4:15:01particular uh section which will be how
  6747. 4:15:04you can call external APIs and what is
  6748. 4:15:06the best way to do that. Okay because
  6749. 4:15:09calling external APIs is very simpler
  6750. 4:15:10but you have to do some of the concepts.
  6751. 4:15:13So let's see how we are going to do that
  6752. 4:15:14in the next part. All right. So now
  6753. 4:15:16let's end this particular section with
  6754. 4:15:18another very important concept that how
  6755. 4:15:20you can work with external API calls
  6756. 4:15:22which is also one of the most frequent
  6757. 4:15:25uh functionality that you can do. I not
  6758. 4:15:27most frequent but yeah some of the times
  6759. 4:15:28you are going to do this. Okay. So now
  6760. 4:15:31so one of the most practical example
  6761. 4:15:33will be let's say you are getting some
  6762. 4:15:35task where you have to do some kind of
  6763. 4:15:37web scrapping. Okay. Now in this case
  6764. 4:15:39what you have to do right? So let's say
  6765. 4:15:40you are going to have some external API
  6766. 4:15:42and in first of all you're going to get
  6767. 4:15:44the content. So you have to call that
  6768. 4:15:46API right? And then let's say whatever
  6769. 4:15:48content you're going to get you have to
  6770. 4:15:49scrap the data something like this. I'm
  6771. 4:15:51just giving a normal example or there
  6772. 4:15:53can be like what we are going to do now.
  6773. 4:15:54So you're going to have normal API call.
  6774. 4:15:56You're going to get the response back
  6775. 4:15:58and then let's say you're going to do
  6776. 4:16:00something with that response and then at
  6777. 4:16:02the end you're going to send the data
  6778. 4:16:03back to your client. Okay. So first of
  6779. 4:16:05all what we are going to do right we
  6780. 4:16:07will be using this JSON placeholder API.
  6781. 4:16:09This is very famous. Okay. And now if
  6782. 4:16:12you notice one thing okay here. So
  6783. 4:16:14notice that what are the format or
  6784. 4:16:16things not format the properties that we
  6785. 4:16:18are receiving here. So we have ID name,
  6786. 4:16:20username, address, phone, website,
  6787. 4:16:22company all these. So first of all what
  6788. 4:16:24we are going to do right so let's say I
  6789. 4:16:26have to actually call the data from this
  6790. 4:16:28particular API only or I have to get the
  6791. 4:16:30data. So what are you going to do right?
  6792. 4:16:32So first I'm going to take this one as
  6793. 4:16:33my API URL. Now one very very important
  6794. 4:16:36concept I'm going to explain. Okay and
  6795. 4:16:38this is going to be a game changanger
  6796. 4:16:40for all of you. I'm not sure if some of
  6797. 4:16:41you actually know this particular
  6798. 4:16:43concept or not. So what I'm trying to do
  6799. 4:16:45right so you notice one thing right when
  6800. 4:16:48you are going to call the external API
  6801. 4:16:50from a API source or from a URL or from
  6802. 4:16:53a source you notice that that particular
  6803. 4:16:56uh is going to give you the data in a
  6804. 4:16:58certain format. So what I mean by this?
  6805. 4:17:00So in this case you have a ID, name,
  6806. 4:17:02username, address and then you're going
  6807. 4:17:04to have street that is inside address.
  6808. 4:17:06Similarly you have company and you see
  6809. 4:17:07that you have name inside this company
  6810. 4:17:10object. So that means this format
  6811. 4:17:12actually provided by this particular
  6812. 4:17:13API. But let's say in our application
  6813. 4:17:17you need the data in a certain format
  6814. 4:17:19and this is what we called as or I
  6815. 4:17:21called as uh uh data trans
  6816. 4:17:25transformation. Okay. So let's do this
  6817. 4:17:27one with example or else this will be a
  6818. 4:17:28little tricky. So what I'm going to do
  6819. 4:17:30right I'm going to create one type and
  6820. 4:17:31I'm just going to give this one as
  6821. 4:17:33placeholder user. Why I'm giving
  6822. 4:17:35placeholder user because this is the
  6823. 4:17:37format that we are getting from this
  6824. 4:17:39URL. So now in this one I'm going to get
  6825. 4:17:42a number. Sorry ID which will be a
  6826. 4:17:43number. Okay. So we have a id then we
  6827. 4:17:45are going to get get a name right. So
  6828. 4:17:47I'm just going to get a name. This will
  6829. 4:17:50be what? String. Then we are going to
  6830. 4:17:52get a email. So this also will be
  6831. 4:17:53string. And now you see that we are
  6832. 4:17:55going to get a company and then inside
  6833. 4:17:57company you're going to get name
  6834. 4:17:59correct. So now I'm just going to take
  6835. 4:18:00this one as company and this particular
  6836. 4:18:02company is going to have a name which
  6837. 4:18:04will be string. Now this is only
  6838. 4:18:06possible if you know the format of that
  6839. 4:18:09external API right from where you are
  6840. 4:18:10fetching the data. If you don't know in
  6841. 4:18:12that case you have to use some unknown
  6842. 4:18:13type and based on that you have to parse
  6843. 4:18:15get the data. you're getting that what
  6844. 4:18:17I'm talking about because in this case
  6845. 4:18:18we actually know that okay this is the
  6846. 4:18:20data format we are going to receive when
  6847. 4:18:22we are going to call this particular API
  6848. 4:18:23URL but now what I'm going to do right
  6849. 4:18:26we are going we are not going to use
  6850. 4:18:28this particular data set instead we are
  6851. 4:18:30going to create our own data set so here
  6852. 4:18:32I'm just going to do this one as public
  6853. 4:18:35user okay and now what I need I need a
  6854. 4:18:38number sorry ID which will be a number I
  6855. 4:18:40need a name which will be another uh
  6856. 4:18:43string I need a email which also will be
  6857. 4:18:46string but in this case I don't need
  6858. 4:18:49name inside company instead what I'm
  6859. 4:18:51going to do right I'm going to get a
  6860. 4:18:53company directly so that means this is
  6861. 4:18:55my data format or this is the
  6862. 4:18:57requirement of this application that I'm
  6863. 4:18:59building and this is the data format I'm
  6864. 4:19:01receiving from that APA okay so that
  6865. 4:19:03means once I'll get the data I need to
  6866. 4:19:05transfer this particular data to this
  6867. 4:19:07particular format so in this case I hope
  6868. 4:19:09this is you're getting right so what I'm
  6869. 4:19:11doing here so now how we are going to do
  6870. 4:19:13that to do this what we going to do
  6871. 4:19:15Right? We need to create a function
  6872. 4:19:16always here. Okay. So I'm just going to
  6873. 4:19:18create a function and this is will be
  6874. 4:19:20your transform data or you can do
  6875. 4:19:22transform user because we are working
  6876. 4:19:23with user here. Right? In this case now
  6877. 4:19:27what will happen right? This will be
  6878. 4:19:28your raw data. Now this raw data is
  6879. 4:19:30nothing but the data that you are
  6880. 4:19:32receiving from this URL. So the type
  6881. 4:19:34will be this because this is the format
  6882. 4:19:36you receiving from that particular data
  6883. 4:19:38source. So this will be my placeholder
  6884. 4:19:40user. But when we are going to return
  6885. 4:19:43it, I need to return this in this
  6886. 4:19:45particular format. So that means we are
  6887. 4:19:47actually transforming the data here. So
  6888. 4:19:49that means when I'm going to do I'll
  6889. 4:19:50return ID which will be my raw data dot
  6890. 4:19:53id. Then I'm going to return a name
  6891. 4:19:55which will be my raw data dot name. Then
  6892. 4:19:58I'm going to return a uh email which
  6893. 4:20:00will be raw data. Sorry if you're
  6894. 4:20:02getting any noise. I think it's coming
  6895. 4:20:03from outside some bike. And then I'm
  6896. 4:20:06going to return the company which will
  6897. 4:20:07be raw data dot company dot name. So
  6898. 4:20:11what we have done based on our
  6899. 4:20:13application requirement we are changing
  6900. 4:20:15the raw data into a certain format. Now
  6901. 4:20:18this format can be anything and this
  6902. 4:20:19format can be anything in case you don't
  6903. 4:20:22know this particular format. So in that
  6904. 4:20:23case you have to use unknown type right.
  6905. 4:20:26So now I think this is done. So first of
  6906. 4:20:28all what we are going to do. So first
  6907. 4:20:30let's see the best way that we can do a
  6908. 4:20:32API call. So here we are going to do
  6909. 4:20:34fetch external user right. This is
  6910. 4:20:36external API call. What will be the data
  6911. 4:20:39uh return type? So return type will be a
  6912. 4:20:41promise and this will be a void. So
  6913. 4:20:43first of all what we have to do here now
  6914. 4:20:46see you can directly use fetch okay but
  6915. 4:20:50we have to use certain functionality
  6916. 4:20:52here and that uh functionality we call
  6917. 4:20:54as about controller. So what actually
  6918. 4:20:56about controller is so first let me
  6919. 4:20:58write it and then I'm going to explain.
  6920. 4:21:00So this will be new about controller.
  6921. 4:21:02Now you have to understand this one in a
  6922. 4:21:04proper way. Now first of all this
  6923. 4:21:06particular thing right whatever this is
  6924. 4:21:09we don't know what actually this is so
  6925. 4:21:10this is going to help us to cancel
  6926. 4:21:14certain fetch request so this will be so
  6927. 4:21:17let me put in a uh proper way so this
  6928. 4:21:20will be about controller
  6929. 4:21:22lets us
  6930. 4:21:24uh cancel and in progress
  6931. 4:21:28fetch request in progress fetch request
  6932. 4:21:33okay Now when you directly use fetch
  6933. 4:21:37right so let's say you are directly
  6934. 4:21:38using fetch what will happen right fetch
  6935. 4:21:40actually doesn't automatically fail
  6936. 4:21:42let's say what will happen right maybe
  6937. 4:21:44you are doing API call and that API call
  6938. 4:21:45didn't give any response in first 10
  6939. 4:21:47seconds that is possible right so in
  6940. 4:21:49that case fetch will not help you to
  6941. 4:21:51cancel that particular request here what
  6942. 4:21:54you're going to do we are going to use
  6943. 4:21:55something called this about controller
  6944. 4:21:57and we'll see that how we are going to
  6945. 4:21:58actually implement this one when your
  6946. 4:22:00external API will become very slow in
  6947. 4:22:03that case this can be very very helpful.
  6948. 4:22:05Okay. So now uh what we are going to do
  6949. 4:22:07right. So first of all what this is
  6950. 4:22:09going to do? So this will gives us a
  6951. 4:22:11signal. Okay. And we pass that signal to
  6952. 4:22:14our fetch and then we use some method.
  6953. 4:22:16So this will give you some method. For
  6954. 4:22:18example about method and using that
  6955. 4:22:21method that particular fetch that we are
  6956. 4:22:23using that gets canled. So how we are
  6957. 4:22:25going to do that? So first of all I'm
  6958. 4:22:27going to get take a set timeout here and
  6959. 4:22:30the oh no not not like this you have to
  6960. 4:22:31take a timer so that so I'll just do a
  6961. 4:22:34timer and here I'm just going to do set
  6962. 4:22:36timeout
  6963. 4:22:37and here what I'm going to do right I'll
  6964. 4:22:39give a 5second time out here so what I'm
  6965. 4:22:41simply doing right so first of all this
  6966. 4:22:43timer is going to do what or set time is
  6967. 4:22:45set timeout is going to do what this is
  6968. 4:22:47going to start a timer now if your
  6969. 4:22:50external API doesn't respond within 5
  6970. 4:22:52seconds because we are giving 5 seconds
  6971. 4:22:54here this will happen right This timer
  6972. 4:22:56is going to do something but what
  6973. 4:22:59actually it's going to do? So this is
  6974. 4:23:00where we are going to use this
  6975. 4:23:02controller.about method. Now now you'll
  6976. 4:23:04see that this will be very clear. So
  6977. 4:23:06what we are doing that okay first we are
  6978. 4:23:08starting this then we are starting a
  6979. 4:23:10timer and we are simply checking that if
  6980. 4:23:12my API is not giving any request or
  6981. 4:23:15response or pin inside 5 seconds I'm
  6982. 4:23:18going to call this controller dotabout
  6983. 4:23:20method. So this is going to do what?
  6984. 4:23:22it's going to cancel that particular uh
  6985. 4:23:25request. Okay, so this will be the first
  6986. 4:23:26thing. After this what I'm going to do
  6987. 4:23:29I'll take a try and here we are going to
  6988. 4:23:31do actual call. So I'm just going to do
  6989. 4:23:33con and I'm simply going to do this one
  6990. 4:23:35as response and this is where I'll do a
  6991. 4:23:38fetch.
  6992. 4:23:40We going to pass the API URL and here we
  6993. 4:23:43going to pass a method. So method will
  6994. 4:23:45be get and here we are going to pass a
  6995. 4:23:47signal and this will be controller dot
  6996. 4:23:50signal. Okay. Now first of all what is
  6997. 4:23:53happening here? What fetch is going to
  6998. 4:23:55do? Fetch is going to send an HTTP
  6999. 4:23:57request to this API URL right method is
  7000. 4:24:00our gate. I I think this is very clear
  7001. 4:24:02because gate means we are reading the
  7002. 4:24:04data right. What this signal is so this
  7003. 4:24:06is going to connect this particular
  7004. 4:24:08request to this about controller above.
  7005. 4:24:11This is going to do that. I hope this is
  7006. 4:24:13clear. Right now what will happen right?
  7007. 4:24:15If you notice that we are using this
  7008. 4:24:17controller.abot method also. Right. In
  7009. 4:24:20case this particular method controller
  7010. 4:24:22that we are using that runs before this
  7011. 4:24:25API response in that case fetch is going
  7012. 4:24:28to throw an about error. Okay. So first
  7013. 4:24:30what we have to do here? So first we are
  7014. 4:24:32going to give a check okay that if this
  7015. 4:24:34response do okay if this is false. So
  7016. 4:24:36I'm just going to log error and let's
  7017. 4:24:39give this one as uh upstream
  7018. 4:24:43API field with HTTP and then we can pass
  7019. 4:24:47the response right. So we do response
  7020. 4:24:49dot status and then we are going to
  7021. 4:24:53return. So this will be the first thing.
  7022. 4:24:56Then we are going to get our data. So
  7023. 4:24:58how we are going to get the data? So
  7024. 4:24:59I'll do raw user and here we are going
  7025. 4:25:02to do a because this is async operation,
  7026. 4:25:06right? I think we are using async here
  7027. 4:25:07if I'm not wrong. We are using async. So
  7028. 4:25:10ait and this will be my response dot
  7029. 4:25:12json. But here you have to give the your
  7030. 4:25:14data format. So what will be your data
  7031. 4:25:16format? how we are going to get the data
  7032. 4:25:18in this particular format. So I'm just
  7033. 4:25:20going to as placeholder user right and
  7034. 4:25:23then we're going to transform the data
  7035. 4:25:26in the format that we want means we want
  7036. 4:25:28in this certain format. So that means we
  7037. 4:25:30need to call this method. So now what
  7038. 4:25:32I'm going to do right so I'm simply
  7039. 4:25:33going to do con
  7040. 4:25:35user and this will be my transform user
  7041. 4:25:38and you're going to pass the raw user.
  7042. 4:25:40That's it. And now what I'm going to do
  7043. 4:25:41right I'll simply just do log and I'm
  7044. 4:25:44just going to log the user and then
  7045. 4:25:46we'll take a catch.
  7046. 4:25:48Let's take a error. Now what are we
  7047. 4:25:51going to do first of all? So first I'm
  7048. 4:25:53going to check if this error is a
  7049. 4:25:55instance
  7050. 4:25:59if this instance of error
  7051. 4:26:02and if the error dot name
  7052. 4:26:06is equal to okay we have to give equal
  7053. 4:26:08to and this we called as about error
  7054. 4:26:12okay and let's just check this one here
  7055. 4:26:16so first of all I'm just going to do
  7056. 4:26:18what is about error
  7057. 4:26:23This is a specific error thrown in
  7058. 4:26:25JavaScript when an ongoing asynchronous
  7059. 4:26:27is deliberately cancelled. Okay. So
  7060. 4:26:30first of all why does it happen? Most
  7061. 4:26:32JavaScript APIs use about controller
  7062. 4:26:34about signal interfaces to manage
  7063. 4:26:36cancellation. When you invoke this about
  7064. 4:26:39that we are doing here right simply we
  7065. 4:26:41are calling this controller.about. So
  7066. 4:26:43that means what will happen right? If
  7067. 4:26:45your time out this particular timeout if
  7068. 4:26:47this is runs and this particular about
  7069. 4:26:49method is canceled sorry not canceled if
  7070. 4:26:51called. So in that case your fetch is
  7071. 4:26:53going to do is going to throw an error
  7072. 4:26:56that error called as about error. Okay.
  7073. 4:26:59So this is the task of your fetch but
  7074. 4:27:01this will only throw in case if you're
  7075. 4:27:03calling this and then you're passing the
  7076. 4:27:05signal here. Okay. This is very very
  7077. 4:27:07important. So now what are you going to
  7078. 4:27:09do right? So first of all I think this
  7079. 4:27:12is clear right or let's see if we know
  7080. 4:27:14okay let's see common operation that
  7081. 4:27:15trigger this canceling an ongoing f
  7082. 4:27:18request stopping a read or write
  7083. 4:27:20operation on streams and some
  7084. 4:27:22interrupting and database query but for
  7085. 4:27:24now we are focusing only on this one and
  7086. 4:27:25this is what actually we are doing here
  7087. 4:27:27right so we are simply telling that okay
  7088. 4:27:29if this timeout fails so in that case
  7089. 4:27:31you're going to call the about method so
  7090. 4:27:33in that case your fetch is going to
  7091. 4:27:34throw that about error so now what are
  7092. 4:27:36you going to do right so first of all if
  7093. 4:27:38this is the case So in that case I'm
  7094. 4:27:40just going to do error again
  7095. 4:27:44and I'm just going to do request failed
  7096. 4:27:47or time out whatever you are going to do
  7097. 4:27:49because
  7098. 4:27:51upstream
  7099. 4:27:52API took so long something like this and
  7100. 4:27:57[snorts] we are going to return okay so
  7101. 4:27:59this is done and then we are going to
  7102. 4:28:02simply do we are also going to take a
  7103. 4:28:04message and I'm simply going to do if
  7104. 4:28:06the error is instance is
  7105. 4:28:10of error. So we are going to get the
  7106. 4:28:13error dot message or else I'm just
  7107. 4:28:15simply going to pass unknown error.
  7108. 4:28:19Okay. And then also again we can log
  7109. 4:28:21error and I'm simply going to do
  7110. 4:28:23external
  7111. 4:28:26API failed
  7112. 4:28:29and then we're going to pass this
  7113. 4:28:30message here and then we are also going
  7114. 4:28:33to take a finally. So in the finally
  7115. 4:28:35what we have to do we have to clear the
  7116. 4:28:37time out because we have a timeout here.
  7117. 4:28:40So I'm just going to do clear time out
  7118. 4:28:41and pass the timer and timer what we are
  7119. 4:28:44using here. We're using timer only
  7120. 4:28:47right? I think let's skip maybe let's
  7121. 4:28:50skip time out. I think time out is
  7122. 4:28:51better.
  7123. 4:28:54And we're going to clear this here.
  7124. 4:28:56Okay. That's it. So now I think this is
  7125. 4:28:58done. So what we have to do right? So
  7126. 4:29:00let's save this. And to call this, we
  7127. 4:29:02are simply going to call this method
  7128. 4:29:03here.
  7129. 4:29:05That's it. So, this is done. So, now
  7130. 4:29:07let's save this. And I'm just going to
  7131. 4:29:10close. Let's clear. And I'll just copy
  7132. 4:29:14this. Let's go to package.json.
  7133. 4:29:17And I'll just paste it here. So, this
  7134. 4:29:19will be 05
  7135. 4:29:21tsx source/
  7136. 4:29:24this. And then we're going to just run
  7137. 4:29:26this. I'll do npm run 05.
  7138. 4:29:30That's it. So you're going to get error
  7139. 4:29:32not get error you're going to get the
  7140. 4:29:33data because definitely it's going to
  7141. 4:29:35pass. Now in this case one thing you
  7142. 4:29:37have to understand that even though your
  7143. 4:29:38response of this particular API is in
  7144. 4:29:41this format that you are having company
  7145. 4:29:43and inside company you are having name
  7146. 4:29:45but when you're getting the actual data
  7147. 4:29:46you're getting company as this directly.
  7148. 4:29:49Okay this is very important why because
  7149. 4:29:51in our code we are actually formatting
  7150. 4:29:53or transforming the data the way we want
  7151. 4:29:56you're getting. So this is what actually
  7152. 4:29:58it's doing. So this is very very helpful
  7153. 4:30:00when you have some certain requirement
  7154. 4:30:02in your project and you want to
  7155. 4:30:04transform the data. So you can use this
  7156. 4:30:05kind of uh uh features. So I this is
  7157. 4:30:09clear. I think this is very important.
  7158. 4:30:10So with this we have completed one very
  7159. 4:30:12important and I'm pretty much sure see
  7160. 4:30:14if you're a beginner or let's see even
  7161. 4:30:15the intermediate level this is more than
  7162. 4:30:17enough in your node HTTP module. Right?
  7163. 4:30:19Again when I'm going to create the part
  7164. 4:30:21three in case if I'm getting good
  7165. 4:30:23response I always mention that or else
  7166. 4:30:24I'm not going to do that. I'll be very
  7167. 4:30:26honest. If I get good response from part
  7168. 4:30:28one and two and you'll see that there
  7169. 4:30:30are certain concepts that normally you
  7170. 4:30:32not find in most of the courses or
  7171. 4:30:34videos those I'm going to cover but for
  7172. 4:30:36now I think this is more than enough. So
  7173. 4:30:38with this what will be the next part see
  7174. 4:30:40I'm going to repeat once again again and
  7175. 4:30:42again and I'm telling you so that you
  7176. 4:30:45find sorry not you find or you actually
  7177. 4:30:48get the most advantage from this
  7178. 4:30:49particular video. What I'm trying to say
  7179. 4:30:51is very very important that I'm going to
  7180. 4:30:53add my posgress full course that I
  7181. 4:30:55already created. This is very recent
  7182. 4:30:57course like I think maybe one month ago
  7183. 4:31:00I uploaded. I'm going to add that in the
  7184. 4:31:02next part. Please do not skip that
  7185. 4:31:04particular section. Okay. You can skip
  7186. 4:31:06the intro part. Why I'm telling because
  7187. 4:31:08that is a video part right? You can skip
  7188. 4:31:10the intro like it will be 1 minute or
  7189. 4:31:12even you can watch the intro because
  7190. 4:31:14you'll understand that what are the
  7191. 4:31:15things you are going to learn. Why this
  7192. 4:31:16is very very important because this next
  7193. 4:31:194 hours is going to give you exact idea
  7194. 4:31:22of posgress every single thing you need
  7195. 4:31:24and remember we are not going to use any
  7196. 4:31:27packages in that so we are not using any
  7197. 4:31:28OM prisma or drizzle okay these kind of
  7198. 4:31:31things we are going to use raw pg and
  7199. 4:31:33this is going to be a game changanger
  7200. 4:31:35because if you really want to learn
  7201. 4:31:36postless and then you start the API
  7202. 4:31:38project that we are going to have after
  7203. 4:31:39that so what I'm trying to say I'm going
  7204. 4:31:42to repeat once again that do not skip
  7205. 4:31:44this particular section okay this part.
  7206. 4:31:46Do not skip. First you learn this one
  7207. 4:31:49and after this you try to complete this
  7208. 4:31:51whole section without skipping any any
  7209. 4:31:53concept and then you do this then you
  7210. 4:31:56jump to this one you already have the
  7211. 4:31:58time stamp right you can directly jump
  7212. 4:32:00in case you already know then you can
  7213. 4:32:03directly jump to this particular section
  7214. 4:32:05but I suggest do not do this one okay so
  7215. 4:32:07with this now let's move on to the next
  7216. 4:32:08one which will be postgress complete
  7217. 4:32:10course and then we will start our actual
  7218. 4:32:12practical project and in this one you're
  7219. 4:32:14going to learn each and everything uh
  7220. 4:32:16that you need to know to start your
  7221. 4:32:18practical journey with node. Okay
  7222. 4:32:21everyone, so now before uh we are going
  7223. 4:32:23to proceed to our next section which
  7224. 4:32:25will be so as I already mentioned that
  7225. 4:32:27in the previous section we have
  7226. 4:32:28completed HTTP and then we are going to
  7227. 4:32:30have a complete dedicated postgress
  7228. 4:32:32section but before that I thought that
  7229. 4:32:34let's uh cover some of the very
  7230. 4:32:35important uh concept and now these will
  7231. 4:32:37be little bit advanced if you want you
  7232. 4:32:39can watch this particular section or
  7233. 4:32:41else you can also skip it's up to you
  7234. 4:32:43because this will be mostly concepts and
  7235. 4:32:45also we going to understand some of the
  7236. 4:32:47nodes internals okay so first of all So
  7237. 4:32:50how we are going to do this one right?
  7238. 4:32:51So we are going to create one one file
  7239. 4:32:53and then we'll go to uh this uh flow and
  7240. 4:32:56then we are going to understand that
  7241. 4:32:58what actually happening here and most of
  7242. 4:33:00the things you can also get in your
  7243. 4:33:01interview also. So first let's start
  7244. 4:33:04with so let's create our first file and
  7245. 4:33:06let's give this one as NodeJS
  7246. 4:33:09internals. Okay. Now in the previous
  7247. 4:33:11part we have already covered that what
  7248. 4:33:13actually NodeJS is right. So we already
  7249. 4:33:15know that NodeJS is a JavaScript
  7250. 4:33:16runtime.
  7251. 4:33:18But what we need to know that node is
  7252. 4:33:21actually combines multiple components.
  7253. 4:33:23So what I mean by multiple components?
  7254. 4:33:25So we have something called V8 right. So
  7255. 4:33:28we have something called V8 engine
  7256. 4:33:30right. So what actually V8 engine is
  7257. 4:33:32actually going to do. So this is what
  7258. 4:33:34actually executes your JavaScript right.
  7259. 4:33:36So this is the very basic things right.
  7260. 4:33:38But we have also covered in the previous
  7261. 4:33:41section that NodeJS also has some code
  7262. 4:33:43modules also. For example let's say
  7263. 4:33:44NodeJS has something called FS file
  7264. 4:33:46system. NodeJS has networking, NodeJS
  7265. 4:33:48has timers and all these things. What
  7266. 4:33:51actually happens right? So there are a
  7267. 4:33:53couple of things you need to understand
  7268. 4:33:54that how actually NodeJS run your code.
  7269. 4:33:56That is the most important part here
  7270. 4:33:58which is what we are going to cover. So
  7271. 4:34:00here we need to know first of all that
  7272. 4:34:02NodeJS actually more than V8 engine is
  7273. 4:34:05there are lot of things that is
  7274. 4:34:06happening behind the scenes. So first of
  7275. 4:34:08all what we going to cover? So some of
  7276. 4:34:10the things you need to know that is
  7277. 4:34:11first of all that what are the main
  7278. 4:34:12NodeJS runtime components that we have.
  7279. 4:34:15So first we have something called very
  7280. 4:34:17very important and that we call as main
  7281. 4:34:20JavaScript. So I'm just going to give
  7282. 4:34:22main JS thread. So what this is going to
  7283. 4:34:24do now first of all you need to know
  7284. 4:34:27that whenever you're going to create a
  7285. 4:34:28normal application right what JavaScript
  7286. 4:34:30is going to do JavaScript execute one
  7287. 4:34:33particular main JavaScript thread or
  7288. 4:34:35JavaScript execute on one main
  7289. 4:34:38JavaScript thread. Sorry I was explained
  7290. 4:34:40that one wrong. So the this will be so
  7291. 4:34:42normal applications. I'm just going to
  7292. 4:34:44do like this. Normal applications
  7293. 4:34:46JavaScript executes on one main
  7294. 4:34:50JavaScript thread. Okay. So what I mean
  7295. 4:34:53by this? So what are the process
  7296. 4:34:55actually? So here you have your top
  7297. 4:34:56level JavaScript, right? So let's say
  7298. 4:34:58you have a function. So you have your
  7299. 4:34:59top level JavaScript, you have your
  7300. 4:35:01function call, you have your request
  7301. 4:35:03handlers, you are going to have your
  7302. 4:35:04timer. So let's you are using set
  7303. 4:35:05timeout, set interval, all these. You
  7304. 4:35:08have your promise call backs, file
  7305. 4:35:09system call backs and all these things,
  7306. 4:35:11right? So that means only one normal
  7307. 4:35:13JavaScript call back execute at a time
  7308. 4:35:15on this particular thread and this we
  7309. 4:35:17call as main JavaScript thread. Right?
  7310. 4:35:19And then the second main component will
  7311. 4:35:22be your V8 engine. So what actually this
  7312. 4:35:24V8 engine uh does. Okay. Now first of
  7313. 4:35:28all you need to know that V8 is the
  7314. 4:35:30JavaScript engine that is used by
  7315. 4:35:32NodeJS. Okay. So V8 will be your
  7316. 4:35:34JavaScript engine that is used by
  7317. 4:35:36NodeJS. What this is going to do or what
  7318. 4:35:38are the main responsibilities? First of
  7319. 4:35:40all, it is going to do parsing your
  7320. 4:35:42JavaScript. So, it is going to parse
  7321. 4:35:43your JavaScript. So, parsing JS, it's
  7322. 4:35:46going to execute your JavaScript. This
  7323. 4:35:48will be the second thing. This is going
  7324. 4:35:50to manage your call stack. So, managing
  7325. 4:35:52call stack.
  7326. 4:35:54Next will be it's also going to manage
  7327. 4:35:56your heap memory also. Heap memory.
  7328. 4:36:00Okay.
  7329. 4:36:02And he memory and performing
  7330. 4:36:06garbage collection.
  7331. 4:36:10Okay.
  7332. 4:36:12And also sometimes it's going to do
  7333. 4:36:14what? It's going to optimize your
  7334. 4:36:15executed code also. But mostly you need
  7335. 4:36:17to know that these three are very very
  7336. 4:36:19important. So first of all it's actually
  7337. 4:36:21going to parse your JavaScript. It's
  7338. 4:36:22going to execute your JavaScript and
  7339. 4:36:23it's going to manage your call stack.
  7340. 4:36:25Okay. So this is another main core
  7341. 4:36:27component. So we have main JavaScript
  7342. 4:36:29thread and then we have your V8 engine.
  7343. 4:36:32Now we have right so we have our NodeJS
  7344. 4:36:34core API. Now you might be thinking okay
  7345. 4:36:36we have already covered but this is
  7346. 4:36:37still very important because now we are
  7347. 4:36:39actually understanding what is happening
  7348. 4:36:40behind the scenes right so we have your
  7349. 4:36:42NodeJS code APIs when I'm talking about
  7350. 4:36:44what actually NodeJS code APIs are the
  7351. 4:36:47things that we have already covered for
  7352. 4:36:48example our file system we have our HTTP
  7353. 4:36:50module we have our path module we have
  7354. 4:36:53our streams right so all these things we
  7355. 4:36:55have covered buffers then you have
  7356. 4:36:57process right process also we have
  7357. 4:36:59covered then you have timers all these
  7358. 4:37:01things now all these core APIs right
  7359. 4:37:03whatever you're seeing these actually
  7360. 4:37:05written in JS only. Okay. Now some of
  7361. 4:37:10these okay some of these core EPS next
  7362. 4:37:13you have something called C++. So I'm
  7363. 4:37:15just going to give it comment. So do C++
  7364. 4:37:18bindings. Okay. Now what this is going
  7365. 4:37:21to do now first of all you have to
  7366. 4:37:22understand that what this is going to
  7367. 4:37:24do. This is going to connect your
  7368. 4:37:25JavaScript facing NodeJS APIs to native
  7369. 4:37:28functionality. Okay. So that means here
  7370. 4:37:30so first of all let's write it. So it's
  7371. 4:37:32going to connect JS facing APIs to
  7372. 4:37:37native functionality.
  7373. 4:37:40So here what this is going to do right
  7374. 4:37:41it's going to allow your JavaScript code
  7375. 4:37:44to communicate to communicate with so we
  7376. 4:37:49have something called leu okay then we
  7377. 4:37:52have something called our operating
  7378. 4:37:53system so OS APIs your native libraries
  7379. 4:37:57okay now if I try to cover this leu only
  7380. 4:38:01right so it's going to take more than 2
  7381. 4:38:03hours okay so there are a lot of things
  7382. 4:38:05so that is the reason I'm just going to
  7383. 4:38:07give you the overview but when I'm going
  7384. 4:38:08to upload the part three possible if I'm
  7385. 4:38:11getting good response from this part one
  7386. 4:38:13then I'm going to upload part part two
  7387. 4:38:14anyways I'm going to upload but part
  7388. 4:38:16three if I upload so I'm going to cover
  7389. 4:38:18all of this in details okay now why
  7390. 4:38:20actually you need all of this because
  7391. 4:38:21what JavaScript is going to do right
  7392. 4:38:23JavaScript doesn't directly call this
  7393. 4:38:25operating system level kernels okay here
  7394. 4:38:28this is one of the most important
  7395. 4:38:30JavaScript core component that you need
  7396. 4:38:31to know if you want to learn NodeJS uh
  7397. 4:38:34properly now this is actually a native
  7398. 4:38:36library that is used by NodeJS okay so
  7399. 4:38:38this is a native library that is uh used
  7400. 4:38:42by NodeJS used by NodeJS. So what this
  7401. 4:38:46is going to provide right so it's going
  7402. 4:38:47to first of all provide your event loop
  7403. 4:38:49then it's going to provide your worker
  7404. 4:38:51thread worker thread pool your timers
  7405. 4:38:56your asynchronous as IO input output
  7406. 4:38:59handling
  7407. 4:39:01and also sometimes uh crossplatform
  7408. 4:39:04abstractions all these things right here
  7409. 4:39:06so first of all four things we have
  7410. 4:39:08understood right so we have something
  7411. 4:39:10called our main JS thread our VS sorry
  7412. 4:39:12V8 engine nodes code APIs we have C++
  7413. 4:39:16bindings and one very important native
  7414. 4:39:19module sorry native library that we call
  7415. 4:39:21as li okay then we have our operating
  7416. 4:39:24system now all these are actually nodes
  7417. 4:39:26internals okay you'll understand why I'm
  7418. 4:39:28explaining all of these so you your
  7419. 4:39:29operating system OS what operating
  7420. 4:39:32system is going to do I think we have
  7421. 4:39:34already covered the OS model remember
  7422. 4:39:35operating system is going to do your
  7423. 4:39:37low-level work so low-level work means
  7424. 4:39:40let's say reading your files reading
  7425. 4:39:41files writing your files
  7426. 4:39:45uh maybe let's say tracking time
  7427. 4:39:48right so all these thing it's going to
  7428. 4:39:50do so now let's say if I go here and if
  7429. 4:39:53you try to understand this one right so
  7430. 4:39:55you have your javascript code this is
  7431. 4:39:57your v8 engine and these all your core
  7432. 4:40:00components so you have your nodejs core
  7433. 4:40:02APIs which interact with your C++
  7434. 4:40:04bindings right and this this is actually
  7435. 4:40:06C++ binding is going to do what so C++
  7436. 4:40:09binding is going to connect your
  7437. 4:40:10JavaScript facing nodejs core APIs this
  7438. 4:40:13on. So you have a JavaScript facing
  7439. 4:40:14NodeJS API to your native functionality.
  7440. 4:40:16So this C++ binding is going to help to
  7441. 4:40:20communicate this particular API to your
  7442. 4:40:23Leo this library or your operating
  7443. 4:40:25system library. Okay. So this is why
  7444. 4:40:26this C++ binding is actually very
  7445. 4:40:29important. So I'm just going to repeat
  7446. 4:40:30once again why this is actually very
  7447. 4:40:32important. So have your JavaScript code.
  7448. 4:40:34So first of all you have to understand
  7449. 4:40:36what VS sorry V8 engine is going to do.
  7450. 4:40:38So V8 engine actually going to parse
  7451. 4:40:41your JavaScript. So that is the first
  7452. 4:40:42thing. It's going to execute your
  7453. 4:40:43JavaScript. It's going to manage your
  7454. 4:40:45call stack. Okay. But then you have your
  7455. 4:40:47some code API. I think we've already
  7456. 4:40:49covered code modules, file system, uh
  7457. 4:40:51HTTP, timers, all these things. But the
  7458. 4:40:53problem is this code some some of these
  7459. 4:40:56core APIs actually written in
  7460. 4:40:57JavaScript. So it's actually can't
  7461. 4:40:59directly interact with your operating
  7462. 4:41:01system, right? So that is the reason we
  7463. 4:41:03need something called or we need some
  7464. 4:41:05mediator and that we call as C++
  7465. 4:41:07bindings. So C++ bindings is going to
  7466. 4:41:10connect your JavaScript facing nodes
  7467. 4:41:12APIs to the native functionality. So
  7468. 4:41:14this native functionality will be your
  7469. 4:41:15native libraries leave and operating
  7470. 4:41:18system. What leop is going to do? So
  7471. 4:41:20this is a native library that is used by
  7472. 4:41:23nodejs. It's going to provide your event
  7473. 4:41:24loop, worker thread, timers and all
  7474. 4:41:26these things. And operating system is
  7475. 4:41:28going to do your low-level work. So
  7476. 4:41:30low-level work means it's going to do
  7477. 4:41:32let's say reading your files or writing
  7478. 4:41:34files uh tracking time let's say it
  7479. 4:41:37needs to needs to schedule some threads
  7480. 4:41:39and all these things is going to do okay
  7481. 4:41:41you need to know one very important
  7482. 4:41:43thing that NodeJS is going to
  7483. 4:41:45coordinates all these work but at the
  7484. 4:41:46end your operating system is going to
  7485. 4:41:48process actual system operations you are
  7486. 4:41:51getting what I'm talking about so these
  7487. 4:41:53all are very very important so if I try
  7488. 4:41:54to explain this one let's say so you
  7489. 4:41:56have your JS code right so this is your
  7490. 4:41:58JS code. Then you have your V8 engine,
  7491. 4:42:01right? And then you have your JavaScript
  7492. 4:42:04code. So your JS code,
  7493. 4:42:07then you have your NodeJS core APIs. So
  7494. 4:42:10do core APIs. You have your
  7495. 4:42:14you have your C++ bindings.
  7496. 4:42:18Then you have your LIV and then you have
  7497. 4:42:22your operating system. So this is how
  7498. 4:42:24it's going to work. Okay. So this is the
  7499. 4:42:26overall flow. Now this is very top level
  7500. 4:42:28but if you try if someone ask you okay
  7501. 4:42:30give me or let's say they will ask you
  7502. 4:42:32in the interview okay give me summary of
  7503. 4:42:34that how actually NodeJS internal flow
  7504. 4:42:36works. So just explain in this way that
  7505. 4:42:38first operating system is going to start
  7506. 4:42:40your NodeJS process right. So NodeJS is
  7507. 4:42:43going to initialize your V8 engine. So
  7508. 4:42:44it's going to initialize your V8 engine.
  7509. 4:42:47NodeJS also initialize your LEU and the
  7510. 4:42:49event loop. Okay. Now let's say you have
  7511. 4:42:52an entry file. I think we already know.
  7512. 4:42:53So entry file is let's say app.js,
  7513. 4:42:55right? So what NodeJS is going to do?
  7514. 4:42:56It's going to load your app.js file.
  7515. 4:42:58Right? Then V8 what it's going to do? V8
  7516. 4:43:02is going to parse and execute your
  7517. 4:43:03JavaScript. Right? V8 is going to parse
  7518. 4:43:06and execute your JavaScript. Then your
  7519. 4:43:08top level JavaScript is going to run on
  7520. 4:43:10your main thread. I already explained
  7521. 4:43:12this main thread, right? Normal
  7522. 4:43:13JavaScript is going to run on the main
  7523. 4:43:14thread. Once this is done, then let's
  7524. 4:43:17say you have some async operation. Maybe
  7525. 4:43:18let's say you're doing some API call. So
  7526. 4:43:20async operation will be registered or
  7527. 4:43:22delegated whatever it's going to do.
  7528. 4:43:24Then the very important point you have
  7529. 4:43:26something called event loop. Now I'm not
  7530. 4:43:28going to go into too much details into
  7531. 4:43:29event loop because it's very vast topic
  7532. 4:43:31and then probably I'm [clears throat]
  7533. 4:43:33going to cover this one in the part
  7534. 4:43:34three okay not this one but event loop
  7535. 4:43:37is going to continue until unless there
  7536. 4:43:38is active work okay and then let's say
  7537. 4:43:40you have some callbacks so comp all the
  7538. 4:43:42callbacks will be completed and those
  7539. 4:43:44call backs will be executed on your main
  7540. 4:43:46thread okay so main thread will be your
  7541. 4:43:48active and this process is going to exit
  7542. 4:43:51when there is no active work remains
  7543. 4:43:53means until unless there is active work
  7544. 4:43:55your main thread will remain active
  7545. 4:43:57Okay. So this will be your overall flow
  7546. 4:43:59or this is how your NodeJS internal
  7547. 4:44:01process works. So what are the things
  7548. 4:44:03you need to know right? Why I'm
  7549. 4:44:04repeating all of these? So first you
  7550. 4:44:05need to know the term called we have
  7551. 4:44:07something called VS sorry main JS
  7552. 4:44:08thread. We have something called V V8
  7553. 4:44:11engine. We have our NodeJS code APIs. We
  7554. 4:44:13have C++ binding. What this is going to
  7555. 4:44:15do? We have something called Leo and
  7556. 4:44:18then we have our operating system. So
  7557. 4:44:20with this now let's move on to the next
  7558. 4:44:22concept. So let's move to the next
  7559. 4:44:23concept and this will be so let's
  7560. 4:44:25understand V8 uh V8 engine in details.
  7561. 4:44:29Okay. So V8 engine MD. So I think we
  7562. 4:44:33already know the basics right that what
  7563. 4:44:34actually V8 engine does. So V8 will be
  7564. 4:44:37your JavaScript engine. So V8 uh is your
  7565. 4:44:40JavaScript engine that is used by used
  7566. 4:44:45by NodeJS. Correct? So we already know
  7567. 4:44:48the responsibility not KS it will be JS
  7568. 4:44:51already know the responsibility it's
  7569. 4:44:52going to parse your JavaScript it's
  7570. 4:44:54going to let's say sorry it's going to
  7571. 4:44:55parse your JavaScript so we are going to
  7572. 4:44:57go into much more details now in this
  7573. 4:44:59particular file and then we understood
  7574. 4:45:01in the previous section that it's going
  7575. 4:45:02to parse your JavaScript it's going to
  7576. 4:45:03execute your code it's going to handle
  7577. 4:45:05your event loop and all these things but
  7578. 4:45:07how actually this whole process works or
  7579. 4:45:10means what actually happens behind the
  7580. 4:45:12scenes so [snorts] first let's
  7581. 4:45:14understand that how V8 actually uh
  7582. 4:45:16execute the JavaScript. Now this is very
  7583. 4:45:18very interesting concept. Okay, what
  7584. 4:45:20this is going to do, right? So first of
  7585. 4:45:22all, it's going to parse your
  7586. 4:45:23JavaScript. Okay, so this is the first
  7587. 4:45:24thing once it will parse your
  7588. 4:45:26JavaScript, right? So what this is going
  7589. 4:45:28to do, right? This is going to create a
  7590. 4:45:29internal representation of your code. So
  7591. 4:45:32what I mean by this? So basically it's
  7592. 4:45:34going to produce something called byte
  7593. 4:45:36code. So it's going to produce byte
  7594. 4:45:38code. So it's going to produce byte code
  7595. 4:45:41and then it's going to execute the code.
  7596. 4:45:43So then it's going to execute the code
  7597. 4:45:45and then I'm just going to let's write
  7598. 4:45:47it here only. So first of all we know
  7599. 4:45:49that it's going to parse your JS. It's
  7600. 4:45:51going to create internal representation
  7601. 4:45:52of your code that what actually that
  7602. 4:45:54code is doing. It's going to produce the
  7603. 4:45:56bite code. It's going to execute the
  7604. 4:45:57code right and then as you already me
  7605. 4:46:00mentioned in the previous section also
  7606. 4:46:01sometime it's going to optimize your
  7607. 4:46:03frequent used code also. Then it's going
  7608. 4:46:06to manage your call stack. So all these
  7609. 4:46:08things it's going to do. This is not uh
  7610. 4:46:11let's keep this one here in the new
  7611. 4:46:13line. So it's going to manage your call
  7612. 4:46:14stack. It's also manage your hip memory
  7613. 4:46:18all these thing it's going to do right
  7614. 4:46:19and also garbage collection also it's
  7615. 4:46:21going to perform garbage collection.
  7616. 4:46:24So all these thing your V8 engine is
  7617. 4:46:26going to do. So now let's understand
  7618. 4:46:29here that what actually it is doing. So
  7619. 4:46:31first you have your JavaScript source
  7620. 4:46:33correct. So this is your JavaScript code
  7621. 4:46:35and then you have some you have
  7622. 4:46:37something called parser. So this is
  7623. 4:46:39where your V8 is going to parse your
  7624. 4:46:41JavaScript right. So that means see
  7625. 4:46:44first of all you need to know that what
  7626. 4:46:45actually mean by parse the code mean V
  7627. 4:46:49V8 engine needs to understand the source
  7628. 4:46:51code right that what actually you're
  7629. 4:46:53writing. So sometimes consider that you
  7630. 4:46:55have something like this you have con
  7631. 4:46:58total equal to price plus tax something
  7632. 4:47:00like that you have written. So V8 is
  7633. 4:47:02going to read that and it's going to
  7634. 4:47:04check the syntax. If the syntax is
  7635. 4:47:07invalid, so it's not going to run the
  7636. 4:47:09code, right? So for example, let's say
  7637. 4:47:11you written const total equals and then
  7638. 4:47:15you don't have anything on the right
  7639. 4:47:16side. So this is wrong. Correct? So that
  7640. 4:47:18means this person is going to check the
  7641. 4:47:20syntax and based on that it's going to
  7642. 4:47:22give a error that if the statement is
  7643. 4:47:24incomplete, right? After this it's going
  7644. 4:47:27to create something called abstract
  7645. 4:47:29syntax tree. Now you might be thinking
  7646. 4:47:31from where I've actually learned all of
  7647. 4:47:33these. So these all the things right
  7648. 4:47:35what you can do right I'll give you the
  7649. 4:47:37best of the best solution. So first
  7650. 4:47:39option will be you search for uh V8
  7651. 4:47:42engine internals or how V8 engine works
  7652. 4:47:44on Google not on chat GP and everything.
  7653. 4:47:46Okay. And then read some blogs. The
  7654. 4:47:49blogs I'm talking about the old blogs.
  7655. 4:47:51Old blogs means when this chat GP and
  7656. 4:47:53all these things uh was not there. Okay.
  7657. 4:47:55and then read some two three blogs and
  7658. 4:47:57try to understand and then [snorts] you
  7659. 4:47:59can ask for some AI tools and
  7660. 4:48:00everything. So this is why I usually
  7661. 4:48:02read multiple blogs and then I came to
  7662. 4:48:04know all these things and based on this
  7663. 4:48:05I actually created and try to simpl
  7664. 4:48:07simplify this particular flow. So what
  7665. 4:48:09this is going to do right after pursing
  7666. 4:48:11it's going to create a abstract syntax
  7667. 4:48:13tree and this we call as a insert. Okay.
  7668. 4:48:16So this is what it's going to do that
  7669. 4:48:18means this is going to represent that
  7670. 4:48:20what actually you have written in the
  7671. 4:48:21code. As simple as that what actually
  7672. 4:48:23you have written in the code means it's
  7673. 4:48:24going to understand your structure and
  7674. 4:48:26the meaning of the program. For example
  7675. 4:48:28let's have a cons total is equal to
  7676. 4:48:30price plus tax. So it's going to
  7677. 4:48:31understand okay you are creating a total
  7678. 4:48:32value from price and tax. So you're
  7679. 4:48:34adding both right. So here it's going to
  7680. 4:48:37understand okay I have a variable
  7681. 4:48:39declaration which is total uh I have a
  7682. 4:48:42addition so I'm adding both price and
  7683. 4:48:44tax so it's going to understand so this
  7684. 4:48:46particular part is going to understand
  7685. 4:48:47that code okay so now what this is going
  7686. 4:48:50to do right and also okay one very
  7687. 4:48:52important point when this is going to
  7688. 4:48:53check right so it actually going to
  7689. 4:48:55remove unnecessary formatting details
  7690. 4:48:57and everything okay for example let's
  7691. 4:48:58say you have spaces indentation all this
  7692. 4:49:01thing is going to remove once this is
  7693. 4:49:02done then it's going to create your bite
  7694. 4:49:04code okay so this will be your bite code
  7695. 4:49:06we also call as bite code and the
  7696. 4:49:09interpreter. Okay. So, V8 is going to
  7697. 4:49:11create the bite code. I already
  7698. 4:49:12mentioned it's going to produce the bite
  7699. 4:49:14code. Why? First of all, you need to
  7700. 4:49:16know what actually bite code. So, it's a
  7701. 4:49:18low-level or lower level internal format
  7702. 4:49:21and this is actually easier to
  7703. 4:49:23understand actually for V8 because at
  7704. 4:49:25the end V8 is going to execute your
  7705. 4:49:27code, right? So, that is the reason what
  7706. 4:49:28this is going to do. It's going to
  7707. 4:49:29actually produce a bite code. Once this
  7708. 4:49:32is done, then we have something called
  7709. 4:49:34interpreter. Okay? So interpreter and
  7710. 4:49:36then you have your optimize. I think
  7711. 4:49:37this part you can understand easily. So
  7712. 4:49:39optimize I mentioned this one right. So
  7713. 4:49:41sometime is going to optimize your
  7714. 4:49:42frequent written code. So it's going to
  7715. 4:49:44optimize your machine code and then at
  7716. 4:49:46the end it's going to execute the code.
  7717. 4:49:47But the main important part actually
  7718. 4:49:49these three line here. So first you have
  7719. 4:49:51a parser. What parser is going to do?
  7720. 4:49:53Parser is going to check your syntax.
  7721. 4:49:54That is very important. If you have a
  7722. 4:49:56wrong wrong syntax it's going to give
  7723. 4:49:58you error. Right? So then you have
  7724. 4:50:00something called abstract syntax tree or
  7725. 4:50:02a So this is going to do what I already
  7726. 4:50:05mentioned. So this is going to create or
  7727. 4:50:06this is going to represent the structure
  7728. 4:50:08or the meaning of your actual code means
  7729. 4:50:10this is going to understand let's have a
  7730. 4:50:12function okay this is a function this is
  7731. 4:50:14the name of the function this function
  7732. 4:50:15is adding two variables and it's going
  7733. 4:50:17to return the sum of these two
  7734. 4:50:18variables. So this is going to
  7735. 4:50:19understand that and based on that it's
  7736. 4:50:21going to produce a bite code and then
  7737. 4:50:23it's going to create uh sorry it's going
  7738. 4:50:25to execute your code. Okay. So this will
  7739. 4:50:28be the overall flow or this is how V8 is
  7740. 4:50:31going to execute your JavaScript uh code
  7741. 4:50:34or NodeJS code. Okay. So I think that's
  7742. 4:50:36all for this particular one. Now with
  7743. 4:50:38this now let's move on to the next one.
  7744. 4:50:39So let's move to the next one. I thought
  7745. 4:50:41that I'm not going to cover but let's
  7746. 4:50:43give you some idea on this particular
  7747. 4:50:44module. Okay. So what actually this
  7748. 4:50:46library does I'm doing TSM.
  7749. 4:50:52Okay. So first of all we already know
  7750. 4:50:54that that Leo
  7751. 4:50:56is a native library that is used by
  7752. 4:51:00NodeJS right so this we already know. So
  7753. 4:51:03first what is the main purpose? So the
  7754. 4:51:06main purpose will be so you have to
  7755. 4:51:08understand this one. So this is actually
  7756. 4:51:09going to help node to handle
  7757. 4:51:12asynchronous or async operations right
  7758. 4:51:15across different OS system also this is
  7759. 4:51:17very important. So when I what I mean by
  7760. 4:51:19this so first let's write it. So it's
  7761. 4:51:21going to help. So it's going to help
  7762. 4:51:23NodeJS to handle
  7763. 4:51:25a sync operations
  7764. 4:51:29across
  7765. 4:51:31different OS
  7766. 4:51:33system. Okay. So here this is going to
  7767. 4:51:36provide first of all in event loop
  7768. 4:51:38worker thread pool
  7769. 4:51:42in your timers right uh async io
  7770. 4:51:46operations.
  7771. 4:51:49So now let's understand that why
  7772. 4:51:51actually NodeJS need this particular
  7773. 4:51:53library. Now what we understood from the
  7774. 4:51:56previous section that what V8 actually
  7775. 4:51:58going to do. So V8 is going to execute
  7776. 4:52:00JavaScript right. So it's going to
  7777. 4:52:02execute a JavaScript code. It's going to
  7778. 4:52:03create you it has some parser. It's
  7779. 4:52:06going to parse your code. It's going to
  7780. 4:52:08create a right as we have already
  7781. 4:52:10covered that one. Then based on that
  7782. 4:52:12it's going to produce produce the bite
  7783. 4:52:13code and based on that it's going to run
  7784. 4:52:15your code. But V8 actually not going to
  7785. 4:52:18provide all these operations. So it's
  7786. 4:52:20not going to provide. So V8 does not
  7787. 4:52:23provide
  7788. 4:52:25file system operations.
  7789. 4:52:29Then you have network
  7790. 4:52:31socket handling all these things
  7791. 4:52:36right and then also
  7792. 4:52:40uh general event loop for node APIs,
  7793. 4:52:43NodeJS APIs, right? So that means in
  7794. 4:52:45this case NodeJS needs something else to
  7795. 4:52:49handle all this. So NodeJS needs
  7796. 4:52:50something else. Correct? So this means
  7797. 4:52:53NodeJS need a separate layer or NodeJS
  7798. 4:52:57needs a another layer that will handle
  7799. 4:53:01uh that will handle not handle but that
  7800. 4:53:03will coordinate
  7801. 4:53:05coordinate
  7802. 4:53:07this runtime because all these are
  7803. 4:53:09runtime features right. all these
  7804. 4:53:10runtime features right and all these
  7805. 4:53:13features are provided by this leuof
  7806. 4:53:15native library okay so this is what it's
  7807. 4:53:17going to do if we try to explain uh
  7808. 4:53:19understand this one so before we'll
  7809. 4:53:21explain this so let's see this one so
  7810. 4:53:22you have something like this you have
  7811. 4:53:24your NodeJS API so I'll do NodeJS APIs
  7812. 4:53:27right and then you have your JavaScript
  7813. 4:53:30code you have NodeJS APIs you have your
  7814. 4:53:32uh then you have your native binding
  7815. 4:53:35then you have your leave and then at the
  7816. 4:53:37end you you're going to have your OS
  7817. 4:53:39right so This is how it's going to do.
  7818. 4:53:41So first of all let's see this
  7819. 4:53:43particular flow and then we understand
  7820. 4:53:44the responsibilities. So you see that we
  7821. 4:53:47have JavaScript uh then you have your
  7822. 4:53:49NodeJS code APIs the code AP we have
  7823. 4:53:51file system HTTP timers and all these
  7824. 4:53:53things then it will go through your CS C
  7825. 4:53:55C++ binding I think this is the same
  7826. 4:53:57flow we covered then we have your leave
  7827. 4:53:59of uh library right so it's going to
  7828. 4:54:01provide all this thread pool event loop
  7829. 4:54:03and everything and at the end it's going
  7830. 4:54:05to interact with your operating system.
  7831. 4:54:07So this is what it's going to do and
  7832. 4:54:08what these uh main responsibilities will
  7833. 4:54:10be. So first of all event loop right? So
  7834. 4:54:12what actually event loop is going to do?
  7835. 4:54:14I'll give you basic overview. So what
  7836. 4:54:16event loop is going to check for? See
  7837. 4:54:17event loop is going to check for a
  7838. 4:54:18couple of things. First it's going to
  7839. 4:54:20check for your completed IO operations.
  7840. 4:54:22It's going to check for a timer. So
  7841. 4:54:24let's say you are using some timers and
  7842. 4:54:25it's going to check whether there is any
  7843. 4:54:27timer which is in ready state. It also
  7844. 4:54:30going to check for your pending
  7845. 4:54:31callbacks and also it socket activity.
  7846. 4:54:33Okay. So socket means let's say you're
  7847. 4:54:35using sockets. So event loop is also
  7848. 4:54:37going to check the activity
  7849. 4:54:38[clears throat] of that particular
  7850. 4:54:38socket. So now I'm just going to write
  7851. 4:54:40here. So what event loop is going to
  7852. 4:54:42check? So it's going to check for your
  7853. 4:54:44completed IO operations.
  7854. 4:54:48It's also going to check for your
  7855. 4:54:49timers. So if you have any timers and if
  7856. 4:54:52some timers
  7857. 4:54:56timers are in ready state
  7858. 4:55:00then you also want to check for your
  7859. 4:55:01pending callbacks. Also it's going to
  7860. 4:55:04check for the socket
  7861. 4:55:06activity. So this is what event loop is
  7862. 4:55:09going to check or this will be one of
  7863. 4:55:12the main responsibility of libu which
  7864. 4:55:14will provide this particular feature.
  7865. 4:55:17Then second will be we have something
  7866. 4:55:18called thread pool. Right? So we have
  7867. 4:55:20something called thread pool. Why
  7868. 4:55:22actually we need thread pool or why liu
  7869. 4:55:24actually provide this particular thing.
  7870. 4:55:26So first of all you need to know that
  7871. 4:55:27libu provides
  7872. 4:55:30provides a shared worker thread pool
  7873. 4:55:35and why actually you need this. Now if
  7874. 4:55:37you go here this particular thing I'm
  7875. 4:55:39talking about this thread pool or this
  7876. 4:55:41pool is is used by those operations that
  7877. 4:55:45cannot be handled efficiently. Now what
  7878. 4:55:47I mean by this? So that means this pool
  7879. 4:55:49that we have is used by only those
  7880. 4:55:54operations
  7881. 4:55:56that cannot be handled efficiently. Now
  7882. 4:56:00the best example of this will be let's
  7883. 4:56:02say you have some project you're
  7884. 4:56:04building some project where you have
  7885. 4:56:05multiple or many file system operations
  7886. 4:56:09many file system operations. Okay then
  7887. 4:56:12let's say you are doing some
  7888. 4:56:14cryptographic operations. So this is
  7889. 4:56:15also where it's needed cryptographic
  7890. 4:56:17operations and then also let's say
  7891. 4:56:19you're using some compression related
  7892. 4:56:21work.
  7893. 4:56:24Okay. So this is where we need this
  7894. 4:56:27thread pool and then also you have
  7895. 4:56:29something called timers right. So timer
  7896. 4:56:30is nothing but so what li is going to do
  7897. 4:56:32it's going to help node to track all
  7898. 4:56:34those timers right and then very very
  7899. 4:56:37important it's going to determine when a
  7900. 4:56:39particular timer is eligible to execute.
  7901. 4:56:41Okay. So that means let's say you have a
  7902. 4:56:43timer with a delay. What will happen?
  7903. 4:56:45Right? It doesn't going to block your
  7904. 4:56:48JavaScript main thread. Okay. So I think
  7905. 4:56:49this part we already covered what this
  7906. 4:56:52runtime is going to do, right? Runtime
  7907. 4:56:54is going to record that particular timer
  7908. 4:56:56and then it's going to continue
  7909. 4:56:57processing the other task. Okay. So this
  7910. 4:57:00is actually very important concept.
  7911. 4:57:01Okay. So let's say you have a timer. So
  7912. 4:57:03li is going to help lies
  7913. 4:57:06to track those timers.
  7914. 4:57:10And then this is important part. So this
  7915. 4:57:12is the important here. So it's going to
  7916. 4:57:14determine when the timer is become
  7917. 4:57:19eligible to run or no not run execute.
  7918. 4:57:23Right? So what will happen right? So
  7919. 4:57:24let's say you have a timer with 5second
  7920. 4:57:26delay. So a 5second delay. So it not
  7921. 4:57:29going to or this doesn't mean so it
  7922. 4:57:32doesn't mean the javascript is going to
  7923. 4:57:35slip on the main thread. It's not going
  7924. 4:57:37to do that. So it's going to do what?
  7925. 4:57:38Right. So this uh timer whatever you
  7926. 4:57:41have runtime is going to runtime is
  7927. 4:57:45going to record this timer that okay I
  7928. 4:57:46have a timer. So runtime is record the
  7929. 4:57:49timer and then it's going to continue
  7930. 4:57:51working other task. So continue
  7931. 4:57:53processing other task and then once this
  7932. 4:57:56timer is become eligible to execute so
  7933. 4:57:58that time li is going to determine and
  7934. 4:58:00based on that it's going to run the
  7935. 4:58:01timer on the main thread. Okay. So this
  7936. 4:58:04is what it's going to do and also li
  7937. 4:58:06also communicate with your operating
  7938. 4:58:08system APIs right. So this part let's
  7939. 4:58:10say file related operations handle uh
  7940. 4:58:12process handling timing as synchronous
  7941. 4:58:14notification and all these things. So
  7942. 4:58:16this is why we need this particular
  7943. 4:58:18module which we call as leave. So with
  7944. 4:58:21this now let's move on to the next
  7945. 4:58:22concept. Okay. So let's move to the next
  7946. 4:58:24one. Uh and these will be so let's
  7947. 4:58:26understand what is uh blocking
  7948. 4:58:31and non-blocking.
  7949. 4:58:34Now you might be thinking I'll tell you
  7950. 4:58:36why actually I'm covering all these uh
  7951. 4:58:37this section will be very simple. So
  7952. 4:58:39this section is going to do what right?
  7953. 4:58:40So whatever you have covered in this
  7954. 4:58:42four files it's going to set your base
  7955. 4:58:45for the part three because what will be
  7956. 4:58:48the part three right again part three
  7957. 4:58:50I'm going to only upload if I get good
  7958. 4:58:52response from part one and two or else
  7959. 4:58:54there is no point okay so I just wanted
  7960. 4:58:55to make this point very very very clear
  7961. 4:58:58so part three will be only NodeJS pure
  7962. 4:59:00concept we are going to cover so there
  7963. 4:59:01will not be any APIs and everything okay
  7964. 4:59:03so only NodeJS internal code modules
  7965. 4:59:06worker threads cluster module uh then
  7966. 4:59:09you have your child processes and all
  7967. 4:59:10these things. Okay. So this is the
  7968. 4:59:11reason we are covering very important uh
  7969. 4:59:13section so that you you will be aware of
  7970. 4:59:15the time. Okay. We have something called
  7971. 4:59:17V8 engine. We have something called
  7972. 4:59:18leave. What is blocking non-blocking?
  7973. 4:59:20For example, in the part three uh the
  7974. 4:59:23event loop section only only will take 1
  7975. 4:59:25hour. Okay. So this is very huge. So
  7976. 4:59:27that is the reason we are doing this
  7977. 4:59:29particular section. So first you need to
  7978. 4:59:31know what actually blocking means and
  7979. 4:59:32what is non-blocking. First of all, you
  7980. 4:59:34need to know blocking means the main
  7981. 4:59:36JavaScript thread that you have, it
  7982. 4:59:39can't continue with other JavaScript
  7983. 4:59:41task until unless your current operation
  7984. 4:59:43that whatever you're doing that
  7985. 4:59:45finishes. So that means it needs to wait
  7986. 4:59:47until unless until the current work
  7987. 4:59:49finishes work finishes. So that will be
  7988. 4:59:52your blocking, right? So it's actually
  7989. 4:59:53block your main thread. Okay. On the
  7990. 4:59:56other side, what will be your
  7991. 4:59:57non-blocking? Non-blocking means
  7992. 4:59:59JavaScript can start operation and then
  7993. 5:00:02it's going to retain or it's going to
  7994. 5:00:04regain the control before the final
  7995. 5:00:06result is available. Okay. So now let's
  7996. 5:00:09see some of the synchronous operation. I
  7997. 5:00:10think this part we've already covered in
  7998. 5:00:12the core module. So synchron synchronous
  7999. 5:00:15operation will be these operation must
  8000. 5:00:16finish before JavaScript continue. So
  8001. 5:00:18you have something called remember we
  8002. 5:00:20have read file sync right? This is we
  8003. 5:00:23have done. So in that case what will
  8004. 5:00:24happen right? So JavaScript let's say
  8005. 5:00:26consider this only JavaScript is going
  8006. 5:00:28to start the operations your main thread
  8007. 5:00:30is going to wait this particular
  8008. 5:00:32operation finishes it's going to return
  8009. 5:00:34the result and then JavaScript is going
  8010. 5:00:36to continue with the other task. So now
  8011. 5:00:38let's say if I go here right so you have
  8012. 5:00:39a blocking it's going to start the
  8013. 5:00:41operation your main thread is going to
  8014. 5:00:43wait until unless let's say this read
  8015. 5:00:45file sync this particular uh logic
  8016. 5:00:47whatever you've written this code will
  8017. 5:00:49execute once this is done then it's
  8018. 5:00:51going to complete that that particular
  8019. 5:00:53operation and then JavaScript is going
  8020. 5:00:54to continue with the other task and this
  8021. 5:00:56is where it's going to block this
  8022. 5:00:58operation until unless your current uh
  8023. 5:01:00task is completed right on the other
  8024. 5:01:03side we have a synchronous API
  8025. 5:01:06asynchronous API so we have read file.
  8026. 5:01:07Now this is this is async operation. So
  8027. 5:01:10in this case what will happen right? So
  8028. 5:01:11very very simple way your work is
  8029. 5:01:14delegated right? So that means
  8030. 5:01:16JavaScript can continue with other task
  8031. 5:01:18right once it will complete the other
  8032. 5:01:20task. Let's say you have a call back and
  8033. 5:01:22that call back becomes ready and then
  8034. 5:01:24call back is going to run on the main
  8035. 5:01:25thread. So it's something like this. So
  8036. 5:01:27you have your start operation and this
  8037. 5:01:29is the delegate work means this is
  8038. 5:01:31non-blocking means your non main thread
  8039. 5:01:34is continue doing other task at the same
  8040. 5:01:36time and then it's going to once let's
  8041. 5:01:38say you're using some callback right
  8042. 5:01:39your callback becomes ready. So in that
  8043. 5:01:41case this particular operations
  8044. 5:01:43completed and then your callbacks
  8045. 5:01:45becomes ready and then it's going to run
  8046. 5:01:46the call back on the main thread. So
  8047. 5:01:48that mean it's not going to block your
  8048. 5:01:50main thread. So this will be the very
  8049. 5:01:51basic definition of non-blocking and the
  8050. 5:01:54blocking. Okay, these can be interview
  8051. 5:01:56questions also. Why? Because they can
  8052. 5:01:58give you okay, you have learned file
  8053. 5:02:00system. Give me some proper example or
  8054. 5:02:03proper method to differentiate between
  8055. 5:02:06blocking and non-blocking operation.
  8056. 5:02:08Okay, so this is the reason I wanted to
  8057. 5:02:09me uh mention this one. [snorts] So I
  8058. 5:02:12think this will be simpler one and I
  8059. 5:02:14think with this we have covered almost
  8060. 5:02:16four things I wanted to cover in this
  8061. 5:02:18particular. Let's do one thing in the
  8062. 5:02:20next section. I'm going to give a very
  8063. 5:02:21basic overview of the cluster module so
  8064. 5:02:24that you'll be aware what actually that
  8065. 5:02:25module is and try to cover that part
  8066. 5:02:28also and then uh we will start with our
  8067. 5:02:32crowd implementation which will be
  8068. 5:02:33postgress section. So let's move on to
  8069. 5:02:35the next one. Okay. So let's create the
  8070. 5:02:37last file and this I'll give you some
  8071. 5:02:39very basic idea of cluster module that
  8072. 5:02:41what actually this module is. Okay. All
  8073. 5:02:43right. So let's uh start understanding
  8074. 5:02:45first that why actually you need this
  8075. 5:02:47cluster module. So first you know that
  8076. 5:02:49normal NodeJS process right so you have
  8077. 5:02:51normal NodeJS process and this will
  8078. 5:02:53always have one main JavaScript thread
  8079. 5:02:55correct so it will have one main JS
  8080. 5:02:58thread so this is the first thing this
  8081. 5:03:01particular process right so this normal
  8082. 5:03:02NodeJS process that you have it can
  8083. 5:03:04handle multiple or many concurrent IO
  8084. 5:03:07operation but at the end this is going
  8085. 5:03:09to execute on one main thread only right
  8086. 5:03:12so now let's say you're running this
  8087. 5:03:14NodeJS code on your machine and that
  8088. 5:03:15machine has multiple CPU nodes. So in
  8089. 5:03:18that case one node just process cannot
  8090. 5:03:20fully use all of them. Correct? So in
  8091. 5:03:22that case you have something called
  8092. 5:03:23cluster module. So what this cluster
  8093. 5:03:26module is going to do this is actually
  8094. 5:03:28going to solve this particular problem
  8095. 5:03:30by starting multiple NodeJS worker
  8096. 5:03:33processes. So by starting multiple
  8097. 5:03:37NodeJS worker processes. So what do we
  8098. 5:03:40mean by this? So first of all you need
  8099. 5:03:42to know each and every worker process
  8100. 5:03:44that we are going to have is going to
  8101. 5:03:46have its own its own NodeJS runtime is
  8102. 5:03:51going to have its own V8 engine.
  8103. 5:03:55It's going to have its own event loop
  8104. 5:03:59and also very very interesting and very
  8105. 5:04:01important it's also going to have its
  8106. 5:04:03own main JS thread right and also its
  8107. 5:04:06own memory. So all these things each and
  8108. 5:04:09every worker process is going to have.
  8109. 5:04:11So what this is going to allow this is
  8110. 5:04:13going to allow several NodeJS processes
  8111. 5:04:15to run in parallel you're getting
  8112. 5:04:16because we have multiple worker process
  8113. 5:04:18right. So uh this is how it's going to
  8114. 5:04:20work. But how actually this is
  8115. 5:04:21happening. So let's see. So you have
  8116. 5:04:23your incoming request. The very key
  8117. 5:04:25point you need to know this cluster
  8118. 5:04:27architecture whatever you seeing this
  8119. 5:04:29will have at least one primary process.
  8120. 5:04:32So it has to be there and then it's
  8121. 5:04:33going to have your multiple worker
  8122. 5:04:35processes. So you have worker process
  8123. 5:04:37one, process two, process three and
  8124. 5:04:38process four. Okay. Now what this
  8125. 5:04:41primary process is going to do? This
  8126. 5:04:43primary process is going to manage all
  8127. 5:04:45these worker processes. Okay. So this is
  8128. 5:04:48going to manage all these worker
  8129. 5:04:49processes. And what will be the
  8130. 5:04:51responsibility of this primary process?
  8131. 5:04:52So first it's going to start all the
  8132. 5:04:54worker processes. It is going to watch
  8133. 5:04:56if all the worker processes are in the
  8134. 5:04:58correct health or not. Health means if
  8135. 5:05:00let's say something is down or it's
  8136. 5:05:02something is failing. So it's going to
  8137. 5:05:03check. It also going to restart the
  8138. 5:05:05failed worker. So for example, let's say
  8139. 5:05:07worker two is not working. It is in
  8140. 5:05:09failed state. So in that case, it's
  8141. 5:05:11going to restart this worker uh this
  8142. 5:05:13particular worker process. Okay. Also,
  8143. 5:05:15it's going to coordinate the process
  8144. 5:05:17life cycle. So this is how it's going to
  8145. 5:05:19work. So that means you have your
  8146. 5:05:21primary process and it's going to have
  8147. 5:05:22your worker process one, process two,
  8148. 5:05:24process three and process four like
  8149. 5:05:26this. Now these workers that whatever
  8150. 5:05:29you're seeing, right? So we have our
  8151. 5:05:30four workers. each and every worker can
  8152. 5:05:34share the same server port. So what I
  8153. 5:05:36mean by this? So let's say I have port
  8154. 5:05:3830,00 right? So in this port 30,000 I
  8155. 5:05:41can run this worker one, worker two,
  8156. 5:05:43worker three and worker four. Okay,
  8157. 5:05:45you're getting and then we have another
  8158. 5:05:48uh concept called request distribution.
  8159. 5:05:51Now what I mean by request distribution
  8160. 5:05:53that means whatever incoming request
  8161. 5:05:54that you are getting that will be
  8162. 5:05:56distributed among these four workers. So
  8163. 5:05:59that means let's say you have a four
  8164. 5:06:00request coming request A, request B,
  8165. 5:06:02request C, request D. So request A will
  8166. 5:06:04be handled by worker process one.
  8167. 5:06:06Request B will be handled by worker
  8168. 5:06:08process two, three and four and so on.
  8169. 5:06:10Right? Now here you need to know one
  8170. 5:06:11thing because each and every workers are
  8171. 5:06:13separate process. Right? In that case
  8172. 5:06:15what let's say you have two requests
  8173. 5:06:17request and request B. These request and
  8174. 5:06:19request B can execute JavaScript at the
  8175. 5:06:22same time on different CPU codes. And
  8176. 5:06:24this is the advantage of using this
  8177. 5:06:26cluster module. Okay. So I hope this is
  8178. 5:06:28clear. Now this will be enough. I wanted
  8179. 5:06:30to give you just very basic idea. These
  8180. 5:06:32are very vast concept right? So if you
  8181. 5:06:34go to NodeJS official documentation and
  8182. 5:06:36just search for cluster module child
  8183. 5:06:38processes worker threads you're going to
  8184. 5:06:40get huge huge so many things. So that is
  8185. 5:06:43the reason I just wanted to cover these
  8186. 5:06:45five very very important concept. NodeJS
  8187. 5:06:47internal V8 engine leave blocking
  8188. 5:06:49non-blocking and cluster model. With
  8189. 5:06:51this now let's move to the next one. So
  8190. 5:06:53next on next part will be uh so we are
  8191. 5:06:56going to cover posgress. So now this is
  8192. 5:06:58actually mandatory section. So next
  8193. 5:07:00section you need to do before we are
  8194. 5:07:02going to start cred. Okay. So with this
  8195. 5:07:04now let's move on to the next one. Hey
  8196. 5:07:06everyone welcome back. So in this
  8197. 5:07:07particular video you are going to learn
  8198. 5:07:09each and everything that you need to
  8199. 5:07:10know to start your journey with
  8200. 5:07:11posgress. Now before I'm going to
  8201. 5:07:13discuss the curriculum one humble
  8202. 5:07:15request that please like this video
  8203. 5:07:17comment and also please share and if
  8204. 5:07:19possible if you can hype this video it
  8205. 5:07:20will be very helpful. See I've given a
  8206. 5:07:22lot of effort to create these kind of
  8207. 5:07:24free videos for all of you so that you
  8208. 5:07:26don't have to buy a paid courses. Okay.
  8209. 5:07:28So this video is a prerequisite of my
  8210. 5:07:31upcoming NodeJS masterclass video. So I
  8211. 5:07:33just wanted to mention in case if you're
  8212. 5:07:35not aware. So I will be uploading a 20
  8213. 5:07:38hours free NodeJS course on next month.
  8214. 5:07:41So in that particular course we are
  8215. 5:07:42going to cover Postgress. So if you
  8216. 5:07:44complete this particular video it will
  8217. 5:07:46be very very helpful. So this video will
  8218. 5:07:48be divided into three sections. So we're
  8219. 5:07:50going to start with foundations where we
  8220. 5:07:52we are going to cover the concept of how
  8221. 5:07:53you can create your database schemas,
  8222. 5:07:55tables, data types, constant and the
  8223. 5:07:58concept of primary keys. Then we'll move
  8224. 5:08:00to the second section which will be very
  8225. 5:08:02very helpful. So we're going to
  8226. 5:08:03understand queries and data operations
  8227. 5:08:05like how you're going to insert rows,
  8228. 5:08:08select specific columns, basic
  8229. 5:08:10filtering, the concept of like, I like
  8230. 5:08:13patterns, null, not null, order by,
  8231. 5:08:16limit, offset and paginance. how we are
  8232. 5:08:18going to update single row, how you can
  8233. 5:08:20update multiple rows and at the end we
  8234. 5:08:22are going to finish with returning after
  8235. 5:08:24insert concept and then we're going to
  8236. 5:08:26move to the last section which will be
  8237. 5:08:28relationship and advanced SQL. So you're
  8238. 5:08:30going to understand the concept of
  8239. 5:08:32foreign keys one to many relationship
  8240. 5:08:34inner join left join many to many
  8241. 5:08:36relationship table allises aggregate
  8242. 5:08:38functions group by count distinct sub
  8243. 5:08:42queries indexes and at the end we are
  8244. 5:08:44going to finish finish this particular
  8245. 5:08:45video with transactions I'm pretty much
  8246. 5:08:48sure that uh if you complete this video
  8247. 5:08:50you don't have to buy any paid courses
  8248. 5:08:52if you're a beginner this will be a
  8249. 5:08:54perfect start I've added the source code
  8250. 5:08:56that is completely free in the
  8251. 5:08:57description you can download from my
  8252. 5:08:59website. So with that now let's get
  8253. 5:09:01started and good luck. All right
  8254. 5:09:03everyone. So now we'll get started. So
  8255. 5:09:05first of all I wanted to mention I think
  8256. 5:09:06I already mentioned this one in the
  8257. 5:09:08intro that this is a practical course.
  8258. 5:09:10So what I mean by this now if you have
  8259. 5:09:11already followed my TypeScript course
  8260. 5:09:13that I've uploaded maybe 2 3 months
  8261. 5:09:15back. So there what I've done right? So
  8262. 5:09:18I will uh create individual file for
  8263. 5:09:22individual concepts and then by writing
  8264. 5:09:24the commands and the ASQL queries you
  8265. 5:09:27are going to learn all those concepts.
  8266. 5:09:28Okay. Now this is actually very very
  8267. 5:09:30important because I just wanted to make
  8268. 5:09:32these things very very clear that this
  8269. 5:09:33is not only a conceptual course. Okay.
  8270. 5:09:36For example, let's say I'm just going to
  8271. 5:09:37spend time on what is poss 10 minutes.
  8272. 5:09:40It's not going to work like that. Okay.
  8273. 5:09:43So this is a completely practical course
  8274. 5:09:45for every uh concepts and commands we
  8275. 5:09:48are going to create individual file and
  8276. 5:09:49then we are going to run those file and
  8277. 5:09:51we'll understand each and everything.
  8278. 5:09:53Now second thing I wanted to mention
  8279. 5:09:55because I'm using Mac here. So I'm going
  8280. 5:09:57to download this one for Mac. In case
  8281. 5:09:59you're using Windows, maybe most of you
  8282. 5:10:01are going to use Windows. What you can
  8283. 5:10:03do right, you can use uh sorry, watch
  8284. 5:10:05these two videos. Both the videos are
  8285. 5:10:07very good. Maybe you can watch the
  8286. 5:10:09second one, right? Second one is the uh
  8287. 5:10:112025 update. So follow this exact one.
  8288. 5:10:14Uh this is going to show you that each
  8289. 5:10:17and everything that you need to install
  8290. 5:10:19and set up posgress. So for this one, we
  8291. 5:10:21are going to use PG admin. Right? So
  8292. 5:10:23here first of all what you're going to
  8293. 5:10:24do right uh you come to this particular
  8294. 5:10:27uh URL from here right and then whatever
  8295. 5:10:31you're using based on that you can just
  8296. 5:10:33download it. So I'm just going to click
  8297. 5:10:34here
  8298. 5:10:36and then it's going to download. So
  8299. 5:10:39let's wait for some time. Okay let's
  8300. 5:10:42open this.
  8301. 5:10:45Let's click here open.
  8302. 5:10:49And here I have to give my password.
  8303. 5:10:54Okay, click on next. So this is where
  8304. 5:10:57it's going to install. Next. So we are
  8305. 5:11:00going to install all of these. So we
  8306. 5:11:01have Postgress, SQL server, PG admin 4,
  8307. 5:11:04stack builder and the command line
  8308. 5:11:05tools. Okay. So I'll do next. Next. And
  8309. 5:11:09this is actually very very important.
  8310. 5:11:11Why? This is where you need to set your
  8311. 5:11:14password. Now try to give the password
  8312. 5:11:16that you will be able to uh like
  8313. 5:11:18recollect. So for this one I'm going to
  8314. 5:11:20use admin. Okay. So I'm going to do
  8315. 5:11:22admin. And here also I'm going to type
  8316. 5:11:25admin.
  8317. 5:11:26Next. This is the port uh that posgress
  8318. 5:11:29is going to listen. So just keep it like
  8319. 5:11:32this. Go to next. Uh this is also I'm
  8320. 5:11:34going to click uh next. And this will be
  8321. 5:11:36all your settings that we are going to
  8322. 5:11:38use for our installation. Do next. And
  8323. 5:11:41now it's going to install. Okay.
  8324. 5:11:45All right. So, I think this is done. So,
  8325. 5:11:47I'm just going to uncheck this one and
  8326. 5:11:48I'm going to do finish. Okay. And this
  8327. 5:11:51is completed. But now what you're going
  8328. 5:11:52to do, right? So, I'm just going to
  8329. 5:11:53close this. And let's see if I just
  8330. 5:11:55search here PG admin. Okay. And you'll
  8331. 5:11:58see that you're not going to get it so
  8332. 5:12:00directly. Why? Because first of all, you
  8333. 5:12:02have to go to your application in case
  8334. 5:12:04you're using uh Mac. And then here you
  8335. 5:12:08are going to get uh Postgress SQL 18. So
  8336. 5:12:11click here and then from here you'll be
  8337. 5:12:13able to open this. So just open this PG
  8338. 5:12:16admin 4.
  8339. 5:12:18All right. So now first of all we need
  8340. 5:12:19to give the password. Okay. So I'm just
  8341. 5:12:21going to give my password here.
  8342. 5:12:25Okay. I'll do always allow. And now what
  8343. 5:12:28we're going to do right. So you can see
  8344. 5:12:29that we have the servers. So here first
  8345. 5:12:32of all we need to connect. So here and
  8346. 5:12:35this is where you have to give your
  8347. 5:12:36password that you have given. So in my
  8348. 5:12:38case I've given what admin. So I'm just
  8349. 5:12:40going to give this one admin and then
  8350. 5:12:42I'm do save password and I'll do okay.
  8351. 5:12:46And now you see that this will be now
  8352. 5:12:47connected. Okay. And you can just open
  8353. 5:12:49this. And this is where you are going to
  8354. 5:12:53see all the things visually. Okay. But
  8355. 5:12:57this is what I will suggest not to focus
  8356. 5:13:00too much at the beginning. Why? The
  8357. 5:13:03reason is when you have something that
  8358. 5:13:05you can learn visually, you will be able
  8359. 5:13:07to understand once you know the uh
  8360. 5:13:10concepts and the basics very very
  8361. 5:13:12clearly. So what I mean by this? So here
  8362. 5:13:14you'll see that if I go to now
  8363. 5:13:16databases, you'll be see you are going
  8364. 5:13:18to see I think this is little slow.
  8365. 5:13:20You'll see that we have one uh default
  8366. 5:13:22database here. Now what you can do right
  8367. 5:13:24you'll be able to create database from
  8368. 5:13:26here. Let's say if I do right click this
  8369. 5:13:29is very slow. Okay. So you can do create
  8370. 5:13:31database and all those things. What we
  8371. 5:13:33are going to do right each and
  8372. 5:13:35everything that how we are going to
  8373. 5:13:37create a table, how we are going to
  8374. 5:13:39insert data, how we are going to read
  8375. 5:13:41our data, how we are going to update our
  8376. 5:13:43data, all those things via command line.
  8377. 5:13:46So for this one we have something called
  8378. 5:13:48psql. Okay. So why I'm actually focusing
  8379. 5:13:51too much on this particular part because
  8380. 5:13:53here if I just focus too much on
  8381. 5:13:55learning all those things definitely we
  8382. 5:13:57can also do that. But why I'm suggesting
  8383. 5:14:00you if you're a complete beginner that
  8384. 5:14:02try to master command line things a much
  8385. 5:14:05better way. So what I'm telling let's
  8386. 5:14:07say you are working in a particular
  8387. 5:14:09project where they are using posgress
  8388. 5:14:11and someone ask you okay give me this
  8389. 5:14:13particular data. So now you don't want
  8390. 5:14:15to go to your DB so you can go to your
  8391. 5:14:17DB and then you can go to your tables
  8392. 5:14:19you can go to rows and you can see the
  8393. 5:14:20data that is fine. But if you are a
  8394. 5:14:22master in Postgress SQL, what you can
  8395. 5:14:24do? You can just simply run a particular
  8396. 5:14:26query and you'll be able to get all the
  8397. 5:14:27data. You're getting you're getting what
  8398. 5:14:29I mean. So this is where you have to
  8399. 5:14:31focus. So these things we are going to
  8400. 5:14:33discuss later. For now, you have to make
  8401. 5:14:35your posgress foundation very very
  8402. 5:14:38strong. And this is exactly how I have
  8403. 5:14:40learned the same thing that how how I'm
  8404. 5:14:42teaching here. You believe me or not?
  8405. 5:14:44Okay. So what we are going to do right
  8406. 5:14:46first of all what we need to do right, I
  8407. 5:14:48already created one blank folder here.
  8408. 5:14:50You can see that I don't have anything
  8409. 5:14:51else. Now we are going to follow a
  8410. 5:14:54certain process and the folder structure
  8411. 5:14:56so that sequentially you can learn all
  8412. 5:14:58those concepts line by line. So what I
  8413. 5:15:01mean by this? So first of all what
  8414. 5:15:02you're going to do right I'm going to
  8415. 5:15:04create two folder here. So I will
  8416. 5:15:06suggest that you also follow the exact
  8417. 5:15:08same process. So one will be part one
  8418. 5:15:10because I've given lot of uh time uh
  8419. 5:15:12deciding all those things right. So
  8420. 5:15:14don't think this is like very random
  8421. 5:15:15that I'm creating. I I will give lot of
  8422. 5:15:17time creating all those curriculum in
  8423. 5:15:19what sequence we are going to learn so
  8424. 5:15:21that it will be easy for you to
  8425. 5:15:22understand. So here I'm going to give
  8426. 5:15:24part one and then there will be part
  8427. 5:15:26two. Okay. So part one we are going to
  8428. 5:15:29learn very very basic things. For
  8429. 5:15:30example, how you can create database,
  8430. 5:15:32how you're going to create schema, how
  8431. 5:15:34you can create your table, what are the
  8432. 5:15:36data types, what are the constants,
  8433. 5:15:38primary keys, all those things. In part
  8434. 5:15:40two we are going to go into little bit
  8435. 5:15:42advanced level. Okay. So you're going to
  8436. 5:15:44learn all the SQL uh concepts for
  8437. 5:15:47example how you can insert a single row
  8438. 5:15:49how you're going to insert multiple rows
  8439. 5:15:51okay you are getting right and then we
  8440. 5:15:53are going to have part three but that we
  8441. 5:15:54are going to do later. So now inside
  8442. 5:15:56this part one what we are going to do we
  8443. 5:15:59will be creating each and every SQL file
  8444. 5:16:01with a sequence. So what I mean by this
  8445. 5:16:03I'm just going to do 01 and then I'm
  8446. 5:16:05going to do first database or DB or
  8447. 5:16:08something. So that mean this will be
  8448. 5:16:09your initial thing that you need to
  8449. 5:16:11understand. I hope this is clear. So
  8450. 5:16:13this will be our folder structure that
  8451. 5:16:14we are going to follow. Now before doing
  8452. 5:16:16that so let's understand very very basic
  8453. 5:16:18things like although I mentioned that we
  8454. 5:16:20are going to not going to cover some of
  8455. 5:16:21the concept but what actually posgress
  8456. 5:16:23is. So posgress is a database system
  8457. 5:16:25right now why uh so now if you're a
  8458. 5:16:29complete beginner what actually a
  8459. 5:16:30database system now see when we have
  8460. 5:16:32some real application we need to store
  8461. 5:16:34our user information let's say our
  8462. 5:16:36product information or the app that you
  8463. 5:16:38are creating based on that posgress is
  8464. 5:16:40going to we are going to use to store
  8465. 5:16:43this kind of application data in a
  8466. 5:16:45structured way okay so in a structured
  8467. 5:16:47way why this is very popular and it's
  8468. 5:16:50extremely popular and the reason is
  8469. 5:16:52because it's it's actually give the
  8470. 5:16:54developers right that we are developers
  8471. 5:16:56we are going to develop so many
  8472. 5:16:58advantages that other DVs actually lacks
  8473. 5:17:01for example first of all will be
  8474. 5:17:03reliability okay now first of all when
  8475. 5:17:06you're creating a products and
  8476. 5:17:08application you can't lose your data
  8477. 5:17:10very very easily right and that will be
  8478. 5:17:12a disaster for your application for
  8479. 5:17:15example let's say I'll give a practical
  8480. 5:17:17example that if your user you have a B2C
  8481. 5:17:20application and your user is placing an
  8482. 5:17:23pays real money okay and let's say or
  8483. 5:17:26maybe updating their profile so what we
  8484. 5:17:28need to do your DB should be responsible
  8485. 5:17:31for updating all those data properly and
  8486. 5:17:34posgress SQL is trusted for this kind of
  8487. 5:17:37data handling like it's extremely
  8488. 5:17:40popular second it's give you the SQL
  8489. 5:17:42support right so possql is usually use
  8490. 5:17:45SQL what uh SQL means structured query
  8491. 5:17:48language right you can also search all
  8492. 5:17:50of these in Google also right So what
  8493. 5:17:52actually structured query language is?
  8494. 5:17:54So this is the language that we talk to
  8495. 5:17:56relational databases. Okay. So that we
  8496. 5:17:59are going to discuss later that what are
  8497. 5:18:01the commands and everything we have and
  8498. 5:18:03one very very popular advantages of
  8499. 5:18:06posgress SQL will be and that is the
  8500. 5:18:08relationship between your data right so
  8501. 5:18:11what will happen right whenever you have
  8502. 5:18:12a backend application your data the real
  8503. 5:18:15data will be connected for example one
  8504. 5:18:18user can create multiple post one order
  8505. 5:18:21belongs to one customer so one order
  8506. 5:18:23cannot belong to multiple customer these
  8507. 5:18:26things are very basic concepts right so
  8508. 5:18:28this is where post SQL is extremely
  8509. 5:18:31strong I repeat extremely strong at
  8510. 5:18:34storing and querying all those relations
  8511. 5:18:37data okay so these things will be the
  8512. 5:18:40advantages some of the very advanced
  8513. 5:18:41features that uh your posgress is going
  8514. 5:18:44to give you that we are going to not
  8515. 5:18:46going to cover that much here let's say
  8516. 5:18:48we have indexes for f queries we have
  8517. 5:18:50transactions we have full text search
  8518. 5:18:52lot of things views triggers stored
  8519. 5:18:55function extension if I'm getting good
  8520. 5:18:57response from this particular video I'm
  8521. 5:18:59going to create one more part two. Okay.
  8522. 5:19:01[snorts] So this will be your postgra
  8523. 5:19:04square with the very definition. Right?
  8524. 5:19:05Now one thing you need to understand and
  8525. 5:19:07that is one of the most important
  8526. 5:19:08concept that you can get. You can also
  8527. 5:19:10note it down that what is the relation
  8528. 5:19:13sorry what is the non-re what is the
  8529. 5:19:15differences between a relational and
  8530. 5:19:17nonreational DB. Okay. Now if you're a
  8531. 5:19:21student you have maybe heard this
  8532. 5:19:23particular term that SQL database NoSQL
  8533. 5:19:26database relational database non
  8534. 5:19:28relational database something like that
  8535. 5:19:29right so what is the actual definition
  8536. 5:19:31of a relational database so relational
  8537. 5:19:33databases actually stores data in a
  8538. 5:19:36table format okay so I'm just going to
  8539. 5:19:39let's do one readme
  8540. 5:19:42so here I'm just going to do that
  8541. 5:19:44relational database
  8542. 5:19:47relational db is going to store the data
  8543. 5:19:49in what? In tables format or in tables,
  8544. 5:19:52right? And these tables these tables can
  8545. 5:19:55be connected using relationships.
  8546. 5:20:00Okay,
  8547. 5:20:02let's say you have a users table,
  8548. 5:20:05you have a post table, you have a
  8549. 5:20:07comments table or whatever blah blah.
  8550. 5:20:11Okay.
  8551. 5:20:14So here what situation can happen that a
  8552. 5:20:16particular post is belong to one user.
  8553. 5:20:19So one post belong belongs to one user
  8554. 5:20:24something like this. Okay.
  8555. 5:20:27And let's say one comment
  8556. 5:20:30belong to one particular post something
  8557. 5:20:34like this. And this connection that we
  8558. 5:20:36have this is called relationship. Okay.
  8559. 5:20:39So now uh this is one more thing we can
  8560. 5:20:42mention that one user okay this is also
  8561. 5:20:44important that one user
  8562. 5:20:47can have many post
  8563. 5:20:50right so this is called one to many
  8564. 5:20:52relationship that we are going to
  8565. 5:20:53discuss later so this will be relational
  8566. 5:20:55DB non- relational DB will be it
  8567. 5:20:58actually doesn't organize the data using
  8568. 5:21:00connected tables right right so we'll do
  8569. 5:21:02non relational
  8570. 5:21:05uh non relations DB
  8571. 5:21:09So it's normally doesn't
  8572. 5:21:13uh organize
  8573. 5:21:17data using connected tables.
  8574. 5:21:22Okay. Now here what actually these kind
  8575. 5:21:25of DVs use? They use documents. So we
  8576. 5:21:27have what MongoDB, right? Or it also
  8577. 5:21:30uses key value pairs. Key value
  8578. 5:21:34pairs. So this will be the very basic
  8579. 5:21:37definition between a relational and non
  8580. 5:21:41relational DB. Okay. Now this is another
  8581. 5:21:45very important interview questions that
  8582. 5:21:46when you're going to use relational DB
  8583. 5:21:48or when relational DBs are a good
  8584. 5:21:51choice. So you have to mention that we
  8585. 5:21:53we are going to use postgress SQL when
  8586. 5:21:55we have our data that has clear
  8587. 5:21:58structure that has proper relationship.
  8588. 5:22:01Right? You also need to implement
  8589. 5:22:03transactions. you need joins right also
  8590. 5:22:07you also need strong validation at the
  8591. 5:22:09database level okay so these things each
  8592. 5:22:12and everything that I'm mentioning very
  8593. 5:22:14very important so first of all will be
  8594. 5:22:15data has clear structure so I'm just
  8595. 5:22:17going to write in short second will be
  8596. 5:22:19data has relationships
  8597. 5:22:22relationships
  8598. 5:22:24then maybe let's say you need to
  8599. 5:22:26implement transactions
  8600. 5:22:29also you need joins and as I already
  8601. 5:22:32mentioned that you also need strong
  8602. 5:22:33valid validation strong validation at DV
  8603. 5:22:37level. Now example will be let's say
  8604. 5:22:39you're creating a banking application
  8605. 5:22:44all right and or maybe let's say you're
  8606. 5:22:46creating a e-commerce something like
  8607. 5:22:48this or let's say you're creating a CRM
  8608. 5:22:50application so here you can use
  8609. 5:22:53relational non-reational you're going to
  8610. 5:22:56use when your data save or data is going
  8611. 5:22:59to change very often right so
  8612. 5:23:02non-reational will be when your data
  8613. 5:23:04changes
  8614. 5:23:06very often. Okay. Documents that you
  8615. 5:23:10have, I already mentioned that
  8616. 5:23:11non-relations DB is going to store your
  8617. 5:23:13documents in sorry store your data in
  8618. 5:23:15documents. So when your docs or
  8619. 5:23:18documents are independent
  8620. 5:23:20so not that much relations are there
  8621. 5:23:22okay you don't need much joins
  8622. 5:23:27right so in this case you have to use or
  8623. 5:23:30you can use not you have to you can use
  8624. 5:23:32non-reational DB. Now why am I
  8625. 5:23:34explaining all of this? Because this is
  8626. 5:23:36actually very very important. This is so
  8627. 5:23:38important right? I can't even explain.
  8628. 5:23:40You go to any company you'll see that
  8629. 5:23:41whenever they're going to start a new
  8630. 5:23:43product there will be a lot of
  8631. 5:23:45discussions with the product business or
  8632. 5:23:47leadership whatever that they what DV
  8633. 5:23:50they're going to use it's extremely
  8634. 5:23:52important why because once you start the
  8635. 5:23:56product let's say you started one
  8636. 5:23:57particular DB and that is a wrong choice
  8637. 5:23:59it can be huge problem because then
  8638. 5:24:01again you need to migrate and there can
  8639. 5:24:03be lot of things right developer cost
  8640. 5:24:06timing and all those things are matter
  8641. 5:24:07so that is the reason these things are
  8642. 5:24:09very important Now if I want to sum up
  8643. 5:24:11that when you are going to implement any
  8644. 5:24:13kind of project right you have to first
  8645. 5:24:15see the requirement but in overall
  8646. 5:24:17posgress SQL is a very very strong very
  8647. 5:24:20strong DB choice okay so this is what I
  8648. 5:24:22wanted to mention so very basic concepts
  8649. 5:24:25so with this now from the next part what
  8650. 5:24:27we're going to mention as I already
  8651. 5:24:30told that for every concepts we are
  8652. 5:24:32going to create one one file and then we
  8653. 5:24:33are going to see in the terminal the
  8654. 5:24:35same thing I'm also going to show you in
  8655. 5:24:36the PG admin but we'll focus more on the
  8656. 5:24:38SQL queries that will be your main
  8657. 5:24:40object. So let's move on to the next
  8658. 5:24:42one. All right. So now we'll start our
  8659. 5:24:44journey of learning Postgress SQL
  8660. 5:24:47concept wise but practically. Before
  8661. 5:24:50that as I already mentioned this
  8662. 5:24:51particular term in case you are not
  8663. 5:24:53aware and that is psql which is the
  8664. 5:24:55official terminal based command line
  8665. 5:24:57interface for postal SQL right. So what
  8666. 5:25:00we are going to uh why we need this or
  8667. 5:25:03when we are going to use this when we
  8668. 5:25:05need to
  8669. 5:25:07run our SQL queries. You can see that it
  8670. 5:25:09allows uh to interactively type SSQL
  8671. 5:25:12queries, issue them to the database
  8672. 5:25:15server and you can also view the result
  8673. 5:25:17directly in your terminal and this is
  8674. 5:25:19the thing I was talking about that
  8675. 5:25:20definitely whenever you are going to
  8676. 5:25:22work with your DB you can definitely see
  8677. 5:25:24the result in your DV right but when you
  8678. 5:25:27can run any kind of commands and then
  8679. 5:25:29you'll be able to get the result in the
  8680. 5:25:31command line. So that means what you
  8681. 5:25:32know your concepts very very well and
  8682. 5:25:35that is very important thing that we are
  8683. 5:25:37going to at least learn in this
  8684. 5:25:39particular video. So first of all let's
  8685. 5:25:41start with I think we have done enough
  8686. 5:25:44talking. So let's start with first file
  8687. 5:25:46which will be 01 first databaseql.
  8688. 5:25:52Okay. So this will be a first file. So
  8689. 5:25:54first what we are going to do it. So
  8690. 5:25:56first we're going to understand that how
  8691. 5:25:58you can create your first DV. Now here
  8692. 5:26:02what I'm going to do right. So first I'm
  8693. 5:26:04just going to write here that okay
  8694. 5:26:06create and then I'm just going to do
  8695. 5:26:08here database and let's give this a name
  8696. 5:26:11as Postgress
  8697. 5:26:13SQL. Okay. So we have post no let's do
  8698. 5:26:16postgra SQL
  8699. 5:26:19part one. So this is my DB name. So what
  8700. 5:26:23actually this command is doing here
  8701. 5:26:25right? So this thing is doing. So this
  8702. 5:26:27create a database is going to create a
  8703. 5:26:29new DB. So this is going to create.
  8704. 5:26:34So this is going to create new DB right
  8705. 5:26:38where inside your postgrade I'm going to
  8706. 5:26:42write insert postgrade server.
  8707. 5:26:45So this is how you can create your first
  8708. 5:26:47DB. So now one thing you need to
  8709. 5:26:50understand that in postg server you can
  8710. 5:26:53have multiple databases right. For the
  8711. 5:26:55part one, what are we going to do? We're
  8712. 5:26:57going to use one single database so that
  8713. 5:26:59we can understand and all the things
  8714. 5:27:00will be in sync. Now when we going to
  8715. 5:27:04run this particular file, what will
  8716. 5:27:06happen? So this file once it will run
  8717. 5:27:09successfully, it is going to connect to
  8718. 5:27:10your new DB, right? And then what we can
  8719. 5:27:14do from our psql command line, we can
  8720. 5:27:16actually connect directly to this
  8721. 5:27:19particular DB. But before that I'm just
  8722. 5:27:21going to give you one more uh command
  8723. 5:27:23here that you need to know. But remember
  8724. 5:27:25I'm going to write it explicitly that
  8725. 5:27:27this is actually not needed and you
  8726. 5:27:28should never do it in your real products
  8727. 5:27:30and application and that is first I'm
  8728. 5:27:32going to do the drop database
  8729. 5:27:35if exists
  8730. 5:27:37and then I'm just going to give this
  8731. 5:27:39name posgressql part one. So what
  8732. 5:27:41actually we are doing here? So you're
  8733. 5:27:43simply telling that you are going to
  8734. 5:27:46drop this particular DB if it is already
  8735. 5:27:48present. drop database is going to
  8736. 5:27:50remove this database completely and I'm
  8737. 5:27:53just going to write it here that you
  8738. 5:27:54should you should never
  8739. 5:27:58do it in prod app and this is so I'm
  8740. 5:28:04just going to write it here so this is
  8741. 5:28:07only for
  8742. 5:28:10learning okay so two things we have
  8743. 5:28:12learned one is how you can drop a
  8744. 5:28:14database and how you can create a new
  8745. 5:28:16database okay so now let's see how we
  8746. 5:28:18are going to connect to this particular
  8747. 5:28:20and how we are going to run this
  8748. 5:28:22particular file that is also important
  8749. 5:28:23as I already mentioned that we are going
  8750. 5:28:25to use command line and for this one you
  8751. 5:28:27can use a individual command line but
  8752. 5:28:29I'm going to use VS code only because it
  8753. 5:28:31will be easy for us to understand
  8754. 5:28:33so first of all I'm going to run this
  8755. 5:28:35particular command okay and I'm going to
  8756. 5:28:37explain that what each and everything is
  8757. 5:28:38doing here this is each and everything
  8758. 5:28:40see we are going to do from this
  8759. 5:28:42particular point you need to understand
  8760. 5:28:44everything you can't memorize these
  8761. 5:28:46things in case you're not able to do it
  8762. 5:28:48will be very difficult for you to
  8763. 5:28:50understand when you will actually build
  8764. 5:28:52your backend applications. So here I'm
  8765. 5:28:55just going to do psql u and I'm just
  8766. 5:28:58going to do this one as posgress
  8767. 5:29:00space - d and I'm just going to do
  8768. 5:29:03posgress and here I'm just going to do
  8769. 5:29:05hyphen f and then I will go to my part
  8770. 5:29:08one which is this particular folder part
  8771. 5:29:10one slash and then I'm just going to
  8772. 5:29:12give this file name. So I'm just going
  8773. 5:29:14to copy this here and then I'm just
  8774. 5:29:16going to paste it. So what actually this
  8775. 5:29:18is doing here? So first our psql is our
  8776. 5:29:21posgressql command line tool hyphen u
  8777. 5:29:24pososegr is nothing but we are simply
  8778. 5:29:26telling that we are logging as our user
  8779. 5:29:28which is posgress hyphen d posgress
  8780. 5:29:31means we are going to connect to our
  8781. 5:29:33database posgress and hyphen af this
  8782. 5:29:36particular means we are going to run
  8783. 5:29:38this commands from this particular file
  8784. 5:29:40as simple as that okay so in sum up you
  8785. 5:29:43it's telling that use psql login as
  8786. 5:29:46posgressql user connect to your posgress
  8787. 5:29:49database and run run this particular SQL
  8788. 5:29:51file. That's it. And we going to do
  8789. 5:29:53enter. You have to give a password and
  8790. 5:29:55I'm just going to give admin enter.
  8791. 5:29:59And you can see that what it's saying.
  8792. 5:30:01So this is fine. So this doesn't exist.
  8793. 5:30:04So it's going to skip which is fine.
  8794. 5:30:05Drop database and create database. So
  8795. 5:30:07that means this is actually created. So
  8796. 5:30:09now if I just go here, right? And let me
  8797. 5:30:11just I don't know why this is very slow.
  8798. 5:30:14So let me refresh this.
  8799. 5:30:17I think I get this particular thing in
  8800. 5:30:20other places also that this is uh little
  8801. 5:30:23slow this issue also reported by other
  8802. 5:30:25users also and you can see that we have
  8803. 5:30:27this postgrad SQL part one and I'm just
  8804. 5:30:30going to do right click not right click
  8805. 5:30:32I think let's open this and you're going
  8806. 5:30:35to see all the details and everything
  8807. 5:30:36schemas and everything but for now let's
  8808. 5:30:39leave it here so that means now you have
  8809. 5:30:41your DB here so what I will suggest let
  8810. 5:30:44me just move this one here So what I'm
  8811. 5:30:47actually suggesting that try to
  8812. 5:30:49understand each and every command very
  8813. 5:30:51very seriously because this is what you
  8814. 5:30:54need to work in your company also when
  8815. 5:30:56you'll be working as a backend developer
  8816. 5:30:58you have to work with your DB you need
  8817. 5:31:00to know each and everything like like
  8818. 5:31:02such a way so that if someone ask you
  8819. 5:31:04okay give me this particular command SQL
  8820. 5:31:06query or everything you should be able
  8821. 5:31:08to do it like this right so first of all
  8822. 5:31:11we learn that how you can create a new
  8823. 5:31:13DB and how we are going to run this okay
  8824. 5:31:15Now I'm going to show you that how you
  8825. 5:31:17can run your psql queries from your
  8826. 5:31:20terminal. So first I'm just going to
  8827. 5:31:22clear this. So first let's do here psql
  8828. 5:31:25- u. So which will be our user which is
  8829. 5:31:27posgress and I'll do hyphen d and this
  8830. 5:31:31is our db and then I'm just going to do
  8831. 5:31:34enter and you'll see that it's asking
  8832. 5:31:35for this password every time. Okay. So
  8833. 5:31:37you have to give the password every
  8834. 5:31:38time. So what we going to do right? So
  8835. 5:31:40let's cancel this and I'm just going to
  8836. 5:31:44add this password here.
  8837. 5:31:48Okay. So now let's clear once again and
  8838. 5:31:50then we are going to just run once again
  8839. 5:31:54here and you can see that now this is
  8840. 5:31:56connected. So now I'm going to show you
  8841. 5:31:58that some of the things that you can
  8842. 5:31:59directly check from here. So first of
  8843. 5:32:02all let's say you want to see which
  8844. 5:32:05current database that you are using. So
  8845. 5:32:07here what you can do right you're going
  8846. 5:32:08to do select and you're going to do
  8847. 5:32:10current underscore and you have to do
  8848. 5:32:12database.
  8849. 5:32:15All right and then you'll do enter and
  8850. 5:32:17you can see that this is our current
  8851. 5:32:18database. And now remember this is we
  8852. 5:32:21are actually writing inside your psql
  8853. 5:32:23command line. Okay. Now you want to see
  8854. 5:32:26the current user. So you're going to do
  8855. 5:32:28select and you'll do current user.
  8856. 5:32:31Enter. This is the current user which is
  8857. 5:32:33posgress. Okay. Now you can also see
  8858. 5:32:37which version of Postgress SQL you're
  8859. 5:32:39using. So I'm just going to do select
  8860. 5:32:41and here if I'm not wrong it will be
  8861. 5:32:44version and this will be the this is
  8862. 5:32:46going to give you the version. Okay you
  8863. 5:32:48can see that this is the version that we
  8864. 5:32:49are using right. So now let's say you
  8865. 5:32:52want to see all your databases that how
  8866. 5:32:55many databases I have. So you have to
  8867. 5:32:56give slash I don't know which what but
  8868. 5:32:59this particular uh icon called u. So I
  8869. 5:33:03think this is called back slash I think
  8870. 5:33:05and then this will be l and here you're
  8871. 5:33:08going to get all these okay all the
  8872. 5:33:11databases. Now let's say you want to see
  8873. 5:33:13all your tables. So for tables you have
  8874. 5:33:15to do dt and here you can see that we
  8875. 5:33:17don't have any tables for now which
  8876. 5:33:19makes sense. Now you want to exit your
  8877. 5:33:21psql command line. So you can also type
  8878. 5:33:24here exit directly. So let's see if I do
  8879. 5:33:26exit it will be it will be uh it will
  8880. 5:33:29exit. Or else you can also do like this.
  8881. 5:33:32You can directly do uh slash and then
  8882. 5:33:35you have to do Q. So this is going to
  8883. 5:33:37also do the exit. Okay. So both the
  8884. 5:33:39things are very important. So you can
  8885. 5:33:42use exit or this particular / Q. So this
  8886. 5:33:45will be the very first thing basic
  8887. 5:33:46things you have learned. So now let's
  8888. 5:33:48move on to the next one which will be
  8889. 5:33:50how we are going to create our schema.
  8890. 5:33:52So here I'm just going to create one
  8891. 5:33:54more file and let's do this one as 02
  8892. 5:33:57and I'm just going to do this one as
  8893. 5:33:59first schema.sql.
  8894. 5:34:02Okay, let's bring it here. So first of
  8895. 5:34:06all, how we are going to create your
  8896. 5:34:08first schema? What actually schema? Now
  8897. 5:34:11schema is like a folder inside your
  8898. 5:34:13database. Okay, now there will be some
  8899. 5:34:16default schema it's going to create,
  8900. 5:34:18right? So you see that you have the
  8901. 5:34:19schema. Let's expand this.
  8902. 5:34:23See that there is a public schema that
  8903. 5:34:25will be created and inside this also
  8904. 5:34:27you're going to have the tables. Okay.
  8905. 5:34:29So this is your public or default schema
  8906. 5:34:31that postsql always has. So now let's
  8907. 5:34:34say we want to create our own schema. So
  8908. 5:34:37we going to have our DB. So it's like
  8909. 5:34:39this. So I'm just going to give a
  8910. 5:34:41comment. Okay. So here I'm just going to
  8911. 5:34:44have the DB. DB is going to have the
  8912. 5:34:46schema. Schema is going to have the
  8913. 5:34:49table. table is going to have the rows
  8914. 5:34:52something like that. So now I want to
  8915. 5:34:53create my own schema. So how I can do
  8916. 5:34:56that and that is very very simple. So
  8917. 5:34:57I'm just going to do create schema and
  8918. 5:35:00then I'm just going to do if not exists
  8919. 5:35:04and then you have to give the name. So
  8920. 5:35:05I'm just going to do basic something
  8921. 5:35:06like this. Right? So create schema is
  8922. 5:35:08going to create a name space for your
  8923. 5:35:10table and your other database objects.
  8924. 5:35:13What actually if not exist means? So
  8925. 5:35:15this is going to prevent an error. Very
  8926. 5:35:17very important. If this schema is
  8927. 5:35:20already created, so this is going to
  8928. 5:35:22this if not exists
  8929. 5:35:26is going to
  8930. 5:35:29prevent an error, right? Uh if the
  8931. 5:35:33schema
  8932. 5:35:36schema
  8933. 5:35:39is already
  8934. 5:35:41created. Okay. So this is how you can
  8935. 5:35:44create a schema. All right. So this is
  8936. 5:35:46how you can create a schema. Now here
  8937. 5:35:48I'm also going to show you that how you
  8938. 5:35:49can create a extension and why actually
  8939. 5:35:51you need it. So I'm just going to do
  8940. 5:35:52here create extension
  8941. 5:35:55if not exist and here I'm just going to
  8942. 5:35:59use pg crypto.
  8943. 5:36:02Okay. So what actually this extension is
  8944. 5:36:04going to do? So in later part we are
  8945. 5:36:06going to use uh something called get
  8946. 5:36:08random UID which will help us to create
  8947. 5:36:12UID primary keys and that is very
  8948. 5:36:14important because in Postgress SQL what
  8949. 5:36:16we can do right we can directly create
  8950. 5:36:18or generate UID. So here this particular
  8951. 5:36:21extension is needed because this is
  8952. 5:36:23going to help us to create that
  8953. 5:36:25particular UID. Okay for later we are
  8954. 5:36:28going to discuss uh this particular
  8955. 5:36:29particular thing in details. So now what
  8956. 5:36:31I'm going to do right so this is going
  8957. 5:36:33to create the schema and this is going
  8958. 5:36:35to create the extension right I think
  8959. 5:36:37extension spelling is also correct all
  8960. 5:36:40right now what I'm going to do right now
  8961. 5:36:42this is where I wanted to explain one
  8962. 5:36:44more thing which is very important so
  8963. 5:36:45here what I'm going to do right I'm
  8964. 5:36:47going to write a query okay so here I'm
  8965. 5:36:49just going to write a query now this
  8966. 5:36:51query I can directly write from my psql
  8967. 5:36:54command line also but now you may ask me
  8968. 5:36:56that why I'm writing this one here the
  8969. 5:36:58reason is this is for the teaching
  8970. 5:37:00purpose this This is not mandatory. This
  8971. 5:37:03is optional. Why I'm doing this? Because
  8972. 5:37:05when you are going to download this
  8973. 5:37:06source code, you will be able to
  8974. 5:37:07directly see this query or you can copy
  8975. 5:37:09and you can run. Okay. So that is the
  8976. 5:37:11reason I'm going to follow this
  8977. 5:37:13particular uh process throughout this
  8978. 5:37:16video, not for this particular file. So
  8979. 5:37:18now let's say what I need to do, right?
  8980. 5:37:20I want to write a particular query which
  8981. 5:37:22is going to give me all the schemas that
  8982. 5:37:24are available in my current DB. Okay. So
  8983. 5:37:27to do that, what we're going to do,
  8984. 5:37:28right? We're going to use a select and
  8985. 5:37:30here I'm just going to use a schema name
  8986. 5:37:33and then I'm just going to do from and
  8987. 5:37:35this we have something called
  8988. 5:37:36information
  8989. 5:37:38schema
  8990. 5:37:40schema schemata. So what actually this
  8991. 5:37:43is doing now this information doss
  8992. 5:37:44schema dos schemata is a system view.
  8993. 5:37:47What actually system view? So system
  8994. 5:37:48view is a built-in view that is going to
  8995. 5:37:51give you the information about your
  8996. 5:37:52database structure. For example, let's
  8997. 5:37:54say you want to see all your database
  8998. 5:37:56schemas that how many schemas are
  8999. 5:37:58available. So in that case, we can use
  9000. 5:38:00this. And here I'm also going to do
  9001. 5:38:01order Y by for now. Let's skip this. We
  9002. 5:38:04are going to have a separate section on
  9003. 5:38:06what actually is order Y. So I'm just
  9004. 5:38:07going to do order Y of schema name.
  9005. 5:38:10That's it. So now what I can do right, I
  9006. 5:38:12can simply directly run this particular
  9007. 5:38:14file which is going to create a schema
  9008. 5:38:17going to create this extension and at
  9009. 5:38:19the end it's going to run this
  9010. 5:38:20particular query. All right. So now
  9011. 5:38:22let's run this file and what we can do
  9012. 5:38:24right. So let me just copy this
  9013. 5:38:28and then we'll paste it here. So what we
  9014. 5:38:30are doing so loopsql u posgress d this
  9015. 5:38:34is the db that we have created in the
  9016. 5:38:36first uh uh file and part one/ Now we
  9017. 5:38:40need to run this file. Okay. So I'm just
  9018. 5:38:42going to copy this and then let's paste
  9019. 5:38:45it here.
  9020. 5:38:47Enter. And you can see that now we have
  9021. 5:38:50the schema is created extension is
  9022. 5:38:52created and you are getting so many
  9023. 5:38:54schemas. Okay. Now this might be a
  9024. 5:38:55little confusing that from where these
  9025. 5:38:57are coming.
  9026. 5:38:59First one so basic schema is your custom
  9027. 5:39:01schema that you have created. What is
  9028. 5:39:03the information schema? This is the
  9029. 5:39:05posgress standard metadata schema. Then
  9030. 5:39:08you have the PG catalog. This is
  9031. 5:39:09posgress internal system schema. PG
  9032. 5:39:12toast. Uh if I'm not wrong this is
  9033. 5:39:15internal storage schema for large
  9034. 5:39:17values. Right? You can also check it
  9035. 5:39:19once not exactly see what's so pg toast
  9036. 5:39:22schema in to place you can just search
  9037. 5:39:24it.
  9038. 5:39:30Okay. So you can see that this is
  9039. 5:39:31internal names we used for the oversiz
  9040. 5:39:33attribute right so it's for large values
  9041. 5:39:36and then you have the public which I
  9042. 5:39:37already mentioned that every posgress is
  9043. 5:39:39going to have a database is going to
  9044. 5:39:41have a default schema uh which will be
  9045. 5:39:43present in every database. Right now why
  9046. 5:39:46this is coming up because in the query
  9047. 5:39:49I'm actually getting all my schemas at
  9048. 5:39:51once. Right? And this information schema
  9049. 5:39:53dots schemata is going to give me all
  9050. 5:39:55the schemas that are available in my uh
  9051. 5:39:57database. All right. So now uh if you go
  9052. 5:40:01here right so so this is our uh DB. So
  9053. 5:40:03now let me just refresh this.
  9054. 5:40:07This is very slow man. In Windows it is
  9055. 5:40:09first. I don't know. I think that is the
  9056. 5:40:11reason so many issues I think they have
  9057. 5:40:13reported on this. I don't know why this
  9058. 5:40:15is this much slow. Even I'm using the
  9059. 5:40:17latest version of Mac but still it is
  9060. 5:40:19very slow. So if I go to schemas and
  9061. 5:40:22let's see what we getting here. So you
  9062. 5:40:24can see that we are already getting two
  9063. 5:40:26schema and you can see that we have the
  9064. 5:40:27basic schema. Fantastic. So that means
  9065. 5:40:29this is now completed. So this is how
  9066. 5:40:31you can create schemas and also
  9067. 5:40:33extension. Now moving to the next one
  9068. 5:40:35which will be very very important. So
  9069. 5:40:37for now what you're going to do right
  9070. 5:40:38I'm just going to do 03
  9071. 5:40:41and let's do this one as first table
  9072. 5:40:44dossql. So now we are going to
  9073. 5:40:46understand that how you can create
  9074. 5:40:47table. Okay. So first of all what
  9075. 5:40:49actually table? So table is going to
  9076. 5:40:51store your data right or store one type
  9077. 5:40:55of data. So here let's create a simple
  9078. 5:40:58students table. So now what we going to
  9079. 5:41:00do right your table is going to live
  9080. 5:41:02inside your schema. And now we have
  9081. 5:41:04created a basic schema here. So what I'm
  9082. 5:41:06going to do right inside my this
  9083. 5:41:08particular schema you wanted to create a
  9084. 5:41:10table. So first of all I'm just going to
  9085. 5:41:11do drop table. Now again don't do this
  9086. 5:41:13in your real project. This is just for
  9087. 5:41:15teaching purpose that this is how you
  9088. 5:41:17can also drop table. Okay. So this will
  9089. 5:41:19be drop table and here this will be if
  9090. 5:41:22exists and I'm just going to do basics
  9091. 5:41:25dot let's do students. Okay. So inside
  9092. 5:41:29my basic schema you wanted to have a
  9093. 5:41:30students table. Simple. And then I'm
  9094. 5:41:32going to create a table. We'll do create
  9095. 5:41:34table and I'm just going to do basics
  9096. 5:41:36dot students and here now we have to
  9097. 5:41:39have all our property. So let's start
  9098. 5:41:41with the first one. First I need to have
  9099. 5:41:42a id which will be serial and this will
  9100. 5:41:46be my primary key. Now what actually we
  9101. 5:41:48are doing here. Now this ID is your
  9102. 5:41:51unique value. Right? So what this serial
  9103. 5:41:54is going to do this is going to create
  9104. 5:41:57an auto incremental
  9105. 5:42:00or auto incrementing
  9106. 5:42:02integer. Okay. So that means if I'm
  9107. 5:42:05inserting one particular student with ID
  9108. 5:42:071 from next time when I'm going to
  9109. 5:42:09insert it it will be like 2, 3 like this
  9110. 5:42:12and so on. Right? so on. So your four
  9111. 5:42:15five something like this right? Primary
  9112. 5:42:18key very very important very important
  9113. 5:42:20concept that this simply means so
  9114. 5:42:22primary key simply means
  9115. 5:42:26that this
  9116. 5:42:28column
  9117. 5:42:29uniquely
  9118. 5:42:32identifies
  9119. 5:42:33each row. Okay. So this will be the
  9120. 5:42:37first property that I wanted to have or
  9121. 5:42:39column. Then I'm going to have a name
  9122. 5:42:41property which will be text and I'm just
  9123. 5:42:43going to make this one as not null. So
  9124. 5:42:46what we are simply telling that this is
  9125. 5:42:48my another property. So text is going to
  9126. 5:42:52represent string data. Then I'm simply
  9127. 5:42:56telling that not null
  9128. 5:43:00uh means
  9129. 5:43:02this column is required. Right? So that
  9130. 5:43:06means
  9131. 5:43:07postgress is going to
  9132. 5:43:10reject if this name value
  9133. 5:43:15is not present. As simple as that. Then
  9134. 5:43:19I'm also going to take a email property.
  9135. 5:43:21So I'm going to take a email which will
  9136. 5:43:22be text. This also will be not null. And
  9137. 5:43:25this will be a unique property. Okay?
  9138. 5:43:27Because every student is going to have
  9139. 5:43:29their unique email. All right? So unique
  9140. 5:43:32means
  9141. 5:43:34unique
  9142. 5:43:37means that um no two student
  9143. 5:43:45is going to have
  9144. 5:43:48uh same email. Okay. Then let's take an
  9145. 5:43:52one more property and then I'm just
  9146. 5:43:54going to take a age property. This will
  9147. 5:43:56be integer. I'm just going to take a
  9148. 5:43:59check. So check is going to add a rule
  9149. 5:44:01that every student age has to be greater
  9150. 5:44:04than or equal 18. So this is one check I
  9151. 5:44:06wanted to give and at the end I'm going
  9152. 5:44:08to take a created at property. So do
  9153. 5:44:11created add which will be timestamp
  9154. 5:44:17and here I'm just going to do this one
  9155. 5:44:18as default. Now so what we are doing
  9156. 5:44:21here so simply telling that this time
  9157. 5:44:23stamp is going to store your date and
  9158. 5:44:26time format. So time stamp is going to
  9159. 5:44:28store your date and time format. Default
  9160. 5:44:33means if you don't give anything it's
  9161. 5:44:34going to take by default.
  9162. 5:44:37Default means if you don't give any
  9163. 5:44:41value
  9164. 5:44:44it will take by default.
  9165. 5:44:49That's it. So now I think this is done.
  9166. 5:44:52So first of all let's create this table
  9167. 5:44:54and let's see if this is creating inside
  9168. 5:44:56that particular schema or not. So first
  9169. 5:44:59of all let's go here. I'm going to clear
  9170. 5:45:03this and then I'm just going to run it
  9171. 5:45:05here. So we are running inside this and
  9172. 5:45:07then I'm just going to copy this. Let's
  9173. 5:45:10do rename and then let's run it here.
  9174. 5:45:16Enter. And you can see that it's created
  9175. 5:45:19the table. So you can go here and let me
  9176. 5:45:21just refresh this.
  9177. 5:45:29So if I go inside this you'll be see
  9178. 5:45:31that we'll be having the tables and
  9179. 5:45:33inside this you're going to have your
  9180. 5:45:34table one table which is my student
  9181. 5:45:36table and you can see the columns and
  9182. 5:45:38everything here right. So you can expand
  9183. 5:45:40this and you can see all your properties
  9184. 5:45:42that you have added. So ID, integer,
  9185. 5:45:44name, email, age and created date. Okay.
  9186. 5:45:47So that mean this is working fine. So
  9187. 5:45:49now [clears throat] what we going to do
  9188. 5:45:50right? So we need to insert some values
  9189. 5:45:53so that we can see that whether we we
  9190. 5:45:55have those values or not. Correct. But
  9191. 5:45:57before that let's do one thing. Let's
  9192. 5:45:59run some of the uh commands here. Okay.
  9193. 5:46:03Inside psql so that you can understand
  9194. 5:46:04what is happening here. So first of all
  9195. 5:46:07inside this. So let's run this command.
  9196. 5:46:10So I'm just going to open this. So do
  9197. 5:46:13psql - u postgress and then I'm going to
  9198. 5:46:17run this db. So inside this and then
  9199. 5:46:19first of all I wanted to see my tables.
  9200. 5:46:21I'll do /dt.
  9201. 5:46:23Okay it's showing no tables.
  9202. 5:46:27Oh I think we need to do /dt and we need
  9203. 5:46:29to go to basics basics dot and then
  9204. 5:46:33we'll do star.
  9205. 5:46:35Okay. So you can see that we have to do
  9206. 5:46:37list of tables. Schema basics name
  9207. 5:46:39students. This is table owner is
  9208. 5:46:41postgress. Fantastic. Now first of all
  9209. 5:46:45before we'll do anything right we need
  9210. 5:46:47to add some data so that you can check
  9211. 5:46:49whether we have any student present or
  9212. 5:46:51not. Correct? So how we are going to do
  9213. 5:46:53that? So see this is what I was talking
  9214. 5:46:56about that these queries that I'm
  9215. 5:46:58writing here these direct queries I can
  9216. 5:47:00directly run here also inside psql. The
  9217. 5:47:03reason why I will be adding here so that
  9218. 5:47:04when you're going to download the source
  9219. 5:47:06code you can take the reference from
  9220. 5:47:07here. I hope you're getting right. Don't
  9221. 5:47:09get confused. These queries you don't
  9222. 5:47:11have to write in the file. This is for
  9223. 5:47:12the teaching purpose. So for now I'm
  9224. 5:47:14just going to exit from here. Clear
  9225. 5:47:16this. And then let's run the first uh
  9226. 5:47:21query here. So I'm just going to do
  9227. 5:47:23what? So I'm going to insert some uh
  9228. 5:47:25data. So insert some data.
  9229. 5:47:28So to do this one, we're going to do
  9230. 5:47:30insert
  9231. 5:47:32and insert we need to do into. So you
  9232. 5:47:34have basics dot students. So this is the
  9233. 5:47:38table and what are the things we need to
  9234. 5:47:39insert. So first of all id will be
  9235. 5:47:41automatically created then we have name
  9236. 5:47:44comma we have email comma we have age
  9237. 5:47:47these three things right. So this is the
  9238. 5:47:50things we want to insert but we need to
  9239. 5:47:52tell right okay what values we wanted to
  9240. 5:47:54insert. So that means we need to give
  9241. 5:47:56here values and these values is going to
  9242. 5:47:59tell me that okay these things I wanted
  9243. 5:48:01to insert. So I'm just going to add sum.
  9244. 5:48:05So let's give one more email. So do
  9245. 5:48:07thegmail.com
  9246. 5:48:10and comma I'm just going to give here 55
  9247. 5:48:13although I'm not 55 just I'm just
  9248. 5:48:14giving. So here I'm just going to copy
  9249. 5:48:16this paste it here. Let's do here John
  9250. 5:48:20and I'll do this one as John and then
  9251. 5:48:23let's do 23. Okay. And we need to give
  9252. 5:48:27this one. So this will be the first uh
  9253. 5:48:30data that I wanted to insert. So now
  9254. 5:48:33what I'm going to do right so let's run
  9255. 5:48:34one second and we'll see that what is
  9256. 5:48:36happening here. Okay. So first of all
  9257. 5:48:38I'm going to run this file again and see
  9258. 5:48:40that we have insert something is
  9259. 5:48:42happening here but we don't know what
  9260. 5:48:43actually happened. So first we need to
  9261. 5:48:45see that whether we have all this data
  9262. 5:48:48present inside our table or not. So
  9263. 5:48:50first I'm going to go inside this and
  9264. 5:48:52then I'm what I'm going to do right. So
  9265. 5:48:54I'm just going to do select
  9266. 5:48:57star and I'm just going to do from. So I
  9267. 5:48:59have to go to basics
  9268. 5:49:02dot students. So it's simply telling
  9269. 5:49:05that select is going to read your data
  9270. 5:49:07from a table. This star means we need
  9271. 5:49:10all the columns. Okay. So this is what
  9272. 5:49:12you need to enter. And you can see that
  9273. 5:49:14we have two data. ID one sum email this
  9274. 5:49:18created at John and this is the data.
  9275. 5:49:20Fantastic. Now this is what I was
  9276. 5:49:22talking about. The same query I can
  9277. 5:49:24write inside this file also so that
  9278. 5:49:27whenever you're going to run this file
  9279. 5:49:28you'll be able to get the result
  9280. 5:49:30immediately. Okay. So let's see if I
  9281. 5:49:33just wanted to write it here. So
  9282. 5:49:34something like select
  9283. 5:49:37from and then you're going to do basics
  9284. 5:49:39dot students it's going to do the exact
  9285. 5:49:41same thing. Okay. So now I think this is
  9286. 5:49:43done. So this is how what you're going
  9287. 5:49:46to do. You're going to create your
  9288. 5:49:47table. You can drop table using drop
  9289. 5:49:49table command. So [snorts] create table
  9290. 5:49:51you have to take all your properties
  9291. 5:49:53that you're going to take and based on
  9292. 5:49:54that if you want to insert some data you
  9293. 5:49:56need to insert and then if you want to
  9294. 5:49:59uh read the data you need to select and
  9295. 5:50:01then you have to tell that okay from
  9296. 5:50:03which place I wanted to read the data.
  9297. 5:50:05So in our case this will be our basic
  9298. 5:50:07schema dot students table. So with this
  9299. 5:50:10now let's move on to the next one. All
  9300. 5:50:12right. So now let's move on to the next
  9301. 5:50:14one which will be going to understand
  9302. 5:50:15some of the data types. Okay. So I'm
  9303. 5:50:17just going to do 04 data types dots sql.
  9304. 5:50:22So I think some of the data types we
  9305. 5:50:23have already covered but still we need
  9306. 5:50:25to understand some more. So first I'm
  9307. 5:50:27just going to do drop table if exists
  9308. 5:50:31and I'm just going to create one more.
  9309. 5:50:33So I'm just going to do here basics dot
  9310. 5:50:35let's give this one as maybe products
  9311. 5:50:38basic. Okay. And then let's create the
  9312. 5:50:40table. So we'll do create table and
  9313. 5:50:44let's give this one as basics.
  9314. 5:50:47dot product
  9315. 5:50:50basic and then
  9316. 5:50:52so we'll start with the first one which
  9317. 5:50:54we've already covered. So this will be
  9318. 5:50:56ID. So we're going to take serial and
  9319. 5:50:58this will be primary
  9320. 5:51:01key. Okay. Then next we are going to
  9321. 5:51:04take our uh let's take a name property
  9322. 5:51:07and here I'm just going to take one more
  9323. 5:51:09uh prop uh thing here and that is called
  9324. 5:51:11vare and I'm just going to give this one
  9325. 5:51:13as 100. Let's do this one as not null.
  9326. 5:51:16Now what actually we mean by this this v
  9327. 5:51:19100 that we have taken here that means
  9328. 5:51:20that we have a string with maximum
  9329. 5:51:23length of 100 characters. Okay, so I'm
  9330. 5:51:25just going to write here that we have a
  9331. 5:51:27string that has a max length of 100
  9332. 5:51:32characters. Okay, you need to use vare
  9333. 5:51:36when you want to enforce a maximum
  9334. 5:51:38length. Okay, so this is one more thing
  9335. 5:51:41that you have learned. Now next thing uh
  9336. 5:51:44let's take a description
  9337. 5:51:47and for description I'm just going to
  9338. 5:51:49take this one as text. Okay. So text is
  9339. 5:51:52normal uh normally as string type. Uh
  9340. 5:51:54what else? So let's take maybe a stock
  9341. 5:51:56count. So I'm just going to take this
  9342. 5:51:58one as stock and let's uh or let's do
  9343. 5:52:00this one as stock. Maybe let's give
  9344. 5:52:03stock only. And I'll do this one as
  9345. 5:52:05integer and I'm just going to give a
  9346. 5:52:07default value. Now we'll be able to also
  9347. 5:52:08give a default value and this will be
  9348. 5:52:10zero. Right? So integer is going to
  9349. 5:52:13store a whole number right. So that
  9350. 5:52:15means your stock should be a whole
  9351. 5:52:17number 0 1 2 300 like that. It should
  9352. 5:52:20not be 10.5 or 5.5. Okay, this is
  9353. 5:52:23actually important when you're using
  9354. 5:52:25integer. Then we have something called
  9355. 5:52:27big int. So let's I'm just going to take
  9356. 5:52:29a total property here or let's do total
  9357. 5:52:31views or something like this. And I'm
  9358. 5:52:33just going to give this one as big int.
  9359. 5:52:35And then here also I'm just going to
  9360. 5:52:36give this one as a default value which
  9361. 5:52:39will be zero. Now what this big int
  9362. 5:52:42means that we want to store larger whole
  9363. 5:52:45number than integer. So we want to store
  9364. 5:52:49larger whole number
  9365. 5:52:53than integer right or we have integer
  9366. 5:52:57something like this. So now in most of
  9367. 5:53:00the times you'll see that your uh
  9368. 5:53:02integer will be more than enough. Then
  9369. 5:53:05we have one more property that you need
  9370. 5:53:06to understand. Let's we have a price
  9371. 5:53:08property and this is called numeric.
  9372. 5:53:10Okay. So I'll do numeric and here I'm
  9373. 5:53:12just going to give 10 2. Now what
  9374. 5:53:14actually this is telling here this
  9375. 5:53:16numeric is actually useful for your
  9376. 5:53:17exact decimal values. So when you are
  9377. 5:53:20going to use decimal values you want to
  9378. 5:53:22take one minute. Okay. So you want to
  9379. 5:53:26take numeric here. So this will be your
  9380. 5:53:27exact decimal
  9381. 5:53:30values. So what I mean by exact? So here
  9382. 5:53:33uh we have 10. So 10 is what? 10 means
  9383. 5:53:37your total digits. So 10 means total
  9384. 5:53:41digits and then you have two. So two
  9385. 5:53:44means here digits
  9386. 5:53:47after the decimal point after the
  9387. 5:53:51decimal
  9388. 5:53:54point. Okay. So let's say you have 999
  9389. 5:53:58something like this 999. Okay. So this
  9390. 5:54:00is where you can take numeric and then
  9391. 5:54:02at the end we have the boolean. So let's
  9392. 5:54:05say I want to take is active something
  9393. 5:54:07like this is active. So let's do is
  9394. 5:54:09active and here I'm just going to take
  9395. 5:54:11this one as boolean and here you can
  9396. 5:54:13also take a default value and default
  9397. 5:54:15value will be true. So this will be my
  9398. 5:54:17default value and this will be a boolean
  9399. 5:54:19property. Okay. So this is how you can
  9400. 5:54:21create. Now here we don't have that much
  9401. 5:54:24things to understand because these are
  9402. 5:54:25very straightforward whatever things we
  9403. 5:54:27have done. Now let's see want to let's
  9404. 5:54:29write the queries also. So I'm just
  9405. 5:54:30going to write the queries that I want
  9406. 5:54:32to add some rows in this particular
  9407. 5:54:36table. So how we can do that? We have
  9408. 5:54:37already learned. So we can do insert and
  9409. 5:54:39let's do into and we are going to do
  9410. 5:54:42basics dot product basic and here let's
  9411. 5:54:46say I wanted to render sorry enter name
  9412. 5:54:50then we have what description then we
  9413. 5:54:52have the stock then we have the total
  9414. 5:54:54views right so I think we have taken the
  9415. 5:54:56total views then we have the price and
  9416. 5:54:59then at the end we have the is active so
  9417. 5:55:02these all the things we want to render
  9418. 5:55:03and what we want to render so we need to
  9419. 5:55:05render the values correct And now let's
  9420. 5:55:08say I wanted to add here. So let's add
  9421. 5:55:12here something like this. Product one
  9422. 5:55:15description. I'm just going to do
  9423. 5:55:16product disk.
  9424. 5:55:19Then we're going to give a uh stock
  9425. 5:55:22count which will be what? So this will
  9426. 5:55:23be whole number. Then we have the total
  9427. 5:55:25views. So what is total views? Total
  9428. 5:55:27views will begin. So you can take
  9429. 5:55:29anything like 1200. Then you have the uh
  9430. 5:55:32so after this so let's take the price.
  9431. 5:55:35So price will be numeric right? So we
  9432. 5:55:37can take something like 2 455.65
  9433. 5:55:41and then we'll take true. Okay. So these
  9434. 5:55:43values I want to insert. And let's say
  9435. 5:55:46now at the end you want to also get the
  9436. 5:55:48values here that how many uh um rows or
  9437. 5:55:53products that we have added. So to do
  9438. 5:55:54this what we can do simply we can do a
  9439. 5:55:56select right and then we'll do start.
  9440. 5:55:59Now remember I already mentioned all of
  9441. 5:56:00this you can directly run for my psql
  9442. 5:56:02command also that I've already showed
  9443. 5:56:04you all of these but here I can I'm
  9444. 5:56:06doing all of this at once. So I'll do
  9445. 5:56:08basics dot product basics. So I'm simply
  9446. 5:56:11telling that okay first create this
  9447. 5:56:12table insert this particular data and
  9448. 5:56:16then I'm going to get the data
  9449. 5:56:17information. Now let's say uh you get
  9450. 5:56:20one more requirement that you need to
  9451. 5:56:22write a query where you are only going
  9452. 5:56:24to get the active products. You have to
  9453. 5:56:27get only the active products not all the
  9454. 5:56:29products. Okay. So how you can do that?
  9455. 5:56:32Now here you'll see that we have a
  9456. 5:56:34boolean column that is is active. Right?
  9457. 5:56:38This one what we can do that we can
  9458. 5:56:39directly use in our filters. So
  9459. 5:56:42something like this that where the field
  9460. 5:56:44name which is is active is equal to true
  9461. 5:56:46that is also valid. So let's see how we
  9462. 5:56:49are going to do that. So here I'm simply
  9463. 5:56:50going to do that. Okay. Select ID.
  9464. 5:56:54Select name. Select price and select is
  9465. 5:56:59active from. So you have to give from
  9466. 5:57:02from where to place. So L basics.p
  9467. 5:57:04product basics this is the table name
  9468. 5:57:06and this is the condition. Then you're
  9469. 5:57:08going to tell where is active is two.
  9470. 5:57:11That's it. So this is going to give you
  9471. 5:57:13all these data where is active will be
  9472. 5:57:15true. Now let's see if this is working
  9473. 5:57:17or not. Okay. So I'm just going to clear
  9474. 5:57:19this and let's run again. And here. So
  9475. 5:57:23first I'm just going to rename copy this
  9476. 5:57:27and then I'm just going to paste it
  9477. 5:57:28here.
  9478. 5:57:32Okay, we are getting one error uh insert
  9479. 5:57:34into basic. Okay, something I think we
  9480. 5:57:37have missed here that is
  9481. 5:57:41okay. We have to give this
  9482. 5:57:44and also we have to give here also.
  9483. 5:57:49So now let's clear this and let's run
  9484. 5:57:52once again.
  9485. 5:57:55All right. So now it's drop table create
  9486. 5:57:57table one product is added and you can
  9487. 5:58:00see that you're getting the information
  9488. 5:58:02here that this is the one product. So
  9489. 5:58:04this is from where you're getting this
  9490. 5:58:05is the query and then you're getting
  9491. 5:58:08only one product where it is is active
  9492. 5:58:10because for now we have only one
  9493. 5:58:11product. If you want you can add one
  9494. 5:58:12more product that I will also give you
  9495. 5:58:14as a home world. do one thing instead of
  9496. 5:58:16adding one product here you can add
  9497. 5:58:18multiple products right what you have to
  9498. 5:58:21do in these values right instead of this
  9499. 5:58:23you have to just give comma and then
  9500. 5:58:25paste it one more time and then you just
  9501. 5:58:26close with the semicolon you'll see that
  9502. 5:58:28and for the second one just make this
  9503. 5:58:30one to false and then when you're going
  9504. 5:58:32to run this right so here you're still
  9505. 5:58:34going to get only one result because
  9506. 5:58:35that is only one product which will be
  9507. 5:58:37true I hope you're getting right so try
  9508. 5:58:38to do this one by yourself but for now
  9509. 5:58:41for the basic data types this is more
  9510. 5:58:43than enough so we have learned the ID
  9511. 5:58:45one is the serial
  9512. 5:58:47which is our primary key one is vare we
  9513. 5:58:50have learned text integer begin numeric
  9514. 5:58:52boolean so with this now let's move on
  9515. 5:58:54to the next one where we are going to
  9516. 5:58:56understand some of the other data types
  9517. 5:58:57that is also very very important that
  9518. 5:58:59you can use uh in some situations based
  9519. 5:59:02on your project requirements so let's
  9520. 5:59:03see how we going to do that in the next
  9521. 5:59:05All right so moving to the next one so
  9522. 5:59:06let's create the next file and this will
  9523. 5:59:09be other
  9524. 5:59:11data typesql
  9525. 5:59:14Okay. So let's first create it. So I'm
  9526. 5:59:16just going to do drop table
  9527. 5:59:19if exists. And I'm just going to do
  9528. 5:59:22again basics. This is our schema. And
  9529. 5:59:24let's give this one as app events or
  9530. 5:59:26something. And I'm just going to do
  9531. 5:59:28create table. And I'll do basics dot app
  9532. 5:59:32events. And here first of all what I
  9533. 5:59:35will do right? So we are going to create
  9534. 5:59:38our id as a uid. Okay. So how we are
  9535. 5:59:41going to do that? So what actually first
  9536. 5:59:43we need to understand what is UU ID
  9537. 5:59:46means so it's a unique identifier type
  9538. 5:59:48okay and here just like we have the
  9539. 5:59:51serial right so this is not a simple 1 2
  9540. 5:59:543 that you added three data and now you
  9541. 5:59:56are adding the fourth data so the ID
  9542. 5:59:58will become four so what will happen
  9543. 6:00:00right it the values will look like a
  9544. 6:00:02certain format okay and how we are going
  9545. 6:00:04to create it so you have something
  9546. 6:00:06called generate random UID okay which
  9547. 6:00:09will going to create your random UID
  9548. 6:00:12automatically. So how we are going to do
  9549. 6:00:13that? So here we have ID and I'm just
  9550. 6:00:16going to give this one as EU id and here
  9551. 6:00:19so we're going to give this one primary
  9552. 6:00:21key and you have to give a default and
  9553. 6:00:23in this default you have to give this
  9554. 6:00:24one as uh gen and then this will be
  9555. 6:00:27random and then you're going to do UID
  9556. 6:00:29and this is going to create a unique UID
  9557. 6:00:32for this particular column. Okay. Now
  9558. 6:00:35next let's take maybe we have a app
  9559. 6:00:37events right? So let's take a event
  9560. 6:00:39name. So I'll do event name and this one
  9561. 6:00:42I'm not explaining. So this is text and
  9562. 6:00:44I'm just going to do not null. And now
  9563. 6:00:46what we're going to do right we'll
  9564. 6:00:48understand one more new uh data type
  9565. 6:00:50which is called JSON B. So what this
  9566. 6:00:53JSON B means right now this is going to
  9567. 6:00:56store your JSON data and this is going
  9568. 6:00:58to store if I'm not wrong it's going to
  9569. 6:01:00store in a binary format or binary
  9570. 6:01:02optimized format. Okay. when you need to
  9571. 6:01:05have some flexible structured metadata
  9572. 6:01:07for that particular column, you should
  9573. 6:01:08take this particular type. Okay. Now
  9574. 6:01:10let's I'm just going to have a metadata
  9575. 6:01:12and this metadata will be my type of
  9576. 6:01:14JSON B and you're going to give a
  9577. 6:01:16default value and in this default value
  9578. 6:01:17I'm going to tell that okay this will be
  9579. 6:01:19my default value for this particular
  9580. 6:01:21metadata and this will be of type JSON
  9581. 6:01:24B. I hope this is clear. Okay. So now
  9582. 6:01:28next also I think time stamp we have
  9583. 6:01:30already discussed still let's take it.
  9584. 6:01:31So I'm just going to do create a and
  9585. 6:01:33this will be a time stamp and I'm just
  9586. 6:01:36going to give a default and this will be
  9587. 6:01:38now. So which will be the current time
  9588. 6:01:40and I think this is done. So now this is
  9589. 6:01:42going to create our table. So this will
  9590. 6:01:44be our this app events table. So if I
  9591. 6:01:47save this and now let's go here. Let's
  9592. 6:01:50run this. And now let's copy this file
  9593. 6:01:54name first. we'll do slowly and then
  9594. 6:02:01okay so it's created the table now what
  9595. 6:02:03I'll do right so let's go here and then
  9596. 6:02:07I'm just going to run this and also I'm
  9597. 6:02:09just going to move this one here and
  9598. 6:02:12then I'll be inside this and here what
  9599. 6:02:14I'll do right so we have the DT and okay
  9600. 6:02:18so we need to I think I always make this
  9601. 6:02:20mistake we need to do DT / DT and we
  9602. 6:02:24have to do basics because this is our
  9603. 6:02:25schema dot start and this is going to
  9604. 6:02:28give you all the uh table. So you can
  9605. 6:02:30see that we have app events just now we
  9606. 6:02:31have created product basics and the
  9607. 6:02:34student and the same thing you can also
  9608. 6:02:36check here also right if you go here and
  9609. 6:02:38let's see if I refresh this I don't know
  9610. 6:02:39why this is this much slow man it's very
  9611. 6:02:41irritating see so if I refresh this
  9612. 6:02:43right you'll be able to see all the
  9613. 6:02:44updated tables you have app events and
  9614. 6:02:46you can go here and you can explore all
  9615. 6:02:48of this whatever columns that you have
  9616. 6:02:50created okay id event name metadata
  9617. 6:02:54created that is fine so now what I'm
  9618. 6:02:56going to do right so first of all let's
  9619. 6:02:58insert one of uh maybe let's insert two
  9620. 6:03:02data here. So what I'm going to do
  9621. 6:03:04simply here only I'm just going to do
  9622. 6:03:05again we can do the same thing from here
  9623. 6:03:07also I'm repeating the same same thing
  9624. 6:03:09again and again the reason why I'm
  9625. 6:03:11adding so that it will be easy for you
  9626. 6:03:12to copy paste or else what you need to
  9627. 6:03:14do right if I write all of these
  9628. 6:03:16directly in command line not in this
  9629. 6:03:18file so when you download the source
  9630. 6:03:20code these queries will not be present
  9631. 6:03:22you're getting uh what I'm explaining
  9632. 6:03:24here so that is the reason I'm just
  9633. 6:03:26writing all of this here so I'm just
  9634. 6:03:28going to do insert into and I'm just
  9635. 6:03:31going to do basics dot app events and
  9636. 6:03:34here I'm just going to do the event name
  9637. 6:03:35because we have the event name and we
  9638. 6:03:37have to add the metadata correct so we
  9639. 6:03:39have the metadata and here I'm just
  9640. 6:03:41going to do values and let's add
  9641. 6:03:44so here I'm just going to do maybe event
  9642. 6:03:46right so maybe let's say sign up is one
  9643. 6:03:48of the method and here now you have to
  9644. 6:03:50add a JSON so this will be JSON property
  9645. 6:03:53and maybe let's I'm just going to do
  9646. 6:03:54sign up and this user sign up from uh
  9647. 6:03:58Chrome browser something like this and
  9648. 6:04:00so this will be browser and And this
  9649. 6:04:02will be Chrome. Okay. So this will be
  9650. 6:04:04your format. This is how we need to take
  9651. 6:04:06it. Now let's see. I'm just going to
  9652. 6:04:07copy this and then copy uh comma paste
  9653. 6:04:11and then I'm just going to do sign in.
  9654. 6:04:13And here I'm just going to do maybe
  9655. 6:04:15browse and install browser. I'm just
  9656. 6:04:17going to do username or whatever you
  9657. 6:04:19want to take user and then I'm just
  9658. 6:04:22going to take this one as someone. Okay,
  9659. 6:04:24it can be anything. So this is now going
  9660. 6:04:27to insert that into that particular uh
  9661. 6:04:30app event table. So now to check it I'm
  9662. 6:04:34just going to do a select query start
  9663. 6:04:36and then I'm just going to do from and
  9664. 6:04:38then I'll do basics do app event. This
  9665. 6:04:40is the table name. So first let's do
  9666. 6:04:42this and let's run it once again.
  9667. 6:04:45So here I'm just going to run this and
  9668. 6:04:48you can see that so we have created this
  9669. 6:04:50inserted and you can see that we getting
  9670. 6:04:52UID. This is the UID. This is the format
  9671. 6:04:54that I'm talking about. This is the
  9672. 6:04:56event name sign up and this is the
  9673. 6:04:57metadata and then you have the time
  9674. 6:04:59stamp. Okay. And this is the second one
  9675. 6:05:02for sign in. So what we are
  9676. 6:05:03understanding that whether it is
  9677. 6:05:05creating the UID not it's actually
  9678. 6:05:06successfully created this UID. Now what
  9679. 6:05:09you can do right you can also query
  9680. 6:05:11these JSON properties also or JSON field
  9681. 6:05:14also. How? So now let's say uh maybe
  9682. 6:05:17what I'm going to do right I have this
  9683. 6:05:19browser. Correct? So now this browser I
  9684. 6:05:22wanted to check that what I wanted to do
  9685. 6:05:25here that I want to open my JSON B
  9686. 6:05:27column named metadata right so we have
  9687. 6:05:30given the metadata right and now here
  9688. 6:05:32let's I wanted to get the value of this
  9689. 6:05:34browser this browser as text so how I'm
  9690. 6:05:38going to do that to do this what you
  9691. 6:05:40have to do right so you have to do
  9692. 6:05:41select and then I'm just going to give
  9693. 6:05:43an event name
  9694. 6:05:46now this is the important part so you
  9695. 6:05:47have to give metadata and here you're
  9696. 6:05:49going to give like this. So now this
  9697. 6:05:52metadata I'm simply telling that okay
  9698. 6:05:53you have to find this browser property.
  9699. 6:05:56Okay so we going to give this one
  9700. 6:05:58browser. So it's going to find and then
  9701. 6:06:00I'm just going to do give me as browser.
  9702. 6:06:05Then this is not it because you have to
  9703. 6:06:07always give your destination that from
  9704. 6:06:09where you are going to find this. So you
  9705. 6:06:10have to give from table name. We'll do
  9706. 6:06:12basics.app events and here we are going
  9707. 6:06:15to simply mention where metadata
  9708. 6:06:20is browser.
  9709. 6:06:23Okay, that's it. So what we are simply
  9710. 6:06:26telling here this metadata uh this arrow
  9711. 6:06:30browser means that you have to open this
  9712. 6:06:32JSON column named as metadata and get
  9713. 6:06:35the value of the browser as text. All
  9714. 6:06:38right. So now let's test it. So first
  9715. 6:06:40let's open this and I'm going to clear
  9716. 6:06:42this and also in the meantime let's do
  9717. 6:06:45one thing let's comment out this one.
  9718. 6:06:47Okay so I only wanted to check this this
  9719. 6:06:49part. So here what I'll do let's uh
  9720. 6:06:53sorry not clear let's run it once again
  9721. 6:06:56and you can see that what you're getting
  9722. 6:06:57we are getting sign up and you're
  9723. 6:06:59getting chrome. Okay so now what is
  9724. 6:07:01happening here you're getting the sign
  9725. 6:07:03up and this is chrome. Why? because we
  9726. 6:07:05are clearly telling here that you have
  9727. 6:07:07to open the JSON B column named metadata
  9728. 6:07:10and you have to get the value of browser
  9729. 6:07:13as text. So this is how you can do this.
  9730. 6:07:15Okay. So this is about the other data
  9731. 6:07:18types. Now next we have another very
  9732. 6:07:20very important and that is how we are
  9733. 6:07:21going to
  9734. 6:07:23work with null, empty, string and zero
  9735. 6:07:26value. Okay. So we have null, empty,
  9736. 6:07:28string and zero value and all three are
  9737. 6:07:31not same. So let's see how uh we are
  9738. 6:07:33going to do that. So first of all I'm
  9739. 6:07:34just going to do 06 maybe and let's do
  9740. 6:07:37null
  9741. 6:07:38empty string
  9742. 6:07:41uh something like this or let's do
  9743. 6:07:45zero also 0.sql is given. Okay. So first
  9744. 6:07:50of all let's clear this.
  9745. 6:07:54I'll close everything else. So first we
  9746. 6:07:57have null, right? So null means unknown
  9747. 6:08:02or missing value. Next we have empty
  9748. 6:08:06string. Empty string will be what? known
  9749. 6:08:11string value but no characters or but it
  9750. 6:08:16contains no characters
  9751. 6:08:20and then at the end you have the zero.
  9752. 6:08:22So zero will be your actual numeric
  9753. 6:08:25value, right? So actual numeric
  9754. 6:08:29value of zero. Okay. So let's create one
  9755. 6:08:34more uh here. So I'm just going to
  9756. 6:08:35copyright instead of rewriting. So copy
  9757. 6:08:37this, paste it here and this I'm just
  9758. 6:08:40going to do value examples something
  9759. 6:08:43like this and then I'm simply going to
  9760. 6:08:45create. So do create table basics dot
  9761. 6:08:49value examples and here I'm just going
  9762. 6:08:52to do so now I'm doing little faster not
  9763. 6:08:55already you have done multiple times. So
  9764. 6:08:56do serial so primary
  9765. 6:09:00key and then after this maybe let's take
  9766. 6:09:04what is it? Let's take a nickname. So
  9767. 6:09:06we'll do nickname which will be text and
  9768. 6:09:09then I'm also going to take a bio which
  9769. 6:09:12also will be text and let's take a score
  9770. 6:09:16and this one I'm just going to do
  9771. 6:09:17integer. Okay. So we have these three
  9772. 6:09:20that's it. So now let's do one thing
  9773. 6:09:22what we are going to do right here. You
  9774. 6:09:24have to understand that when we are
  9775. 6:09:25going to insert the value then you can
  9776. 6:09:27clearly understand what is happening
  9777. 6:09:29here. And I'm going to make these things
  9778. 6:09:30very very simple and easy to understand.
  9779. 6:09:33So I'm just going to do insert. I'll do
  9780. 6:09:34into and I'll do basics dot value
  9781. 6:09:37examples. So we have nickname, we have
  9782. 6:09:40bio should be in that order. Okay. And
  9783. 6:09:42then we have score. Correct? And then
  9784. 6:09:44we're going to add values. Now you see
  9785. 6:09:46what we are doing here. So for the first
  9786. 6:09:48one, notice for the nickname I'm passing
  9787. 6:09:51explicitly as null. Okay. And then I'm
  9788. 6:09:53just going to do here by learning rej
  9789. 6:09:58or maybe learning posgress.
  9790. 6:10:01Right? And then I'm passing the score.
  9791. 6:10:03I'll pass in 10. So this is my first
  9792. 6:10:05one. Now here clearly I'm mentioning
  9793. 6:10:08that nickname is null.
  9794. 6:10:11Nickname is null. Right?
  9795. 6:10:14Uh also let's move it here. So this will
  9796. 6:10:17be the first one. First condition. Next
  9797. 6:10:19what I'm going to do give comma and I'm
  9798. 6:10:22going to add one more property. Sorry uh
  9799. 6:10:24row here. Here I'm explicitly mentioning
  9800. 6:10:27that nickname is empty string. See, I'm
  9801. 6:10:29giving here empty string and I'm just
  9802. 6:10:31going to do empty nickname
  9803. 6:10:34and let's give 20 and then at the end uh
  9804. 6:10:38what I'm going to do right so let's do
  9805. 6:10:41here some names I'm just going to do sum
  9806. 6:10:44and for the second one I'm going to pass
  9807. 6:10:46empty and here for this score I'm giving
  9808. 6:10:49explicitly zero so here what this is
  9809. 6:10:52this means the score is zero this is not
  9810. 6:10:54missing there is a huge difference
  9811. 6:10:56missing means it's a null value. Zero
  9812. 6:10:59means you're telling that okay he got
  9813. 6:11:01zero in the exam. So it's a real number.
  9814. 6:11:03Okay. So this is the difference. And
  9815. 6:11:05then you can also pass multiple as a
  9816. 6:11:07null also. Let's say you are passing
  9817. 6:11:09here uh one more here that this guy is
  9818. 6:11:12John and his bio is null and his score
  9819. 6:11:16is also null. This also you can do.
  9820. 6:11:19Okay. And this is done. So now this is
  9821. 6:11:21going to add these four. Now what I
  9822. 6:11:23going to do right? First of all, let's
  9823. 6:11:24say I'm now from this point, right? We
  9824. 6:11:27have already done all the queries and
  9825. 6:11:29everything multiple times. You should be
  9826. 6:11:31able to write some of the basic queries.
  9827. 6:11:33For example, now I want to query all
  9828. 6:11:35these values from my DB. How I'm going
  9829. 6:11:36to do that or from that column? So, it's
  9830. 6:11:38very simple, right? I'm just going to do
  9831. 6:11:39select
  9832. 6:11:41uh star. So, star and then I'm just
  9833. 6:11:44going to do what? From so to give basics
  9834. 6:11:47dot value examples. So, this will be the
  9835. 6:11:50first thing. So if I save this and then
  9836. 6:11:53let's say I'm just going to run it here.
  9837. 6:11:56So this will be the null empty sorry
  9838. 6:12:03and let's remove this
  9839. 6:12:07and you can see that these two null this
  9840. 6:12:10also whenever you're getting empty or
  9841. 6:12:11null you're getting those columns are
  9842. 6:12:13empty right so that mean this is working
  9843. 6:12:15fine now let's say you only want to
  9844. 6:12:18check that maybe I want to check that
  9845. 6:12:20give me the records where the nickname
  9846. 6:12:23is only null so how you can do that so
  9847. 6:12:25I'm going to comment it out here. So I'm
  9848. 6:12:27just going to tell that okay select star
  9849. 6:12:30from
  9850. 6:12:32basics do app sorry not app value
  9851. 6:12:35examples and then here I'm simply going
  9852. 6:12:37to do that where nickname
  9853. 6:12:42will be what is null simply so this is
  9854. 6:12:46going to give me that okay give me from
  9855. 6:12:47this table where the nickname is null so
  9856. 6:12:49this is going to give me all the
  9857. 6:12:50examples where the nickname is null and
  9858. 6:12:52you can run it once again so let's say
  9859. 6:12:55if I just run it Here you can see that
  9860. 6:12:57we have only one where the nickname is
  9861. 6:12:59null. Okay. Then you can also do maybe
  9862. 6:13:02let's say you want to see where the
  9863. 6:13:04nickname is empty. So I'm just going to
  9864. 6:13:06do this will be a homework that write
  9865. 6:13:08one query
  9866. 6:13:11to find where nickname is empty string.
  9867. 6:13:18So this will be your homework. Okay. Try
  9868. 6:13:20to do it by yourself.
  9869. 6:13:22Now let's say you want to check whe the
  9870. 6:13:24score is only zero. So to do this you
  9871. 6:13:26can do select star. So I do from basics
  9872. 6:13:30dot value examples and then you're going
  9873. 6:13:33to do where score will be equal to what?
  9874. 6:13:36Zero. That's it. So this is going to
  9875. 6:13:37give you where the column the score is
  9876. 6:13:40zero. And if you run this here once
  9877. 6:13:41again
  9878. 6:13:44and you can see that you're getting this
  9879. 6:13:46right. So that means this is working
  9880. 6:13:49fine. And this is how you have to use.
  9881. 6:13:52Now you also can use reverse way that
  9882. 6:13:55you want to check whe the nickname is
  9883. 6:13:57not null. Okay, I'm going to repeat once
  9884. 6:14:00again. You want to check whe the
  9885. 6:14:01nickname is not null. So how you can do
  9886. 6:14:03that? Copy this exact thing. Paste it
  9887. 6:14:06here and you're going to do is not null.
  9888. 6:14:08Okay, so this is where it's going to
  9889. 6:14:10give you the reverse uh result. So here
  9890. 6:14:13I'm just going to do clear and let's run
  9891. 6:14:15once again.
  9892. 6:14:17You can see that you're getting nickname
  9893. 6:14:20is not null. Okay. Now here okay first
  9894. 6:14:23we are getting three but for the first
  9895. 6:14:25one it is null right. How many we have
  9896. 6:14:28added? Let me just check. Okay we have
  9897. 6:14:30added four correct. So it should be
  9898. 6:14:32getting three because first one is
  9899. 6:14:33explicitly null second was not empty. So
  9900. 6:14:37this is different. Okay. So here it's
  9901. 6:14:39going to give you that where it is not
  9902. 6:14:41null. I hope this is clear right? So we
  9903. 6:14:44are not going to use equal to null. we
  9904. 6:14:45are going to use if not null something
  9905. 6:14:47like this. So I think this is all what
  9906. 6:14:50else uh I think I think yeah okay one
  9907. 6:14:55more thing I wanted to mention I think
  9908. 6:14:56that part we have covered right the
  9909. 6:14:58empty one we have covered is null we
  9910. 6:15:00have covered yeah I think we have
  9911. 6:15:01covered almost all the things so try to
  9912. 6:15:04practice this one the reverse one that
  9913. 6:15:06when you want to check that if there is
  9914. 6:15:0810 products or 10 uh examples that rows
  9915. 6:15:12that are added in that particular table
  9916. 6:15:14so there can be two scenarios you can
  9917. 6:15:16check you want to check the products
  9918. 6:15:19where it is null. So you're going to do
  9919. 6:15:20where that particular key name is null.
  9920. 6:15:23If you want to check the reverse, you
  9921. 6:15:24have to give where the key proper key
  9922. 6:15:26name is not null. Okay, it's going to
  9923. 6:15:28give you the reverse order. So with this
  9924. 6:15:30now, let's move on to the next one. Next
  9925. 6:15:31one, we're going to understand what is
  9926. 6:15:33constants. Okay, and this is also very
  9927. 6:15:34very important. All right. So moving to
  9928. 6:15:36the next one. Another important concept
  9929. 6:15:38which will be constants, right?
  9930. 6:15:42Uh let's do this one as SQL.
  9931. 6:15:46All right. So close this. So we have
  9932. 6:15:48different column constants. Okay. So we
  9933. 6:15:51have something like I think we have
  9934. 6:15:52already discussed not null. Then we have
  9935. 6:15:55unique
  9936. 6:15:57then you have default then you have
  9937. 6:15:59check all these right now these all are
  9938. 6:16:03database rules. Now why this is very
  9939. 6:16:05very powerful because it's going to
  9940. 6:16:08protect your data and user will not be
  9941. 6:16:10able to pose wrong data to D. You can do
  9942. 6:16:14backend validation because backend
  9943. 6:16:16validation always you need to do it. But
  9944. 6:16:18remember one thing and this is also very
  9945. 6:16:20important concept that you should know
  9946. 6:16:21if you're a beginner let's you are
  9947. 6:16:23working in sorry you are studying in
  9948. 6:16:24college that even though backend
  9949. 6:16:26validations are good but database
  9950. 6:16:29constants are always always stronger
  9951. 6:16:31than backend validation. Why? Because
  9952. 6:16:33that is going to directly protect the
  9953. 6:16:35data even let's say uh you are writing a
  9954. 6:16:39different app or script or developer
  9955. 6:16:41whatever they are doing. So you're
  9956. 6:16:43you're understanding what I'm saying. So
  9957. 6:16:46you have a back end logic or back end
  9958. 6:16:48validation logic in your back end code.
  9959. 6:16:50Now let's say you don't have any
  9960. 6:16:52constants in your DB. Now if we can
  9961. 6:16:55write in our DB in different ways,
  9962. 6:16:57right? So what I mean by this, we can
  9963. 6:16:59write in a different ways. You can write
  9964. 6:17:00from your app. So let's say you're doing
  9965. 6:17:02some API call and API call is writing
  9966. 6:17:04into your DB. You can also write uh data
  9967. 6:17:07into DB using script or directly
  9968. 6:17:10developer can go to DB and they can
  9969. 6:17:12write makes sense right easily we can do
  9970. 6:17:14it but let's say if you have constant on
  9971. 6:17:17the DB level in all scenarios if you're
  9972. 6:17:20writing a wrong data it's going to fail
  9973. 6:17:23so that is the reason backend validation
  9974. 6:17:25sorry database constants are always
  9975. 6:17:27always always stronger than backend
  9976. 6:17:29validation doesn't mean that I'm telling
  9977. 6:17:31that you don't have to write backend
  9978. 6:17:32validations you have to write but both
  9979. 6:17:34are equally important important but
  9980. 6:17:35database constants are much stronger
  9981. 6:17:37okay whenever you'll be working with
  9982. 6:17:39databases so first let's do one thing so
  9983. 6:17:41I'm just going to do here let me just
  9984. 6:17:42copy from here so I'm just going to copy
  9985. 6:17:45this paste it here and let's give this
  9986. 6:17:47one name as basics dot let's create
  9987. 6:17:49accounts and here I'm just going to do
  9988. 6:17:51create table and I'm just going to do
  9989. 6:17:54basics dot accounts and let's add ID
  9990. 6:18:00let's give this one as primary Okay,
  9991. 6:18:05let's add a full name. So I'm just going
  9992. 6:18:08to do text and this will be not null.
  9993. 6:18:11Okay, so this is one of the constant. So
  9994. 6:18:14not null means we're simply telling that
  9995. 6:18:15full name is required and postgress is
  9996. 6:18:18going to reject whenever we going to
  9997. 6:18:20insert and we'll see that if the full
  9998. 6:18:22name is missing as simple as that. What
  9999. 6:18:24else? Uh I think unique we have already
  10000. 6:18:26discussed right still let's do it. So
  10001. 6:18:28I'll do email. This will be text. And
  10002. 6:18:31I'm just going to do not null. And this
  10003. 6:18:33will be unique. And also we are going to
  10004. 6:18:36give a default value. So I'm just going
  10005. 6:18:38to do isore active. And I'm just going
  10006. 6:18:41to do this one as boolean. And this will
  10007. 6:18:43be default. And I'm just going to give
  10008. 6:18:45this one as true. Okay. And check also
  10009. 6:18:48we have discussed previously. So I'll do
  10010. 6:18:50integer. And I'm just going to take a
  10011. 6:18:52check. And here I'm just going to give
  10012. 6:18:54age should be greater than or equal 80.
  10013. 6:18:58Okay. And let's take the last one which
  10014. 6:19:00will be created at and here I'll do this
  10015. 6:19:05one as time stamp. We also have a time
  10016. 6:19:07stamp Z. There is one more property and
  10017. 6:19:10then we're going to take this one as
  10018. 6:19:12now. Okay. So this is going to create
  10019. 6:19:14our table. All right. Now let's do some
  10020. 6:19:17of the valid insert. Okay. So valid
  10021. 6:19:19cases. So let's I'm just going to do
  10022. 6:19:21insert into
  10023. 6:19:25here into. So I'll do basics dot
  10024. 6:19:28accounts. So we have full name. We have
  10025. 6:19:31email and let's do age is active we
  10026. 6:19:34don't have to do because it's given as
  10027. 6:19:36default. And I'm just going to do
  10028. 6:19:38values. And I'm just going to add my
  10029. 6:19:39name here.
  10030. 6:19:42And let's give the email.
  10031. 6:19:46And I'm just going to give the age.
  10032. 6:19:48Let's do 11. Okay. That's it.
  10033. 6:19:52So now let's save this. And we are going
  10034. 6:19:54to run this file.
  10035. 6:19:56Okay. So, let's copy this
  10036. 6:20:02and we'll go here and then I'm just
  10037. 6:20:05going to replace this.
  10038. 6:20:11Okay. What is telling that no new row
  10039. 6:20:13for relation while check constant
  10040. 6:20:16account? Oh, see we are already getting
  10041. 6:20:17added. Fantastic. Because we have given
  10042. 6:20:19this one as 11, right? So, it should be
  10043. 6:20:21greater than 20 uh sorry 18. So we have
  10044. 6:20:24to give this one as 20. All right. So
  10045. 6:20:26it's already working. You can see that.
  10046. 6:20:29And I didn't give this one
  10047. 6:20:30intentionally. So I missed it. So now it
  10048. 6:20:32should be able to. So you can see that
  10049. 6:20:33it's working, right? And if you want to
  10050. 6:20:35get it the data, what you can do? Simply
  10051. 6:20:38do select start from and you'll do
  10052. 6:20:40basics dot accounts, right? And this is
  10053. 6:20:43going to give you the information.
  10054. 6:20:44Again, I'm repeating the same thing you
  10055. 6:20:46can do from where? From your ESQL uh
  10056. 6:20:49terminal also, right? So if you run
  10057. 6:20:51this, you can see that you're getting
  10058. 6:20:52one data. Fantastic. Right. So now what
  10059. 6:20:55you're going to do, right? So see what I
  10060. 6:20:57mean. Right. So you can directly do from
  10061. 6:20:58your uh this psq alg. So let's see if I
  10062. 6:21:01clear this and then I'm just going to go
  10063. 6:21:04here copy this.
  10064. 6:21:07Okay. Now inside this let's say you want
  10065. 6:21:10to get the data from this accounts
  10066. 6:21:11table. So what you can do? You can do
  10067. 6:21:12directly just select start and then
  10068. 6:21:15you're going to do from and you'll do
  10069. 6:21:17basics,
  10070. 6:21:18right? And then you'll do accounts.
  10071. 6:21:20That's it. And if you enter here, you're
  10072. 6:21:22going to get the same result. So now
  10073. 6:21:24you're understanding why I'm not doing
  10074. 6:21:26here all the things because if I do
  10075. 6:21:28here, you will not be able to get it
  10076. 6:21:30from the source code. So here you'll be
  10077. 6:21:32able to see the exact queries, right? So
  10078. 6:21:33that is the reason. But these are
  10079. 6:21:35optional. These are not needed. I'm
  10080. 6:21:37repeating this thing so many times
  10081. 6:21:38because some of you can be quite
  10082. 6:21:40confused that why he is writing these
  10083. 6:21:42queries inside this file. That is
  10084. 6:21:44actually intentional. So I'll clear
  10085. 6:21:47this. So this is working fine. Now let's
  10086. 6:21:49say I'm going to give some invalid
  10087. 6:21:50scenarios now. So what I mean by invalid
  10088. 6:21:52scenario so that we can check. So first
  10089. 6:21:54one will be let's say I I I I will be
  10090. 6:21:57adding the email and age. I'm not going
  10091. 6:21:59to add the full name. But if you see the
  10092. 6:22:02rule right full name is not null. So
  10093. 6:22:04that means we need to add it. So this
  10094. 6:22:06should fail and you'll see that it is
  10095. 6:22:08going to fail. So I'm just going to
  10096. 6:22:09comment it out from for uh now copy this
  10097. 6:22:12comment it out and then let's paste it
  10098. 6:22:15here and then I'm just going to remove
  10099. 6:22:17the full name. I'm going to keep the
  10100. 6:22:18email and I will just give here missing
  10101. 6:22:23name at thegmail.com 20. And then if you
  10102. 6:22:26try to run this and you'll see that this
  10103. 6:22:28is going to fail for sure. And you can
  10104. 6:22:31see that this full name null value in
  10105. 6:22:33the column full name relation accounts
  10106. 6:22:35violates not null constant. Correct? So
  10107. 6:22:38this is another fail case scenario. What
  10108. 6:22:41else? All right. So let's add one more
  10109. 6:22:43case. So what we can do right maybe I'm
  10110. 6:22:44just going to okay let's copy this
  10111. 6:22:48and I'm going to comment it here. Let's
  10112. 6:22:50remove this comment and I'm going to
  10113. 6:22:52paste it one more time. Okay. And see
  10114. 6:22:54what we are doing here. So I have sa
  10115. 6:22:56mukari sangamgmail.com
  10116. 6:22:58and I'm going to take uh duplicate
  10117. 6:23:02email user and I'm going to keep the
  10118. 6:23:05same email. Maybe let's do this one as
  10119. 6:23:0723. So this also should fail. Why?
  10120. 6:23:10because we have two user with same
  10121. 6:23:12email. Okay. So let's run this and you
  10122. 6:23:16can see that we have duplicate key for
  10123. 6:23:17this unique. So account email key. This
  10124. 6:23:20is also another constant. Okay. So
  10125. 6:23:22similarly you can check multiple
  10126. 6:23:24scenarios here. For example, let's say I
  10127. 6:23:27we have already checked right. So if you
  10128. 6:23:28give the age less than 18 so it's going
  10129. 6:23:31to fail. For example, let's say I'm
  10130. 6:23:32going to do sum one. But at the same
  10131. 6:23:34time, I'm going to make this one as
  10132. 6:23:36three or maybe let's say uh five.
  10133. 6:23:38Whatever you want to do, this is also
  10134. 6:23:40going to fail, right? So let's say clear
  10135. 6:23:42this and try to push. And you can see
  10136. 6:23:46that what you're getting the account age
  10137. 6:23:48check constant. So this is failing. Why?
  10138. 6:23:50Because the age is five and in the
  10139. 6:23:52constant we have clearly given the check
  10140. 6:23:53should the age should be greater than or
  10141. 6:23:55equal to 80.
  10142. 6:23:58So this is the concept of constant. So
  10143. 6:24:00this is how you need to use right. So I
  10144. 6:24:02think we have covered all the things and
  10145. 6:24:04one thing we need to understand and that
  10146. 6:24:06is uh some more about primary keys that
  10147. 6:24:08we are going to discuss in the next part
  10148. 6:24:10and then we'll move to the part two.
  10149. 6:24:12Why? Because for part one we have almost
  10150. 6:24:14covered most of the things and in
  10151. 6:24:16details right I'm pretty sure you will
  10152. 6:24:17not find this kind of explanation in
  10153. 6:24:19paid courses also because I'm explaining
  10154. 6:24:21each and everything slowly and once you
  10155. 6:24:23do the practice right you'll be able to
  10156. 6:24:25understand you'll not going to forget
  10157. 6:24:26you're not going to believe me that when
  10158. 6:24:28I usually learn all these things right I
  10159. 6:24:30took things very very seriously even if
  10160. 6:24:32I learn all these things five years back
  10161. 6:24:34right the way I've learned things I I
  10162. 6:24:36never memorized these things right so
  10163. 6:24:38maybe there is a possibility that I
  10164. 6:24:40won't be able to memor uh like recollect
  10165. 6:24:43the uh let's say the syntax and
  10166. 6:24:45everything because it's not possible for
  10167. 6:24:47everyone to remember everything right
  10168. 6:24:49but once I'll see once right maybe let's
  10169. 6:24:52say 5 years back I've done SQL I'm just
  10170. 6:24:54giving example and after 5 years I'm
  10171. 6:24:55just I need two three days and I'll be
  10172. 6:24:57able to again implement project from the
  10173. 6:25:00next day so this is how you need to
  10174. 6:25:02learn all these things so with this now
  10175. 6:25:03let's move to the next one all right so
  10176. 6:25:05let's finish our part one with little
  10177. 6:25:07more concept that we are going to cover
  10178. 6:25:09that is on primary keys
  10179. 6:25:12So let's create primary is dosql.
  10180. 6:25:17I think this one we've already discussed
  10181. 6:25:18that primary key is going to denotes
  10182. 6:25:20that it's uniquely identifies each row
  10183. 6:25:23in a table. Okay. Now if you don't have
  10184. 6:25:26any primary key what are the things that
  10185. 6:25:28will become difficult. Now these can be
  10186. 6:25:30interview questions also. So if you
  10187. 6:25:32don't primary key it becomes harder to
  10188. 6:25:35update, delete, do any kind of reference
  10189. 6:25:39or even if you want to connect to a
  10190. 6:25:41specific row to rows in other tables.
  10191. 6:25:44All these things can be very very
  10192. 6:25:46tricky. Okay. So that means there has to
  10193. 6:25:48be something unique in each and every
  10194. 6:25:50row so that you'll be able to identify
  10195. 6:25:51that particular row which is what your
  10196. 6:25:54primary keys is going to do. Okay. So
  10197. 6:25:56here now let's do one thing. I'm just
  10198. 6:25:58going to do drop table. I'm just going
  10199. 6:25:59to create a simple table. I'm just going
  10200. 6:26:01to do if exist. So let's do basics dot
  10201. 6:26:04uh what we can do maybe let's do courses
  10202. 6:26:08courses or maybe let's do sales
  10203. 6:26:11then I'm just going to do create table
  10204. 6:26:14and I'm just going to do basics dot
  10205. 6:26:16sales and here I think we have already
  10206. 6:26:19discussed I'm not explaining because we
  10207. 6:26:21have already covered so we have a ID
  10208. 6:26:23which is a serial and this will be my
  10209. 6:26:25primary key okay and then let's say you
  10210. 6:26:29want to do title sorry sales name maybe
  10211. 6:26:32let's do title or whatever you want to
  10212. 6:26:34take. So I'm going to take text. This
  10213. 6:26:36will be not null. And what else you can
  10214. 6:26:39take? Uh maybe let's do one price
  10215. 6:26:42property sales price. And I'm just going
  10216. 6:26:45to do this one as numeric.
  10217. 6:26:49So let's do 10, 2. And here I'm just
  10218. 6:26:52going to do this one as not null. And
  10219. 6:26:54also I'm going to give a default value
  10220. 6:26:55of zero. And let's take one more which
  10221. 6:26:58will be created at. So this will be
  10222. 6:27:00timestamp and I'm going to do default
  10223. 6:27:02and this will be now.
  10224. 6:27:06Okay. Now let's add some value here. So
  10225. 6:27:09I'm just going to do insert into
  10226. 6:27:12basics dot sales. So what are the things
  10227. 6:27:16we have? We have the title and the
  10228. 6:27:17price.
  10229. 6:27:19And let's add here some values.
  10230. 6:27:24So let's add maybe sale one. And I'm
  10231. 6:27:28just going to do cell 200. Let's give
  10232. 6:27:31comma. So I'll do cell two. And let's
  10233. 6:27:35give this one as 500. Something like
  10234. 6:27:37this. Okay, this is done. So save this.
  10235. 6:27:40And let's run it here. So I'm just going
  10236. 6:27:42to clear,
  10237. 6:27:45rename, copy this, and then I'm just
  10238. 6:27:49going to run it. So it's going to create
  10239. 6:27:51the table.
  10240. 6:27:53All right. Fantastic. So now let's say
  10241. 6:27:56first of all I wanted to check that what
  10242. 6:27:58I have here. So I'm just going to do
  10243. 6:27:59select star from sold basics dot cells
  10244. 6:28:06save this and let's run once again.
  10245. 6:28:11So you have two data okay so ID 1 and
  10246. 6:28:14ID2. So till this part this is fine
  10247. 6:28:16right now what we can do right we can
  10248. 6:28:18also query based on the ID. So now let's
  10249. 6:28:21say I only wanted to see the data of
  10250. 6:28:25ID2. So I can simply do your select
  10251. 6:28:27start and you can do from you do basics
  10252. 6:28:30dot sales
  10253. 6:28:32and here you have to give where your ID
  10254. 6:28:34will be equal to two. So this is going
  10255. 6:28:37to give you the second information
  10256. 6:28:38second row information. So if you clear
  10257. 6:28:41and run this you're going to get only
  10258. 6:28:43one. Okay. Now one thing is very
  10259. 6:28:47important. And now let's say I'm going
  10260. 6:28:48to give a invalid scenario here. So
  10261. 6:28:51let's do one thing, right? What we are
  10262. 6:28:53going to do maybe uh okay let's do one
  10263. 6:28:56thing. Let's keep this one like this and
  10264. 6:28:58let's comment it out. And I'm going to
  10265. 6:28:59insert one more data here. Okay. So I'm
  10266. 6:29:02just going to do here. Let's copy this
  10267. 6:29:04only from here. Paste it. And I'm just
  10268. 6:29:07going to do insert into basic uh dot
  10269. 6:29:09quotes. Sorry. And here I'll do ID,
  10270. 6:29:12title, and price. All three things I'm
  10271. 6:29:14adding in the values. Let's I'm going to
  10272. 6:29:16add only one value. But see what I'm
  10273. 6:29:18doing in the ID I'm giving a duplicate
  10274. 6:29:20ID. So here I'm giving one. Why? Because
  10275. 6:29:22see if you notice here this one ID will
  10276. 6:29:25be automatically created before only. So
  10277. 6:29:27here I'm just going to giving it
  10278. 6:29:28duplicate ID and you'll see that this is
  10279. 6:29:30going to fail. Okay. So I'll do
  10280. 6:29:32duplicate
  10281. 6:29:35ID something like this. And if you now
  10282. 6:29:38save this right and see what will
  10283. 6:29:40happen. So I'm just going to clear this
  10284. 6:29:43run it and you can see that your
  10285. 6:29:45duplicate key value violates unique
  10286. 6:29:47constant sales primary key fantastic key
  10287. 6:29:50ID equal to one is already exist. So
  10288. 6:29:52this is a proof that your primary key
  10289. 6:29:55will be always unique and primary key is
  10290. 6:29:57going to denotes that okay this is my
  10291. 6:29:59unique identifier for each row in a
  10292. 6:30:01certain table. So with these the first
  10293. 6:30:04basic things we have covered I think we
  10294. 6:30:06have learned a lot of things these
  10295. 6:30:07things are very very important without
  10296. 6:30:08these we can't move to the next section.
  10297. 6:30:10So with that now let's move on to the
  10298. 6:30:12part two. Okay. All right everyone. So
  10299. 6:30:14now it's time to start on the part two.
  10300. 6:30:16So what actually we are going to cover
  10301. 6:30:17in the part two. So part two will be
  10302. 6:30:19more on more SQL basics and the concepts
  10303. 6:30:21we are going to cover. For example let's
  10304. 6:30:23say what is the concept of so I think
  10305. 6:30:25insert we have already covered in uh
  10306. 6:30:27still we are going to cover insert
  10307. 6:30:28single row multiple rows how you're
  10308. 6:30:31going to select specific columns. Uh
  10309. 6:30:33[snorts] where and basic filters then
  10310. 6:30:35you have and or not right like I like.
  10311. 6:30:39So many things are there. So all those
  10312. 6:30:41things we are going to cover in this
  10313. 6:30:42part two. So for this one what we're
  10314. 6:30:44going to do right we'll be having a
  10315. 6:30:45setup file. So what this setup file is
  10316. 6:30:47going to do this setup file is going to
  10317. 6:30:49create the table and based on this
  10318. 6:30:51particular setup file whatever concept
  10319. 6:30:53that we are going to cover in the next
  10320. 6:30:54section we are going to use this
  10321. 6:30:56particular table as a base table. Okay.
  10322. 6:30:58Now the reason is very simple. So I'm
  10323. 6:31:00going to show you if you search on
  10324. 6:31:03Google right postgress SQL mac and
  10325. 6:31:05everything you'll see that it has lot of
  10326. 6:31:06issues also and it's extremely slow. I
  10327. 6:31:08don't know why I think if you're using
  10328. 6:31:10Windows right you will not face this
  10329. 6:31:12issue. So what I'm trying to explain
  10330. 6:31:13that if you want to visualize all those
  10331. 6:31:15data from your posgress also you can
  10332. 6:31:17easily do that. But as I already
  10333. 6:31:19mentioned that we are going to use a
  10334. 6:31:20terminal only or let's say psql command
  10335. 6:31:23line which is much better. I will say
  10336. 6:31:25which is better because it will give you
  10337. 6:31:27much more better idea how we can write
  10338. 6:31:29commands and everything. So first let's
  10339. 6:31:31start with create. So I'm just going to
  10340. 6:31:33do this one as 0 0 or let's do 01 and
  10341. 6:31:36I'm just going to do SQL concepts
  10342. 6:31:40base file. Okay. So I'll do base files
  10343. 6:31:42SQL. All right. So first let's create
  10344. 6:31:45some of the tables and everything. And
  10345. 6:31:47for this one I'm not going to explain
  10346. 6:31:48each and everything because some of the
  10347. 6:31:50things we have already covered. So let's
  10348. 6:31:52start with first I'm just going to do
  10349. 6:31:53create extension
  10350. 6:31:58extension spelling. Okay. It's correct.
  10351. 6:32:00So I do if not exist and I'm just going
  10352. 6:32:03to take pgto.
  10353. 6:32:06So after this I'm just going to do a
  10354. 6:32:08drop table if exist. Let's give this one
  10355. 6:32:11as products
  10356. 6:32:13and then let's create our table. So I'm
  10357. 6:32:15just going to do create table and I'll
  10358. 6:32:17do this one as products. Okay. Now let's
  10359. 6:32:20see what are the things that we are
  10360. 6:32:21going to take. So first let's take ID
  10361. 6:32:23and remember all these uh properties
  10362. 6:32:25that we are going to take right you have
  10363. 6:32:27to recolct because whenever we are going
  10364. 6:32:29to cover the next concept we are going
  10365. 6:32:31to use this table as a base table. So
  10366. 6:32:33I'm just repeating this thing multiple
  10367. 6:32:34times. This is important. So we are
  10368. 6:32:36going to take this one as UID and I'll
  10369. 6:32:38take primary key. Let's take default and
  10370. 6:32:41for this one I'm just going to do gen
  10371. 6:32:44random UID. I think this we have already
  10372. 6:32:46covered.
  10373. 6:32:48Next let's take a name property. So name
  10374. 6:32:50I'm just going to take this one as text
  10375. 6:32:52not null.
  10376. 6:32:56Next let's take a category. So this also
  10377. 6:32:58I'm going to take as text not null.
  10378. 6:33:03Let's take a price. So for price I'm
  10379. 6:33:05just going to do this one as numeric
  10380. 6:33:08and this I'll do 10A 2 not null and
  10381. 6:33:13let's give a check. So for check I'm
  10382. 6:33:16just going to do price
  10383. 6:33:18has to be greater than or equal zero.
  10384. 6:33:20Okay. So this is one check I'm giving.
  10385. 6:33:22Then let's take a stock.
  10386. 6:33:25I'm just going to do this one as integer
  10387. 6:33:27not null. I'll do default. Let's give
  10388. 6:33:31default as zero and check. So here also
  10389. 6:33:34I'm just going to give the stock also
  10390. 6:33:35has to be positive. So it will be
  10391. 6:33:36greater than or equal zero. Then let's
  10392. 6:33:40take a is active which will be boolean
  10393. 6:33:44not null also. And then I'm just going
  10394. 6:33:46to do a default which will be true.
  10395. 6:33:48Okay. Let's take a skew property and I'm
  10396. 6:33:52just going to do this one as text. But
  10397. 6:33:54for this one I'm going to take this one
  10398. 6:33:55as unique. So for every product there
  10399. 6:33:57has to be one skew which will be unique.
  10400. 6:34:00Then let's take a description.
  10401. 6:34:02This will be text. And at the end we're
  10402. 6:34:04going to take created_.
  10403. 6:34:08And let's take this one as time stamp
  10404. 6:34:10not null and I'll do default. Let's give
  10405. 6:34:13the current time. Okay. So these all the
  10406. 6:34:16properties that we are going to have or
  10407. 6:34:18all the columns for this particular
  10408. 6:34:19table. Okay. So first let's save this
  10409. 6:34:21and let's see if this is working or not.
  10410. 6:34:23So first what I'm going to do right?
  10411. 6:34:25Let's copy this. All right. So now let's
  10412. 6:34:27paste it here. So what we have done the
  10413. 6:34:28same command we just have to go to part
  10414. 6:34:30two and then this is the first file and
  10415. 6:34:32then run. And you see that this is going
  10416. 6:34:34to create the table. And then
  10417. 6:34:35[clears throat] I'll just do clear and
  10418. 6:34:38let's go here and let me quickly refresh
  10419. 6:34:40this.
  10420. 6:34:44See the how much uh slow this is. I
  10421. 6:34:47think they have created this one using
  10422. 6:34:49electron framework. This is what I'm
  10423. 6:34:51thinking. And there must be some issue
  10424. 6:34:52in the math specially. And if you go to
  10425. 6:34:55public
  10426. 6:34:58and then let's go to table. So you can
  10427. 6:35:00see that we have products. And if you go
  10428. 6:35:02to columns
  10429. 6:35:05and you're going to see all these
  10430. 6:35:07columns, right? So this is the columns
  10431. 6:35:08that we have created. Fantastic.
  10432. 6:35:10So now what I'll do, right? So let's
  10433. 6:35:12insert some of the data first. So
  10434. 6:35:16here I'm just going to do insert into
  10435. 6:35:20products.
  10436. 6:35:22So what are the things we have to do? So
  10437. 6:35:23we have the name, we have category, we
  10438. 6:35:25have price, we have uh stock, uh we have
  10439. 6:35:30is active skew and also we have
  10440. 6:35:33description. So these many things we
  10441. 6:35:34need to uh add and then I'm just going
  10442. 6:35:37to take this one as values and then I
  10443. 6:35:40already have some data that I have
  10444. 6:35:41created. Okay, this is the data that I
  10445. 6:35:43wanted to add here. So copy this and
  10446. 6:35:46after this I'm just going to paste it
  10447. 6:35:48here. Okay, so this is what we are going
  10448. 6:35:51to add. So now also let's do one thing.
  10449. 6:35:54Let's run this command so that we can
  10450. 6:35:56see that if these datas are present or
  10451. 6:35:58not. So this is just a simple select
  10452. 6:36:00command. So do select start from
  10453. 6:36:03products.
  10454. 6:36:04Save this and then we going to run once
  10455. 6:36:07again.
  10456. 6:36:09See that we are already getting the data
  10457. 6:36:11here. Right? So this is all the data. So
  10458. 6:36:13now what we need to do right using this
  10459. 6:36:15data now we are going to understand all
  10460. 6:36:16the concepts one by one. So let's clear
  10461. 6:36:19this
  10462. 6:36:22and let's start with the first one. So
  10463. 6:36:24first will be I'm just going to do 0 to
  10464. 6:36:26although with this one we have already
  10465. 6:36:28done but I just wanted to still uh do
  10466. 6:36:30this one more time. So I'm just going to
  10467. 6:36:32do insert single row dosql.
  10468. 6:36:36So how we going to do insert single row?
  10469. 6:36:38I think this you have already know. So
  10470. 6:36:41we just have to do insert into
  10471. 6:36:45the table name. So you have products
  10472. 6:36:46correct and then you have to give all
  10473. 6:36:48the properties. So just now we have
  10474. 6:36:49done. So we have name, we have category,
  10475. 6:36:52we have price, we have stock,
  10476. 6:36:55we have uh skew
  10477. 6:36:58and also let's say I wanted to add
  10478. 6:37:00description. Okay. So these many things
  10479. 6:37:02we have right why is active is not
  10480. 6:37:05needed because we have a default value
  10481. 6:37:06here. Okay. This also have but let's say
  10482. 6:37:08I wanted to so what will happen right?
  10483. 6:37:10If you don't add this is active it will
  10484. 6:37:12automatically take that one as true. So
  10485. 6:37:14this is done. So now what we need to do?
  10486. 6:37:16So let's add values and then let's add
  10487. 6:37:19all the values that we wanted to add. So
  10488. 6:37:22here also one more thing that each value
  10489. 6:37:24that we are going to add that always
  10490. 6:37:26should match or must match the column
  10491. 6:37:29that we have added in the above. This is
  10492. 6:37:31also very very important. So let's I'm
  10493. 6:37:33just going to do here laptop lap laptop
  10494. 6:37:37stand,
  10495. 6:37:38category. Let's give this one as maybe
  10496. 6:37:40electronics.
  10497. 6:37:42Then I'll give comma let's give a price
  10498. 6:37:43of 5,000.
  10499. 6:37:460. Then after this I'm just going to
  10500. 6:37:48give stock of let's say 23. Then I'm
  10501. 6:37:51just going to do skew. Now this will be
  10502. 6:37:53important. Okay. So let's see what are
  10503. 6:37:55the skew that we are using. You see all
  10504. 6:37:56these skew that we are using here.
  10505. 6:37:58Electronics key 001 like this. So here
  10506. 6:38:02maybe I'm just going to do electronics.
  10507. 6:38:04So which one we can do? Electronics key
  10508. 6:38:06002. Something like this. If it is
  10509. 6:38:08you're giving wrong or if it is not
  10510. 6:38:10unique then it's going to fail. So here
  10511. 6:38:12I'm just going to do electronics key 002
  10512. 6:38:17then I'm just going to give a
  10513. 6:38:18description. So let's give this one as
  10514. 6:38:20laptop
  10515. 6:38:22stand description.
  10516. 6:38:25Okay so this is done. So now first of
  10517. 6:38:27all what we going to do right? So I'm
  10518. 6:38:29just going to run this command. So first
  10519. 6:38:31I'll copy this rename this. Sorry not
  10520. 6:38:34rename. I'm going to copy this and then
  10521. 6:38:36I'm going to run this here. Again, I
  10522. 6:38:38already mentioned you can run the same
  10523. 6:38:39thing in in your psql also.
  10524. 6:38:44Okay, we are getting no y.
  10525. 6:38:50Oh man, I have to put this one inside.
  10526. 6:38:54So, let's try once again.
  10527. 6:38:57So, you can see that inserting is done.
  10528. 6:38:59So, now this will be your homework from
  10529. 6:39:01this part because we have already
  10530. 6:39:02covered. Now let's say I only wanted to
  10531. 6:39:05check that if this data is present or
  10532. 6:39:07not. Right? So here what I need to do so
  10533. 6:39:10let's I'm just going to do select star
  10534. 6:39:12then I'm just going to do from I have to
  10535. 6:39:15give this products and then I need to to
  10536. 6:39:17check so I need to give a where where
  10537. 6:39:19the skew will be what the skew that I
  10538. 6:39:21have given here because this is always
  10539. 6:39:22unique. So this is going to give me the
  10540. 6:39:24correct information always right and if
  10541. 6:39:26I save this and let's say if I run it
  10542. 6:39:28here so it should give me that
  10543. 6:39:30particular result right what is syntax
  10544. 6:39:33error oh you have to give this sorry my
  10545. 6:39:36bad save clear and let's run again you
  10546. 6:39:40can see that what you're getting
  10547. 6:39:42duplicate key while let's this one this
  10548. 6:39:44is already exist okay this we have
  10549. 6:39:45already added my bad so here we just
  10550. 6:39:47have to comment it out save it and now
  10551. 6:39:50let's run it again and then you're going
  10552. 6:39:53going to get one result. Okay. So you
  10553. 6:39:54see that you're getting this result.
  10554. 6:39:57So you have this UID laptop stand
  10555. 6:40:00electronics 523. This is true. This is
  10556. 6:40:03your skew 002 laptop stand description.
  10557. 6:40:06This is your time stamp. So that means
  10558. 6:40:08this is working fine. You're getting why
  10559. 6:40:10you got that particular error because
  10560. 6:40:12we've already added this and then we try
  10561. 6:40:14to run this particular command but in
  10562. 6:40:16before it's try to execute this
  10563. 6:40:18particular query once again and then it
  10564. 6:40:21check that okay there is a key that is
  10565. 6:40:23already present because we already done
  10566. 6:40:24this one in the before right so that is
  10567. 6:40:26the reason we have to comment it out and
  10568. 6:40:28then you have to do here now let's say
  10569. 6:40:30you want to do like this let's say you
  10570. 6:40:32want to check that I wanted to check if
  10571. 6:40:34something is present with electronic key
  10572. 6:40:36003 or not right and let's say I'm just
  10573. 6:40:38going to clear this and And you'll have
  10574. 6:40:40to run it once again and you will see
  10575. 6:40:41that we don't have any row. But the
  10576. 6:40:43moment I'm going to execute this one
  10577. 6:40:45here once again. Let's I'm just going to
  10578. 6:40:47change this one to 003 and let's do this
  10579. 6:40:50on a laptop stand 03 and rest all let's
  10580. 6:40:53keep it same and you'll see that what
  10581. 6:40:55will happen right? It's going to do two
  10582. 6:40:56things. First it's going to execute this
  10583. 6:40:58and at the same time it will it will be
  10584. 6:41:00giving me the updated data with this
  10585. 6:41:03particular skew. So let's say now I'm
  10586. 6:41:05just going to run it one second. Okay.
  10587. 6:41:08Why you getting zero rows? We have to
  10588. 6:41:11save this. See this kind of mistake I'll
  10589. 6:41:13do. We have to save this file and let's
  10590. 6:41:15run once again. And you can see that you
  10591. 6:41:17are getting one data. And this is the
  10592. 6:41:19skew just we have created. Okay. So this
  10593. 6:41:21will be inserting a single row. Now
  10594. 6:41:23inserting multiple rows. Now multiple
  10595. 6:41:25rows are again very very simple but
  10596. 6:41:27still I wanted to show one interesting
  10597. 6:41:29thing that how you can query multiple
  10598. 6:41:31data when you're going to add multiple
  10599. 6:41:33uh how you're going to query multiple
  10600. 6:41:36data from DV when you're going to insert
  10601. 6:41:38multiple data into DV. Okay. So here
  10602. 6:41:40let's do one thing. I'm just going to do
  10603. 6:41:4203 and I'll do insert
  10604. 6:41:45multiple
  10605. 6:41:47rows dosql.
  10606. 6:41:50Again this will be the same thing. So
  10607. 6:41:52you have to do insert into products
  10608. 6:41:57and here I'm just going to do this one
  10609. 6:41:58as name category
  10610. 6:42:01price stock
  10611. 6:42:04then you have skew and then you have
  10612. 6:42:06description correct and then I'm just
  10613. 6:42:08going to take values
  10614. 6:42:11and here what I will do right I'm going
  10615. 6:42:13to add multiple here okay so let's I'm
  10616. 6:42:15just going to do laptop stand five let's
  10617. 6:42:18give this one as electronics
  10618. 6:42:22Let's give some data. Let's give this
  10619. 6:42:24one anything. And here I'm just going to
  10620. 6:42:26do electronic
  10621. 6:42:28laptop
  10622. 6:42:30100. Something like this. And then I'm
  10623. 6:42:33just going to do laptop
  10624. 6:42:37stand 5
  10625. 6:42:40desk. So give comma copy this. And then
  10626. 6:42:45I'm just going to paste it here. This
  10627. 6:42:47will be laptop.
  10628. 6:42:52So copy once again and then I'm just
  10629. 6:42:54going to paste it three times. And let's
  10630. 6:42:56do this one as 006. Then I'm just
  10631. 6:42:58[snorts] going to do 101 and this will
  10632. 6:43:01be laptop 6. Let's do this one as laptop
  10633. 6:43:047. And change this one 102. And I'm just
  10634. 6:43:06going to do seven. Okay, I'm just
  10635. 6:43:08keeping all the same which is fine. And
  10636. 6:43:10then I'm just going to close this. And
  10637. 6:43:11now what I'm going to do right, this is
  10638. 6:43:13going to insert all these three data.
  10639. 6:43:15Now I wanted to check all these data
  10640. 6:43:17that I have added just now multiple
  10641. 6:43:19data. So how you can do that right? So
  10642. 6:43:21you do select let's see I'm just going
  10643. 6:43:23to take name I need category I need
  10644. 6:43:25price I need stock and also I need skew.
  10645. 6:43:30Now what you need to do you have to
  10646. 6:43:31always give the uh from destination from
  10647. 6:43:34where and now I wanted to give okay
  10648. 6:43:36where skew. Now we have to give in.
  10649. 6:43:40Okay. So in is going to give give you
  10650. 6:43:42the or allow you to add multiple
  10651. 6:43:46points that you wanted to query. For
  10652. 6:43:47example, I wanted to check whe the skew
  10653. 6:43:50is this. So you can add it here. Then
  10654. 6:43:53you have to give comma. And then you
  10655. 6:43:54wanted to check this data also.
  10656. 6:43:57And then you also wanted to give this
  10657. 6:43:59102 also. So what this is going to do?
  10658. 6:44:02So this is going to insert all these
  10659. 6:44:04three. And at the same time this is
  10660. 6:44:05going to give you the information back.
  10661. 6:44:07Right? So let's see if this is working
  10662. 6:44:08or not. So I'm going to close this,
  10663. 6:44:11clear this, run this here. So copy this
  10664. 6:44:20and then I'm going to paste it here and
  10665. 6:44:22you can see that you are getting all
  10666. 6:44:23three. So you're getting only name,
  10667. 6:44:24category, price and skew. And here
  10668. 6:44:27you're getting all three. Now these
  10669. 6:44:28three are the newly data. How this is
  10670. 6:44:30giving? This is giving the based on skew
  10671. 6:44:32because skew is unique here. Okay. So
  10672. 6:44:34this is the concept of inserting single
  10673. 6:44:37row query, inserting multiple rows and
  10674. 6:44:40how you can query. So with this now
  10675. 6:44:41let's move on to the next one. All
  10676. 6:44:43right. So moving to the next one. So
  10677. 6:44:44let's create one more file and I'm just
  10678. 6:44:46going to give this one as select
  10679. 6:44:50specific
  10680. 6:44:53columns
  10681. 6:44:54versus select start that we have already
  10682. 6:44:57done right. So I'm just going to do
  10683. 6:44:58versus select
  10684. 6:45:01stars.
  10685. 6:45:02Select stars SQL. Okay. So what I mean
  10686. 6:45:06by this? So first of all, what is select
  10687. 6:45:08star? Select stars is going to return
  10688. 6:45:11every column for every product. As
  10689. 6:45:13simple as that, right? Uh now if you're
  10690. 6:45:16a beginner, I think we have already
  10691. 6:45:17covered select star from this select
  10692. 6:45:19star from this particular destination.
  10693. 6:45:21And that is very easy. But when your
  10694. 6:45:24application will grow, let's say you're
  10695. 6:45:26creating actual production level
  10696. 6:45:27application, what actually you should
  10697. 6:45:29do, right? Or maybe there can be some
  10698. 6:45:31specific scenarios where you only want
  10699. 6:45:34to use specific columns or you want to
  10700. 6:45:36get specific columns not you want to
  10701. 6:45:38return each and every column. Consider
  10702. 6:45:40it's possible 100% possible that there
  10703. 6:45:42is a particular table that has 100
  10704. 6:45:45columns. I'm just giving example okay
  10705. 6:45:47and you don't want to return all the 100
  10706. 6:45:49columns every time. So this is where we
  10707. 6:45:52have something called select specific
  10708. 6:45:54columns concept. Okay. So how we are
  10709. 6:45:56going to do do that? So first of all I'm
  10710. 6:45:58just going to write comment here that
  10711. 6:46:01okay select star is going to return
  10712. 6:46:02every column. So it returns every
  10713. 6:46:05columns and then we have something
  10714. 6:46:07called select specific columns. So if
  10715. 6:46:10select specific columns
  10716. 6:46:13is going to return is going to return
  10717. 6:46:17the columns that I want that I want.
  10718. 6:46:22So first of all we have select.
  10719. 6:46:26So I'll do star from and then I'm just
  10720. 6:46:29going to do this one as products right?
  10721. 6:46:31So this is going to give me every single
  10722. 6:46:33information or every single products
  10723. 6:46:35that I have. So if I save this now let's
  10724. 6:46:38run it first we'll do one by one and
  10725. 6:46:40let's
  10726. 6:46:42copy this
  10727. 6:46:47paste it here and then if I enter so
  10728. 6:46:49it's going to give me employee product
  10729. 6:46:50okay whatever that we have added here
  10730. 6:46:53that's fine now what I need to do right
  10731. 6:46:56I only want to get certain columns only
  10732. 6:46:59although this part we've already covered
  10733. 6:47:01so I'm just going to comment it out
  10734. 6:47:02let's do select so I only want to get
  10735. 6:47:05name I only want to get category. I only
  10736. 6:47:08want to get price and I only want to get
  10737. 6:47:11the stock of this part individual
  10738. 6:47:14product. And then you just have to do
  10739. 6:47:16from and then you're going to give
  10740. 6:47:18product. So this is going to give you
  10741. 6:47:20only the four columns that you need. As
  10742. 6:47:23simple as that, right? So again run it
  10743. 6:47:25and you can see that you're going to get
  10744. 6:47:26only four columns and it's very very
  10745. 6:47:28clean, right? You can see that. Now for
  10746. 6:47:30example that can be s possibility that
  10747. 6:47:32you only want price column. So you just
  10748. 6:47:35have to pass price here, right? So for
  10749. 6:47:37example, let's say you only want price.
  10750. 6:47:43Okay. So save this and let's clear this
  10751. 6:47:46and run it once again. And you're only
  10752. 6:47:48going to get price. Makes sense, right?
  10753. 6:47:51So this is how you have to select
  10754. 6:47:53specific columns. Now what I need to do,
  10755. 6:47:55right, you can also add allis. Now allis
  10756. 6:47:58actually are very useful and you'll see
  10757. 6:48:00that in many applications developers
  10758. 6:48:03will use allis. So allis is going to do
  10759. 6:48:05this right. So allis is going to help
  10760. 6:48:08you to understand the output column name
  10761. 6:48:10much better way. So okay. So I'm just
  10762. 6:48:12going to do allis creates.
  10763. 6:48:15No I'm just going to do as so this is we
  10764. 6:48:18have something called as this is sort
  10765. 6:48:21this is in sort as allis. So I'm just
  10766. 6:48:23going to do as creates and allis.
  10767. 6:48:29Okay. for the output
  10768. 6:48:35output of that column name. Output of
  10769. 6:48:38that
  10770. 6:48:40column
  10771. 6:48:42name, right? And what is the advantage?
  10772. 6:48:45No, definitely allis will be makes the
  10773. 6:48:48column name
  10774. 6:48:51easier to read. How we can do that?
  10775. 6:48:55Simply we are going to do select and
  10776. 6:48:57let's say I wanted to get name but I
  10777. 6:48:59wanted to get this name column as
  10778. 6:49:03you can give any name you want product
  10779. 6:49:05name. So now I'll be able to now you
  10780. 6:49:07might counter me okay when we have
  10781. 6:49:09created this table we can definitely
  10782. 6:49:11give this one as product name absolutely
  10783. 6:49:12yes you can you can do that but there
  10784. 6:49:14can be situation where actually want to
  10785. 6:49:16use this as a license okay that can be
  10786. 6:49:18so many situation can occur now let's
  10787. 6:49:20say want to give here price I want to
  10788. 6:49:22get the price but I wanted to get the
  10789. 6:49:24price as my selling price okay or I
  10790. 6:49:28wanted to get my stock and stock will be
  10791. 6:49:30my how my how many available quantity
  10792. 6:49:33quantities are there so I'll do
  10793. 6:49:34available quantity and then always you
  10794. 6:49:37have to give your destination. So you
  10795. 6:49:39have to give from products. So if I save
  10796. 6:49:41this also let's comment it off here.
  10797. 6:49:44Save this one and see that what will
  10798. 6:49:46happen. You're going to get the exact
  10799. 6:49:47details or syntax error as something we
  10800. 6:49:50have done. Maybe I haven't given comma.
  10801. 6:49:52See so I have to give comma here. So
  10802. 6:49:55save this and then if I clear this let's
  10803. 6:49:59run it once again. So you're going to
  10804. 6:50:00get no product name. This column name is
  10805. 6:50:02not changed. This is a selling price and
  10806. 6:50:04this is your available quantity and
  10807. 6:50:05you're going to get as many details
  10808. 6:50:07sorry as many columns you have that is
  10809. 6:50:10that stays same only thing is your
  10810. 6:50:13column name allis now change to the name
  10811. 6:50:15that you want okay so this will be the
  10812. 6:50:17concept of your select specific columns
  10813. 6:50:20that you have already discussed here and
  10814. 6:50:22then also how you can how you can use
  10815. 6:50:23allis so with this now let's move on to
  10816. 6:50:25the next one so next one maybe what we
  10817. 6:50:27can do here I think where we have
  10818. 6:50:29already discussed right where means I'm
  10819. 6:50:31talking about this on this so where is
  10820. 6:50:33actually we usually use for our basic
  10821. 6:50:36filtering but still let's uh do one more
  10822. 6:50:38time so that we will understand this one
  10823. 6:50:40much better way so I'm just going to do
  10824. 6:50:42this one as well basic filter
  10825. 6:50:46dosql okay so first of all let's say I
  10826. 6:50:49wanted to get couple of things that I
  10827. 6:50:51wanted to get the products from one
  10828. 6:50:53particular category and now why this is
  10829. 6:50:55helpful let's say you're creating
  10830. 6:50:56e-commerce website and you want to have
  10831. 6:50:58a API call where these will be products
  10832. 6:51:01and let's say you have category and this
  10833. 6:51:03category will be electronics. You're
  10834. 6:51:05getting what I mean. So I only wanted to
  10835. 6:51:07get all the categories of this
  10836. 6:51:08electronics, right? So all electronics
  10837. 6:51:10category products. This is where your
  10838. 6:51:13where filter is very very helpful. Okay.
  10839. 6:51:16Now what actually where helps where
  10840. 6:51:19helps? So this is going to reduce the
  10841. 6:51:21rows returned by a particular query. Now
  10842. 6:51:23let's how I'm going to do this. Now you
  10843. 6:51:25have to try to do this all by your own.
  10844. 6:51:28For example, let's say I wanted to get
  10845. 6:51:29select. I wanted to get the name. name I
  10846. 6:51:31wanted to get the category I wanted to
  10847. 6:51:33get the price
  10848. 6:51:35and I wanted to get from products
  10849. 6:51:37definitely this is the table but now I'm
  10850. 6:51:39just going to give okay where the
  10851. 6:51:41category should be equal to electronics
  10852. 6:51:45so you don't want all these uh products
  10853. 6:51:50only one products where the category is
  10854. 6:51:52electronics and this is going to give
  10855. 6:51:53you all the electronics products so
  10856. 6:51:55let's copy this the name and let's run
  10857. 6:51:58it here.
  10858. 6:52:01Okay. So now you can see you're getting
  10859. 6:52:03name, category and these all are your
  10860. 6:52:04electronics products. You make also
  10861. 6:52:07confirm this just uh if I go here let's
  10862. 6:52:10say you have furniture you can see the
  10863. 6:52:11office chair and you can see this office
  10864. 6:52:13chair is not coming here. Okay. So that
  10865. 6:52:16means this is working. Now I'm going to
  10866. 6:52:18give you some of the homework or maybe
  10867. 6:52:20I'm going to do here also but try to do
  10868. 6:52:21by yourself that you have to give uh you
  10869. 6:52:24have to find some products
  10870. 6:52:26where the price is greater than 1,000
  10871. 6:52:31and it's very very simple. So what you
  10872. 6:52:33can do you can again do select I want to
  10873. 6:52:35get the name of the product I wanted to
  10874. 6:52:37get the price of the product I want to
  10875. 6:52:39get from my products table but I wanted
  10876. 6:52:41to give a basic filter that where the
  10877. 6:52:44product price has to be what greater
  10878. 6:52:46than 1,000 that's it and if you run this
  10879. 6:52:49you're going to get result and you'll
  10880. 6:52:52see that you're going to get where the
  10881. 6:52:53price is greater than 1,000. So this is
  10882. 6:52:56another situation right and the same way
  10883. 6:52:58you can do other also. Now let's say you
  10884. 6:53:01only wanted to get where the is active
  10885. 6:53:04is false. Correct? So let's see if we
  10886. 6:53:06have an is active false or not. So we
  10887. 6:53:09have two true true we have one false
  10888. 6:53:11item. Okay fine. Then so let's say I'm
  10889. 6:53:14just going to comment it out that I'll
  10890. 6:53:16give that find products which are not
  10891. 6:53:22active. So here I'm just going to do a
  10892. 6:53:24select give name give me is_active
  10893. 6:53:30from products and here you have to give
  10894. 6:53:33what you have to give where e is active
  10895. 6:53:35will be what equal to false right and
  10896. 6:53:38this is going to give you where it is
  10897. 6:53:40false and just run it here
  10898. 6:53:46now it see all monitor and this is the
  10899. 6:53:50product so this is how you can make use
  10900. 6:53:53of your where filter. So I think this
  10901. 6:53:56should be more than enough. Now moving
  10902. 6:53:57to the next one. Next let's understand
  10903. 6:54:00one more. So I'm just going to do 06 and
  10904. 6:54:02let's give this one as and or not. These
  10905. 6:54:06all are very important. Okay. These all
  10906. 6:54:08are also filters all three. So let's
  10907. 6:54:11close everything else. Okay. First one
  10908. 6:54:14what is and I think this is universal
  10909. 6:54:18concept right? Hands mean every
  10910. 6:54:21condition
  10911. 6:54:23must be true. Right? Then you have or at
  10912. 6:54:29least
  10913. 6:54:34one condition
  10914. 6:54:37must be true.
  10915. 6:54:39And then you have not. So not will be
  10916. 6:54:42your reverse condition, right? Reverse
  10917. 6:54:44or exclude. I'm going to say a
  10918. 6:54:47condition. So let's say how this will
  10919. 6:54:50work. Now let's say I wanted to make a
  10920. 6:54:53filter or I wanted to create a filter
  10921. 6:54:55which is going to return me electronics
  10922. 6:54:57product. Okay. But if the price is
  10923. 6:55:00greater than 100. So I I'm going to
  10924. 6:55:03repeat once again that give me the
  10925. 6:55:04products
  10926. 6:55:06where it is electronics product. It is
  10927. 6:55:09electronics
  10928. 6:55:12electronics
  10929. 6:55:14product but the price has to be greater
  10930. 6:55:17than 1,000. So this is the query that I
  10931. 6:55:20need to write. So how I can do that?
  10932. 6:55:22Here you have a and condition. Why?
  10933. 6:55:25Because both these condition has to be
  10934. 6:55:27true. It has to be electronics and at
  10935. 6:55:29the same time it has to be price has to
  10936. 6:55:31be greater than 1,000. Makes sense,
  10937. 6:55:32right? So that means if I try to do here
  10938. 6:55:34select
  10939. 6:55:36give me name give me category
  10940. 6:55:42price from
  10941. 6:55:45products.
  10942. 6:55:46Now I have to give
  10943. 6:55:49where the category is equal to
  10944. 6:55:53electronics.
  10945. 6:55:56And now I have to give here and filter.
  10946. 6:55:59Okay. So I'm just going to do and let's
  10947. 6:56:02do this one as price has to be greater
  10948. 6:56:05than 1,000. Okay. So this is going to
  10949. 6:56:07give you all the data that satisfy this
  10950. 6:56:09condition. Now let's go one by one I
  10951. 6:56:11think. Right. So we have electronics.
  10952. 6:56:13Electronics electronics. So this USBC
  10953. 6:56:15cable this should not come here. Why?
  10954. 6:56:17Because this is electronics but this is
  10955. 6:56:19less than 1,000. So let's see what is
  10956. 6:56:21happening here. So I'm just going to
  10957. 6:56:22save this. Go up. Let's clear this.
  10958. 6:56:28Copy this. And then I'm just going to
  10959. 6:56:31paste it here. And let's say how many
  10960. 6:56:33data we are getting. All right. So you
  10961. 6:56:35can see that you are getting five rows.
  10962. 6:56:37Now if I just do this one as let's do
  10963. 6:56:39this one as 100. And you'll see that
  10964. 6:56:42this will increase for sure because now
  10965. 6:56:44the condition is separate. Right? So I'm
  10966. 6:56:46just going to run here. So you're
  10967. 6:56:47getting six rows. So USB C cable this is
  10968. 6:56:50also coming right. So this is the
  10969. 6:56:52concept of and filter. Now what how you
  10970. 6:56:56can use odd filter. Now see these things
  10971. 6:56:58are not that much difficult to
  10972. 6:57:00understand right you can do only once I
  10973. 6:57:02can guarantee you'll be able to apply in
  10974. 6:57:04a real project also and that is the
  10975. 6:57:06reason you'll think that why these kind
  10976. 6:57:07of videos are very important like if
  10977. 6:57:09you're watching this video you can watch
  10978. 6:57:11any kind of complicated project videos
  10979. 6:57:13but if your basics are clear right you
  10980. 6:57:15don't have to watch those videos also
  10981. 6:57:17you'll be able to create project by
  10982. 6:57:18yourself for example let's say you don't
  10983. 6:57:20know the concept of where and and
  10984. 6:57:22posgrsql and you started a AI project on
  10985. 6:57:25YouTube uh that using posgress skill you
  10986. 6:57:28can do that but learning concepts are
  10987. 6:57:30very much important and how you going to
  10988. 6:57:31apply [clears throat] those concept in a
  10989. 6:57:33real project that is the main skill so
  10990. 6:57:36now here I'm just going to commit it out
  10991. 6:57:37now let's give one more filter here that
  10992. 6:57:40uh what I'm going to do like maybe let's
  10993. 6:57:42do that you have to give me products
  10994. 6:57:45right where
  10995. 6:57:48category is electronics
  10996. 6:57:51electronics and let's see what else we
  10997. 6:57:53have we have electronics and let's do
  10998. 6:57:55furniture both Sorry uh category is
  10999. 6:57:58electronics or this can be furniture
  11000. 6:58:01right so give me the products where the
  11001. 6:58:03category is electronics or it can be
  11002. 6:58:05furniture it can be either so now I'm
  11003. 6:58:08just going to do select so I'll give
  11004. 6:58:10name category price and I'm just going
  11005. 6:58:14to give from the destination products
  11006. 6:58:17now what you'll do so where the category
  11007. 6:58:20has to be either it is electronics
  11008. 6:58:24and now we have to give a or condition
  11009. 6:58:26here Right. So I'm just going to do this
  11010. 6:58:27can be or.
  11011. 6:58:30So or category can be furniture also.
  11012. 6:58:34Okay. That's it. So now this is a or
  11013. 6:58:38condition.
  11014. 6:58:39So now let's run it once again.
  11015. 6:58:43So we're getting both electronics and
  11016. 6:58:45furniture. Fantastic. So this is about
  11017. 6:58:47the and and or. Now let's understand
  11018. 6:58:51some not condition, right? Not is going
  11019. 6:58:53to exclude a particular condition. So
  11020. 6:58:55now maybe let's do one thing that I
  11021. 6:58:57wanted to return some products that are
  11022. 6:59:00not in a maybe let's say stationary
  11023. 6:59:02category something like that. So let's
  11024. 6:59:04say how many categories we have. We have
  11025. 6:59:05a stationary category here. You can see
  11026. 6:59:07two products are there. So what I'm
  11027. 6:59:09trying to do here that I wanted to get
  11028. 6:59:11all the products where it is not
  11029. 6:59:13stationary right so it's exclude
  11030. 6:59:16condition. So here let's I'm just going
  11031. 6:59:18to do okay give me name also give me
  11032. 6:59:20only category. So we using selective fil
  11033. 6:59:23uh column logic here that we have
  11034. 6:59:24discussed here select specific columns
  11035. 6:59:27also if you notice one thing that every
  11036. 6:59:29time we are going forward with different
  11037. 6:59:31concept we are trying to apply the
  11038. 6:59:33previous concept that we have learned
  11039. 6:59:35for example we have just now we have
  11040. 6:59:37learned select specific columns and you
  11041. 6:59:38can see that we are applying that
  11042. 6:59:39concept this is the learning that I
  11043. 6:59:41always mentioned in all my other videos
  11044. 6:59:43that when you're learning incrementally
  11045. 6:59:45right or gradually every time you're
  11046. 6:59:47going to learn a new concept see if any
  11047. 6:59:49of the concept from the previous thing
  11048. 6:59:50you can apply there or not. That is the
  11049. 6:59:52real learning that you can do. Okay. So
  11050. 6:59:55here this will be select name and
  11051. 6:59:56category. So I'm just going to do from
  11052. 6:59:58products. And here what we need to do we
  11053. 7:00:00have to do where and then I'm just going
  11054. 7:00:03to do what not. Okay. So this is negate.
  11055. 7:00:05It's a exclude condition and I'm just
  11056. 7:00:07going to do category is equal to. So
  11057. 7:00:09let's say maybe let's do furniture only.
  11058. 7:00:11So I'm simply telling that where the
  11059. 7:00:14category is not furniture you have to
  11060. 7:00:15return me those products. So now let's
  11061. 7:00:17run this again and you can see that you
  11062. 7:00:19getting electronics stationary but
  11063. 7:00:21you're not getting furniture. If you
  11064. 7:00:23change this one to stationary you'll see
  11065. 7:00:25that you're going to get now you're not
  11066. 7:00:28going to get stationary here. Okay
  11067. 7:00:34see you're getting category and
  11068. 7:00:35furniture.
  11069. 7:00:36So this will be your not condition. Now
  11070. 7:00:40maybe let's do one more right. So let's
  11071. 7:00:42do little complex that what you can do
  11072. 7:00:45right you can also use parenthesis okay
  11073. 7:00:47now for example let's say I want to do
  11074. 7:00:49something like this I wanted to write
  11075. 7:00:50one query let's see if you're able to
  11076. 7:00:52understand or not so I'm just going to
  11077. 7:00:53take the I'm going to get name I wanted
  11078. 7:00:55to get category I also wanted to get
  11079. 7:00:58price I also wanted to get uh stock also
  11080. 7:01:01this time from I think this you know
  11081. 7:01:03from products now I wanted to do right
  11082. 7:01:06so give me where now I have to give a
  11083. 7:01:08parenthesis parenthesis is nothing but
  11084. 7:01:11it's going to If you're writing a
  11085. 7:01:12complex logic, it is much much easier to
  11086. 7:01:15understand and it is also safe. So here
  11087. 7:01:17I'm just going to give okay category is
  11088. 7:01:20equal to electronics
  11089. 7:01:23and here I'm just going to or this can
  11090. 7:01:25be so I'm just going to do category can
  11091. 7:01:27be also furniture.
  11092. 7:01:31Okay. And then I'm going to give another
  11093. 7:01:33and condition here. So I'm just going to
  11094. 7:01:34do okay and and let's give stock has to
  11095. 7:01:38be greater than zero. That's it. Okay.
  11096. 7:01:40So this is another we can apply here. So
  11097. 7:01:44if I run this once again
  11098. 7:01:47and you can see that you're going to get
  11099. 7:01:49all this right. So you're going to get
  11100. 7:01:50either it is electronics or it is
  11101. 7:01:52furniture but the stock is greater than
  11102. 7:01:55zero. Okay. Now I think this is done.
  11103. 7:01:58What else? So maybe I think this
  11104. 7:02:02uh I'm thinking to write one more. No, I
  11105. 7:02:05think this should be enough or let's
  11106. 7:02:06write one more here. So I'm just going
  11107. 7:02:08to give one real world example. Okay. So
  11108. 7:02:10let's do select let's do name price
  11109. 7:02:14stock and I'll do e is active. Now I
  11110. 7:02:17wanted to give is active also. So I
  11111. 7:02:19wanted to give from products
  11112. 7:02:21and now I'm just going to do where is
  11113. 7:02:24active should be equal to true right? So
  11114. 7:02:27it will be true and at the same time so
  11115. 7:02:29I'm just going to do and the price is
  11116. 7:02:33less than 1,000 or the stock can be
  11117. 7:02:38greater than or equal to 100. See I just
  11118. 7:02:40wanted to explain what you know by
  11119. 7:02:42writing all this that you can do
  11120. 7:02:43anything any combination you have to
  11121. 7:02:45understand the concept. So first you
  11122. 7:02:46tell me that you give me all the active
  11123. 7:02:48products where the price can be less
  11124. 7:02:50than 1,000 or stock has to be greater
  11125. 7:02:53than or equal to 100. So this is also
  11126. 7:02:56going to give you the result here. So
  11127. 7:02:57let's see if I run once again
  11128. 7:03:00and you can see that what you're
  11129. 7:03:01getting. You notice in all here stock is
  11130. 7:03:05actually greater than 100 and also it is
  11131. 7:03:08less than 100. Why? Because you're using
  11132. 7:03:10a or condition here the price is
  11133. 7:03:13actually 100. So it's actually
  11134. 7:03:14satisfying this condition. Price has to
  11135. 7:03:17be less than 1,000. Okay. So that is the
  11136. 7:03:19reason you're getting this particular
  11137. 7:03:20combination. But what you need to see
  11138. 7:03:22you have to see that this is active is
  11139. 7:03:24actually true for all. Okay. So this is
  11140. 7:03:27how you can make use of this and or and
  11141. 7:03:30not filter. So with this now let's move
  11142. 7:03:32on to the next one. Okay. So let's move
  11143. 7:03:33on to the next one. Okay. So I'm just
  11144. 7:03:35going to create one more file and let's
  11145. 7:03:36give this one name as like I like and
  11146. 7:03:41also let's give this one as patterns.
  11147. 7:03:44SQL. Okay. So let's understand these
  11148. 7:03:48some of the uh pattern matching that we
  11149. 7:03:50can use. These are also very very
  11150. 7:03:52important. So one we have like
  11151. 7:03:56okay so this like is nothing but this
  11152. 7:03:59will be case sensitive pattern match.
  11153. 7:04:04Okay. Then you have something called I
  11154. 7:04:06like. Okay. This will be case
  11155. 7:04:08insensitive
  11156. 7:04:11pattern
  11157. 7:04:12match. Okay. Then you have something
  11158. 7:04:15called I don't know how to say I think
  11159. 7:04:18percentile I think this will be
  11160. 7:04:20percentile only percentile means this
  11161. 7:04:22will be I think any number of characters
  11162. 7:04:26yes so this will be any number of cars
  11163. 7:04:28okay or characters whatever you want to
  11164. 7:04:29write and then you have uh we also have
  11165. 7:04:32this I think underscore sorry uh this
  11166. 7:04:35underscore this underscore will be means
  11167. 7:04:38exactly one character so this will be
  11168. 7:04:40exactly one character okay so let's see
  11169. 7:04:42so first I wanted to do something like
  11170. 7:04:45this. Okay, let me go here and I'll see
  11171. 7:04:47that I have this wireless uh product.
  11172. 7:04:50Okay, so I wanted to find some product
  11173. 7:04:53where the name will start with wireless.
  11174. 7:04:56Okay, so let's how we are going to do
  11175. 7:04:58that and then I'm going to explain. So
  11176. 7:04:59I'm just going to do here select and
  11177. 7:05:01wanted to get name and price both and
  11178. 7:05:04then first of all we need to do from
  11179. 7:05:06products
  11180. 7:05:08and now I will give that where the name
  11181. 7:05:13like okay so the name like so here I'm
  11182. 7:05:15using a pattern and I wanted to give
  11183. 7:05:18here that this will be wireless okay and
  11184. 7:05:20when I'm going to give this right here
  11185. 7:05:24so this means so this after means
  11186. 7:05:28anything can uh anything can come after
  11187. 7:05:30it. Okay, this percentile after wireless
  11188. 7:05:34means this percentile we have right. So
  11189. 7:05:35this is after wireless text. So this
  11190. 7:05:38means anything can come after that.
  11191. 7:05:41Okay, I'm I will just write it here. So
  11192. 7:05:44the percentile
  11193. 7:05:46percentage whatever after wireless
  11194. 7:05:50means
  11195. 7:05:53anything can come after it. Okay. So
  11196. 7:05:58let's see how this is happening. So save
  11197. 7:05:59this and I'll go here. Let's clear this.
  11198. 7:06:02We'll do one by one and then I'm just
  11199. 7:06:04going to rename copy this.
  11200. 7:06:08Let's remove this.
  11201. 7:06:13Okay. So here you can see that what you
  11202. 7:06:15are getting. Okay. Something we getting
  11203. 7:06:17syntax is there. Okay. Got it.
  11204. 7:06:21To give this Oh, we we have to remove
  11205. 7:06:24this. I've given in the wrong place. So
  11206. 7:06:26let's run once again. Clear.
  11207. 7:06:30Okay. See. So now you're getting
  11208. 7:06:32wireless mouse and it makes sense,
  11209. 7:06:33right? Because we have only one product.
  11210. 7:06:36All right. Now what this line you
  11211. 7:06:38understand that this anything can come
  11212. 7:06:40after it. So this is what I'm talking
  11213. 7:06:41about. So anything can come after
  11214. 7:06:43wireless. But this is going to do that
  11215. 7:06:45matching. Okay. Next. Let's do one more
  11216. 7:06:48example that. Okay. Let's see how many
  11217. 7:06:51products we have. Uh what else? Maybe
  11218. 7:06:53let's do something with this desk. Okay.
  11219. 7:06:56So I wanted to find some product where
  11220. 7:06:58the name contents desk. Now what this I
  11221. 7:07:02like is going to do as I already
  11222. 7:07:04mentioned is this is a case insensitive
  11223. 7:07:06means it's usually going to ignore any
  11224. 7:07:09kind of upper case or lower case
  11225. 7:07:10differences. So what I mean by this what
  11226. 7:07:13is like how we understand that this is
  11227. 7:07:16going to ignore. So for example let's
  11228. 7:07:17say this is going to match with desk.
  11229. 7:07:21This is also going to match with desk.
  11230. 7:07:23So you can see that this is capital this
  11231. 7:07:25is small and this is also going to match
  11232. 7:07:28with disk you're getting right. So this
  11233. 7:07:30is what we are talking about case
  11234. 7:07:32insensitive. So let's have this is
  11235. 7:07:33happening. So I'm just going to do name.
  11236. 7:07:35Let's also take category now and I'm
  11237. 7:07:37also going to do price and here I'm just
  11238. 7:07:40going to do again from products. And now
  11239. 7:07:42I'm just going to do where name now you
  11240. 7:07:45have to do I like. Okay very very
  11241. 7:07:47important. And you just have to give
  11242. 7:07:49this will be desk.
  11243. 7:07:51Okay. So this is going to do this
  11244. 7:07:53particular match. So now let's save this
  11245. 7:07:56and run once again and we'll see what is
  11246. 7:07:58happening here.
  11247. 7:08:02Okay. So you're getting a standing desk
  11248. 7:08:04and I think we have only one. So this is
  11249. 7:08:06the standing desk. Okay. So we have
  11250. 7:08:08learned these two I like. Sorry like and
  11251. 7:08:10I like. Okay. So let's write one more
  11252. 7:08:12here. So maybe uh you can do right. So
  11253. 7:08:15you can also search across multiple
  11254. 7:08:17columns. So for example, let's say let's
  11255. 7:08:19go here and maybe we'll take this
  11256. 7:08:22example. Okay, let me just check. Okay,
  11257. 7:08:24so this is good. So we have chair in
  11258. 7:08:26this name also and let's see have chair
  11259. 7:08:28in this description also. So you can
  11260. 7:08:30check in both the columns. Okay, so for
  11261. 7:08:32example, let me just comment it out
  11262. 7:08:34here. This one and also this one.
  11263. 7:08:38So we'll just do select. Let's do name
  11264. 7:08:41category
  11265. 7:08:43and also I'm just going to take the
  11266. 7:08:44description also. Okay. And here I'm
  11267. 7:08:47just going to do from and I'll do
  11268. 7:08:49products. And now first of all I will do
  11269. 7:08:52where the name. So here I'm just going
  11270. 7:08:55to do I like and I'm just going to give
  11271. 7:08:57this one as so I wanted to take chair
  11272. 7:08:59right. So I'll do chair. And then what
  11273. 7:09:02you can do right you can give a or.
  11274. 7:09:04Okay. Now this one we have already
  11275. 7:09:06discussed in the previous part. So you
  11276. 7:09:07or the description I like and then you
  11277. 7:09:11have to give the pattern that you wanted
  11278. 7:09:12to match. For example I wanted to check
  11279. 7:09:14the chair word only. So this is how you
  11280. 7:09:16need to check with multiple columns.
  11281. 7:09:18Okay. So let's save this and we'll
  11282. 7:09:21[clears throat] go let's run this
  11283. 7:09:24and you can see that you're getting
  11284. 7:09:25office chair and this is also
  11285. 7:09:27comfortable chair. So this both you
  11286. 7:09:29have. So this is how you can make use of
  11287. 7:09:31like I like and percentile. Okay. So now
  11288. 7:09:35maybe let's move on to the next one.
  11289. 7:09:37What what else we have? Let me think.
  11290. 7:09:39Okay. We also do in not I think in we
  11291. 7:09:42have already covered in some uh previous
  11292. 7:09:44uh parts but still let's do it. So I'm
  11293. 7:09:46just going to create one more. So I'm
  11294. 7:09:47just going to do 08.
  11295. 7:09:50So let's do here. Let's give in. We also
  11296. 7:09:52have to not and also sorry we have in
  11297. 7:09:56not in and between this. So we'll do in
  11298. 7:09:59not in and also let's do between this
  11299. 7:10:01also important between also we have to
  11300. 7:10:04understand. Okay. So first let's start
  11301. 7:10:06with in. So in will be in simple way
  11302. 7:10:10value that value you want to check that
  11303. 7:10:13must match one item from the list.
  11304. 7:10:15Simple. So here value must match one
  11305. 7:10:20item from the list. So this will be in.
  11306. 7:10:24Next you have not in. So what actually
  11307. 7:10:27not in? So this will be value must not
  11308. 7:10:30match. By the name only you can
  11309. 7:10:31understand right? So this will be value
  11310. 7:10:33must not match any item from the list
  11311. 7:10:39and then you have something called
  11312. 7:10:41between. Okay, this is also important.
  11313. 7:10:43So between will be value must be inside
  11314. 7:10:46particular range because from the name
  11315. 7:10:47only you can understand. So this will be
  11316. 7:10:49value must be between or I'm just going
  11317. 7:10:52to write in a better way. This will be
  11318. 7:10:54inside a range. Okay. So now let's say
  11319. 7:10:57what we are going to do right. So in the
  11320. 7:10:59previously we have done what that we are
  11321. 7:11:01checking right that if the category is
  11322. 7:11:03electronics or the category is furniture
  11323. 7:11:06here we can use in instead of doing that
  11324. 7:11:08how so now let's I'm just going to do
  11325. 7:11:10select name give me category and also
  11326. 7:11:13give me the price from products right
  11327. 7:11:17now what I'm going to do right I'm going
  11328. 7:11:18to check where the category in so
  11329. 7:11:20previously we have done okay category is
  11330. 7:11:22equal to this or category equal to that
  11331. 7:11:24like that but here I'm just going to do
  11332. 7:11:26give me in so that means these value
  11333. 7:11:28must be one of that value that I wanted
  11334. 7:11:30to check. So value can be either
  11335. 7:11:32electronics or the value can be either
  11336. 7:11:35furniture.
  11337. 7:11:38I hope this is clear. This is not that
  11338. 7:11:39much difficult to understand. Simple.
  11339. 7:11:41Okay. So this is where you can use this
  11340. 7:11:43in.
  11341. 7:11:45So clear this and let's run it here.
  11342. 7:11:51Okay.
  11343. 7:11:54Right. So you're getting electronics and
  11344. 7:11:56furniture stationary. you are not
  11345. 7:11:58getting. Okay. So now you can also
  11346. 7:12:01double check this one. Maybe let's say I
  11347. 7:12:02wanted to check this stationary and I
  11348. 7:12:04wanted to remove this electronics,
  11349. 7:12:05right? Save this and you are only going
  11350. 7:12:08to get now stationary and furniture. So
  11351. 7:12:10you're getting furniture and stationary.
  11352. 7:12:12All right. So this will be in. Very easy
  11353. 7:12:15to understand. Next uh what else we
  11354. 7:12:17have? We have not in. Not in is again
  11355. 7:12:19it's going to exclude multiple values.
  11356. 7:12:21Means I wanted to return all the
  11357. 7:12:24products that are not electronics, not
  11358. 7:12:26furniture. So that means what if it is
  11359. 7:12:28not electronics not furniture means it
  11360. 7:12:30is only going to give me stationary
  11361. 7:12:31products. So how we can do that? So do
  11362. 7:12:33name category from products. And now you
  11363. 7:12:37have to give me where the category
  11364. 7:12:41not in. Okay. And then you're going to
  11365. 7:12:43give okay you want to check that okay
  11366. 7:12:45this has not be has uh has to be only
  11367. 7:12:48stationary. I'm talking opposite. So it
  11368. 7:12:50has to be only stationary. It don't have
  11369. 7:12:53to be electronics and also it should not
  11370. 7:12:55be furniture. So we excluding both. And
  11371. 7:12:59here the part is important that is not
  11372. 7:13:01in. And if you run this, so you're only
  11373. 7:13:03going to get the stationary product,
  11374. 7:13:06right? See, so you're getting only
  11375. 7:13:07stationary product. Simple. So this is
  11376. 7:13:10about the not in. What else we have? We
  11377. 7:13:12also have the between
  11378. 7:13:14how we can check. Now let's say I wanted
  11379. 7:13:16to check on the price that give me all
  11380. 7:13:18the products where the price is between
  11381. 7:13:20100 to 2,000 something like that. So it
  11382. 7:13:23will be in that bit range right. So in
  11383. 7:13:25that case we can check that. So now here
  11384. 7:13:27you can also use and that if it is
  11385. 7:13:29greater than 100 less than this like
  11386. 7:13:31that but this uh this in not in between
  11387. 7:13:34are very very very useful once you start
  11388. 7:13:36using all of these. So I'm just going to
  11389. 7:13:38do this one as price. I'll do from
  11390. 7:13:42products. And what you have to give? You
  11391. 7:13:44have to give where you want to check the
  11392. 7:13:45property. So I have to check the price
  11393. 7:13:47property. And this will be between. And
  11394. 7:13:49now you have to give okay 100 and see we
  11395. 7:13:52using and and 2,00.
  11396. 7:13:55So it will give me in that range only.
  11397. 7:13:59So now
  11398. 7:14:01also it this is also important. This is
  11399. 7:14:03going to include 10 and 2,000 if I'm not
  11400. 7:14:06wrong. So let me just quickly check. So
  11401. 7:14:08include means it's not like from 101 and
  11402. 7:14:111 9. It is also going to add 100 and
  11403. 7:14:142,000 also. So let me just check that.
  11404. 7:14:17Will this
  11405. 7:14:19include 100 and 2,000 also. I think yes.
  11406. 7:14:25Just confirm.
  11407. 7:14:31Okay. Right. So this is see it is like
  11408. 7:14:34where right? It is similar to where
  11409. 7:14:36price is greater than equal to 100 and
  11410. 7:14:38price is less than or equal to 2,000. So
  11411. 7:14:40that means this is also going to add
  11412. 7:14:42sorry this is going to keep 100 and
  11413. 7:14:432,000 also. Makes sense right? So now if
  11414. 7:14:45you run this you can see that you're
  11415. 7:14:46getting 100 see all three two uh three 9
  11416. 7:14:49and 1 2 9. So it will be in that range
  11417. 7:14:51but again I'm repeating this is also
  11418. 7:14:53going to include 100 and 2,000
  11419. 7:14:55important. So I think this is done. Next
  11420. 7:14:58let's comment it out. What else is left?
  11421. 7:15:02Uh
  11422. 7:15:04I think we have covered all three right
  11423. 7:15:06now we can let's do one more
  11424. 7:15:07combination. So let's say I'm just going
  11425. 7:15:09to get name category price and stock
  11426. 7:15:14from products.
  11427. 7:15:17Now I'm just going to do where the
  11428. 7:15:18category in. So give me electronics
  11429. 7:15:24and maybe let's say stationary product.
  11430. 7:15:30And then I'm also going to do here and
  11431. 7:15:33price between 100 and 1,500 or whatever
  11432. 7:15:38you wanted to give 149 something like
  11433. 7:15:40that you're getting how we are combining
  11434. 7:15:43all of these all the concept that we are
  11435. 7:15:44learning so far [snorts] and if you run
  11436. 7:15:46this so clear this and run it here
  11437. 7:15:49you're going to get all this result here
  11438. 7:15:50okay so this is how you have to do it in
  11439. 7:15:53stationary I think we don't have let's
  11440. 7:15:54see if stationary if it is 101,499
  11441. 7:15:58so this is 99 in stationary. So if I
  11442. 7:16:01make this one to 10, so it should give
  11443. 7:16:03me stationary also if I'm not wrong. So
  11444. 7:16:06clear this and run once again. Now
  11445. 7:16:08you're getting stationary also because
  11446. 7:16:09both are less than 100. Okay. So this is
  11447. 7:16:12about in, not in and between. All three
  11448. 7:16:15that you have learned so far in this
  11449. 7:16:17particular file are actually very
  11450. 7:16:18important. So now what else we have? We
  11451. 7:16:20also have something called null
  11452. 7:16:23filtering. And I think we have already
  11453. 7:16:25covered that null and not null. But
  11454. 7:16:26still it is important. So let's do let's
  11455. 7:16:29not skip this. So I'm just going to do
  11456. 7:16:31this one as null. Let's do and uh what
  11457. 7:16:34I'll do is not null something like this
  11458. 7:16:36is not nullsql.
  11459. 7:16:40So here first of all null filtering
  11460. 7:16:42means again null means very important
  11461. 7:16:44means missing value or unknown value
  11462. 7:16:47something like this.
  11463. 7:16:50Okay. So here what we need to understand
  11464. 7:16:55right that when you're going to check
  11465. 7:16:57you should not check with null you
  11466. 7:16:59should not check null using equal to
  11467. 7:17:01null so what I'm talking about that you
  11468. 7:17:03should not you should not check null
  11469. 7:17:09using equal to null you should not do
  11470. 7:17:11that so correct is what we need to do
  11471. 7:17:13either we need to check is null or we
  11472. 7:17:16have to check is not null okay so this
  11473. 7:17:19is the important part here. Now let's
  11474. 7:17:21say [snorts]
  11475. 7:17:22here what we need to check that I wanted
  11476. 7:17:24to get all the products but the
  11477. 7:17:25description is missing. I'm just giving
  11478. 7:17:27example. Okay. Now here we have to see
  11479. 7:17:30that this doesn't mean the empty string
  11480. 7:17:33that mean that no value is present for
  11481. 7:17:35that particular description property. So
  11482. 7:17:38here so I'll do select let's say select
  11483. 7:17:40name comma description
  11484. 7:17:44from products
  11485. 7:17:47and here I'm just going to do where the
  11486. 7:17:50description
  11487. 7:17:51is null okay so simply telling the
  11488. 7:17:54description is not present so let's run
  11489. 7:17:56this okay so clear this and I'm just
  11490. 7:17:59going to copy here and let's run it
  11491. 7:18:03here.
  11492. 7:18:07Okay. So you can see that USBC cable and
  11493. 7:18:10jail pen. So let's see what is happening
  11494. 7:18:11here. So you can see that we have null
  11495. 7:18:12here. Okay. And for this also we have
  11496. 7:18:15null here. So you're directly checking
  11497. 7:18:17that if this is equal to null. Correct.
  11498. 7:18:20Now you want to check that where the
  11499. 7:18:22description is available. So how you're
  11500. 7:18:24going to do that is not null. Okay. So
  11501. 7:18:26here you copy this and then you're going
  11502. 7:18:29to do is not null. That's it. So this is
  11503. 7:18:33the difference and if you now run this
  11504. 7:18:35so you're going to get all the products
  11505. 7:18:36where the description is available.
  11506. 7:18:38Okay. So we need to understand again I'm
  11507. 7:18:40repeating this thing that null means
  11508. 7:18:43missing value or unknown value. But when
  11509. 7:18:45you're going to check null using equal
  11510. 7:18:47to null you can't check. You should
  11511. 7:18:49check with is null or is not null. Now
  11512. 7:18:52let's see a practical example right
  11513. 7:18:53practical back end example. Let's say
  11514. 7:18:55you're creating admin dashboard where
  11515. 7:18:56admin wanted to check something like
  11516. 7:18:58that. Okay me [snorts] all the active
  11517. 7:19:00products right? These products are
  11518. 7:19:01active that means user can see the
  11519. 7:19:03active products from the e-commerce
  11520. 7:19:05website. But for this particular product
  11521. 7:19:07the description is missing. So admin
  11522. 7:19:09admin needs to query this. So how we we
  11523. 7:19:12can do that? So I'm just going to do
  11524. 7:19:13okay give me name give me the category
  11525. 7:19:15of this product. Give me is active
  11526. 7:19:19and also let's say give me description
  11527. 7:19:21also. So from products
  11528. 7:19:25where
  11529. 7:19:26is active is equal to what? True. And
  11530. 7:19:29here you have to give and
  11531. 7:19:31description is null.
  11532. 7:19:34That's it. So this is going to give you
  11533. 7:19:36all the active products but at the same
  11534. 7:19:38time the description is missing. So
  11535. 7:19:40let's see if we have so this product
  11536. 7:19:42both are active. So you can see that
  11537. 7:19:43both are active but description is null.
  11538. 7:19:45So this is how you can make use of null
  11539. 7:19:48and is not null. Okay. So with this I
  11540. 7:19:51think we have covered most of the
  11541. 7:19:53things. Now still we have lot of things
  11542. 7:19:54to cover in this one. So what are the
  11543. 7:19:56things still we need to cover? We need
  11544. 7:19:57to cover order by limit offset all these
  11545. 7:20:00things how we can update right we
  11546. 7:20:02haven't started update yet that how we
  11547. 7:20:03are going to update a single row how you
  11548. 7:20:05can update multiple rows how you can
  11549. 7:20:07delete rows and also the concept of
  11550. 7:20:09returning so all these things we're
  11551. 7:20:11going to understand in the next part all
  11552. 7:20:12right so let's move on to the next one
  11553. 7:20:14which will be so let's understand order
  11554. 7:20:15by
  11555. 7:20:19now this is one of the most used uh
  11556. 7:20:23concept that you're going to use because
  11557. 7:20:25in projects sorting
  11558. 7:20:27that you're going to use is everywhere,
  11559. 7:20:29right? Almost in every functionality or
  11560. 7:20:31most of the functionality I will say you
  11561. 7:20:33have to do some kind of sorting right so
  11562. 7:20:35here uh so let's understand so maybe
  11563. 7:20:37let's say let's do this one practically
  11564. 7:20:39that we wanted to sort our products from
  11565. 7:20:42cheapest to expensive right so that
  11566. 7:20:45means it will be in ascending order now
  11567. 7:20:47these are very simple concepts I'm just
  11568. 7:20:49going to do select and give me name and
  11569. 7:20:51the price of the product from my
  11570. 7:20:54products collection and you just have to
  11571. 7:20:57use order Y okay and then you have to
  11572. 7:21:00give the property and I wanted to do
  11573. 7:21:03what I wanted to sort based on this
  11574. 7:21:05price property right so and also what
  11575. 7:21:07kind of sort you wanted to do that also
  11576. 7:21:09we need to give so I wanted to give
  11577. 7:21:11ascending order so this will be my query
  11578. 7:21:13very very simple and if I just run this
  11579. 7:21:15and we'll see that this is going to give
  11580. 7:21:17me the name and the price in ascending
  11581. 7:21:19order so let's run this
  11582. 7:21:23okay and you can see that this is
  11583. 7:21:25working fine so from 25 to 15,990 29.
  11584. 7:21:29All right. So I think this is done. The
  11585. 7:21:31same thing you can do for descending
  11586. 7:21:33order also. So I'm just going to do
  11587. 7:21:35exact same. So I'll copy this, paste it
  11588. 7:21:38here. And this will be descending order.
  11589. 7:21:41Okay. Nothing else. So it will be from
  11590. 7:21:43expensive to cheapest. So do clear. And
  11591. 7:21:47now this will be opposite. That's it.
  11592. 7:21:49Nothing much to discuss here. Then what
  11593. 7:21:51else you can do? Right? You can also
  11594. 7:21:54sort multiple columns. Okay. So what I
  11595. 7:21:56mean by this? Now let's say I wanted to
  11596. 7:21:59do something like that. Okay, give me
  11597. 7:22:01name, give me the let's do category and
  11598. 7:22:05also give me the price from products and
  11599. 7:22:09now I'm just going to do order by so I
  11600. 7:22:12wanted to do category in ascending order
  11601. 7:22:15and I wanted to do price in descending
  11602. 7:22:17order. So this also you can do. So if I
  11603. 7:22:20now save this and let's see what we are
  11604. 7:22:22getting. So clear this and run it again.
  11605. 7:22:25So you can see that for price this will
  11606. 7:22:27be in the ascending order sorry
  11607. 7:22:29descending order and the category will
  11608. 7:22:30be in the ascending order. Okay.
  11609. 7:22:33So now uh what else I think for this one
  11610. 7:22:35this will be more than enough right? We
  11611. 7:22:36don't have any uh anything else to
  11612. 7:22:38discuss. Let's move to the next one
  11613. 7:22:40which will be limit and offset that we
  11614. 7:22:43need to discuss. So I'm just going to
  11615. 7:22:44create one more here and let's give this
  11616. 7:22:46one as underscore limit offset example.
  11617. 7:22:50Okay.
  11618. 7:22:52And pagination something like this.
  11619. 7:22:55Now these all are very very practical
  11620. 7:22:57thing. Okay. Now these you're going to
  11621. 7:22:58use let's say you're implementing pagin
  11622. 7:23:00essence. You have to implement all this
  11623. 7:23:01logic correct. Now also this is basic
  11624. 7:23:05pagination. I'm not going go into much
  11625. 7:23:07complicated things. Now what actually
  11626. 7:23:09limit and offset means so that you need
  11627. 7:23:11to understand. So limit means how many
  11628. 7:23:14rows you want to return. Okay. So limit
  11629. 7:23:16means how many rows you want to return
  11630. 7:23:22and what will be the offset. So offset
  11631. 7:23:26will be controls so offset is going to
  11632. 7:23:30control how many rows we want to skip.
  11633. 7:23:33So this will be how many rows
  11634. 7:23:37we want to skip. Right? So now let's say
  11635. 7:23:42how it will work. So for example, you
  11636. 7:23:44have a task that you only want to return
  11637. 7:23:46the five products. Okay. So I wanted to
  11638. 7:23:49return five products and I wanted to
  11639. 7:23:51sort the name of the product in
  11640. 7:23:53ascending order. So this is the query
  11641. 7:23:55you have to write. So how you can do
  11642. 7:23:57that? So let's say I wanted to do name,
  11643. 7:23:59give me name, give me the price of that
  11644. 7:24:02product,
  11645. 7:24:04right? And here I'm just going to do
  11646. 7:24:05this one as
  11647. 7:24:07from products.
  11648. 7:24:11Now I first I wanted to apply my order
  11649. 7:24:13by
  11650. 7:24:16name and this will be ascending
  11651. 7:24:18and now what you have to do you have to
  11652. 7:24:20give a limit property sorry this will be
  11653. 7:24:22limit property and then because I wanted
  11654. 7:24:25to return only five products so I wanted
  11655. 7:24:27to give the limit as five so this is
  11656. 7:24:29going to do what it's going to give me
  11657. 7:24:31selective columns all the concept you
  11658. 7:24:32have discussed previously so this is the
  11659. 7:24:35concept of selective columns this is
  11660. 7:24:36from where I wanted to get this is going
  11661. 7:24:38to order by means it's going to do the
  11662. 7:24:40sorting on name based on ascending order
  11663. 7:24:42and this will be the limit when this
  11664. 7:24:43many products I wanted to fetch and if I
  11665. 7:24:46now clear this let's run it here
  11666. 7:24:50I'll copy this
  11667. 7:24:52and you can see that you're getting only
  11668. 7:24:54five products right now remember it's
  11669. 7:24:56also going to do the this order by based
  11670. 7:24:59on name right it's going to do the in
  11671. 7:25:01ascending order and you are only getting
  11672. 7:25:03now let's say if I wanted to do price
  11673. 7:25:04here price I wanted to do in ascending
  11674. 7:25:07order now this will completely change
  11675. 7:25:08now you can see that you're getting in
  11676. 7:25:10ascending order. If you want to do in
  11677. 7:25:12descending order, you can also do that.
  11678. 7:25:14So you can let's say clear this and run
  11679. 7:25:17it again. So you are going to get in
  11680. 7:25:18descending order. But you are always
  11681. 7:25:20going to get only five products because
  11682. 7:25:22you are limiting the query by five
  11683. 7:25:24products. Okay. So this is the use of
  11684. 7:25:26limit. Now also now let's say in
  11685. 7:25:29normally what will happen in the
  11686. 7:25:30pagination. So in the first page you are
  11687. 7:25:32going to get so this is page number one
  11688. 7:25:34you're going to get five products. In
  11689. 7:25:36page number two you're going to get five
  11690. 7:25:37products. Or let's say you're going to
  11691. 7:25:39get the product from zero sorry 6 to 10.
  11692. 7:25:41Why? Because in first page you only have
  11693. 7:25:45get the five products. So you're going
  11694. 7:25:46to skip in third page again we are going
  11695. 7:25:48to skip all 10 products because in the
  11696. 7:25:51previous two pages we have already
  11697. 7:25:52rendered those products. So we don't
  11698. 7:25:54want to render. So this is where the
  11699. 7:25:55concept of offset comes. Okay. So let's
  11700. 7:25:58say what I need to do right I wanted to
  11701. 7:26:01show on page one example that we wanted
  11702. 7:26:03to do offset. Offset how offset is going
  11703. 7:26:06to work. So offset zero means we don't
  11704. 7:26:09want to skip anything. Offset five means
  11705. 7:26:12we wanted to skip five products. You're
  11706. 7:26:14getting what I mean? So let's do page
  11707. 7:26:16one example. So consider you're
  11708. 7:26:17implementing a page incent and I'm
  11709. 7:26:19implementing the page one uh products.
  11710. 7:26:22Okay? Or the query. So I'll do name give
  11711. 7:26:24me price from products.
  11712. 7:26:29Let's do order by only. Okay. So I'm
  11713. 7:26:30just going to do order by based on name
  11714. 7:26:32and I'll do ascending order only. And
  11715. 7:26:34here I'm just going to do limit five.
  11716. 7:26:36And now I need to do offset. So offset I
  11717. 7:26:40wanted to give zero. Why? Because when
  11718. 7:26:42you're going to render the first page
  11719. 7:26:45products, you are not going to skip
  11720. 7:26:47anything, right? Because in the first
  11721. 7:26:48page, you need to render all the first
  11722. 7:26:50products. So your offset will become
  11723. 7:26:52zero. So now if you just do this one,
  11724. 7:26:55right? First of all, clear this.
  11725. 7:26:57[snorts] And if I just run this, so
  11726. 7:26:59you're going to get five products. And
  11727. 7:27:00these will be the five products, the
  11728. 7:27:02first five products. Okay. Now let's see
  11729. 7:27:04how page two will look like. So I'm just
  11730. 7:27:06going to copy exact same thing and then
  11731. 7:27:09I'm just going to paste it here. What
  11732. 7:27:10will be the difference in the page two
  11733. 7:27:13what you have to do right? You have to
  11734. 7:27:14give a offset of five. Why? Because for
  11735. 7:27:16the page one you've already rendered the
  11736. 7:27:19five product. When you're going to
  11737. 7:27:20render the page two you want to skip
  11738. 7:27:22those five products and you want to
  11739. 7:27:23render the next set of products. Okay.
  11740. 7:27:26So if I now save this let's see how this
  11741. 7:27:28is working. And you'll see that [snorts]
  11742. 7:27:29if I clear this right and next time if I
  11743. 7:27:31render you can see that both the page
  11744. 7:27:34record is different because this is a
  11745. 7:27:36first page data this is a second page
  11746. 7:27:38data in the second page we already
  11747. 7:27:40skipped this one because we've already
  11748. 7:27:41rendered this five in the first page so
  11749. 7:27:43this is the concept of offset
  11750. 7:27:46now how the practical API example look
  11751. 7:27:49like right so in the practical example
  11752. 7:27:52what will happen right normally this is
  11753. 7:27:53how it will look so consider you have if
  11754. 7:27:56I'm not wrong so it would be page page
  11755. 7:27:58minus one into limit. So let's say you
  11756. 7:28:02are in the page two. So it will become 2
  11757. 7:28:05minus one. So into let's say you are
  11758. 7:28:08rendering five products. So five. So
  11759. 7:28:10that means how this will work. So let's
  11760. 7:28:12say in the zeroth page you're going to
  11761. 7:28:14render five for the second page. So this
  11762. 7:28:16will become five. For the third page
  11763. 7:28:19this will become 3 - 1 into 5. So this
  11764. 7:28:24will become 10. So when you're going to
  11765. 7:28:26go to the third page, your offset will
  11766. 7:28:28become 10. Correct? Because in the first
  11767. 7:28:30page, you've already rendered five
  11768. 7:28:32products. In the second page, you
  11769. 7:28:33already rendered five products. So total
  11770. 7:28:3510 products. So in the third page, you
  11771. 7:28:37want to skip 10 products. Correct?
  11772. 7:28:39Because you want to render from 11th
  11773. 7:28:41product. So this is how it will look. So
  11774. 7:28:43let's say if I just do same thing,
  11775. 7:28:45right? So this will be my third page
  11776. 7:28:47example here. So this will become 10. So
  11777. 7:28:49I wanted to skip 10 products because I
  11778. 7:28:52already rendered and you are going to
  11779. 7:28:54get the last set of products. So this
  11780. 7:28:57will be your concept of limit offset and
  11781. 7:29:00pagenation. Okay. So now uh I'm going to
  11782. 7:29:04pause here. It's not pause for you but
  11783. 7:29:06I'm going to pause and then we I'm going
  11784. 7:29:08to start in the next part the update
  11785. 7:29:10part and delete part. So let's see how
  11786. 7:29:12we we are going to do that in the next
  11787. 7:29:13section. All right. So moving to the
  11788. 7:29:15next one. So let's create the next file
  11789. 7:29:17which will be how we are going to update
  11790. 7:29:19single row. Okay. So I'll do update
  11791. 7:29:21single row dosql.
  11792. 7:29:24So let's see how we are going to update
  11793. 7:29:25data. This is also very very important.
  11794. 7:29:27Okay. So first of all let's do one
  11795. 7:29:29thing. Let's see that you can see that
  11796. 7:29:32we have this particular product right.
  11797. 7:29:33So we have this mechanical keyboard
  11798. 7:29:35electronics blah blah. So [snorts] what
  11799. 7:29:36I'm going to do right. So first I'm
  11800. 7:29:38going to see that whether this data is
  11801. 7:29:40present or not. So I'm just going to do
  11802. 7:29:41select name. Let's take price. Let's
  11803. 7:29:44take stock and also I'm going to take
  11804. 7:29:46the skew. So I'm just going to do from
  11805. 7:29:49products and then I'm simply going to do
  11806. 7:29:51where skew is equal to. So let's copy
  11807. 7:29:54this. Right? So I'm just going to copy
  11808. 7:29:56this
  11809. 7:29:58and then I'm just going to paste it
  11810. 7:29:59here. Okay. So this is the first thing.
  11811. 7:30:02So now let's go here and let's say I'm
  11812. 7:30:04just going to do clear
  11813. 7:30:07and let's run this file.
  11814. 7:30:14All right. So we have one data fine. Now
  11815. 7:30:17my task is that what I need to do right.
  11816. 7:30:19So let's say I wanted to update the
  11817. 7:30:22price from 349 to something else and
  11818. 7:30:25also I wanted to change the stock from
  11819. 7:30:2715 to 23. So how I can do that? So first
  11820. 7:30:30of all what we need to do right we need
  11821. 7:30:32to do update right? So we are simply
  11822. 7:30:34telling that we need to update this
  11823. 7:30:36particular product. But where we want to
  11824. 7:30:38update? So we wanted to update in a
  11825. 7:30:40table right? So you always need to give
  11826. 7:30:41the table name. So I want to update in
  11827. 7:30:44the products table. Now next will be
  11828. 7:30:46what you want to update. So this will be
  11829. 7:30:48set that okay set means okay this will
  11830. 7:30:50be my new value. So I wanted to set some
  11831. 7:30:52value. So set will be that okay I wanted
  11832. 7:30:54to make the price as 119.00
  11833. 7:30:58comma you can update multiple properties
  11834. 7:31:00also rows also and I wanted to update
  11835. 7:31:02stock from 15 to 23. That's it. But this
  11836. 7:31:06is not the case. Why? Because this is a
  11837. 7:31:08single row update. So you need to tell
  11838. 7:31:10that okay because your SQL doesn't know
  11839. 7:31:12right that which product you wanted to
  11840. 7:31:14update you can have 100 product thousand
  11841. 7:31:16product lots of products. So here we
  11842. 7:31:18need to use where clause not wear clause
  11843. 7:31:20where filter so [snorts] here I'm just
  11844. 7:31:22going to do skew and then I'm going to
  11845. 7:31:24simply tell that okay you are updating
  11846. 7:31:25this particular product here. Okay so
  11847. 7:31:29I'm simply telling that update products
  11848. 7:31:31table change the price from that
  11849. 7:31:33particular value to 119.00 0 0 5 0
  11850. 7:31:36whatever you wanted to give stock I
  11851. 7:31:38wanted to update to 23 where the skew is
  11852. 7:31:41equal to this skew
  11853. 7:31:43I hope this is clear right so now what I
  11854. 7:31:46going to do right let's copy this and
  11855. 7:31:48then I'm just going to run it here so
  11856. 7:31:50what I'm doing that I'm first checking
  11857. 7:31:52if that is present I'm updating the data
  11858. 7:31:54and then I want to see the updated data
  11859. 7:31:57that whether the update is actually
  11860. 7:31:59happened or not so now let's run it once
  11861. 7:32:01again and we'll see that what is
  11862. 7:32:02happening here okay so I'm just going to
  11863. 7:32:04run here all right now you can See that
  11864. 7:32:06we are getting 349. Now it's changed to
  11865. 7:32:0811 1 9 stock changed from 15 to 23. It's
  11866. 7:32:11very simple to understand the key that
  11867. 7:32:13you need to know that how we going to
  11868. 7:32:15set the data. Set data means you need to
  11869. 7:32:17use this set whatever properties wanted
  11870. 7:32:19update where this is the important part
  11871. 7:32:21because you are updating a single row.
  11872. 7:32:23Okay. So this is all about the single
  11873. 7:32:25row update uh updated updates. Now
  11874. 7:32:28[snorts] next will be uh let's take
  11875. 7:32:30multiple rows. So I'm just going to take
  11876. 7:32:3213. Let's do update
  11877. 7:32:35multiple
  11878. 7:32:37rows
  11879. 7:32:38SQL. Okay. Now here first of all let's
  11880. 7:32:43see what we can do. So first of all
  11881. 7:32:45let's do one thing. Let's see we have
  11882. 7:32:47some stationary product right. So this
  11883. 7:32:48stationary product that we have. So
  11884. 7:32:51first I want to see that okay how many
  11885. 7:32:53stationary product I have. Okay. So that
  11886. 7:32:55will be my first step. So let's do one
  11887. 7:32:56thing. So I'm just going to do select.
  11888. 7:32:58So do name. Give me also category. also
  11889. 7:33:01give me price and let's say I wanted to
  11890. 7:33:03also get the is active from where so
  11891. 7:33:06from products table and then I'm simply
  11892. 7:33:08going to do where I wanted to check the
  11893. 7:33:10category is equal to stationary so I'm
  11894. 7:33:12just going to copy this and let's paste
  11895. 7:33:14it here okay so this is the first thing
  11896. 7:33:16copy this sorry save this and then let's
  11897. 7:33:18run it first let's copy here
  11898. 7:33:24and then I'm simply going to paste it
  11899. 7:33:28so we have two data makes sense
  11900. 7:33:31what I need to do right so let's say you
  11901. 7:33:33have some requirement that what you need
  11902. 7:33:34to do you need to update all the
  11903. 7:33:36stationary products prices these prices
  11904. 7:33:39by 10%. So you have to increase that.
  11905. 7:33:41Okay. So how we are going to do that?
  11906. 7:33:44Now here definitely you don't want to do
  11907. 7:33:46let's say you have 1,000 data you you
  11908. 7:33:48are going to do single single update. No
  11909. 7:33:50right. So there has to be some way that
  11910. 7:33:52where you actually update all the
  11911. 7:33:54products price by 10% at once. So to do
  11912. 7:33:58this what we can do right simply I'm
  11913. 7:34:00just going to do update
  11914. 7:34:02and in this update so we are going to
  11915. 7:34:04simply again this is same products. So
  11916. 7:34:06I'm just going to do set and I'm going
  11917. 7:34:08to do okay price now equal to so I'm
  11918. 7:34:10going to take a round and I'm simply
  11919. 7:34:12going to say that okay this price will
  11920. 7:34:14now become 10% more or whatever you want
  11921. 7:34:17to do let's say you want to increase by
  11922. 7:34:18+ 10 whatever you want to do for now I'm
  11923. 7:34:20just going to increase by 10% and I'm
  11924. 7:34:22going to take this one comma 2 right so
  11925. 7:34:24round two means it's going to keep our
  11926. 7:34:26two decimal places sorry this will be
  11927. 7:34:27two and now the important part how you
  11928. 7:34:30are going to update also here you need
  11929. 7:34:32to just pass that okay where the
  11930. 7:34:33category is stationary so where the
  11931. 7:34:35category is stationary simply telling
  11932. 7:34:37that take all the stationary products
  11933. 7:34:40increase the price by 10% and then you
  11934. 7:34:42can also check it once again right so
  11935. 7:34:43I'm just going to copy this and then you
  11936. 7:34:45can run it here save this and you can
  11937. 7:34:47see that what we are getting right so
  11938. 7:34:49let's clear this run it again and you
  11939. 7:34:52can see that this is increased by 10%
  11940. 7:34:54this is also increased by 10% right so
  11941. 7:34:5625 into 10 into 10% 2.5 so this is 27.5
  11942. 7:35:00and you can see this is updated in both
  11943. 7:35:02the rows so this is how you can update
  11944. 7:35:04multiple column s at once. You can also
  11945. 7:35:07do right. So let's say uh what you want
  11946. 7:35:10to do right you want to mark out of
  11947. 7:35:12stock products inactive. So how you can
  11948. 7:35:14do that? So to do this one what you can
  11949. 7:35:16do right you can just do update and
  11950. 7:35:18let's say you want to do products and
  11951. 7:35:20then you want to do set is active equal
  11952. 7:35:23to false. So you're changing all from
  11953. 7:35:25true to false where the stock is equal
  11954. 7:35:29to zero. So this is going to do what?
  11955. 7:35:31whenever there will be any products that
  11956. 7:35:34is out of stock. So zero means it's out
  11957. 7:35:36of stock. So you're setting that product
  11958. 7:35:38as inactive. That's it. And then you can
  11959. 7:35:40if you want to check it. So you do
  11960. 7:35:42select name stock and let's say is
  11961. 7:35:45active from products.
  11962. 7:35:49And then I'm simply going to do where is
  11963. 7:35:52active is equal to false. Okay. So this
  11964. 7:35:55is where you're going to get this one.
  11965. 7:35:57So I'm just going to comment it out
  11966. 7:35:58here. Save this.
  11967. 7:36:01Clear this and let's run it again. So
  11968. 7:36:03you have only one. So this is changed to
  11969. 7:36:05false. So we have old monitor. Let's see
  11970. 7:36:08where we have old monitor and this is
  11971. 7:36:09the stock is zero. So we're changing
  11972. 7:36:11this one to false. Okay. But this is how
  11973. 7:36:14you can update multiple product at once.
  11974. 7:36:17Okay. Okay. So let's move to the next
  11975. 7:36:19one which will be delete functionality.
  11976. 7:36:21So for delete what I will do right. We
  11977. 7:36:22are going to add one particular data and
  11978. 7:36:24then we're going to delete that data
  11979. 7:36:26only. So we we will not delete any of
  11980. 7:36:28the existing all these data. So first
  11981. 7:36:30let's insert one data here. So I'm just
  11982. 7:36:31going to do insert into products and
  11983. 7:36:35what are the things? So we have name, we
  11984. 7:36:37have category, we have price, let's add
  11985. 7:36:41stock. So skew and also we are going to
  11986. 7:36:44have the description. So here we will do
  11987. 7:36:47values. And now let's do here temp
  11988. 7:36:51product
  11989. 7:36:53to be deleted. Means we going to delete
  11990. 7:36:55the same product.
  11991. 7:36:58Then take uh category. So I'm just going
  11992. 7:37:00to take this one as electronics only.
  11993. 7:37:03Let's do here uh price maybe 345.45.
  11994. 7:37:08Stock let's take 45. SK I'm just going
  11995. 7:37:10to do electronic temp 009 or 013
  11996. 7:37:14whatever you wanted to take. And what
  11997. 7:37:17else? I'm just going to do uh temp
  11998. 7:37:20product disk. So this is going to insert
  11999. 7:37:23that product. And now what I wanted to
  12000. 7:37:25do first I wanted to check if the
  12001. 7:37:27insertion is done or not. So I'm just
  12002. 7:37:28going to select name category and skew.
  12003. 7:37:32So I'm just going to do prop from
  12004. 7:37:34products. And here I'm just going to
  12005. 7:37:36check where skew is equal to this skew
  12006. 7:37:41that we have added this one. So copy
  12007. 7:37:43this and I'm going to paste it here.
  12008. 7:37:45Okay. So this will be my first step. So
  12009. 7:37:47now let's run it. So I'm just going to
  12010. 7:37:51copy this.
  12011. 7:37:54Okay. So we have one data that we have.
  12012. 7:37:56Sorry. Yeah. One data that we are going
  12013. 7:37:58to delete. So now we need to delete
  12014. 7:38:00this. Now deleting is very simple. So
  12015. 7:38:02first what we can do right. So let's
  12016. 7:38:04comment it out because we've already
  12017. 7:38:06inserted that into our DB and then I'm
  12018. 7:38:08just going to do here delete. So delete
  12019. 7:38:11word you have to use from where? From
  12020. 7:38:13products. So this is the table. And then
  12021. 7:38:15simply mention that okay where the skew
  12022. 7:38:17is equal to this particular skew simple
  12023. 7:38:20and this will delete and then you want
  12024. 7:38:22to check it. So just copy this and then
  12025. 7:38:25just paste it here again. So simply we
  12026. 7:38:27are first checking if this present then
  12027. 7:38:28we are deleting and then we are checking
  12028. 7:38:30again if that is present. So in that
  12029. 7:38:32case it should not give any result.
  12030. 7:38:34Okay. [snorts] So if I just go here and
  12031. 7:38:36let's run it. So I'll do clear run it
  12032. 7:38:38again. You can see that first time you
  12033. 7:38:40have deleted one and then you don't have
  12034. 7:38:42anything else. Now there are much
  12035. 7:38:44nothing much to discuss here. These
  12036. 7:38:46things are very very simple but this is
  12037. 7:38:48how you need to delete. So moving to the
  12038. 7:38:50next one which will be the last one in
  12039. 7:38:52this particular section which will be
  12040. 7:38:53you need to understand what is returning
  12041. 7:38:56concept. So I'm just going to do 15 and
  12042. 7:38:58let's do this one returning after insert
  12043. 7:39:02dosql. So here what returning is going
  12044. 7:39:05to do? Returning is going to gives back
  12045. 7:39:07the affected rows after you will do
  12046. 7:39:10anytime insert update or delete. So I'm
  12047. 7:39:12just going to do returning usually
  12048. 7:39:15returns back
  12049. 7:39:18the rows returns back the rows
  12050. 7:39:22immediately
  12051. 7:39:24after
  12052. 7:39:26insert or let's say you're doing update
  12053. 7:39:29or whatever even you are doing uh doing
  12054. 7:39:31delete. So here this is actually
  12055. 7:39:34extremely helpful and you'll see that
  12056. 7:39:35even if I'm created I created any videos
  12057. 7:39:37previously on posgress for our DBs I
  12058. 7:39:41have used this one in multiple places.
  12059. 7:39:42Okay so whenever I'm going to insert
  12060. 7:39:44something immediately I wanted to check
  12061. 7:39:46that okay return me that inserted data
  12062. 7:39:48to see that whether it is done or not as
  12063. 7:39:50simple as that. So to do this you have
  12064. 7:39:52to do insert into first you have to give
  12065. 7:39:54the table name and here again pass the
  12066. 7:39:56name category then what we have we have
  12067. 7:39:59the price we have the stock we have the
  12068. 7:40:02skew and also you have the description
  12069. 7:40:04right so all these things and then let's
  12070. 7:40:05say I wanted to do values and then I'm
  12071. 7:40:07just going to do maybe let's take any
  12072. 7:40:09random webcam camera
  12073. 7:40:13uh I'm just going to do this one as
  12074. 7:40:14electronics
  12075. 7:40:17let's give some price
  12076. 7:40:20uh talk and also webcam camera
  12077. 7:40:26description. Okay. So now what I need to
  12078. 7:40:30do I need to return back. So I wanted to
  12079. 7:40:32see that if the insertion is done or
  12080. 7:40:34not. So for this one we need to do
  12081. 7:40:37returning. We have to use returning. So
  12082. 7:40:40what I need to return. So once the
  12083. 7:40:43product is now added. So what will
  12084. 7:40:44happen? Postgress is going to create a
  12085. 7:40:46UID right? So that will be a unique ID.
  12086. 7:40:49So I will be having the ID. Similarly in
  12087. 7:40:51MongoDB you are going to have something
  12088. 7:40:52called uh underscore id. Remember this
  12089. 7:40:55is a document ID. So similarly we are
  12090. 7:40:57going to have the ID. So I wanted to see
  12091. 7:40:59that actually the product is created. If
  12092. 7:41:01the product is created I will be having
  12093. 7:41:03some ID. I'll be having the name. I
  12094. 7:41:05wanted to return the category. I wanted
  12095. 7:41:07[snorts] to return the price. I wanted
  12096. 7:41:09to return the stock and also let's say I
  12097. 7:41:11wanted to return created
  12098. 7:41:14add. So this is what I wanted to do.
  12099. 7:41:16Okay. So now can save this and let's say
  12100. 7:41:20we are going to run this. So let's clear
  12101. 7:41:22this
  12102. 7:41:24rename copy this and let's run it here.
  12103. 7:41:30What you're getting insert has more than
  12104. 7:41:34name 1 2 3 4 5 6
  12105. 7:41:41oh skew we haven't added my bad man. So
  12106. 7:41:44you have to do electronic
  12107. 7:41:46uh let's do webcam 009.
  12108. 7:41:50So now let's save this clear this and
  12109. 7:41:53now we going to run it again and we'll
  12110. 7:41:54see what we're going to get
  12111. 7:41:57and you can see that we have ID name
  12112. 7:41:58category price stock created. So you're
  12113. 7:42:00getting the data right and you see that
  12114. 7:42:02immediately it's returning right so this
  12115. 7:42:04is your unique ID it's created. So this
  12116. 7:42:06is what returning usually does. So it's
  12117. 7:42:09return back the result or the rows
  12118. 7:42:11immediately after your insert. You can
  12119. 7:42:14also use update right. So let's say
  12120. 7:42:16you've done insert right. You can also
  12121. 7:42:18use update and then you can return the
  12122. 7:42:20data also. For example let's say I
  12123. 7:42:21wanted to update products
  12124. 7:42:24and then I wanted to do set
  12125. 7:42:28I wanted to set the stock is equal to
  12126. 7:42:30stock + 10. So here it will become 56.
  12127. 7:42:35Let's do 11. So 67 and then I'm just
  12128. 7:42:37going to do where skew is equal to this
  12129. 7:42:41skew just now I have created. Okay. So
  12130. 7:42:43where the skew is this and then I wanted
  12131. 7:42:45to use returning. So here also you can
  12132. 7:42:47return ID name and then also let's say
  12133. 7:42:50you want to return the stock. So what
  12134. 7:42:51you're doing you're updating the product
  12135. 7:42:53where the SQ is this and then at the end
  12136. 7:42:55you're using returning. So returning
  12137. 7:42:57clause you're using which is going to
  12138. 7:42:58return. So I'm just going to clear and
  12139. 7:43:00then if you run again you can see that
  12140. 7:43:02now this will become 67. Okay. So the
  12141. 7:43:05stock
  12142. 7:43:07all right so I think this is done same
  12143. 7:43:08also you can do for delete also let's
  12144. 7:43:10say you want to delete this you can use
  12145. 7:43:12delete from products where skew is equal
  12146. 7:43:18to this
  12147. 7:43:22and then you are going to use returning
  12148. 7:43:25so you're going to return the ID name
  12149. 7:43:27and the skew so this is going to delete
  12150. 7:43:29and if you want to see that if the
  12151. 7:43:31delete is successful or not you'll see
  12152. 7:43:32that if I use select now right that
  12153. 7:43:35product will not be present because that
  12154. 7:43:36will be deleted. So I'm just going to do
  12155. 7:43:38form. So this will become products where
  12156. 7:43:41the skew is equal to this.
  12157. 7:43:46Save this and see what will happen.
  12158. 7:43:51You can see so this is deleted. So
  12159. 7:43:52you're getting the ID name and once the
  12160. 7:43:55delete is deletion is successful you you
  12161. 7:43:57don't have that product present. So this
  12162. 7:44:00is how you can use the returning
  12163. 7:44:02concept. Okay. So this is very very very
  12164. 7:44:05helpful. So with this I think we have
  12165. 7:44:07completed the part two and now let's
  12166. 7:44:09move on to the next one which will be
  12167. 7:44:10our last part. So there we are going to
  12168. 7:44:12cover some of the little uh I not say
  12169. 7:44:15advanced concept but intermediate to
  12170. 7:44:17advanced level of concept. We need to
  12171. 7:44:19understand [snorts] the concept of
  12172. 7:44:20foreign keys one to many relationship
  12173. 7:44:23many to many inner join left join
  12174. 7:44:26aggregate functions group by the having
  12175. 7:44:29all these things we're going to cover in
  12176. 7:44:31the next section. All right. So now we
  12177. 7:44:33are almost at the end of section which
  12178. 7:44:35will be part three. Not here we have to
  12179. 7:44:38create outside and this also is very
  12180. 7:44:40very important. So we are going to cover
  12181. 7:44:42relationship uh joins aggregate
  12182. 7:44:45functions group by indexes all these
  12183. 7:44:47things. Okay. And first file will be so
  12184. 7:44:49for this one what we going to do right
  12185. 7:44:51we going to create a reset and seed
  12186. 7:44:55relationships.
  12187. 7:44:57So we are going to first add our data
  12188. 7:44:59then only after that we'll be able to
  12189. 7:45:01start uh understanding all the concepts.
  12190. 7:45:04So first what we going to do right? So
  12191. 7:45:05let's create the extension. I'm not
  12192. 7:45:07going to explain most of the things
  12193. 7:45:08because these things we have already
  12194. 7:45:09covered in the previous part. Extension
  12195. 7:45:13if not exist and I'm just going to use
  12196. 7:45:16pg crypto.
  12197. 7:45:19Okay. Now what we going to do right? See
  12198. 7:45:22these will be relationship sections
  12199. 7:45:24right? So what we need to do we can't
  12200. 7:45:26have one single table. We need to have
  12201. 7:45:27multiple tables so that we can
  12202. 7:45:29understand the relationship between
  12203. 7:45:30tables the properties and everything. So
  12204. 7:45:33first I'm going to drop some of the
  12205. 7:45:34tables in case we have all those tables
  12206. 7:45:36so that we can start fresh. So I'm start
  12207. 7:45:39with drop tables if exist. So we going
  12208. 7:45:42to have one post tags table. Copy this.
  12209. 7:45:46We also going to have one more table
  12210. 7:45:48that will be for comments. So comments
  12211. 7:45:50table we also going to have one for post
  12212. 7:45:53table one will be for only tags and at
  12213. 7:45:57the end we are going to have one for
  12214. 7:45:58users. Okay. So these many tables we are
  12215. 7:46:02going to have now let's create all the
  12216. 7:46:04tables one by one. It's very very
  12217. 7:46:06important to understand all these things
  12218. 7:46:07that what are the properties that we are
  12219. 7:46:09taking. Why? Because our entire section
  12220. 7:46:11this part three will be dependent on
  12221. 7:46:13these tables only. Okay. So let's start
  12222. 7:46:15with the first table. So I'm just going
  12223. 7:46:17to do create table and I'm start with
  12224. 7:46:19maybe let's do users. Okay. So in the
  12225. 7:46:21users first I'm going to have a ID u ID
  12226. 7:46:24and this will be my primary key and here
  12227. 7:46:28I'm just going to take a default
  12228. 7:46:29property and I'm just going to take here
  12229. 7:46:31gen random uu id. Okay. So this will be
  12230. 7:46:35the ID of that user. All right. So now
  12231. 7:46:38after this I'm simply going to take one
  12232. 7:46:39more property. Let's take a name. So
  12233. 7:46:41every user is going to have a name and
  12234. 7:46:43this will be not null. Okay. So these
  12235. 7:46:46two things we are going to have for this
  12236. 7:46:47particular table. Now moving to the next
  12237. 7:46:49one. Next will be our second table which
  12238. 7:46:51will be create table posts. Now this
  12239. 7:46:55will become now very very important.
  12240. 7:46:57Okay. So first every post is going to
  12241. 7:46:59have a ID. So I'm just going to copy
  12242. 7:47:00this and then we're going to paste it
  12243. 7:47:02here. And now this is where we are going
  12244. 7:47:05to start our relationship. So every user
  12245. 7:47:08can create multiple post. So that means
  12246. 7:47:09there has to be some relation so that we
  12247. 7:47:11can understand okay this particular post
  12248. 7:47:13is created by which user. So we are
  12249. 7:47:16going to have a user ID property and
  12250. 7:47:18this user ID will be my user ID of this
  12251. 7:47:21particular table. Right? So that let's
  12252. 7:47:23say we have a post five fifth post is
  12253. 7:47:26created by user number two. So from this
  12254. 7:47:28user ID I will be able to understand
  12255. 7:47:30okay this is created by user number two
  12256. 7:47:32whose ID is this. So here we are going
  12257. 7:47:34to have UID which will be not null. And
  12258. 7:47:37now we are going to understand one very
  12259. 7:47:38important concept that is references. So
  12260. 7:47:40how we are going to tell that okay this
  12261. 7:47:41user id belongs to this particular users
  12262. 7:47:44do id. So this is where we are going to
  12263. 7:47:46have something called references. These
  12264. 7:47:49references now I'm going to tell okay
  12265. 7:47:51this is my users table and this will be
  12266. 7:47:53my ID. I hope this is clear. Next let's
  12267. 7:47:56take a title for every post. We going to
  12268. 7:47:58take a title.
  12269. 7:48:01Sorry if you're getting any noise. I
  12270. 7:48:03think it's some bike noise from outside.
  12271. 7:48:05It's coming. These people are crazy.
  12272. 7:48:07Okay, so title I'm just going to take
  12273. 7:48:09text and this will be not null. And then
  12274. 7:48:12I'm also going to take a status for
  12275. 7:48:14every post you're going to have a status
  12276. 7:48:16because later when we going to
  12277. 7:48:18understand the concept of where group by
  12278. 7:48:20having all these things this actually is
  12279. 7:48:23needed this particular property. So for
  12280. 7:48:25this one you're going to take this one
  12281. 7:48:26as text and I'm just going to do not
  12282. 7:48:28null. Let's take a default property and
  12283. 7:48:30for default I'm going to maybe let's
  12284. 7:48:32give this one as wrapped. And here I'm
  12285. 7:48:34also give a check property. So let's
  12286. 7:48:36give a check property for this. So I'm
  12287. 7:48:38just going to do this one as status. And
  12288. 7:48:41status I'm going to give in. So it can
  12289. 7:48:43be either dropped or it can be either
  12290. 7:48:46published.
  12291. 7:48:48Okay. So this is another property. Next
  12292. 7:48:51what else we can take? So let's take one
  12293. 7:48:54views property. So here I'm just going
  12294. 7:48:56to take a views property which will be
  12295. 7:48:58integer. I'm just going to take not
  12296. 7:49:00null. Let's take a default. I'm going to
  12297. 7:49:02do zero. Let's take a check property and
  12298. 7:49:05here I'm just going to take views. These
  12299. 7:49:07views has to be greater than or equal
  12300. 7:49:09zero. So this is the check that we are
  12301. 7:49:11going to have. So these all the
  12302. 7:49:13properties this post table is going to
  12303. 7:49:15have. Then we going to take our comments
  12304. 7:49:18table. Now let's create again. So do
  12305. 7:49:20create table and I'm going to do
  12306. 7:49:22comments. For this one again we are
  12307. 7:49:24going to have the ID. So copy this
  12308. 7:49:27paste it here. Now what else this is
  12309. 7:49:29going to have? Every comment that you're
  12310. 7:49:31going to have will be associated to some
  12311. 7:49:33post, right? Let's say you're using
  12312. 7:49:35Facebook or Insta for a particular post.
  12313. 7:49:37There can be thousands of comments. But
  12314. 7:49:39you need to identify that okay, this
  12315. 7:49:41comment is belongs to which post,
  12316. 7:49:43correct? So that is the reason we need
  12317. 7:49:44to have one more reference set which
  12318. 7:49:46will be post ID. And here you have to
  12319. 7:49:48take UID and this will be again not
  12320. 7:49:50null. And here you have to take
  12321. 7:49:52references. And then I'm just going to
  12322. 7:49:55denote to my post
  12323. 7:49:58reference to my post table and it will
  12324. 7:50:00reference to this post id. So I'm just
  12325. 7:50:02going to give this one as id. Okay. And
  12326. 7:50:04then also let's take a body which will
  12327. 7:50:06be make actual comment. So this will be
  12328. 7:50:08text and I'm just going to give this one
  12329. 7:50:10as not null. So these three things our
  12330. 7:50:12comments table is going to have. Okay.
  12331. 7:50:15So now uh next what else we have? We
  12332. 7:50:17also need to take our tags table. So I'm
  12333. 7:50:19just going to create one more table.
  12334. 7:50:21Let's give this one as tags. Let's take
  12335. 7:50:24our ID.
  12336. 7:50:26Okay. And here I'm just going to take a
  12337. 7:50:28name. I'll do text not null. And for
  12338. 7:50:32every tag it has to be unique. So I'm
  12339. 7:50:34going to take a unique property. Okay.
  12340. 7:50:36And then at the end we are going to have
  12341. 7:50:38our post tags. Uh now this is actually a
  12342. 7:50:41junction table. And why actually you
  12343. 7:50:43need it? Now let's say one particular
  12344. 7:50:45post can have multiple tags and one tag
  12345. 7:50:48can belongs to many posts. Now I I know
  12346. 7:50:50that this might be little tricky for
  12347. 7:50:51someone uh like if you're a beginner to
  12348. 7:50:53understand but if you give little bit of
  12349. 7:50:55time you'll see that everything is
  12350. 7:50:56actually connecting or connected with
  12351. 7:50:58each other and this is actually called
  12352. 7:51:00many to many relationships. So I'm going
  12353. 7:51:02to repeat once again this post tags. So
  12354. 7:51:04one post let's say you have one post
  12355. 7:51:06that can have multiple tags but one tag
  12356. 7:51:09let's say there is a tag ReactJS. Now
  12357. 7:51:11you have one particular post and that
  12358. 7:51:13particular post has a tag reactjs and
  12359. 7:51:16this particular reactjs tag can belong
  12360. 7:51:18to multiple post right so let's say you
  12361. 7:51:20have 10 post and this reactj tag can be
  12362. 7:51:22present in five post so that means one
  12363. 7:51:25post can have many tags one tag can
  12364. 7:51:28belong to multiple post so it's a many
  12365. 7:51:30to many relations so here what I'm going
  12366. 7:51:32to do I'm just going to create one more
  12367. 7:51:33create table and let's give this one as
  12368. 7:51:36post tags and here what we need to do
  12369. 7:51:39right now Remember here we need to have
  12370. 7:51:42a post id because one post can have
  12371. 7:51:45multiple many texts. We need to
  12372. 7:51:46understand that this is belongs to which
  12373. 7:51:47post? This will be UID
  12374. 7:51:50uh not null and I have to give
  12375. 7:51:52references and this will be post table
  12376. 7:51:55id
  12377. 7:51:56and then I'm also going to have a tag
  12378. 7:51:58id. So I'm just going to do tag ID. This
  12379. 7:52:01is also u ID. So I'll do not null. Let's
  12380. 7:52:04do references. And this will be this
  12381. 7:52:06particular tags table that we have
  12382. 7:52:07created. So this will be tags id. Okay.
  12383. 7:52:10So this will be the relations and also
  12384. 7:52:13let's take we need to take a primary key
  12385. 7:52:15here. So what actually we are doing
  12386. 7:52:16here? We are going to take a composite
  12387. 7:52:18primary key. That means these pair that
  12388. 7:52:20whatever we are taking that must be
  12389. 7:52:22unique. Okay. Why? Now you might ask me
  12390. 7:52:25why? Because if you have one particular
  12391. 7:52:27post the same post cannot be connected
  12392. 7:52:30to same tag twice. You are getting what
  12393. 7:52:32I'm telling. So let's say you have a
  12394. 7:52:33particular post and there is a tag
  12395. 7:52:35ReactJS. You can't add that same ReactJS
  12396. 7:52:38tag two times. Okay. So how you we can
  12397. 7:52:41do that? So I'm just going to take a
  12398. 7:52:42primary key here and let's pass this
  12399. 7:52:44post id, right? And then you have the
  12400. 7:52:47tag ID. So this is going to create a
  12401. 7:52:49composite primary key. So these all the
  12402. 7:52:53uh tables that we need. Okay. So we'll
  12403. 7:52:55start with users ID and name. Post ID,
  12404. 7:52:58user ID, reference, title, status and
  12405. 7:53:00views. Comments ID, post ID because
  12406. 7:53:03every comment is belongs to a post.
  12407. 7:53:05Tags, this is just a unique tag. Uh and
  12408. 7:53:08because this will be unique, right? Now
  12409. 7:53:10this is important. This post tag, every
  12410. 7:53:12post can belong to one tag and one tag
  12411. 7:53:14can belong to multiple post. Okay, with
  12412. 7:53:16a composite primary key. So now what I
  12413. 7:53:19have done right, I actually have created
  12414. 7:53:20all the data that we are going to seed.
  12415. 7:53:23Now what is this? So these are the dummy
  12416. 7:53:24data that I'm going to add and based on
  12417. 7:53:26this data we are going to understand all
  12418. 7:53:28the concept one by one. But let's do
  12419. 7:53:30this one in the next part. Till this
  12420. 7:53:31part this is all the tables. Do this you
  12421. 7:53:34can pause this particular part. I will
  12422. 7:53:36highly suggest and try to go through all
  12423. 7:53:39the properties or all the rows that we
  12424. 7:53:41have created for each and every table.
  12425. 7:53:42It's very very important to understand
  12426. 7:53:44all the concepts that we are going to
  12427. 7:53:46discuss in the next part. All right. So
  12428. 7:53:47let's add all the data one by one and
  12429. 7:53:49then we are going to run this particular
  12430. 7:53:51file because until unless we're going to
  12431. 7:53:52seed our data in our DB and create the
  12432. 7:53:54tables we can't start all the concepts
  12433. 7:53:56like foreign keys one one to many
  12434. 7:53:58relations many to many relations inner
  12435. 7:54:00joint lab join so many things are there
  12436. 7:54:02so first [snorts] let's uh copy this one
  12437. 7:54:04so I'm just going to copy this paste it
  12438. 7:54:06here and see what we are doing simply we
  12439. 7:54:08are inserting the two users we have two
  12440. 7:54:11users on and Rahul into our users table
  12441. 7:54:15then add uh these three I'm going to
  12442. 7:54:17copy and I can clearly understand what
  12443. 7:54:20we are doing here. It's not that much
  12444. 7:54:21difficult to understand. So we are
  12445. 7:54:23inserting into post table user id title
  12446. 7:54:27status and views right. So here ID how
  12447. 7:54:31we going to get so from users where the
  12448. 7:54:33name is this particular name because
  12449. 7:54:34we've already added this in our users
  12450. 7:54:36table. So we have added two post here
  12451. 7:54:39and one is for Rahul user one post.
  12452. 7:54:42Okay. Next let's copy these two. We also
  12453. 7:54:46need to add some comment. So I'm just
  12454. 7:54:48going to paste it here. So what actually
  12455. 7:54:50we doing? We are inserting comment post
  12456. 7:54:52body.
  12457. 7:54:54So id ID how we are going to get? So
  12458. 7:54:55from a post you can see that this
  12459. 7:54:57postgress SQL join explained this is one
  12460. 7:54:59of the post that I have added here. So
  12461. 7:55:01this id is belong because we already
  12462. 7:55:03have the relations in the comments
  12463. 7:55:05table. If you go to the comments table,
  12464. 7:55:07you'll see that there is a relations
  12465. 7:55:08with post ID which is your post ID. So
  12466. 7:55:11that means when we we are setting this
  12467. 7:55:14particular data this id we are going to
  12468. 7:55:17get from post where the title is equal
  12469. 7:55:19to this and this will be for this
  12470. 7:55:21particular title. Now you see back end
  12471. 7:55:23API with posgressql this is created by
  12472. 7:55:26Rahul user and this is created by this
  12473. 7:55:28particular postgress SQL join explain is
  12474. 7:55:30created by on my user. Okay and [snorts]
  12475. 7:55:33then I think let's add uh this tag. So
  12476. 7:55:36I'm also going to add two unique tag. So
  12477. 7:55:38let's add this into our tag. So you have
  12478. 7:55:40SQL and back end. That's it. Next, we
  12479. 7:55:44also need to add our post tag. And then
  12480. 7:55:47at the end, we're going to have our uh
  12481. 7:55:49select message. So see what we doing
  12482. 7:55:52here. So for this one, what we doing,
  12483. 7:55:54right? So post ID, we have the post ID
  12484. 7:55:55and the tag ID, remember? So we taking
  12485. 7:55:58some allis. So from post we are taking
  12486. 7:56:00as a T. Tags we're taking as a T. So you
  12487. 7:56:03take the post ID and the tag ID where
  12488. 7:56:05the post title is postgql joint explain
  12489. 7:56:08and the tag ID is SQL because we have
  12490. 7:56:09already added the SQL here. Okay. And
  12491. 7:56:12the one we have added for I think uh
  12492. 7:56:14backend tag. Okay. So this is going to
  12493. 7:56:16insert in our post tag collection. And
  12494. 7:56:19then we just logging this. Now save this
  12495. 7:56:23and then we need to run this. Okay. So
  12496. 7:56:25first let's clear this
  12497. 7:56:29and I'm going to copy this.
  12498. 7:56:32Let's run it here.
  12499. 7:56:35Okay. So now this is going to create all
  12500. 7:56:38of this. It's going to insert the data.
  12501. 7:56:40And if you want you can just see all of
  12502. 7:56:41this here also. If I now refresh this.
  12503. 7:56:46See how slow this is. Man,
  12504. 7:56:52I think this would solve this one very
  12505. 7:56:55quickly. See, we have the commands, post
  12506. 7:56:57tag, post, product, tags, and users. And
  12507. 7:56:59if you right click here,
  12508. 7:57:04so you can see that you can count rows
  12509. 7:57:05and you can also view the data. If I do
  12510. 7:57:07all rows, right, this will load forever.
  12511. 7:57:10So for now, I'm not going to do that. If
  12512. 7:57:12you're using Windows, right, just do
  12513. 7:57:13right click and do view data. you'll be
  12514. 7:57:14able to see all the data that we have
  12515. 7:57:16created here. Okay? Or else you can also
  12516. 7:57:18directly um do from here. Right? So for
  12517. 7:57:21example, let's say uh I'll [snorts] just
  12518. 7:57:23do here only. Right? Maybe let's do
  12519. 7:57:24select
  12520. 7:57:28select star from and then you're going
  12521. 7:57:31to give all the tables and everything.
  12522. 7:57:33For example, let's say post tags, right?
  12523. 7:57:36Something like this. And then you'll be
  12524. 7:57:37able to do that also. I think we have
  12525. 7:57:39already done this one part one and two.
  12526. 7:57:40I'm not going to do that one. It's just
  12527. 7:57:42going to take some time. So what I mean
  12528. 7:57:43by this whatever data we have added or
  12529. 7:57:46we done the sitting in our DB you'll be
  12530. 7:57:48able to see that uh easily in the DB you
  12531. 7:57:50can just do that right click and do view
  12532. 7:57:53data. So go here any of the table do
  12533. 7:57:55right click you see it's very slow for
  12534. 7:57:57me I don't know why and then you do view
  12535. 7:58:00and then you'll be able to see the rows
  12536. 7:58:01directly from here. Okay. So now now we
  12537. 7:58:05need to start uh all the concepts one by
  12538. 7:58:08one because you already have very good
  12539. 7:58:11idea that how many tables we have what
  12540. 7:58:13is the relations okay if you want you
  12541. 7:58:15can just go through one by one more time
  12542. 7:58:17it's very very important to understand
  12543. 7:58:19right that for example user is has a
  12544. 7:58:22relations with post has a relations with
  12545. 7:58:24comments these tags are unique but when
  12546. 7:58:27you have post tags this has relations
  12547. 7:58:30with both post and the tag so now let's
  12548. 7:58:32create our second file which will be
  12549. 7:58:34foreign keysql.
  12550. 7:58:39Okay.
  12551. 7:58:41Now you know one thing these particular
  12552. 7:58:43sections that we are going to do right
  12553. 7:58:44this is these you need to learn the
  12554. 7:58:46concept more than doing unnecessary
  12555. 7:58:49example. Why? Because you if you can
  12556. 7:58:51understand the relations joins all these
  12557. 7:58:53things you'll be able to do any kind of
  12558. 7:58:55queries you will be able to write. But
  12559. 7:58:56if you don't understand let's you do 10
  12560. 7:58:58example also and you don't know the
  12561. 7:59:01basics it will be difficult for you to
  12562. 7:59:02write in a real project. So what
  12563. 7:59:04actually foreign keys are. So foreign
  12564. 7:59:06key will be you can consider that as a
  12565. 7:59:08column that is going to points to the
  12566. 7:59:11primary key of another table. Okay. So
  12567. 7:59:13I'm just going to write once again. So
  12568. 7:59:15foreign key will be foreign key is a
  12569. 7:59:18column in sort that points
  12570. 7:59:22to the primary key of another
  12571. 7:59:27table.
  12572. 7:59:29Okay. So this will be the foreign key.
  12573. 7:59:31Now just take one very simple example.
  12574. 7:59:33So you have users do ID which is a
  12575. 7:59:36parent key and then you have post dot
  12576. 7:59:41user id. Now this is the reason I
  12577. 7:59:43mentioned that this is important. So go
  12578. 7:59:45to post and see that post has a user ID.
  12579. 7:59:48This is a foreign key. Why? Because what
  12580. 7:59:50this is actually doing this is pointing
  12581. 7:59:53to the parent key here. Parent uh sorry
  12582. 7:59:56this is pointing to the another table.
  12583. 7:59:58So this is pointing to the another
  12584. 8:00:00table. So another table is users and
  12585. 8:00:01this is pointing to the another table
  12586. 8:00:03primary key. What is the primary key?
  12587. 8:00:05ID. So ID is a primary key. So this is a
  12588. 8:00:07proper example of a parent key and a
  12589. 8:00:10primary key. Okay. And what actually
  12590. 8:00:12this means? This means that every post
  12591. 8:00:14that you're going to create that must
  12592. 8:00:16belong to one existing users. So that
  12593. 8:00:19means every post you will create
  12594. 8:00:23that belongs
  12595. 8:00:26to a that not belongs that must belong
  12596. 8:00:30that must belongs or belongs to a to an
  12597. 8:00:34existing users.
  12598. 8:00:38Okay. So this is what it will be. So now
  12599. 8:00:41uh maybe let's do one thing right. So
  12600. 8:00:43let's see some simple example but this
  12601. 8:00:45is all about the concept. So first let's
  12602. 8:00:47see our parent table. So I'm just going
  12603. 8:00:48to do here select let's do ID let's do
  12604. 8:00:51name and I'm just going to do here from
  12605. 8:00:54users table because we have the users
  12606. 8:00:56table. Okay. And then what we going to
  12607. 8:00:58do right then we are going to check our
  12608. 8:01:00child table. So child table we will
  12609. 8:01:01select ID. Now we have the user ID and
  12610. 8:01:04then I'm just also going to take the
  12611. 8:01:06title of that particular post. So this
  12612. 8:01:08will be from my posts table. Okay. So
  12613. 8:01:11save this and let's run it here. Okay.
  12614. 8:01:14So let's remove this.
  12615. 8:01:16And you can see that what you're
  12616. 8:01:18getting. So you're getting two users.
  12617. 8:01:20But if you [snorts] notice one thing,
  12618. 8:01:21you see this part here. This part right?
  12619. 8:01:25See there is a match. So this actually
  12620. 8:01:28is belongs to this particular. So these
  12621. 8:01:30two post that we have is belongs to user
  12622. 8:01:33ona. And this particular post that we
  12623. 8:01:36have is belongs to user Rahul. So there
  12624. 8:01:39is a match. So this is a foreign key
  12625. 8:01:41which points to the primary key of a
  12626. 8:01:45different table or another table. Very
  12627. 8:01:47important. So foreign keys points to the
  12628. 8:01:49primary key of another table. This is
  12629. 8:01:51the concept that you need to understand
  12630. 8:01:52as simple as that. Okay. So I think
  12631. 8:01:55that's all. I think we don't have
  12632. 8:01:57anything much to discuss here. So let's
  12633. 8:01:59move on to the next one which now we are
  12634. 8:02:02going into much more important concept
  12635. 8:02:04and that is now this is also important
  12636. 8:02:06concept because this is you need to
  12637. 8:02:08understand now see if you go here right
  12638. 8:02:10this particular file that we have
  12639. 8:02:11created and if you can understand this
  12640. 8:02:13part whatever we are doing how do we are
  12641. 8:02:15doing the differences and everything
  12642. 8:02:17this will be like a piece of cake
  12643. 8:02:19because it will be a very very simple
  12644. 8:02:20for you to understand but if you don't
  12645. 8:02:22know how your table structure is see
  12646. 8:02:25that is the reason you see my teaching
  12647. 8:02:27process or style right I Always focus
  12648. 8:02:29the thing that is important. If you
  12649. 8:02:31don't know this structure and someone
  12650. 8:02:33will tell you this, it will be difficult
  12651. 8:02:35for you to understand. I can 100%
  12652. 8:02:37guarantee. Okay. So this is the thing
  12653. 8:02:38that you need to understand. Now let's
  12654. 8:02:40move to the next one which will be 03
  12655. 8:02:43and I'll do one to many
  12656. 8:02:46one to many
  12657. 8:02:49uh relationship
  12658. 8:02:51now relationship.
  12659. 8:02:54Okay. So let's close now. I'll close
  12660. 8:02:56this file. I'll open this one. So what
  12661. 8:02:58actually one to many listen that means
  12662. 8:03:00in simple sentence one parent row can
  12663. 8:03:04have many child rows simple so we'll do
  12664. 8:03:07one parent rows can have uh can have
  12665. 8:03:12multi no many child rows many child rows
  12666. 8:03:15okay what will be our example in our
  12667. 8:03:18example one user can write many post so
  12668. 8:03:21one user so user Rahul can have 10 post
  12669. 8:03:25but one post is belong to one user. So
  12670. 8:03:29what I mean by this? Let's see user one.
  12671. 8:03:32So user one is Anunna and user two is
  12672. 8:03:34Rahul. So Rahul can have 10 post. Let's
  12673. 8:03:37say 1a 2a 3a 4 like that 10. But every
  12674. 8:03:42post this single post always belong to
  12675. 8:03:45one user. So this post also belong to
  12676. 8:03:47Rahul. This post also belongs to Rahul.
  12677. 8:03:50You're getting what I'm talking about.
  12678. 8:03:52So that means one user can write many
  12679. 8:03:56posts but one post will always belongs
  12680. 8:04:01to
  12681. 8:04:04no belong to one belongs to one user.
  12682. 8:04:09Okay. So this is what it will be. So
  12683. 8:04:12here if I want to explain in much more
  12684. 8:04:14detail way so or much more simpler way.
  12685. 8:04:18So users again is your parent table and
  12686. 8:04:22then posts will be your child table.
  12687. 8:04:28Okay. So this is about the concept. Now
  12688. 8:04:31here although we haven't started our
  12689. 8:04:33join yet but this is the this is all
  12690. 8:04:35about the concept. Okay. This is one to
  12691. 8:04:36many. Now one more thing I wanted to
  12692. 8:04:39explain here that is let me just write
  12693. 8:04:42here comment only. So you see this
  12694. 8:04:45relationship right? This is a
  12695. 8:04:46relationship correct? This is a one to
  12696. 8:04:48many relationship. So here if I try to
  12697. 8:04:51take post dot user id. So this actually
  12698. 8:04:56relate to our user do ID. Right? This is
  12699. 8:04:59what actually happening here. Correct?
  12700. 8:05:01So simple words that this user do ID is
  12701. 8:05:04your original user ID. This post do user
  12702. 8:05:07ID stores this user ID inside the post
  12703. 8:05:10table. Okay. This is also another very
  12704. 8:05:12very important concept. Yeah. Okay. So
  12705. 8:05:15here what I'm talking about that this
  12706. 8:05:17users do ID please note it down this is
  12707. 8:05:19actually important is the original is
  12708. 8:05:22the original user ID and what this post
  12709. 8:05:25dot I don't know I'll [snorts] make this
  12710. 8:05:29control minus and then it will uh zoom
  12711. 8:05:31out okay so this post dot user ID is
  12712. 8:05:35nothing but it's the same user ID but
  12713. 8:05:38this post dot user ID is going to stores
  12714. 8:05:40that user ID only that original user ID
  12715. 8:05:43only I'm draw that use original user ID
  12716. 8:05:46inside the posts table. So this is what
  12717. 8:05:50is happening here. Okay. So now although
  12718. 8:05:53we haven't started our uh join yet,
  12719. 8:05:55right? Because join is one of the most
  12720. 8:05:57important concept. But I'm going to show
  12721. 8:05:58you one example that uh that let's say
  12722. 8:06:01someone tell you that okay give me all
  12723. 8:06:03the post. So you have to give me all
  12724. 8:06:05post with their authors.
  12725. 8:06:07But you might be thinking okay I can
  12726. 8:06:09give all the post very easily but in my
  12727. 8:06:12post table I only have the user ID. How
  12728. 8:06:16I'm going to get the username right? So
  12729. 8:06:18this is the query can come here because
  12730. 8:06:20I can give the user ID but I need to
  12731. 8:06:22somehow get the username. So this is
  12732. 8:06:24where the relations comes into picture
  12733. 8:06:26because this user ID is nothing but this
  12734. 8:06:28user ID. Correct? This ID. So if you
  12735. 8:06:30know this user ID, you know this ID and
  12736. 8:06:33from this ID you'll be able to get this
  12737. 8:06:35name. Simple. So now what you can do
  12738. 8:06:37right although again I'm repeating we
  12739. 8:06:39haven't done the uh join yet but this is
  12740. 8:06:42also another join concept only. So here
  12741. 8:06:44I'm just going to do users okay dot
  12742. 8:06:47name. So I need the username as I'm just
  12743. 8:06:49going to do remember we have done the
  12744. 8:06:51allis so use using as and this I'm going
  12745. 8:06:54to take as author name and also give
  12746. 8:06:56comma and here I'm going to take the
  12747. 8:06:58posts dot title as post title but now
  12748. 8:07:05also let's take the posts dot status I
  12749. 8:07:08think we also have the status right but
  12750. 8:07:10now how we are going to get this so now
  12751. 8:07:12we need to work with two tables because
  12752. 8:07:15this user name is not present in this
  12753. 8:07:18post table. Post table only has the user
  12754. 8:07:20ID. But to get the username, I need to
  12755. 8:07:23query this user table. So that means I
  12756. 8:07:25need to get from users table and at the
  12757. 8:07:28same time I need to do a inner join.
  12758. 8:07:30Okay. So this we are going to discuss
  12759. 8:07:32later. For now just write it. So we're
  12760. 8:07:33going to do a inner join with which
  12761. 8:07:36table? Post table. Why we can do that?
  12762. 8:07:38Because there is a relation between this
  12763. 8:07:40user ID and this user ID here. So this
  12764. 8:07:44will be inner join. And then what we
  12765. 8:07:46need to do how we going to do that? So
  12766. 8:07:48we need to check on so what we are
  12767. 8:07:51asking that we need to give me all post
  12768. 8:07:53right. So we are doing that. So all post
  12769. 8:07:58with their authors right? This is what
  12770. 8:08:02we're doing. So that means this user
  12771. 8:08:05this user ID will be same as this ID. So
  12772. 8:08:08when I'm going to do on so I need to
  12773. 8:08:10check this user do id has to be equal
  12774. 8:08:14post dot user id. So you're getting I'm
  12775. 8:08:17repeating this thing that what you're
  12776. 8:08:18doing simply you're checking that okay
  12777. 8:08:19this user ID and both are same that
  12778. 8:08:21means this is a match okay and then what
  12779. 8:08:24you're going to do simply you can also
  12780. 8:08:25do a order by so let's do order by so
  12781. 8:08:27I'm just going to do order by and I'll
  12782. 8:08:29do users dot name posts
  12783. 8:08:35dot title
  12784. 8:08:38okay so now what is happening here I'm
  12785. 8:08:41going to repeat once again so we are
  12786. 8:08:43simply telling that we are connecting
  12787. 8:08:46ing user do ID with post do ID so that
  12788. 8:08:49means if ona that user has written two
  12789. 8:08:52post that user row will appear with two
  12790. 8:08:55post you're getting what I'm talking
  12791. 8:08:57about so this proves our concept of one
  12792. 8:09:00to many relation so that mean one user
  12793. 8:09:02row can be connected with multiple post
  12794. 8:09:05rows simple and now why we are using
  12795. 8:09:07inner join now that we going to discuss
  12796. 8:09:08later because inner join normally it
  12797. 8:09:11will do right it will going to return
  12798. 8:09:12only rows where the relationship exit
  12799. 8:09:15exist on the both sides. Okay. All
  12800. 8:09:18right. So now let's run it here. Right.
  12801. 8:09:20So I'm going to paste this. Sorry, not
  12802. 8:09:22this one.
  12803. 8:09:25So I'll just paste it here / this. And
  12804. 8:09:28then if I do enter and you can see that
  12805. 8:09:30what you're getting. So you're getting
  12806. 8:09:31the post title author name. But now you
  12807. 8:09:34are able to get the author name also for
  12808. 8:09:36that corresponding post. And why this is
  12809. 8:09:37happening? because we are doing a inner
  12810. 8:09:40join and there is a match that if the
  12811. 8:09:42user id is equal to post dot user id
  12812. 8:09:44then only I'm going to get but this is
  12813. 8:09:46where even though your post table don't
  12814. 8:09:48have the username still using this
  12815. 8:09:50relation you are able to get this
  12816. 8:09:52particular name okay so this is although
  12817. 8:09:54this we have we have discussed about
  12818. 8:09:56inner join but this is the concept of
  12819. 8:09:58one to many relationship so with that
  12820. 8:10:00now let's move on to the next one all
  12821. 8:10:02right so although we have to cover the
  12822. 8:10:04many to many relations right but I
  12823. 8:10:05thought that let's do the inner join
  12824. 8:10:07first and left join and then we going to
  12825. 8:10:09do that. Okay, it will be helpful. So
  12826. 8:10:11I'll do inner joinsql.
  12827. 8:10:14Let's close this one. Okay. So what
  12828. 8:10:18actually inner join is right. So
  12829. 8:10:20remember one thing is the key here that
  12830. 8:10:23is interview questions. See this
  12831. 8:10:25particular video right? Whatever we are
  12832. 8:10:27doing everything can be interview
  12833. 8:10:28questions because this is a conceptual
  12834. 8:10:30and practical posgress course right. So
  12835. 8:10:33whenever you're going for any interview
  12836. 8:10:35let's say backend interview or whatever
  12837. 8:10:36database interview you're giving there
  12838. 8:10:38is a possibility that you're going to
  12839. 8:10:39get a lot of questions from this
  12840. 8:10:41particular video whatever we are
  12841. 8:10:42learning. So inner join is going to
  12842. 8:10:45returns this is important only the
  12843. 8:10:48matching rows
  12844. 8:10:51from both tables. So from both tables
  12845. 8:10:55okay this is the important part here. So
  12846. 8:10:57in our case I think we've already
  12847. 8:10:58discussed that part right that in our
  12848. 8:11:00post there is a user ID that points to
  12849. 8:11:03our user table ID this ID again I'm
  12850. 8:11:06going to repeat the same thing this user
  12851. 8:11:08ID points to this user ID simple right
  12852. 8:11:12so what postgress is going to do so
  12853. 8:11:14postgress now will be able to match each
  12854. 8:11:17post with the user that user actually
  12855. 8:11:20created the post why because there is
  12856. 8:11:22something some relations between these
  12857. 8:11:24two if there is no match it's is not
  12858. 8:11:27going to return anything simple okay so
  12859. 8:11:29now let's create a simple example and
  12860. 8:11:32you're going to understand everything
  12861. 8:11:33that we are doing here okay and then I'm
  12862. 8:11:35going to explain that what actually this
  12863. 8:11:37example is doing and why I've actually
  12864. 8:11:39decided to take this as example so I'm
  12865. 8:11:41just going to do select
  12866. 8:11:43I'll do users do name as I'm going to
  12867. 8:11:46take this one as author name
  12868. 8:11:49comma you have to give comma here and
  12869. 8:11:51I'll do posttitle as post title
  12870. 8:11:57Then I'm also going to take the posts
  12871. 8:11:59dot status
  12872. 8:12:02and also let's take posts dot views
  12873. 8:12:04because we have the views also. So we
  12874. 8:12:06have the views also. Okay. So these four
  12875. 8:12:09things now from where because you have
  12876. 8:12:10to give the destination always. So from
  12877. 8:12:12my post table and now I need to see I
  12878. 8:12:16need the username right because I
  12879. 8:12:17already mentioned this thing multiple
  12880. 8:12:19times that your post table don't have
  12881. 8:12:21the username. So that means you have to
  12882. 8:12:24get the username from your user table.
  12883. 8:12:26So this is where you have to do inner
  12884. 8:12:27join as simple. So you're going to do
  12885. 8:12:29inner join with the users table and then
  12886. 8:12:31you have to give a matching condition.
  12887. 8:12:33So that is the reason I mentioned that
  12888. 8:12:34it is going to return only the matching
  12889. 8:12:36rows. So there has to be something there
  12890. 8:12:38is a match. So what is the match? Post
  12891. 8:12:40dot user ID is a match with user do ID.
  12892. 8:12:43Simple. So here you're going to give
  12893. 8:12:45that on. So we do on posts
  12894. 8:12:49dot user id is a match with users do id.
  12895. 8:12:53So it's a matching condition. So now
  12896. 8:12:55this is going to do the inner join
  12897. 8:12:56successfully. So postgress will be able
  12898. 8:12:58to understand. And now what you can do
  12899. 8:13:00right I'm just going to do a where. So
  12900. 8:13:01let's do posts
  12901. 8:13:04dot status because we have the post dot
  12902. 8:13:07status right see and then while adding
  12903. 8:13:09the post I've added this one right. So
  12904. 8:13:11go here. So we have this published or
  12905. 8:13:14dropped. So I'm just going to get that
  12906. 8:13:16where the post dot status is equal to
  12907. 8:13:19published
  12908. 8:13:22and let's do order by also. So I'm just
  12909. 8:13:24going to do order by and I'll do post
  12910. 8:13:27dot views and I'm just going to do this
  12911. 8:13:30and maybe let's do descending order.
  12912. 8:13:32Okay. So this will be a perfect example
  12913. 8:13:34of inner join. Now how we are going to
  12914. 8:13:37read this if you're a beginner. So you
  12915. 8:13:39start with the beginning right. So first
  12916. 8:13:40from the post table what we are doing we
  12917. 8:13:43are doing inner joining our users table.
  12918. 8:13:46So simply in a simple way we are
  12919. 8:13:48connecting each post to that particular
  12920. 8:13:51user right and this is the matching rule
  12921. 8:13:53I already mentioned this is the matching
  12922. 8:13:55rule here sorry this is the matching
  12923. 8:13:57rule so this is the matching rule
  12924. 8:14:01without this it won't be possible and
  12925. 8:14:03then we giving some condition that where
  12926. 8:14:05it is published and then we're going to
  12927. 8:14:06do order by and now if you run this see
  12928. 8:14:09what will happen. So we're going to do
  12929. 8:14:10clear
  12930. 8:14:12and see what we're going to get.
  12931. 8:14:16All right. So we're getting zero result.
  12932. 8:14:18Published spelling is wrong. I know
  12933. 8:14:20that. Okay. So this is wrong. Save this
  12934. 8:14:23and run again.
  12935. 8:14:28Okay. So now you're getting author name
  12936. 8:14:30also you're getting post title, you're
  12937. 8:14:32getting status published and also views.
  12938. 8:14:34This is also in descending order. So
  12939. 8:14:35you're doing a lot of things but the
  12940. 8:14:37important part what we are doing here we
  12941. 8:14:38are doing a inner join. So the interview
  12942. 8:14:41questions that you might get that what
  12943. 8:14:42actually inner join is what actually it
  12944. 8:14:44will return and how actually it will
  12945. 8:14:46work. So the most important part you
  12946. 8:14:47have to mention that it's written only
  12947. 8:14:49the matching rows from both tables but
  12948. 8:14:52there has to be some matching condition
  12949. 8:14:53because if there is no matching
  12950. 8:14:55condition how posgress is going to
  12951. 8:14:57understand that okay this particular
  12952. 8:14:58post is written by this user you're
  12953. 8:15:00getting. So this is where it is very
  12954. 8:15:02very important. So now I think this is
  12955. 8:15:05done. Let's move on to the next one
  12956. 8:15:07which will be our left join.
  12957. 8:15:13Okay. So close this. Now what actually
  12958. 8:15:15left join will do. Now left join is
  12959. 8:15:19going to keeps all rows from the left
  12960. 8:15:22table. Now this might be a little tricky
  12961. 8:15:24to understand. Okay. So let's write all
  12962. 8:15:26the things first. So I'm just going to
  12963. 8:15:27do left join keeps all rows from the
  12964. 8:15:33left table.
  12965. 8:15:36Okay. Now say that consider you have a
  12966. 8:15:39left table and you have a right table.
  12967. 8:15:41Now what this first condition is telling
  12968. 8:15:43that this statement that it is going to
  12969. 8:15:45keep all your rows from the left side
  12970. 8:15:46table and if the right side table has
  12971. 8:15:49the matching data then postgress is
  12972. 8:15:52going to include that data. If not what
  12973. 8:15:55possess is going to do? It's going to
  12974. 8:15:56return null for the right side column.
  12975. 8:15:59These things are very very important. So
  12976. 8:16:00I'm just going to do so consider you
  12977. 8:16:02have a two table left table right table.
  12978. 8:16:04If the right table has matching data
  12979. 8:16:10so in that case posgress is going to
  12980. 8:16:13include that. Supposedly it's going to
  12981. 8:16:14include that
  12982. 8:16:17includes that if it's don't have any
  12983. 8:16:19matching data.
  12984. 8:16:26So in that case it's going to return
  12985. 8:16:28null. It returns null.
  12986. 8:16:32All right. So now what example we can
  12987. 8:16:34take from our setup. Now here you can
  12988. 8:16:37see that consider we have our post table
  12989. 8:16:39over here.
  12990. 8:16:43So this is our post table. This is our
  12991. 8:16:46left table. Okay.
  12992. 8:16:49So I'm going to take this one as a left
  12993. 8:16:51table.
  12994. 8:16:53And let's take our comments table and
  12995. 8:16:56this I'm going to take as a right table.
  12996. 8:16:58So this is my right table. So this post
  12997. 8:17:01uh this comments. Okay. And why we are
  12998. 8:17:04taking this? Because
  12999. 8:17:06every post can have comments or can't
  13000. 8:17:09have. So it's a possibility. So that
  13001. 8:17:12mean simply telling that because not
  13002. 8:17:15every post is going to have comments.
  13003. 8:17:16You're getting what I'm talking about.
  13004. 8:17:18So not every post is going to have
  13005. 8:17:21comments. Let's say some post some post
  13006. 8:17:27will have 100 comments
  13007. 8:17:33and some
  13008. 8:17:35will have zero.
  13009. 8:17:38You're getting. So now we'll be able to
  13010. 8:17:40see that how left chain is going to show
  13011. 8:17:42the difference very very clearly. So now
  13012. 8:17:45what we going to do right let's write
  13013. 8:17:47one. So what we we will do we'll write a
  13014. 8:17:49query which is going to show all our
  13015. 8:17:51post including the post that do not have
  13016. 8:17:55any comments or don't have any comments.
  13017. 8:17:57Okay. So see how we are going to do
  13018. 8:17:59that. So I'm going to do select and here
  13019. 8:18:01I'm just going to render the post dot
  13020. 8:18:03title and I'm going to take allis and
  13021. 8:18:05I'm just going to do post title comma
  13022. 8:18:09and also I'm going to take this com uh
  13023. 8:18:11comments is going to have the body right
  13024. 8:18:13so I'm just going to do comments dot
  13025. 8:18:16body comments is spelling is wrong dot
  13026. 8:18:20body as comment_body
  13027. 8:18:24now we are going to do a from so from my
  13028. 8:18:27post table this is my left table and now
  13029. 8:18:30we don't have to do inner join you have
  13030. 8:18:31to do a left join
  13031. 8:18:34left join will be right side table so
  13032. 8:18:36which is your commands table okay and
  13033. 8:18:39then I'm just simply going to give a
  13034. 8:18:40check condition matching condition why
  13035. 8:18:42because there is a match right because
  13036. 8:18:44this post do ID is similar to this post
  13037. 8:18:47ID because every comment is going to
  13038. 8:18:50have that post ID associated right so
  13039. 8:18:52that mean there is a match so here I can
  13040. 8:18:54simply check if this post do ID is equal
  13041. 8:18:57to comment commands dot
  13042. 8:19:00post ID. So here I'm just going to do
  13043. 8:19:02comments dot post ID. Okay. And then I'm
  13044. 8:19:05simply going to do a order by and I'll
  13045. 8:19:08do posts
  13046. 8:19:10dot title. So I'm just going to do order
  13047. 8:19:12by title. So how we are doing this? So
  13048. 8:19:15simply here left join comments. What
  13049. 8:19:18this line is simply doing that we try to
  13050. 8:19:20connect each post with its comments
  13051. 8:19:23simply. And this is the matching rule.
  13052. 8:19:26If there is a post that has no comments,
  13053. 8:19:29what it's going to do? The comment body
  13054. 8:19:31will be what? This comment body null.
  13055. 8:19:34This is what I mentioned, right? If
  13056. 8:19:35there is a no matching, so it's return
  13057. 8:19:36null. You're getting what I'm talking
  13058. 8:19:39about. And left join is going to keep
  13059. 8:19:41all our post. Inner join, what it's
  13060. 8:19:44going to do, right? It's going to hide
  13061. 8:19:45the post that have no comments. There is
  13062. 8:19:47a huge difference, big difference. Left
  13063. 8:19:50join is going to keep all our post, but
  13064. 8:19:52it's going to return the comment body as
  13065. 8:19:54null. Inner journey is going to hide the
  13066. 8:19:56post that don't have any commands. So
  13067. 8:19:58here you can simply see this right? You
  13068. 8:20:00can run it and see what is happening
  13069. 8:20:02here.
  13070. 8:20:04See so now we have back end API with
  13071. 8:20:07posgress. Please add more example
  13072. 8:20:09indexes for beginner. We don't have
  13073. 8:20:10anything posgress explained. This is the
  13074. 8:20:12commenter. Now if you go here where we
  13075. 8:20:14have see added the data go below.
  13076. 8:20:19So you can see that for posgress join
  13077. 8:20:22explain we have very clear explanation
  13078. 8:20:24this is the command please add more
  13079. 8:20:26example uh sorry no backend uh APIs with
  13080. 8:20:30posgress for this one we have please add
  13081. 8:20:31more example but for indexes for
  13082. 8:20:34beginner this is another one for this
  13083. 8:20:36particular post we don't have any
  13084. 8:20:38comment so that means right side there
  13085. 8:20:40is no match so what's is going to return
  13086. 8:20:42it's going to return null so that's why
  13087. 8:20:44you're not getting anything see simple
  13088. 8:20:46right or not simple but it's clear if
  13089. 8:20:48you're a beginner It will be a little
  13090. 8:20:50tricky but see with this explanation I
  13091. 8:20:52hope you are able to understand what
  13092. 8:20:53we're talking about. This is the
  13093. 8:20:54important part. The left journey is
  13094. 8:20:56going to always keep all the data from
  13095. 8:20:58the left table. Right uh table has
  13096. 8:21:01matching data then only it will render
  13097. 8:21:02or else it's going to return null. So
  13098. 8:21:04this is the concept of left join. Okay.
  13099. 8:21:07So we have completed inner join and left
  13100. 8:21:09join. Let's create the next file which
  13101. 8:21:11will be many to many relationship. So do
  13102. 8:21:14many to many
  13103. 8:21:19relationships.
  13104. 8:21:23So let's do this one in the next part.
  13105. 8:21:25All right. So now let's move on to the
  13106. 8:21:26next one which will be now man to many
  13107. 8:21:28relationship. Right. So from our example
  13108. 8:21:30what will be the best uh way to explain
  13109. 8:21:33this one. So I think this part we have
  13110. 8:21:35already discussed and that is that in
  13111. 8:21:36our case right where we have added the
  13112. 8:21:38data that the many to many means one
  13113. 8:21:42post can have multiple tags. You
  13114. 8:21:43remember we have the tags uh this one
  13115. 8:21:46right I already mentioned that let's say
  13116. 8:21:47you have one particular tag this can
  13117. 8:21:49have uh sorry one particular post and
  13118. 8:21:51that can have multiple tags. Let's say
  13119. 8:21:53HTML is a tag, CSS is a tag, ReactJS is
  13120. 8:21:56a tag and one tag can belong to many
  13121. 8:21:59post. So that means this reacts tag can
  13122. 8:22:02be uh associated with multiple post. So
  13123. 8:22:06this will be a proper many to many uh
  13124. 8:22:09relationship example. So that means one
  13125. 8:22:11post
  13126. 8:22:13can have multiple uh tags and one tag
  13127. 8:22:19can be associated
  13128. 8:22:22with multiple posts.
  13129. 8:22:26I hope this is clear right. So now in
  13130. 8:22:29this example in our example what
  13131. 8:22:32actually post is doing so it's store
  13132. 8:22:33post data tags is going to store uh tag
  13133. 8:22:37uh tags data and then we have one more
  13134. 8:22:40post tags. Now this is going to connect
  13135. 8:22:43both post and tags. So if you see so if
  13136. 8:22:46you go here right so go to the post so
  13137. 8:22:48do post do ID this post do ID is similar
  13138. 8:22:53to this post id that we have in this
  13139. 8:22:55post tags table. Similarly, this tax do
  13140. 8:22:59ID is similar to this tags id that we
  13141. 8:23:01have in this post tags dot tags id in
  13142. 8:23:04this particular table. So that means
  13143. 8:23:06here in our case we have a post do ID
  13144. 8:23:08which is similar. So I'm just writing
  13145. 8:23:10triple equals which is equal to this
  13146. 8:23:12post tags
  13147. 8:23:15dot this post id and similarly you have
  13148. 8:23:19the tags do ID right? So we do tags do
  13149. 8:23:21id is again is equal to this post tag
  13150. 8:23:25dot tag id.
  13151. 8:23:28All right. So now let's see one simple
  13152. 8:23:31example. So example will be now your
  13153. 8:23:32task is that you have to show every post
  13154. 8:23:36with its tag. So that means show me
  13155. 8:23:41every post
  13156. 8:23:44with its tag. So how we can do that? Now
  13157. 8:23:48let's see. So we'll do select. Now first
  13158. 8:23:50of all what we need to get so I need to
  13159. 8:23:52get the see when I wanted to show the
  13160. 8:23:54post I don't have the post title here
  13161. 8:23:57right so that means if I want to get the
  13162. 8:23:58post title I need to query this
  13163. 8:24:00particular table so that means I need to
  13164. 8:24:02get this post dot title correct so first
  13165. 8:24:05I need to do what I need to get posts
  13166. 8:24:08dot title as let's do this one as post
  13167. 8:24:13title and then also I need to get the
  13168. 8:24:16tags name so what I mean by tags name
  13169. 8:24:18these tags name because you don't have
  13170. 8:24:20the tax name here. So I need to get the
  13171. 8:24:22tags doame. So I'm just going to do tags
  13172. 8:24:25dotname as let's do this one as tag
  13173. 8:24:27name. Okay. So this will be my tag name
  13174. 8:24:30and this I'm going to get from my post
  13175. 8:24:32table. And here what you have to do. So
  13176. 8:24:35you have to do a inner join because you
  13177. 8:24:37need to check in both the sides. Right?
  13178. 8:24:39So I'm just going to do inner join. And
  13179. 8:24:41also if you go to inner join what I
  13180. 8:24:43mentioned inner join is going to return
  13181. 8:24:45only the matching rows from both tables.
  13182. 8:24:48Okay. There is a difference between
  13183. 8:24:49inner join and the left join. So this
  13184. 8:24:51I'm just going to do inner join but
  13185. 8:24:53inner join you have to do with what you
  13186. 8:24:56have to do with this post tag this
  13187. 8:24:58particular uh table. Okay. So I'm going
  13188. 8:25:01to show you how we are going to do that.
  13189. 8:25:02So first we need to do what? So I need
  13190. 8:25:03to get post tag. So I'm doing what? So
  13191. 8:25:06I'm doing in from this post table with
  13192. 8:25:09this post tag. Why? Because there is a
  13193. 8:25:12relation with this post ID. This post ID
  13194. 8:25:15is equal to this post do ID. So that
  13195. 8:25:17means what will be my matching criteria
  13196. 8:25:18here? So I'm going to give here on. So
  13197. 8:25:21I'll do post do id is equal to post tag
  13198. 8:25:26post tag dot post ID. You're getting
  13199. 8:25:29right. So this will be my matching
  13200. 8:25:30criteria. And this is for the first one.
  13201. 8:25:33Similarly I also need to get the tags
  13202. 8:25:36name. So that means I there should be
  13203. 8:25:38some matching criteria between this
  13204. 8:25:40table and that this table and that is
  13205. 8:25:41what tags do id is equal to post tags
  13206. 8:25:44dot tag ID. So these two are matching.
  13207. 8:25:47So so that if there is a matching so
  13208. 8:25:48I'll be able to get the name. So that
  13209. 8:25:50means again what you need to do again
  13210. 8:25:52you need to do another inner join here.
  13211. 8:25:54Now this time this inner join will be
  13212. 8:25:56not with the post tag. This will be from
  13213. 8:25:59your tags table. So you have this tags
  13214. 8:26:02table here. Correct? And now what you're
  13215. 8:26:04going to do? So you have to do the
  13216. 8:26:05matching criteria. So what will be
  13217. 8:26:07matching criteria? So matching criteria
  13218. 8:26:09will be your post tag. So this will be
  13219. 8:26:12post tag dot. And now you have what? tag
  13220. 8:26:15id and this will be equal to what? So
  13221. 8:26:17this will be equal to your tags do id.
  13222. 8:26:20You're getting what you're doing. So I'm
  13223. 8:26:21explaining line by line. So this will be
  13224. 8:26:23a tags do ID. Okay. And then we are
  13225. 8:26:25going to do what we are going to do
  13226. 8:26:27order by. Let's do order by and I'm just
  13227. 8:26:29going to do here posts
  13228. 8:26:32dot title,
  13229. 8:26:35tags dot name. So this is what we need
  13230. 8:26:38to do. Now I think I don't have to
  13231. 8:26:41explain here now because I've already
  13232. 8:26:42explained line by line. Still I'm just
  13233. 8:26:44going to show you sorry explain one more
  13234. 8:26:45time that what actually this is doing.
  13235. 8:26:47So select simply we are going to get
  13236. 8:26:49this post title and the tag name. Right.
  13237. 8:26:51So here first of all what we are doing
  13238. 8:26:53so from the post table we are doing
  13239. 8:26:55inner join with the post tag table
  13240. 8:26:56because there is a matching. So in the
  13241. 8:26:58matching we need to give the matching
  13242. 8:26:59criteria. So post do ID if there is a
  13243. 8:27:02match with the post tag dot post ID. So
  13244. 8:27:04that means there is a matching. So it's
  13245. 8:27:05going to give me that particular data.
  13246. 8:27:07Then we going to do inergen with the
  13247. 8:27:09tags because there is also a matching
  13248. 8:27:11criteria with the tags do ID with the
  13249. 8:27:13post tags dot tag ID. So this is going
  13250. 8:27:15to give me the tags name. This is what
  13251. 8:27:17we're simply doing here. Now let's run
  13252. 8:27:19this and let's see what is happening
  13253. 8:27:20here. Okay. So I'm going to just clear
  13254. 8:27:22this.
  13255. 8:27:24Let's copy the name [clears throat]
  13256. 8:27:33syntax error text name at okay let me
  13257. 8:27:35see what we doing here.
  13258. 8:27:38Oh say I always do this mistake going to
  13259. 8:27:40give comma here. So now let's run one
  13260. 8:27:43second
  13261. 8:27:45and you can see that what you're getting
  13262. 8:27:46you're getting the post title with the
  13263. 8:27:49tag name. Okay. And now this is very
  13264. 8:27:51very important concept. Now this is not
  13265. 8:27:53a simple posgress concept. Okay, this is
  13266. 8:27:56like I'm going to I'm going to say this
  13267. 8:27:58will be little bit intermediate side. So
  13268. 8:28:00you need to know all of these things
  13269. 8:28:01very very clearly so that it's like you
  13270. 8:28:03don't have to go to any other resources
  13271. 8:28:05to see how these things are working. So
  13272. 8:28:07what I mean by this this many to many
  13273. 8:28:09one to many inner join left join you
  13274. 8:28:11have to master all of this if you want
  13275. 8:28:13to become good like if you want to
  13276. 8:28:16become uh good developer I will say in
  13277. 8:28:18writing all these queries okay so this
  13278. 8:28:20is a very very important concept that we
  13279. 8:28:22have learned here okay so with this I
  13280. 8:28:25think that's all for this particular uh
  13281. 8:28:28uh concept now let's move on to the next
  13282. 8:28:30one next I think what we can do right
  13283. 8:28:32let's understand some of the table allis
  13284. 8:28:35now this is also another important
  13285. 8:28:36concept concept. So what I'm going to do
  13286. 8:28:38right, I'm just going to do 07. Let's do
  13287. 8:28:40this one as table allis now
  13288. 8:28:45spelling is wrong I think.
  13289. 8:28:49Okay. So now let's close this. So what
  13290. 8:28:52actually table allis? Allis I think we
  13291. 8:28:54have already discussed that allis is
  13292. 8:28:56going to make your join query shorter
  13293. 8:28:58and easier to read. Right? So allis is
  13294. 8:29:02going to
  13295. 8:29:04make your queries
  13296. 8:29:08shorter and easier to read. So this is
  13297. 8:29:12the first thing. So simply I'm telling
  13298. 8:29:14that okay if I want the title from the
  13299. 8:29:17post table. So I'm just going to do
  13300. 8:29:19what? Post.title. This is what we are
  13301. 8:29:21doing right. What we can do right? No I
  13302. 8:29:23don't want to write this. Instead I want
  13303. 8:29:25to write I want to write like this. So
  13304. 8:29:29instead of this I want to do P.Title.
  13305. 8:29:31Now this P I need to tell Postgress.
  13306. 8:29:33Okay this P means I'm actually talking
  13307. 8:29:36about the post table or let's say want
  13308. 8:29:38to get the uh what you can do maybe
  13309. 8:29:40users do.ame instead you want to do what
  13310. 8:29:44you want to do you do. Right. So these
  13311. 8:29:48will be your allis.
  13312. 8:29:50So now let's see how we are going to do
  13313. 8:29:52that. Now this will be a very simple
  13314. 8:29:53one. So what we can do right maybe let's
  13315. 8:29:55say
  13316. 8:29:57I wanted to select something so I wanted
  13317. 8:29:59to get my
  13318. 8:30:01let's go here. So I wanted to get the
  13319. 8:30:03post title let's say and also post
  13320. 8:30:05status and maybe let's do username also
  13321. 8:30:08I'm going to get and also let's go to
  13322. 8:30:11the comment and also let's say I want to
  13323. 8:30:12get the comment body. So all these
  13324. 8:30:15things I wanted to get. So first of all
  13325. 8:30:16what I will do right I'm just going to
  13326. 8:30:18ptitle. Now notice one thing we are not
  13327. 8:30:20doing posts do. And here I'm just going
  13328. 8:30:23to take a license and I'm just going to
  13329. 8:30:25do this one as post title. We have to
  13330. 8:30:27give comma here. Let's I'm just going to
  13331. 8:30:29give P dot status and also let's take
  13332. 8:30:31the post views also. So I think we have
  13333. 8:30:34the views right? So I'm just going to do
  13334. 8:30:35P dot views. So this is what I'm talking
  13335. 8:30:37about. So I'll do P dot views.
  13336. 8:30:41Also let's take the username. So I'm
  13337. 8:30:43just going to do U dot name as let's do
  13338. 8:30:46this one as author name. And also I'm
  13339. 8:30:48going to get the comment body. So I'm
  13340. 8:30:50just going to do C dot body as comment
  13341. 8:30:55body. Now this is where you have to do
  13342. 8:30:59the analysis because till this part your
  13343. 8:31:02postgrade doesn't know what actually
  13344. 8:31:04this P.title means. Very very important
  13345. 8:31:07because there is no table that named
  13346. 8:31:10exactly as P. So that means when I'm
  13347. 8:31:12going to do here from ports. So now I'm
  13348. 8:31:15simply telling that okay this is the
  13349. 8:31:16table I'm talking about. I need to give
  13350. 8:31:18a allis here and that allis will be that
  13351. 8:31:20allis that you are using here. So you're
  13352. 8:31:22simply telling that okay I'm actually
  13353. 8:31:24quiring from the post table but I want
  13354. 8:31:26to rename this post from posts to P so
  13355. 8:31:30that I don't have to write the post word
  13356. 8:31:33multiple times as simple as that. And
  13357. 8:31:34now let's say I wanted to do a inner
  13358. 8:31:36join here so I need to do inner join uh
  13359. 8:31:39maybe let's do the user because we also
  13360. 8:31:41need to get the inner join of that uh
  13361. 8:31:43sorry we also need to get the username
  13362. 8:31:45right? So we need to do inner journal on
  13363. 8:31:46the users table but you have to give
  13364. 8:31:48another allis as U because you are not
  13365. 8:31:52using users dotname here you are using u
  13366. 8:31:55dotname okay so this is one more now
  13367. 8:31:57what we need to do I think this part
  13368. 8:31:59already discussed so there will be a
  13369. 8:32:01relations why because your user do ID
  13370. 8:32:04will be similar to your post dot user id
  13371. 8:32:07so that means now what you can do right
  13372. 8:32:09you don't have to do post do user ID
  13373. 8:32:11instead you can take only these allises
  13374. 8:32:13here so you'll do p dot user user id
  13375. 8:32:15you're getting. So you'll do P dot user
  13376. 8:32:18id and this will be equal to your user
  13377. 8:32:20do ID. So user do ID this id. So that
  13378. 8:32:24means user will now become u. So you you
  13379. 8:32:26have to do u do ID right and then what I
  13380. 8:32:30need to do right I also wanted to get
  13381. 8:32:32the comments. So I'm just going to do a
  13382. 8:32:33left join and I'll do comments because
  13383. 8:32:36comments is the commands table that I'm
  13384. 8:32:38actually quering this one. But now this
  13385. 8:32:40time I wanted to do allis of C. So
  13386. 8:32:42comments will be as C and then I'm just
  13387. 8:32:45going to do here on. So there will be
  13388. 8:32:48another match. Why? Because comments do
  13389. 8:32:51id right sorry comments.post id will be
  13390. 8:32:54equal to your post do ID. So that means
  13391. 8:32:56here the matching condition will be your
  13392. 8:32:58p dot id because you are taking the post
  13393. 8:33:00as p which will be equal to c which is
  13394. 8:33:04here c for comments and these comments
  13395. 8:33:06will have the post ID. So this will be
  13396. 8:33:10c.post ID you're getting? So this will
  13397. 8:33:12be a matching condition and let's do a
  13398. 8:33:14order by
  13399. 8:33:16maybe let's do which one we can do order
  13400. 8:33:19by
  13401. 8:33:20let's do p dot views as order by okay so
  13402. 8:33:23I'm just going to do disk so this is one
  13403. 8:33:26more query now remember it's this
  13404. 8:33:28particular concept is not about the
  13405. 8:33:30query because these all the things we've
  13406. 8:33:31already discussed this is about how
  13407. 8:33:33you're writing this allis so here post
  13408. 8:33:35we are talking as a p or writing as a p
  13409. 8:33:38user table we are writing as a u
  13410. 8:33:40commands table we are writing as a See?
  13411. 8:33:42Okay. So now if I just let's say clear
  13412. 8:33:44this and let's run it once again
  13413. 8:33:53and you can see that you're going to get
  13414. 8:33:55the result. So you're getting post,
  13415. 8:33:56title, status, views, author name and
  13416. 8:33:59comment body and this is also in the
  13417. 8:34:01descending order. But again I already
  13418. 8:34:03mentioned this is about how we are going
  13419. 8:34:04to write the allis. Okay. So I think
  13420. 8:34:07that's all for this particular concept.
  13421. 8:34:09Now in the next part what we can do what
  13422. 8:34:11else okay we had to do aggregate also
  13423. 8:34:13right I already mentioned so let's
  13424. 8:34:15create one more file here and I'm going
  13425. 8:34:16to do aggregate
  13426. 8:34:19functions
  13427. 8:34:21dossql
  13428. 8:34:24so now what we are talking about the
  13429. 8:34:26aggregate function so simply in a simple
  13430. 8:34:30definition aggregate functions will be
  13431. 8:34:32it always calculates right so it's
  13432. 8:34:35always going to calculate or calculate
  13433. 8:34:38one [snorts] result from many rows. This
  13434. 8:34:42will be your aggregate function. Now
  13435. 8:34:45there can be so many aggregate
  13436. 8:34:46functions. So I'm just going to give
  13437. 8:34:48some of the examples. So I'll do we have
  13438. 8:34:49the count. What count is going to do?
  13439. 8:34:52Count is going to give you the number of
  13440. 8:34:54rows if I'm not wrong. Then you have
  13441. 8:34:57something called sum. Sum I think by the
  13442. 8:34:59name you can understand. This will be
  13443. 8:35:00total value. Then also you have average.
  13444. 8:35:04So look average and this will be give
  13445. 8:35:07the average value.
  13446. 8:35:12Okay. Then what else? We also have the
  13447. 8:35:14mean. So this mean will be the smallest
  13448. 8:35:17value. And then also we have the max
  13449. 8:35:21which will be maximum value. Okay. Or
  13450. 8:35:24let's do largest or max value. Okay.
  13451. 8:35:31All right. Now where where actually you
  13452. 8:35:34can use all of this very very very
  13453. 8:35:36helpful and that will be I'm going to
  13454. 8:35:38give you some real life example for
  13455. 8:35:40example let's say you're creating some
  13456. 8:35:41admin dashboard where you have to
  13457. 8:35:42calculate your sales let's say you're
  13458. 8:35:44creating some reports you're creating
  13459. 8:35:46some analytics dashboard there also
  13460. 8:35:49mostly it will be around your admin
  13461. 8:35:51panels and those related work and you'll
  13462. 8:35:53see that you're going to use this one
  13463. 8:35:54lot of time now let's say I wanted to
  13464. 8:35:57write a very very simple example that
  13465. 8:35:59what I wanted to do right I wanted to
  13466. 8:36:01count all the rows from my post table.
  13467. 8:36:04Okay. So let's do one by one then I'm
  13468. 8:36:06going to explain. So I'm just going to
  13469. 8:36:07do select. Now let's say you want to
  13470. 8:36:08read all the uh rows from a certain
  13471. 8:36:12table. Okay. So how you're going to do
  13472. 8:36:14that? So for now we are only going to
  13473. 8:36:15use the post table only. To to read all
  13474. 8:36:18the rows from a certain table you have
  13475. 8:36:20to use a count here. And this count
  13476. 8:36:22you're simply going to tell okay this
  13477. 8:36:23here I'm just going to count everything
  13478. 8:36:25and this I'm just going to also give
  13479. 8:36:27allis. So this will be my total
  13480. 8:36:29underscore
  13481. 8:36:32posts. Okay. And here what you're going
  13482. 8:36:35to do. So I'm just going to give a from
  13483. 8:36:38post table. So I'm simply telling we'll
  13484. 8:36:40do one by one. Okay. So this is the
  13485. 8:36:42first one. So it's simply telling that
  13486. 8:36:43give me or count all the rows from this
  13487. 8:36:46post table because this is the table
  13488. 8:36:47that we are using. Now let's run this.
  13489. 8:36:59Okay. So, you're getting three.
  13490. 8:37:00Fantastic. Now, now let's say what I
  13491. 8:37:03need to do, right? I [snorts] wanted to
  13492. 8:37:05now count what I wanted to give a
  13493. 8:37:06filter. So, what filter I wanted to
  13494. 8:37:08give? I wanted to give a filter where
  13495. 8:37:10the status is published. If I'm not
  13496. 8:37:12wrong, we have a published status. See,
  13497. 8:37:14published or dropped. So, I only wanted
  13498. 8:37:17to count all the rows where the status
  13499. 8:37:20is published. So that means now you
  13500. 8:37:22can't directly use count you have to use
  13501. 8:37:24a filter. So how you can do that? So you
  13502. 8:37:25have to do count
  13503. 8:37:28and then you have to use a filter here.
  13504. 8:37:30Okay. And this filter will be now I
  13505. 8:37:33think we have already used this one
  13506. 8:37:34multiple times where keyword status will
  13507. 8:37:38be equal to what whatever status you
  13508. 8:37:40wanted to check. So now I wanted to
  13509. 8:37:41check about the published and then let's
  13510. 8:37:43give a license and I'm just going to do
  13511. 8:37:45this one as published
  13512. 8:37:47posts. So what this is going to give
  13513. 8:37:50this is going to count all the rows but
  13514. 8:37:52it's going to apply a filter now. Okay.
  13515. 8:37:54Now let's run it and let's see what
  13516. 8:37:56you're getting. So if I go here but
  13517. 8:37:59telling syntax in the count
  13518. 8:38:02again I forgot to give comma. Okay. So
  13519. 8:38:06let's do
  13520. 8:38:09so you can see that we have three post
  13521. 8:38:11publish post two. Change this one to
  13522. 8:38:13dropped and you'll see that you are
  13523. 8:38:14going to get one because we have only
  13524. 8:38:16one uh dropped post right. So if I just
  13525. 8:38:19go here, run it again, you see that we
  13526. 8:38:22are getting only one. Fantastic.
  13527. 8:38:24Now let's undo here.
  13528. 8:38:26Next, what we want to do, we'll give
  13529. 8:38:29comma here. So now let's say sum. I want
  13530. 8:38:32to do sum or or what I will do, right?
  13531. 8:38:35Maybe let's do this one. Go to post.
  13532. 8:38:38We have these views, right? So I wanted
  13533. 8:38:40to do a sum of all these views of all
  13534. 8:38:43rows. You're getting what I'm talking
  13535. 8:38:45about. So when you're going to do that
  13536. 8:38:48this very very simple so views is I'm
  13537. 8:38:50talking about these views I wanted to
  13538. 8:38:51sum so you have to use sum and then
  13539. 8:38:53you're going to tell that okay calculate
  13540. 8:38:55all the sum of what so you have to give
  13541. 8:38:57here which column so this will be my
  13542. 8:38:59views column simple and then you're
  13543. 8:39:01going to give as and let's giveis so
  13544. 8:39:03this will be my total views okay so that
  13545. 8:39:06mean now this is going to do what this
  13546. 8:39:08is going to add all your view counts
  13547. 8:39:11together from this table okay from this
  13548. 8:39:14particular table so now if I just run it
  13549. 8:39:16and you'll see that you're going to get
  13550. 8:39:18320 and you can also uh like reconfirm.
  13551. 8:39:21So you have what
  13552. 8:39:23140 140 + 180 so 320.
  13553. 8:39:28What else? Let's do the average views
  13554. 8:39:31also. So for average it will be simple
  13555. 8:39:32just use average pass the views and
  13556. 8:39:35you're going to do here average
  13557. 8:39:38views also to give comma here. Save this
  13558. 8:39:42and now run again.
  13559. 8:39:45So this will be your average views. So I
  13560. 8:39:48think this is done. What else? I think
  13561. 8:39:50min minimum and maximum let's do that
  13562. 8:39:51also. So minimum again it will be
  13563. 8:39:53simple. So do minimum views as lowest
  13564. 8:39:57views and also you're going to do a
  13565. 8:40:00maximum. So do max of views as highest
  13566. 8:40:07views.
  13567. 8:40:09So this is going to give the minimum and
  13568. 8:40:11the maximum. So here, let's clear this
  13569. 8:40:13and run it here again. So we can see
  13570. 8:40:16that you're going to get the lowest
  13571. 8:40:17views here. So let me just do one thing.
  13572. 8:40:20I'm just going to remove some of these
  13573. 8:40:22here. Let's comment it out
  13574. 8:40:26this this.
  13575. 8:40:32Okay. So you have the lowest views which
  13576. 8:40:34is 40 and highest view which is 180. So
  13577. 8:40:37you can go here and can confirm 40 and
  13578. 8:40:40180. Okay. So with this I think we have
  13579. 8:40:42covered the many to many table analysis
  13580. 8:40:44aggregate. Now let's move to the next
  13581. 8:40:46one. So let's understand group by having
  13582. 8:40:48count disting and some of the basics of
  13583. 8:40:50indexes. Okay. Okay. So moving to the
  13584. 8:40:52next one which will be group by. Okay.
  13585. 8:40:55So let's create a group by dossql file.
  13586. 8:40:58So what actually group by is right. So
  13587. 8:41:00in simple way to explain group by will
  13588. 8:41:03be or group by creates
  13589. 8:41:06groups
  13590. 8:41:08of rows. Okay. In the previous section
  13591. 8:41:11we discussed aggregate function right
  13592. 8:41:13count sum average mean max all these.
  13593. 8:41:16Now let's say what we are going to do
  13594. 8:41:18right these uh functions calculates
  13595. 8:41:21values inside each group right. So here
  13596. 8:41:24what we are going to understand in this
  13597. 8:41:26particular section in the previous
  13598. 8:41:27multiple times we have used something
  13599. 8:41:29called where. So where actually do what
  13600. 8:41:33where actually filters very very
  13601. 8:41:35important normal rows before grouping.
  13602. 8:41:40Okay. So this will be where keyword I'm
  13603. 8:41:43just talking about I'm just telling
  13604. 8:41:45keyword only so that you can understand
  13605. 8:41:46where actually filters normal rows
  13606. 8:41:49before grouping what having will do
  13607. 8:41:53having filters right we have having
  13608. 8:41:55filters. So having filters groups
  13609. 8:41:59after grouping now this might be a
  13610. 8:42:02little tricky until unless we are going
  13611. 8:42:03to understand the concept. Okay. Now
  13612. 8:42:06when you're going to use having so
  13613. 8:42:08you're going to use having when the
  13614. 8:42:10condition is going to depend or
  13615. 8:42:12condition depends on a aggregate
  13616. 8:42:13function.
  13617. 8:42:15So now the best option will be I've
  13618. 8:42:17already created one proper example for
  13619. 8:42:20this that you have to find the authors.
  13620. 8:42:23Okay. So to find the authors who have
  13621. 8:42:27written
  13622. 8:42:29at least two posts. Okay. So you have to
  13623. 8:42:33find the authors who have written at
  13624. 8:42:35least two post. Okay. Now here you also
  13625. 8:42:38have to do what? You have to use some
  13626. 8:42:40kind of aggregate functions. Correct?
  13627. 8:42:42Because you have to get the count right
  13628. 8:42:44count sorry count of the post. So let's
  13629. 8:42:47how we can do that. So I'm going to do
  13630. 8:42:50select. Now first of all when I'm
  13631. 8:42:52talking about find authors so that means
  13632. 8:42:54what I need to get what the author name
  13633. 8:42:57so what actually author name so author
  13634. 8:42:58name here is either this is Anona or
  13635. 8:43:00Rahul so author name is nothing but your
  13636. 8:43:03username right so this is where so what
  13637. 8:43:05I'm going to do I'm just going to do eu
  13638. 8:43:07dot name as author name
  13639. 8:43:12comma and now what I need to do I
  13640. 8:43:14already mentioned that we need to have
  13641. 8:43:16the count why because we need to see who
  13642. 8:43:18written at least two post so that means
  13643. 8:43:20I need to use aggregate function here
  13644. 8:43:23and how I can see that whether this
  13645. 8:43:25particular author already created two
  13646. 8:43:27post or not and that is from this post
  13647. 8:43:31id. So we have this post id right. So
  13648. 8:43:33I'm just going to do here p do id as
  13649. 8:43:37total
  13650. 8:43:38posts and now what I need to do right
  13651. 8:43:41I'll also give you the sum and let's do
  13652. 8:43:44the p dot views as total views.
  13653. 8:43:49Okay. So now first of all we need to get
  13654. 8:43:51the table. So first table will be my
  13655. 8:43:53user table because I need to get the
  13656. 8:43:55username. So I'm just going to do user.
  13657. 8:43:57But very very important whenever you're
  13658. 8:43:59going to use the table allis you have to
  13659. 8:44:01use allis here or else I already
  13660. 8:44:03mentioned that posgress will not be able
  13661. 8:44:05to understand that this U means the user
  13662. 8:44:08table. Okay. And then what we need to do
  13663. 8:44:10right we need to do a left join. So I'll
  13664. 8:44:13do left join with the post table as P
  13665. 8:44:16because we're using the P here. Okay. So
  13666. 8:44:18this will be small P where so here what
  13667. 8:44:22will be the matching condition multiple
  13668. 8:44:23times I will discuss because your U do
  13669. 8:44:26ID user do ID similar to your post dot
  13670. 8:44:29user id so that means here I'm just
  13671. 8:44:31going to do u do id will be equal to p
  13672. 8:44:34dot user id correct so this will be my
  13673. 8:44:37matching condition and now here what I'm
  13674. 8:44:39going to do right I'm just going to do a
  13675. 8:44:40group by
  13676. 8:44:42u do ID and let's do a uh what else we
  13677. 8:44:45going to do maybe let's do u dot name
  13678. 8:44:47Okay. User do ID user dot name. Now what
  13679. 8:44:50I mentioned here, so let's go here. What
  13680. 8:44:53I mentioned having filters groups after
  13681. 8:44:55grouping filter normal rows before
  13682. 8:44:57grouping. So now we need to use a having
  13683. 8:45:00because we already have done the already
  13684. 8:45:02used the grouping. So here what you have
  13685. 8:45:05to do you have to use having. So this
  13686. 8:45:06having is simply going to tell that okay
  13687. 8:45:08now I need to see if the count of that
  13688. 8:45:11post. So count of p dot id is greater
  13689. 8:45:14than or equal to two or not because that
  13690. 8:45:16is my criteria here that that user at
  13691. 8:45:18least have written two post. So that
  13692. 8:45:20means now I'm going to see having count
  13693. 8:45:23dot count of p do ID should be greater
  13694. 8:45:25than or equal to two simple and then I'm
  13695. 8:45:28also going to do order by simply and
  13696. 8:45:30let's do order by of total post.
  13697. 8:45:34Let's do this one as descending order.
  13698. 8:45:36So now let's see what actually this is
  13699. 8:45:38doing. So first of all we have got from
  13700. 8:45:39user as you start with the user table
  13701. 8:45:43left join post sp means connect each
  13702. 8:45:46user with their post. What actually this
  13703. 8:45:49group by is doing now? So this group by
  13704. 8:45:52I already mentioned this group by is
  13705. 8:45:53going to create one group for each user.
  13706. 8:45:57So group by creates group of rows. So
  13707. 8:45:59this group by is going to do what is
  13708. 8:46:01going to create a group of each user.
  13709. 8:46:04Why? Now you might ask me okay why it
  13710. 8:46:06will create? So whatever post let's say
  13711. 8:46:09we have two users Anona and Rahul. So
  13712. 8:46:12all post written by Anunna become one
  13713. 8:46:14group and all post written by Rahul
  13714. 8:46:17become another group. Okay. So this is
  13715. 8:46:20very very important. So this is this
  13716. 8:46:22line is actually doing that. So it's
  13717. 8:46:24going to create one group for each user.
  13718. 8:46:26Count is going to do what? So it's
  13719. 8:46:28simply going to check the count that how
  13720. 8:46:31many post exist inside each user group.
  13721. 8:46:35Right? This what I'm talking about. Then
  13722. 8:46:36we have the sum. Sum is going to give
  13723. 8:46:38you the sum. And this having what
  13724. 8:46:40actually having is doing? Now this is a
  13725. 8:46:42criteria. This is simply telling that
  13726. 8:46:44keep only those user group where the
  13727. 8:46:48post count is greater than or equal to
  13728. 8:46:50two. You're getting so first we are
  13729. 8:46:53using group by then we are using having
  13730. 8:46:55very very important that is the reason I
  13731. 8:46:57mentioned that having filter groups
  13732. 8:46:59after grouping. So first we created the
  13733. 8:47:01group and then we simply telling and
  13734. 8:47:03sorry first we have created the group
  13735. 8:47:05then we counted all the post then we
  13736. 8:47:08created the sum also then we created one
  13737. 8:47:10group for ana one group for Rahul and
  13738. 8:47:13now I'm doing what now I'm doing the
  13739. 8:47:15filtering on that count that you are
  13740. 8:47:17going to only keep those user group
  13741. 8:47:20where the count is more than two you're
  13742. 8:47:23getting so if the count is less so it's
  13743. 8:47:25not going to give you that result and
  13744. 8:47:26then you're simply doing a order by so
  13745. 8:47:28now let's run this and see what you're
  13746. 8:47:30getting. Okay,
  13747. 8:47:33that's it. So now you're getting Anunna
  13748. 8:47:36total post is two and total views is
  13749. 8:47:40140. Why? Because Anunna created two
  13750. 8:47:42post as much as I remember I added. Let
  13751. 8:47:46me just check. Okay, see this one right?
  13752. 8:47:51So this is how you need to do. Let's say
  13753. 8:47:52you want to do that. Okay, I wanted to
  13754. 8:47:54check whe the count is exactly equal to
  13755. 8:47:56one. So now if you run this, what will
  13756. 8:47:58happen?
  13757. 8:48:00So you're going to get Rahul because
  13758. 8:48:01Rahul created only one post. So this is
  13759. 8:48:04how you have to use group by and having.
  13760. 8:48:07Okay. So [snorts] I think that's all for
  13761. 8:48:10this particular one. Let's do one more
  13762. 8:48:12uh concept here which is also important
  13763. 8:48:14and that is count distinct. This will
  13764. 8:48:16not take much time but let's do this and
  13765. 8:48:19I'll do SQL. So what this count distinct
  13766. 8:48:22is going to do right. So this is going
  13767. 8:48:24to count unique values right? So it's
  13768. 8:48:26going to count
  13769. 8:48:28unique values. So what I mean by this?
  13770. 8:48:32Okay. Now first of all also another
  13771. 8:48:35thing I need to mention and that is when
  13772. 8:48:37this is useful. Now let's say you're
  13773. 8:48:39using some joints, right? And this is
  13774. 8:48:41useful when joins create repeated rows.
  13775. 8:48:44Now example will be okay let me think.
  13776. 8:48:46So okay so let's say you have one post
  13777. 8:48:49right? I already mentioned that one post
  13778. 8:48:50is going to have multiple tags right?
  13779. 8:48:52You have one post. That post has ReactJS
  13780. 8:48:54tag, NexJS tag, SQL tag, DB tag, HTML
  13781. 8:48:57tag. Now when you're going to join this
  13782. 8:49:01post with post tags and the tags table
  13783. 8:49:04that we have, the same post can appear
  13784. 8:49:07multiple times, right? It's possible. So
  13785. 8:49:09this is where what we going to do,
  13786. 8:49:11right? We are going to use something
  13787. 8:49:12called count distinct and the property
  13788. 8:49:14which is going to count only the unique
  13789. 8:49:16post. So the best example will be let's
  13790. 8:49:19say I wanted to count how many
  13791. 8:49:22unique post sorry how many unique posts
  13792. 8:49:26are connected
  13793. 8:49:28to each tag very good example that how
  13794. 8:49:32many unique post are connected to each
  13795. 8:49:34tag this is what I wanted to uh see now
  13796. 8:49:38let's we are going to do that if you
  13797. 8:49:40notice that whenever we are going to
  13798. 8:49:41write this particular uh query right
  13799. 8:49:43you'll notice that how many previous
  13800. 8:49:45concept we are applying here. So what I
  13801. 8:49:47mean by this? So previously we have
  13802. 8:49:48understood the concept of select, the
  13803. 8:49:50concept of allis, the concept of left
  13804. 8:49:53joint, concept of inner joint, concept
  13805. 8:49:55of group by order by. So basically what
  13806. 8:49:57I'm telling that the mode we are
  13807. 8:49:59progressing in this particular video,
  13808. 8:50:00the mode we are applying the previous
  13809. 8:50:01concept. So here for example what I need
  13810. 8:50:04to get I need to get the tag. So first
  13811. 8:50:06I'm just going to get the tag name. So
  13812. 8:50:08I'm just going to do this one as tag
  13813. 8:50:10name. Next give comma. And now what I'm
  13814. 8:50:13going to do right I need to count how
  13815. 8:50:14many unique post how many unique post
  13816. 8:50:16I'm going to give count and here you
  13817. 8:50:18have to give distinct
  13818. 8:50:21okay distinct p dot id okay so post do
  13819. 8:50:24ID as total
  13820. 8:50:28unique
  13821. 8:50:31posts and now what I'm going to do first
  13822. 8:50:34of all what I need to get I need to get
  13823. 8:50:35the tags table so tag the allies as I've
  13824. 8:50:38given t so this will be as t and then we
  13825. 8:50:40are going to do a left join
  13826. 8:50:42So I'm going to do a left join with my
  13827. 8:50:44post tags table because there is a
  13828. 8:50:47relations with the post tag table also.
  13829. 8:50:49So here I'm just going to take this one
  13830. 8:50:51as let's do post tag. So PT and then I'm
  13831. 8:50:54simply oh sorry PT.
  13832. 8:50:57Then I'm simply going to check the
  13833. 8:50:58matching condition that the tag do ID
  13834. 8:51:01should be equal to PT which is the post
  13835. 8:51:04tag dot tag ID because there is a
  13836. 8:51:06matching here right? So your post tag
  13837. 8:51:08has the tag id which is similar to this
  13838. 8:51:10tags do id correct. So I'm simply going
  13839. 8:51:13to do this t dot id which is tag do id
  13840. 8:51:16is equal to post tag dot tag id. Okay so
  13841. 8:51:19this is the first one. Then [snorts] I'm
  13842. 8:51:21going to give another left join and this
  13843. 8:51:24left join will be on my post as p. This
  13844. 8:51:27is what I have given here p. And here
  13845. 8:51:29also you do the same logic. So now this
  13846. 8:51:32time this will become t dot id will be
  13847. 8:51:35your what? Sorry, let's do PT dot PT dot
  13848. 8:51:40post ID. I think PT do.post ID this one.
  13849. 8:51:43So PT dot post ID will be equal to P do
  13850. 8:51:45ID and now I'm going to do a group by.
  13851. 8:51:49So do group by T dot id tag ID and P dot
  13852. 8:51:53name. And maybe let's do a order by
  13853. 8:51:56also. So I'll do order by total unique
  13854. 8:51:59post in descending order.
  13855. 8:52:03the main part that we are simply doing
  13856. 8:52:05for this particular section which is
  13857. 8:52:07this one. So we're simply getting the
  13858. 8:52:09unique post that are connected to each
  13859. 8:52:11tag. Now you might be thinking okay how
  13860. 8:52:13I'm writing all of this. Believe me I've
  13861. 8:52:15written all of this so many times
  13862. 8:52:17previously. So for me it will be easy
  13863. 8:52:19for you. It can can be difficult if
  13864. 8:52:21you're a beginner if you're a student if
  13865. 8:52:23you're very less experienced. You need
  13866. 8:52:25to do lot of practice. These things will
  13867. 8:52:27not come very easily. You can be very
  13868. 8:52:29honest that how where you have to apply
  13869. 8:52:30lab join where you're going to apply
  13870. 8:52:32group by where you going to apply having
  13871. 8:52:35okay these thing where you're going to
  13872. 8:52:37use where so you need to do a lot of
  13873. 8:52:39practice but this is exactly what this
  13874. 8:52:41query is doing see whenever you're going
  13875. 8:52:43to work with relationships right you
  13876. 8:52:45have to see the table that what actually
  13877. 8:52:47your table structure is and what are the
  13878. 8:52:49properties in two tables that are
  13879. 8:52:52connected with each other for example
  13880. 8:52:54tag ID is similar to your tag ID here
  13881. 8:52:57post ID for this table is similar to
  13882. 8:52:59your post do ID. So this is what I'm
  13883. 8:53:01talking about. So now let's run this. If
  13884. 8:53:04I run here, right? So let's see what
  13885. 8:53:05you're getting
  13886. 8:53:12and you're going to get the unique post
  13887. 8:53:14that how many tag. So this SQL is
  13888. 8:53:16associated with two unique post back end
  13889. 8:53:19is associated with one post. And if you
  13890. 8:53:22want we can also verify. So let's see
  13891. 8:53:24what we are getting here.
  13892. 8:53:30we have SQL two times and back end one
  13893. 8:53:34time. All right. So that means this is
  13894. 8:53:36working fine. So with this I think we
  13895. 8:53:38have covered the group by and also count
  13896. 8:53:41distinct. So now let's move on to the
  13897. 8:53:43next one. All right. So moving to the
  13898. 8:53:44next one. Let's understand another very
  13899. 8:53:46very interesting concept and that is
  13900. 8:53:47called sub queries. Okay.
  13901. 8:53:52SQL. So let's see what actually this is
  13902. 8:53:54right. So I'll do sub queries
  13903. 8:53:57in post posgress.
  13904. 8:54:03Let me refresh this
  13905. 8:54:08in postgress
  13906. 8:54:14queries [snorts]
  13907. 8:54:15nested inside another SQL statement.
  13908. 8:54:18Okay. All right. So I just wanted to
  13909. 8:54:19show you because I'm going to explain
  13910. 8:54:21this one in a very simple way. So what
  13911. 8:54:23[snorts] I mean by this? So this is
  13912. 8:54:25simply we are running one query inside
  13913. 8:54:27another query. So we are running one
  13914. 8:54:30query inside another query as simple as
  13915. 8:54:33that. Okay. So what postgress is going
  13916. 8:54:36to do? Postgress is going to run the
  13917. 8:54:38inner query first. So I'll do runs
  13918. 8:54:42the inner query
  13919. 8:54:45first and then it's going to run the
  13920. 8:54:47outer query. Okay. So it's going to run
  13921. 8:54:49the outer query. All right. So now let's
  13922. 8:54:53understand this one in a uh simple
  13923. 8:54:55example. So your task is what we need to
  13924. 8:54:57do right maybe let's do one thing that
  13925. 8:55:00first we are going to have two queries.
  13926. 8:55:02Okay in the inner query we are going to
  13927. 8:55:05calculate some average values and that
  13928. 8:55:08value your outer query is going to use.
  13929. 8:55:11So that means when your inner query is
  13930. 8:55:13going to return sorry calculate
  13931. 8:55:15something right it it's I'm just giving
  13932. 8:55:17as part of this example because we are
  13933. 8:55:19using this file as our data source. But
  13934. 8:55:22for your project it can be anything. So
  13935. 8:55:24for this one I'm just doing right let's
  13936. 8:55:25say I'm going to calculate my average
  13937. 8:55:27views and that average views I wanted to
  13938. 8:55:29return. So that is going to return some
  13939. 8:55:31value and that value I'm going to use in
  13940. 8:55:33my outer query. So that will be two
  13941. 8:55:35query. Okay. So let's how we are going
  13942. 8:55:37to do that. So now uh okay what will be
  13943. 8:55:41the question? The question will be maybe
  13944. 8:55:42let's do this one in this way that which
  13945. 8:55:44post are perform performing better than
  13946. 8:55:50average
  13947. 8:55:52something like this. So here how I can
  13948. 8:55:55do that right. So I'm just going to do
  13949. 8:55:56select so give me the title of that post
  13950. 8:55:59comma give me the status of that post
  13951. 8:56:01and also give me the views but this one
  13952. 8:56:04you wanted to give me from my post
  13953. 8:56:06table.
  13954. 8:56:08Now here what I'm going to do where
  13955. 8:56:11the views now we have our inner query.
  13956. 8:56:15So where the views is what? So views is
  13957. 8:56:18greater than the average value. So that
  13958. 8:56:21means here now I'm going to write my
  13959. 8:56:22inner query. So now I'm just going to do
  13960. 8:56:24a select. So how you can calculate
  13961. 8:56:27average views. What thing you have to
  13962. 8:56:29use here? Means what are the things you
  13963. 8:56:30have learned so far? So to calculate
  13964. 8:56:32average views you have to use aggregate
  13965. 8:56:34function. Right? So what will be my
  13966. 8:56:35aggregate function? I have to do is
  13967. 8:56:36average. So average will be on what on
  13968. 8:56:39views and then you have to give what? So
  13969. 8:56:41from where you have to do this from your
  13970. 8:56:43post table. So now this is your inner
  13971. 8:56:46query. So this is going to give you the
  13972. 8:56:48average views. This is going to return
  13973. 8:56:50the data and then you're going to use or
  13974. 8:56:53run this outer query. So that means
  13975. 8:56:55you're simply telling select title
  13976. 8:56:57status views from post where views is
  13977. 8:57:00greater than 100. Sorry views is greater
  13978. 8:57:02than 20. So why I'm talking about this?
  13979. 8:57:04So consider this particular query result
  13980. 8:57:07is this particular query's result is 20
  13981. 8:57:11right so this
  13982. 8:57:13block is going to return 20 so it will
  13983. 8:57:16become this so that means I'm simply
  13984. 8:57:17telling that where the views is greater
  13985. 8:57:19than 20 you're getting so this is about
  13986. 8:57:21the subqueries and then you can also do
  13987. 8:57:24order by so do order by views
  13988. 8:57:30and then I'm going to do descending
  13989. 8:57:32order so if you save this and run this
  13990. 8:57:35it's going to work. So let's run it
  13991. 8:57:37again.
  13992. 8:57:39Okay. So this is what you're getting
  13993. 8:57:42right. But the point is you have to
  13994. 8:57:43understand this one that how you're
  13995. 8:57:44running queries. And you can also see
  13996. 8:57:46some example. Give me one example.
  13997. 8:57:49See what it's telling here.
  13998. 8:57:54Select name query where the price is
  13999. 8:57:56select average price from products. This
  14000. 8:57:58is almost the same example but this is
  14001. 8:58:00also doing the same thing. This is a
  14002. 8:58:01subquery, right? This is one subquery.
  14003. 8:58:04The point you have to understand that
  14004. 8:58:05it's going to run the subquery first.
  14005. 8:58:07It's going to return the result and then
  14006. 8:58:10the outer query is going to use that
  14007. 8:58:12result. Okay. So I think that's all for
  14008. 8:58:15this particular concept. I think we
  14009. 8:58:17don't have much here. Now also I thought
  14010. 8:58:20that I'm not going to explain or cover
  14011. 8:58:22the indexes but let's give you some idea
  14012. 8:58:24so that if this I already mentioned if
  14013. 8:58:26the video this video will do good I'm
  14014. 8:58:28going to create anyway one more uh part
  14015. 8:58:32two for this particular video but that
  14016. 8:58:35will happen only if this one will do
  14017. 8:58:36good. Okay. So I'm going to cover some
  14018. 8:58:38of the very advanced level of database
  14019. 8:58:40concept that you have to pay to watch
  14020. 8:58:42those courses. Okay. So I'm going to do
  14021. 8:58:44this one for free. So all right. So
  14022. 8:58:46first of all let's understand what are
  14023. 8:58:47indexes. Okay simply indexes help you or
  14024. 8:58:51indexes helps not me indexes helps
  14025. 8:58:53postgress find rows faster. So I'm just
  14026. 8:58:56going to do index helps postgress
  14027. 8:59:01find rows faster. This will be the best
  14028. 8:59:05example. What will happen without an
  14029. 8:59:07index? Now let's say you have 1,000
  14030. 8:59:10data. You have to find something. So
  14031. 8:59:12without if there is no index posgress is
  14032. 8:59:14going to scan many rows to find your
  14033. 8:59:17matching data with an index what it
  14034. 8:59:20usually do right it's going to jump
  14035. 8:59:22closer to the data it needs so how what
  14036. 8:59:25will be the uh how to frame mental model
  14037. 8:59:30consider there is a book now you have a
  14038. 8:59:32book index right so book index means
  14039. 8:59:34number what that will help you so that
  14040. 8:59:37is going to help you find a particular
  14041. 8:59:38topic without reading every page. This
  14042. 8:59:42is the best example, correct? Similarly,
  14043. 8:59:45if you have a database index, it's going
  14044. 8:59:47to find rows without checking every
  14045. 8:59:49data. So, I'm going to repeat once again
  14046. 8:59:52your book index, right? Book will always
  14047. 8:59:54have the index, right? In the first
  14048. 8:59:55page, let's say you have some indexes,
  14049. 8:59:57you can go to a certain page. Similarly,
  14050. 9:00:00in posgress or any DB, if you have
  14051. 9:00:02indexes or database index is going to
  14052. 9:00:04help find rows without checking every
  14053. 9:00:08row. So, that means what? It's going to
  14054. 9:00:10do what? It's going to speed up your
  14055. 9:00:12query reading process. For example,
  14056. 9:00:15let's say your query reading process is
  14057. 9:00:16select, right? So, it's going to speed
  14058. 9:00:19it up this process. This particular
  14059. 9:00:22process, right? So, I do this particular
  14060. 9:00:25process.
  14061. 9:00:27So, now uh what I will do right maybe
  14062. 9:00:30let's do one thing. So, first I will
  14063. 9:00:33just see my data. So, I'm just going to
  14064. 9:00:36select let's take the ID. I'll do a
  14065. 9:00:38title. Let's do a status. I'm going to
  14066. 9:00:41do a views and also we have the user ID.
  14067. 9:00:45Correct. And I'm just going to do this
  14068. 9:00:46one from posts. So this is my post table
  14069. 9:00:51data. Correct? So now I have one
  14070. 9:00:54requirement that I need to filter the
  14071. 9:00:55post by status. So something like this,
  14072. 9:00:58right? So you have / posts and then
  14073. 9:01:00you're going to do status equal to
  14074. 9:01:04published.
  14075. 9:01:05Correct? So this is one filter. So here
  14076. 9:01:08how you can do that. So what we'll do
  14077. 9:01:10select give me ID give me my title give
  14078. 9:01:14me my status and here I'm just going to
  14079. 9:01:18do from posts table and then I'm simply
  14080. 9:01:20going to do where the status is equal to
  14081. 9:01:23published correct. So status will be
  14082. 9:01:26published.
  14083. 9:01:29So this will be my query. Simple.
  14084. 9:01:32So now the problem will occur when this
  14085. 9:01:35post table will become very large. So it
  14086. 9:01:38always needs to do what posgress needs
  14087. 9:01:40to find each and every rows to find this
  14088. 9:01:42particular uh criteria here that okay
  14089. 9:01:46give me these many post maybe there are
  14090. 9:01:48500 post where it is the status is
  14091. 9:01:50published. Now this is where what we can
  14092. 9:01:53do right we can create a index. So let's
  14093. 9:01:56how we are going to do that. So first
  14094. 9:01:57I'm going to do create. So to create
  14095. 9:01:59index you have to do index again. First
  14096. 9:02:01you have to check that if not exist.
  14097. 9:02:05Okay. And let's give this one as idx
  14098. 9:02:10posts. And now see because we need to
  14099. 9:02:12write index for this status column. So
  14100. 9:02:14I'm just giving this one as underscore
  14101. 9:02:16status. Okay. And what you have to do
  14102. 9:02:19you have to give on post and you have to
  14103. 9:02:22tell that okay which column you want to
  14104. 9:02:24do. So we want to do on the status
  14105. 9:02:26column. So now this is actually very
  14106. 9:02:28important. Okay. And you'll see that any
  14107. 9:02:30if you are working in a company or let's
  14108. 9:02:31say any open source you'll see that they
  14109. 9:02:33try to use this particular structure and
  14110. 9:02:35this is the naming style. Now what this
  14111. 9:02:38idx means this is index this is the
  14112. 9:02:40table name and this is the column name
  14113. 9:02:42very very important. Okay. So this will
  14114. 9:02:44be very important. So here
  14115. 9:02:49idx is simply telling this is the id or
  14116. 9:02:53maybe this is the index.
  14117. 9:02:55posts simply telling this is the table
  14118. 9:02:57name and status
  14119. 9:03:01will be your what column name
  14120. 9:03:04okay so now what we are going to do
  14121. 9:03:07right so let's say we want to now filter
  14122. 9:03:12and sort together so now I wanted to
  14123. 9:03:15write a query that is going to maybe
  14124. 9:03:17let's do something like this it's going
  14125. 9:03:18to find the publish post also at the
  14126. 9:03:21same time it's going to show you the
  14127. 9:03:22most viewed post first. Okay. So
  14128. 9:03:26something like this. Now here I want to
  14129. 9:03:28do very very important. Okay. See what
  14130. 9:03:29you're doing. So I'm just going to do
  14131. 9:03:30here title. Give me title. Give me my
  14132. 9:03:33status and give me the views from which
  14133. 9:03:36table. So from posts
  14134. 9:03:39table. I'm just going to do where. So we
  14135. 9:03:42already using status is equal to list
  14136. 9:03:48and I'm just going to do order by and
  14137. 9:03:50I'll do on views and descending order.
  14138. 9:03:54Now here this query although it's may
  14139. 9:03:57look very simple it's easy to understand
  14140. 9:03:59but it has two important parts okay and
  14141. 9:04:01this part we haven't discussed before
  14142. 9:04:03this where status equal to publish right
  14143. 9:04:06this is going to do what it's going to
  14144. 9:04:07filter by status and order by views
  14145. 9:04:10descending it's going to do what it's
  14146. 9:04:12going to sort by views from highest to
  14147. 9:04:13lowest
  14148. 9:04:15okay now here what we can do right now
  14149. 9:04:18let's say I again I'm just talking about
  14150. 9:04:20various way of writing indexes only for
  14151. 9:04:22now so this is one way now here this is
  14152. 9:04:25a composite thing we are doing so we are
  14153. 9:04:27doing two things you getting what I'm
  14154. 9:04:29talking about so that means we are using
  14155. 9:04:32what we are doing two things together so
  14156. 9:04:35that means this is a concept of a
  14157. 9:04:37composite index so we are doing we are
  14158. 9:04:39first filtering and also we doing a
  14159. 9:04:41order by so this will be a composite
  14160. 9:04:45composite index so how you can create
  14161. 9:04:48the index so you can create the index
  14162. 9:04:50like this so you're going to do create
  14163. 9:04:51index
  14164. 9:04:53if not exist now I'm just going to do
  14165. 9:04:56see idx for index post will be my table
  14166. 9:05:00status is one of the column that I'm
  14167. 9:05:02doing and then also I have a views
  14168. 9:05:04column and then what you have to do you
  14169. 9:05:06have to do on posts because that is the
  14170. 9:05:08table name and now you have to give what
  14171. 9:05:10you have to give status and you have to
  14172. 9:05:13give views in descending order so this
  14173. 9:05:15is one of your index you're getting what
  14174. 9:05:18I'm talking about so this is how you can
  14175. 9:05:20write a composite index index. Okay. So
  14176. 9:05:23now maybe let's do one thing. I'll give
  14177. 9:05:26you some little intermediate level of
  14178. 9:05:28concept or how we can write indexes on
  14179. 9:05:30foreign key columns. Now we already have
  14180. 9:05:33discussed the concept of foreign key.
  14181. 9:05:34Right? For example, let's say if I go
  14182. 9:05:37here, your post dot user id is
  14183. 9:05:39referencing the user do ID. This one,
  14184. 9:05:41right? So means every post is belong to
  14185. 9:05:44a valid user. Correct? Now this is the
  14186. 9:05:47concept about this is a concept of
  14187. 9:05:49foreign keys. So something like this.
  14188. 9:05:50Let's say you have one get request
  14189. 9:05:54users and then I'm just going to do id
  14190. 9:05:58and then give me posts.
  14191. 9:06:00So that means your back end needs to
  14192. 9:06:02find all post written by one particular
  14193. 9:06:05user. You're getting all these things
  14194. 9:06:06what I'm talking about very very
  14195. 9:06:07important thing. So that mean this is
  14196. 9:06:09this is a very valid scenario right
  14197. 9:06:11let's say you are creating one
  14198. 9:06:12particular user profile where you need
  14199. 9:06:14to render all the post that is written
  14200. 9:06:16by that particular user. So that means
  14201. 9:06:18how you can do that. So you have to do
  14202. 9:06:20like this right? So we're going to do
  14203. 9:06:21select and give me title, give me
  14204. 9:06:25status, give me views.
  14205. 9:06:28Now what I have to do first from posts
  14206. 9:06:31because this is the table and then what
  14207. 9:06:34I'm going to do here I'm just going to
  14208. 9:06:35do where user ID is equal to. So I'm
  14209. 9:06:38going to write inner query here. User ID
  14210. 9:06:40is going to what? Select ID from where?
  14211. 9:06:44from users table and then I'm simply
  14212. 9:06:48going to do okay give me where name is
  14213. 9:06:51equal to anona now this anona name is
  14214. 9:06:53can be dynamic value because you're not
  14215. 9:06:55going to write a hardcoded value but
  14216. 9:06:57this name will be this id that you are
  14217. 9:07:00getting correct so this will be let's
  14218. 9:07:02say this can be also rahul I'm just
  14219. 9:07:03giving one example
  14220. 9:07:06now here how you can write a index for
  14221. 9:07:09this here if you notice a post do user
  14222. 9:07:12id actually using to find post for a
  14223. 9:07:15particular user. So that is the reason
  14224. 9:07:17this will be good column that to write a
  14225. 9:07:20index. So that means if I want to do
  14226. 9:07:22write index for this so I'm just going
  14227. 9:07:24to do create index
  14228. 9:07:27if not
  14229. 9:07:29if not exist
  14230. 9:07:32and here I'm do again same format I'll
  14231. 9:07:34do id sorry idx post is the table name
  14232. 9:07:37and I'm just going to do user ID because
  14233. 9:07:40this is what we're doing and this will
  14234. 9:07:41be on post table and what will be your
  14235. 9:07:44property your property will be on user
  14236. 9:07:46id because this is a foreign key
  14237. 9:07:47relations right so this is how you can
  14238. 9:07:50yet. So I think we have covered most of
  14239. 9:07:52the things and this now here we don't
  14240. 9:07:54have anything that much to run but this
  14241. 9:07:56is how you can write indexes. The point
  14242. 9:07:58is you can't write indexes blindly. You
  14243. 9:08:01have to see the requirement that where
  14244. 9:08:03you are going to write but the format
  14245. 9:08:04has to be very important and it is also
  14246. 9:08:06very very easier to understand also that
  14247. 9:08:08why you writing all this. Okay. So with
  14248. 9:08:11this I think we have almost covered all
  14249. 9:08:13the things. Let me just think that what
  14250. 9:08:14else we can cover that we going to cover
  14251. 9:08:16in the next part and with that we'll end
  14252. 9:08:18this particular video. So let's move on
  14253. 9:08:20to the next one. All right. So let's end
  14254. 9:08:21this particular video with transactions
  14255. 9:08:24although I'm thought that I'm not going
  14256. 9:08:25to cover but let's uh let me give you
  14257. 9:08:27some of the very little glimpse what
  14258. 9:08:29actually it is because it's very
  14259. 9:08:31important concept and [snorts] then if
  14260. 9:08:33I'm creating any next but uh anytime
  14261. 9:08:35soon then I'll be also covering this one
  14262. 9:08:37later. So this will be transactions.
  14263. 9:08:41Okay. So what actually transaction does
  14264. 9:08:44is it lets you uh multiple or it give
  14265. 9:08:49you the opportunity to run multiple.
  14266. 9:08:54So do multiple
  14267. 9:08:57SQL statements
  14268. 9:09:00run as one safe
  14269. 9:09:05unit. Okay. So simply we are telling
  14270. 9:09:10that whatever we are running either all
  14271. 9:09:12thing or everything is going to succeed
  14272. 9:09:15or everything will be cancelled.
  14273. 9:09:18So now
  14274. 9:09:19okay this where this will be very very
  14275. 9:09:22helpful. Now this is very very helpful.
  14276. 9:09:24Let's say we're creating an e-commerce
  14277. 9:09:26website where user is placing an order
  14278. 9:09:29at the same time when you're placing an
  14279. 9:09:30order right. So what are the things
  14280. 9:09:32you're going to do? So you place a order
  14281. 9:09:34at the same time if the order is
  14282. 9:09:36successful you need to reduce the stock
  14283. 9:09:38count of that particular product right.
  14284. 9:09:40So reduce the stock of that product. So
  14285. 9:09:44this is also comes under transaction. At
  14286. 9:09:46the same time you need to do creating
  14287. 9:09:48payment records,
  14288. 9:09:52transferring money,
  14289. 9:09:55right? Also you need to do what? You
  14290. 9:09:58need to also do creating user
  14291. 9:10:02records
  14292. 9:10:03with related profile data. All right. So
  14293. 9:10:09that means what what actually we are
  14294. 9:10:11doing here if I if I ask you to sum up
  14295. 9:10:14in one single sentence. So this is
  14296. 9:10:16simply we are using a transaction where
  14297. 9:10:17multiple database changes must succeed
  14298. 9:10:20together. So we have to do all these
  14299. 9:10:22things. Okay. So now let's see how we
  14300. 9:10:25are going to do this uh in our example.
  14301. 9:10:28So now first of all what we need to do
  14302. 9:10:30right how we are going to start a
  14303. 9:10:32transaction. So that is the first thing
  14304. 9:10:33you have to understand. So first
  14305. 9:10:35whenever you're going to start a
  14306. 9:10:36transaction you have to do begin. So
  14307. 9:10:39begin is going to start a transaction.
  14308. 9:10:41Now what will happen right once you
  14309. 9:10:44write begin postgress is not going to
  14310. 9:10:46save the changes permanently until
  14311. 9:10:49unless we are going to do something
  14312. 9:10:50called commit. So that we are going to
  14313. 9:10:52discuss later. Now let's say we start a
  14314. 9:10:54transaction. Now in the first step what
  14315. 9:10:56we wanted to do right we wanted to
  14316. 9:10:58update our draft post. So we have a
  14317. 9:11:00draft post right you search here we have
  14318. 9:11:02a draft post correct. So which one is
  14319. 9:11:07that? This is the draft one. Correct. So
  14320. 9:11:08this is a draft post. So here we have
  14321. 9:11:11what indexes for uh beginner. This is
  14322. 9:11:15the title. Maybe let's do one thing.
  14323. 9:11:17Let's change the status from draft to
  14324. 9:11:19published. So this will be my first
  14325. 9:11:21change. So I'm just going to do update.
  14326. 9:11:23So update my post table. Set the status
  14327. 9:11:28to published
  14328. 9:11:33where title is equal to what?
  14329. 9:11:37This one title is indexes for beginner
  14330. 9:11:41correct and then I'm also going to give
  14331. 9:11:43a and because see there can be two title
  14332. 9:11:46also with the same so you always have to
  14333. 9:11:48give status is draft so these small
  14334. 9:11:51small things you have to understand so
  14335. 9:11:52this is the first thing that we are
  14336. 9:11:53doing what you're doing right now in the
  14337. 9:11:56step two this is not the transaction is
  14338. 9:11:58not yet completed I wanted to increase
  14339. 9:12:00the views for the same post okay so
  14340. 9:12:03let's say how many views I have 40 so
  14341. 9:12:05now I wanted to increase the views of
  14342. 9:12:07this post. So again, I'm just going to
  14343. 9:12:08do update posts
  14344. 9:12:11set views equal to views + 50 where
  14345. 9:12:18title is equal to indexes for beginners.
  14346. 9:12:24Okay, so this is the third one and then
  14347. 9:12:27what I'll do right maybe let's do I want
  14348. 9:12:30to check the result that if this is done
  14349. 9:12:31or not. So I'm just going to do give me
  14350. 9:12:33the title, give me the status, give me
  14351. 9:12:36the views
  14352. 9:12:38from posts table where title is equal to
  14353. 9:12:43indexes for beginners.
  14354. 9:12:46Okay. And then at the end what you have
  14355. 9:12:49to do as I already mentioned very very
  14356. 9:12:50important you have to do commit. So what
  14357. 9:12:53commit is going to do? Commit is going
  14358. 9:12:54to permanently save all these changes
  14359. 9:12:56inside the transaction because this is
  14360. 9:12:58whatever we are doing this is a
  14361. 9:12:59transaction. Okay. Now there is a
  14362. 9:13:02concept called roll back also, right?
  14363. 9:13:04Roll back is going to cancel all the
  14364. 9:13:05changes. But for now, if you're
  14365. 9:13:07understanding this thing, it is more
  14366. 9:13:08than enough. Okay. So now let's run this
  14367. 9:13:11first and see what is happening here.
  14368. 9:13:14Run again. I'm doing some mistakement.
  14369. 9:13:17Update post.
  14370. 9:13:22I'm reading update post. Something we
  14371. 9:13:24doing wrong. Let me just check.
  14372. 9:13:27There might be some places I haven't
  14373. 9:13:29given. Okay, this is where I have to
  14374. 9:13:31give this.
  14375. 9:13:35Okay, so let's run again and you can see
  14376. 9:13:37that we do begin update one update one.
  14377. 9:13:39This is the result. This is now publish
  14378. 9:13:41the PUS are also changed and this is
  14379. 9:13:42commit. Okay, why you're seeing for 140?
  14380. 9:13:46Because the previous time also we have
  14381. 9:13:47already updated one time. That's the
  14382. 9:13:48reason. But you can see that from begin
  14383. 9:13:50to commit this transaction is now
  14384. 9:13:52completed. So overall you have to
  14385. 9:13:54understand the concept that what you're
  14386. 9:13:55doing when we are doing multiple DB
  14387. 9:13:56queries at the same time transactions
  14388. 9:13:59are extremely helpful. So I think that's
  14389. 9:14:02all for this particular video. I think
  14390. 9:14:03we have covered almost most of the
  14391. 9:14:05things this is more than enough. Believe
  14392. 9:14:06me or not if you can understand all
  14393. 9:14:08these things and do right you will be in
  14394. 9:14:10a very good position. So if you like
  14395. 9:14:12this video give a like and I will see in
  14396. 9:14:14my next video. Till then good luck and
  14397. 9:14:16peace. All right everyone. So now this
  14398. 9:14:18is the time that we are going to build
  14399. 9:14:20our actual project. So this will be a
  14400. 9:14:22complete practical section and in this
  14401. 9:14:24project we are going to apply all the
  14402. 9:14:26concepts that we have learned so far
  14403. 9:14:28right and I'm pretty much sure that you
  14404. 9:14:29have already covered the previous uh
  14405. 9:14:31posgress uh section I already mentioned
  14406. 9:14:34multiple times that that is actually
  14407. 9:14:35mandatory and prerequisite and it's
  14408. 9:14:37going to give you a proper idea that how
  14409. 9:14:39you can uh write SQL queries right how
  14410. 9:14:42what are the concept of postgress and
  14411. 9:14:44all these things so what are the things
  14412. 9:14:46we're going to understand in this
  14413. 9:14:47particular project and why this project
  14414. 9:14:48is very very important even though I'm
  14415. 9:14:50Pretty much some of you already know
  14416. 9:14:53CRUD, JWT O, Google Earth, all those
  14417. 9:14:55things. But still this project or
  14418. 9:14:57whatever you are going to build will be
  14419. 9:14:59a complete game changer for all of you.
  14420. 9:15:02Why? Because what are the things that we
  14421. 9:15:04are going to learn? First of all, you
  14422. 9:15:05have to understand that this project is
  14423. 9:15:07going to give you complete idea on how
  14424. 9:15:09you can create a proper NodeJS project.
  14425. 9:15:12How you can work with the Node Express
  14426. 9:15:15Postgress with Docker. What is or what
  14427. 9:15:19usually a proper NodeJS project folder
  14428. 9:15:22structure looks like? What will be the
  14429. 9:15:24proper and best practices? How you can
  14430. 9:15:27do proper error handling? Uh form
  14431. 9:15:30validation. Each and everything we are
  14432. 9:15:32going to cover why I've taken this
  14433. 9:15:34particular project little basic to
  14434. 9:15:35intermediate level because this is going
  14435. 9:15:37to help you even though you know lot of
  14436. 9:15:39things in node still I can guarantee
  14437. 9:15:40this is going to give a complete
  14438. 9:15:42different idea that how you can create a
  14439. 9:15:44proper full node project. So first what
  14440. 9:15:47we will do right so you can see that I
  14441. 9:15:49have a package dojson now this is I
  14442. 9:15:50think we have done multiple times so
  14443. 9:15:52that is the reason I've already created
  14444. 9:15:53a package dojson with three dev
  14445. 9:15:55dependencies we have the node types tsx
  14446. 9:15:57and typescript that's it and in the
  14447. 9:16:00tsconig I've created a tsconfig dot uh
  14448. 9:16:03json file now here you'll see that one
  14449. 9:16:06thing that we are using source folder so
  14450. 9:16:09source will be where we are going to
  14451. 9:16:10write all our source code and also we
  14452. 9:16:13are going to have our main env file
  14453. 9:16:15where we going to add all our env
  14454. 9:16:17variables. Okay. But first let's
  14455. 9:16:19understand that what actually we are
  14456. 9:16:21going to build in this particular
  14457. 9:16:22project. Okay. So now consider like the
  14458. 9:16:25so what we'll do right. So let's say
  14459. 9:16:27this is our main capstone project or
  14460. 9:16:30whatever project you are building. You
  14461. 9:16:31are working in a freelance project or
  14462. 9:16:33maybe let's say you are working in a
  14463. 9:16:35company. At the end you have to
  14464. 9:16:36understand one thing is uh very
  14465. 9:16:38important that everything whatever
  14466. 9:16:40you're going to build is nothing but a
  14467. 9:16:42CRUD. Now this is I usually mention in
  14468. 9:16:44all my videos. What I mean by this CRUD
  14469. 9:16:46is nothing but when you're going to
  14470. 9:16:47create something, when you're going to
  14471. 9:16:49read something, when you're going to
  14472. 9:16:50update something and when you're going
  14473. 9:16:52to delete something. So how this
  14474. 9:16:54particular uh project looks like? So
  14475. 9:16:57here we are going to have main two uh
  14476. 9:16:59roles. Okay, one will be normal user and
  14477. 9:17:01one will be our super admin or admin.
  14478. 9:17:04And in this project we are going to have
  14479. 9:17:06only one admin. Why? When you work with
  14480. 9:17:08one admin and you'll create a project,
  14481. 9:17:10right? You will see that no matter how
  14482. 9:17:12complicated the project is, even though
  14483. 9:17:15let's say some project you're building,
  14484. 9:17:17you're going to have a separate role.
  14485. 9:17:19Let's say you're going to have a user,
  14486. 9:17:20you're going to have a admin, you're
  14487. 9:17:21going to have a support role, still it
  14488. 9:17:24will be much easier for you to do all of
  14489. 9:17:26these. So you have to make your basics
  14490. 9:17:27very strong. So that is the reason again
  14491. 9:17:30in my previous NodeJS uh project also
  14492. 9:17:32although that was little basic like we
  14493. 9:17:33have used MongoDB and all and also that
  14494. 9:17:36project we haven't used uh TypeScript
  14495. 9:17:38and postgress but still in that video
  14496. 9:17:40also that I uploaded maybe two years
  14497. 9:17:42back I also taken the same uh uh role
  14498. 9:17:45user and admin I'll see that that video
  14499. 9:17:47also got got pretty good response why
  14500. 9:17:50because we'll try to make things simpler
  14501. 9:17:52and then we'll try to implement this one
  14502. 9:17:54without using any AI tools. Okay. So
  14503. 9:17:57that is another good practice. If you
  14504. 9:17:59try to complete this particular project
  14505. 9:18:00without making use of any AI tools, it
  14506. 9:18:03will be much better. So you're going to
  14507. 9:18:04have a normal user and admin. User is
  14508. 9:18:07going to do what is user is going to
  14509. 9:18:09create support task. Okay. So I'm just
  14510. 9:18:11going to take as short as st support
  14511. 9:18:13task. So user will be able to create
  14512. 9:18:16support task. User will be able to see
  14513. 9:18:19all the support task that is created by
  14514. 9:18:22this user only. Means let's see if I
  14515. 9:18:24have a user A and user B and user A
  14516. 9:18:27created two two support task. User B
  14517. 9:18:29created five support task. User A will
  14518. 9:18:33be able to see only uh task created by
  14519. 9:18:36him. User B will be able to see the
  14520. 9:18:38support task created by user B. But what
  14521. 9:18:41about the admin? So what admin will be
  14522. 9:18:43able to see? Admin will be able to see
  14523. 9:18:45both user A and user B task. So admin
  14524. 9:18:48will be able to see seven task. So admin
  14525. 9:18:50has the capacity to view all the task.
  14526. 9:18:53So that will be one feature. Then so
  14527. 9:18:55user will be able to create task. User
  14528. 9:18:57will be able to see task. User will be
  14529. 9:18:59able to update their task. What I mean
  14530. 9:19:01by this user A only will be update his
  14531. 9:19:05task. User B only will be able to update
  14532. 9:19:07his task. Same for delete also. Okay. So
  14533. 9:19:11this what will happen when you create
  14534. 9:19:13this particular part. This is going to
  14535. 9:19:15give a proper clear understanding of how
  14536. 9:19:17this creation means card usually works.
  14537. 9:19:20Okay. But this is not that much simple.
  14538. 9:19:22Why? Because before this we will have a
  14539. 9:19:24authentication layer. So something like
  14540. 9:19:25this. Let's say you have your user, you
  14541. 9:19:28have your admin and here you are doing
  14542. 9:19:30some activity. But immediately you are
  14543. 9:19:31going to have authentications.
  14544. 9:19:33Authentication means until unless this
  14545. 9:19:35user is logged in they will not be able
  14546. 9:19:38to create anything. Right? So how we are
  14547. 9:19:39going to do the authentication? So here
  14548. 9:19:41what I decided right this is I'm going
  14549. 9:19:44to make little complex. So here we are
  14550. 9:19:45going to have both email and password
  14551. 9:19:47login. Okay. And at the same time we are
  14552. 9:19:51also going to have a Google login. Okay.
  14553. 9:19:53So both together. So if you want user
  14554. 9:19:55will be able to uh login using or
  14555. 9:19:58register using email and password at the
  14556. 9:20:00same time if they want they will be able
  14557. 9:20:01to login using Google. But if this
  14558. 9:20:04particular user is already logged in,
  14559. 9:20:06okay, using that particular email and
  14560. 9:20:08they try to login with the same Google
  14561. 9:20:10email. So in that case they will not be
  14562. 9:20:12able to login. Okay. So this is what we
  14563. 9:20:15are going to do. So this will be our
  14564. 9:20:16authentication. So this is going to give
  14565. 9:20:18you a proper understanding of how JWT
  14566. 9:20:20works. How we are going to implement
  14567. 9:20:22middleware, right? Middleware means
  14568. 9:20:24let's say user A try to see user B task,
  14569. 9:20:28it will not happen, right? So in that
  14570. 9:20:29case we have to have some protection. So
  14571. 9:20:31we're going to have some kind of
  14572. 9:20:32middleware. So that that medial where is
  14573. 9:20:34going to check whether that particular
  14574. 9:20:36user has that permission or not. If that
  14575. 9:20:38user don't have the permission so it
  14576. 9:20:40will not give that user to do that
  14577. 9:20:42particular task. So for example let's
  14578. 9:20:44say admin admin will be able to see all
  14579. 9:20:46his task. So in that case we have to see
  14580. 9:20:49whether that particular user is actually
  14581. 9:20:51admin user or not. If that user is admin
  14582. 9:20:54user so in that case only we are going
  14583. 9:20:55to give that user permission or else we
  14584. 9:20:57will simply say that okay you are not
  14585. 9:20:58admin you will not be able to do this
  14586. 9:21:00particular task. Okay. So these things
  14587. 9:21:03we are going to build. Third, what
  14588. 9:21:04you're going to do do right for admin.
  14589. 9:21:06Admin will be able to do a file upload
  14590. 9:21:08feature. So we are going to have a file
  14591. 9:21:10upload feature. Okay. This file upload
  14592. 9:21:12feature what we going to do? Admin will
  14593. 9:21:14be able to upload some banners. Okay. So
  14594. 9:21:16we are going to have some banners
  14595. 9:21:17feature. We are going to use malter and
  14596. 9:21:20we are going to use cloudinary both
  14597. 9:21:22along with our posgress DB. Okay. So
  14598. 9:21:24admin will be able to upload assets.
  14599. 9:21:27Okay. that assets we are going to store
  14600. 9:21:30in our cloudinary as well as that asset
  14601. 9:21:33URL means the image URL we are going to
  14602. 9:21:35store in our posgress DB okay so this is
  14603. 9:21:38what we are going to do and then at the
  14604. 9:21:40end we are going to have our background
  14605. 9:21:42job feature but remember before we going
  14606. 9:21:44to do background job functionality we
  14607. 9:21:46need to also have a radius uh section so
  14608. 9:21:48that video also have I have in my
  14609. 9:21:51channel I'm pretty much sure I think
  14610. 9:21:52most of you haven't watched that video
  14611. 9:21:54because that video didn't get proper
  14612. 9:21:55response what I'm going to do again
  14613. 9:21:57these This will be mandatory section. So
  14614. 9:21:59this video also I'm going to add and
  14615. 9:22:01then we are going to do the background
  14616. 9:22:03job feature. So this is this will be a
  14617. 9:22:04big one. Okay. Because there are a lot
  14618. 9:22:07of things you are going to understand in
  14619. 9:22:08this particular project. It's going to
  14620. 9:22:09give you overall idea how you can create
  14621. 9:22:12a proper NodeJS project. So with this
  14622. 9:22:14now let's get started. The first thing
  14623. 9:22:15what we're going to do right. So first
  14624. 9:22:17we need to install some of the packages.
  14625. 9:22:19Okay. So first what we are going to
  14626. 9:22:21install. So first let's do one thing. Uh
  14627. 9:22:24if I go to package.json JSON right you
  14628. 9:22:26will see that we have typescript tsx and
  14629. 9:22:29types/ node correct and now what are we
  14630. 9:22:33going to do right in the source we are
  14631. 9:22:35going to have a entry file now as I
  14632. 9:22:37already mentioned what actually entry
  14633. 9:22:39file is this entry file or the server.ts
  14634. 9:22:42TS simp simply will be our first entry
  14635. 9:22:45file where it will simply check that
  14636. 9:22:47okay whatever code we have written each
  14637. 9:22:49and everything should be combined and we
  14638. 9:22:52are going to add all those logic in the
  14639. 9:22:53server.ts file. So what I mean by this
  14640. 9:22:55let's say in this particular project we
  14641. 9:22:57[snorts] are going to have 10 API
  14642. 9:22:59endpoints. So now when we are going to
  14643. 9:23:01build this particular project right so
  14644. 9:23:04this when this code will run right so
  14645. 9:23:06code needs to understand right what is
  14646. 9:23:08our first file that I need to see. So in
  14647. 9:23:10this server.ts ts file. This would be an
  14648. 9:23:12entry file. So whenever code we are
  14649. 9:23:14going to run, it will check this file.
  14650. 9:23:16Okay, then it will say now all these 10
  14651. 9:23:18routes we have added here. So then it it
  14652. 9:23:20can go to that each and every file and
  14653. 9:23:22it it can see that whatever logic we
  14654. 9:23:24have written and based on that it's
  14655. 9:23:26going to do that. So what I simply mean
  14656. 9:23:28by this that this is a entry file which
  14657. 9:23:30is mandatory. This is where we are going
  14658. 9:23:32to run our main code or this is where we
  14659. 9:23:34are going to run our express server.
  14660. 9:23:36Okay. But before that let's not go into
  14661. 9:23:39too much uh uh what is that too much
  14662. 9:23:43concept but let's see what actually
  14663. 9:23:44express is do what is express js okay
  14664. 9:23:48because anyways we are going to
  14665. 9:23:49understand all of this practically so
  14666. 9:23:50express is nothing but it's a framework
  14667. 9:23:53whatever you have learned so far you see
  14668. 9:23:54that in the beginning section you have
  14669. 9:23:56understood all the code uh nodejs
  14670. 9:23:58concept and also the very important http
  14671. 9:24:00section right so what express is going
  14672. 9:24:03to give you right so this is a simple in
  14673. 9:24:05simple way this is going to give you
  14674. 9:24:06some of the features
  14675. 9:24:08If you don't use express those things
  14676. 9:24:10you have to manually write and you have
  14677. 9:24:12to implement again without express also
  14678. 9:24:14you'll be able to do this but this is
  14679. 9:24:16actually going to give you some of your
  14680. 9:24:18see if you uh notice right it's a
  14681. 9:24:20lightweight wrapper on top of NodeJS
  14682. 9:24:22build networking features as simple as
  14683. 9:24:24that means it is using NodeJS in the
  14684. 9:24:26background it's using NodeJS features
  14685. 9:24:28only but this is going to help you or
  14686. 9:24:30it's going to give you it's going to
  14687. 9:24:32give you some of the out ofthe-box
  14688. 9:24:34features that is going to simplify the
  14689. 9:24:36code that's needed to build web server,
  14690. 9:24:38web application and restful APIs. As
  14691. 9:24:40simple as that. So in in definition wise
  14692. 9:24:44express will be a minimal opensource
  14693. 9:24:46framework that built on top of NodeJS
  14694. 9:24:49networking or NodeJS built-in features
  14695. 9:24:51and that is going to give you some out
  14696. 9:24:53ofthe-box features that you need to
  14697. 9:24:56build web servers web application and
  14698. 9:24:58restful API means it's going to give you
  14699. 9:25:00some extra advantage that if you don't
  14700. 9:25:02use it you have to write manually all
  14701. 9:25:04those things but again if you don't use
  14702. 9:25:06this one still you will be able to
  14703. 9:25:08implement those things also. So this is
  14704. 9:25:10what express is. So first what we are
  14705. 9:25:12going to do right. So first we'll do npm
  14706. 9:25:14install express. Okay. So I'm going to
  14707. 9:25:17install express first. So that will be
  14708. 9:25:18our first. So I'll do npm install
  14709. 9:25:20express. So let's install this. If you
  14710. 9:25:23go to package dojson, you can see that
  14711. 9:25:25we have added the express. And also
  14712. 9:25:26let's install the uh types also. So I'll
  14713. 9:25:29do npm install d. And here I'm simply
  14714. 9:25:32going to do add the types
  14715. 9:25:35express.
  14716. 9:25:38Okay. So types also we need because we
  14717. 9:25:41are using typescript. So this is the
  14718. 9:25:42first thing. Second what actually env is
  14719. 9:25:47I think in the core module we have
  14720. 9:25:49already discussed about the env
  14721. 9:25:50variables right. So env is again it's a
  14722. 9:25:52package right? I think we have already
  14723. 9:25:54discussed this one. So it's a package
  14724. 9:25:55that will help us to read our env
  14725. 9:25:57variables because when we are going to
  14726. 9:25:59have all these variables that we need
  14727. 9:26:02for this particular project we can't
  14728. 9:26:04directly hardcode all those things in
  14729. 9:26:06our code. So that is the reason we need
  14730. 9:26:07to keep all of this in a separate env
  14731. 9:26:09file. Second, when we are going to use
  14732. 9:26:11some sensitive information, let's say
  14733. 9:26:12you're using some password or you are
  14734. 9:26:14using some Google uh credentials secrets
  14735. 9:26:18so that you just can't expose in your
  14736. 9:26:20code. Consider this particular project
  14737. 9:26:21is a public source project means you are
  14738. 9:26:24going to post this one to your GitHub
  14739. 9:26:26anyone can see. Now let's say you added
  14740. 9:26:28your Google secret directly in your
  14741. 9:26:29code. Anyone can copy and can misuse,
  14742. 9:26:31right? So that is the reason we need a
  14743. 9:26:34separate file where we are going to add
  14744. 9:26:35all of these. So this is where we need
  14745. 9:26:38file. Okay.
  14746. 9:26:40So now here we are going to do npm
  14747. 9:26:42install env.
  14748. 9:26:46That's it. And then also we need
  14749. 9:26:49something called course. Okay. So this
  14750. 9:26:50we are going to discuss later. For now I
  14751. 9:26:52think this is fine. So first we have
  14752. 9:26:54installed uh express types node types
  14753. 9:26:59tsx that is that is going to help us to
  14754. 9:27:02run our typescript project and then we
  14755. 9:27:04have typescript env and express these
  14756. 9:27:08two we have installed as a dependency
  14757. 9:27:10and these all are dev dependency we have
  14758. 9:27:12already know the difference between
  14759. 9:27:13dependencies and the dev dependencies
  14760. 9:27:15okay so now first of all what we're
  14761. 9:27:17going to do right so first we need to go
  14762. 9:27:19to our source so let's create our basic
  14763. 9:27:22folder structure now these This folder
  14764. 9:27:23structure whatever we are going to
  14765. 9:27:24create will be extremely important. Why
  14766. 9:27:26this is extremely important? Because you
  14767. 9:27:28need to know that how why we are
  14768. 9:27:30creating each and every folder and what
  14769. 9:27:31is the reason or what is the
  14770. 9:27:33responsibility behind creating each and
  14771. 9:27:35every folder. So first we are going to
  14772. 9:27:36create a config folder. So this config
  14773. 9:27:39is going to hold all our config related
  14774. 9:27:40information. Then I'm going to create a
  14775. 9:27:42leave folder. So leave folder is going
  14776. 9:27:45to handle all our utils related. So
  14777. 9:27:48let's we are creating some helpers.
  14778. 9:27:50Okay. Config folder is going to do what?
  14779. 9:27:52configuration related things. Okay, for
  14780. 9:27:53example, let's say we are going to
  14781. 9:27:54manage our env here. Then we are going
  14782. 9:27:57to have our middleware. Now middleware
  14783. 9:27:59concept can be little tricky. We are
  14784. 9:28:01going to understand what actually middle
  14785. 9:28:02words are. But for now, let's create one
  14786. 9:28:04middleware folder and then we are going
  14787. 9:28:07to create a routes folder. This is where
  14788. 9:28:08we are going to create all our routes.
  14789. 9:28:11So for now these fold are fine. But
  14790. 9:28:12before we go forward, right, let's
  14791. 9:28:14understand what middleware is because
  14792. 9:28:16you don't have to know coding for this.
  14793. 9:28:18Let's try to simplify this one. Consider
  14794. 9:28:20like this that you have your user here.
  14795. 9:28:23Okay. And then you are doing some task
  14796. 9:28:27but this task you have to do it only if
  14797. 9:28:31this user is a logged in user. Loged in
  14798. 9:28:34user means if until the user is actually
  14799. 9:28:38logged in. We don't know that actually
  14800. 9:28:40this user is a user or this user is a
  14801. 9:28:42scammer. You don't know right? So that
  14802. 9:28:44means you need something to understand
  14803. 9:28:46this one. So this is where we have
  14804. 9:28:48something called there is a barrier
  14805. 9:28:50right so it's like a middleware so this
  14806. 9:28:52middleware is going to check that if
  14807. 9:28:54this user has certain permission or not
  14808. 9:28:56if this user don't have permission so
  14809. 9:28:58first of all it's it's going to check it
  14810. 9:29:00has a permission if it has permission
  14811. 9:29:02it's going to give you so it's going to
  14812. 9:29:03give you okay you can go and do this if
  14813. 9:29:06it don't have permission it's going to
  14814. 9:29:07block the request here so this is the
  14815. 9:29:09task of a middleware middleware is like
  14816. 9:29:11a middle person it's going to do
  14817. 9:29:13something now it's don't have to uh
  14818. 9:29:15about always this user and permissions
  14819. 9:29:17it can be anything right but the
  14820. 9:29:19important part is that in the node
  14821. 9:29:21middleware you have to understand that
  14822. 9:29:22we have something called next concept
  14823. 9:29:25now you need to know what actually this
  14824. 9:29:27next is until unless we're going to
  14825. 9:29:29implement in the code but you need to
  14826. 9:29:30understand in a very simple way
  14827. 9:29:32middleware is like a middle person
  14828. 9:29:34that's going to do some task or it's
  14829. 9:29:36going to check something and based on
  14830. 9:29:38that it's going to move on to the next
  14831. 9:29:40task. So in this case the next task is
  14832. 9:29:42this one or else it's going to move on
  14833. 9:29:44to the next route handler. So either it
  14834. 9:29:46will move on to the next task or it will
  14835. 9:29:50move to the next route handler and this
  14836. 9:29:52you'll understand once we'll implement
  14837. 9:29:54the code. Right? So for now first let's
  14838. 9:29:56do one thing. Let's go here and then
  14839. 9:29:59first let's go to env. Okay. And in the
  14840. 9:30:01env what I'm going to do so first I'm
  14841. 9:30:03going to take that okay this is my env
  14842. 9:30:06file where we're going to have all our
  14843. 9:30:08variables. So first of all we need to
  14844. 9:30:09understand where we want to run this
  14845. 9:30:11particular code. So now let's say I
  14846. 9:30:13wanted to run this on port and I'm just
  14847. 9:30:15going to give I'll give some random okay
  14848. 9:30:17because I already have maybe some other
  14849. 9:30:19port that are already occupied. So I'm
  14850. 9:30:22just going to give this one as 5010. Now
  14851. 9:30:24this is the port I wanted to run this
  14852. 9:30:26particular project. As simple as that.
  14853. 9:30:28You can use any port you want. For
  14854. 9:30:30example, you can use 3,000, you can use
  14855. 9:30:3230,0001, you can use 4,000. Okay? Or
  14856. 9:30:34maybe let's use 4,000 here. So I'm going
  14857. 9:30:36to use port 4,000 for this. Okay? And
  14858. 9:30:40then I'm just going to use here node
  14859. 9:30:42env. And in this case we are using
  14860. 9:30:44development because we are doing
  14861. 9:30:46development now. So we going to have two
  14862. 9:30:47env now. Okay. So now what we going to
  14863. 9:30:50do right we will go to our config and
  14864. 9:30:53this is where we are going to create our
  14865. 9:30:54base structure first. So we'll create
  14866. 9:30:56env.ts and here what we going to do
  14867. 9:30:59right? So first of all we need to load
  14868. 9:31:01all our env variables right because
  14869. 9:31:03until unless we're going to load the env
  14870. 9:31:04variables we'll not be able to read it.
  14871. 9:31:06So this is where we need the env
  14872. 9:31:08package. So I'm just going to do env
  14873. 9:31:10from
  14874. 9:31:12env. And then how we are going to load
  14875. 9:31:14it. Okay, I'll minimize this one. So to
  14876. 9:31:16load this one, you're going to use
  14877. 9:31:17something called env.config. Now this
  14878. 9:31:20config is going to do what? It's going
  14879. 9:31:21to load your env file containing into
  14880. 9:31:23process. You have discussed remember
  14881. 9:31:25process.default uh process. Default
  14882. 9:31:28process. Env by default. Okay. If key is
  14883. 9:31:32present, it simply attempts to load
  14884. 9:31:33encrypted. Okay. Fine. First first you
  14885. 9:31:35understand this one. loadb file contents
  14886. 9:31:38whatever you have inside this it's going
  14887. 9:31:40to load into process by default okay now
  14888. 9:31:44this is very important because you are
  14889. 9:31:46going to use this one a lot and then I'm
  14890. 9:31:48just going to use export con and I'll do
  14891. 9:31:51env
  14892. 9:31:52here I'm just going to add all these
  14893. 9:31:54whatever I have so first I have a port
  14894. 9:31:56correct so the port will be so I'm going
  14895. 9:31:58to take a number and here I'm just going
  14896. 9:32:00to take process env dot
  14897. 9:32:04so port here so I'll do
  14898. 9:32:07and always give this uh value here.
  14899. 9:32:10Okay, default. That's it. Then I'm
  14900. 9:32:12simply going to check uh that if this is
  14901. 9:32:15the products and environment or not. So
  14902. 9:32:17I'll do is production send here. I'm
  14903. 9:32:19just going to do process.v
  14904. 9:32:21dot. So we have the node env.
  14905. 9:32:26I'm just going to keep this one as
  14906. 9:32:28development by default. So when this
  14907. 9:32:30will be is production when this node
  14908. 9:32:31enval to production that time it will be
  14909. 9:32:35production if this is not that means we
  14910. 9:32:37are doing a development okay so that's
  14911. 9:32:40it I think these two and also let's do
  14912. 9:32:41node envo
  14913. 9:32:43so here also I'm just going to do
  14914. 9:32:45process env
  14915. 9:32:48or else this will be development and
  14916. 9:32:50then I'm just going to keep this one as
  14917. 9:32:51as const that's it so this will be my
  14918. 9:32:54env file you will understand one concept
  14919. 9:32:58remember I already mentioned the idea of
  14920. 9:33:00this project is to give you the proper
  14921. 9:33:02understanding of how you can create
  14922. 9:33:03NodeJS project. Right? Now let's say you
  14923. 9:33:04added two more ENV. You'll come here and
  14924. 9:33:08you'll add two more ENV in this file.
  14925. 9:33:10Now what will happen like when you're
  14926. 9:33:11going to do env dot you're going to get
  14927. 9:33:13this env. Environ
  14928. 9:33:17is production and you can directly use
  14929. 9:33:18this one like this. Okay. So this is the
  14930. 9:33:20idea of this particular file. So this is
  14931. 9:33:23first one. Now what we going to do
  14932. 9:33:25right? So first when we need express so
  14933. 9:33:27express is going to help us to create
  14934. 9:33:29our web server. So that means we need to
  14935. 9:33:31run our server correct. So that means
  14936. 9:33:33first what we going to do right? So
  14937. 9:33:34let's go to uh this app.ts file. I'm
  14938. 9:33:38going to create entry file. So this will
  14939. 9:33:40be my entry file for express. So means
  14940. 9:33:44all express related logic I'm going to
  14941. 9:33:46add here
  14942. 9:33:48and this express related or this app.ts
  14943. 9:33:51I'm going to add in this server.ts. So
  14944. 9:33:53this is our main root file. Okay. And
  14945. 9:33:56this will be entry file for express. So
  14946. 9:33:58first of all what we need to do here. So
  14947. 9:34:00first I'm going to do export function
  14948. 9:34:04and I just do create app or create
  14949. 9:34:07application whatever you're going to do
  14950. 9:34:10and then first of all I need to do I
  14951. 9:34:12need to create my express application
  14952. 9:34:14right because how we are going to use
  14953. 9:34:16express features until unless you're
  14954. 9:34:18going to create the express instance.
  14955. 9:34:20Correct? So in this case we need to
  14956. 9:34:21first import express from express and
  14957. 9:34:26then we are going to simply do express
  14958. 9:34:28that's it. So what this is going to do
  14959. 9:34:30create an express application the
  14960. 9:34:32express function is a top level function
  14961. 9:34:34exported by the express module. So this
  14962. 9:34:36is the root and now if you do app dot
  14963. 9:34:39and this is where you're going to get
  14964. 9:34:41all of these each and every feature
  14965. 9:34:43you're getting. So this is where this
  14966. 9:34:46app is now holding each and every
  14967. 9:34:48feature provided by express. So now we
  14968. 9:34:50are keeping this one in a variable and
  14969. 9:34:52now what we going to do right we are
  14970. 9:34:54simply going to do app dot use and I'm
  14971. 9:34:56simply going to do express dot JSON okay
  14972. 9:35:00this is actually middleware again
  14973. 9:35:03middleware we are going to understand
  14974. 9:35:04once we create little uh more logic or
  14975. 9:35:08once we'll go a little further because
  14976. 9:35:09you need to understand that how this
  14977. 9:35:11middleware work and it will be much
  14978. 9:35:13easier to explain this one practically
  14979. 9:35:14for now you understand this is actually
  14980. 9:35:16a middleware that parses JSON And it's
  14981. 9:35:20only look at the request where the
  14982. 9:35:21content type header matches the type
  14983. 9:35:23option. You'll understand what we are
  14984. 9:35:24doing in this particular case. I'm going
  14985. 9:35:26to explain and then I'm going to simply
  14986. 9:35:29do now let's say here what I can do
  14987. 9:35:32right in this particular file. Now what
  14988. 9:35:34I mention that when you are going to
  14989. 9:35:36have a root file when this code will
  14990. 9:35:38compile this particular file will be
  14991. 9:35:40read at first. So it's going to check
  14992. 9:35:42each and everything. Now let's say you
  14993. 9:35:44are using this create app method here.
  14994. 9:35:47So then it will go to this particular
  14995. 9:35:49file and this is where you have to add
  14996. 9:35:51all your routes, all your middleware and
  14997. 9:35:53everything. What I need to do right
  14998. 9:35:55first of all I need to have some feature
  14999. 9:35:58in this particular uh project so that
  15000. 9:36:00whenever there is a any error happens or
  15001. 9:36:03let's say something crashes
  15002. 9:36:05this particular application should
  15003. 9:36:06automatically pick that error and it
  15004. 9:36:08will notify the user. Okay, there is a
  15005. 9:36:10problem you try after something. So that
  15006. 9:36:12means we need to have a global error
  15007. 9:36:14handler. You're getting global error
  15008. 9:36:17handler. we need to have. Okay. What
  15009. 9:36:19this error handler is going to do? This
  15010. 9:36:21error handler is simply going to give
  15011. 9:36:23you one information. Okay. Something
  15012. 9:36:24happened. There is internal server
  15013. 9:36:26error. There is a network error.
  15014. 9:36:28Whatever. Not network but internal
  15015. 9:36:29server error happened. So that
  15016. 9:36:31middleware we need to create. Every time
  15017. 9:36:33that uh something breaks so that
  15018. 9:36:35middleware will get executed and then
  15019. 9:36:37that user will be notified. Okay. But
  15020. 9:36:40for this before that also we are going
  15021. 9:36:42to use one more package that is called
  15022. 9:36:45Pino. Okay. So let's see what actually
  15023. 9:36:46Pino is. So what is Pino?
  15024. 9:36:50So it's a logger. Okay, you can see that
  15025. 9:36:52it's a highly popular opensource logging
  15026. 9:36:55utility means here whenever we want to
  15027. 9:36:58notify the user. Okay, let's or maybe
  15028. 9:37:00let's consider from a developer point of
  15029. 9:37:02view. When you're developing, let's say
  15030. 9:37:04you're running the code, you want to
  15031. 9:37:05check your logger. This is going to
  15032. 9:37:07generate the logs in a structured JSON
  15033. 9:37:09format. Okay. Now this is also very
  15034. 9:37:12fast. You see that this is also very
  15035. 9:37:13fast. So we going to use this pino
  15036. 9:37:16package. Okay. So here we will use this
  15037. 9:37:18pen and then I'm just going to show you
  15038. 9:37:20I think it's called pino pretty. Okay.
  15039. 9:37:23So let's see this one pen pretty these
  15040. 9:37:25two.
  15041. 9:37:27Okay. So we can directly so this is
  15042. 9:37:29actually uh how we are going to do this.
  15043. 9:37:32So you can see that we have a logger. We
  15044. 9:37:34have to set a label and then you can use
  15045. 9:37:36some transport here. Okay. So colorize
  15046. 9:37:38means it's going to enable your color
  15047. 9:37:40tags. Uh this is the trans. So human
  15048. 9:37:43readable time stamp. So it's going to
  15049. 9:37:44create human readable time stamp and
  15050. 9:37:46also it's going to ignore if you want to
  15051. 9:37:49ignore something. But for now these two
  15052. 9:37:50I think more than enough. And also I
  15053. 9:37:53think we also need to take the target
  15054. 9:37:55also. I think target is also I think
  15055. 9:37:57optional. So what we will do right label
  15056. 9:37:59we need to take and transport we are
  15057. 9:38:02going to check if this is production we
  15058. 9:38:04are going to do this. uh
  15059. 9:38:07no sorry if this is not production we
  15060. 9:38:09are going to do this or else we're going
  15061. 9:38:10to do undefined in production fall back
  15062. 9:38:12to raw altra first JSON string okay so
  15063. 9:38:15this is what we are going to build uh
  15064. 9:38:17now remember one thing I wanted to
  15065. 9:38:19mention see whenever I'll create these
  15066. 9:38:20videos right so you can see that I'm
  15067. 9:38:22doing this on real time means I'm
  15068. 9:38:23actually I don't have all of this
  15069. 9:38:25created so I'm just checking and writing
  15070. 9:38:27what I mean by this whenever I'm going
  15071. 9:38:29to do let's say there is some mistake or
  15072. 9:38:30some slowness in the explanation it's
  15073. 9:38:34your responsibility You have to do your
  15074. 9:38:36own research and you have to learn.
  15075. 9:38:37You're getting what I'm talking about.
  15076. 9:38:38For example, let's say there are
  15077. 9:38:39something I'm not explaining. You have
  15078. 9:38:42to go or in this case you can make use
  15079. 9:38:43of some AI tools also that will be much
  15080. 9:38:46easier. So in this case what you're
  15081. 9:38:47going to do right. So first let's do npm
  15082. 9:38:49install. So we'll go here and we'll do
  15083. 9:38:52npm install pino and then we have pino
  15084. 9:38:55pretty.
  15085. 9:38:59That's it. Now where we going to add
  15086. 9:39:02this pino file? This you are going to
  15087. 9:39:04add in your leave file. Okay. So this
  15088. 9:39:07will be a logger.ts. So first what I'm
  15089. 9:39:09going to do I'll do import
  15090. 9:39:12pino from
  15091. 9:39:15pino and then also I'm just going to I
  15092. 9:39:18think let's do export
  15093. 9:39:20con logger and here I'm simply going to
  15094. 9:39:23take pino.
  15095. 9:39:25So first I'm going to take the label. So
  15096. 9:39:27label will be where uh okay also one
  15097. 9:39:30very important point let's do one thing.
  15098. 9:39:32Let's go to env see what we are doing
  15099. 9:39:34each and everything you have to
  15100. 9:39:35understand. So instead of adding
  15101. 9:39:36directly hard coding there I'm just
  15102. 9:39:38going to do log level here and I'm just
  15103. 9:39:40going to keep by default info. Okay. So
  15104. 9:39:42log level will be info. So now if I'm
  15105. 9:39:44adding a new one so I need to go to env.
  15106. 9:39:46And then I'm just going to add a log
  15107. 9:39:48level and this will be process env dot.
  15108. 9:39:53So log level or else this will be info.
  15109. 9:39:58Okay. So now we'll go here and then I'm
  15110. 9:40:01just going to use env that I need to
  15111. 9:40:03import and I can use this log label here
  15112. 9:40:06you're getting. So this is how you're
  15113. 9:40:07going to do this. So once this is done
  15114. 9:40:09now we are going to also add a
  15115. 9:40:11transport. So here I'm going to add a
  15116. 9:40:12transport. So I'm going to check if this
  15117. 9:40:14EMV do is production. So in that case
  15118. 9:40:17I'm going to keep undefined. And for
  15119. 9:40:19Dave I'm just going to keep a target.
  15120. 9:40:22And here we are simply going to take
  15121. 9:40:24let's take this one target as we know
  15122. 9:40:27pretty
  15123. 9:40:30also we're going to take options. And
  15124. 9:40:32here I'm just going to take colorize.
  15125. 9:40:35Let me just copy from here.
  15126. 9:40:39Colorize will be true.
  15127. 9:40:43And also we need this one. Translate
  15128. 9:40:45time we're going to keep so that we'll
  15129. 9:40:47get human readable time stamp.
  15130. 9:40:52I think this is done. Why getting error?
  15131. 9:40:54Oh, we have to do this. Okay. So I think
  15132. 9:40:57this is done. So we taking a label which
  15133. 9:40:59will be log label. By default it will be
  15134. 9:41:00info because that is what we have added
  15135. 9:41:02here. Transport target we targeting pen
  15136. 9:41:06pretty options color is true and
  15137. 9:41:08translate time sard.
  15138. 9:41:10Okay. So this is what I need. So now
  15139. 9:41:12what we can do right we can use this. So
  15140. 9:41:15maybe let's do one thing. We need to use
  15141. 9:41:17this one in all the places. First let's
  15142. 9:41:20go to middleware and then I'm going to
  15143. 9:41:21create a global error handler. Okay. So
  15144. 9:41:23I'm just going to do error handler
  15145. 9:41:27TS file. And now this I already
  15146. 9:41:30mentioned this is going to give you a
  15147. 9:41:31general idea whenever there is something
  15148. 9:41:33goes wrong we are going to trigger this
  15149. 9:41:35middleware. And this we're going to use
  15150. 9:41:36in our app.ts file. So here I'm simply
  15151. 9:41:39going to do export function and I'll do
  15152. 9:41:41error handler. So first of all here I'm
  15153. 9:41:44going to take a error. This will be
  15154. 9:41:46error. Then I'm going to take a request
  15155. 9:41:50and this will be okay. This particular
  15156. 9:41:53request is the request object from
  15157. 9:41:55express. Okay. So this will be now you
  15158. 9:41:59consider this one as you remember in the
  15159. 9:42:00HTTP model we have discussed about the
  15160. 9:42:02request and response. You consider this
  15161. 9:42:05one similar to this. Okay. I think now
  15162. 9:42:07this will be easier to understand and
  15163. 9:42:09this is the reason in the uh HTTP model
  15164. 9:42:11we have explained all of this in details
  15165. 9:42:13that if you don't use any third party
  15166. 9:42:15framework how this will look. So this is
  15167. 9:42:17consider this one as that only. So this
  15168. 9:42:19request object this is a response
  15169. 9:42:21object. So here I'm going to take this
  15170. 9:42:23one as request then I'm going to take a
  15171. 9:42:25response because I need to return the
  15172. 9:42:26response. And now you have this next
  15173. 9:42:30function here. Okay. Now this next
  15174. 9:42:32function is extremely important concept.
  15175. 9:42:34This we are going to understand little
  15176. 9:42:36later. So for now let's take this one as
  15177. 9:42:38next function from your express. Okay.
  15178. 9:42:40Okay. Let me explain what this next
  15179. 9:42:41function is. So what happen right when
  15180. 9:42:43you are doing let's say you have a
  15181. 9:42:44middleware. Okay. And you are doing one
  15182. 9:42:47user is doing a task and this task is
  15183. 9:42:49here. So this is task one. Now you have
  15184. 9:42:54or maybe let's uh let's take this on
  15185. 9:42:56task one. Let's take this on task two.
  15186. 9:42:59So this is task two. This is task one.
  15187. 9:43:02And then in middle you have a
  15188. 9:43:04middleware. What this middleware is
  15189. 9:43:06going to do? Let's say user already
  15190. 9:43:08completed the task one. So this task is
  15191. 9:43:10completed. Before it will go to T2 which
  15192. 9:43:14is task two. You have a middleware in
  15193. 9:43:16middle. So this middleware is going to
  15194. 9:43:18check something. I already mentioned it.
  15195. 9:43:20It's going to do something. If this
  15196. 9:43:22condition satisfies means this condition
  15197. 9:43:24whatever you given if this satisfies
  15198. 9:43:26it's going to move to next one. Right?
  15199. 9:43:28But how it's going to move? This is
  15200. 9:43:30where this next is actually important.
  15201. 9:43:32So when you're going to use middleware
  15202. 9:43:34we are going to call this next function.
  15203. 9:43:37So next means now we are simply telling
  15204. 9:43:38middleware oh your task is completed now
  15205. 9:43:40you have to go to next task. So that is
  15206. 9:43:43the reason once will move to sorry once
  15207. 9:43:45you call the next it will go to task
  15208. 9:43:47two. What will happen if you don't use
  15209. 9:43:49this next function. Consider user
  15210. 9:43:51completed task one. Task one now called
  15211. 9:43:54middleware. Middleware already check
  15212. 9:43:57condition satisfies. Now it should go to
  15213. 9:43:59task two. But you haven't called the
  15214. 9:44:00next function. What will happen? it will
  15215. 9:44:02be stuck here forever. It will never go
  15216. 9:44:04to the next part. You're getting so this
  15217. 9:44:06is where this next function is very
  15218. 9:44:07important. So whenever you're going to
  15219. 9:44:09do some functionality or using
  15220. 9:44:11middleware you need to call the next
  15221. 9:44:13function only if that needed only if
  15222. 9:44:15that functionality is needed. Okay. So
  15223. 9:44:18now this is our next function. What we
  15224. 9:44:20going to do right? Okay let's take this
  15225. 9:44:22one. We are going to return void. We're
  15226. 9:44:24not doing anything here. And then I'm
  15227. 9:44:25simply going to take my logger from the
  15228. 9:44:27logger that I have created. And here
  15229. 9:44:29this logger is going to give you some
  15230. 9:44:31label. For example, you can use logger
  15231. 9:44:32dot error logger. fatal logger.info. Now
  15232. 9:44:36because we creating error handler. So
  15233. 9:44:38what will happen? We need to do logger
  15234. 9:44:39dot error. Correct? And this is where
  15235. 9:44:41you're going to pass the error. This
  15236. 9:44:43error. Okay. Whatever error will happen.
  15237. 9:44:45And then I'm just going to give a
  15238. 9:44:47general message that unhandled error.
  15239. 9:44:52Okay. Now what we have to do right, we
  15240. 9:44:54need to return a response back to user.
  15241. 9:44:57Correct. See whenever you are going to
  15242. 9:44:58create an application you're going to
  15243. 9:45:00have your back end you're going to have
  15244. 9:45:01your UI correct. So when your back end
  15245. 9:45:04will do ter some task it's going to
  15246. 9:45:06return the response back. This is where
  15247. 9:45:08this response object is helpful. So now
  15248. 9:45:11this is where what we can do right you
  15249. 9:45:12can do race dot status. Now all these
  15250. 9:45:14things now you're seeing this is
  15251. 9:45:16actually provided some of the things
  15252. 9:45:18provided by express out of the box. So
  15253. 9:45:20this is why we need some framework to
  15254. 9:45:22make make our life much easier. You can
  15255. 9:45:24do the exact same thing using node HTTP
  15256. 9:45:27package also. I think we have already
  15257. 9:45:28discussed this on previously. So here
  15258. 9:45:30I'm going to return a 500. And now I'm
  15259. 9:45:32going to return a JSON response back.
  15260. 9:45:34Okay, this JSON response is going to
  15261. 9:45:36return a success. This will be false
  15262. 9:45:39because something goes wrong. And then
  15263. 9:45:40I'm also going to return a message and
  15264. 9:45:42this will tell that okay there is a
  15265. 9:45:44internal
  15266. 9:45:46server error. Okay. So this will be our
  15267. 9:45:49global error handler. But how we are
  15268. 9:45:51going to use this? So to use this one we
  15269. 9:45:53have to tell express that okay you are
  15270. 9:45:55going to use this error handler. So to
  15271. 9:45:57do this one we are going to do
  15272. 9:46:01uh app dot and you have something called
  15273. 9:46:03use which is very important right. So
  15274. 9:46:05this use is going to use something. So
  15275. 9:46:08what you're simply telling that okay
  15276. 9:46:09whenever something goes wrong you're
  15277. 9:46:10going to use this error handler. So it
  15278. 9:46:12will automatically check that and it's
  15279. 9:46:14going to give you the error. It's going
  15280. 9:46:15to throw error. So this is the first
  15281. 9:46:17thing. Now next we need to create one
  15282. 9:46:20more middleware. what actually that med
  15283. 9:46:22and that is not found. Consider you are
  15284. 9:46:25creating some APIs right user is hitting
  15285. 9:46:27this login endpoint correct now let's
  15286. 9:46:30say user it's possible right user can
  15287. 9:46:32give some random value for example let's
  15288. 9:46:34see if I go here and I give some random
  15289. 9:46:36value here whatever you can do here it
  15290. 9:46:39will not work okay here they have added
  15291. 9:46:41the logic that whatever you'll put it
  15292. 9:46:43will always go to here but normally in
  15293. 9:46:44your application let's say this route is
  15294. 9:46:46not even present so what we have to do
  15295. 9:46:48right you have to tell user okay this is
  15296. 9:46:50not found this route that you are trying
  15297. 9:46:53to access is not found you're getting.
  15298. 9:46:55So this is a generic message that you
  15299. 9:46:58should always implement before even
  15300. 9:46:59starting your logic. So that is the
  15301. 9:47:01reason we need one more middleware and
  15302. 9:47:03this will be not found. DS and this is
  15303. 9:47:07very simple. Okay. So do export function
  15304. 9:47:09and I'll do not found and this is where
  15305. 9:47:12we also need the request and response.
  15306. 9:47:17So we're going to get request and
  15307. 9:47:18response. So I'll do not found. So I'll
  15308. 9:47:21have the request and this will be my
  15309. 9:47:24request. I'm going to have the response.
  15310. 9:47:26This will be my response. It's going to
  15311. 9:47:28return v. And what I'll do right, I'll
  15312. 9:47:30simply do raise dot status
  15313. 9:47:33404
  15314. 9:47:35dot json. And here also I'm simply going
  15315. 9:47:37to do success will be false and message
  15316. 9:47:41will be route not found.
  15317. 9:47:49So save this and now we'll go here
  15318. 9:47:50again. We'll do app dot use not found.
  15319. 9:47:54That's it. So it's going to use these
  15320. 9:47:56two middleware. Okay. Now this is see
  15321. 9:47:58whatever we are doing at extremely
  15322. 9:48:00important. These are the base concept
  15323. 9:48:01that you need to do. Now let's do one
  15324. 9:48:03thing. Let's go here and understand what
  15325. 9:48:06actually this is. So I'll do express URL
  15326. 9:48:08encoded.
  15327. 9:48:12Okay. So you can see that express
  15328. 9:48:15encoded is a built-in middleware. Again
  15329. 9:48:16this is also another middleware that
  15330. 9:48:18parses incoming request with URL encoded
  15331. 9:48:21payloads. Okay. Now how we are going to
  15332. 9:48:23understand this one? This we will
  15333. 9:48:25understand once you'll implement our
  15334. 9:48:26CRUD. Remember I already mentioned the
  15335. 9:48:28concept of CRUD create react because
  15336. 9:48:30that time when you're going to pass some
  15337. 9:48:32form data from your UI side. So in that
  15338. 9:48:34case this the browser encoded the data
  15339. 9:48:36into long query string format the middle
  15340. 9:48:39inter blah blah and then it's accessible
  15341. 9:48:41in your request. body. For now this will
  15342. 9:48:43be a little tricky to understand but we
  15343. 9:48:45need to use this one in our main entry
  15344. 9:48:47file. So for now what we are going to do
  15345. 9:48:49right we are simply going to use this.
  15346. 9:48:50So do app dot use express url encoded
  15347. 9:48:53and then we're going to pass extended
  15348. 9:48:54will be true. So this is going to
  15349. 9:48:56middleware that is going to pass your
  15350. 9:48:58incoming HTML form submission. So
  15351. 9:49:00whenever you're going to have some kind
  15352. 9:49:01of form you're going to pass some form
  15353. 9:49:03data. This is where you need this
  15354. 9:49:05particular middleware. Okay. So now
  15355. 9:49:07we'll go here and after this I'm simply
  15356. 9:49:10going to do app dot use and I'll do
  15357. 9:49:11express dot url encoded and you're going
  15358. 9:49:14to pass a extended will be true.
  15359. 9:49:18I hope this is clear. So with this this
  15360. 9:49:20part is completed. So in the next
  15361. 9:49:21section what you're going to understand
  15362. 9:49:22we understand what actually course is
  15363. 9:49:24and then we'll create our first route.
  15364. 9:49:26We are going to create our entry file.
  15365. 9:49:28This is not yet created. We are going to
  15366. 9:49:30run our express server and we'll test in
  15367. 9:49:32postman that if this is working or not.
  15368. 9:49:34So let's do this this one in the next
  15369. 9:49:36part. All right. So what actually course
  15370. 9:49:38is right? So course is nothing but cross
  15371. 9:49:40origin resource sharing. Now you
  15372. 9:49:41understand this one with practical way.
  15373. 9:49:43So consider you have a front end URL you
  15374. 9:49:45have a back end URL. Correct. Now what
  15375. 9:49:47usually happen right? So first of all
  15376. 9:49:49let's understand how you can do this. So
  15377. 9:49:51we're going to install this course
  15378. 9:49:52package and then you simply tell our
  15379. 9:49:54express. So app dot use means what app
  15380. 9:49:57is your express app. So you're going to
  15381. 9:49:58tell express okay you are going to use
  15382. 9:50:00quartz. That means you're going to allow
  15383. 9:50:02all cross origin request. But what will
  15384. 9:50:05happen? Let's say you have a certain
  15385. 9:50:06front end URL and here in this case you
  15386. 9:50:09can see this is actually very dangerous
  15387. 9:50:10right so you should restrict access to
  15388. 9:50:13your specific front- end domain only so
  15389. 9:50:15that means you have a front end domain
  15390. 9:50:17and you want that particular domain only
  15391. 9:50:20can call your backend URL or that
  15392. 9:50:23particular domain only can call your
  15393. 9:50:25backend endpoint or backend API so in
  15394. 9:50:27that case what you have to do right you
  15395. 9:50:29have to explicitly say okay whenever
  15396. 9:50:32you're going to set this course in your
  15397. 9:50:33back end code something like this that
  15398. 9:50:35in know in my course option I have a
  15399. 9:50:37origin this is my front end URL this is
  15400. 9:50:40the method that I want or this is the
  15401. 9:50:42allowed HTTP action and then you can use
  15402. 9:50:45some other options also here okay maybe
  15403. 9:50:47let's see if you're using cookies you
  15404. 9:50:49can from your u you can you have to pass
  15405. 9:50:51credentials equal to true all these
  15406. 9:50:53things but first you have to understand
  15407. 9:50:54that this is how you can uh use a
  15408. 9:50:56filtered course options mean now this
  15409. 9:50:59particular origin only can do a request
  15410. 9:51:02to your backend APIs but in our case
  15411. 9:51:04what we're going to do right so for now
  15412. 9:51:06let's do one thing I think we haven't
  15413. 9:51:07installed the course package I think
  15414. 9:51:09let's do this
  15415. 9:51:12so do npm install course
  15416. 9:51:16clear and then npm install - d
  15417. 9:51:21we also need the course types
  15418. 9:51:24that's it and then first of all what
  15419. 9:51:26we're going to do right so let's go to
  15420. 9:51:28our appts
  15421. 9:51:30and I'm going to import here so I'll do
  15422. 9:51:32import course from course
  15423. 9:51:38and then uh we'll directly use it here.
  15424. 9:51:41So for now I'll just do app dot use and
  15425. 9:51:43then I'll do course if I get time so I'm
  15426. 9:51:45just going to give you some uh advanced
  15427. 9:51:47level of exp or maybe let's do this one.
  15428. 9:51:49Let's keep this one for your node just
  15429. 9:51:51part two because anyways we're going to
  15430. 9:51:52create our microservices and all but
  15431. 9:51:55here you can directly use all of this
  15432. 9:51:56right. So you can pass a option and then
  15433. 9:51:58let's say you can give uh this let's say
  15434. 9:52:00we're going to pass origin you can add
  15435. 9:52:02all of these all these allowed headers
  15436. 9:52:04so many options are there. So for now I
  15437. 9:52:06think this is fine. So this is going to
  15438. 9:52:07handle your chords. Now what we're going
  15439. 9:52:10to do right so let's go to routes and
  15440. 9:52:12then I'm just going to do health dot
  15441. 9:52:15routes.ts.
  15442. 9:52:17Okay so this will be our first route
  15443. 9:52:19that we wanted to create. So first of
  15444. 9:52:21all you have to understand now how you
  15445. 9:52:22can create a express route. So here we
  15446. 9:52:26have something called router. So that we
  15447. 9:52:28are going to import. So we are going to
  15448. 9:52:29import this one from express and this
  15449. 9:52:31will be my router. So now whenever I'm
  15450. 9:52:33going to create a router I need to use
  15451. 9:52:36this router just like in my app.ts when
  15452. 9:52:38I'm creating express application I need
  15453. 9:52:40to use this correct. So that's why first
  15454. 9:52:42of all I'm just going to do export con
  15455. 9:52:44and I'll do health route
  15456. 9:52:47or router and then again I need to use
  15457. 9:52:49this. So that means now this health
  15458. 9:52:52router is going to give me all the
  15459. 9:52:54information that I need for example all
  15460. 9:52:56this method each and every property you
  15461. 9:52:58can use. So what you're doing right
  15462. 9:52:59you're putting all you're taking all the
  15463. 9:53:02functionality or feature that this
  15464. 9:53:04particular thing will give and you're
  15465. 9:53:06assigning this one to a variable. Now
  15466. 9:53:08what I can do right I can do a health
  15467. 9:53:10router and let's say I wanted to create
  15468. 9:53:11a get request. Okay so I can do like
  15469. 9:53:14this get if I wanted to create a post
  15470. 9:53:16request I can do post. If I wanted to
  15471. 9:53:18create a put, I can do put. If I wanted
  15472. 9:53:20to delete, you have options. So many
  15473. 9:53:22things. So here I'm just going to do a
  15474. 9:53:24get. This first of all you have to give
  15475. 9:53:26the path. So this will be your API path.
  15476. 9:53:28Okay, this will be your API path. Now
  15477. 9:53:31this is important concept. So whenever
  15478. 9:53:33let's say you have a front end URL that
  15479. 9:53:35is running on local host 3000 correct
  15480. 9:53:40now from your front end when you're
  15481. 9:53:42going to call this AP endpoint, how
  15482. 9:53:44you're going to call where your back end
  15483. 9:53:46is running? your back end is running on
  15484. 9:53:47localhost 4,000. So now this will become
  15485. 9:53:51local host 4,000 / health. Now let's say
  15486. 9:53:55what I wanted to do right I don't want
  15487. 9:53:57to do like this for [snorts] all my API
  15488. 9:54:00endpoints I wanted to add a prefix API.
  15489. 9:54:03So in that case what you can do so you
  15490. 9:54:04have to do / API/ health/ API/ users/
  15491. 9:54:09API/ product something like this. Okay.
  15492. 9:54:12So now here what I'll do right. So for
  15493. 9:54:14now I'm going to take a health and now
  15494. 9:54:17here what I have to do right you have to
  15495. 9:54:18create the handler. Okay. So here I'm
  15496. 9:54:20going to have my request object.
  15497. 9:54:23I'm going to have my response object.
  15498. 9:54:26And then what I need to do I need to
  15499. 9:54:28return a response back. Response back to
  15500. 9:54:31the user. You think always like this.
  15501. 9:54:33You have to return back something to the
  15502. 9:54:35user because at the end your user is
  15503. 9:54:37going to see something on UI. Correct.
  15504. 9:54:39So you're going to do raise dot status.
  15505. 9:54:41This will be what? 200 because this is a
  15506. 9:54:43success and this will be JSON and here
  15507. 9:54:46you're going to pass a success would be
  15508. 9:54:48true and then I'm going to pass a
  15509. 9:54:50message. I'll do health route is
  15510. 9:54:54working. So this will be the first route
  15511. 9:54:57that I have created. Okay. Now what is
  15512. 9:54:59what will be best approach here? You
  15513. 9:55:01should you can directly use this in
  15514. 9:55:02app.ts but I'll not suggest what you
  15515. 9:55:05should do right you should always create
  15516. 9:55:06a entry file here. So this will be your
  15517. 9:55:09root route file. Okay. So this is where
  15518. 9:55:12you're going to combine all your routes
  15519. 9:55:16of application means every route. Let's
  15520. 9:55:18say you're having a 1,000 route also
  15521. 9:55:21you're going to add it here. Okay. So
  15522. 9:55:23now again we need to create our router.
  15523. 9:55:26So I'll do uh import something from
  15524. 9:55:28express. So I'm just going to take
  15525. 9:55:30router.
  15526. 9:55:32And now what I'm going to do right I'm
  15527. 9:55:34just going to do export con. I'll do API
  15528. 9:55:37router and this will be router. So now
  15529. 9:55:41what I need to do right I need to tell
  15530. 9:55:42that okay this API router now needs to
  15531. 9:55:45use this health router. Okay how we are
  15532. 9:55:47going to do this? So it's simply going
  15533. 9:55:49to do okay API router you have to use
  15534. 9:55:52this health router that's it so now what
  15535. 9:55:55you have done you have actually plugged
  15536. 9:55:57this health router in this component. So
  15537. 9:56:00you're plugging
  15538. 9:56:02all routes
  15539. 9:56:04in one place. Okay. So later let's say I
  15540. 9:56:08created a user router. So you can do app
  15541. 9:56:10API router dot use and you'll do user
  15542. 9:56:13router. So you can have user router. I'm
  15543. 9:56:16just showing okay these things are very
  15544. 9:56:18important. Let's say you're creating a
  15545. 9:56:19post router. You're creating a admin
  15546. 9:56:22router. You're creating let's say
  15547. 9:56:24dashboard router whatever. So you're
  15548. 9:56:25going to combine all of these here in
  15549. 9:56:27this component. Something like this.
  15550. 9:56:29So I think this is done. Save this. So
  15551. 9:56:31now what we have to do right? We have to
  15552. 9:56:33use this in this app dot ts. Now this is
  15553. 9:56:35one of the most important concept that
  15554. 9:56:36we are going to understand. So now what
  15555. 9:56:38I have to do right see this is why we we
  15556. 9:56:41created this. So we don't have to use
  15557. 9:56:42all this individual route in this
  15558. 9:56:44component. What I can do right you can
  15559. 9:56:46simply tell okay app dot use and this
  15560. 9:56:48will be our base API endpoint. Now this
  15561. 9:56:51is very very important. App dot use this
  15562. 9:56:55starts with API. So that means when we
  15563. 9:56:58are going to call this one from our
  15564. 9:56:59front end it will understand okay this
  15565. 9:57:01is the API that we are calling and here
  15566. 9:57:03we are going to simply pass API router
  15567. 9:57:06that's it. So now if you go here what
  15568. 9:57:10will be the final
  15569. 9:57:12uh endpoint here final endpoint will be
  15570. 9:57:15your backend URL which is localhost
  15571. 9:57:194000
  15572. 9:57:20/ API because you are actually adding a
  15573. 9:57:24prefix here. Okay, this is your prefix
  15574. 9:57:26API. Okay, and then you're going to have
  15575. 9:57:29/ health like this. So this will be your
  15576. 9:57:32actual route. So health first API will
  15577. 9:57:36come and then health will come here like
  15578. 9:57:38this. So I think this is completed and I
  15579. 9:57:41think this is done. Now what you're
  15580. 9:57:43going to do and at the end you're going
  15581. 9:57:44to return the app here.
  15582. 9:57:47This app that's it. So now this is
  15583. 9:57:49completed and then we'll go to our
  15584. 9:57:51server.ts. This will be our entry file.
  15585. 9:57:53This is where we are going to do what?
  15586. 9:57:55We are going to call this create app.
  15587. 9:57:58Call this create app means we're going
  15588. 9:57:59to call this method and we're going to
  15589. 9:58:01run our server because we haven't run
  15590. 9:58:03the server yet. Okay. So I'm just going
  15591. 9:58:05to do con app will be create app. This
  15592. 9:58:10is the first thing. And then I'm simply
  15593. 9:58:12going to do app.list.
  15594. 9:58:14So it will listen to a port, right?
  15595. 9:58:16Which port? We have the port 4,000. So
  15596. 9:58:18we are going to do envo
  15597. 9:58:22and then we're going to take a call back
  15598. 9:58:24and here I'm simply going to do logger.
  15599. 9:58:26We already have the logger right and
  15600. 9:58:28we'll do logger.info. And now I'll do
  15601. 9:58:30server is now running on port. And here
  15602. 9:58:36let's do like this on port http
  15603. 9:58:41localhost.
  15604. 9:58:43And then here I'm just going to pass the
  15605. 9:58:44env.
  15606. 9:58:47That's it. Now you'll see that how much
  15607. 9:58:49cleaner this particular file is and that
  15608. 9:58:51is the reason we mentioned that this is
  15609. 9:58:52our entry file. So what will happen when
  15610. 9:58:54the code will run it will come here it
  15611. 9:58:56will check this it will go here it will
  15612. 9:58:59check all of this it will check okay
  15613. 9:59:00this route we have it will go here it
  15614. 9:59:03will check again we have the health
  15615. 9:59:04route it will go to this particular file
  15616. 9:59:08you're getting so simply we are plugging
  15617. 9:59:10each and everything and we are keeping
  15618. 9:59:12in a single file so now we have to go to
  15619. 9:59:15package dojson and then I'm just going
  15620. 9:59:17to add some script here okay let's
  15621. 9:59:19remove this
  15622. 9:59:21uh what are the script we need so Let's
  15623. 9:59:23take a div script. So here I'm just
  15624. 9:59:26going to take a tsx watch. I think this
  15625. 9:59:28you already know we have the
  15626. 9:59:30source/server.ts
  15627. 9:59:31file. So this is our entry file. Okay.
  15628. 9:59:34Then I'm just going to do a build. Build
  15629. 9:59:36will be our ts uh tsc. And then also I'm
  15630. 9:59:41just going to take a start and start
  15631. 9:59:44will be all node dish folder because if
  15632. 9:59:47you go to ts config we have the dish
  15633. 9:59:50folder that where we are going to
  15634. 9:59:51compile and we are going to keep the
  15635. 9:59:52code. So here we will we'll be having
  15636. 9:59:54dish/server.js
  15637. 9:59:56file.
  15638. 9:59:58Save this. And now let's go here. And to
  15639. 10:00:01run this what we have to do we have to
  15640. 10:00:02run the script. Correct? And also here
  15641. 10:00:05we can do like this. So we will do dist
  15642. 10:00:08/ server.js. JS save this and I'm just
  15643. 10:00:12going to do npm run dev
  15644. 10:00:16that's it server is now running on and
  15645. 10:00:18this is the reason we have using
  15646. 10:00:19coloring so you can see that it's coming
  15647. 10:00:21as a color so local host 4,000 correct
  15648. 10:00:23now we'll go here copy this
  15649. 10:00:26and then I'll just paste it here and
  15650. 10:00:28I'll do / health let's see what is
  15651. 10:00:30happening and I will send here route not
  15652. 10:00:33found fantastic now from where it is
  15653. 10:00:35coming I'll see that each and every
  15654. 10:00:37middleware that we have created that are
  15655. 10:00:39getting executed. So now this health
  15656. 10:00:41route is not present correct because
  15657. 10:00:43what you have to do you have to do /
  15658. 10:00:44API/ health. So that is the reason
  15659. 10:00:46you're getting route not found but this
  15660. 10:00:48is actually coming from here not found.
  15661. 10:00:50So this middleware is getting executed.
  15662. 10:00:53You're getting so now this will
  15663. 10:00:55automatically check that okay this route
  15664. 10:00:57is not found. I need to run this one.
  15665. 10:00:59But if I just do localhost 4000/ API/
  15666. 10:01:02health send this you're getting health
  15667. 10:01:04route is working. Fantastic. So in this
  15668. 10:01:06case now it will go check here. it will
  15669. 10:01:08go and okay there is a health route is
  15670. 10:01:11present so that means this is working so
  15671. 10:01:13this will be the base setup that we have
  15672. 10:01:16done so we've understood about the
  15673. 10:01:17course and how we can create express
  15674. 10:01:19route and then express server so with
  15675. 10:01:21this now let's move on to the next one
  15676. 10:01:23okay so what will be our next step now
  15677. 10:01:25remember for this particular project we
  15678. 10:01:27are going to use docker and uh database
  15679. 10:01:30will be definitely possess right now
  15680. 10:01:32what I wanted to mention right uh if you
  15681. 10:01:34are not aware because see for me it's
  15682. 10:01:36not possible to again cover docker and
  15683. 10:01:38docker compose all of these what I'll
  15684. 10:01:40suggest you just go to YouTube and just
  15685. 10:01:41search with my name and uh just type
  15686. 10:01:44DevOps and you'll see that you are going
  15687. 10:01:45to get multiple DevOps videos and I also
  15688. 10:01:49have multiple Docker videos also so if
  15689. 10:01:51you want you can watch this video also
  15690. 10:01:53DevOps full course part two so here I
  15691. 10:01:55have covered the uh docker in details
  15692. 10:01:58same you can also if you want to learn
  15693. 10:02:00with GitLab CI/CD you can watch this one
  15694. 10:02:03or the best will be you can watch the
  15695. 10:02:04first one okay so if you click here and
  15696. 10:02:07you'll see There there will be time
  15697. 10:02:08stamp and where you will be able to see
  15698. 10:02:10docker and docker compose only this part
  15699. 10:02:12you can watch from this uh time stamp
  15700. 10:02:14okay why I'm saying this one because see
  15701. 10:02:17when uh we are going to use or whenever
  15702. 10:02:20we are going to create any products
  15703. 10:02:21level nodejs application right
  15704. 10:02:23especially in the part two we are going
  15705. 10:02:24to cover microservices in part three we
  15706. 10:02:26are going to have some more advanced
  15707. 10:02:28topic docker is one of the best
  15708. 10:02:30absolutely best uh thing that you can
  15709. 10:02:32use okay there is no competition because
  15710. 10:02:35whenever you go to any product based
  15711. 10:02:37company, service bus company doesn't
  15712. 10:02:38matter you'll see that almost all the
  15713. 10:02:41company more than I will say 80 to 90%
  15714. 10:02:43of the projects they are using docker
  15715. 10:02:46okay uh and also it's industry standard
  15716. 10:02:48and that is the main reason I've decided
  15717. 10:02:50to use docker for this particular
  15718. 10:02:52project also and this is very very
  15719. 10:02:54simple what we're going to do right we
  15720. 10:02:55are not going to have multiple services
  15721. 10:02:57for now we are going to have only one
  15722. 10:02:59service and if needed later we are going
  15723. 10:03:00to add more so we are going to have only
  15724. 10:03:02one service and that will be posgress
  15725. 10:03:04and you can see that maybe let's Uh
  15726. 10:03:06first of all let's copy this and then
  15727. 10:03:08I'm just going to change couple of
  15728. 10:03:09things. So what we going to do right in
  15729. 10:03:11this project root we are going to create
  15730. 10:03:14our docker compos file. Okay. So in the
  15731. 10:03:16project root you create docker compose
  15732. 10:03:19do yimml. The reason is very simple
  15733. 10:03:21because in this particular section we
  15734. 10:03:23are going to create all our uh sql
  15735. 10:03:25schemas migration and also we are going
  15736. 10:03:28to create our own custom migration
  15737. 10:03:30script. We are not going to use any
  15738. 10:03:31third party packages. Okay. So here just
  15739. 10:03:33paste it and then I'm just going to
  15740. 10:03:35change this one from support desk
  15741. 10:03:37posgress to let's do this one as nodejs
  15742. 10:03:40capstone
  15743. 10:03:42project. Okay that's it and I think we
  15744. 10:03:46don't have to make any other changes. So
  15745. 10:03:48what we are doing we are using port. So
  15746. 10:03:50if you want you can use a different
  15747. 10:03:51port. Maybe let's say I'll use 5 440.
  15748. 10:03:55Uh and then uh we are using posgress
  15749. 10:03:57image. This is our container name. This
  15750. 10:03:59will be our posgress user posgress
  15751. 10:04:01password and also posgress db. You can
  15752. 10:04:03also change this one to something else.
  15753. 10:04:05Maybe let's do this one as nodejs
  15754. 10:04:07capstone.
  15755. 10:04:10Okay. So this is the first thing that we
  15756. 10:04:12are going to use. Okay. Once we'll do
  15757. 10:04:14that what we are going to do right also
  15758. 10:04:17I'm pretty much sure that you should
  15759. 10:04:18have docker desktop installed on your
  15760. 10:04:22device. You can see that I already have
  15761. 10:04:24this one. So what we have to do? We have
  15762. 10:04:25to start this particular service and
  15763. 10:04:27also we need to go to our renv and we
  15764. 10:04:30need to add our DB URL also. But before
  15765. 10:04:32this first let's do one thing. Let's
  15766. 10:04:34start our uh docker service. So what I'm
  15767. 10:04:36going to do I'm just going to do docker
  15768. 10:04:38compose up - d and let's see what you're
  15769. 10:04:41getting.
  15770. 10:04:45All right. So you can see that this is
  15771. 10:04:46now created and you're going to have
  15772. 10:04:48this capstone project. And if I go here
  15773. 10:04:50you can see that this is running on this
  15774. 10:04:51particular port. So that's it. We don't
  15775. 10:04:54have to make any other changes for now
  15776. 10:04:55because we are going to understand each
  15777. 10:04:57and everything manual way. What I mean
  15778. 10:04:59by manual ways if I want I can I thought
  15779. 10:05:01that I'm going to use some custom sorry
  15780. 10:05:03some packages for migration and
  15781. 10:05:05everything. But I think some of you also
  15782. 10:05:06commented that no we want to learn uh
  15783. 10:05:09our own manual migration. So that is the
  15784. 10:05:11reason we are going to create our own
  15785. 10:05:12SQL migration logic. Okay before this
  15786. 10:05:15what we going to do right we are going
  15787. 10:05:17to add one database URL here. So I'm
  15788. 10:05:19just going to do database URL and let's
  15789. 10:05:21give this one as. So you'll have to do
  15790. 10:05:22postgress because we'll do postgra SQL
  15791. 10:05:25and this will be like this. So posgress
  15792. 10:05:27and then you have to give posgress your
  15793. 10:05:29username your password and then you have
  15794. 10:05:32to give your port. So do local host
  15795. 10:05:36and this is running on 5440 right? So
  15796. 10:05:40you're running on 5440. If you're using
  15797. 10:05:42some different port you can use that.
  15798. 10:05:44And then we are going to give that uh
  15799. 10:05:46what is that name? So you're using more
  15800. 10:05:48JS capstrong. So you're going to use
  15801. 10:05:49this one. Okay,
  15802. 10:05:52that's it. So now let's save this. And
  15803. 10:05:54also it's I think you can also do this,
  15804. 10:05:57right? You can also use underscore here
  15805. 10:06:00if you don't want to use this one. I
  15806. 10:06:01think that would be better. But it's
  15807. 10:06:02fine. Uh so for now I think that's all
  15808. 10:06:05we have to do this particular. So I
  15809. 10:06:07wanted to mention couple of things. So
  15810. 10:06:08the prerequisite for this particular
  15811. 10:06:10section will be better. If you already
  15812. 10:06:12know Docker that's fine then you can
  15813. 10:06:14just continue or else watch that video
  15814. 10:06:16that I mentioned. Second, install docker
  15815. 10:06:18desktop and then you just have to create
  15816. 10:06:20docker compos file and start the
  15817. 10:06:22container. That's it. Now what we're
  15818. 10:06:24going to do right so first let's go to
  15819. 10:06:26our okay we are already here. So we need
  15820. 10:06:28to understand that okay how many users
  15821. 10:06:30we sorry how many tables we need first
  15822. 10:06:32of all I'm pretty much sure you've
  15823. 10:06:34already completed the postgress section
  15824. 10:06:37right in the previous because if you
  15825. 10:06:38don't do this one it will be tricky okay
  15826. 10:06:40I highly recommend you watch that
  15827. 10:06:42complete section do not skip that will
  15828. 10:06:44be a game changanger for you because in
  15829. 10:06:46that whoever completed you'll agree with
  15830. 10:06:48me that I actually covered almost all
  15831. 10:06:49the concepts if you haven't covered I'll
  15832. 10:06:52suggest watch all the posgress topic and
  15833. 10:06:54you'll see that how much easier it will
  15834. 10:06:56be to complete this particular project
  15835. 10:06:57So what we're going to do right we going
  15836. 10:06:59to have one user table. Okay. Second we
  15837. 10:07:02are going to have our support task table
  15838. 10:07:04where we are going to store our support
  15839. 10:07:06related task and then we are going to
  15840. 10:07:08have our banners table. Banner means
  15841. 10:07:10where we are going to do our upload or
  15842. 10:07:12we are going to upload our images. Okay.
  15843. 10:07:14What your uh this particular image uh
  15844. 10:07:17user table is going to consist. User
  15845. 10:07:19table is going to have your ID. So ID of
  15846. 10:07:21that user. It is going to have the email
  15847. 10:07:24of that user. It's going to have
  15848. 10:07:26password hash. We are going to store
  15849. 10:07:28password has. We are also going to have
  15850. 10:07:31our Google ID, right? Because we also
  15851. 10:07:33going to implement our Google login for
  15852. 10:07:36this. And also we are going to have our
  15853. 10:07:38role. Now role is very important because
  15854. 10:07:40for this one we're going to have user as
  15855. 10:07:42a role or this can be admin. Okay. So
  15856. 10:07:45these two will be role and then we're
  15857. 10:07:47going to have created and updated. So
  15858. 10:07:49created will be when this user is
  15859. 10:07:50created. If we are updating that user
  15860. 10:07:52information when the user will be
  15861. 10:07:54updated. So these all the things we
  15862. 10:07:55going to have in our user table. What
  15863. 10:07:57our support table is going to consist.
  15864. 10:08:00This also will have the ID. So ID will
  15865. 10:08:02be your primary key for this particular
  15866. 10:08:04table. Then you're going to have the
  15867. 10:08:05title of that uh support. Then you're
  15868. 10:08:08going to have a status right. And then
  15869. 10:08:10we are also going to have very very
  15870. 10:08:12important we are going to have the user
  15871. 10:08:13ID of that particular. So means which
  15872. 10:08:17user actually creating this particular
  15873. 10:08:18support. So this will be what? This will
  15874. 10:08:20be a I think we have already covered
  15875. 10:08:22this one. So we have primary key and
  15876. 10:08:24foreign key. So this means this
  15877. 10:08:26particular support task is created by
  15878. 10:08:28this particular user. So that is that is
  15879. 10:08:30an amazing if you completed the previous
  15880. 10:08:32uh postgress section it will be way
  15881. 10:08:34easier to understand each and
  15882. 10:08:35everything. Okay. So these all the
  15883. 10:08:36things we are going to have in this
  15884. 10:08:37support table. Next we are going to have
  15885. 10:08:40a banners. Banners is means where we
  15886. 10:08:42going to store our ID. Now you might be
  15887. 10:08:43thinking why we are doing all of these
  15888. 10:08:46schemas at once. There is a huge reason
  15889. 10:08:49and this is how you actually going to
  15890. 10:08:51implement your own project because this
  15891. 10:08:52is you take this one kind of a system
  15892. 10:08:54design means before implementing the
  15893. 10:08:56project you are actually checking your
  15894. 10:08:58project scope how many tables what will
  15895. 10:09:01be your relation all these things okay
  15896. 10:09:03so that is the reason or else if we can
  15897. 10:09:05also do this on step by step but I think
  15898. 10:09:07this will be much easier okay so for
  15899. 10:09:09banners we are also going to have the
  15900. 10:09:10banner ID we are going to have the image
  15901. 10:09:12URL image URL will be what so we are
  15902. 10:09:15going to store the image to our client
  15903. 10:09:17Cloudinary right so that we are going to
  15904. 10:09:19discuss later and then cloudinary cloud
  15905. 10:09:21is going to give you one particular URL
  15906. 10:09:23and that URL we are going to store in
  15907. 10:09:25our postgress we are also going to store
  15908. 10:09:27one cloudinary unique ID and that
  15909. 10:09:30particular thing we call as cloudinary
  15910. 10:09:32public ID and you'll understand why
  15911. 10:09:34actually we need this one and then we're
  15912. 10:09:35going to have created and updated that.
  15913. 10:09:37So these all the tables we are going to
  15914. 10:09:39create. So first what we will do right
  15915. 10:09:42so let's go to our codebase and let's
  15916. 10:09:44create uh maybe so let's do outside only
  15917. 10:09:47so I'm just going to do migrations okay
  15918. 10:09:49now from this point everything will be
  15919. 10:09:51very very important whatever we are
  15920. 10:09:52going to do so first what will be the
  15921. 10:09:54first file so first we're going to
  15922. 10:09:55create our so let's give this one
  15923. 10:09:57sequentially so I'll do enable and here
  15924. 10:09:59we are going to enable our pg crypto um
  15925. 10:10:02extension so I'll do enable pg so let me
  15926. 10:10:05check the spelling so let's c r y p2 I
  15927. 10:10:08think this is correct pg crypto And then
  15928. 10:10:10we going to do let's do this one as um
  15929. 10:10:12maybe let's do pgc crypto.sql. Okay. Uh
  15930. 10:10:16here what we going to do? So we going to
  15931. 10:10:17do create extension.
  15932. 10:10:21Extension spelling is wrong. I think
  15933. 10:10:23extension will be this if not exist
  15934. 10:10:27right. And here we are going to do what?
  15935. 10:10:29So we do pg crypto.
  15936. 10:10:32If those who have already completed my
  15937. 10:10:34posgress they already know why actually
  15938. 10:10:36we need this one. Okay. So I'm not going
  15939. 10:10:38to explain again. So do create extension
  15940. 10:10:40if not exist PG crypto. Next we're going
  15941. 10:10:43to create our user table. So I'm just
  15942. 10:10:44going to do 002
  15943. 10:10:47and I'll do create underscore I'll do
  15944. 10:10:50users
  15945. 10:10:52table.sql.
  15946. 10:10:54So let's create. So I'm just going to do
  15947. 10:10:56create table and here we're going to do
  15948. 10:10:59users. Now first of all what we going to
  15949. 10:11:02have? So we're going to have our ID. ID
  15950. 10:11:04will be UU ID. And here this will be
  15951. 10:11:06your primary key. And then we are going
  15952. 10:11:09to have a default. And what will be a
  15953. 10:11:11default? Default will be generate random
  15954. 10:11:13ID. Remember we have actually covered
  15955. 10:11:15this one also.
  15956. 10:11:17That's it. Then we are going to have
  15957. 10:11:20email. So email will be var. So let's
  15958. 10:11:23give this one as 255
  15959. 10:11:25not [snorts] null. And this also will be
  15960. 10:11:27unique. Very very important. Next we are
  15961. 10:11:30also going to have a password has. So
  15962. 10:11:32I'm just going to take this one as
  15963. 10:11:33password. Why breaking capital? So do
  15964. 10:11:36password has and this will be text. Now
  15965. 10:11:39very very important here when we are
  15966. 10:11:42going to implement our Google login that
  15967. 10:11:43time we don't need password has because
  15968. 10:11:45Google is going to handle all of these.
  15969. 10:11:47So that is the reason we are keeping
  15970. 10:11:48this one as nullable. Okay. [laughter]
  15971. 10:11:50Next we are also going to have our
  15972. 10:11:52Google ID. So I'm just going to do
  15973. 10:11:54Google id. So I'll do vare 255
  15974. 10:11:58and I'm also going to do unique. Next we
  15975. 10:12:01are going to do a role and I'll do vare
  15976. 10:12:04and let's do 20 and I'll do not null.
  15977. 10:12:09So let's take default. So what will be
  15978. 10:12:11the default? By default we're going to
  15979. 10:12:12create this one as user. So do user
  15980. 10:12:15I'm just going to give a check
  15981. 10:12:18and check let's give this one role. So
  15982. 10:12:21I'll do ro in here I'm [snorts] just
  15983. 10:12:23going to give two. So one will be either
  15984. 10:12:25user or else this will be admin. Okay.
  15985. 10:12:28So either it will be user or it will be
  15986. 10:12:30admin. But by default this will be user.
  15987. 10:12:32Then we are also going to have our
  15988. 10:12:34created at. So I'll do created
  15989. 10:12:37at created at time stamp.
  15990. 10:12:41Uh let's do not null
  15991. 10:12:44and what will be default? Default will
  15992. 10:12:46be now. And the same thing we are going
  15993. 10:12:49to do for updated also.
  15994. 10:12:52So do updated at. So updated at this is
  15995. 10:12:56also same. So this will be our first
  15996. 10:12:58table. So you have ID, email of that
  15997. 10:13:01user, password has, Google ID, RO,
  15998. 10:13:03created at, and updated at. Okay. Next,
  15999. 10:13:05we're going to have our third table,
  16000. 10:13:07which will be our support table. So I'll
  16001. 10:13:09do 003 underscore. So I'm just going to
  16002. 10:13:12do create uh maybe let's do support.
  16003. 10:13:15Let's do task. Now tasks and then I'm
  16004. 10:13:19just going to do tablesql.
  16005. 10:13:22So here we are going to do again. So
  16006. 10:13:24I'll do create table.
  16007. 10:13:26Uh let's do support
  16008. 10:13:29tasks
  16009. 10:13:31and here I'm simply going to do id u uu
  16010. 10:13:35id uh this will be primary key this will
  16011. 10:13:38be default and here we also going to do
  16012. 10:13:41generate random key so go here and copy
  16013. 10:13:44this
  16014. 10:13:47next title where care uh how much maybe
  16015. 10:13:51let's do 150 and let's do not null
  16016. 10:13:56Then we're going to have a status. Now
  16017. 10:13:58status we're going to take a var. So
  16018. 10:14:00this will be 20 not null
  16019. 10:14:04default uh let's keep a couple of status
  16020. 10:14:07maybe default we'll keep open or else we
  16021. 10:14:10are going to give a check property here.
  16022. 10:14:14So you check and here what I'll do right
  16023. 10:14:16I'll do status in uh let's give maybe
  16024. 10:14:20open let's give one in progress in
  16025. 10:14:23underscore
  16026. 10:14:25progress and also I'm just going to give
  16027. 10:14:28result so so you have three status okay
  16028. 10:14:32uh what else very very important so
  16029. 10:14:34you're going to have our foreign key
  16030. 10:14:35which will be our user ID UU id so here
  16031. 10:14:39we this will be not null definitely and
  16032. 10:14:41what you have to use we have to use
  16033. 10:14:42references Remember this we have
  16034. 10:14:44actually discussed clearly in that
  16035. 10:14:46posgress see I already mentioned
  16036. 10:14:48everything everything whatever I'm
  16037. 10:14:51mentioning that I'm that whatever you
  16038. 10:14:53are seeing here right all are covered in
  16039. 10:14:55the previous section okay so now here
  16040. 10:14:58what you have to do is you have to give
  16041. 10:15:00the user because see we are using user
  16042. 10:15:02so user will be what will be your
  16043. 10:15:04primary key primary key is ID now this
  16044. 10:15:07is user ID okay so this will be your ID
  16045. 10:15:10now we are going to do on delete
  16046. 10:15:15cusk
  16047. 10:15:17and then uh let's create created add
  16048. 10:15:20timestamp
  16049. 10:15:22not null and I'll do null and then we're
  16050. 10:15:27going to have updated
  16051. 10:15:29add timestamp not null null
  16052. 10:15:35that's it
  16053. 10:15:37so I think this is what we need and at
  16054. 10:15:40the end we need our banners So now we
  16055. 10:15:42will create one more. So I'll do 004
  16056. 10:15:48create banners
  16057. 10:15:51table.sql.
  16058. 10:15:54So here let's create create table and
  16059. 10:15:57here I'm just going to do banners.
  16060. 10:16:00So first of all let's copy couple of
  16061. 10:16:01things right. So this will be same.
  16062. 10:16:04Then we're going to have our image url.
  16063. 10:16:07This will be text and this also will be
  16064. 10:16:09not null. You can't have empty image
  16065. 10:16:11URL. Then we are going to have
  16066. 10:16:13cloudinary public ID. Don't worry, we
  16067. 10:16:16are going to actually cover all of this.
  16068. 10:16:18Even I'll discuss the flow that how all
  16069. 10:16:19all of this is working. For now, you
  16070. 10:16:22consider this one as a unique ID that
  16071. 10:16:24you need to interact with cloudinary
  16072. 10:16:27something like this. And this will be
  16073. 10:16:28cloudinary public ID and this will be
  16074. 10:16:31text not null. Then we also going to
  16075. 10:16:35take these two
  16076. 10:16:38and then paste it here. That's it. So
  16077. 10:16:41these four tables we need first of all
  16078. 10:16:45not four three tables. One is our users,
  16079. 10:16:48one is our support tasks and one is our
  16080. 10:16:51create banner table. Now what important
  16081. 10:16:53thing you are going to understand right?
  16082. 10:16:55Because if you completed the postgress
  16083. 10:16:57video you understand all of these what
  16084. 10:16:58is happening. the thing that we are
  16085. 10:17:00going to understand in the next part
  16086. 10:17:01which will be how you can create your
  16087. 10:17:03own custom migration and we are going to
  16088. 10:17:05migrate the this particular schema in
  16089. 10:17:07our posgress. So let's see how we are
  16090. 10:17:09going to do that in the next part. All
  16091. 10:17:10right. So now let's understand uh
  16092. 10:17:13properly that how you can create your
  16093. 10:17:14own custom migration script. So for this
  16094. 10:17:17one what I'll do right go to source and
  16095. 10:17:20create one more folder here and this I'm
  16096. 10:17:22going to give this one as scripts and
  16097. 10:17:24let's give this one as migrate.ts. Now
  16098. 10:17:26if you want you can also keep this one
  16099. 10:17:27in DB folder uh like DB then you can
  16100. 10:17:30also do migrate.ts that is fine but we
  16101. 10:17:33will use this only. Let's close
  16102. 10:17:35everything else for now. Also before
  16103. 10:17:38that we need to install one very very
  16104. 10:17:40important package and that is we need to
  16105. 10:17:42use something called PG. So PG package
  16106. 10:17:44we are going to actually use to create
  16107. 10:17:46to create our DB connection pool. Okay.
  16108. 10:17:49Now you consider this one if you are
  16109. 10:17:50familiar with right? So for
  16110. 10:17:52what we usually use you use something
  16111. 10:17:54called mongus right and then you'll do
  16112. 10:17:56mongus doconnect and then you'll give
  16113. 10:17:57your mongus mongodbd connection it will
  16114. 10:18:00connect to your db remember in your code
  16115. 10:18:03you need some medium right so that you
  16116. 10:18:05will be able to query from your db so
  16117. 10:18:07this is where we need one particular
  16118. 10:18:09package and that package called pg okay
  16119. 10:18:11because we're using raw posgress right
  16120. 10:18:13raw pg we are not using any like prisma
  16121. 10:18:17all these things right so if you want
  16122. 10:18:19let me just quickly show you what I mean
  16123. 10:18:20by this Chrome. Okay. So if I go here,
  16124. 10:18:24right? So let's refresh this. So I'll do
  16125. 10:18:26explain
  16126. 10:18:28what's pg package is for posgress.
  16127. 10:18:38So it's a official most popular
  16128. 10:18:40low-level posgress client for your
  16129. 10:18:42nodejs. Right? Postgress client is as I
  16130. 10:18:44mentioned that you'll be able to create
  16131. 10:18:46your connection pooling, right? you'll
  16132. 10:18:47be able to query all these things
  16133. 10:18:49whatever you need to do related to your
  16134. 10:18:51uh posgress you'll be able to do that
  16135. 10:18:54right so this is nothing but your client
  16136. 10:18:56for your posgress in NodeJS similarly
  16137. 10:18:58just just like you have Kafka JS for
  16138. 10:19:00your Kafka client for NodeJS something
  16139. 10:19:02like that so what you're going to do
  16140. 10:19:04that we're going to install PG so copy
  16141. 10:19:06this
  16142. 10:19:08and then we will go here clear this and
  16143. 10:19:11let's install this one first
  16144. 10:19:14okay so this is added what I going to do
  16145. 10:19:17right so first of all now you will
  16146. 10:19:20understand another folder structure
  16147. 10:19:22pattern now I already mentioned that how
  16148. 10:19:24we are going to create the folder
  16149. 10:19:26structure remember that whenever you
  16150. 10:19:28will work with any config related thing
  16151. 10:19:31you are going to keep this one inside
  16152. 10:19:33your config folder when you are going to
  16153. 10:19:35do let's say any DB related thing you
  16154. 10:19:37can also keep this one inside your lip
  16155. 10:19:39folder or the better will be you can
  16156. 10:19:41create DB folder and inside this you can
  16157. 10:19:43create something like that but you can
  16158. 10:19:45also keep this one inside leave folder
  16159. 10:19:47only middleware and all these things
  16160. 10:19:49we're going to discuss later so for now
  16161. 10:19:50what I'll do right so for now let's
  16162. 10:19:52create here only so I'll do db ds and
  16163. 10:19:54then first of all what we have to do so
  16164. 10:19:56first I'm going to create on pool here
  16165. 10:19:58const pool and this will be so first we
  16166. 10:20:02need to do new and we are going to use
  16167. 10:20:04this pool and this pool we are going to
  16168. 10:20:06import from pg package so let's go here
  16169. 10:20:09and then I'm going to import something
  16170. 10:20:11from pg
  16171. 10:20:13and this is going to give me I think we
  16172. 10:20:14have to install the types uh Uh I think
  16173. 10:20:16I always miss this. We need to also
  16174. 10:20:18install the PG types also. Let's install
  16175. 10:20:21this. Okay. So this is going to now I
  16176. 10:20:24think it will not give you error. So
  16177. 10:20:25just import and here what you have to
  16178. 10:20:27pass right you have to pass the
  16179. 10:20:28connection string. Okay. Now before that
  16180. 10:20:31we also need to do one very very
  16181. 10:20:33important point. Uh I'm not sure if you
  16182. 10:20:35can recolct or if you can remember I
  16183. 10:20:37mentioned whenever we are going to add a
  16184. 10:20:38new env we also need to update our
  16185. 10:20:41env.ts file that we have created inside
  16186. 10:20:43config. So here what I'm going to do
  16187. 10:20:45right we are going to create a helper
  16188. 10:20:46function and let's do this one as check
  16189. 10:20:49required env uh maybe let's do variables
  16190. 10:20:53something env values or variables is
  16191. 10:20:56better and here I'm going to check the
  16192. 10:20:59uh string and let's return this one as
  16193. 10:21:01string. So what I'll do right I'm just
  16194. 10:21:03going to check the value and this will
  16195. 10:21:05be process env.
  16196. 10:21:09So this is going to give me value if
  16197. 10:21:11there is not value present. So I'm just
  16198. 10:21:13going to throw a new error. Okay. So
  16199. 10:21:15I'll do throw new error and here we are
  16200. 10:21:18going to do missing env variables for
  16201. 10:21:22this variable for or then pass this key
  16202. 10:21:26that this key is not present. And then
  16203. 10:21:27what you have to do you have to note
  16204. 10:21:29here outside return uh value. That's it.
  16205. 10:21:34And then what you can do right because
  16206. 10:21:35we need to add one more remember. So we
  16207. 10:21:37are going to add our database C model.
  16208. 10:21:39So do database C model. This database C
  16209. 10:21:41model is the one that we have added just
  16210. 10:21:43now this one. So do database C model and
  16211. 10:21:45this will be now you can pass this and
  16212. 10:21:47then here what you have to do you have
  16213. 10:21:48to pass the value. So this will be our
  16214. 10:21:51database model. So this is going to
  16215. 10:21:52check this. Okay. So save this. Now
  16216. 10:21:54we'll go to our db.ts and what you have
  16217. 10:21:57to do right very very simple. Just
  16218. 10:21:59import your env right. So you have envy
  16219. 10:22:02cy that's it. So this is going to create
  16220. 10:22:04your pool or connection pool and this is
  16221. 10:22:07where this pool we can basically use to
  16222. 10:22:09interact with our DV. Just think like
  16223. 10:22:11that. So this is the first thing we have
  16224. 10:22:12to do. Now we need to go to our
  16225. 10:22:14migrate.ts and here you need to
  16226. 10:22:16understand quite a few things and it's
  16227. 10:22:18not that much simple. Okay. Uh so let's
  16228. 10:22:21see how much simpler I can do. So first
  16229. 10:22:24of all what you have to do right when
  16230. 10:22:26you're creating a custom migration right
  16231. 10:22:28because when you are a beginner I think
  16232. 10:22:30this is more than enough also. I think
  16233. 10:22:32that you should understand the custom
  16234. 10:22:34migration first then you should use the
  16235. 10:22:37third party packages because you have
  16236. 10:22:39something called node pg migrate if I'm
  16237. 10:22:41not wrong let me just show you right so
  16238. 10:22:42do node pg migrate you'll see we have
  16239. 10:22:44something like this
  16240. 10:22:48uh see we have this node pg migrator so
  16241. 10:22:51this is going to help you to m to
  16242. 10:22:52migrate this one you can do this one
  16243. 10:22:54right and also see here uh whatever blah
  16244. 10:22:57blah but it's fine so you can pass this
  16245. 10:23:00migrate you can do node p bracket and
  16246. 10:23:01it's going to do the migration but we
  16247. 10:23:03don't want to do this. Okay. What we'll
  16248. 10:23:06do right we are going to migrate custom
  16249. 10:23:08way. So how we are going to do that?
  16250. 10:23:09First of all when you're going to do
  16251. 10:23:11migration you need to understand where
  16252. 10:23:12your migration folder uh stays right. So
  16253. 10:23:15here now I'm going to pause this video
  16254. 10:23:18and you have to understand that whether
  16255. 10:23:20you can replay all the concept that we
  16256. 10:23:21have learned so far. Remember in the
  16257. 10:23:24path module in the core module you
  16258. 10:23:25understood that how you're going to join
  16259. 10:23:27your path how you're going to get your
  16260. 10:23:29current working directory. remember so
  16261. 10:23:31that will be your first step. So I'm
  16262. 10:23:33just going to do con migration
  16263. 10:23:35directory and this will be equal to your
  16264. 10:23:38path. You're going to import from path
  16265. 10:23:40node path dot join and you're going to
  16266. 10:23:43get process dot cwd
  16267. 10:23:47remember and then what you have to do
  16268. 10:23:48you have to do migration. So this is
  16269. 10:23:50going to give you the current working
  16270. 10:23:52directory and then you have to go to
  16271. 10:23:53migration. So here this is going to give
  16272. 10:23:55you what? So this will give you your
  16273. 10:23:57migration directory. So now this
  16274. 10:23:59particular script we will understand
  16275. 10:24:00okay this is where all my table stays.
  16276. 10:24:04Okay
  16277. 10:24:05next what we going to do right we are
  16278. 10:24:07also going to create one particular
  16279. 10:24:09table this migration table is going to
  16280. 10:24:11track that which all file we have
  16281. 10:24:14already migrated. So for example we now
  16282. 10:24:17have three table consider later let's
  16283. 10:24:19say we have added seven more tables. So
  16284. 10:24:21it this particular table will understand
  16285. 10:24:24that okay these three tables we have
  16286. 10:24:26already migrated. So these three you
  16287. 10:24:28don't have to do anything. Next seven
  16288. 10:24:30new files we have to migrate. So for
  16289. 10:24:33this one what we're going to do we're
  16290. 10:24:34going to create a new create migration
  16291. 10:24:39create migrations table. Okay. So this
  16292. 10:24:41is what we're going to do the tracking
  16293. 10:24:43now. So we can directly just use raqql
  16294. 10:24:46here. So create table if not exist.
  16295. 10:24:50Right. And I'm going to give migrations.
  16296. 10:24:52Right. This is the name. And inside this
  16297. 10:24:54what we're going to do. So I'm just
  16298. 10:24:55going to use ID. Let's give this one as
  16299. 10:24:58serial. This will be my primary key. As
  16300. 10:25:00I already mentioned, see I've done all
  16301. 10:25:02of these. You might be thinking how I'm
  16302. 10:25:04writing first or this is first. You can
  16303. 10:25:06pause or you can rewatch the same thing.
  16304. 10:25:09I've done this all these things 100
  16305. 10:25:11times. Once you'll do right, you'll
  16306. 10:25:13understand all of these is nothing. It's
  16307. 10:25:14very very simple. Okay. So here we're
  16308. 10:25:16going to take name and I'm just going to
  16309. 10:25:18do V care and I'll do 255
  16310. 10:25:22not null and this name will be unique.
  16311. 10:25:24Okay. So this name will be me and I'm
  16312. 10:25:26also going to track that when this
  16313. 10:25:28particular table is already migrated
  16314. 10:25:30means when this is already executed. So
  16315. 10:25:32do executed at. So we'll take this one
  16316. 10:25:35as time stamp not null and let's take a
  16317. 10:25:39default value and default will be now.
  16318. 10:25:41I'm not sure if there is a spelling
  16319. 10:25:42mistake. If I'm doing a spelling mistake
  16320. 10:25:44don't blindly write. Just do if there is
  16321. 10:25:46any mistake right just do it properly.
  16322. 10:25:49But I think it's correct only. So create
  16323. 10:25:51table if not exist migration ID name
  16324. 10:25:53executed. That's it. So now what we're
  16325. 10:25:56going to do right. So first of all let's
  16326. 10:25:57do one thing. Let's create a function.
  16327. 10:25:59One by one we need to do. So I'm just
  16328. 10:26:00going to create async function. And here
  16329. 10:26:04I'll do migrate. This one is going to
  16330. 10:26:07return a promise of void. So first of
  16331. 10:26:12all what I need to do? So first I need
  16332. 10:26:15to create our uh migration table first
  16333. 10:26:17because we need to create this table
  16334. 10:26:19right. Then only we'll be able to track
  16335. 10:26:20that which all migration we have already
  16336. 10:26:22done. How you can do that? So first
  16337. 10:26:24you're going to do like this. First I'll
  16338. 10:26:26do a so you pull this just now created.
  16339. 10:26:29So you can do query and then you're
  16340. 10:26:31going to pass this create migration
  16341. 10:26:32table SQL. Just now we have created.
  16342. 10:26:35Once we'll do that now we have to find
  16343. 10:26:39all the files inside this particular
  16344. 10:26:41folder that are not yet executed. So
  16345. 10:26:44that means if we can understand okay
  16346. 10:26:46let's say this particular table is not
  16347. 10:26:47executed. So we need to find this
  16348. 10:26:49particular file. Okay. So that means
  16349. 10:26:51what you're going to do right? So I'm
  16350. 10:26:52just going to do con. Let's do executed
  16351. 10:26:55and I'm just going to do new. So I'll
  16352. 10:26:56take a set just just normal concept
  16353. 10:27:00JavaScript concept. So I'll do set and
  16354. 10:27:02then I'm just going to get all my
  16355. 10:27:03executed migration that are already
  16356. 10:27:05executed. So I'll do get executed
  16357. 10:27:08migrations. You're getting what we are
  16358. 10:27:10doing here. See everything we are doing
  16359. 10:27:12I'm doing in that particular sequence so
  16360. 10:27:14that it will be easier. So this get
  16361. 10:27:16executed migration is going to give me
  16362. 10:27:18all the migration that I've already
  16363. 10:27:19done. So I'm simply going to do async
  16364. 10:27:22function get executed migration. Uh
  16365. 10:27:25let's do a promise and maybe let's
  16366. 10:27:27return a string. So I'll do a string and
  16367. 10:27:31of array. And what I'll do right so I'm
  16368. 10:27:34just going to do con result.
  16369. 10:27:37So this will be a so dot query. And here
  16370. 10:27:41what we can do right we can also create
  16371. 10:27:42a type. So just create a type directly
  16372. 10:27:44here. And I'm just going to do migration
  16373. 10:27:47row. And this will be so I'm going to
  16374. 10:27:50take the name which will be string
  16375. 10:27:52because I return string. So here I can
  16376. 10:27:54pass this type. So I do migration log.
  16377. 10:27:57Now what I'm going to do right so I'm
  16378. 10:27:59going to actually uh query. So how we
  16379. 10:28:01going to query? You have to write is SQL
  16380. 10:28:04query. So I'll do select name. We
  16381. 10:28:08already have name here. See we have name
  16382. 10:28:10here. So select name from what is the
  16383. 10:28:14table name. So table name is migration.
  16384. 10:28:15So we'll do select name from migration
  16385. 10:28:18and I'll do order by and order by name.
  16386. 10:28:21Very very simple. We are not doing any
  16387. 10:28:23like very complicated thing. We're
  16388. 10:28:25simply getting the name from the
  16389. 10:28:26migration table. And then what we'll do
  16390. 10:28:28right it's going to give you a row. So
  16391. 10:28:30I'll do result dot rows. So we're going
  16392. 10:28:33to get rows dot map. So we're going to
  16393. 10:28:35get current row which will be our
  16394. 10:28:37migration row. This will this will only
  16395. 10:28:39have name right because we're getting
  16396. 10:28:41name here. And then what we're going to
  16397. 10:28:43do simply going to return the row name.
  16398. 10:28:46So this is going to already give me
  16399. 10:28:48let's consider this that this migration
  16400. 10:28:50table that you created already have this
  16401. 10:28:52create user table. So that means by this
  16402. 10:28:54particular function we'll understand
  16403. 10:28:56that okay this particular table is
  16404. 10:28:57already executed. So that will be our
  16405. 10:28:59second step. Now if we found that okay
  16406. 10:29:02these are executed. So we'll be able to
  16407. 10:29:04see which all are pending. So I'll do
  16408. 10:29:06get uh what I'll do right? We need to
  16409. 10:29:09create one more file here. So let's do
  16410. 10:29:11one thing. We also need to get all the
  16411. 10:29:13migration file. So first I'm just going
  16412. 10:29:15to create one more function and I'll do
  16413. 10:29:17get all migration file that I need to
  16414. 10:29:19migrate. So look get migration files. Uh
  16415. 10:29:22and this will be string of array. And
  16416. 10:29:24what I'll do right I'm going to return.
  16417. 10:29:26So I'll import fs. FS I'm going to
  16418. 10:29:28return from node fs. So I'll do import
  16419. 10:29:32fs from node
  16420. 10:29:36fs.
  16421. 10:29:38So what I can do right so fs dot read
  16422. 10:29:43directory sync and here what I'll do
  16423. 10:29:46right I already have my migration
  16424. 10:29:48directory so it will go to this
  16425. 10:29:49particular path so I'm telling that okay
  16426. 10:29:52go to this particular path no not
  16427. 10:29:54migration migration directory then
  16428. 10:29:56you're going to filter so here I'm
  16429. 10:29:59simply going to take the file and this
  16430. 10:30:01will be file dot and if you go here
  16431. 10:30:05every file is ending with sql so what I
  16432. 10:30:08can do right I'm just going to tell that
  16433. 10:30:09okay if the file actually ends with
  16434. 10:30:13SQL so dossql so that means now we'll
  16435. 10:30:16understand okay for now because we
  16436. 10:30:18haven't migrated anything so it will
  16437. 10:30:20understand these 1 2 3 4 files I have
  16438. 10:30:23correct and then what you can do right
  16439. 10:30:24you can just do a sort here you can do
  16440. 10:30:27sort
  16441. 10:30:28so save this so this is going to give
  16442. 10:30:30you all the migration file that you need
  16443. 10:30:32to migrate now what you can do right you
  16444. 10:30:34can just use this get migration files
  16445. 10:30:37dot filter filter
  16446. 10:30:39and this is going to give you the file
  16447. 10:30:41and then you're going to check let's say
  16448. 10:30:43we have four correct out of these two
  16449. 10:30:46are already executed so now pending will
  16450. 10:30:48be what four minus 2 correct so it will
  16451. 10:30:50be two so we can filter it so it's
  16452. 10:30:52simply going to do if this executed dot
  16453. 10:30:54has this particular file means if this
  16454. 10:30:57particular executed don't have this
  16455. 10:31:00particular file that means that is not
  16456. 10:31:01executed yet correct so that is painting
  16457. 10:31:04it's normal JavaScript there is nothing
  16458. 10:31:05related to any complicated thing we are
  16459. 10:31:08doing if this pending.length length if
  16460. 10:31:10this is zero so that means we don't have
  16461. 10:31:12to do anything so we are going to get
  16462. 10:31:14the logger dot info so I'm going to do
  16463. 10:31:17no pending migration
  16464. 10:31:22and then we going to return then we are
  16465. 10:31:25going to do right now we going to
  16466. 10:31:26actually run the migrations consider two
  16467. 10:31:28are already pending so I'm going to do
  16468. 10:31:30for so do con item no not item let's
  16469. 10:31:33take file name of pending and then what
  16470. 10:31:37we're going to do right here we are
  16471. 10:31:38going to create our actual migration
  16472. 10:31:40file. So we'll do run migration
  16473. 10:31:43and here we're going to pass the file
  16474. 10:31:45name. So this migration actually will be
  16475. 10:31:47our main function where we are going to
  16476. 10:31:49run the migration. So we'll go here and
  16477. 10:31:51let's do uh this will be a sync function
  16478. 10:31:56and I'll do run migration. So this is
  16479. 10:31:58going to give me the file name
  16480. 10:32:01which will be string and here this is
  16481. 10:32:03also going to return a promise of void.
  16482. 10:32:06Okay.
  16483. 10:32:08All right. Right. So I'm going to pause
  16484. 10:32:09this. I'm doing recording from long time
  16485. 10:32:11and then we will resume from here. All
  16486. 10:32:13right. So now let's implement the actual
  16487. 10:32:15migration logic. Okay. So first of all
  16488. 10:32:17what we going to do right? So first
  16489. 10:32:18we'll get our uh SQL. So let's take a
  16490. 10:32:22variable SQL. And then we are going to
  16491. 10:32:23first read. So I'll do fs. Sorry this
  16492. 10:32:26will be fs dot and I'll do read file
  16493. 10:32:30sync. And what we're going to do we need
  16494. 10:32:32to do path dot join. And this is where
  16495. 10:32:35our migration directory will come. And
  16496. 10:32:37then we're going to pass our file name.
  16497. 10:32:39Okay. So this file name and then also we
  16498. 10:32:42have to give the type which will be
  16499. 10:32:44UTF8.
  16500. 10:32:46So now what I'm going to do right so
  16501. 10:32:48let's connect to our client. So have it.
  16502. 10:32:51So we have our pool right. So created
  16503. 10:32:54our pool uh and this will be pool dot
  16504. 10:32:56connect.
  16505. 10:32:58And now we are going to start try catch
  16506. 10:33:03uh in the catch error. So here what I'm
  16507. 10:33:06going to do right client and then here
  16508. 10:33:09you can do a query and we're just going
  16509. 10:33:11to pass a roll back here
  16510. 10:33:15and here I'm just going to throw the
  16511. 10:33:17error. So do throw error finally. So
  16512. 10:33:20once everything is done
  16513. 10:33:23so we are simply going to do what? So
  16514. 10:33:25we'll do client dot release.
  16515. 10:33:28Now in the try now first of all let's
  16516. 10:33:30start. So look have it. So do client do
  16517. 10:33:32query and we'll do begin.
  16518. 10:33:37Now we are going to query the SQL. So do
  16519. 10:33:39client do query create query this SQL.
  16520. 10:33:44We'll do a client dot query and then
  16521. 10:33:47we're going to insert. So do insert. So
  16522. 10:33:49we need to insert each and every file
  16523. 10:33:51once uh in this migration table so that
  16524. 10:33:54we can keep a track. I think this part
  16525. 10:33:55we've already discussed right. Right. So
  16526. 10:33:57we going to do insert into
  16527. 10:34:01insert into migrations
  16528. 10:34:04and then we're going to do what? We're
  16529. 10:34:06going to pass the name
  16530. 10:34:08and here we're going to pass the values.
  16531. 10:34:10So what will be values? So values here
  16532. 10:34:11I'm just going to give. So the values
  16533. 10:34:14will be this uh file name. Okay. So here
  16534. 10:34:17you're going to pass the file name. So
  16535. 10:34:19now this every time we're going to run a
  16536. 10:34:21migration, this is going to actually
  16537. 10:34:23post that particular file name into our
  16538. 10:34:24migration table. Okay. And then we're
  16539. 10:34:27going to just do commit. So do a client
  16540. 10:34:29do query and we'll do commit.
  16541. 10:34:33That's it. And let's take a logger do
  16542. 10:34:36info. And I'm simply going to do
  16543. 10:34:39migration
  16544. 10:34:40completed.
  16545. 10:34:44And let's pass the file name. Okay. So
  16546. 10:34:46this is what we going to do. So this is
  16547. 10:34:48going to actually run your migration.
  16548. 10:34:50Right. So what we doing? So first we are
  16549. 10:34:52checking what all are already executed,
  16550. 10:34:54what all are pending. If nothing is
  16551. 10:34:55pending, we don't have any pending
  16552. 10:34:57migration. And then we are going to just
  16553. 10:34:59uh check get or pending count for every
  16554. 10:35:02file. We are going to run the migration
  16555. 10:35:04and at the end what we can do right
  16556. 10:35:06we'll do logger do info and I'm simply
  16557. 10:35:09going to do all migrations completed
  16558. 10:35:14and then here we're going to run the
  16559. 10:35:16migration. So this migrate this one. So
  16560. 10:35:18this is going to what return a promise
  16561. 10:35:20right? So you can take a catch here. So
  16562. 10:35:22I'll do a error. So there is any error.
  16563. 10:35:26So what I'm going to do is I'll do
  16564. 10:35:27logger dot error. And here we can pass a
  16565. 10:35:31error and let's pass this error here.
  16566. 10:35:33Okay. And also let's do a migration
  16567. 10:35:36field something like this. Migrations
  16568. 10:35:39field.
  16569. 10:35:41And then what we can do we can take a
  16570. 10:35:42process.exit and we can do a
  16571. 10:35:45process.exit of one. This also we have
  16572. 10:35:47already discussed. And let's take a
  16573. 10:35:49finally. And the finally we just have to
  16574. 10:35:53call this pool
  16575. 10:35:55dot end.
  16576. 10:35:58That's it. So this will be what actually
  16577. 10:36:01we are going to uh implement our
  16578. 10:36:03migration script. This is completely
  16579. 10:36:05manual, right? We are not using any
  16580. 10:36:06third party packages or anything. And
  16581. 10:36:08I'll go to package.json. And then here
  16582. 10:36:10we can add one more script. So I'll do
  16583. 10:36:12migrate and then I'm simply going to do
  16584. 10:36:15tsx source/
  16585. 10:36:18So we have a scripts, right?
  16586. 10:36:19webcripts/migrate.
  16587. 10:36:23DS
  16588. 10:36:24also if you want you can add some more
  16589. 10:36:26uh script here. For example, let's say
  16590. 10:36:28you wanted to add a docker up. So you
  16591. 10:36:30can simply do docker compose up hyphen
  16592. 10:36:33d. You want to take a docker down. So
  16593. 10:36:36you can do do a docker down and I'm just
  16594. 10:36:38going to do docker compose down. So we
  16595. 10:36:42can just do all of these. So save this.
  16596. 10:36:44Now let's see what is happening. Right.
  16597. 10:36:46So we are going to do all right. So we
  16598. 10:36:48run simply npm run migrate. Okay. So
  16599. 10:36:50let's do this. I'll do npm run migrate.
  16600. 10:36:55Okay. Getting some error. Syntax error
  16601. 10:36:58near end. All right.
  16602. 10:37:08Oh, we have to pass default here. We
  16603. 10:37:11have to pass default here. And also
  16604. 10:37:14let's give semicolon. And also I have
  16605. 10:37:16checked one more issue is there. So this
  16606. 10:37:17will be gen random id okay not get
  16607. 10:37:21this is also wrong
  16608. 10:37:24uh default we are using here so you need
  16609. 10:37:26to use default let's see here okay see
  16610. 10:37:29here also you're not using default so
  16611. 10:37:30you have to use a default default value
  16612. 10:37:36and also this will be len so now let's
  16613. 10:37:40run it once again
  16614. 10:37:42clear this
  16615. 10:37:45fantastic so my is Now completed
  16616. 10:37:47migration completed all migrations are
  16617. 10:37:49completed. So that means now this is
  16618. 10:37:52migrated this particular
  16619. 10:37:55schemas and whatever table we have this
  16620. 10:37:56is migrated to our posgress. So with
  16621. 10:37:59this what we have understood till this
  16622. 10:38:01part that you can also implement your
  16623. 10:38:03own script or even create your own
  16624. 10:38:05script for migration. You don't have to
  16625. 10:38:07always use your third party packages
  16626. 10:38:09right but again all these whatever you
  16627. 10:38:12have written right these has to be
  16628. 10:38:13precise it means you need to know all of
  16629. 10:38:15these or if you don't have any idea
  16630. 10:38:17right what why I'm mentioning all this
  16631. 10:38:18one because don't learn any tech blindly
  16632. 10:38:21for example let's say you don't have
  16633. 10:38:23that much patience that okay you haven't
  16634. 10:38:24watched the postgress part in the
  16635. 10:38:25previous section and you directly
  16636. 10:38:27watching this video some of the things
  16637. 10:38:29will be very tricky for example let's
  16638. 10:38:31say here only I made this mistake right
  16639. 10:38:34I missed this one while writing but I I
  16640. 10:38:36can guarantee some of you already was
  16641. 10:38:38that party already catch it. Okay, there
  16642. 10:38:40is default part is missing. you're
  16643. 10:38:42getting what I'm talking about or maybe
  16644. 10:38:44let's say this one also gen random ID
  16645. 10:38:46right for example let's say if I wanted
  16646. 10:38:48to ask you okay what actually this gen
  16647. 10:38:50random ID is let me just refresh this so
  16648. 10:38:53I'll do uh
  16649. 10:38:55random ID in SQL
  16650. 10:38:59now these whatever line that we are
  16651. 10:39:01writing so gen random ID is going to
  16652. 10:39:03build in postgress function generate u
  16653. 10:39:05ID right here this also can be interview
  16654. 10:39:08question now if you don't have any idea
  16655. 10:39:09and blindly writing all of these then it
  16656. 10:39:11will be difficult for you to understand.
  16657. 10:39:13So that is the reason first prerequisite
  16658. 10:39:15will be posgress and then we also
  16659. 10:39:18understood that we are using docker
  16660. 10:39:20compose. We created one simple uh
  16661. 10:39:22service where we are using posgress.
  16662. 10:39:24This is our env variables. This is the
  16663. 10:39:26port where we are running. We have added
  16664. 10:39:28this one in our env right. We have our
  16665. 10:39:29database URL. We added the config uh
  16666. 10:39:32this database URL. We have created our
  16667. 10:39:34DB pool. And this is what we are going
  16668. 10:39:36to use to query uh everything. So with
  16669. 10:39:39this our base structure is now
  16670. 10:39:40completed. What will be next step? Next
  16671. 10:39:43will be we are going to actually start
  16672. 10:39:44our CRUD and authentication. Each and
  16673. 10:39:46every flow we are uh we are going to
  16674. 10:39:48understand in very details. So let's get
  16675. 10:39:51started. And also for this particular
  16676. 10:39:52project we are going to use our own uh
  16677. 10:39:55error handling. We are not going to use
  16678. 10:39:57any third party packages like zord and
  16679. 10:39:58everything that I will keep that
  16680. 10:40:00particular part for our microservices
  16681. 10:40:02video. So with this now let's move on to
  16682. 10:40:04the next one. All right. So first let's
  16683. 10:40:06create one more folder here and here I'm
  16684. 10:40:08just going to give this one as errors.
  16685. 10:40:10Okay. And inside this, let's create one
  16686. 10:40:12app error. DS. So this is where we're
  16687. 10:40:16going to actually handle our error. So
  16688. 10:40:18I'm just going to do export class. This
  16689. 10:40:21will be simply app error. And this is
  16690. 10:40:23going to extend our error. Okay, error
  16691. 10:40:26interface. What we going to do? We are
  16692. 10:40:28going to have a status code. And this
  16693. 10:40:30status code will be number. And now I'll
  16694. 10:40:33simply take a constructor. And here what
  16695. 10:40:36we going to do, right? We're going to
  16696. 10:40:37take our status code which will be
  16697. 10:40:39number. And also there is a possibility
  16698. 10:40:41that we are also going to send send one
  16699. 10:40:43uh message whenever it's needed. So now
  16700. 10:40:46we going to p take a super pass the
  16701. 10:40:48message and then I'm simply going to do
  16702. 10:40:50this dot status code will be equal to
  16703. 10:40:52the status code that we have. That's it.
  16704. 10:40:54And we don't have to do anything where
  16705. 10:40:56we are going to use this particular uh
  16706. 10:40:58error class. So we're going to use this
  16707. 10:41:00one in our error handler. Remember we
  16708. 10:41:02have created this error handler. This is
  16709. 10:41:04what we are going to do. Now if you
  16710. 10:41:05notice what we are simply doing, we have
  16711. 10:41:07error here. request response blah blah
  16712. 10:41:09whatever and at the end then we are
  16713. 10:41:10sending this response back. What we need
  16714. 10:41:12to check that if this particular error
  16715. 10:41:15that we have if this is a instance of
  16716. 10:41:18this app error. So in that case what we
  16717. 10:41:20can do right we don't have to pass
  16718. 10:41:22always some success false and message uh
  16719. 10:41:25whatever it is why in this case what we
  16720. 10:41:27can do right we can directly get our
  16721. 10:41:29error dot status code also okay and also
  16722. 10:41:32we going to get our message so what we
  16723. 10:41:34going to do right simply to use this I'm
  16724. 10:41:37simply going to do if this error
  16725. 10:41:38whatever error that we have if this
  16726. 10:41:41error is instance of the app error that
  16727. 10:41:43we have created our custom error so in
  16728. 10:41:45that case I'm simply going to do race
  16729. 10:41:47dot status and this will error dot
  16730. 10:41:50status code. So you can see that you're
  16731. 10:41:51getting error dot status code dot JSON
  16732. 10:41:54and then you can do a success will be
  16733. 10:41:57false
  16734. 10:42:02and we are going to pass message which
  16735. 10:42:05will be error dot message right so now
  16736. 10:42:08this will be a custom way that we are
  16737. 10:42:10handling the error okay so now what you
  16738. 10:42:12can do that whenever we need to use this
  16739. 10:42:14let's say I'm going to pass a new app
  16740. 10:42:15error and then we're going to pass a
  16741. 10:42:16status code correct and the message so
  16742. 10:42:19it's going to let's say something
  16743. 10:42:20breaks. So it will check okay the
  16744. 10:42:22currently the error is the instance of
  16745. 10:42:24app. So it's going to send this one and
  16746. 10:42:26this error handler we are actually using
  16747. 10:42:28in our app. DS okay so this is how it's
  16748. 10:42:31going to work. So this is the first
  16749. 10:42:32thing we have to do. Now what will be
  16750. 10:42:34the second step? Second step will be now
  16751. 10:42:37before we will start writing let's
  16752. 10:42:39understand that how that actual flow
  16753. 10:42:41will work. It will be easier. Uh let's
  16754. 10:42:43use this one. See first of all we have
  16755. 10:42:45to understand understand that okay how
  16756. 10:42:47this flow will work. So let's say you
  16757. 10:42:48have a register and you have a login
  16758. 10:42:51right. So how register will work? So
  16759. 10:42:53let's say you have a email right
  16760. 10:42:54consider you have a UI you have a email
  16761. 10:42:57you have a password correct? So this is
  16762. 10:43:00your password and then you have one more
  16763. 10:43:02button and this is like continue with
  16764. 10:43:06Google. Now you'll see that this
  16765. 10:43:08particular thing you are going to see so
  16766. 10:43:10many places. So that means we are giving
  16767. 10:43:13user options that either you can
  16768. 10:43:14register using email and password or so
  16769. 10:43:18here I'm going to do or you can register
  16770. 10:43:20directly using Google. Now this Google
  16771. 10:43:22part we are not going to do now okay and
  16772. 10:43:24there is a reason for this because this
  16773. 10:43:26will be little tricky because you need
  16774. 10:43:28to know the concept of all three direct
  16775. 10:43:30call back and all those things this one
  16776. 10:43:32we're going to do later. Okay first we
  16777. 10:43:34have to understand that how this flow
  16778. 10:43:37will work. So when we are going to do
  16779. 10:43:38email and password so user will enter a
  16780. 10:43:41email. So first of all we need to check
  16781. 10:43:42if the email format is correct or not.
  16782. 10:43:44If this is a valid email or not. So that
  16783. 10:43:47is the first thing. Second we also going
  16784. 10:43:49to check whether user entered the
  16785. 10:43:51password or not. So that is the very
  16786. 10:43:52basic validation. If user don't enter
  16787. 10:43:56email or password we are going to send a
  16788. 10:43:58error back. Okay. So that is the first
  16789. 10:44:00thing. Second we need to check if the
  16790. 10:44:02email is a valid password. Sorry email
  16791. 10:44:04is a valid email. Second, if the
  16792. 10:44:06password format is valid or not. So
  16793. 10:44:08let's say I need at least six characters
  16794. 10:44:10in the password. But when user is
  16795. 10:44:12giving, user is giving 1 2 3 4 5
  16796. 10:44:15characters. So that means this condition
  16797. 10:44:17is not satisfying. So in that case we
  16798. 10:44:19are also going to throw an error. Now
  16799. 10:44:21once registration is completed, so once
  16800. 10:44:24this is completed, we will not redirect
  16801. 10:44:27the user to our application. Now this is
  16802. 10:44:29actually very important. Why? What we
  16803. 10:44:31are going to do right? Once registration
  16804. 10:44:33is done, we will simply mention user.
  16805. 10:44:35Okay, registration is completed. Okay.
  16806. 10:44:38Now what we have to do? You have to log
  16807. 10:44:40to enter into application. Why? Because
  16808. 10:44:43directly we are not going to do that. Uh
  16809. 10:44:45and now remember one thing, there is so
  16810. 10:44:47many things we can do here. You can
  16811. 10:44:48implement a two-step verification. You
  16812. 10:44:50can send email and know those things.
  16813. 10:44:51But I'm not going to do all of this. I
  16814. 10:44:53already have a separate video for that
  16815. 10:44:55also. If you want you can check all of
  16816. 10:44:56this. I think there is no point of
  16817. 10:44:58adding each and everything in this
  16818. 10:44:59particular project. So if you if you're
  16819. 10:45:02not sure I okay you can go to my channel
  16820. 10:45:05and just search for sangamukhari nodejs
  16821. 10:45:07security and those things and you'll see
  16822. 10:45:08that you are going to get all of these
  16823. 10:45:10okay but what I'm trying to say that
  16824. 10:45:11after register we will not allow the
  16825. 10:45:13user to enter into our application once
  16826. 10:45:16registration is done user needs to login
  16827. 10:45:18and once you're going to do login what
  16828. 10:45:20will happen so user will again give
  16829. 10:45:21email user will again give password if
  16830. 10:45:24user is giving wrong email or wrong
  16831. 10:45:25password we'll simply say that okay your
  16832. 10:45:27email is wrong password is wrong once
  16833. 10:45:30they will give correct credential what
  16834. 10:45:32we're going to do right we're going to
  16835. 10:45:33create a token and this token we call as
  16836. 10:45:36JWT right so this will be our JWT token
  16837. 10:45:39what this JWT token is going to hold
  16838. 10:45:42this token is going to hold this
  16839. 10:45:44particular user information so user
  16840. 10:45:46information what user information so
  16841. 10:45:49here in this using this JWT token we
  16842. 10:45:52will be able to verify whether this user
  16843. 10:45:54is a correct user or not so consider we
  16844. 10:45:56have a user A and user B so the JWT
  16845. 10:46:00information of this user A will be
  16846. 10:46:01different. The JWT token of this user B
  16847. 10:46:04will be different. This user JWT is will
  16848. 10:46:07be holding user A information, this uh
  16849. 10:46:10user BJ JWT is going to hold user B JWT
  16850. 10:46:14information. Right? So that means when
  16851. 10:46:16let's say if user B try to access user A
  16852. 10:46:19account, it's not going to happen.
  16853. 10:46:21Correct? You're getting what I'm talking
  16854. 10:46:22until unless this user know the password
  16855. 10:46:25of this particular user. So that is the
  16856. 10:46:27reason we have something called JWT
  16857. 10:46:29right JWT is going to give our uh
  16858. 10:46:32capability to check whether that
  16859. 10:46:34particular user is a valid user or not.
  16860. 10:46:36So first of all what we're going to do
  16861. 10:46:38is we'll be implementing our register
  16862. 10:46:39flow. Once user will register user will
  16863. 10:46:42go to login. Once user will login then
  16864. 10:46:45we're going to create our JWT token.
  16865. 10:46:48Once they will create the token then
  16866. 10:46:49we're going to create our middleware.
  16867. 10:46:51Now this is where you are actually going
  16868. 10:46:53to implement uh or understand one very
  16869. 10:46:55important concept that is middleware. So
  16870. 10:46:57now what are we going to do right? So
  16871. 10:46:59first let's start with the register
  16872. 10:47:00first and then we'll go to medial
  16873. 10:47:02because this is one of the most
  16874. 10:47:03important thing you have to understand
  16875. 10:47:05right how you can implement uh protect
  16876. 10:47:07routes or how you can protect routes. So
  16877. 10:47:10one will be protection of routes and
  16878. 10:47:11second something called rolebased access
  16879. 10:47:14control. Now this is actually rolebased
  16880. 10:47:16access control. So this is actually very
  16881. 10:47:18important. Okay. So let's uh start with
  16882. 10:47:20the register
  16883. 10:47:22logic in the next part. Okay. So we'll
  16884. 10:47:24start implementing our register and
  16885. 10:47:26login flow. Correct. So first let's go
  16886. 10:47:28here. And now here also you are going to
  16887. 10:47:31understand now how you can create proper
  16888. 10:47:33folder structure practically. Right. We
  16889. 10:47:35will have now more practical code
  16890. 10:47:36implementation rather than only
  16891. 10:47:37concepts. So let's create our first
  16892. 10:47:39folder which will be types. Types will
  16893. 10:47:41be one folder where you're going to keep
  16894. 10:47:43all your TypeScript types. As simple as
  16895. 10:47:45that. In our case because in register
  16896. 10:47:47and login flow we are working with user.
  16897. 10:47:50So we are going to create our first type
  16898. 10:47:52which will be user.ts.
  16899. 10:47:54So now let's close everything else. So
  16900. 10:47:56what this is going to have this is only
  16901. 10:47:58going to have your user type. So we do
  16902. 10:48:00export type and I'm just going to give
  16903. 10:48:02user. So user is going to have your ID
  16904. 10:48:04which will be string. User is going to
  16905. 10:48:06have your email which will be string.
  16906. 10:48:08User will be having your role which will
  16907. 10:48:10be string. And also it's going to have
  16908. 10:48:12your created. Now remember these all we
  16909. 10:48:14already have here, right? say uh id uh
  16910. 10:48:17this id email created at all this. So
  16911. 10:48:20here also we're going to take created
  16912. 10:48:23add and this will be date. Okay. So this
  16913. 10:48:26is the first thing we are going to do.
  16914. 10:48:28Now you have to understand one very
  16915. 10:48:30important point that how the actual
  16916. 10:48:32folder structure works. So need to
  16917. 10:48:33understand this one in three parts.
  16918. 10:48:35Okay. So first one
  16919. 10:48:38or let's do like this. So this is
  16920. 10:48:41consider this is your root file. This is
  16921. 10:48:43your root file. I'm going to keep our
  16922. 10:48:45root. What I mean by root file? Root
  16923. 10:48:47file is your app.ts. Consider like this
  16924. 10:48:49this one. Correct?
  16925. 10:48:51Now this root file is going to hold all
  16926. 10:48:55the information of your code. Correct?
  16927. 10:48:57What you're going to do right? Normally
  16928. 10:48:59we usually divide this one in three main
  16929. 10:49:01files. One file we called as repository.
  16930. 10:49:04So I'm going to call this one as repo.
  16931. 10:49:06Second one we call as our services. So
  16932. 10:49:10services is our where business logic
  16933. 10:49:12we're going to keep. And third will be
  16934. 10:49:13our actual routes and this routes file.
  16935. 10:49:16So this will be your routes and then
  16936. 10:49:18there will be your root routes file.
  16937. 10:49:20Root route root routes file. Now this
  16938. 10:49:25might be little tricky. So let's
  16939. 10:49:26understand first of all what actually
  16940. 10:49:27repository is. Repository is going to
  16941. 10:49:29hold or repository will be responsible
  16942. 10:49:31for writing all your DB related logic.
  16943. 10:49:35DB related logic means whenever you need
  16944. 10:49:37to interact with your post DB. So in
  16945. 10:49:40that case you are going to write all
  16946. 10:49:42those logic in your repo. You are not
  16947. 10:49:45going to write your service logic in
  16948. 10:49:47your repository file. Okay. Repository
  16949. 10:49:49means you will only interact with your
  16950. 10:49:51DB. What your service logic? Service
  16951. 10:49:53logic is going to actually hold your
  16952. 10:49:54business logic. For example, let's say
  16953. 10:49:56you want to implement some validation.
  16954. 10:49:58You want to implement some extra logic.
  16955. 10:50:00You are using some helper function.
  16956. 10:50:01You're creating your JWT token. All
  16957. 10:50:03these things okay that is going to hold
  16958. 10:50:05your service logic. And this is not
  16959. 10:50:07about only register or login flow. This
  16960. 10:50:09will be for all the logic that you're
  16961. 10:50:11going to write. And what your route
  16962. 10:50:13information is going to hold? Route
  16963. 10:50:14information is going to hold your actual
  16964. 10:50:16routes based on that particular feature.
  16965. 10:50:19What I mean by based on that particular
  16966. 10:50:21feature, let's say you have a O feature
  16967. 10:50:23and you have a CRUD feature. So there
  16968. 10:50:25will be two file. One route file will be
  16969. 10:50:27for O, one route file will be for uh
  16970. 10:50:30CRUD. And then these two routes you are
  16971. 10:50:33going to combine. So these two routes
  16972. 10:50:35you're going to combine in your root
  16973. 10:50:37route file. Correct? So root route I
  16974. 10:50:39think we have already created. Root
  16975. 10:50:41route means this route. So this is where
  16976. 10:50:43you are going to have all your routes
  16977. 10:50:45and this route file you're going to use
  16978. 10:50:47in your main entry file. Right? So this
  16979. 10:50:50will be the flow. So that means now in
  16980. 10:50:52our case what you have to create we have
  16981. 10:50:54to create couple of folders. So first
  16982. 10:50:56I'm going to create repository.
  16983. 10:51:00Then we're going to create services.
  16984. 10:51:03Then we have routes which we already
  16985. 10:51:06have. Correct? So first let's start with
  16986. 10:51:08our uh repository. So here I'll go to
  16987. 10:51:11repository.
  16988. 10:51:13Where is repository? So here I'm just
  16989. 10:51:15going to create user dot. So I'll do
  16990. 10:51:18repository
  16991. 10:51:20dot ds. Okay. So this is where we are
  16992. 10:51:24going to actually have all your DB logic
  16993. 10:51:26in this particular file. Then you're
  16994. 10:51:28going to have a services. Services means
  16995. 10:51:30where we are going to add our actual
  16996. 10:51:32services logic. Okay. All right. Sorry I
  16997. 10:51:34need to pause because of some noise.
  16998. 10:51:35Okay. So what I was talking about right
  16999. 10:51:37so in the services we're going to write
  17000. 10:51:38actual business logic. Okay. So first of
  17001. 10:51:40all let's create our o service. So I'm
  17002. 10:51:42just going to do o dot service.
  17003. 10:51:45Yes. Now normally in my other videos
  17004. 10:51:46what I do right I usually come from like
  17005. 10:51:49a [snorts] reverse way means I'll
  17006. 10:51:51implement the repository and services
  17007. 10:51:52and the then routes but let's do in this
  17008. 10:51:55particular video I'm going to do much
  17009. 10:51:57better way. So what are we going to do
  17010. 10:51:58right? You are going to understand the
  17011. 10:51:59these concepts uh very very clearly. So
  17012. 10:52:02now so we already have used the
  17013. 10:52:03repository o service and now we'll go to
  17014. 10:52:06our routes and let's create our o routes
  17015. 10:52:08and you'll see that how much easier it
  17016. 10:52:10will be for you to understand all this.
  17017. 10:52:13So what you're going to do right first
  17018. 10:52:14of all let's import routes okay so sorry
  17019. 10:52:16router
  17020. 10:52:18so I'm going to import router I think
  17021. 10:52:20these we have already discussed and now
  17022. 10:52:22I'll do export const
  17023. 10:52:25router and this will be about this
  17024. 10:52:28router make sense what you're going to
  17025. 10:52:31do right so if you go here right you see
  17026. 10:52:33this part this particular file is going
  17027. 10:52:36to hold all your routes I already
  17028. 10:52:38mentioned so what you're going to do
  17029. 10:52:40first of all let's do reverse way so I'm
  17030. 10:52:42simply going to do here okay app router
  17031. 10:52:45sorry API router dot use and then I'm
  17032. 10:52:47simply going to tell that whenever I
  17033. 10:52:49will go to this o route so this will be
  17034. 10:52:52my parent route okay / will be the
  17035. 10:52:54parent route for all our o related
  17036. 10:52:57routes now this might be little tricky
  17037. 10:52:58so what I mean by this let's say in your
  17038. 10:53:00o routes file you have five routes one
  17039. 10:53:03is /register one is / login right like
  17040. 10:53:08this so what will happen when I'm using
  17041. 10:53:10/ so now this will become/ / o/register/
  17042. 10:53:14o/ login/ o/me whatever right so this is
  17043. 10:53:18my parent route this is my parent route
  17044. 10:53:20for my o and this is where I'm going to
  17045. 10:53:23use my o router now for now this is not
  17046. 10:53:25going to work okay even if I save this
  17047. 10:53:27this is going to give me error uh that
  17048. 10:53:29okay something is breaking and whatever
  17049. 10:53:31that's fine but now let's go here so
  17050. 10:53:33first of all we created our o router we
  17051. 10:53:35have plugged this o router in our root
  17052. 10:53:37router that's it now what I have to do
  17053. 10:53:39right so in this o router first we have
  17054. 10:53:41to decide that how many routes we need
  17055. 10:53:42to have. So for now let's start with
  17056. 10:53:44register only first. Okay, we'll do one
  17057. 10:53:46at a time. So do all route dot post. So
  17058. 10:53:50now this will be my register. So this
  17059. 10:53:51will be my register route. This will be
  17060. 10:53:53my path that I need to give. And then
  17061. 10:53:55I'm going to have my async. So I'll do
  17062. 10:53:58request response. And here I'm simply
  17063. 10:54:01going to have next.
  17064. 10:54:05And inside this now first of all you
  17065. 10:54:07need to do always try and catch. The
  17066. 10:54:10first thing what you have to understand
  17067. 10:54:12here. So I'm just going to take catch
  17068. 10:54:14error and then whenever there is error
  17069. 10:54:16what I'm going to do right I'm going to
  17070. 10:54:17call this next and I'm going to pass
  17071. 10:54:20this error to our next middleware. Okay
  17072. 10:54:23so this is always going to check it
  17073. 10:54:25because we are already using it uh using
  17074. 10:54:27this in our app.ts TS right you'll see
  17075. 10:54:29that we have this error handler and all
  17076. 10:54:31of these you remember that part we have
  17077. 10:54:32checked that even if you try to send any
  17078. 10:54:34route which is not present so this not
  17079. 10:54:37found is always going to catch it and
  17080. 10:54:39it's going to understood okay this route
  17081. 10:54:40is not present so I need to return a 404
  17082. 10:54:42something like this so this will be the
  17083. 10:54:44first thing in this try what I need to
  17084. 10:54:46do now here we have to understood uh
  17085. 10:54:49sorry understand some of the very
  17086. 10:54:50important thing that when user is going
  17087. 10:54:52to send your email right and password so
  17088. 10:54:57this will be where this will be in your
  17089. 10:54:59UI correct UI means where user will be
  17090. 10:55:01actually typing these values but in your
  17091. 10:55:03back end how we going to get these
  17092. 10:55:05values so this is where we have
  17093. 10:55:06something called request so the request
  17094. 10:55:09is going to give you this body so
  17095. 10:55:11request do body so this request do body
  17096. 10:55:14is going to hold your email and password
  17097. 10:55:16information that you are going to
  17098. 10:55:18receive here so I'm just going to do con
  17099. 10:55:20and this will be my request do body so
  17100. 10:55:24you'll see that we have this request do
  17101. 10:55:25body this is going to have all your
  17102. 10:55:27information that you're going to pass
  17103. 10:55:28because you're using a post request and
  17104. 10:55:30this is what I'm going to get email and
  17105. 10:55:32password right because for now we have
  17106. 10:55:34only email and password now let's say
  17107. 10:55:35you have name also so in this case you
  17108. 10:55:37also take name let's say you have age
  17109. 10:55:39also you're going to take this you have
  17110. 10:55:40address also so whatever you are going
  17111. 10:55:42to send from your UI side you're going
  17112. 10:55:44to get it from your request do body so
  17113. 10:55:46for now we have only email and password
  17114. 10:55:49that's it and now what we going to do
  17115. 10:55:50right we are not going to write so do
  17116. 10:55:53not or not writing
  17117. 10:55:56service logic here right service logic
  17118. 10:55:59here so service logic will be in service
  17119. 10:56:02logic in service file right so service
  17120. 10:56:04file and now this is where I'm going to
  17121. 10:56:06create so I'm going to create our first
  17122. 10:56:08service and this will be my register
  17123. 10:56:11user and this is where I'm going to pass
  17124. 10:56:13this email and the password okay and
  17125. 10:56:16then once this service logic is
  17126. 10:56:18completed what we need to do we need to
  17127. 10:56:20send back the response back to our UI
  17128. 10:56:22right because if you don't send back how
  17129. 10:56:24your user is going to understand that
  17130. 10:56:27okay if my register is successful or
  17131. 10:56:29something goes wrong so that means
  17132. 10:56:30always you need to send your status back
  17133. 10:56:32so what will be status because you are
  17134. 10:56:34creating something remember you are
  17135. 10:56:36creating user so in this case you can't
  17136. 10:56:37use two 200 you have to use 201
  17137. 10:56:41status code we usually use for create
  17138. 10:56:43okay and then you'll do JSON if you just
  17139. 10:56:46do write see 200 versus 2010
  17140. 10:56:55okay means request succeeded two 201
  17141. 10:56:57created means and end request succeeded
  17142. 10:56:59and brand new resource was successfully
  17143. 10:57:01generated on the server. So in this case
  17144. 10:57:02we're creating a new user correct so
  17145. 10:57:05it's a create so that's why you're using
  17146. 10:57:07201 and this will be JSON so we going to
  17147. 10:57:10pass a success we'll keep this one very
  17148. 10:57:12simple main idea is to so that every
  17149. 10:57:15student every level of student can
  17150. 10:57:17understand what we are doing here so
  17151. 10:57:18it's simply going to do registration
  17152. 10:57:20successful and then we are going to do
  17153. 10:57:23like this see please login to continue
  17154. 10:57:25that means you can't get into our
  17155. 10:57:27application yet you have to login then
  17156. 10:57:29only you can continue so this is Done.
  17157. 10:57:31Save this. Now this is going to give you
  17158. 10:57:33error. That is fine. Now we'll go to our
  17159. 10:57:36service file. So go to service file and
  17160. 10:57:38this is where we're going to create our
  17161. 10:57:39first service. So what will be our first
  17162. 10:57:41service? I'm just going to do export
  17163. 10:57:42async function and we're going to use
  17164. 10:57:45this register. See I'm going to repeat
  17165. 10:57:47one thing only the register user. I'm
  17166. 10:57:49doing very very slowly from the next
  17167. 10:57:51time I'm not going to explain each and
  17168. 10:57:52every line. This video will be already
  17169. 10:57:54very long, right? Because some of the
  17170. 10:57:57things will be repeated. You'll see
  17171. 10:57:59that. So we have email and this will be
  17172. 10:58:01string and then we have a password this
  17173. 10:58:04also will be a string right because
  17174. 10:58:06these two we are receiving correct where
  17175. 10:58:07is that we're receiving email and
  17176. 10:58:10password what's this is going to return
  17177. 10:58:12so we're going to return a promise and
  17178. 10:58:14this will void now first of all what we
  17179. 10:58:17going to do we going to check simple
  17180. 10:58:18validation do if email is not present or
  17181. 10:58:21if password is not present now you see
  17182. 10:58:24what we're doing throwing new app error
  17183. 10:58:27this is very very important because Now
  17184. 10:58:29everything you're creating manually
  17185. 10:58:31you're not using any third party
  17186. 10:58:32package. So what I'm doing I'm simply
  17187. 10:58:34using this app error here. So I'm
  17188. 10:58:36calling this app error and similarly I'm
  17189. 10:58:37telling that okay I have to pass a
  17190. 10:58:39status code. So my status code will be
  17191. 10:58:41400 because it's something goes wrong
  17192. 10:58:43and then I'm going to pass message that
  17193. 10:58:45either email and password are required.
  17194. 10:58:47Email and password are required. Now
  17195. 10:58:50what will happen? This app error is
  17196. 10:58:51going to catch it and then in your error
  17197. 10:58:54handler you are simply telling that if
  17198. 10:58:55this error is a instance of app error.
  17199. 10:58:57So you're going to return this. you're
  17200. 10:58:58getting what I'm doing. So this message
  17201. 10:59:00error dot message is nothing but this
  17202. 10:59:02message that we are going to receive. So
  17203. 10:59:04this is the first thing then I'm simply
  17204. 10:59:06going to check if my password dot length
  17205. 10:59:09right if this is less than mean password
  17206. 10:59:12length. So for example let's say I'm
  17207. 10:59:14just going to give mean password length
  17208. 10:59:15as six. Okay. Now here I'm going to give
  17209. 10:59:17a homework. Now this is not a homework
  17210. 10:59:19but simpler. What you can do right
  17211. 10:59:22create one more folder
  17212. 10:59:25called constants
  17213. 10:59:28and keep this six value 61 six in a
  17214. 10:59:33constant and use this here you're
  17215. 10:59:37getting what I'm talking about so that
  17216. 10:59:38means you'll create one more folder here
  17217. 10:59:40constant keep this one in a constant
  17218. 10:59:42like maybe minimum password length
  17219. 10:59:44something like this and then use it here
  17220. 10:59:46so that you'll also uh you will be also
  17221. 10:59:49able to understand that okay what we are
  17222. 10:59:50doing and when next time you're going to
  17223. 10:59:53create project you'll know that okay
  17224. 10:59:54what each and every folder means so here
  17225. 10:59:57also I'm going to throw new app error so
  17226. 11:00:00I'll give a 400 and I'll simply do
  17227. 11:00:02password
  17228. 11:00:04must be at least six characters long
  17229. 11:00:11that's it
  17230. 11:00:13going to do right I'm going to do con
  17231. 11:00:15and I'm just going to do normalize email
  17232. 11:00:17so I'm just going to do email whatever
  17233. 11:00:19email and I'm just going to do two lower
  17234. 11:00:21case. So two lower case and then I'm
  17235. 11:00:23going to do a trim here. This is normal
  17236. 11:00:25concept. Now what we have to do first of
  17237. 11:00:29all you'll see that when you are
  17238. 11:00:31registering a particular user let's say
  17239. 11:00:32one user email is John at the
  17240. 11:00:36redgmail.com. This user is already
  17241. 11:00:38registered. Okay. Next time let's say
  17242. 11:00:41there is one more user is coming B and
  17243. 11:00:43this user is using same email this
  17244. 11:00:45Johntheregmail.com. So what will happen?
  17245. 11:00:48should not happen right because there
  17246. 11:00:49will be only one user with unique email.
  17247. 11:00:52Next time if you allow user to register
  17248. 11:00:55with the same email so there will be two
  17249. 11:00:57user with same email and you don't even
  17250. 11:00:59know whether that is a user A or that is
  17251. 11:01:01a user B. Right? So that means first of
  17252. 11:01:04all we we have to check that if this
  17253. 11:01:06current email that whatever user is
  17254. 11:01:08sending if this email is already present
  17255. 11:01:10in our DB or not. So that means you have
  17256. 11:01:13to find the user if it's present if it's
  17257. 11:01:17already present in DB or not. If it is
  17258. 11:01:22present if yes then we'll not allow we
  17259. 11:01:24will not allow to register with same
  17260. 11:01:29email again. That's it. Right? That's
  17261. 11:01:32it. So this is actually very very
  17262. 11:01:34important especially for
  17263. 11:01:37um beginners. Right? So this we have to
  17264. 11:01:39know. So first we need to know here
  17265. 11:01:43existing user correct. So we do existing
  17266. 11:01:45user and now I'm just going to do a and
  17267. 11:01:47this is where now we have to use our
  17268. 11:01:50repository because how you're going to
  17269. 11:01:51understand if this is existing user you
  17270. 11:01:53need to check in your DB correct so that
  17271. 11:01:55means I what I mentioned that whenever
  17272. 11:01:56you need to do something with DB so that
  17273. 11:01:59means you have to write that particular
  17274. 11:02:02logic in your user repository means in
  17275. 11:02:04your repository file. So first of all
  17276. 11:02:06what are we going to do right? So let's
  17277. 11:02:08go to our types and I'm just going to do
  17278. 11:02:11export type and I'm just going to do uh
  17279. 11:02:15DB DB user row. So DB user row is
  17280. 11:02:20nothing but in which format or in which
  17281. 11:02:23format you are actually storing the user
  17282. 11:02:25in your DB. So this will be same only
  17283. 11:02:27with this. Okay. So this DB user row is
  17284. 11:02:30nothing but what all properties that you
  17285. 11:02:32have in your DB. Okay. So we'll go here
  17286. 11:02:35and also we need one more type here that
  17287. 11:02:37we are going to use later. So I'm just
  17288. 11:02:38going to do export type and let's do DB
  17289. 11:02:42user with password
  17290. 11:02:47and this will be your DB user and and
  17291. 11:02:50here we are going to have what when
  17292. 11:02:52you're going to have password so you're
  17293. 11:02:54also going to have this password has
  17294. 11:02:55this we going to use later. So this will
  17295. 11:02:57be either string or null but for now I
  17296. 11:02:59think this is fine. So now what you can
  17297. 11:03:01do that we'll go to our user repository
  17298. 11:03:03and this is where I'm just going to
  17299. 11:03:04first check that find
  17300. 11:03:07user by email because in our o service
  17301. 11:03:10we need to check that if this user is
  17302. 11:03:12present or not you're getting so first
  17303. 11:03:15I'm simply going to check that find
  17304. 11:03:19user
  17305. 11:03:21uh let's keep by email what you have to
  17306. 11:03:24pass you have to pass this normalized
  17307. 11:03:25email this one so this method now we are
  17308. 11:03:28going to create in our user repository
  17309. 11:03:30So I'm simply going to do export as sync
  17310. 11:03:33function
  17311. 11:03:36find user by email. So here we are going
  17312. 11:03:39to get the email which will be string
  17313. 11:03:41and here what we're going to do right we
  17314. 11:03:43are simply going to get promise what
  17315. 11:03:46will be the return type return type will
  17316. 11:03:48be user this user will be in our type
  17317. 11:03:52now okay one thing I I think I should
  17318. 11:03:54mention don't get confused with this so
  17319. 11:03:56you might be thinking if both are same
  17320. 11:03:58why why you are creating two separate
  17321. 11:04:00this is the format how we are storing
  17322. 11:04:01the user in your DB this is the format
  17323. 11:04:04how you want to return the data for
  17324. 11:04:05example let's say you have one more
  17325. 11:04:07something like this first name and this
  17326. 11:04:10first name you want to return like this
  17327. 11:04:12but in your DB this first name is
  17328. 11:04:14present like this you're getting so
  17329. 11:04:16these both are different so that is the
  17330. 11:04:18reason we taking uh different type so
  17331. 11:04:20that for now it is same but in some
  17332. 11:04:23cases this will not be same
  17333. 11:04:26so now there will be some snake case
  17334. 11:04:27camel case all those things right so
  17335. 11:04:29this is how we are going to return so
  17336. 11:04:30either it will be user or it will be
  17337. 11:04:33null and now what I'm going to do right
  17338. 11:04:35I'm simply do const
  17339. 11:04:37get the result. So result will be a we
  17340. 11:04:40need to get our pool because this is
  17341. 11:04:41where we are going to query. So you pull
  17342. 11:04:43dot query and now this is where you have
  17343. 11:04:45to give the type. This type will be your
  17344. 11:04:47DB user row. This is how we are storing
  17345. 11:04:49you're getting. And now we are going to
  17346. 11:04:51write our SQL query. So I'm simply going
  17347. 11:04:53to do select
  17348. 11:04:58ID. And believe me or not, I usually
  17349. 11:05:01like writing these raw queries so much.
  17350. 11:05:03Okay? because I always believe that
  17351. 11:05:05these all are much easier to understand
  17352. 11:05:07and also it will give you very strong
  17353. 11:05:08foundation. So I need ID email or so
  17354. 11:05:11sorry this will be ro and created at
  17355. 11:05:13from where from my user table. So I'm
  17356. 11:05:15just going to do from my user table
  17357. 11:05:17where what where the email is this
  17358. 11:05:19right? So where email is equal to dollar
  17359. 11:05:22one and this is where I'm going to just
  17360. 11:05:24simply pass our email. So this is now is
  17361. 11:05:27going to return this whether this user
  17362. 11:05:30is present or not. And then I'm just
  17363. 11:05:31going to do read result dot rows. I only
  17364. 11:05:34need the first one or else it will be
  17365. 11:05:37null. That's it. So now this is going to
  17366. 11:05:40tell if this user is already present or
  17367. 11:05:42not. So I'm going to import this. That's
  17368. 11:05:44it. So what will be our next step? Once
  17369. 11:05:47this is done, first we also have to give
  17370. 11:05:49that if this existing user is already
  17371. 11:05:51present. So in that case we're going to
  17372. 11:05:52throw a new error. I think I already
  17373. 11:05:54mentioned that we're going to throw a
  17374. 11:05:56new app error and I'm simply going to do
  17375. 11:05:58409 and I'll say that email already
  17376. 11:06:02present. You're getting what are you
  17377. 11:06:03doing that you can't use same email
  17378. 11:06:05again.
  17379. 11:06:07Once we'll do that now next time when
  17380. 11:06:09we're going to give email and password
  17381. 11:06:11right so let's see user given password 1
  17382. 11:06:132 3 4 5 6 7 8 like that you never store
  17383. 11:06:17this raw password in your DB okay should
  17384. 11:06:20not do that what you have to do you have
  17385. 11:06:22to create encryption or we usually call
  17386. 11:06:24that one as has I think this part you
  17387. 11:06:25already discussed in the crypto module
  17388. 11:06:28so we need to create a password hash we
  17389. 11:06:30are not going to store raw password in
  17390. 11:06:33our DB correct but how we are going to
  17391. 11:06:34do that this is where we need to inst
  17392. 11:06:36install our b-rypt package. So brypt is
  17393. 11:06:39a package that will give you that uh
  17394. 11:06:41feature to hash your password. Okay. Uh
  17395. 11:06:45so I'll just do give this one. Give me
  17396. 11:06:46npm install bcrypt give me types uh npm
  17397. 11:06:51also types brypt also.
  17398. 11:06:59Okay. So these two things we need. So
  17399. 11:07:00let's install npm install bcrypt js. You
  17400. 11:07:03can use something called argan. So there
  17401. 11:07:05is argon but brypt is actually very
  17402. 11:07:07popular. So that is the reason uh we are
  17403. 11:07:09going to use this. So npm install bcrypt
  17404. 11:07:12and also need the types. So we'll copy
  17405. 11:07:14this
  17406. 11:07:16and I'm simply going to do types.
  17407. 11:07:19So now what are you going to do right?
  17408. 11:07:20So here I'm going to create a password
  17409. 11:07:22has. So I'm just going to do con
  17410. 11:07:24password
  17411. 11:07:26has
  17412. 11:07:29and this will be ait. So here what I'm
  17413. 11:07:32going to do is let's uh sorry we have to
  17414. 11:07:34import our brypt. So I'll do import
  17415. 11:07:36bcrypt from brypt
  17416. 11:07:40js
  17417. 11:07:44and here I'm just going to do brypt and
  17418. 11:07:46this is going to give you h has and here
  17419. 11:07:48you have to pass the password. So
  17420. 11:07:49password will be which password? The
  17421. 11:07:50password that you are receiving this
  17422. 11:07:52password. So you're going to pass the
  17423. 11:07:53password here and then you have to pass
  17424. 11:07:55a salt. Okay. Now in this case again
  17425. 11:07:58what you can do right for now I'm just
  17426. 11:07:59going to give direct as 10 but what you
  17427. 11:08:01can do you can move this one to a
  17428. 11:08:03constant file just like I mentioned here
  17429. 11:08:05okay and this is done and then what
  17430. 11:08:07you're going to do right now we are
  17431. 11:08:08going to simply create the user now this
  17432. 11:08:11create user is going to do what it's
  17433. 11:08:12going to actually store your user in
  17434. 11:08:14your DV so I'm simply going to have it
  17435. 11:08:16and I'm simply going to do create user
  17436. 11:08:19right create user and this is where what
  17437. 11:08:23you have to pass you have to pass the
  17438. 11:08:24normalized email and your password has
  17439. 11:08:26both and this create user where you have
  17440. 11:08:29to do it pause it and try to guess again
  17441. 11:08:33you are doing something with your DB so
  17442. 11:08:35that means you have to do in your
  17443. 11:08:36repository so now if you go here and
  17444. 11:08:39let's create one more so I'm simply
  17445. 11:08:40going to do export as sync function and
  17446. 11:08:44I'll do create user this is where we're
  17447. 11:08:46going to get the email which will be
  17448. 11:08:48string we're going to get the password
  17449. 11:08:51has
  17450. 11:08:53this also will be string and Now we are
  17451. 11:08:56going to simply uh this will be type
  17452. 11:08:58will be promise of user.
  17453. 11:09:01So now I'm simply going to do con result
  17454. 11:09:04this will be a pool dot query
  17455. 11:09:08pass out db user row and this is where
  17456. 11:09:11you're going to write our uh query.
  17457. 11:09:14Okay. So I'm simply going to do insert
  17458. 11:09:18into what is a table users. What we have
  17459. 11:09:21to insert? You have to insert email and
  17460. 11:09:23password
  17461. 11:09:25has right. So your password has.
  17462. 11:09:30So you have a values
  17463. 11:09:37and then we are also going to do a
  17464. 11:09:39returning here
  17465. 11:09:42email ro and also let's return the
  17466. 11:09:44created at that's it. And what will be
  17467. 11:09:48the values? values will be your email
  17468. 11:09:50and your password has. Right? Because
  17469. 11:09:52we're passing email and password has.
  17470. 11:09:54That's it. And then you're going to
  17471. 11:09:55simply return
  17472. 11:09:58result dot rows of zero. And this is
  17473. 11:10:03done. Okay. So this will be your query.
  17474. 11:10:08I think there is extra that's fine.
  17475. 11:10:10Let's remove this line.
  17476. 11:10:20Okay, let's keep it like this only. Save
  17477. 11:10:22this. So now, [snorts] oh no, I don't
  17478. 11:10:24like this one. Let's keep it like this.
  17479. 11:10:34And we can move this one here
  17480. 11:10:37like this. Okay, so this is fine. And
  17481. 11:10:39now what we can do, right, we'll go to
  17482. 11:10:40our o service and we'll just import it.
  17483. 11:10:45Done. Go to what routes and then import
  17484. 11:10:48your register user. That's it. And now
  17485. 11:10:50save this and you'll see that now you
  17486. 11:10:52should not get any error here.
  17487. 11:10:56Okay, let's save this. Let's run it once
  17488. 11:11:00again.
  17489. 11:11:01I'll clear this and I'll do npm rundev.
  17490. 11:11:05It's running. Okay, so now let's go to
  17491. 11:11:07our postman. So I'm just going to copy
  17492. 11:11:08this and let's run it. Right. So we have
  17493. 11:11:114,000/ API/ o/ we have register correct.
  17494. 11:11:16Now let's go to our body and we have to
  17495. 11:11:18select raw and we have JSON. So now
  17496. 11:11:21let's send uh email.
  17497. 11:11:24So I'll send this email [email protected]
  17498. 11:11:28and then here also uh what we are going
  17499. 11:11:30to do right maybe let's only keep email
  17500. 11:11:34for now and let's send this.
  17501. 11:11:37Okay we are getting route not found. Oh,
  17502. 11:11:40we have to do post I bet. So do send.
  17503. 11:11:42You'll see that we are getting email and
  17504. 11:11:44uh password are required. Let's see what
  17505. 11:11:45we getting here.
  17506. 11:11:48So we're getting app email and password
  17507. 11:11:49and required.
  17508. 11:11:52We getting cannot set headers after they
  17509. 11:11:54say. Okay, these means we are sending
  17510. 11:11:56the response to time sending back. So
  17511. 11:11:58something we are doing wrong here. Uh so
  17512. 11:12:01let's go to our uh service. So we are
  17513. 11:12:03doing email and password require app
  17514. 11:12:05error.
  17515. 11:12:09Okay. So go to error handler. We'll see
  17516. 11:12:11what we doing. How we have to return
  17517. 11:12:13here. That is the reason. So when we
  17518. 11:12:15doing response uh sending this response,
  17519. 11:12:17right? So you have to return from this.
  17520. 11:12:19So now let's save this. So now let's try
  17521. 11:12:22once again. I think that error should
  17522. 11:12:23not come. So we send we'll see that you
  17523. 11:12:26are not getting that right. So getting
  17524. 11:12:27email and password are required. Comma.
  17525. 11:12:29And here we are going to do password.
  17526. 11:12:32And now what we going to do right? So
  17527. 11:12:33for this one 2 3 4. Let's keep it like
  17528. 11:12:36this. send this. You'll see that we're
  17529. 11:12:37getting password at least six characters
  17530. 11:12:39long. Fantastic. So now let's do 5 six
  17531. 11:12:43and then I'm just going to do send. You
  17532. 11:12:44can see the registration is successful
  17533. 11:12:46and please login to continue. That means
  17534. 11:12:48this user is now present in our DB.
  17535. 11:12:51Okay. So that means this uh particular
  17536. 11:12:54code is now ran this create user. Okay.
  17537. 11:12:57So with this this part is completed. The
  17538. 11:13:00only basic register flow. Now you might
  17539. 11:13:02be thinking one very important point.
  17540. 11:13:04I'm not sure if you can uh actually
  17541. 11:13:06check this particular thing that in our
  17542. 11:13:08user table we also have what is that uh
  17543. 11:13:11where is that user table man uh this one
  17544. 11:13:14role so you have by default this is as
  17545. 11:13:17user correct but how we are going to
  17546. 11:13:19create admin then and you notice that we
  17547. 11:13:22are not sending like this role equal to
  17548. 11:13:24user actually you should not do like
  17549. 11:13:25this and this is not a good good
  17550. 11:13:27practice so the point is there are
  17551. 11:13:29multiple ways we can create admin user
  17552. 11:13:31okay so I'm just going to give you some
  17553. 11:13:32very basic overview and some of The
  17554. 11:13:34things are actually very important
  17555. 11:13:35you'll see that in companies also they
  17556. 11:13:36usually use first one is when you are
  17557. 11:13:39going to create any admin user first of
  17558. 11:13:41all you can directly update in your DB
  17559. 11:13:43that is absolutely fine there is no risk
  17560. 11:13:45though no wrong in this particular
  17561. 11:13:47process also so let's say you are
  17562. 11:13:49creating one particular SAS application
  17563. 11:13:51and you are the owner and you want you
  17564. 11:13:53to be admin right so it'll be admin so
  17565. 11:13:55you can go to your DB you can directly
  17566. 11:13:58uh update that particular role you can
  17567. 11:14:00change your role from user to admin
  17568. 11:14:02second And you can have a admin script
  17569. 11:14:05just like we created a migration script.
  17570. 11:14:07We can also have a admin script. Right?
  17571. 11:14:09Third is like this is little more
  17572. 11:14:11production level. So what companies are
  17573. 11:14:12using they they use different tool.
  17574. 11:14:14Okay. Now they have their internal tool
  17575. 11:14:16also. Now in this internal tool they
  17576. 11:14:18have some configuration management.
  17577. 11:14:20Configuration management and there they
  17578. 11:14:22can do right they can have some admin
  17579. 11:14:24emails. Consider like this. And once
  17580. 11:14:26they will change that admin email and
  17581. 11:14:28update the configuration automatically
  17582. 11:14:29in your DB these values will be also
  17583. 11:14:31updated. So they have some mechanism. So
  17584. 11:14:33there are multiple ways we can actually
  17585. 11:14:35create admin user because I'm pretty
  17586. 11:14:37much sure sir this question can come to
  17587. 11:14:39your mind that how we are going to
  17588. 11:14:40create a admin user then because most of
  17589. 11:14:43the user whatever we're going to create
  17590. 11:14:44by default it will be created as a
  17591. 11:14:46normal user and that makes sense. So
  17592. 11:14:48that is the reason I just wanted to send
  17593. 11:14:49this point. So with this so our uh
  17594. 11:14:52register flow is completed. So in the
  17595. 11:14:54register flow we don't have to do
  17596. 11:14:55anything just save the user and save the
  17597. 11:14:58password. That's it. Okay. also uh this
  17598. 11:15:00one also very important. Okay. So just
  17599. 11:15:02add a return here. I think this part we
  17600. 11:15:04have missed. So with this now let's move
  17601. 11:15:05to the next one which will be our login
  17602. 11:15:07flow. Okay. So now let's create our
  17603. 11:15:09login flow and we are going to use exact
  17604. 11:15:11same uh flow that what we have done. So
  17605. 11:15:14let's close everything. We'll start with
  17606. 11:15:15our o routes. Okay. So first let's
  17607. 11:15:19create one more route here and this will
  17608. 11:15:21be so I'm just going to drawouter.post
  17609. 11:15:24and this will be path will be login. So
  17610. 11:15:26it will be SL api/
  17611. 11:15:28login. Here also we're going to do
  17612. 11:15:30async. So do request response next and
  17613. 11:15:36here let's take a try catch. So in the
  17614. 11:15:40catch we'll get error and then we're
  17615. 11:15:42going to call out next and pass the
  17616. 11:15:44error. So in the try first of all what
  17617. 11:15:46you have to do again when you're going
  17618. 11:15:47to login user is going to type email and
  17619. 11:15:49password only. So I'm not repeating the
  17620. 11:15:51same thing it's same only. So do email
  17621. 11:15:53password and you're going to get from
  17622. 11:15:55request dot body. Okay.
  17623. 11:15:59Now first of all what we have to do here
  17624. 11:16:01right remember once user will login now
  17625. 11:16:04this time we are only not only going to
  17626. 11:16:07redirect the user to our application we
  17627. 11:16:09also need to store something related to
  17628. 11:16:11that particular information right and
  17629. 11:16:13what that something that's something we
  17630. 11:16:15called as access token. Now by the name
  17631. 11:16:18only you can understand access means
  17632. 11:16:19this token is going to give user access.
  17633. 11:16:22Right? Without this particular token,
  17634. 11:16:24you will not be able to understand
  17635. 11:16:25whether this particular user is actually
  17636. 11:16:27authenticated user or not. So that means
  17637. 11:16:29this access token is the token that we
  17638. 11:16:32are going to store in our local storage
  17639. 11:16:34session storage or in our cookies. That
  17640. 11:16:36is like front end side logic, right?
  17641. 11:16:38Once we are going to send this but
  17642. 11:16:39because this is a backend tutorial, you
  17643. 11:16:41have to understand that as a backend
  17644. 11:16:42developer, your task is to create access
  17645. 11:16:45token or refresh token. Refresh token is
  17646. 11:16:46a separate concept. But let's understand
  17647. 11:16:48access token because this is very
  17648. 11:16:50important. Also this token is going to
  17649. 11:16:52have some lifetime. Lifetime means let's
  17650. 11:16:53say you want to you are creating very
  17651. 11:16:55sensitive application there you want
  17652. 11:16:58your uh access token to be only lived
  17653. 11:17:01till 15 minutes means after 15 minutes
  17654. 11:17:03if user uh don't do any kind of activity
  17655. 11:17:06you are not doing any kind of activity
  17656. 11:17:08you are going to auto log out that
  17657. 11:17:10particular user from the application or
  17658. 11:17:11maybe let's say you are creating some
  17659. 11:17:13e-commerce application in that case 15
  17660. 11:17:15minutes is not fine so you can keep the
  17661. 11:17:17access token as maybe let's say 7 days
  17662. 11:17:19I'm just giving example or 1 day 24
  17663. 11:17:21hours Okay. So at the end you have to
  17664. 11:17:24understand this access token whatever we
  17665. 11:17:26are going to create is going to hold
  17666. 11:17:28that user information and based on this
  17667. 11:17:30we are going to check if the user is a
  17668. 11:17:32authenticated user or not. So first what
  17669. 11:17:35we going to do right. So let's do one
  17670. 11:17:36thing. So for now I'm just going to do
  17671. 11:17:39XY Z. Okay. Later we'll change this one.
  17672. 11:17:41You'll understand why I'm doing like
  17673. 11:17:42this. And for now let's create a login
  17674. 11:17:45user. So I'll do login user. And here
  17675. 11:17:47I'm going to pass the email and the
  17676. 11:17:49password. Okay. We are not doing
  17677. 11:17:50anything. this login user will be our
  17678. 11:17:54service logic. So we'll go here and
  17679. 11:17:56let's create another here. So I'm just
  17680. 11:17:57going to do not here we have to do
  17681. 11:18:00export as sync function this will be
  17682. 11:18:05login user. So here also I'm going to
  17683. 11:18:07get email this will be string password
  17684. 11:18:09this will be string and here what this
  17685. 11:18:12is going to return so this is going to
  17686. 11:18:14return promise but now what we going to
  17687. 11:18:16return right we are going to return one
  17688. 11:18:18access token here. So I'm just going to
  17689. 11:18:20return a access token. Okay. And this
  17690. 11:18:22access token will be string. So that
  17691. 11:18:24means this access token we are going to
  17692. 11:18:26receive here. So now if I just do import
  17693. 11:18:28and let's see if I just do access token
  17694. 11:18:30here. I'm going to get the access token.
  17695. 11:18:31So for now I'm not returning. Let's keep
  17696. 11:18:33it like this. Okay. I'm also going to
  17697. 11:18:35remove the import. Let's uh keep it like
  17698. 11:18:37this only for now. So first of all we
  17699. 11:18:39are going to simply give a condition and
  17700. 11:18:41also remember because we doing a custom
  17701. 11:18:43validation right? If I get some more
  17702. 11:18:45like if I have time like if this this
  17703. 11:18:48video is not crossing like two maybe 16
  17704. 11:18:51hours or 18 hours something like that
  17705. 11:18:52then it will be tricky or else I'm going
  17706. 11:18:54to give you some idea that how you can
  17707. 11:18:56use zord and everything to implement
  17708. 11:18:57this validation but you might be
  17709. 11:18:59thinking why I'm not using zord
  17710. 11:19:01absolutely zord is industry standard you
  17711. 11:19:03can use other packages but again because
  17712. 11:19:05I when I create this video right I have
  17713. 11:19:07to think from a beginner point of view
  17714. 11:19:09also let's see if a student is watching
  17715. 11:19:11this video and then you they don't have
  17716. 11:19:13any idea that how they can create a
  17717. 11:19:15custom error class and directly jump in
  17718. 11:19:17learning in zod that that doesn't make
  17719. 11:19:19any sense you're getting so you have to
  17720. 11:19:21go from level one to level five not
  17721. 11:19:24directly to level five so first of all
  17722. 11:19:26let's copy this and then we going to
  17723. 11:19:28first keep it like this simple now what
  17724. 11:19:30you have to do so again we have to do
  17725. 11:19:32normalize email and then I'm simply
  17726. 11:19:34going to do email dot to lowerase dot
  17727. 11:19:38trim okay now what you have to do here
  17728. 11:19:41now this is actually very important
  17729. 11:19:43concept Now what will happen right when
  17730. 11:19:45you are actually giving your email
  17731. 11:19:48and also you're giving your password. So
  17732. 11:19:51now this time you're not registering you
  17733. 11:19:53actually doing a login activity. So we
  17734. 11:19:55have to check if this email is present.
  17735. 11:19:57So that is the first thing you also have
  17736. 11:19:58to check if this password is present and
  17737. 11:20:00also if this is not a valid password. If
  17738. 11:20:03this is not a valid password so we are
  17739. 11:20:05we will not allow user to login. So
  17740. 11:20:08let's say during registration I've given
  17741. 11:20:10here 1 2 3 4 5 6. But while login I'm
  17742. 11:20:13giving 1 2 3 4 6 5 and this is wrong
  17743. 11:20:18correct. So that means we need to
  17744. 11:20:19validate the password also when we going
  17745. 11:20:22to login. So that means here what I'm
  17746. 11:20:24going to do first I'm going to check
  17747. 11:20:25this user is present or not. So I'm just
  17748. 11:20:27going to do a find user by email
  17749. 11:20:33with password email with password. Okay.
  17750. 11:20:37And here we're going to pass a normal
  17751. 11:20:38HTML. this method again when we sorry
  17752. 11:20:41where we are going to create this this
  17753. 11:20:42we're going to create in our repository
  17754. 11:20:44so we'll go to our repository first
  17755. 11:20:46let's copy this we copy this and then
  17756. 11:20:49I'm simply going to do export as sync
  17757. 11:20:52function so this will find user email
  17758. 11:20:55with password here we're going to get
  17759. 11:20:57the email which will be string and here
  17760. 11:20:59we are going to return a promise
  17761. 11:21:02so here we have this DB user with
  17762. 11:21:05password row remember we have created
  17763. 11:21:07this in our types or null. So now you
  17764. 11:21:10can simply take our result. So result
  17765. 11:21:12will be ait pool dot query. So here
  17766. 11:21:16we're going to pass DB user with
  17767. 11:21:18password row and here we can simply
  17768. 11:21:21write. So I'll do select
  17769. 11:21:24ID select email uh select email also we
  17770. 11:21:29need the role also role password has and
  17771. 11:21:33also let's take the created at.
  17772. 11:21:37Okay. So this we're going to get from so
  17773. 11:21:39do from so we have our users where email
  17774. 11:21:43will be this
  17775. 11:21:46okay and then we're going to simply pass
  17776. 11:21:48our email and then at the end we're
  17777. 11:21:50going to return result dot rows of zero
  17778. 11:21:54or else null so this is going to give me
  17779. 11:21:58that particular information okay so now
  17780. 11:22:00we can go back to our o service so we're
  17781. 11:22:02going to check so first what you have to
  17782. 11:22:04check right so here I'm just going to
  17783. 11:22:05check if this user dot
  17784. 11:22:08And this is where I'm going to have this
  17785. 11:22:10password has. Oh, first I have to import
  17786. 11:22:12that. So I'm not getting that right. We
  17787. 11:22:14have to import this. So user dot
  17788. 11:22:16password has if this is not present. So
  17789. 11:22:19I'm just going to do no. So in that case
  17790. 11:22:22I'm simply going to throw new app error.
  17791. 11:22:25And we are going to give a 401. Okay.
  17792. 11:22:28And I'm just going to do invalid email
  17793. 11:22:31or password. Okay. That's it.
  17794. 11:22:35Uh what else? Now we are going to check
  17795. 11:22:37if this password is a valid password or
  17796. 11:22:39not. So I'm just going to do con is
  17797. 11:22:40password valid. So then we are going to
  17798. 11:22:44use a and we're going to use this brypt.
  17799. 11:22:48We have to use bcrypt. We are using it
  17800. 11:22:52brypt dot we have compare. So how we
  17801. 11:22:56going to compare? Now this is one of the
  17802. 11:22:57most important concept. How we going to
  17803. 11:22:59compare? So you're going to compare with
  17804. 11:23:01this password. This
  17805. 11:23:04password is the password that you are
  17806. 11:23:05sending from the UI side. Okay, means
  17807. 11:23:08this is the actual password user is
  17808. 11:23:10going to type. But then this password
  17809. 11:23:12you're going to compare with which one?
  17810. 11:23:15So you're going to compare with the one
  17811. 11:23:16that is stored in your DB. Okay. So this
  17812. 11:23:19password has easily stored in your DB.
  17813. 11:23:21Correct? So that means if this password
  17814. 11:23:24and user dot password has both are same
  17815. 11:23:27that means this user given correct
  17816. 11:23:29password 1 2 3 4 5 6. But if this user
  17817. 11:23:31given 1 2 3 4 6 5 so that means this
  17818. 11:23:34password is wrong. Okay. So that means
  17819. 11:23:36there will be no match you are getting.
  17820. 11:23:38So if this is the case so now we can
  17821. 11:23:40simply check that if this password valid
  17822. 11:23:43is false. So is password valid is false.
  17823. 11:23:45So in that case I'm just going to throw
  17824. 11:23:49new app error and I'm simply going to do
  17825. 11:23:52401 and I'll do invalid email or
  17826. 11:23:56password. And you notice one thing we
  17827. 11:23:58are simply not directly telling that
  17828. 11:24:00whether your email is invalid or
  17829. 11:24:02password is invalid. Let user guess
  17830. 11:24:03because there is a possibility that
  17831. 11:24:05let's say some hacker uh hackers are
  17832. 11:24:07trying to access right. So they don't
  17833. 11:24:09know that whether the email is invalid
  17834. 11:24:11or password is invalid. So giving a
  17835. 11:24:12general message here. So if this is not
  17836. 11:24:16invalid so I'm going to throw error if
  17837. 11:24:18this is invalid. So now we are going to
  17838. 11:24:20do what? We're going to create a access
  17839. 11:24:21token. Now this is one of the most
  17840. 11:24:23important concept. Okay. So we'll do
  17841. 11:24:25access token and then we are going to
  17842. 11:24:27create a sign access token
  17843. 11:24:31and then what we're going to pass we're
  17844. 11:24:33going to pass some information but
  17845. 11:24:34before that let's create one more
  17846. 11:24:38file here and that we're going to create
  17847. 11:24:40in our leave and I'm just going to do
  17848. 11:24:41jwt.ds DS. Okay. So this is where we are
  17849. 11:24:45going to create our token. Now what
  17850. 11:24:46actually token will consist now this is
  17851. 11:24:48what we have to understand. See your
  17852. 11:24:50[snorts] token is going to have let's
  17853. 11:24:51say you have a user A. Okay. This token
  17854. 11:24:55is going to have a user ID.
  17855. 11:24:58Why? This user ID will be unique. This
  17856. 11:25:01token is going to have a email
  17857. 11:25:03information of that user and this token
  17858. 11:25:06also going to have the role of that
  17859. 11:25:07user. Either this user is a normal user
  17860. 11:25:10or this user is a admin user. Correct.
  17861. 11:25:12Now when we are going to create the
  17862. 11:25:14token and that particular token is going
  17863. 11:25:16to hold all this information. So we will
  17864. 11:25:18be able to understand right that this is
  17865. 11:25:20which user from the user ID what is the
  17866. 11:25:23user email from this email and what is
  17867. 11:25:25the user role from this role. Correct.
  17868. 11:25:27So that means first of all we have to
  17869. 11:25:28create a type here. So I'm just going to
  17870. 11:25:29do export type and I'm just going to do
  17871. 11:25:32token payload. Okay. Or maybe I'm doing
  17872. 11:25:35wrong. Let's go to our types and then
  17873. 11:25:38we'll keep it here only. token payload
  17874. 11:25:41and this is where I'm just going to use
  17875. 11:25:43a user ID. This will be string. Uh this
  17876. 11:25:46also is going to have a email which also
  17877. 11:25:48will be string. This is also going to
  17878. 11:25:50have a role which will be string. So
  17879. 11:25:52save this. Now we'll go here and we're
  17880. 11:25:54going to create our access token. So I'm
  17881. 11:25:55just going to export function. I'll do
  17882. 11:25:58sign access token and this is where I'm
  17883. 11:26:00going to get my payload. Why you need
  17884. 11:26:02payload? Because you need to get the
  17885. 11:26:03user ID, email and password. Correct?
  17886. 11:26:05And this is what you're going to do
  17887. 11:26:06token payload and this is going to
  17888. 11:26:09return a string. So now what we're going
  17889. 11:26:11to do right we're going to create some
  17890. 11:26:12options but before that we need a
  17891. 11:26:15particular package. So which package we
  17892. 11:26:17need? So now let's search here JSON.
  17893. 11:26:20What is JSON web token
  17894. 11:26:27right? So JSON web token is a security
  17895. 11:26:30blah blah between a code structure. JSON
  17896. 11:26:33is a single uh string split into dots
  17897. 11:26:35headers payload that we are going to
  17898. 11:26:37discuss. So first you can see that we
  17899. 11:26:39have to install JSON web token and we
  17900. 11:26:41have this user payload and then based on
  17901. 11:26:43this you have to create a token here.
  17902. 11:26:45Okay first what we're going to do right
  17903. 11:26:47so we'll go here install this. So I'm
  17904. 11:26:50just going to install this one here. So
  17905. 11:26:52go to this I'm just going to do clear
  17906. 11:26:55install
  17907. 11:26:56and also install the types.
  17908. 11:27:02Okay. Now what you have to do, right? So
  17909. 11:27:04this is going to need your secret JWT
  17910. 11:27:07secret. Okay. So here I'm going to copy
  17911. 11:27:09this. You go to your env.
  17912. 11:27:13And then we are going to add one more
  17913. 11:27:14here. So I'll do JWT secret and this
  17914. 11:27:17will be super secret JWT key. Okay. Now
  17915. 11:27:22we can change this one. For now we're
  17916. 11:27:24going to keep it like this. And this one
  17917. 11:27:26first we have to add this. So we're
  17918. 11:27:28going to add this in our env. So we'll
  17919. 11:27:30go here and go to our env. And then I'm
  17920. 11:27:33simply going to take JWT access
  17921. 11:27:38secret. Okay. And this is where I'm just
  17922. 11:27:40going to use this sorry check required
  17923. 11:27:42variables and I'm going to pass this JWT
  17924. 11:27:44secret. Okay. Then I'm also going to
  17925. 11:27:47take very very important. I'm also going
  17926. 11:27:48to add one more here that okay what will
  17927. 11:27:51be the time that till what time this
  17928. 11:27:53particular secret will be accessible. So
  17929. 11:27:55I'm just going to do here. So let's do
  17930. 11:27:57JWT access expires in and let's give
  17931. 11:28:02this one as 15 minutes. Okay. So till 15
  17932. 11:28:05minutes this will be this particular one
  17933. 11:28:07will be available or maybe let's do this
  17934. 11:28:08one 30 for now you can change this one.
  17935. 11:28:11So now what I can do right I can go here
  17936. 11:28:13and then add one more. So I'll do JWT
  17937. 11:28:15access expires in. So this will be check
  17938. 11:28:19this and then we are going to pass the
  17939. 11:28:21JWT access expires in.
  17940. 11:28:25Okay, this is done. So now we can create
  17941. 11:28:27our options. So how we going to create
  17942. 11:28:29options? So first of all, we need to
  17943. 11:28:30import something from JWT package. So
  17944. 11:28:33I'm just going to do import JWT,
  17945. 11:28:38from JSON web token. And what we need?
  17946. 11:28:41We need sign in options. It's called
  17947. 11:28:42sign options. Okay. So now this will be
  17948. 11:28:45my type. So I'm just going to do sign
  17949. 11:28:47options. And here what I have to pass
  17950. 11:28:49it, you have to pass a expired when this
  17951. 11:28:51will be expired. So here I'm going to
  17952. 11:28:52get my env dot you'll see that we have
  17953. 11:28:55this JSON access expires in and here you
  17954. 11:28:58have to go see this is going to give you
  17955. 11:29:00error okay TypeScript error. So here
  17956. 11:29:02you're going to give as and you're going
  17957. 11:29:03to give sign in options and then you
  17958. 11:29:05have to pass the expires in here. Okay.
  17959. 11:29:07And then you are not going to get the
  17960. 11:29:09error and then how you're going to
  17961. 11:29:10create actual token. So to create actual
  17962. 11:29:12token you're going to return JWT and
  17963. 11:29:14this is going to give you the sign
  17964. 11:29:16method here. What you have to pass very
  17965. 11:29:18very important. This is where you're
  17966. 11:29:19going to actually pass the payload and
  17967. 11:29:21this payload is going to hold your user
  17968. 11:29:24information. You're going to pass the
  17969. 11:29:25payload. You also need to pass your JWT
  17970. 11:29:28secret. So I'll do JWT secret. And then
  17971. 11:29:31you're going to pass the options. So
  17972. 11:29:33these options now is going to hold your
  17973. 11:29:35expiry. This is going to hold your
  17974. 11:29:37token. So based on this token and based
  17975. 11:29:39on this user information, it's going to
  17976. 11:29:41create a uh JWT token. Okay. So now
  17977. 11:29:44let's save this. So we go to all service
  17978. 11:29:47to see how much uh sound is it's coming
  17979. 11:29:49right from outside. Can't do anything.
  17980. 11:29:52How many times I'm going to pause and
  17981. 11:29:54redo. It's very uh difficult. Okay. So
  17982. 11:29:57now we're going to create sign in. So
  17983. 11:29:59this is going to receive the token.
  17984. 11:30:01Correct?
  17985. 11:30:02Uh this one we're going to import first
  17986. 11:30:04and this is going to receive payload. In
  17987. 11:30:07this payload now we going to pass the
  17988. 11:30:08user ID. See we getting now from where
  17989. 11:30:10you're going to get the user ID. This
  17990. 11:30:12user ids you're going to get from this
  17991. 11:30:14user information because this
  17992. 11:30:15information is going to give you this
  17993. 11:30:18user information right here. Select all
  17994. 11:30:20of these. So now you [snorts] can go
  17995. 11:30:21here and then I can simply pass user.
  17996. 11:30:25user is going to have your ID email user
  17997. 11:30:29dot email and then role user dot role
  17998. 11:30:34you notice we are not adding the
  17999. 11:30:35password here okay and then at the end
  18000. 11:30:38we're going to now return the access
  18001. 11:30:39token that's it so that means in the end
  18002. 11:30:42we are [clears throat] doing the login
  18003. 11:30:43and we're returning the access token
  18004. 11:30:45save this and now if you go here and
  18005. 11:30:49import this and now you can get the
  18006. 11:30:51access token directly here so do access
  18007. 11:30:53token
  18008. 11:30:57Okay, we are getting error man.
  18009. 11:31:03Oh, value is not used. That's correct.
  18010. 11:31:06So now what we can do right simply we
  18011. 11:31:08are going to return. So do raise dot
  18012. 11:31:10status. So I'll do 200 dot JSON.
  18013. 11:31:15And here let's pass the success will be
  18014. 11:31:19true. And here we're going to pass a
  18015. 11:31:21data. And here I'm going to call this
  18016. 11:31:23access token.
  18017. 11:31:28Okay, still we getting error. Access
  18018. 11:31:30token doesn't exist on promise access
  18019. 11:31:32token. This how come
  18020. 11:31:41I'm doing some silly mistake only. Oh, I
  18021. 11:31:44have to take a man here. See that's
  18022. 11:31:47what. So now this is done. So that means
  18023. 11:31:50we going to get the access token and
  18024. 11:31:51this flow is now completed. Okay. So now
  18025. 11:31:54what will be the next step? Next we have
  18026. 11:31:56to create our uh what is that called
  18027. 11:31:57middleware. But before that let's do one
  18028. 11:31:59thing. Let's check this login flow
  18029. 11:32:01first. So I'm going to go here
  18030. 11:32:06and let's do post and I'll do login
  18031. 11:32:10and let's do here email.
  18032. 11:32:14So I'll do sum at the rate one but I'll
  18033. 11:32:17change this on this and send this email
  18034. 11:32:20and password are required. So here I'm
  18035. 11:32:22just going to take the password and I'll
  18036. 11:32:24do I'll give correct uh password but
  18037. 11:32:26I'll given wrong email. You can see here
  18038. 11:32:28right? So the email is wrong. Send this
  18039. 11:32:30invalid email or password. So now I'm
  18040. 11:32:32going to give correct email but wrong
  18041. 11:32:34password. Send this invalid email or
  18042. 11:32:38password. I'm going to give correct one.
  18043. 11:32:393 4 5 6. Okay. send this and you're
  18044. 11:32:42going to get your success data and this
  18045. 11:32:44is your access token. Okay, this token
  18046. 11:32:47is going to is actually holding all your
  18047. 11:32:49information. This is holding your user
  18048. 11:32:50ID, user email and user role. Okay, so
  18049. 11:32:53this will be the overall flow. Okay. So
  18050. 11:32:55what you should do right instead of
  18051. 11:32:56directly jumping to middleware try to
  18052. 11:32:58understand that what we have done we
  18053. 11:32:59getting the data from our UI we're going
  18054. 11:33:01to services. Okay. We are then checking
  18055. 11:33:05this one. This is very basic validation.
  18056. 11:33:07That's okay. Then we checking the email
  18057. 11:33:09and then we're checking if the email is
  18058. 11:33:11pres sorry user is present. If user is
  18059. 11:33:13not present throw error. If password is
  18060. 11:33:15not not matching throw error. If this is
  18061. 11:33:18not the case then we're going to create
  18062. 11:33:20the token and we are going to returning
  18063. 11:33:21the token here. Okay. So with this now
  18064. 11:33:23let's move to the next one which will be
  18065. 11:33:26now we're going to create our middleware
  18066. 11:33:28and also how you're going to get your
  18067. 11:33:29current user information. So let's do
  18068. 11:33:31this one in the next part. All right. So
  18069. 11:33:33now let's create our middleware. So we
  18070. 11:33:35have to create two middleware. One will
  18071. 11:33:36be our authentication middleware and
  18072. 11:33:38second one will be your admin middleware
  18073. 11:33:40and both are very important. Okay. So
  18074. 11:33:41first let's do one thing. Let's go here
  18075. 11:33:43and let's create the first one. I'm just
  18076. 11:33:45going to do o dot middleware.ts
  18077. 11:33:49ts and also we going to create admin dot
  18078. 11:33:52middleware
  18079. 11:33:54ts. So first in this o middleware what
  18080. 11:33:58we have to do. So first you have to see
  18081. 11:34:00that when we are going to send the token
  18082. 11:34:02right let's say you have a UI correct.
  18083. 11:34:05Now let's say you're doing a API request
  18084. 11:34:07something like this. So how you usually
  18085. 11:34:09send your token right? So you are going
  18086. 11:34:11to send your token in your request
  18087. 11:34:14headers. Now if you remember in the HTTP
  18088. 11:34:17module we have discussed about the
  18089. 11:34:18headers. So in these headers one of the
  18090. 11:34:20headers property will be called
  18091. 11:34:22authorization. Okay. And in this
  18092. 11:34:24authorization we are going to send our
  18093. 11:34:26token. But how we usually send our
  18094. 11:34:27token? We send this particular token as
  18095. 11:34:30a beer token. So it's like this. So you
  18096. 11:34:32have beer B E A R E R and then there
  18097. 11:34:37will be a space and then there will be
  18098. 11:34:40your token. So it will be beer space
  18099. 11:34:44token. Now this beer is going to hold
  18100. 11:34:46your token. So it's like a it's beering
  18101. 11:34:48your token. So it's like a taking your
  18102. 11:34:50token and this token we are going to
  18103. 11:34:51send from our UI side to our back end
  18104. 11:34:54side and back end is going to validate
  18105. 11:34:55this token and based on that we are
  18106. 11:34:57going to understand whether this user is
  18107. 11:34:58a normally user or admin user whether
  18108. 11:35:01this user is authenticated user or non
  18109. 11:35:03unauthenticated user all these things.
  18110. 11:35:05So if you go here and you'll see that be
  18111. 11:35:08error token. So let's see what is what
  18112. 11:35:10it's telling right. So first you'll see
  18113. 11:35:12that it's a security grant the B
  18114. 11:35:15industry standard to handle your JSON
  18115. 11:35:16web token or JWT right. So you have your
  18116. 11:35:19authorization you have your BR and then
  18117. 11:35:21your actual token and you see that there
  18118. 11:35:22is a space. Now this space is important
  18119. 11:35:24because what you're going to do right
  18120. 11:35:26when we'll split it so we are going to
  18121. 11:35:28split it with space. So left side will
  18122. 11:35:31be beer right side will be your token.
  18123. 11:35:33So how we are going to get the token. So
  18124. 11:35:35once you're going to split and take the
  18125. 11:35:37right side of it, we will be getting our
  18126. 11:35:38actual token. Okay. So this is how we
  18127. 11:35:41going to send. So authorization BL and
  18128. 11:35:43this is your token. So first what we're
  18129. 11:35:45going to do, right? So I'm just going to
  18130. 11:35:46do export function and I'll do
  18131. 11:35:48authentication or authenticate. I think
  18132. 11:35:50let's keep authenticate. Here we're
  18133. 11:35:52going to get our request. Now this
  18134. 11:35:54request will be
  18135. 11:35:56so I'm just going to do import from
  18136. 11:35:59express. So here we're going to get
  18137. 11:36:01request. We're going to get response and
  18138. 11:36:03we are also going to get
  18139. 11:36:06next function. So request will be our
  18140. 11:36:09request. Then we're going to get our
  18141. 11:36:11response which will be our response and
  18142. 11:36:13then we're going to have our next very
  18143. 11:36:15very important which will be our next
  18144. 11:36:17function. And here we're going to have a
  18145. 11:36:19void. So first we are going to get our
  18146. 11:36:21header. So I'm just going to do all
  18147. 11:36:23header and this will be your request dot
  18148. 11:36:26headers and then you're going to have
  18149. 11:36:28your authentication. So you request
  18150. 11:36:30header dot authentication. So you can
  18151. 11:36:32see that right. So not authentication
  18152. 11:36:34authorization request do header dot
  18153. 11:36:36authorization. This is what actually
  18154. 11:36:38it's telling here. Okay. Now what I'm
  18155. 11:36:40going to do? So I'm simply going to do
  18156. 11:36:42if not equals this o header dot starts
  18157. 11:36:46with if this is not started with B
  18158. 11:36:48error. So that means there is a wrong
  18159. 11:36:50because it will always start with B
  18160. 11:36:52error. So you can see that. So it's
  18161. 11:36:53simply checking the if this token is not
  18162. 11:36:55started with B error that means you're
  18163. 11:36:56sending a wrong format from your UI side
  18164. 11:36:59and also this will be space right so B
  18165. 11:37:01error space so in that case what you're
  18166. 11:37:03going to do you're going to call next
  18167. 11:37:04middleware then you're going to pass a
  18168. 11:37:06new app error and then you're going to
  18169. 11:37:09pass a 401 so whenever there is a
  18170. 11:37:11authentication related thing you're
  18171. 11:37:13going to send a 401 and this will be
  18172. 11:37:15access token is required
  18173. 11:37:19so this is the first thing and then
  18174. 11:37:20you're going to return then we're going
  18175. 11:37:23to get our actual token. Okay. So, how
  18176. 11:37:25we going to get our actual token? So,
  18177. 11:37:26we're going to do token and this will be
  18178. 11:37:29so we're going to get o header and now
  18179. 11:37:30we're going to do I already mentioned
  18180. 11:37:32we're going to split. So, we are going
  18181. 11:37:34to split with what space and then we're
  18182. 11:37:36going to take the right side right. So,
  18183. 11:37:38split means zeroth and this is first
  18184. 11:37:40one. So, we're going to do split and
  18185. 11:37:42then we are going to do with space and
  18186. 11:37:44then I'm going to take the first one.
  18187. 11:37:46So, this is going to give me my actual
  18188. 11:37:47token the right side of it. And now what
  18189. 11:37:50we have to do now this is actually very
  18190. 11:37:52important. Okay, now this is extremely
  18191. 11:37:54important. I'm going to show you one
  18192. 11:37:55very interesting thing. You'll see that
  18193. 11:37:56if I just do request dot user, right?
  18194. 11:38:00And now you'll see that this user is not
  18195. 11:38:02present. And this makes sense because
  18196. 11:38:04this request which is a type of request,
  18197. 11:38:07this is not holding any user
  18198. 11:38:09information. So that means we need to
  18199. 11:38:11some inform express that okay in my
  18200. 11:38:14request object, I also need to include
  18201. 11:38:16this user object here. But how we going
  18202. 11:38:19to do this? You're getting what I'm
  18203. 11:38:20talking about means this will give you a
  18204. 11:38:22typescript error because this user
  18205. 11:38:23doesn't exist. So that means to inform
  18206. 11:38:26express that okay you need to add or you
  18207. 11:38:28need to append this user information in
  18208. 11:38:30your request object. So how you going to
  18209. 11:38:32do this? So go to types and then I'm
  18210. 11:38:35just going to create express dot
  18211. 11:38:38uh d.ts. So I'm going to create a global
  18212. 11:38:41express and uh so first I'm going to
  18213. 11:38:44declare uh express and here I'm simply
  18214. 11:38:47going to append that one uh that
  18215. 11:38:49particular
  18216. 11:38:51sorry not express we are going to
  18217. 11:38:52declare global
  18218. 11:38:54and here inside this I'm going to take a
  18219. 11:38:56name space which will be express
  18220. 11:38:59and inside this we're going to take our
  18221. 11:39:01interface and in our interface will be
  18222. 11:39:03our interface of this request right so
  18223. 11:39:06we have this request so simply we're
  18224. 11:39:07telling express that you take your
  18225. 11:39:09request object object and in this
  18226. 11:39:11request object also append this user so
  18227. 11:39:13that I can access this request do user.
  18228. 11:39:15So now here we are going to submit a
  18229. 11:39:17login this request interface I am going
  18230. 11:39:20to add a user here and this will be
  18231. 11:39:21optional and this user will be the token
  18232. 11:39:23payload that we have created. This token
  18233. 11:39:25payload is going to hold your user ID
  18234. 11:39:27mail and role and you'll see that now
  18235. 11:39:28you're not getting this error because
  18236. 11:39:30this express is now holding. So express
  18237. 11:39:32dot request do user equal to token
  18238. 11:39:34payload you're getting. So that means
  18239. 11:39:36this user is now appended in your
  18240. 11:39:37request object. And then you're going to
  18241. 11:39:39just do export this. That's it. So now
  18242. 11:39:43this request do user what this value
  18243. 11:39:45will be. So this will be so this request
  18244. 11:39:47do user will be nothing but here we are
  18245. 11:39:49now going to verify our access token. So
  18246. 11:39:51I'm just going to do verify access token
  18247. 11:39:53and you're going to pass this token here
  18248. 11:39:55because you need to verify it. You just
  18249. 11:39:57can't uh use any random token and then
  18250. 11:40:01you can give user uh access. So we'll go
  18251. 11:40:04to our JWT and then I'm just simply
  18252. 11:40:06going to do export function and I'll do
  18253. 11:40:09verify access token. We're going to get
  18254. 11:40:12the token which will be string and here
  18255. 11:40:14we're going to get our token payload.
  18256. 11:40:16And now to do this we're simply going to
  18257. 11:40:18do a try catch.
  18258. 11:40:21So here I'm simply going to throw a new
  18259. 11:40:23app error 401 and I'll do invalid or
  18260. 11:40:27expired
  18261. 11:40:29access token.
  18262. 11:40:31And in this try now I'm simply going to
  18263. 11:40:33do return.
  18264. 11:40:35So we do JWT dot I think it's called
  18265. 11:40:39verify. And here you're going to pass
  18266. 11:40:41the token. But how you're going to
  18267. 11:40:42verify? So you have remember in the env
  18268. 11:40:45you have this JWT access secret. And
  18269. 11:40:47based on it it's going to verify. Okay.
  18270. 11:40:50And here you're going to simply pass as
  18271. 11:40:52so do token payload. And this is done.
  18272. 11:40:54So now this method is going to verify
  18273. 11:40:56your JWT token. And this is what we
  18274. 11:40:59actually verifying here. Okay. And then
  18275. 11:41:01what you have to do, can you guess what
  18276. 11:41:02you have to do here? Extremely important
  18277. 11:41:05concept. Extremely important. So you
  18278. 11:41:07have to always call your next
  18279. 11:41:08middleware. Remember I already explained
  18280. 11:41:10this one. If you don't call this next
  18281. 11:41:12middleware, it will always stuck in
  18282. 11:41:14middle. Okay, it will never call your
  18283. 11:41:16next handler or it will never call your
  18284. 11:41:18next middleware. So that means now this
  18285. 11:41:20particular authenticate is a middleware
  18286. 11:41:23which is going to check every request
  18287. 11:41:26whenever you're going to use this
  18288. 11:41:27middleware that whether the user is a
  18289. 11:41:29authenticated user or not. Now to check
  18290. 11:41:32the admin uh middleware how we are going
  18291. 11:41:34to do this one. So first of all let's do
  18292. 11:41:36one thing. Go here. I'm going to just
  18293. 11:41:39copy this paste it here. So I'm simply
  18294. 11:41:41going to do export function
  18295. 11:41:44require
  18296. 11:41:45admin.
  18297. 11:41:47So I'm going to take request which will
  18298. 11:41:49be request response which will be
  18299. 11:41:52response
  18300. 11:41:53next which will be next function
  18301. 11:41:57and this will void. So simply I'm just
  18302. 11:41:59going to check if this request do user
  18303. 11:42:02I'm going to have user dot role. If this
  18304. 11:42:05is not admin right if this user is not
  18305. 11:42:08admin user so that means we're simply
  18306. 11:42:09going to return next and this will be
  18307. 11:42:12new app error and I'm going to return
  18308. 11:42:14403 and I'm simply do that admin access
  18309. 11:42:18required
  18310. 11:42:20you don't have do not have uh admin
  18311. 11:42:24access it seems something like this and
  18312. 11:42:28then I'm simply going to return this and
  18313. 11:42:31then what you have to do here again you
  18314. 11:42:33have to call the next Okay. Always you
  18315. 11:42:35have to call the next or else it will be
  18316. 11:42:37stuck. Now I think this is done. So what
  18317. 11:42:40will be the next? Next will be now let's
  18318. 11:42:42do one thing. Let's go to our o routes
  18319. 11:42:45and then I'm going to create one more
  18320. 11:42:47route here. So what I need to do right I
  18321. 11:42:49wanted to get my current user
  18322. 11:42:52information. How you going to get
  18323. 11:42:54current user information? Until unless
  18324. 11:42:55you log in you don't have the token and
  18325. 11:42:57from the token you'll be able to get the
  18326. 11:42:59user information. So I'm simply going to
  18327. 11:43:00do O router dot get
  18328. 11:43:04and then I'm simply going to do me.
  18329. 11:43:06Okay. And here for now I'm just going to
  18330. 11:43:08do request response. Now this is not
  18331. 11:43:10correct. It's not going to work but I
  18332. 11:43:12wanted to show you that only. So ludase
  18333. 11:43:14dot status and I'm simply going to pass
  18334. 11:43:16200 dot JSON and here what I'm going to
  18335. 11:43:19do right I'm simply going to do success
  18336. 11:43:23will be true and here I'm going to pass
  18337. 11:43:25a data and here I'm going to do user and
  18338. 11:43:28this will be request and you're going to
  18339. 11:43:29have this user correct save this and
  18340. 11:43:32you'll see that this is not going to
  18341. 11:43:33work and this is where you're going to
  18342. 11:43:35understand one very important concept
  18343. 11:43:36and that is how you can protect your
  18344. 11:43:39routes
  18345. 11:43:41save this so now I think We don't have
  18346. 11:43:43any error. So let's go here. Uh copy
  18347. 11:43:46this
  18348. 11:43:48get and this will be / API/og/me
  18349. 11:43:51and I'm going to just do send here.
  18350. 11:43:54You're not getting any error. Sorry not
  18351. 11:43:55getting any error but you're not getting
  18352. 11:43:57any data. Why? First of all until unless
  18353. 11:44:00you have the token. So here we don't
  18354. 11:44:02know right if the user is logged in user
  18355. 11:44:04or not. So how you're going to get user
  18356. 11:44:05information? So that means what you have
  18357. 11:44:07to do right in your authorization you
  18358. 11:44:09have to actually pass your token. So
  18359. 11:44:11let's say I'm going to pass a beer token
  18360. 11:44:12here. I have to pass the token and then
  18361. 11:44:14I also need to protect this particular
  18362. 11:44:16router. How I'm going to protect? So
  18363. 11:44:18this is where I need to use middleware.
  18364. 11:44:20So middleware here is going to protect.
  18365. 11:44:22So it's going to check if the user is
  18366. 11:44:24authenticated user or not. So now to
  18367. 11:44:26check this what you have to do in middle
  18368. 11:44:28you just have to call that authenticate
  18369. 11:44:30middleware. That's it. Now this is now
  18370. 11:44:32is going to work as a barrier. So that
  18371. 11:44:34means when it's going to do here it's
  18372. 11:44:36going to check this middleware. If this
  18373. 11:44:38is passed then it's going to run this.
  18374. 11:44:40If this is fail so it's going to return
  18375. 11:44:42error. For example, if you go here right
  18376. 11:44:44and let's say first of all save this and
  18377. 11:44:48now let's say if I run that particular
  18378. 11:44:50from the postman you'll see that you're
  18379. 11:44:52not going to get this response. What
  18380. 11:44:54you'll get right you'll get B token is
  18381. 11:44:56needed or access token is needed. Now
  18382. 11:44:58see what will happen if I go here and
  18383. 11:45:01send this see access token is required.
  18384. 11:45:03So that means now this particular route
  18385. 11:45:06is protected. Okay, protected by whom?
  18386. 11:45:08Protected by this middleware. So if I go
  18387. 11:45:10to middleware, it's checking okay, you
  18388. 11:45:12don't have any beer token. So it's going
  18389. 11:45:15to return this. If it will have beer
  18390. 11:45:17token, then it's going to verify the
  18391. 11:45:18token and then you're going to get your
  18392. 11:45:20user information. I hope this is clear.
  18393. 11:45:22So now what I'll do right, I'll go here
  18394. 11:45:24login. I'm going to send this. I'm going
  18395. 11:45:26to get the token. Copy this token.
  18396. 11:45:29Go to this. Go to uh headers. And then
  18397. 11:45:32I'm going to add one header. Okay. So
  18398. 11:45:34let's add here authorization. And here
  18399. 11:45:36I'm just going to do beer, right? So be
  18400. 11:45:39error and then paste this. And then if I
  18401. 11:45:41send this, that's it. So now you're
  18402. 11:45:43getting your user, user ID, email, role,
  18403. 11:45:46and the rest of the expiry blah blah.
  18404. 11:45:49But this is the important. So you're
  18405. 11:45:50getting user, you're getting what I'm
  18406. 11:45:52talking about. So this is how you're
  18407. 11:45:53going to implement the middleware. Now
  18408. 11:45:55this token is actually hold this
  18409. 11:45:58information here. Why? Because when
  18410. 11:46:00you're doing verifying, right, is
  18411. 11:46:01actually doing the verification here.
  18412. 11:46:03Correct? And based on it, it's going to
  18413. 11:46:05return also another very important
  18414. 11:46:07concept. Uh where is that? Let's go to
  18415. 11:46:10that
  18416. 11:46:11o routes not routes service.
  18417. 11:46:17Uh see this is where we are actually not
  18418. 11:46:20this one uh
  18419. 11:46:28uh this part only I'm talking about. So
  18420. 11:46:29request dot user equal to verify access
  18421. 11:46:32token right and here what we doing we're
  18422. 11:46:34returning this return verify this is
  18423. 11:46:36what I'm talking about. So actually what
  18424. 11:46:37we doing first we are verifying the
  18425. 11:46:39token if that is correct then only we
  18426. 11:46:41are doing what we are attaching that
  18427. 11:46:43user information in our request object.
  18428. 11:46:45So this one in our request object. So
  18429. 11:46:47that is the reason you're getting this
  18430. 11:46:48information. The moment you will just
  18431. 11:46:50change this token. Let's say if I do uh
  18432. 11:46:52maybe let's do P to A. Okay. And send
  18433. 11:46:55this invalid or expired access token.
  18434. 11:46:58See it will work right and then if you
  18435. 11:47:01let's say copy this again from here
  18436. 11:47:03let's say I'm going to copy this and
  18437. 11:47:04then again replace here you'll see that
  18438. 11:47:06you're going to get the result. So send
  18439. 11:47:09this. That's it. So this is how you're
  18440. 11:47:12going to protect. Okay. Now we haven't
  18441. 11:47:14used this admin middleware because this
  18442. 11:47:16we're going to use later but for now
  18443. 11:47:18let's keep it like this with this. Now
  18444. 11:47:19let's move to the next one. What will be
  18445. 11:47:21the next part? Next part will be we are
  18446. 11:47:22going to implement cloud right cloud
  18447. 11:47:25means how we going to create update
  18448. 11:47:26delete and all those things. And
  18449. 11:47:27remember one thing here I wanted to
  18450. 11:47:29mention before I know some of you are
  18451. 11:47:31going to comment that okay in the O
  18452. 11:47:32routes or O service we haven't
  18453. 11:47:34implemented cookies where is log out and
  18454. 11:47:36all of this we're not going to have all
  18455. 11:47:38of this in this particular video and the
  18456. 11:47:39reason is very simple if you want to
  18457. 11:47:41learn all of this I already have a video
  18458. 11:47:43for this or else wait for the next part
  18459. 11:47:45in the next part we are going to have
  18460. 11:47:46the rest of the topics that I already
  18461. 11:47:48mentioned some of the things that I
  18462. 11:47:50already mentioned in the previous core
  18463. 11:47:51modules that we are going to have child
  18464. 11:47:53process worker threads then some of the
  18465. 11:47:55advanced topics uh here we are going to
  18466. 11:47:58implement some advanced level of uh
  18467. 11:48:00middle so not middleware means login
  18468. 11:48:03flow email verification all all those
  18469. 11:48:05things it's not possible to include each
  18470. 11:48:07and everything here so I wanted to
  18471. 11:48:08mention but for a beginner or even to
  18472. 11:48:10complete a project this is more than
  18473. 11:48:12enough so first let's implement our CRUD
  18474. 11:48:14we are going to understand RBSC means
  18475. 11:48:16role access control we are going to
  18476. 11:48:18implement our admin side logic and then
  18477. 11:48:20we'll move to file upload because there
  18478. 11:48:21are still so many things are left so
  18479. 11:48:23with this now let's move on to the next
  18480. 11:48:24one all right so now what I going to do
  18481. 11:48:26right So we start implementing our CRUD.
  18482. 11:48:28CRUD means create, read, update and
  18483. 11:48:31delete. Okay. Now first of all, let's go
  18484. 11:48:33to types and we will start with creating
  18485. 11:48:35our task uh type. So task.ts.
  18486. 11:48:39Let's do this one as export type. Uh
  18487. 11:48:42let's do task. So for your task, you're
  18488. 11:48:44going to have a ID. So this will be
  18489. 11:48:46string. You're also going to have
  18490. 11:48:49task title which also will be string.
  18491. 11:48:52You're also going to have a status. Now
  18492. 11:48:54this will be string. And also let's see
  18493. 11:48:56what else we have. Title status user id
  18494. 11:48:59created and updated at. Right? So we'll
  18495. 11:49:01copy this. So here we are going to have
  18496. 11:49:03user id. This also will be string
  18497. 11:49:06created at. This will be date. And then
  18498. 11:49:09we are also going to have the updated at
  18499. 11:49:12which also will be date. So this will be
  18500. 11:49:14our type. So now first of all let's
  18501. 11:49:16close everything. So let's see how many
  18502. 11:49:19files first of all we need because we
  18503. 11:49:21need to first check the flow and
  18504. 11:49:24everything. So first of all as a user
  18505. 11:49:26the first requirement will be this user
  18506. 11:49:28has to be logged in user. So that means
  18507. 11:49:31if you haven't logged in you will not be
  18508. 11:49:33able to create any task. So that that is
  18509. 11:49:35the first requirement. Then user will be
  18510. 11:49:37able to create task. So this will be
  18511. 11:49:40first requirement. User can only see the
  18512. 11:49:44task created by that user only. I think
  18513. 11:49:46that part we have already covered
  18514. 11:49:48previously. User can update their task
  18515. 11:49:50and user can delete their task. So four
  18516. 11:49:53uh routes. Okay, from admin side what
  18517. 11:49:57are you going to do? Right, admin will
  18518. 11:49:58be able to see all the task created by
  18519. 11:50:01all the users. So that means let's say I
  18520. 11:50:02have two users. First user created 10
  18521. 11:50:05task, second user created five task. So
  18522. 11:50:07admin will be able to see all these 15
  18523. 11:50:09task together. Okay. And second admin
  18524. 11:50:12only will be able to update task status.
  18525. 11:50:16So admin can update task uh status. Uh
  18526. 11:50:19user can update uh their task title
  18527. 11:50:22something like this. So first of all
  18528. 11:50:23what we going to do right we need to see
  18529. 11:50:25how many repository file we need. So
  18530. 11:50:26first let's start with uh repository. So
  18531. 11:50:29here I'm just going to create uh maybe
  18532. 11:50:31let's do this one as either we can
  18533. 11:50:34create only let's do user dot task dot
  18534. 11:50:38repository.
  18535. 11:50:42And then I'm also going to create one
  18536. 11:50:43more. So this will be admin.task
  18537. 11:50:46repository.ts.
  18538. 11:50:48So one for user one for admin. Correct.
  18539. 11:50:52Then we are also going to need our task
  18540. 11:50:54services. So we'll go to our services.
  18541. 11:50:56I'm going to create user dot task dots
  18542. 11:51:00service.ts.
  18543. 11:51:02Then I'll do admin.task dots service dot
  18544. 11:51:06ts. Then we need our routes. Correct. So
  18545. 11:51:10in the routes we are going to have two
  18546. 11:51:11routes only. Uh the first one will be
  18547. 11:51:13same. So we do user.task dot routes. DS
  18548. 11:51:18and I'll do admin.task.outs.
  18549. 11:51:21is. So then first of all let's start
  18550. 11:51:24with again from our routes file. So
  18551. 11:51:26let's close everything else go to index
  18552. 11:51:29and let's create our first uh route
  18553. 11:51:31which will be our task route for user.
  18554. 11:51:34So here the first of all let's import
  18555. 11:51:36okay then we are going to do so first
  18556. 11:51:38import something from
  18557. 11:51:40uh express
  18558. 11:51:42and here we are going to first take I'll
  18559. 11:51:45do export const and this will be our
  18560. 11:51:48user task router. Okay. And this will be
  18561. 11:51:51our router. Now first what we have to
  18562. 11:51:53see right that all the task whatever
  18563. 11:51:56operation that user is going to do so
  18564. 11:51:59that user has to be logged in user. So
  18565. 11:52:01that means whatever routes we are going
  18566. 11:52:02to have in this particular file all
  18567. 11:52:04routes has to be protected by our
  18568. 11:52:06authenticated middleware. Authenticate
  18569. 11:52:08middleware you're getting. So this one
  18570. 11:52:10[snorts] we are going to understand uh
  18571. 11:52:11in a minute. First let's do one thing.
  18572. 11:52:12So I'm just going to do here uh let's go
  18573. 11:52:15here. Right. So we'll go to our index.ts
  18574. 11:52:17and here what are you going to do? So
  18575. 11:52:19first of all you have to use this one.
  18576. 11:52:20So I'll do app router
  18577. 11:52:23sorry API router dot use and here let's
  18578. 11:52:26give this one as uh let's do tasks and
  18579. 11:52:30here I'm just going to do user task
  18580. 11:52:32router. So that is the first thing. Now
  18581. 11:52:34I'll go here. First of all we going to
  18582. 11:52:36create our first route which will be how
  18583. 11:52:38we are going to create one particular
  18584. 11:52:40task. So I'm just going to do task user
  18585. 11:52:43task router dot post. Let's give this
  18586. 11:52:47one slash. So this will be our home
  18587. 11:52:49route and here we're going to get a
  18588. 11:52:51sync. So do request response and next
  18589. 11:52:56and here so let's take our try catch.
  18590. 11:53:01So you error
  18591. 11:53:03and then we are going to call the next
  18592. 11:53:05without error. So that is fine. But here
  18593. 11:53:08what we are going to do right so first
  18594. 11:53:09I'm just going to do con task and I'm
  18595. 11:53:12just going to have it. So now we are
  18596. 11:53:14going to create our service. So we'll go
  18597. 11:53:15to our user service and here we're going
  18598. 11:53:17to have our first service which will be
  18599. 11:53:19create user task and what we are going
  18600. 11:53:23to pass here. So first of all we're
  18601. 11:53:24going to pass two things. Now you need
  18602. 11:53:26to understand a couple of things here.
  18603. 11:53:27Okay as I already mentioned that this
  18604. 11:53:29router this route has to be protected.
  18605. 11:53:31So here there are two ways you can do
  18606. 11:53:34this. Now this is one of the most
  18607. 11:53:35important concept you have to
  18608. 11:53:36understand. First of all let's
  18609. 11:53:37understand you have two routes here. One
  18610. 11:53:40route has to be protected and other
  18611. 11:53:41routes not. So in that case what you can
  18612. 11:53:43do right you can use this authenticate
  18613. 11:53:45middleware that we have on the route
  18614. 11:53:47level only why because you can't use
  18615. 11:53:50this one on the page level sorry on the
  18616. 11:53:51file level because let's say you have
  18617. 11:53:53one more here
  18618. 11:53:55right you have one more and this
  18619. 11:53:57particular route don't need this
  18620. 11:53:58authenticate so in that case what you
  18621. 11:54:00can do you can use this one individually
  18622. 11:54:01you're getting but let's say now you
  18623. 11:54:03have one more route and this route also
  18624. 11:54:05need authenticate middleware so in that
  18625. 11:54:07case instead of doing this one two times
  18626. 11:54:09so one time here again one time here we
  18627. 11:54:12can do something like this. We can use
  18628. 11:54:13this one on the file level. Okay. So
  18629. 11:54:16what I mean by this now if I go here and
  18630. 11:54:18I'm simply going to tell okay use a task
  18631. 11:54:20router dot use this authenticate. So
  18632. 11:54:23simply we are telling that whatever
  18633. 11:54:25routes I'm going to have in this
  18634. 11:54:26particular file it can be two routes 10
  18635. 11:54:28routes 20 routes all the routes will be
  18636. 11:54:31now protected by the middleware. So now
  18637. 11:54:34what you have to do right you have to
  18638. 11:54:35use this one on the page level top top
  18639. 11:54:37file. So not top file on the file on the
  18640. 11:54:39top on the top you have to use. So
  18641. 11:54:42whatever you are going to use after this
  18642. 11:54:44all these routes so for example this
  18643. 11:54:45route this route and you have let's say
  18644. 11:54:4750 more routes all will be protected by
  18645. 11:54:50authenticate authenticate middleware.
  18646. 11:54:52Okay so that means all these routes are
  18647. 11:54:54now protected. So all the routes below
  18648. 11:54:58are now protected as simple as that. So
  18649. 11:55:01this is how you can you can use or you
  18650. 11:55:03can protect routes. As I already
  18651. 11:55:05mentioned, I'm going to repeat once
  18652. 11:55:06again. Let's say the requirement is you
  18653. 11:55:08have to protect this one. You don't have
  18654. 11:55:10to protect then you can't use this one
  18655. 11:55:12because this will apply for all the
  18656. 11:55:13routes. So in that case, you have to use
  18657. 11:55:14this one on the route level. Something
  18658. 11:55:16like this. So for this one, you need
  18659. 11:55:18this for this you don't and then you
  18660. 11:55:19have to remove this one. Okay. So for
  18661. 11:55:21now because we need this particular
  18662. 11:55:23authenticate middleware for all the
  18663. 11:55:25routes, we are not going to use this one
  18664. 11:55:27on the route level. Okay. So now let's
  18665. 11:55:29remove this. So first of all, we are
  18666. 11:55:31going to create our user task. Correct.
  18667. 11:55:33So now we'll go to our service file and
  18668. 11:55:35let's create our first service. So here
  18669. 11:55:37I'm just going to do export async
  18670. 11:55:40function and this will be create user
  18671. 11:55:42task. What we need right? So first of
  18672. 11:55:44all we need to know that which user
  18673. 11:55:46actually creating this particular task
  18674. 11:55:47and that is the reason we have this uh
  18675. 11:55:50user ID right. So we have this user ID.
  18676. 11:55:52So that means this one needs to have the
  18677. 11:55:54user ID correct. So we are going to need
  18678. 11:55:56our user ID. So user ID will be string
  18679. 11:55:58and also we need the title. So title and
  18680. 11:56:01this will be unknown. Now here what's
  18681. 11:56:04this is going to return? So this is
  18682. 11:56:05going to return promise and it's going
  18683. 11:56:07to return task type that we have created
  18684. 11:56:09in our task uh in the types file. Okay,
  18685. 11:56:11remember we have created this one. So
  18686. 11:56:13now what we going to do right? So first
  18687. 11:56:14of all let's do validation. So first
  18688. 11:56:17we're going to do con
  18689. 11:56:19and let's do valid title. We are only
  18690. 11:56:23checking the title here and this will be
  18691. 11:56:25let's create one function. So I'm just
  18692. 11:56:27going to do function and I'll do
  18693. 11:56:28validate or valid title. Now let's do
  18694. 11:56:31validate title and this is going to uh
  18695. 11:56:34receive your title. This will be unknown
  18696. 11:56:37and here you're going to use string. Now
  18697. 11:56:39as I already mentioned we are actually
  18698. 11:56:41going to use zord only but here you also
  18699. 11:56:44need to know how you can implement all
  18700. 11:56:45of this manually. So first I'm going to
  18701. 11:56:48use this validate title. We are going to
  18702. 11:56:50pass this user ID right not user ID
  18703. 11:56:52title. So here what I'm going to do
  18704. 11:56:54simply I'm going to check that if this
  18705. 11:56:56type of title
  18706. 11:56:59if this is not equal to equal to string
  18707. 11:57:03or if this is false so that means if
  18708. 11:57:06title dot trim
  18709. 11:57:08in that case we are going to throw new
  18710. 11:57:11app error and we're going to tell that
  18711. 11:57:13okay this is 400 and I'm simply going to
  18712. 11:57:15do title is required okay and now I'm
  18713. 11:57:20simply going to do con trimmed title
  18714. 11:57:23And this will be title dot trim.
  18715. 11:57:27And then what you can do right that you
  18716. 11:57:28can also give some condition. For
  18717. 11:57:29example, let's say I wanted to check if
  18718. 11:57:31this trimmed title.length
  18719. 11:57:34if this is greater than maybe let's give
  18720. 11:57:36a max title length of let's say 150 or
  18721. 11:57:39100 something like this. So in that case
  18722. 11:57:40also we can throw one more error. So
  18723. 11:57:42I'll do throw new app error and I'm
  18724. 11:57:44simply going to do 400. And I'll do
  18725. 11:57:47title must be 100
  18726. 11:57:50characters or less. Right? Now this also
  18727. 11:57:54you can put this one in a constant file.
  18728. 11:57:56Remember I told you to keep this one as
  18729. 11:57:57a homework. So that you can do and then
  18730. 11:57:59we are going to return the streamed
  18731. 11:58:01title. Okay? So now this is going to
  18732. 11:58:03validate title. Now what we're going to
  18733. 11:58:05do right we are simply going to return
  18734. 11:58:07and we are actually going to now
  18735. 11:58:09interact with our DB. Now when I'm
  18736. 11:58:10talking about interact with our DB so
  18737. 11:58:13that means we need to at the end save
  18738. 11:58:15this particular type support task in our
  18739. 11:58:17DB correct so now we have to go to our
  18740. 11:58:20repository file so we'll go to user.task
  18741. 11:58:23task dot repository and we'll create our
  18742. 11:58:25first function. So I'll do export async
  18743. 11:58:29function and I'll do create task and
  18744. 11:58:32here we are going to do user ID. We need
  18745. 11:58:34the user id that which user is actually
  18746. 11:58:36creating. So this will be string we also
  18747. 11:58:38need the title of that task and this
  18748. 11:58:40also will be string and here what will
  18749. 11:58:43be the return type return type will be
  18750. 11:58:44promise of this task.
  18751. 11:58:47So now we are going to use this here. So
  18752. 11:58:49this will be our return and you can do
  18753. 11:58:51create task and what this is going to
  18754. 11:58:53receive first of all let's import this
  18755. 11:58:55and this is going to receive your user
  18756. 11:58:57ID and title. So I'm just going to pass
  18757. 11:58:59the user ID and I'm going to pass a
  18758. 11:59:01valid title. Okay. So save this. So now
  18759. 11:59:04what we have to do here? So simply going
  18760. 11:59:06to create our SQL uh we have to write
  18761. 11:59:09our query. So cool dot query and here
  18762. 11:59:13you can do this one as task only. If you
  18763. 11:59:16want you can also do like this. So you
  18764. 11:59:18can do a type and let's do task row and
  18765. 11:59:20this will be equal to this task and you
  18766. 11:59:22can pass this also. Remember I told you
  18767. 11:59:23that the DV for format and the actual
  18768. 11:59:26format that you want to to return that
  18769. 11:59:28that can be different but in this case
  18770. 11:59:29it's same. So I'm just simply taking
  18771. 11:59:31this only and here now I'm just going to
  18772. 11:59:33write so what we have to write so we are
  18773. 11:59:36going to insert. So I'm just going to do
  18774. 11:59:38insert into and let's check our uh so
  18775. 11:59:42this will be support task. This is where
  18776. 11:59:44we are inserting this our table. What
  18777. 11:59:46are you going to insert? You're going to
  18778. 11:59:47insert title and also you're going to
  18779. 11:59:49insert user ID. Correct? Then I'm just
  18780. 11:59:52going to do values. So values will be
  18781. 11:59:54one and two. And then I wanted to do a
  18782. 11:59:58returning.
  18783. 12:00:00So I'll return ID. I wanted to return
  18784. 12:00:02the title. I wanted to return the
  18785. 12:00:03status. Why we are not taking the
  18786. 12:00:05status? Because by default status will
  18787. 12:00:07be open. Okay. So you take status. Then
  18788. 12:00:10I wanted to return the user ID.
  18789. 12:00:13Then I want to uh return the create edit
  18790. 12:00:15and update.
  18791. 12:00:20That's it. And then uh what I have to
  18792. 12:00:23do? So here after this I'm going to pass
  18793. 12:00:26the title
  18794. 12:00:29and the user ID.
  18795. 12:00:31So I think this is done. And at the end
  18796. 12:00:33we're going to return result dot rows of
  18797. 12:00:37zero.
  18798. 12:00:38So save this. So I think this is done.
  18799. 12:00:41So now if we go here so what you have to
  18800. 12:00:44use we have to use this create task user
  18801. 12:00:46task here. So I'm going to import from
  18802. 12:00:47our service and this is going to receive
  18803. 12:00:50the user ID and the title. Now from
  18804. 12:00:52where you're going to pass this user ID.
  18805. 12:00:54Now this is where you have to understand
  18806. 12:00:55from this request remember we have this
  18807. 12:00:57request and this request is going to
  18808. 12:00:59have your user information and this is
  18809. 12:01:01going to give you the user ID. Now this
  18810. 12:01:04we are going to actually get remember
  18811. 12:01:05when we have validated the token right
  18812. 12:01:08that time we are attaching this user
  18813. 12:01:09information in our request object. So
  18814. 12:01:11from where we'll be able to get this
  18815. 12:01:12user id and then this will be request
  18816. 12:01:14dot body dot title
  18817. 12:01:18and now what you can do you can simply
  18818. 12:01:20pass a dot status of 201
  18819. 12:01:23JSON and here I'm just going to do
  18820. 12:01:25success
  18821. 12:01:27will be true and let's pass a data. So
  18822. 12:01:30data will be this task that we have
  18823. 12:01:32created. Save this. Now let's run it
  18824. 12:01:35once again. See that this is running. So
  18825. 12:01:38now first of all let's go here. Okay. So
  18826. 12:01:39I'm just going to copy this.
  18827. 12:01:42So I'll do post and I'll paste it. So
  18828. 12:01:45this will be app API/ what is the route
  18829. 12:01:48name that we are using. So let's check
  18830. 12:01:51routes routes index. So we'll do / task.
  18831. 12:01:55So / API/ task. Why? Because if you go
  18832. 12:01:59to user task we are using only slash
  18833. 12:02:01right so this will be / task and this
  18834. 12:02:03will be post request we go to body json
  18835. 12:02:05and let's I'm going to pass the title
  18836. 12:02:08right so we have the title
  18837. 12:02:12and this will be first
  18838. 12:02:15support task correct let's try to send
  18839. 12:02:18this you can see access token is
  18840. 12:02:20required now from where you are actually
  18841. 12:02:22getting you have to understand all this
  18842. 12:02:24see because of this line you are getting
  18843. 12:02:25this this authenticate is now is going
  18844. 12:02:28to protect all the routes. So if you go
  18845. 12:02:29to this authenticate, you'll see that
  18846. 12:02:31it's simply checking whether you have
  18847. 12:02:32the access token or not. So this is
  18848. 12:02:34going to block the request. Correct? So
  18849. 12:02:36now what you have to do right, you have
  18850. 12:02:37to go to our login. Let's send this.
  18851. 12:02:39We're going to get the token. So copy
  18852. 12:02:41this token.
  18853. 12:02:45Let's copy this one from here. And now
  18854. 12:02:48if I go to authorization or let's go to
  18855. 12:02:50headers, add one more. So I'm just going
  18856. 12:02:52to add authorization and let's add a
  18857. 12:02:54beer right space. So now let's try to
  18858. 12:02:58send and that's it. So now you have your
  18859. 12:03:01ID title is created status is open by
  18860. 12:03:03default. This is your user ID. The user
  18861. 12:03:05ID that created now you see this user ID
  18862. 12:03:08and if you go to this user ID that
  18863. 12:03:10particular user ID both will be same.
  18864. 12:03:11Right? So for example if you go check
  18865. 12:03:13this one B 0 A and check this one B 0 A.
  18866. 12:03:18So that means this user actually created
  18867. 12:03:20this particular task. So we'll be able
  18868. 12:03:22to actually track that which support
  18869. 12:03:24task is created by which user you're
  18870. 12:03:26getting this user ID and this particular
  18871. 12:03:28user ID both are same. So this about the
  18872. 12:03:32creating. So creating is actually
  18873. 12:03:33simple. Now remember you might be
  18874. 12:03:35thinking I I know that some of you will
  18875. 12:03:36be thinking oh this is very simple. I'm
  18876. 12:03:38just adding one particular title. It
  18877. 12:03:41doesn't like that. What you can do you
  18878. 12:03:43don't have to exactly do like this. Now
  18879. 12:03:45I'm adding only title. What will be your
  18880. 12:03:48homework? Instead of adding only title,
  18881. 12:03:50you can add 10 more properties here. But
  18882. 12:03:53at the end, you have to understand this
  18883. 12:03:55one that the process will remain same.
  18884. 12:03:57Doesn't matter how many properties you
  18885. 12:03:58are going to have here. At the end, the
  18886. 12:04:00process or the flow will remain same. If
  18887. 12:04:03you understand this flow, you will be
  18888. 12:04:05able to create any kind of project.
  18889. 12:04:06Okay. So with this now, let's move to
  18890. 12:04:08the next one which will be our uh what
  18891. 12:04:10is that? Uh next will be get fetching
  18892. 12:04:13all the um task. So for this one we are
  18893. 12:04:16going to do user task router get. So
  18894. 12:04:19this will be slash and I'll do async.
  18895. 12:04:22Let me just copy this one from here.
  18896. 12:04:26And then we are simply going to take a
  18897. 12:04:28try catch
  18898. 12:04:33and then we're going to simply pass the
  18899. 12:04:35error. And here what are we going to
  18900. 12:04:38get? We are simply going to get all our
  18901. 12:04:39task right? So I'm just going to do con
  18902. 12:04:41tasks and I'll do a and I'll do get user
  18903. 12:04:46tasks. Okay. So now first of all when
  18904. 12:04:50we're going to create this service so
  18905. 12:04:51let's go to service and let's create
  18906. 12:04:53this. So I'm just going to do
  18907. 12:04:55export async function and I'm simply
  18908. 12:04:59going to do get user tasks. What
  18909. 12:05:03actually you need? Now here you have to
  18910. 12:05:05understand all of this. when you are
  18911. 12:05:07going to get the task you need to pass
  18912. 12:05:09this user ID because let's say there are
  18913. 12:05:11two user A and B A created five task B
  18914. 12:05:13created 10 task and you only need the
  18915. 12:05:16created the task that is created by A.
  18916. 12:05:18So in that case you need to know this
  18917. 12:05:19user ID then only you'll be able to get
  18918. 12:05:21it correct. So that means this get user
  18919. 12:05:23task now need the user ID. So you are
  18920. 12:05:26going to get the user ID which will
  18921. 12:05:27string what this is going to return it
  18922. 12:05:29is going to return a promise of task and
  18923. 12:05:31this will be array because user can
  18924. 12:05:33create multiple task also and here now
  18925. 12:05:36we are going to return what we're going
  18926. 12:05:37to create one more repository function
  18927. 12:05:40here right here because we need to get
  18928. 12:05:41the task from our DB only correct. So
  18929. 12:05:44I'm just going to do export as sync
  18930. 12:05:46function and I'm just going to do fetch
  18931. 12:05:48task by user ID by user ID. Okay. So you
  18932. 12:05:53need the user id this will be string and
  18933. 12:05:57here what you're going to do you're
  18934. 12:05:58going to do promise and this will be so
  18935. 12:06:01we are going to get task. So now what
  18936. 12:06:04you're going to do that simply will do
  18937. 12:06:06const result.
  18938. 12:06:08So this will be a pull dot query. So
  18939. 12:06:12we're going to
  18940. 12:06:14uh so this will be task row and now
  18941. 12:06:16we're going to write our query. So I'll
  18942. 12:06:18do select.
  18943. 12:06:20So I'll do select ID title user ID. No
  18944. 12:06:25uh we have a status status user ID
  18945. 12:06:30created at
  18946. 12:06:32and updated at. So all these I need from
  18947. 12:06:35where? From our table. The table name is
  18948. 12:06:38support task. So we'll do support tasks
  18949. 12:06:42correct. So from that this table where
  18950. 12:06:45based on what we are checking based on
  18951. 12:06:47user ID. So where this user ID
  18952. 12:06:50will be equal to this right. And then
  18953. 12:06:53I'm also going to do a order by. So
  18954. 12:06:56let's do order by of created at
  18955. 12:06:59let's do disk. Oh no this will be wrong.
  18956. 12:07:02Create at and this will be disk. And
  18957. 12:07:04here you're going to simply pass a user
  18958. 12:07:06ID. That's it. And then what you going
  18959. 12:07:08to do? You have to return result dot
  18960. 12:07:10rows. Why? Because there can be multiple
  18961. 12:07:13task also. So save this. So now what
  18962. 12:07:16you're going to do, right? We'll go to
  18963. 12:07:17our uh task service.
  18964. 12:07:20So here we're going to call this fetch
  18965. 12:07:21task by user ID
  18966. 12:07:24and we're going to pass the user ID.
  18967. 12:07:26This is done. So now we'll go to our
  18968. 12:07:27routes and then we'll import this one
  18969. 12:07:29from our service. And what this is going
  18970. 12:07:31to receive? This is going to receive the
  18971. 12:07:32user ID. So you do request dot user
  18972. 12:07:37dot user id. That's it. And then you're
  18973. 12:07:40going to simply return. So you do race
  18974. 12:07:42dot status of 200 dot JSON. And here now
  18975. 12:07:48we're going to return success will be
  18976. 12:07:49true. And we're going to return the data
  18977. 12:07:52which will be these tasks. And this is
  18978. 12:07:55done. So now this particular route also
  18979. 12:07:57will be protected. Okay. So let's see
  18980. 12:08:00this one whether this is working or not.
  18981. 12:08:02So let's go here and then I'm going to
  18982. 12:08:04just copy this create one more and this
  18983. 12:08:07will be get and now let's do first of
  18984. 12:08:08all let's do one thing let's try to send
  18985. 12:08:10access token is required correct so we
  18986. 12:08:13go to our login let's create one token
  18987. 12:08:16we're going to copy this we'll go here
  18988. 12:08:19let's go to headers let's add
  18989. 12:08:21authorization
  18990. 12:08:24let's add ber
  18991. 12:08:27paste this and now see what will happen
  18992. 12:08:28if I send this so we're getting the task
  18993. 12:08:31created
  18994. 12:08:32Now you might be thinking how it is
  18995. 12:08:34getting the user ID. Correct. Remember
  18996. 12:08:36this is actually getting the user ID
  18997. 12:08:37from your request object and this
  18998. 12:08:39request object from where you are
  18999. 12:08:40getting we are actually getting from
  19000. 12:08:42this token because at the end you are
  19001. 12:08:44validating the JWT token right. So if
  19002. 12:08:46you go to post here and let's say if I
  19003. 12:08:47try to add one more so let's do second.
  19004. 12:08:51So it's created one and if you try to go
  19005. 12:08:53and send this now you're getting two
  19006. 12:08:55task right. So first support task and
  19007. 12:08:57second support task. Fantastic. Right.
  19008. 12:08:59So that means fetching all the
  19009. 12:09:03what that fetching all the task is also
  19010. 12:09:05working. So now what you're going to do
  19011. 12:09:06right we'll create our third one which
  19012. 12:09:08is how we going to fetch a single task
  19013. 12:09:10because now what is happening right we
  19014. 12:09:12are getting all the tasks. So we need to
  19015. 12:09:13fetch one single task. Okay. So now
  19016. 12:09:16let's go here and let's see how we are
  19017. 12:09:17going to do that. So first of all let's
  19018. 12:09:19do one thing. I'm just going to do user
  19019. 12:09:21task router dot get. Now what you have
  19020. 12:09:24to do right remember here you are going
  19021. 12:09:26to understand how you can create a
  19022. 12:09:27dynamic API route. Dynamic API route we
  19023. 12:09:31have to create like this. So we have to
  19024. 12:09:32do task ID. Now what actually we're
  19025. 12:09:34doing here? We're simply telling this is
  19026. 12:09:35a dynamic parent. Why? Because when this
  19027. 12:09:38particular user let's say created 10
  19028. 12:09:40task each every t each and every task
  19029. 12:09:42will have separate ID. So that means
  19030. 12:09:44this task ID will be different. For
  19031. 12:09:47example, your first task ID is one,
  19032. 12:09:50second task ID is three, 2, 3, four,
  19033. 12:09:52like this. Right? So when I wanted to
  19034. 12:09:54get only fifth task, I need to replace
  19035. 12:09:56this one with / five something like
  19036. 12:09:58this. So that is the reason I'm sending
  19037. 12:10:00a colon and then the param or query
  19038. 12:10:03sorry parameter name here that will be
  19039. 12:10:05dynamic right. So here now what I can do
  19040. 12:10:07right I'll simply do async and this will
  19041. 12:10:10be request response next.
  19042. 12:10:15And now I'll just do try catch.
  19043. 12:10:21So I'll do next call the error. And in
  19044. 12:10:24this try so first of all I'm just going
  19045. 12:10:25to do con task because we are fetching
  19046. 12:10:28single task. So do ait. So do get user
  19047. 12:10:32task by id.
  19048. 12:10:37Okay. So what all we have to pass here.
  19049. 12:10:39So first of all before this also let's
  19050. 12:10:41do one thing. Let's return this. I'll do
  19051. 12:10:42uh race dot status of 200 dot JSON. So
  19052. 12:10:47here I'm just going to do success will
  19053. 12:10:49be true and data I'm just going to pass
  19054. 12:10:51this task here. But this is not yet
  19055. 12:10:53completed. So first we have to go and
  19056. 12:10:55fetch the single task. Correct. So go to
  19057. 12:10:57service file and then we are going to
  19058. 12:10:59create one more. So I'm just going to do
  19059. 12:11:01export async function and I'm simply
  19060. 12:11:04going to do get user task
  19061. 12:11:07task
  19062. 12:11:10by ID. Okay. Get user task or fetch user
  19063. 12:11:14task. What we are using here? Get user
  19064. 12:11:16task. I think it is fine. Get user task.
  19065. 12:11:19That's fine. Get user task by ID. It's
  19066. 12:11:21okay. Now here what are the things you
  19067. 12:11:24need? Can you guess? First of all, you
  19068. 12:11:26need to get the user ID. Second, you
  19069. 12:11:29need to get the task ID. Because let's
  19070. 12:11:31say if I try to get this particular
  19071. 12:11:32task, you need this ID. Also, you need
  19072. 12:11:36which user actually created this because
  19073. 12:11:38it's actually better. Okay, you can
  19074. 12:11:40actually face this use in this also. But
  19075. 12:11:42you need the user ID here also. Okay, so
  19076. 12:11:44that means you need two things. So first
  19077. 12:11:45you need the task ID which will be
  19078. 12:11:48string and also you need the user ID
  19079. 12:11:51which also will be string. What this is
  19080. 12:11:54going to return? So it's going to return
  19081. 12:11:55a promise of the single task. So it will
  19082. 12:11:57be task. And now what I'm can do right I
  19083. 12:12:00can do const and this will be task and
  19084. 12:12:02here I'm just going to do ait and I'll
  19085. 12:12:04do find
  19086. 12:12:06task by
  19087. 12:12:09ID find task by ID and user ID. Okay.
  19088. 12:12:14And here we're going to pass the task ID
  19089. 12:12:16and we are also going to pass the user
  19090. 12:12:17ID. So this one now we are going to
  19091. 12:12:19create where again in our repository
  19092. 12:12:21file because we at the end we need to f
  19093. 12:12:23this one from our
  19094. 12:12:25DB. So I'll do export as sync function.
  19095. 12:12:28So do fetch task uh by ID and user id.
  19096. 12:12:32So you need the task ID. This will be
  19097. 12:12:34string. And also you need the user ID.
  19098. 12:12:38This also will be string. So what this
  19099. 12:12:41is going to return? This is going to
  19100. 12:12:42return a promise of task
  19101. 12:12:44or it can return null because it's
  19102. 12:12:46possible that you are giving some random
  19103. 12:12:48ID and that data is not present. Okay.
  19104. 12:12:50So now I'm just going to do con result.
  19105. 12:12:53So this will be have it pull dot query.
  19106. 12:12:57So we're going to pass the task row and
  19107. 12:13:00here we are going to now write our
  19108. 12:13:02query. So we'll do
  19109. 12:13:04select
  19110. 12:13:07ID, title, status,
  19111. 12:13:10user ID,
  19112. 12:13:12created at and updated at. So all these
  19113. 12:13:15thing we need from where? From our
  19114. 12:13:18support task table. So do from support
  19115. 12:13:20task
  19116. 12:13:22and then we are going to do where id
  19117. 12:13:25will be this. Okay, ID means your actual
  19118. 12:13:30ID, the task ID. And you also need the
  19119. 12:13:33user ID where user ID is equal to this.
  19120. 12:13:36And then you're going to simply pass
  19121. 12:13:37your ID means this will be your task ID.
  19122. 12:13:39And then you're going to have your user
  19123. 12:13:41ID. Very simple. And then you're simply
  19124. 12:13:44going to return
  19125. 12:13:46result dot rows of zero. But here it is
  19126. 12:13:50possible. So you can also return null
  19127. 12:13:51here. Okay, if that is not present.
  19128. 12:13:53That's it you have to do here. So now we
  19129. 12:13:55are going to import this in our service
  19130. 12:13:57file and then we are going to give a
  19131. 12:14:00very very important check because if
  19132. 12:14:02this task is not present so in that case
  19133. 12:14:04what I going to do right will throw new
  19134. 12:14:06app error and I'm just going to do a 404
  19135. 12:14:10and I'll do task not found. Okay and at
  19136. 12:14:14the end we going to return the task.
  19137. 12:14:17That's it. So this about our service.
  19138. 12:14:20Now we go to here. So we need to first
  19139. 12:14:22import this from our service and then
  19140. 12:14:25what you need we need to was the task ID
  19141. 12:14:26and the user ID. So task ID will be your
  19142. 12:14:29this ID. But how you're going to get
  19143. 12:14:31this ID now? So this ID you're going to
  19144. 12:14:33get from request and request is going to
  19145. 12:14:35give you these params and these params
  19146. 12:14:36is going to have your task ID. See this
  19147. 12:14:40is going to have your task ID and this
  19148. 12:14:41this depend on what you're giving here.
  19149. 12:14:43For example, if I do ID, see you're
  19150. 12:14:45getting error. Why? Because now you have
  19151. 12:14:46to give task ID you're getting. So
  19152. 12:14:48that's the reason we just change this
  19153. 12:14:50one to task ID. And here I'm simply
  19154. 12:14:52going to do user ID. You already know.
  19155. 12:14:54So do request dot user dot user ID. And
  19156. 12:14:57this is done. So now let's save this and
  19157. 12:15:00let's say if I just go here and let's
  19158. 12:15:03create one more. So what I'll do right.
  19159. 12:15:04So let's copy this
  19160. 12:15:07first. Let's go here. So we're going to
  19161. 12:15:09do this. And now what I have to do, we
  19162. 12:15:11have to pass the dynamic value, right?
  19163. 12:15:13So / task ID. So maybe let's take this
  19164. 12:15:16task one first one. Okay. So this ID you
  19165. 12:15:18wanted to take and I'll do / this. And
  19166. 12:15:23now what I'm going to do right I'm going
  19167. 12:15:24to send here and you can see that we are
  19168. 12:15:25getting access token is required which
  19169. 12:15:27is uh expected because we need to pass
  19170. 12:15:29our token. So we go to our login
  19171. 12:15:33get our token
  19172. 12:15:36and let's go to headers add
  19173. 12:15:40authorization and I'm simply going to
  19174. 12:15:43add a beer
  19175. 12:15:48and paste and now let's try to send and
  19176. 12:15:52that's it. So you're getting success and
  19177. 12:15:54you're getting this data. Okay. So now
  19178. 12:15:56let's see go here and maybe let's I try
  19179. 12:15:58to add this copy this and if I go here
  19180. 12:16:01let's change this one to this send and
  19181. 12:16:04you're getting second task and let's say
  19182. 12:16:06if I just change this I uh ID to a
  19183. 12:16:08different ID send this you're getting
  19184. 12:16:10task not found that means you're getting
  19185. 12:16:12404 and that is coming from your service
  19186. 12:16:15from this line okay so this is completed
  19187. 12:16:18so now next one will be update right so
  19188. 12:16:20let's say how we are going to do update
  19189. 12:16:22so I'm going to create one more and this
  19190. 12:16:24time we're going to use not get not post
  19191. 12:16:27we are going to use patch because we are
  19192. 12:16:29only going to update a certain uh
  19193. 12:16:31portion or certain property in our DB so
  19194. 12:16:33you don't have to update all the
  19195. 12:16:34properties right so in this case when
  19196. 12:16:36you're going to patch something let's
  19197. 12:16:38say uh if I go here let's say one task
  19198. 12:16:41two task three task and in this
  19199. 12:16:43particular task you wanted to update
  19200. 12:16:45some property let's say you wanted to
  19201. 12:16:46change the name from sum to John so in
  19202. 12:16:49that case you need to also know the
  19203. 12:16:51which ID you wanted to update right so
  19204. 12:16:53that means these al will be a dynamic
  19205. 12:16:55value. So in this case also what we
  19206. 12:16:57going to do? We are actually going to
  19207. 12:16:59tell that okay this ID we wanted to
  19208. 12:17:01update right. So this task ID will be
  19209. 12:17:03what again dynamic value you're getting
  19210. 12:17:05and then we are going to take async.
  19211. 12:17:08So we're going to get request response
  19212. 12:17:11next
  19213. 12:17:13and here we are going to do a try catch
  19214. 12:17:17error and then we're going to do next
  19215. 12:17:19and we're going to call out error. So
  19216. 12:17:21first of all what we have to do. So
  19217. 12:17:23first let's do a const and we're going
  19218. 12:17:25to take task and I'll do a and I'll do
  19219. 12:17:28update user task. Okay. So what are the
  19220. 12:17:32things that you need here? So this is
  19221. 12:17:33the first thing. See first of all you
  19222. 12:17:35need to know which task you wanted to
  19223. 12:17:36update. So that means you need the task
  19224. 12:17:38ID. Second you need which user actually
  19225. 12:17:41created that particular task. So that
  19226. 12:17:43means you also need the user ID. And
  19227. 12:17:45that makes sense. Correct? Let's say I
  19228. 12:17:46wanted to update this task from second
  19229. 12:17:50support task to second support task
  19230. 12:17:52edited. So that means I need to change
  19231. 12:17:54this title. Correct? So that means I
  19232. 12:17:56need sorry I need this title. I need
  19233. 12:17:59this user ID and also I need this title
  19234. 12:18:02also. Why? Because consider you are
  19235. 12:18:04having a UI and from this UI user is
  19236. 12:18:07going to update right? So that means
  19237. 12:18:09this title value you need the updated
  19238. 12:18:11title value. Consider I wanted to change
  19239. 12:18:14this uh second support task to sum
  19240. 12:18:17something like this. So that means I
  19241. 12:18:19need that updated value correct then
  19242. 12:18:20only you'll be able to understand okay I
  19243. 12:18:22need to update the title but what will
  19244. 12:18:24be the value that you don't know so that
  19245. 12:18:26you're going to get from your UI side
  19246. 12:18:28right from your front end so that means
  19247. 12:18:30in this one we need three things but
  19248. 12:18:32before that let's do go to our service
  19249. 12:18:34and let's first create this. So here
  19250. 12:18:36what I'm going to do right I'm simply
  19251. 12:18:37going to do export as sync function and
  19252. 12:18:40here I'm just going to do update user
  19253. 12:18:43task. So we need the task
  19254. 12:18:46we need the task ID which will be
  19255. 12:18:48string. We also need the user ID which
  19256. 12:18:51will be string and also we need the
  19257. 12:18:53title that will be also string. What
  19258. 12:18:56this is going to return again it's going
  19259. 12:18:57to return promise of the task.
  19260. 12:19:00So first of all I want to do valid
  19261. 12:19:02right? I wanted to do valid title and
  19262. 12:19:04this will be our validate title and I'm
  19263. 12:19:06going to pass the title and now what I'm
  19264. 12:19:09going to do first I'm going to do con
  19265. 12:19:11task and I'll do update task
  19266. 12:19:15title and this one we are going to do
  19267. 12:19:18where we are going to write in our
  19268. 12:19:19repository because at the end you have
  19269. 12:19:20to remember you have to interact with
  19270. 12:19:22your DB only right so if you go here and
  19271. 12:19:25let's create one more so I'm just going
  19272. 12:19:26to do export async function and I'm
  19273. 12:19:30simply going to do update task ask title
  19274. 12:19:33this is also going to receive these
  19275. 12:19:35three things I'm not going to write
  19276. 12:19:36let's copy and paste and then here
  19277. 12:19:42we are going to do promise and I'm
  19278. 12:19:43simply going to do this will be either
  19279. 12:19:45task or else this will be null again why
  19280. 12:19:47because let's say you are updating one
  19281. 12:19:48particular task that task is not even
  19282. 12:19:50present so in that case this will return
  19283. 12:19:52null correct so I'm simply going to do
  19284. 12:19:55con result so I do ait pool dot query so
  19285. 12:19:59we're going to pass the task row and
  19286. 12:20:01then try to write the query by yourself
  19287. 12:20:03because by this time I hope that you
  19288. 12:20:05completed the postgress section and also
  19289. 12:20:07you have written multiple uh query also
  19290. 12:20:09so I think you should do all of this how
  19291. 12:20:11you going to update so here you can't
  19292. 12:20:13use select correct so you have to do
  19293. 12:20:14update where you wanted to update we
  19294. 12:20:16wanted to update our support task table
  19295. 12:20:20right so now what we have to update so
  19296. 12:20:23you have to update the title property so
  19297. 12:20:25I'm going to do set which property title
  19298. 12:20:28which will be equal to one and then I'm
  19299. 12:20:31Just going to do updated at this will be
  19300. 12:20:34equal to what now because the when
  19301. 12:20:37you're updating that time you also you
  19302. 12:20:39need right that time stamp now what
  19303. 12:20:41you're going to do where now you're
  19304. 12:20:42going to tell okay because your DB
  19305. 12:20:44doesn't know right okay which item I
  19306. 12:20:46need to update let's say you have 10
  19307. 12:20:48task and you wanted to update the fifth
  19308. 12:20:50one so you need to tell right okay where
  19309. 12:20:52the ID will be this
  19310. 12:20:54and user ID will be this so simply I'm
  19311. 12:20:58telling that okay you wanted to update
  19312. 12:21:00this particular First of all, you wanted
  19313. 12:21:02to update support task table. You wanted
  19314. 12:21:04to update the title property. You wanted
  19315. 12:21:07to update the updated property. But how
  19316. 12:21:08we are going to understand which title
  19317. 12:21:10where the ID is this and user ID is this
  19318. 12:21:13mean this means user ID means this task
  19319. 12:21:16is created by that particular user. And
  19320. 12:21:18this is the title ID where I wanted to
  19321. 12:21:20update. And now what I'm going to do I'm
  19322. 12:21:22going to do a returning and this will be
  19323. 12:21:25ID title status user ID
  19324. 12:21:30create or let's write it man copy this
  19325. 12:21:32from here created and updated at these
  19326. 12:21:34two things so here I need this that's it
  19327. 12:21:37and then you're going to password so
  19328. 12:21:39first of all what is our first one first
  19329. 12:21:40variable so which is title so this will
  19330. 12:21:43be your title then next is your ID so
  19331. 12:21:46this will be your task ID don't make
  19332. 12:21:47this mistake okay all of these and last
  19333. 12:21:49one is your user ID and This will be
  19334. 12:21:51your user ID. And then at the end you're
  19335. 12:21:54going to return result dot rows of zero
  19336. 12:21:58or else
  19337. 12:22:02this will be null.
  19338. 12:22:04So save this. I think this is done. So
  19339. 12:22:06now we'll go to our service means task
  19340. 12:22:10service. And now here what we're going
  19341. 12:22:12to do. So first of all let's import this
  19342. 12:22:14and we're going to pass the so we have
  19343. 12:22:17task ID, user ID and title. So first I'm
  19344. 12:22:19going to pass the task ID, user ID and
  19345. 12:22:21the title. And then I'm going to give a
  19346. 12:22:24check that if this task is not present
  19347. 12:22:26or we have to pass a valid title, not
  19348. 12:22:28this one. Task is not present. So I'm
  19349. 12:22:30just going to throw new app error.
  19350. 12:22:33So 404 and I'll do task not found. And
  19351. 12:22:38also this is going to return null also.
  19352. 12:22:40So in that case I'm simply going to
  19353. 12:22:42return the task here.
  19354. 12:22:45So save this. So now I'll go here and
  19355. 12:22:47then you're going to copy sorry import
  19356. 12:22:49this first and this update task is going
  19357. 12:22:51to receive your task ID. So how you
  19358. 12:22:52going to get the task ID? Task ID will
  19359. 12:22:54be your request dot params dot task ID.
  19360. 12:22:58Next you're going to pass the user ID.
  19361. 12:23:00User ID will be request dot user dot
  19362. 12:23:04user ID and then you're going to pass
  19363. 12:23:06the title. Title will be from where
  19364. 12:23:07you're going to get. So title you're
  19365. 12:23:09going to get from request dot body dot
  19366. 12:23:12title. That's it. Save this. And then at
  19367. 12:23:15the end what we going to do? We're going
  19368. 12:23:16to return race dot status of 200. So in
  19369. 12:23:20this case also we're going to return 200
  19370. 12:23:23dot JSON and we're going to do success
  19371. 12:23:26will be true and data will be task
  19372. 12:23:31this task. Save this. So now if we go
  19373. 12:23:34here right so let's say maybe I wanted
  19374. 12:23:36to update this task only. So I'm just
  19375. 12:23:38going to copy this add one more paste it
  19376. 12:23:42here. This will be patch and let's say
  19377. 12:23:44try to okay first of all I'm just going
  19378. 12:23:46to add one
  19379. 12:23:48here I'm just going to update the title
  19380. 12:23:51so title I have second support task
  19381. 12:23:55correct so let's change this one to
  19382. 12:23:57second support task title edited okay
  19383. 12:24:03try to send first we're getting access
  19384. 12:24:05token is needed so first let's log get
  19385. 12:24:09the token
  19386. 12:24:12And then I'll go to headers add a
  19387. 12:24:15authorization
  19388. 12:24:17and I'm going to do be error
  19389. 12:24:20and then try to send and you can see
  19390. 12:24:23that now you're getting second support
  19391. 12:24:24task is edited. Okay. So that means this
  19392. 12:24:27is working fine. Now try to fetch all
  19393. 12:24:29the task. Okay. So I'll go here send
  19394. 12:24:31this. So it's invalid. So okay let's
  19395. 12:24:33change the token first
  19396. 12:24:36paste here and then try to send and you
  19397. 12:24:39can see that now you're getting second
  19398. 12:24:40support task edited. So that mean this
  19399. 12:24:42is working fine. If you go here right
  19400. 12:24:44try to send this you're getting second
  19401. 12:24:46support task edited. All right. So I
  19402. 12:24:47think this is completed. So the last
  19403. 12:24:49step the last one that is left and that
  19404. 12:24:51will be delete. Okay. So with this crowd
  19405. 12:24:54will be completed. So create update uh
  19406. 12:24:56read and delete. So let's do the delete
  19407. 12:24:58in the next part. Okay. So now let's
  19408. 12:25:00implement our uh delete one right. So
  19409. 12:25:02for delete what we'll do? We'll do use a
  19410. 12:25:04task router and we going to use the
  19411. 12:25:06delete method. Here also when you're
  19412. 12:25:08going to delete so we are going to
  19413. 12:25:09delete a certain support task correct so
  19414. 12:25:12in this case also this will be dynamic
  19415. 12:25:15uh so here uh task ID and then let's
  19416. 12:25:18take a sync and let's copy this
  19417. 12:25:26and here we are going to take a try
  19418. 12:25:29catch
  19419. 12:25:32so call next with error and here in the
  19420. 12:25:35try so simply we're going to do private.
  19421. 12:25:37So we'll do delete user task. So what
  19422. 12:25:41are the things we need? So first of all
  19423. 12:25:42let's create our service, right? So this
  19424. 12:25:44will be simpler. So what I can do is
  19425. 12:25:46I'll do export
  19426. 12:25:49as sync function and this will be delete
  19427. 12:25:53user task. So first of all you need the
  19428. 12:25:55task ID
  19429. 12:25:58and also you need the user ID. both you
  19430. 12:26:00need and what this is going to return
  19431. 12:26:03it's going to return a promise and it's
  19432. 12:26:04going to return void nothing so here I'm
  19433. 12:26:07simply going to do const deleted so do
  19434. 12:26:10ait delete task so here let's go to our
  19435. 12:26:16repository first
  19436. 12:26:18and let's uh delete the task so first
  19437. 12:26:20I'm just going to do export as sync
  19438. 12:26:23function
  19439. 12:26:25so do delete
  19440. 12:26:28task so So you need the task ID which
  19441. 12:26:31will be string and also you need the
  19442. 12:26:34user ID. This also will be string and
  19443. 12:26:38here so let's do a promise and because
  19444. 12:26:40we deleting this one from our DB I'm
  19445. 12:26:42going to return a boolean here. Okay. So
  19446. 12:26:44we'll do con result. So this will have
  19447. 12:26:47it pull dot query. So what are you going
  19448. 12:26:51to do simply? So we're going to tell
  19449. 12:26:54that you are going to delete something.
  19450. 12:26:56So delete from where? So from your
  19451. 12:27:00support task table uh here. Okay. So
  19452. 12:27:03we'll do delete from support task where
  19453. 12:27:07id equal to this and user id is equal to
  19454. 12:27:13this. And then you're going to simply
  19455. 12:27:16pass your task ID and user ID. That's
  19456. 12:27:19it. What do you have to return? So
  19457. 12:27:21you're going to return your result
  19458. 12:27:24dot row count or else zero and this
  19459. 12:27:29we're going to simply do this is greater
  19460. 12:27:30than zero. So then it will be true.
  19461. 12:27:33Okay. So that mean this particular row
  19462. 12:27:34will be deleted. All right. So now let's
  19463. 12:27:37import this. We're going to import
  19464. 12:27:38delete task here. So I'll do delete task
  19465. 12:27:42and we're going to pass the task ID and
  19466. 12:27:44the user ID. Okay. So I'm simply going
  19467. 12:27:46to check if not equal to deleted. So
  19468. 12:27:48just throw one error here. So do new
  19469. 12:27:50error. Sorry, app error 404.
  19470. 12:27:55I'll do task not found.
  19471. 12:27:58Sorry, this will be not deleted. Save
  19472. 12:28:00this. So now we'll go to here and what
  19473. 12:28:03we're going to do, right? So we can
  19474. 12:28:04directly call this delete user task.
  19475. 12:28:06This one, this particular service from
  19476. 12:28:08our route. But we have to send a
  19477. 12:28:10response back. So I'm simply going to do
  19478. 12:28:11rest dot status 200. And let's do a
  19479. 12:28:15JSON. And we are simply going to do
  19480. 12:28:17success will be true.
  19481. 12:28:21And let's pass a message that task
  19482. 12:28:25deleted successfully
  19483. 12:28:28from DB. Okay. So save this. So now I'll
  19484. 12:28:32go here. Maybe let's do one thing. I'm
  19485. 12:28:35going to copy this only. Create one
  19486. 12:28:37more. Uh paste this one. Let's do this
  19487. 12:28:41one. Delete. And we have this ID, right?
  19488. 12:28:43This one. So let's copy the I think this
  19489. 12:28:45only we are using here. That's fine. So
  19490. 12:28:47let's try to send. So access token is
  19491. 12:28:50needed which is correct. So let's get
  19492. 12:28:53one token now. So I'm going to
  19493. 12:28:56take this. We'll go here. Go to headers.
  19494. 12:29:02Uh so let's do authorization. And here
  19495. 12:29:04I'm going to do the error.
  19496. 12:29:07And then let's see send task deleted
  19497. 12:29:11successfully. Right. And if you try to
  19498. 12:29:12send one more time, task not found.
  19499. 12:29:14Fantastic. So let's go here and let's
  19500. 12:29:16try to send. You're getting only one
  19501. 12:29:18task means that is created. Sorry that
  19502. 12:29:20is deleted. Okay. So this will be our
  19503. 12:29:23CRUD. So create, read, update and
  19504. 12:29:26delete. What you have to understand or
  19505. 12:29:27what you have to practice right this
  19506. 12:29:29page. Why this page is very important
  19507. 12:29:31for this is where you are actually going
  19508. 12:29:33to do and believe me uh one thing I'm
  19509. 12:29:35going to mention that whatever project
  19510. 12:29:37you are going to create any project any
  19511. 12:29:39project I'm talking now there can be
  19512. 12:29:41some exception that is fine. whatever
  19513. 12:29:43project whatever complexity at the end
  19514. 12:29:46you are going to do something create
  19515. 12:29:48something you're going to do like
  19516. 12:29:50reading or fetching updating or delete.
  19517. 12:29:52So that is the reason this part is very
  19518. 12:29:54important. What you can do right you go
  19519. 12:29:56to support task instead of title and
  19520. 12:29:58status try to add some three or four
  19521. 12:30:00more properties and then try to
  19522. 12:30:02implement the same thing again and
  19523. 12:30:03again. Okay. So to do more practice. So
  19524. 12:30:05with this now let's move on to the next
  19525. 12:30:06one. So next will be our admin set
  19526. 12:30:08feature. So admin set feature. So what
  19527. 12:30:10admin will be able to do? So for now
  19528. 12:30:12user can only fetch their own task.
  19529. 12:30:15Correct. But admin as admin you will be
  19530. 12:30:17able to fetch your uh fetch all the
  19531. 12:30:19task. All right. So with this now let's
  19532. 12:30:21move on to the next one. All right. So
  19533. 12:30:23now we are going to create our admin
  19534. 12:30:24side routes. Correct. So as I already
  19535. 12:30:26mentioned that admin will be able to do
  19536. 12:30:28what? We'll be able to fetch all the
  19537. 12:30:30task. Second admin will be able to
  19538. 12:30:32update the status. But before that you
  19539. 12:30:33remember that we only have one
  19540. 12:30:35particular user. Correct. So we need to
  19541. 12:30:36create a admin user. So how we can do
  19542. 12:30:38that? We can directly query our DB and
  19543. 12:30:40then we can update one particular user
  19544. 12:30:43role from normal user to admin user.
  19545. 12:30:45Okay. So let's see how we are going to
  19546. 12:30:46do that. So first of all what I'll do
  19547. 12:30:48right? So let's increase this one and
  19548. 12:30:51then okay we'll go here let's clear and
  19549. 12:30:54then first of all we are going to run
  19550. 12:30:56couple of commands. So we're going to
  19551. 12:30:57run this docker execute and then this
  19552. 12:31:00will be what? So this is our container
  19553. 12:31:03name right. So you go to docker compose
  19554. 12:31:05we'll see that we have container name
  19555. 12:31:07and then you have your posgress db. So
  19556. 12:31:10this is your posgress db right? So we
  19557. 12:31:11are going to just copy this and then we
  19558. 12:31:14are going to just run it. Okay. So now
  19559. 12:31:18here we can directly query. So I I need
  19560. 12:31:20the id, email and role from my user
  19561. 12:31:22table normal query. Okay. So just paste
  19562. 12:31:24it. So do paste enter and see that we
  19563. 12:31:27have only one right. What I'm going to
  19564. 12:31:29do? So first let's go to our uh user
  19565. 12:31:32right. So register. So here I'm just
  19566. 12:31:35going to create an admin user. So I'm
  19567. 12:31:36just going to do john at thegmail.com.
  19568. 12:31:38I'll keep the same password and I'm
  19569. 12:31:39going to send registration successful.
  19570. 12:31:42Correct. So now what I'll do right? So
  19571. 12:31:44I'm just going to exit from this. So
  19572. 12:31:45I'll do Q. So let's clear. Let's run
  19573. 12:31:48once again. And then what I'm going to
  19574. 12:31:50do, I'm just going to copy this and
  19575. 12:31:53paste it here. Enter. And now you have
  19576. 12:31:55two user. So what I'll do right, I'm
  19577. 12:31:57going to change this [email protected]
  19578. 12:31:59from normal user or let's say this user
  19579. 12:32:02also. Let's do this only from normal
  19580. 12:32:04user to admin user. So how we can do
  19581. 12:32:06that? So update user, right? So we are
  19582. 12:32:09updating the user table. Set role will
  19583. 12:32:11be admin where email is what? Email is
  19584. 12:32:14this email. So [email protected]
  19585. 12:32:16makes sense. Now this we are going to
  19586. 12:32:18just paste it. So now what I'll do right
  19587. 12:32:21I'll copy this and then I'm just going
  19588. 12:32:23to execute this enter and you can see
  19589. 12:32:26that this is done. So now what we can do
  19590. 12:32:28right if you check this you'll see that
  19591. 12:32:29now this is a admin user correct and
  19592. 12:32:31this is what actually we need. So now we
  19593. 12:32:34have one super admin which is
  19594. 12:32:35[email protected]
  19595. 12:32:37correct? So this is what we are going to
  19596. 12:32:39do first of all. Now let's do one thing.
  19597. 12:32:41Let's go to our admin routes first. So
  19598. 12:32:44we'll go to our admin routes. So we have
  19599. 12:32:46routes routes. Routes we have admin
  19600. 12:32:48routes. So okay. So here first let's
  19601. 12:32:51start with creating. So how many routes
  19602. 12:32:53we need? First we need mainly two
  19603. 12:32:55routes. One where we are going to fetch
  19604. 12:32:57all the task. Second we are going to
  19605. 12:32:58update the status. But before that one
  19606. 12:33:01very important thing you're going to
  19607. 12:33:02understand now and that is so first
  19608. 12:33:04let's import something from uh we do
  19609. 12:33:08express and I'm simply going to do
  19610. 12:33:10export con admin task router and this
  19611. 12:33:16will be router and here first uh let's
  19612. 12:33:20do one thing let's go to our index and
  19613. 12:33:22I'm just going to do copy this and let's
  19614. 12:33:25do /admin / task and here I'm just going
  19615. 12:33:28to do admin task ask router. Okay, save
  19616. 12:33:31this. So now first of all what we have
  19617. 12:33:33to do here we have to protect the
  19618. 12:33:35routes. Now this is important. Now this
  19619. 12:33:37is where this particular page or these
  19620. 12:33:40routes whatever we are going to create
  19621. 12:33:42in this particular file only admin can
  19622. 12:33:44access. So that means if you directly
  19623. 12:33:46just do admin task router
  19624. 12:33:49while I'm doing no let's do this one
  19625. 12:33:51capital man. This is not correct. Let's
  19626. 12:33:54keep capital and I'm going to import
  19627. 12:33:56this and then I'm just going to do if I
  19628. 12:33:59do admin.outer do use let's I'm just
  19629. 12:34:02going to do authenticate. Now what will
  19630. 12:34:04happen now this is actually normal user
  19631. 12:34:06authentication right but we need to add
  19632. 12:34:08one more layer that means first we have
  19633. 12:34:11to check if this user is a normal user
  19634. 12:34:14and if this user is a admin user second
  19635. 12:34:17we also have to check if this user is a
  19636. 12:34:19logged in user or not. So that means
  19637. 12:34:21what I mean uh what I mean by this so
  19638. 12:34:23let's say we have a user we have a admin
  19639. 12:34:25correct for normal user we are simply
  19640. 12:34:28checking if the user is logged in or not
  19641. 12:34:30so if the user is logged in so this is
  19642. 12:34:33fine but for the admin user we have to
  19643. 12:34:35check two things first we have to check
  19644. 12:34:36if the user is logged in plus we also
  19645. 12:34:39have to check if the user role is admin
  19646. 12:34:41user or not. So that is [snorts] two
  19647. 12:34:43check we need to do. So that means this
  19648. 12:34:45is only one check. So this is only going
  19649. 12:34:47to check if the user is logged in. So
  19650. 12:34:49how we are going to check if the user is
  19651. 12:34:50a admin user or not? This is where we
  19652. 12:34:53have this admin middleware. So this
  19653. 12:34:55admin middleware is actually going to
  19654. 12:34:57check if this user is admin user or not.
  19655. 12:34:59So that means what we can do that we can
  19656. 12:35:01use multiple middleware together. So to
  19657. 12:35:03use multiple middleware together we have
  19658. 12:35:05to give comma and we are going to use
  19659. 12:35:07require admin. That's it. So that means
  19660. 12:35:09we are simply telling that there are two
  19661. 12:35:11level of check. First you check if the
  19662. 12:35:13user is logged in user plus if the user
  19663. 12:35:15is a admin user or not. both has to be
  19664. 12:35:18satisfied. Then only we will allow user
  19665. 12:35:21or we'll allow this particular code to
  19666. 12:35:23go to the next middleware or go to the
  19667. 12:35:24next route handler. So now what I can do
  19668. 12:35:27right I'll do admin task router dot get.
  19669. 12:35:30Now let's do slash and here I'll do
  19670. 12:35:32async request
  19671. 12:35:36response next.
  19672. 12:35:39So I'll do try catch
  19673. 12:35:43and we're going to call the next with
  19674. 12:35:45error. So now first of all let's do a
  19675. 12:35:49const and I'll do data and I'll do a get
  19676. 12:35:53admin task.
  19677. 12:35:57Okay. So first what we have to do here?
  19678. 12:36:00So first of all let's do one thing.
  19679. 12:36:02Let's go to our admin service. Right. So
  19680. 12:36:05let's go to admin task service because
  19681. 12:36:07this is where we are going to write our
  19682. 12:36:09service logic. So first of all here we
  19683. 12:36:11are going to uh do couple of changes.
  19684. 12:36:13Okay. Before that we also let's do one
  19685. 12:36:16thing right. So let's also implement uh
  19686. 12:36:18some of the search and filter pattern so
  19687. 12:36:21that you'll understand. So because in
  19688. 12:36:22the previous previous section we have
  19689. 12:36:24understood right that how you can do uh
  19690. 12:36:26create uh CR create update fetch and
  19691. 12:36:28everything. But in this one let's not
  19692. 12:36:30only do normal fetching but as well as
  19693. 12:36:32let's include search query that how you
  19694. 12:36:35can implement search and how also you
  19695. 12:36:37can implement filter. So let's do this
  19696. 12:36:39one in the next part. All right. So
  19697. 12:36:40let's create our uh services. So first
  19698. 12:36:43we have our first one which will be to
  19699. 12:36:45fetch all the task correct. So what are
  19700. 12:36:47we going to do right? So let's create
  19701. 12:36:48the function here. So I'll do export
  19702. 12:36:50async function. Let's do this one as get
  19703. 12:36:53admin task. So here what we are going to
  19704. 12:36:55receive right. So remember now we are
  19705. 12:36:57actually going to implement a search
  19706. 12:36:59feature also. So what will happen like
  19707. 12:37:01whenever we are going to do the API call
  19708. 12:37:03so let's I can pass a query parameter.
  19709. 12:37:05So what I mean by query parameter so
  19710. 12:37:06something like let's say I wanted to
  19711. 12:37:08search all those title of the of those
  19712. 12:37:11task but the title starts with first
  19713. 12:37:14something like this. So when I do this,
  19714. 12:37:16so let's I wanted to call this API with
  19715. 12:37:18local host
  19716. 12:37:21whatever 3,000 or something and then I
  19717. 12:37:23have API and then what I'm going to do
  19718. 12:37:24right I wanted to pass a query params
  19719. 12:37:26here. These query params will be my
  19720. 12:37:28search param and here what I can do
  19721. 12:37:29right I can pass a value. So that means
  19722. 12:37:31what I'm simply doing that I'm actually
  19723. 12:37:33implementing a search sorry function ID
  19724. 12:37:36but this is my key and this is my value.
  19725. 12:37:38So simply I'm telling that okay whenever
  19726. 12:37:40I wanted to do a search I wanted to see
  19727. 12:37:43all those support task where the title
  19728. 12:37:45starts with this title or maybe it start
  19729. 12:37:48with first whatever it is okay so for
  19730. 12:37:50this one what I going to do right so
  19731. 12:37:51let's create the type here only so I'm
  19732. 12:37:53going to create one uh type so I'm just
  19733. 12:37:55going to do this one as admin task task
  19734. 12:37:58list and this will be my query
  19735. 12:38:02okay and here what I'm going to do right
  19736. 12:38:04I let's do a search and this search will
  19737. 12:38:06be string and also I want to do search
  19738. 12:38:08with my status. This also will be
  19739. 12:38:10optional and this will be string. Now
  19740. 12:38:12why optional is actually important?
  19741. 12:38:13Because there is a possibility that
  19742. 12:38:14sometime user want to search or they
  19743. 12:38:16don't want to search. Okay. And then I'm
  19744. 12:38:19also going to take one more type and
  19745. 12:38:20this will be my admin task
  19746. 12:38:23list response
  19747. 12:38:26and this will be task and we are going
  19748. 12:38:29to import task of this
  19749. 12:38:32array. Okay. So what this is going to
  19750. 12:38:34receive right? So this is going to
  19751. 12:38:36receive query. The query will be type of
  19752. 12:38:38this admin task query
  19753. 12:38:41and what this is going to return. So it
  19754. 12:38:43is going to return promise and this will
  19755. 12:38:45be admin task query response. Okay. So
  19756. 12:38:48this is what we are going to return. So
  19757. 12:38:50first of all what you have to do that
  19758. 12:38:51see now let's understand that how you
  19759. 12:38:53can implement a proper search or filter
  19760. 12:38:54functionality right whenever you are
  19761. 12:38:57going to do any search functionality
  19762. 12:38:59right you need to actually first trim
  19763. 12:39:01that uh input that you are receiving.
  19764. 12:39:03Okay, trim means we have to remove some
  19765. 12:39:06unnecessary spaces from our query
  19766. 12:39:08parameters. So first let's start with
  19767. 12:39:10search. So I'm just simply going to do
  19768. 12:39:12search and this will be so I'll do
  19769. 12:39:14search query dot search. So you can see
  19770. 12:39:17that we're getting dot trim. Okay. So
  19771. 12:39:21this is what you're going to do or else
  19772. 12:39:23I'm just going to do this one as
  19773. 12:39:24undefined. Same I wanted to do for my
  19774. 12:39:26status also. So this will be query dot
  19775. 12:39:29search dot trim and then I'm simply
  19776. 12:39:33going to do or else this will be
  19777. 12:39:35undefined. So this is the first thing I
  19778. 12:39:37need to do. Now very important now when
  19779. 12:39:40we are actually going to do a uh filter
  19780. 12:39:43with status let's say I wanted to filter
  19781. 12:39:46all those task where the status is uh
  19782. 12:39:49open right. So in that case what we have
  19783. 12:39:52to do right because in our uh status uh
  19784. 12:39:55logic so you can see that this
  19785. 12:39:56particular property has fixed uh status
  19786. 12:39:59here right so fixed means either it will
  19787. 12:40:01be open or it will be in progress or it
  19788. 12:40:03will be resolved right it will not be
  19789. 12:40:05something else maybe let's say failed
  19790. 12:40:07failed is not there so now let's see if
  19791. 12:40:09user try to give some different value
  19792. 12:40:12which value is not present so you have
  19793. 12:40:13to actually return a error back so
  19794. 12:40:16you're getting so now to do this one
  19795. 12:40:17what you're going to do right I'm simply
  19796. 12:40:19going to do okay if this status is true
  19797. 12:40:22and now what we're going to do right so
  19798. 12:40:24let's maybe
  19799. 12:40:26uh we are going to create one let's do
  19800. 12:40:28here only so I'm just going to do a con
  19801. 12:40:31let's do task status
  19802. 12:40:36and these will be either open right so
  19803. 12:40:39we have either open or it will be either
  19804. 12:40:42in progress
  19805. 12:40:44or it will be after uh either
  19806. 12:40:47resolved and then I'm simply we're going
  19807. 12:40:50to do as constant right so these
  19808. 12:40:52statuses has to be between these three
  19809. 12:40:56options and if it is not there so that
  19810. 12:40:58means we have to mention that and then
  19811. 12:41:00I'm simply going to do if not equal task
  19812. 12:41:02status dot includes the current status
  19813. 12:41:05that we have right as task status
  19814. 12:41:10uh what is that name task status right
  19815. 12:41:16task oh we have to create one more here
  19816. 12:41:18I bet so let's create one type here. So
  19817. 12:41:21I'm just going to do type and I'll do
  19818. 12:41:22task status and this will be so I'm
  19819. 12:41:26simply going to do a type of this task
  19820. 12:41:28status and this will be number.
  19821. 12:41:32Okay. And this one we can use here that
  19822. 12:41:34if this is not as task status. So in
  19823. 12:41:37that case what you're going to do we are
  19824. 12:41:39simply going to throw new error. So I'll
  19825. 12:41:40throw new app error
  19826. 12:41:43and I simply going to tell 400 and I'll
  19827. 12:41:45do status must be
  19828. 12:41:50between
  19829. 12:41:52open in progress
  19830. 12:41:55or result. Okay. So this is the first
  19831. 12:41:59thing. So now what we going to do right?
  19832. 12:42:01So first here after this we are simply
  19833. 12:42:04going to create our task and let's do
  19834. 12:42:07ait and I'm simply going to do find all
  19835. 12:42:10task. Now this is where we are actually
  19836. 12:42:12going to implement our uh DB uh
  19837. 12:42:14property. So not DB properties our
  19838. 12:42:16repository logic and here we are going
  19839. 12:42:18to pass the search and we are going to
  19840. 12:42:19pass the status and at the end what
  19841. 12:42:22we're going to do we're going to return
  19842. 12:42:23this task here
  19843. 12:42:26this task. Okay.
  19844. 12:42:29Okay this is fine. So now we are going
  19845. 12:42:31to create our actual logic where we have
  19846. 12:42:34to do couple of things. So first let's
  19847. 12:42:36go to our admin task repository and here
  19848. 12:42:39first let's create this. I'm simply
  19849. 12:42:40going to do export as sync function and
  19850. 12:42:44this will be find all task and here we
  19851. 12:42:48are going to receive what we are going
  19852. 12:42:49to receive filters. So what will be the
  19853. 12:42:51filters? So we are getting search and
  19854. 12:42:53status right? So we need to create a
  19855. 12:42:54type. So simply I'm going to do type
  19856. 12:42:57let's do admin task list
  19857. 12:43:01filters
  19858. 12:43:04and this will be so either it will be
  19859. 12:43:06search or it will be status correct. So
  19860. 12:43:08copy this
  19861. 12:43:11and oh sorry here we are going to do
  19862. 12:43:14this and now this will be type will be
  19863. 12:43:17this and then what we're going to return
  19864. 12:43:19we going to return a promise of task
  19865. 12:43:22of array. So first we need to do couple
  19866. 12:43:25of things right and also let's see one
  19867. 12:43:27thing you have to return in this one not
  19868. 12:43:30query we have to return the response
  19869. 12:43:32here. So now what we have to do first?
  19870. 12:43:34So first we have to create one
  19871. 12:43:36condition. So I'll do conditions
  19872. 12:43:40and this will be string of array and
  19873. 12:43:42this will be equal to array. Also we
  19874. 12:43:44need the values. So I'm just going to do
  19875. 12:43:46values. Values we don't know what is the
  19876. 12:43:48value. So I'm just going to do unknown
  19877. 12:43:50of array and this will be equal to
  19878. 12:43:52array.
  19879. 12:43:54Now what I'm going to do? So first of
  19880. 12:43:55all what we have to do? So we have to
  19881. 12:43:57check that if currently in our URL
  19882. 12:44:01search is present. We are going to push
  19883. 12:44:02this search into our condition and also
  19884. 12:44:05we are going to push the value into our
  19885. 12:44:07values array. If status is present then
  19886. 12:44:10we are going to also push the status
  19887. 12:44:11into our condition and also we are going
  19888. 12:44:13to push our values here also. So to do
  19889. 12:44:15this one first what we'll do? So I'll
  19890. 12:44:17take a param index and this will be
  19891. 12:44:20equal to one. I'm simply going to check
  19892. 12:44:21that if filter dot search if this is
  19893. 12:44:24actually present. So in that case I'm
  19894. 12:44:26going to do condition dot push and here
  19895. 12:44:28we are going to write a query right. So
  19896. 12:44:30title I like right and this part also I
  19897. 12:44:33have covered in my uh actually uh
  19898. 12:44:36posgress video uh if you have watched
  19899. 12:44:38that particular section and we are going
  19900. 12:44:40to pass the param index and then we are
  19901. 12:44:42also going to push the values so values
  19902. 12:44:44will be what? So values will be here our
  19903. 12:44:48uh filters dot search. So do filter dot
  19904. 12:44:51search
  19905. 12:44:53that's it and then we are going to
  19906. 12:44:55increase the param index by one like
  19907. 12:44:57this. Similarly, we have to do the same
  19908. 12:44:59thing for our status also. So, I'll do
  19909. 12:45:02if filter dot status if this is present.
  19910. 12:45:05So, in that case, I'm simply going to do
  19911. 12:45:07condition dot push. And here we are
  19912. 12:45:09going to check the status property. So,
  19913. 12:45:10I'll do status is equal to
  19914. 12:45:14and this will be our param index. And
  19915. 12:45:16here we are going to pass value dot push
  19916. 12:45:19and d will be filters dot status. Okay.
  19917. 12:45:24So now based on whatever is present it's
  19918. 12:45:27going to whose uh in the values values
  19919. 12:45:30are important also and also in the
  19920. 12:45:32condition you have to check that which
  19921. 12:45:33property actually we are filtering. So
  19922. 12:45:35here we are checking our title for
  19923. 12:45:36status we checking our status and from
  19924. 12:45:38where it is coming this is actually from
  19925. 12:45:40our DB properties that we are checking.
  19926. 12:45:43So now we can simply create a um where
  19927. 12:45:45clause. So I'll do con and I'll do where
  19928. 12:45:48clause
  19929. 12:45:50and this will be the condition length if
  19930. 12:45:52this is greater than zero. So I'm simply
  19931. 12:45:54going to do where
  19932. 12:45:56and here we are going to simply pass
  19933. 12:45:58conditions dot join because if both are
  19934. 12:46:01present what you have to do we are going
  19935. 12:46:02to join with and okay like this. So this
  19936. 12:46:06is done or else this will be empty and
  19937. 12:46:08then at the end you can return the
  19938. 12:46:10result. do res uh result and this will
  19939. 12:46:12be avit. So we have pool dot query and
  19940. 12:46:16here we're going to pass a task
  19941. 12:46:18and now
  19942. 12:46:22okay let's do one thing maybe it will be
  19943. 12:46:24confusing so let's create like this we
  19944. 12:46:26have done remember task row and this
  19945. 12:46:28will be equal to our task like this and
  19946. 12:46:30this task row we going to use here
  19947. 12:46:34task row and now what I'm going to do so
  19948. 12:46:37we'll do select
  19949. 12:46:40so I'll do select ID title
  19950. 12:46:44status
  19951. 12:46:46user ID
  19952. 12:46:49created at and updated data. So all this
  19953. 12:46:52I need and now I'm simply going to do
  19954. 12:46:55from
  19955. 12:46:56support tasks this is my table name and
  19956. 12:46:59you have to pass the web clause here and
  19957. 12:47:02also now let's do order by
  19958. 12:47:05created at disk
  19959. 12:47:08and we are going to pass the values. So
  19960. 12:47:11values will be so this is our condition
  19961. 12:47:14and this will be our actual values the
  19962. 12:47:16values that we have this one. So I think
  19963. 12:47:19this is done and then at the end we are
  19964. 12:47:21going to return result dot rows.
  19965. 12:47:26So this is completed. So this is what we
  19966. 12:47:28are going to do based on what whatever
  19967. 12:47:29is present we add the condition that we
  19968. 12:47:32based on what we are going to filter
  19969. 12:47:33right and then uh we are also going to
  19970. 12:47:36push the values created a wear clause
  19971. 12:47:38and then just create the query. So this
  19972. 12:47:40is done. So now we can go to our
  19973. 12:47:42services. So here let's import this
  19974. 12:47:45first. So we're passing sorry uh this
  19975. 12:47:49will find all task search and status and
  19976. 12:47:51then it's going to return the task
  19977. 12:47:53return. So this is done. Now what we
  19978. 12:47:55have to do right? So you have to go to
  19979. 12:47:56our task route. So first of all this get
  19980. 12:47:59admin out going to import from our
  19981. 12:48:01service. This is our get admin and here
  19982. 12:48:03you see that you have to pass a query.
  19983. 12:48:05So how you're going to pass a query. Now
  19984. 12:48:07to pass the query what you do right
  19985. 12:48:09you're going to pass a request and
  19986. 12:48:11request is going to give you this query
  19987. 12:48:13that's it and then you can simply do
  19988. 12:48:16status
  19989. 12:48:18200
  19990. 12:48:21uh JSON and then you'll pass the success
  19991. 12:48:24it will be true and then
  19992. 12:48:27you're going to pass the data. So this
  19993. 12:48:29is done. So now this is actually the
  19994. 12:48:31important part here. Okay. So now what
  19995. 12:48:34we going to do right we need to do the
  19996. 12:48:35uh check here or we are going to test.
  19997. 12:48:38So first let's go to our okay let me
  19998. 12:48:40just move this one here.
  19999. 12:48:45Okay so first let's go to our login and
  20000. 12:48:49then first I'll try to login. Okay I'm
  20001. 12:48:52not running maybe. Okay I got it because
  20002. 12:48:55I'm actually recording after some time.
  20003. 12:48:58So I have to start again. So now let's
  20004. 12:49:01check what we passing. We are passing
  20005. 12:49:03body sumgmail.com
  20006. 12:49:06send this we're getting the token so
  20007. 12:49:08first check this okay so I'll go here uh
  20008. 12:49:10and then in the headers I'll just check
  20009. 12:49:13that okay what is this uh user
  20010. 12:49:21send this okay this is a normal user I
  20011. 12:49:24think we have created one more user
  20012. 12:49:26right I think that is John I remember
  20013. 12:49:29let's send I think this is the admin
  20014. 12:49:31user because we need to check the admin
  20015. 12:49:33user also and then now let's try to send
  20016. 12:49:36this one
  20017. 12:49:39okay so this is admin right so now first
  20018. 12:49:41of all what I'll do right so let's
  20019. 12:49:43create one here so this will be get
  20020. 12:49:46right so go here copy this API till this
  20021. 12:49:50part
  20022. 12:49:52so API now let's go to our index so we
  20023. 12:49:56have / API/ admin / task correct so /
  20024. 12:50:00admin/ as tasks. Okay. So now first of
  20025. 12:50:04all I try to send access token is
  20026. 12:50:06required. Now what I'll do right I'll go
  20027. 12:50:07to login and I'll try to login with my
  20028. 12:50:10normal user. Okay. This is very
  20029. 12:50:11important. So try to send and you'll see
  20030. 12:50:14that I'm just going to copy this and
  20031. 12:50:17let's go to our headers. So add
  20032. 12:50:19authorization. Let's add our beer.
  20033. 12:50:25And then you see what will happen. Try
  20034. 12:50:26to send admin access is required and you
  20035. 12:50:29do not have admin access. It seems
  20036. 12:50:30fantastic right? So that means this
  20037. 12:50:33particular line now is actually able to
  20038. 12:50:35understand that okay this actually need
  20039. 12:50:37a admin access because this also need
  20040. 12:50:39authenticate as well as require admin.
  20041. 12:50:41So now if I go to login and try to login
  20042. 12:50:44with my admin user get the token
  20043. 12:50:48and now I try to go here and try to
  20044. 12:50:51replace this
  20045. 12:50:53and try to send that's it. So you're
  20046. 12:50:56getting this one. Now what I'll do right
  20047. 12:50:57so maybe
  20048. 12:50:59uh maybe let's do one thing we have a
  20049. 12:51:01title first open task and status is open
  20050. 12:51:04correct so maybe I'm just going to login
  20051. 12:51:07with a normal user okay so I'll try to
  20052. 12:51:09send I'll get the token
  20053. 12:51:12and then go to our uh where is that post
  20054. 12:51:15right so your body second support task
  20055. 12:51:18let's try to add one more here
  20056. 12:51:23try to create so it's created so now if
  20057. 12:51:25I go here and try to send. So you're
  20058. 12:51:27getting two, right? So now let's see how
  20059. 12:51:29we are going to uh test. So first of
  20060. 12:51:32all, let's say I wanted to check the
  20061. 12:51:34title of the second one only. So what I
  20062. 12:51:36can do, right? I can pass a query here.
  20063. 12:51:38So see I'll do this and see here we have
  20064. 12:51:40a query paramedic and I'm just going to
  20065. 12:51:42do a search. So you can see that the key
  20066. 12:51:44is value is search and search is equal
  20067. 12:51:46to I'm going to do second. Okay. And now
  20068. 12:51:48try to send okay status must be okay.
  20069. 12:51:52Why this is coming? Status must be
  20070. 12:51:53between open in progress and result.
  20071. 12:51:55This should not come.
  20072. 12:52:00Oh, this will be status. See, this is
  20073. 12:52:02the problem because you given for both
  20074. 12:52:03sim. So now try to send and you can see
  20075. 12:52:05that you're only getting the second
  20076. 12:52:07support. Now I'll do first. Try to send.
  20077. 12:52:10You're getting only first, right? And
  20078. 12:52:12also you can do with status also. Let's
  20079. 12:52:15say I'm just going to do status and I
  20080. 12:52:17wanted to check the open status. So I'll
  20081. 12:52:19do status and this will be open like
  20082. 12:52:21this. Okay. Try to send you're getting
  20083. 12:52:23get both. And now change this one to
  20084. 12:52:25maybe result
  20085. 12:52:27right and try to send you're getting
  20086. 12:52:29empty right if you try to send in
  20087. 12:52:31progress.
  20088. 12:52:33Try to send you're getting empty. Okay.
  20089. 12:52:36Now let's say you can uh send both. So
  20090. 12:52:38how you going to send both? Let's say
  20091. 12:52:39you wanted to add search. Search will be
  20092. 12:52:41second. And then if you want to add both
  20093. 12:52:44you have to give and here and see that
  20094. 12:52:45one mode will be added here and this
  20095. 12:52:47will be status equal to open. So
  20096. 12:52:49basically I'm telling that give me all
  20097. 12:52:50the support task where the title is
  20098. 12:52:53second and status is open try to send
  20099. 12:52:55you're getting only one fantastic right
  20100. 12:52:58so with this now easily you will be able
  20101. 12:53:01to implement search and filter logic
  20102. 12:53:02also okay so with this now let's move on
  20103. 12:53:04to the next one so next one will be how
  20104. 12:53:07admin can update task status so that we
  20105. 12:53:09are going to do in the next part all
  20106. 12:53:11right so now next one will be so we need
  20107. 12:53:13one more route here that admins should
  20108. 12:53:15be able to update the uh update task
  20109. 12:53:17status
  20110. 12:53:18Okay. So, first let's do admin task
  20111. 12:53:21router patch. I think I'm not explaining
  20112. 12:53:23again because you uh I hope that you
  20113. 12:53:26know that when we are updating only
  20114. 12:53:27certain portion we don't have to do like
  20115. 12:53:29post and everything right. So we need
  20116. 12:53:31only patch. Uh so here I'm simply going
  20117. 12:53:33to take the task ID because we need to
  20118. 12:53:35know which task we are updating and then
  20119. 12:53:38we have to tell look at this will be my
  20120. 12:53:39status of update. So here I'm simply
  20121. 12:53:41going to do a sync. We'll do request
  20122. 12:53:43response
  20123. 12:53:46and next
  20124. 12:53:49correct. So now let's take a try catch
  20125. 12:53:57call the next with error. So in this try
  20126. 12:54:00first now this will be simpler. Okay. So
  20127. 12:54:02it will be const and then I'm simply
  20128. 12:54:04going to d it. Let's give this one as
  20129. 12:54:06update admin task status.
  20130. 12:54:11Okay. and we going to send something
  20131. 12:54:13here. So you'll understand what actually
  20132. 12:54:14we need to send. But first let's create
  20133. 12:54:16our services. So I'm simply going to do
  20134. 12:54:18export async
  20135. 12:54:21function and I'm simply going to do
  20136. 12:54:23update admin task status. So what you
  20137. 12:54:26need? So first of all you need the task
  20138. 12:54:27ID correct which will be what string and
  20139. 12:54:30then you also need to know status. Why
  20140. 12:54:32I'm keeping unknown extremely important
  20141. 12:54:34because we don't know what value user is
  20142. 12:54:36sending from the UI side. Let's say in
  20143. 12:54:38the UI side you have a drop-down then
  20144. 12:54:39user don't have any other option they
  20145. 12:54:41needs to select those but let's say in
  20146. 12:54:43the UI side they have input text and
  20147. 12:54:44they can put any value right so that
  20148. 12:54:46should not happen so in that case we
  20149. 12:54:48have to give a check here so I'm just
  20150. 12:54:49going to give a promise of task
  20151. 12:54:52so I'm going to give this check this one
  20152. 12:54:54okay so copy this
  20153. 12:54:57paste it here somebody is simply going
  20154. 12:54:59to tell that if this type of
  20155. 12:55:02status is not equal to equal to string
  20156. 12:55:07And if not equal to task status includes
  20157. 12:55:10status as task status. So I'm simply
  20158. 12:55:12going to tell that status must be
  20159. 12:55:13between open in progress or result.
  20160. 12:55:16Right? So this is the first thing. Now
  20161. 12:55:19after this I'm simply going to do con
  20162. 12:55:21task and this will be ait update task
  20163. 12:55:25status
  20164. 12:55:28and this will be our task state id and
  20165. 12:55:31status both you have to pass. Okay. if
  20166. 12:55:34this task is not present. So in that
  20167. 12:55:37case we're going to throw
  20168. 12:55:39new app error 404
  20169. 12:55:43and I'll do task not found and then I'm
  20170. 12:55:47simply going to return the task.
  20171. 12:55:52So here now first let's go because from
  20172. 12:55:54our routes you need to import this. So
  20173. 12:55:56import this
  20174. 12:55:58here and then what you have to pass. So
  20175. 12:56:00first you have to pass the task ID. So
  20176. 12:56:02how you're going to get the task id? So
  20177. 12:56:03you're going to get from request
  20178. 12:56:05dotparams dot task id and then what you
  20179. 12:56:08have to you have to pass the actual
  20180. 12:56:10status. This actual status from where
  20181. 12:56:12you're going to get see this part we've
  20182. 12:56:14already done multiple times. So you're
  20183. 12:56:16going to get from your from your request
  20184. 12:56:18body. So request body dot status. Okay.
  20185. 12:56:22And now what you can do right you can
  20186. 12:56:23simply return the responsible race dot
  20187. 12:56:25status of 200
  20188. 12:56:28dot JSON and I'm simply going to do
  20189. 12:56:31success will be to data will be uh the
  20190. 12:56:35task that we are getting. So this is
  20191. 12:56:38fine. Now we have to create our
  20192. 12:56:40repository. Repository means we have to
  20193. 12:56:42create our actual task to update the
  20194. 12:56:44task. Right? So first let's go here and
  20195. 12:56:47then we'll do export
  20196. 12:56:50function this.
  20197. 12:56:53So we'll get the task ID which will be
  20198. 12:56:55string
  20199. 12:56:57and status and this will be string. Why?
  20200. 12:57:00Because here we are already checking if
  20201. 12:57:01it is not string. So that means if it is
  20202. 12:57:03not string in this case so we can
  20203. 12:57:06actually uh like take this one as a
  20204. 12:57:08string here because we already have a
  20205. 12:57:10check. So that is the reason giving all
  20206. 12:57:12or adding all these checks are very
  20207. 12:57:13important. So now what you have to do?
  20208. 12:57:15So you have to return a promise of task
  20209. 12:57:18or this can return null also it's
  20210. 12:57:20possible. So now just write the query
  20211. 12:57:23right. So we'll do con result. So we'll
  20212. 12:57:25do a pull dot query and here I'm simply
  20213. 12:57:29going to pass a task row and now we can
  20214. 12:57:32simply create it. So I'm simply going to
  20215. 12:57:34do select sorry what I'm doing man
  20216. 12:57:38update. So the update support tasks. So
  20217. 12:57:41here we are simply going to tell set
  20218. 12:57:43because we're updating status property
  20219. 12:57:45right. So status will be equal to one.
  20220. 12:57:49Now also I think you already know we
  20221. 12:57:51need to update the updated at property
  20222. 12:57:52also which will be now current time and
  20223. 12:57:55then what we going to do where the id is
  20224. 12:57:57equal to this because based on the ID
  20225. 12:58:00only you have to understand that what
  20226. 12:58:01actually we have to update. So do
  20227. 12:58:04returning we need that id title status
  20228. 12:58:08user id created at and updated at
  20229. 12:58:14okay and then we can simply pass out. So
  20230. 12:58:17first one will be your status status
  20231. 12:58:19will be the status and id will be your
  20232. 12:58:22task id and then we are going to return
  20233. 12:58:26result dot rows of oh I think I have to
  20234. 12:58:30do let's keep result only. So do result
  20235. 12:58:33dot rows of zero or else this will be
  20236. 12:58:36null. So save this and now we can do
  20237. 12:58:39just import this from here.
  20238. 12:58:42What I'm doing man we're getting status
  20239. 12:58:51but we are already giving check here if
  20240. 12:58:53type of status is not equal to string.
  20241. 12:58:57Oh we have to give or
  20242. 12:59:00that is the reason. So if it is not
  20243. 12:59:02equal to string or if this is the case.
  20244. 12:59:04So now I think this should work. Okay.
  20245. 12:59:07So to check this what we can do. So
  20246. 12:59:09first of all let's say let's do one by
  20247. 12:59:11one. Okay. So first I will go to my uh
  20248. 12:59:14let's go here. Let's try to get all the
  20249. 12:59:16task. So we're getting two task. Let's
  20250. 12:59:19create one more. So this will be patch.
  20251. 12:59:23And now what I'm going to do right will
  20252. 12:59:25copy this.
  20253. 12:59:27And let's say I wanted to update this
  20254. 12:59:29particular second task status from open
  20255. 12:59:32to in progress or result. So I'm going
  20256. 12:59:34to copy this
  20257. 12:59:36uh paste it. Then I'll do status. But
  20258. 12:59:38see what I'll do. I'll go to body and
  20259. 12:59:40I'll try to send the status as random
  20260. 12:59:45value. Okay. Let's do one by one. So
  20261. 12:59:47first we'll be getting what token is
  20262. 12:59:49required. Access token is needed.
  20263. 12:59:51Fantastic. Now let's go to login and try
  20264. 12:59:53to login with the normal user. Send
  20265. 12:59:56this. Get the token.
  20266. 12:59:59Go here. Go to headers. Add
  20267. 13:00:01authorization. Add beer. And now see
  20268. 13:00:05that you're going to get admin access is
  20269. 13:00:07required. Fantastic. Now we'll go here.
  20270. 13:00:10And then I'm just going to login with my
  20271. 13:00:12norm uh admin user. Get the token.
  20272. 13:00:17Go here and change the token. So do be
  20273. 13:00:21error
  20274. 13:00:23and see what will happen. send this
  20275. 13:00:25status must be between open in progress
  20276. 13:00:27and result. So that means you just can't
  20277. 13:00:29pass any random value. So now I'm just
  20278. 13:00:31simply going to do in progress. Let's
  20279. 13:00:32say I wanted to do in progress. Send
  20280. 13:00:35this updated this is in progress. Now
  20281. 13:00:37you go to get and try to send this again
  20282. 13:00:40and you're going to get in progress.
  20283. 13:00:42Fantastic. So that means update is now
  20284. 13:00:44working. See the main idea of creating
  20285. 13:00:46this project. You have to understand how
  20286. 13:00:47you can implement CRUD especially what
  20287. 13:00:49is the authentication middleware. What
  20288. 13:00:52is RBC means? ROB based access control.
  20289. 13:00:54For example, just now we have done
  20290. 13:00:55right. So see if you're a normal user,
  20291. 13:00:56you can't update the status only admin
  20292. 13:00:58can update status. Okay. So now see with
  20293. 13:01:01[snorts] this we actually have completed
  20294. 13:01:03our CR section. I think we have done
  20295. 13:01:05almost all the things enough thing that
  20296. 13:01:07you should know. Okay. I know that we
  20297. 13:01:09haven't covered so many complicated
  20298. 13:01:11things like pagination and so many other
  20299. 13:01:13logic there but I think it's not
  20300. 13:01:14possible to include each and everything
  20301. 13:01:15in a single video. But with this so what
  20302. 13:01:18will be the next step? Now this is
  20303. 13:01:21actually now things will be very
  20304. 13:01:23interesting. Okay. So listen carefully
  20305. 13:01:25what I'm going to say. Next part what
  20306. 13:01:28I'm going to do right I will be adding
  20307. 13:01:30my uh I think that is I think recently I
  20308. 13:01:32created I think 2 3 weeks back I created
  20309. 13:01:35a radius video that particular video
  20310. 13:01:38that I'm going to add in the next part.
  20311. 13:01:40So it will be around 2 hours. It is very
  20312. 13:01:42very very important. Again if you really
  20313. 13:01:44want to learn NodeJS properly I will
  20314. 13:01:46highly recommend just like I mentioned
  20315. 13:01:48in the postgress right please watch the
  20316. 13:01:50radius video also because in that
  20317. 13:01:52particular video you are going to
  20318. 13:01:53understand radius concept radius basics
  20319. 13:01:56and also how you can use radius in a
  20320. 13:01:58real NodeJS project. Now why this is
  20321. 13:02:00very important because in this
  20322. 13:02:02particular project now we are going to
  20323. 13:02:03implement our file upload feature. Now
  20324. 13:02:05in the file upload feature we are not
  20325. 13:02:07going to only have file upload we are
  20326. 13:02:08going to have file upload plus radius
  20327. 13:02:11plus we are going to have background
  20328. 13:02:13job. we are going to use boolean q right
  20329. 13:02:15so this is the reason you need to watch
  20330. 13:02:17the next section and there is a reason
  20331. 13:02:20why I'm adding all of these okay so
  20332. 13:02:21don't think like this is just like I'm
  20333. 13:02:23randomly adding all these video if that
  20334. 13:02:25is the case I already have let's say
  20335. 13:02:26five or six nodejs videos I I think I
  20336. 13:02:29should have added each and everything
  20337. 13:02:30and this video will be 50 hours long
  20338. 13:02:32doesn't work like that so that is the
  20339. 13:02:34reason decided to add only postgress and
  20340. 13:02:36the radius part because it's actually
  20341. 13:02:37important if you don't cover or don't
  20342. 13:02:39watch that section you will not be able
  20343. 13:02:41to understand because I'm not going to
  20344. 13:02:42covered basics in this one. Okay, this
  20345. 13:02:44will be only practical and I'm going to
  20346. 13:02:46give you the basic idea that how you can
  20347. 13:02:47implement radius and bul MQ and
  20348. 13:02:49implement background job and background
  20349. 13:02:51workers. So I'm repeating once again
  20350. 13:02:53next part will be radius complete
  20351. 13:02:54dedicated section watch that particular
  20352. 13:02:58section and then you can directly jump
  20353. 13:02:59to the timestamp and you can implement
  20354. 13:03:01the this particular project the rest
  20355. 13:03:03whatever is left. So what are the things
  20356. 13:03:05you have to do? You have to do file
  20357. 13:03:06upload radius implementation and booq in
  20358. 13:03:09the next part. Hey everyone welcome
  20359. 13:03:11back. So in this particular video we are
  20360. 13:03:12going to cover each and everything that
  20361. 13:03:14you need to know to start your journey
  20362. 13:03:16with radius. Now this is a completely
  20363. 13:03:18practical project based video and for
  20364. 13:03:22this one we are going to use a uh NodeJS
  20365. 13:03:24and express application and we are going
  20366. 13:03:26to use docker and posgress as our DB.
  20367. 13:03:28Now before [snorts] I'm going to discuss
  20368. 13:03:30the curriculum as usual I wanted to
  20369. 13:03:32mention one uh important point and that
  20370. 13:03:34is you can see that I clearly mentioned
  20371. 13:03:35that if this particular video gets more
  20372. 13:03:38than 2,000 likes because I'm getting so
  20373. 13:03:39many request I'm going to create uh
  20374. 13:03:42another video on posgress that will be
  20375. 13:03:44advanced part two and also radius part
  20376. 13:03:46two where I'm going to cover some of the
  20377. 13:03:48uh more advanced topics like sessu
  20378. 13:03:51background jobs and all the other
  20379. 13:03:54products and level patterns that you
  20380. 13:03:55need to know and also please like this
  20381. 13:03:58video comment comment any possible if
  20382. 13:03:59you can share with your friends it will
  20383. 13:04:01be very very helpful so what are the
  20384. 13:04:03things that we are going to cover so
  20385. 13:04:04first we'll start with what is radius
  20386. 13:04:07why actually we need it and if I go to
  20387. 13:04:10the details you can see that I added all
  20388. 13:04:11the details that why radius is used as
  20389. 13:04:13first inmemory data store where radius
  20390. 13:04:16fits in a real backend project right the
  20391. 13:04:18difference between radius and the normal
  20392. 13:04:20database and all the other things then
  20393. 13:04:22we are going to start with adding radius
  20394. 13:04:25to our backend project as I already
  20395. 13:04:27mentioned we are going to use a NodeJS
  20396. 13:04:28project. So you're going to understand
  20397. 13:04:30how you can start Radius locally with
  20398. 13:04:32docker install radius client package
  20399. 13:04:35create reusable radius connection file
  20400. 13:04:37and also we are going to understand very
  20401. 13:04:39very important thing that radius as the
  20402. 13:04:41first inmemory layer that we are going
  20403. 13:04:43to use in this backend project. Then we
  20404. 13:04:46are going to understand a complete
  20405. 13:04:48dedicated section on radius data type.
  20406. 13:04:50So you're going to understand hashes,
  20407. 13:04:52list, sets, sorted list at the beginner
  20408. 13:04:55level. How you are going to use counters
  20409. 13:04:58using radius commands and then all these
  20410. 13:05:01concepts we're going to apply in our
  20411. 13:05:03backend project. Okay. So that will be
  20412. 13:05:05another dedicated section where we are
  20413. 13:05:07going to add radius caching to our
  20414. 13:05:08product APIs. Moving to the next one
  20415. 13:05:11which will be another important section.
  20416. 13:05:12We are going to understand cash
  20417. 13:05:13invalidation still cash problem. Right?
  20418. 13:05:16So here we are going to use cast asset
  20419. 13:05:18pattern. understand cash uh issue
  20420. 13:05:21invalidate uh patterns right so there
  20421. 13:05:23will be lot of things we are also going
  20422. 13:05:25to understand or we are going to have a
  20423. 13:05:26dedicated section on TTL okay then uh we
  20424. 13:05:30are going to have another section on uh
  20425. 13:05:31how you can implement rate limiting with
  20426. 13:05:34radius and at the end we are going to
  20427. 13:05:35end this particular video with pubsub or
  20428. 13:05:37how you can actually implement uh
  20429. 13:05:40publish and subscriber pattern using
  20430. 13:05:42radius so this is what we are going to
  20431. 13:05:44understand in this particular video I'm
  20432. 13:05:46pretty sure you're going to learn a lot
  20433. 13:05:49things and as usual the source code will
  20434. 13:05:51will be added in the description so you
  20435. 13:05:53can download from free from my website
  20436. 13:05:56and also I wanted to mention I think
  20437. 13:05:57most of you already aware that I am
  20438. 13:05:59planning to launch a huge course on my
  20439. 13:06:02uh website that is not yet live so if
  20440. 13:06:04you want you can join the weight list
  20441. 13:06:05that is completely free so you can see
  20442. 13:06:07that almost more than 4,000 people
  20443. 13:06:09already joined so with that [snorts] now
  20444. 13:06:11let's get started and good luck all
  20445. 13:06:13right so now let's get started see the
  20446. 13:06:15first thing I wanted to mention that
  20447. 13:06:17this video is not for complete
  20448. 13:06:18beginners. So what I mean by this, you
  20449. 13:06:21should have at least some idea on node
  20450. 13:06:23express. Okay. And also some basic idea
  20451. 13:06:26how usually docker works, how we can
  20452. 13:06:28create docker compos file and some of
  20453. 13:06:30the very basic concepts like how you can
  20454. 13:06:32create controllers, services, express
  20455. 13:06:35routes, some of the postgress queries.
  20456. 13:06:37Now why this is important? Uh so first I
  20457. 13:06:39wanted to mention because see if you
  20458. 13:06:42don't have all of these right, it will
  20459. 13:06:43be very difficult for you to understand
  20460. 13:06:44this particular project. Why? So this is
  20461. 13:06:47a completely practical project. So what
  20462. 13:06:48I mean by practical? So as you can see
  20463. 13:06:50that I have a project created. Okay. So
  20464. 13:06:53here we already have all the routes and
  20465. 13:06:54everything. So we're not going to write
  20466. 13:06:56each and every logic because this course
  20467. 13:06:59is going to only on radius. So what we
  20468. 13:07:02going to do? We are only going to apply
  20469. 13:07:04the radics related concepts practically
  20470. 13:07:07and by implementing practically we are
  20471. 13:07:09going to learn the concepts. I hope
  20472. 13:07:10you're getting and also before we'll
  20473. 13:07:12implement the concepts we are going to
  20474. 13:07:14have some kind of conceptual and some
  20475. 13:07:15very small examples so that you can
  20476. 13:07:18understand that okay what uh that
  20477. 13:07:20particular concept is and where you are
  20478. 13:07:22going to apply that particular uh
  20479. 13:07:23concept in a real project. So first
  20480. 13:07:26before uh we will jump into radius
  20481. 13:07:30concept first you have to understand the
  20482. 13:07:31project structure that what actually we
  20483. 13:07:33are using here. Okay. So as you can see
  20484. 13:07:35that we have a simple uh node express
  20485. 13:07:37project. So for this one we're using
  20486. 13:07:39docker with posgress. So for our DB so
  20487. 13:07:41you can see that in the docker compose
  20488. 13:07:44we just have a service. So this is our
  20489. 13:07:46image container name uh the username
  20490. 13:07:50password and blah blah all the other
  20491. 13:07:51things right. So we have the port
  20492. 13:07:53volumes and you can see that all these
  20493. 13:07:54details also have added in the env right
  20494. 13:07:56so we have a database host database port
  20495. 13:07:59database user database password database
  20496. 13:08:02name. Okay. Now what I'm going to do
  20497. 13:08:04right, I'm going to add this starter
  20498. 13:08:06code in the attachment. Very very
  20499. 13:08:08important. First you have to download
  20500. 13:08:09this and then on top of this starter
  20501. 13:08:12code you are going to implement all the
  20502. 13:08:14logic that we are going to do. Now next
  20503. 13:08:16will be so let's start with the first
  20504. 13:08:18one. So you can see that we have a DB
  20505. 13:08:19folder we have a pool. DS. So here we're
  20506. 13:08:21just creating the connection pool.
  20507. 13:08:24Right? So we have the new pool from the
  20508. 13:08:26pg package. We have av package and
  20509. 13:08:28before that also I want do. And if you
  20510. 13:08:30go to package.js JS and you can see that
  20511. 13:08:32we have chords envir and pg and we have
  20512. 13:08:35a start uh script db migrate that I'm
  20513. 13:08:38going to explain and the db sid uh so
  20514. 13:08:41what this is going to do this is going
  20515. 13:08:43to do the migration right this script
  20516. 13:08:45and then we have a simple dummy data
  20517. 13:08:47that I have created that we are going to
  20518. 13:08:48use a demo products and this is going to
  20519. 13:08:52seed this particular data in your
  20520. 13:08:53posgress db next let's move on to the
  20521. 13:08:56services so what services is going to do
  20522. 13:08:57services is actually going to interact
  20523. 13:08:59with your posgress DB. So that is the
  20524. 13:09:02reason first we have a get all products
  20525. 13:09:04which is going to fetch all the
  20526. 13:09:05products. Then you have get product by
  20527. 13:09:07ID uh and then create product and update
  20528. 13:09:10product. So if you go to the postman so
  20529. 13:09:12you can see that we have all these
  20530. 13:09:13routes. We have a health route products
  20531. 13:09:15to fetch the products right and then we
  20532. 13:09:18have the products uh filter products by
  20533. 13:09:21ID creating a product and updating a
  20534. 13:09:23particular product. Next move on to the
  20535. 13:09:25controllers. controller is nothing but
  20536. 13:09:27your uh logic where we are going to
  20537. 13:09:29actually return our response back. Let's
  20538. 13:09:31say we are having some front end. Uh so
  20539. 13:09:33I'm not explaining all of this. If you
  20540. 13:09:34have some idea, you'll understand what
  20541. 13:09:36actually this thing is doing. And this
  20542. 13:09:37is also going to call the product
  20543. 13:09:39service, right? Then uh we have a simple
  20544. 13:09:41middleware and then the routes file. So
  20545. 13:09:43routes file is going to have your
  20546. 13:09:45products related routes file and one
  20547. 13:09:48index dot ts file which is your root
  20548. 13:09:51route file. And this root route file we
  20549. 13:09:53are going to use where we are going to
  20550. 13:09:54use in the app.ts ts file right so you
  20551. 13:09:56can see that we are using creating the
  20552. 13:09:58express application using the course
  20553. 13:10:00using the express json middleware using
  20554. 13:10:02the routes and then exponing the app and
  20555. 13:10:04this app we are going to use in our main
  20556. 13:10:07file so this is our main root file right
  20557. 13:10:09serverts we are starting the server we
  20558. 13:10:11are creating or testing the DB
  20559. 13:10:13connection and then you can see that we
  20560. 13:10:15are listening to this particular port so
  20561. 13:10:17this is very very important to
  20562. 13:10:19understand this uh project structure or
  20563. 13:10:21else it will be difficult for you to
  20564. 13:10:24understand that what actually we are
  20565. 13:10:25going to do in the next section. Now
  20566. 13:10:26remember as I already mentioned this is
  20567. 13:10:28a radius course. We are not going to
  20568. 13:10:31write too many logic and everything and
  20569. 13:10:33we're not going to go into too much
  20570. 13:10:34advanced details for now but the uh
  20571. 13:10:37information that I'm going to give you
  20572. 13:10:38is going to give you enough information
  20573. 13:10:40to get started with radius. So what will
  20574. 13:10:43be the next step and what structure we
  20575. 13:10:45are going to follow. So first of all
  20576. 13:10:47what we need to do right. So let's uh do
  20577. 13:10:49one thing let's install the radius
  20578. 13:10:51package first we have to do that. So
  20579. 13:10:53what I'm going to do right so let's go
  20580. 13:10:55here and then I'm just going to do we
  20581. 13:10:57haven't started anything that what is
  20582. 13:10:58radius and everything that we're going
  20583. 13:11:00to do. So first download the starter
  20584. 13:11:02code very important okay if you don't do
  20585. 13:11:04it it will be difficult I'm repeating
  20586. 13:11:06again and again download it and then we
  20587. 13:11:08are just going to do npm install radius
  20588. 13:11:11and this you have to do on the root
  20589. 13:11:12that's it okay so this is going to
  20590. 13:11:14install the radius package this is done
  20591. 13:11:17so now first of all we have to
  20592. 13:11:18understand that what actually radius is
  20593. 13:11:21and why actually we need radius right so
  20594. 13:11:23this is important because uh this is a
  20595. 13:11:25very important interview questions also
  20596. 13:11:27why because when you have something
  20597. 13:11:28called post or let's say you're using
  20598. 13:11:30some different DB why actually you need
  20599. 13:11:32radius so first of all you you are going
  20600. 13:11:34to write that radius is nothing but your
  20601. 13:11:37in-memory data store right so this will
  20602. 13:11:39be your in-memory data store okay now
  20603. 13:11:44what is the meaning of this so if you go
  20604. 13:11:45to here right so let's always try to use
  20605. 13:11:48this AI mode okay side by side or you
  20606. 13:11:49can use some chat GP and everything it's
  20607. 13:11:52very helpful okay so if you are watching
  20608. 13:11:54a video and at the same time make use of
  20609. 13:11:55these tools it will be very very easy so
  20610. 13:11:59here you can See the words telling that
  20611. 13:12:00okay it's a inmemory nosql data
  20612. 13:12:02structure something like that but we
  20613. 13:12:03have to understand that what actually in
  20614. 13:12:05memory means so that simply means the
  20615. 13:12:08radius that we have right it's going to
  20616. 13:12:10keeps your data in RAM okay
  20617. 13:12:13[clears throat] you're getting so it's
  20618. 13:12:14going to keeps data mainly in RAM so
  20619. 13:12:19what will happen if we have something in
  20620. 13:12:21RAM so in a simple way the read and
  20621. 13:12:23write operation will be extremely fast
  20622. 13:12:26or very fast right so read and write
  20623. 13:12:28operations questions will be very fast.
  20624. 13:12:32So this is one of the main reason. Now
  20625. 13:12:33why actually you need ready? So what are
  20626. 13:12:35the best use cases interview questions
  20627. 13:12:38right? So for example let's say we want
  20628. 13:12:39to implement some kind of cash. So here
  20629. 13:12:42okay let me just give here. So we want
  20630. 13:12:44to implement some kind of cash. It's
  20631. 13:12:46very important right? Let's say we have
  20632. 13:12:48to implement some kind of rate limiter.
  20633. 13:12:50I'm giving very basic example. Okay
  20634. 13:12:52there are so many advanced use cases
  20635. 13:12:54radius can have. Let's say you want to
  20636. 13:12:56implement some kind of message broker
  20637. 13:13:00right or uh maybe okay let's say you
  20638. 13:13:04have a normal database right so in our
  20639. 13:13:05case we're using postgress now we want
  20640. 13:13:08to implement or we want to keep some
  20641. 13:13:10kind of temporary first layer in front
  20642. 13:13:12of our existing database so it's like
  20643. 13:13:15something like this so we have a temp
  20644. 13:13:16first layer in front of our normal
  20645. 13:13:22database or main database right
  20646. 13:13:25So these all will be the use cases. Now
  20647. 13:13:29now the next question will be why radius
  20648. 13:13:31is used even though we have a normal
  20649. 13:13:33back end right for example in our case
  20650. 13:13:35we're using posgress right extremely
  20651. 13:13:37important concept you can't uh skip all
  20652. 13:13:39of these so even though our normal
  20653. 13:13:43database is what it's actually for your
  20654. 13:13:46permanent structured data. So whatever
  20655. 13:13:48database you're using that is for your
  20656. 13:13:51permanent structured
  20657. 13:13:54data right what radius is going to do
  20658. 13:13:57right so there can be some task that
  20659. 13:14:00whatever application that we are
  20660. 13:14:01building that we need to have some kind
  20661. 13:14:03of speed
  20662. 13:14:05or some kind of temporary data and this
  20663. 13:14:09is where radius can be helpful right so
  20664. 13:14:11this is where radius can be helpful how
  20665. 13:14:13so it can be for example let's say we
  20666. 13:14:15have a product list and I want to c that
  20667. 13:14:18product list. So caching product list.
  20668. 13:14:21So this is where you need radius or
  20669. 13:14:23let's say you want to count some kind of
  20670. 13:14:25product views. How many views are there
  20671. 13:14:28for that particular product or you want
  20672. 13:14:30to limit that how many request
  20673. 13:14:34a particular user can do right or can
  20674. 13:14:37make
  20675. 13:14:39or you can do running background checks
  20676. 13:14:41uh sorry background jobs also running
  20677. 13:14:43background jobs. So this is where you
  20678. 13:14:44need radius. What radius? Radius is
  20679. 13:14:47going to help. So it's going to help
  20680. 13:14:48with this particular kind of task. Why?
  20681. 13:14:51As I already mentioned, it's going to
  20682. 13:14:52store the data in your RAM. It will be
  20683. 13:14:54much faster for your sort lived. Sort
  20684. 13:14:57lived. Okay, this is not permanent data
  20685. 13:15:00or your frequent frequent accessed data.
  20686. 13:15:05So this will be the main reason why we
  20687. 13:15:07need this. Okay. Now what is the
  20688. 13:15:10difference between? So as I already
  20689. 13:15:12mentioned right it is a inmemory data
  20690. 13:15:13store but what posgress usually do? So
  20691. 13:15:15Postgress is our normal DB, right?
  20692. 13:15:18Postgress is going to store the data.
  20693. 13:15:20It's going to store the data where in so
  20694. 13:15:23on disk. Okay. Why? Because this is a
  20695. 13:15:27long-term storage versus radius is going
  20696. 13:15:30to store in memory. Okay. So in RAM so
  20697. 13:15:33it will be much faster. So if someone
  20698. 13:15:36ask you okay what is the one single
  20699. 13:15:37reason of using radius? Now remember the
  20700. 13:15:40there can be so many reasons but number
  20701. 13:15:43one reason will be the speed right. So
  20702. 13:15:45the accessing of data will be much
  20703. 13:15:47faster. So this would be the very basic
  20704. 13:15:51definition what what is radius why
  20705. 13:15:53actually we need radius even though we
  20706. 13:15:55have a normal database right very very
  20707. 13:15:57important concept all these so now what
  20708. 13:16:00we going to do right so first of all
  20709. 13:16:01let's do one thing we are going to do in
  20710. 13:16:02a different way so first we are going to
  20711. 13:16:05understand some of the very basic things
  20712. 13:16:07for example some of the radius data
  20713. 13:16:09types we have to understand so what we
  20714. 13:16:12can do right so directly you can create
  20715. 13:16:14a examples folder so that is the reason
  20716. 13:16:16I mentioned that please create this uh
  20717. 13:16:19uh sorry please for the uh download not
  20718. 13:16:21for download the startup project it's
  20719. 13:16:23very helpful okay so here I'm just going
  20720. 13:16:25to do this one as radius data types and
  20721. 13:16:29I'm not going to spend too much time on
  20722. 13:16:31explaining so many things let's keep
  20723. 13:16:32this whole uh video very very p
  20724. 13:16:35practical and we'll see that it will be
  20725. 13:16:37very easier for you to understand all
  20726. 13:16:39right so first of all what we need to do
  20727. 13:16:41right so we need to do something we need
  20728. 13:16:44to add our uh radius image in for uh
  20729. 13:16:48docker combos correct so first of all
  20730. 13:16:50what are you going to do so let's add it
  20731. 13:16:52here so let's add one more here so I'm
  20732. 13:16:56just going to do this one as radius and
  20733. 13:16:58here what I'm going to do so this will
  20734. 13:17:00be so I'm just going to add a image so
  20735. 13:17:01what will be the image so for this one
  20736. 13:17:03I'm just going to do radius colon 7 so
  20737. 13:17:06let's use radius 7
  20738. 13:17:10and here I'm just going to use the
  20739. 13:17:11container name so let's give this one as
  20740. 13:17:14radius
  20741. 13:17:16uh ports radius something like this
  20742. 13:17:18whatever you want to give I'm going to
  20743. 13:17:20do restart and this will be unless
  20744. 13:17:23stopped
  20745. 13:17:25okay let's use the ports so normally
  20746. 13:17:27radius uses this 63 79 port okay so this
  20747. 13:17:31is what we are going to map and then we
  20748. 13:17:34are also going to have a volumes
  20749. 13:17:37so for volumes this will be my radius
  20750. 13:17:41data and here I'm just going to do /
  20751. 13:17:44data and here we're going to have the
  20752. 13:17:45radius
  20753. 13:17:47data. Okay, so this is what we need.
  20754. 13:17:51Okay, so this will be the first thing.
  20755. 13:17:52So what are you going to do, right? So
  20756. 13:17:54let's go here.
  20757. 13:17:56So I'm just going to do docker compose
  20758. 13:17:59down and then I'll docker compose up -
  20759. 13:18:04d.
  20760. 13:18:07Okay, so what you're getting uh
  20761. 13:18:10networking this is already in used.
  20762. 13:18:12Okay, so you're already using this.
  20763. 13:18:16Maybe let's use this one. Change this
  20764. 13:18:18one to some different code
  20765. 13:18:22and then run once again.
  20766. 13:18:25Okay, so it's running. So let's verify
  20767. 13:18:27once
  20768. 13:18:30go to image. Okay, we have ready. This
  20769. 13:18:33is running. So now what we need to do,
  20770. 13:18:35right, we also need to add this in our
  20771. 13:18:37env. So in our env we don't have. So
  20772. 13:18:39what we're going to do, right, I'm just
  20773. 13:18:40going to add one more. So let's do this
  20774. 13:18:42one as radius_l
  20775. 13:18:44and this will be radius and this is
  20776. 13:18:47running on local host col 6380
  20777. 13:18:52right whatever port you are using that
  20778. 13:18:54you have to use it so using 6380
  20779. 13:18:57so now I think this is done so first of
  20780. 13:18:58all what we are going to do first we
  20781. 13:19:01will understand the basic radius data
  20782. 13:19:03types or else we can't directly jump to
  20783. 13:19:06the practical part okay very important
  20784. 13:19:08all right so I'm just going to explain
  20785. 13:19:10that what are the things we're going to
  20786. 13:19:11learn. Okay. So, first I'm just going to
  20787. 13:19:14So, here I'm just going to write. So,
  20788. 13:19:15we're going to uh learn about string.
  20789. 13:19:18Let's do has also I'm just going to
  20790. 13:19:22let's do list set. There are so many.
  20791. 13:19:25Okay. Then you have something called
  20792. 13:19:27sorted
  20793. 13:19:30sorted set and then you have TTL. Okay.
  20794. 13:19:33So, these all the things we are going to
  20795. 13:19:35understand. At the same time, I'm also
  20796. 13:19:37going to give you the basic concept side
  20797. 13:19:40by side and you'll see that how much
  20798. 13:19:41easier it would be for you to understand
  20799. 13:19:43all of these. So let's do this one in
  20800. 13:19:45the next part. Okay. So let's start with
  20801. 13:19:47string, right? So we have strings. So
  20802. 13:19:49what is a radius string? The definition
  20803. 13:19:52will be radics uh radius string stores
  20804. 13:19:56one value under one key. Okay. So this
  20805. 13:19:59usually stores
  20806. 13:20:02one value under one key. Okay, what are
  20807. 13:20:06the things that it can store? Right. So
  20808. 13:20:08for example, first of all, plain text if
  20809. 13:20:11you want to store. Next, it can also
  20810. 13:20:14stored numbers
  20811. 13:20:16stored as text. Okay, so these two and
  20812. 13:20:20also something like counters.
  20813. 13:20:23So this will be very simple definition.
  20814. 13:20:25Now what will be the example? Let's say
  20815. 13:20:26you have a key, right? So your key is
  20816. 13:20:29something like we are let's say you're
  20817. 13:20:31uh counting some page views that how
  20818. 13:20:34many views this particular page gets
  20819. 13:20:36something like that very simple example.
  20820. 13:20:38So there will be some key okay so let's
  20821. 13:20:40say this key is your page views and then
  20822. 13:20:43there will be one value very very simple
  20823. 13:20:45to understand and this value will be
  20824. 13:20:47let's say it's 100 something like this
  20825. 13:20:50okay so that is the reason I mentioned
  20826. 13:20:52it's going to store one value under one
  20827. 13:20:54key okay so now let's understand this
  20828. 13:20:58one with a very simple example so first
  20829. 13:21:00of all what we going to do we already
  20830. 13:21:02have enval so I'm just going to do env
  20831. 13:21:05from dot
  20832. 13:21:07env and also we are going to import
  20833. 13:21:11something
  20834. 13:21:12from our radius package. So just now we
  20835. 13:21:14have installed radius. So what are the
  20836. 13:21:16thing what actually we need here? So we
  20837. 13:21:18need to create a connection. So we need
  20838. 13:21:20to import create client. Okay. So first
  20839. 13:21:23what are we going to do right in our in
  20840. 13:21:25view this rad whatever code that you are
  20841. 13:21:27running and then what you're going to do
  20842. 13:21:29first. So first we need to load the
  20843. 13:21:31config. So I'll do.config.
  20844. 13:21:33So this will be the first thing. And now
  20845. 13:21:35we need our radius connection URL that
  20846. 13:21:38we are going to use to create the
  20847. 13:21:39connection. So I'm just going to do con.
  20848. 13:21:41So I'll do radius URL and this will be
  20849. 13:21:44equal to. So we'll do process env dot
  20850. 13:21:47and we have our radius url right or else
  20851. 13:21:50you can just simply directly pass this
  20852. 13:21:52one here also. So copy this and then
  20853. 13:21:55we're going to pass it here. So now what
  20854. 13:21:57we need to do we're going to create one
  20855. 13:21:58radius client. Okay. Why actually you
  20856. 13:22:02need radius client if someone ask you.
  20857. 13:22:04So radius client will be our main
  20858. 13:22:06connection that we need so that we will
  20859. 13:22:08be able to connect to radius. So I'm
  20860. 13:22:10just going to do con radius. So this
  20861. 13:22:12will be a create client and this
  20862. 13:22:14actually you need the URL. Okay. So this
  20863. 13:22:16is going to do the connection and what
  20864. 13:22:18will be your URL? Your URL will be rad.
  20865. 13:22:21So this is going to create the
  20866. 13:22:22connection. So first of all what we
  20867. 13:22:24going to do first? I'm just going to do
  20868. 13:22:26a sync function and this will be run.
  20869. 13:22:29And here first we need to see that if we
  20870. 13:22:32are able to open the connection to our
  20871. 13:22:35radius server or not. So I'll do open
  20872. 13:22:37connection
  20873. 13:22:38to radius
  20874. 13:22:41server right. So how we going to do
  20875. 13:22:43that? So this we have it and this radius
  20876. 13:22:46instance that we have created this one
  20877. 13:22:48right so here this is going to give a
  20878. 13:22:50connect method and this is going to do
  20879. 13:22:52the connection and then I'm simply going
  20880. 13:22:54to do a console.log log and I'm simply
  20881. 13:22:57going to do connected to radius because
  20882. 13:23:00this we need to see and also I'm just
  20883. 13:23:03going to do console.log log and let's
  20884. 13:23:05say I'm just going to check the ping and
  20885. 13:23:07this ping will be a so you have the
  20886. 13:23:09radius dot ping right so this is going
  20887. 13:23:13to uh explain us sorry not explain this
  20888. 13:23:16is going to tell us that okay if the
  20889. 13:23:18connection is successful or not so now
  20890. 13:23:21what are going to do so this will be a
  20891. 13:23:22promise so do run dot catch in case any
  20892. 13:23:25error so we're going to get the error
  20893. 13:23:28and then I'm simply going to do console
  20894. 13:23:31dot error and I'm Just going to do demo
  20895. 13:23:35or maybe let's what let's do demo field
  20896. 13:23:37because we doing the demo right here. So
  20897. 13:23:39demo field and this will be error and we
  20898. 13:23:42are just going to exit from this
  20899. 13:23:43process. So do process doexit will be
  20900. 13:23:46one. So let's save this. So we we are
  20901. 13:23:50not doing anything else here. Okay.
  20902. 13:23:51We're not adding the example of string
  20903. 13:23:53that we are going to do. So go to
  20904. 13:23:55package dojson and then I'm just going
  20905. 13:23:57to maybe let's do a example colon and
  20906. 13:24:00I'll do radius types. So how we are
  20907. 13:24:04going to run it. So this will be tsx and
  20908. 13:24:08I'm just going to do so we have to do
  20909. 13:24:10example right? So you have to go to
  20910. 13:24:12examples because this is what we are
  20911. 13:24:14using / the radius data type.ts. So I'll
  20912. 13:24:18do radius data type not types.ts.
  20913. 13:24:23So this will be our command that we are
  20914. 13:24:25going to use directly in the terminal.
  20915. 13:24:27So if I now open this let's clear it and
  20916. 13:24:31I'll do npm run enter and you can see
  20917. 13:24:35that it's now connected to radius and
  20918. 13:24:37you're getting ping and this is the
  20919. 13:24:39pong. So that means the connection is
  20920. 13:24:41now successful and see this is the
  20921. 13:24:43reason I when I usually it is right I
  20922. 13:24:45use use docker because docker is if you
  20923. 13:24:48have ID on docker it's very very easy to
  20924. 13:24:50use and also using docker you can do lot
  20925. 13:24:53of things okay see how much easier it is
  20926. 13:24:55to understand that whether the
  20927. 13:24:56connection is successful and if it is
  20928. 13:24:58working or not but one thing you have to
  20929. 13:24:59be make sure that you using correct
  20930. 13:25:01ready URL here if you're doing any
  20931. 13:25:03mistake it's going to fail so this is
  20932. 13:25:05done so now let's start with uh what we
  20933. 13:25:09have to do Okay, so let's start with
  20934. 13:25:10some very basic string example, right?
  20935. 13:25:12So we'll go here and then I'm just going
  20936. 13:25:14to do strings
  20937. 13:25:17and first let's do one thing. What I
  20938. 13:25:19usually mention that there will be one
  20939. 13:25:21key and for one particular key it's
  20940. 13:25:23going to hold one particular value,
  20941. 13:25:26right? So we're going to take a string
  20942. 13:25:27key. Okay, so I'm just going to do
  20943. 13:25:29string key and this will be maybe let's
  20944. 13:25:32say I wanted to do get some page view.
  20945. 13:25:34So I'm just going to do demo page views
  20946. 13:25:37something like this and then what I'm
  20947. 13:25:40going to do right I'm just going to do a
  20948. 13:25:42and now we are going to use some methods
  20949. 13:25:44that you have to understand. So what
  20950. 13:25:45method we are going to use we're going
  20951. 13:25:46to use a set method. So set method it's
  20952. 13:25:49going to store that particular value
  20953. 13:25:51under this key. Okay. So how we going to
  20954. 13:25:53do that? So it's very simple. So do
  20955. 13:25:55radius dot set
  20956. 13:25:57and here you have to give what you have
  20957. 13:25:59to give the key which is radius
  20958. 13:26:00argument. So key is this and then we'll
  20959. 13:26:03give the value right. So let's give this
  20960. 13:26:05one as 100. So this is how you can set a
  20961. 13:26:08particular value based on one key. Okay.
  20962. 13:26:10I'm repeating this thing again and again
  20963. 13:26:12that set is going to do what? It's going
  20964. 13:26:14to store one value under one key. Okay.
  20965. 13:26:18Now let's say I wanted to get this value
  20966. 13:26:19or I wanted to read this value. So how I
  20967. 13:26:22can do that? So now I'm simply going to
  20968. 13:26:24do okay let's take the page views. So
  20969. 13:26:26this will have it. Now I'll do radius
  20970. 13:26:29dot get. And what it was it was a key.
  20971. 13:26:31It's very simple. So we'll do strings
  20972. 13:26:33key and then let's do a log. So I'll do
  20973. 13:26:35console dot log and let's [snorts] take
  20974. 13:26:38the page views. So now let's save this.
  20975. 13:26:41Now what we can do right uh let's
  20976. 13:26:44close this and I'll clear it and then
  20977. 13:26:48run once again and we'll see what you're
  20978. 13:26:50getting. So we're getting 100. Okay. Now
  20979. 13:26:52what you can do right you can use
  20980. 13:26:54another property uh on strings and that
  20981. 13:26:56is called increment. insert something
  20982. 13:26:58like incr. Okay. So what it's going to
  20983. 13:27:01do? It's going to increase the number by
  20984. 13:27:03one. But here you might be thinking okay
  20985. 13:27:04this is a string. But what happens
  20986. 13:27:07writing radius or normally the radius
  20987. 13:27:09strings can also works work like
  20988. 13:27:11counters. So I'm just going to write it.
  20989. 13:27:13So radius
  20990. 13:27:15strings can also work like counters. So
  20991. 13:27:19now let's say if I wanted to do const
  20992. 13:27:22after increment and I'll do a and I'll
  20993. 13:27:25do radius and then you'll do in cr. So
  20994. 13:27:29here what you have to pass. So you have
  20995. 13:27:31to pass a string key, right? So this
  20996. 13:27:34will be the key that you have to pass
  20997. 13:27:35and then you'll just log it once again.
  20998. 13:27:40Right? You'll save and run it again. So
  20999. 13:27:43you can see that you're getting 101.
  21000. 13:27:45Okay? So this is all about the strings.
  21001. 13:27:48Now what will be that uh next step next
  21002. 13:27:50will be h has. Now has is going to do
  21003. 13:27:52what right? So let's understand here
  21004. 13:27:54only.
  21005. 13:27:55So has is going to so I'm just going to
  21006. 13:27:58take this one as all right. So now let's
  21007. 13:28:00understand h has right. So what actually
  21008. 13:28:01radius has is so radius has is going to
  21009. 13:28:04stores many uh many small fields under
  21010. 13:28:07one key. So it's going to stores
  21011. 13:28:10many small uh fields under one key. Now
  21012. 13:28:15what will be a good example for this? So
  21013. 13:28:18one good example for this will be uh so
  21014. 13:28:21let's [snorts] say we have a small
  21015. 13:28:22object right so this will be a good
  21016. 13:28:24example or something like map inside
  21017. 13:28:28radius so this will be your has right so
  21018. 13:28:32now let's say uh there can be any key
  21019. 13:28:35right so there is an any key so this is
  21020. 13:28:38your key name and then you're going to
  21021. 13:28:40have some fields here for that
  21022. 13:28:42particular key so now let's say you have
  21023. 13:28:44a uh field which is name and the name is
  21024. 13:28:49Sam something like this and you also can
  21025. 13:28:52be having a email and this is your email
  21026. 13:28:56something like this so let's understand
  21027. 13:28:58this one with example so what I'll do
  21028. 13:29:00right so let's do this one as const and
  21029. 13:29:03I'll do has key and what we can do maybe
  21030. 13:29:06let's do demo colon user colon profile
  21031. 13:29:11okay so now what we can do simply so I'm
  21032. 13:29:14just going to do a And here we have a
  21033. 13:29:16radius and we need to understand one
  21034. 13:29:18method and that is called h set. So what
  21035. 13:29:21this is going to do this is going to
  21036. 13:29:23store the many fields that we wanted to
  21037. 13:29:25have under this particular key. It's
  21038. 13:29:27going to store that. Okay. So it's like
  21039. 13:29:29a has like a small object inside radius.
  21040. 13:29:33So this is the method we need to do how
  21041. 13:29:36the structure will be. So here you have
  21042. 13:29:38to pass the key. So this will be your
  21043. 13:29:41key and then you can pass the property.
  21044. 13:29:43So let's say I'm going to take a name
  21045. 13:29:46and this will be I think this is wrong.
  21046. 13:29:48This will be capital right? So here uh
  21047. 13:29:51this will be key and then we have to
  21048. 13:29:52take the property. So now let's say I'm
  21049. 13:29:54just going to take this one as sum and
  21050. 13:29:56I'm also going to take a city and let's
  21051. 13:29:59do something like US. Okay. And then
  21052. 13:30:01what you can do right uh so here uh
  21053. 13:30:04let's say I wanted to get this value
  21054. 13:30:07now. Okay. So this is how we are going
  21055. 13:30:09to set some certain fields inside a uh
  21056. 13:30:13inside a key using h has concept right.
  21057. 13:30:15So we are using h set. Now to get this
  21058. 13:30:19I'm just going to do extract profile
  21059. 13:30:21info whatever you wanted to give here.
  21060. 13:30:24So this will be equal to have it. So do
  21061. 13:30:27radius and you're going to use h get
  21062. 13:30:30all. So this is what you have to use.
  21063. 13:30:33And here also you have to pass the key.
  21064. 13:30:35So I'm just going to pass this one as
  21065. 13:30:36has key. Now let's go here and then I'm
  21066. 13:30:39just going to copy this
  21067. 13:30:41and I'll paste it here and let's see
  21068. 13:30:44what you're getting right. So I'll just
  21069. 13:30:46run this one second. So I'm getting name
  21070. 13:30:48and city. Okay. So this is how you're
  21071. 13:30:50going to make use of uh has. Now all
  21072. 13:30:54they might be thinking okay these are
  21073. 13:30:56very uh simple concept but these simple
  21074. 13:30:58concepts are actually important. Why?
  21075. 13:31:00Because when you are going to learn all
  21076. 13:31:02of these and then you're going to let's
  21077. 13:31:03say you're creating a practical project
  21078. 13:31:05there you have to understand whether you
  21079. 13:31:07actually can apply all these concept
  21080. 13:31:09inside your real project you're getting.
  21081. 13:31:11So let's say you have a particular
  21082. 13:31:13endpoint and you wanted to use radius in
  21083. 13:31:16that. Now the requirement is such a way
  21084. 13:31:18that you have to use string concept and
  21085. 13:31:21has concept both. So this is the
  21086. 13:31:23knowledge that you have to acquire that
  21087. 13:31:26okay in that particular scenario you
  21088. 13:31:28will be able to understand that okay
  21089. 13:31:29which are properties I need to apply or
  21090. 13:31:32what are the concepts so what I mean by
  21091. 13:31:34it's not like we are blindly doing all
  21092. 13:31:35of these at the end when we going to do
  21093. 13:31:38practical you'll see that some of the
  21094. 13:31:39things that we are doing here we will be
  21095. 13:31:41applying in a real project and that will
  21096. 13:31:43be your final outcome okay uh next uh
  21097. 13:31:47what else we have okay let's understand
  21098. 13:31:49list
  21099. 13:31:51so list will be so I'm just going to do
  21100. 13:31:55list
  21101. 13:31:56so radius list will be ordered okay so
  21102. 13:32:01this will be ordered collection of
  21103. 13:32:04values okay so again here also you're
  21104. 13:32:07going to have a key and then there will
  21105. 13:32:08be some values okay so this is the
  21106. 13:32:10simplest example that these will be some
  21107. 13:32:12collections of values but it will be
  21108. 13:32:14ordered so now let's see one simple
  21109. 13:32:17example [snorts] so I'll just do here
  21110. 13:32:19const I will do list
  21111. 13:32:22key and here I'm just going to do maybe
  21112. 13:32:24demo let's say I'm just going to take
  21113. 13:32:26some messages in order and here what I
  21114. 13:32:29will do simply first I'm just going to
  21115. 13:32:30have it so I'll do list key and you're
  21116. 13:32:33going to do l push
  21117. 13:32:37no not loose man so here we are going to
  21118. 13:32:40use a radius
  21119. 13:32:43loose
  21120. 13:32:45okay so this is what you're going to use
  21121. 13:32:47you have to pass a key which will be
  21122. 13:32:48list key and let's say I wanted to give
  21123. 13:32:51uh Hello.
  21124. 13:32:54Okay. And again, let's I wanted to add
  21125. 13:32:56some more. So maybe I I'll do hi radius.
  21126. 13:33:00All right. So now let's say we wanted to
  21127. 13:33:01extract. So what I'll do, right, I'm
  21128. 13:33:03just going to do extract
  21129. 13:33:05messages and this will be avid and here
  21130. 13:33:08we're going to have radius dot we have
  21131. 13:33:11to use something called l range. Here
  21132. 13:33:13we're going to pass the list key and
  21133. 13:33:15then we're going to give the start and
  21134. 13:33:17the stop. And now let's log this. Right?
  21135. 13:33:20So I'm just going to do extract
  21136. 13:33:22messages. Save this. And now let's
  21137. 13:33:26clear. And then I'm just going to run it
  21138. 13:33:28again.
  21139. 13:33:30Okay. So you can see that first you're
  21140. 13:33:31getting high radius and then hello. This
  21141. 13:33:33is why because we using l push. Okay.
  21142. 13:33:36Now if you're getting confused, let me
  21143. 13:33:39just quickly show you that diff between
  21144. 13:33:42lus
  21145. 13:33:44and rpus.
  21146. 13:33:48So you can see that now L pus is going
  21147. 13:33:50to do what? It's going to insert the X
  21148. 13:33:53at the left side. Okay. R push is going
  21149. 13:33:56to insert this one on the right side. So
  21150. 13:33:59now if I'm first adding this one, so
  21151. 13:34:01this will be added. When I'm going to
  21152. 13:34:02add high radius, it will be added on the
  21153. 13:34:04left side. So that's why you're getting
  21154. 13:34:06this. You're getting this one. And that
  21155. 13:34:08is the reason you're getting hello on
  21156. 13:34:10the at the end. Okay? Because we using
  21157. 13:34:13loose. If you want to get in the reverse
  21158. 13:34:16order, what you have to use? you to use
  21159. 13:34:18arpus. Okay. So I'm just going to give
  21160. 13:34:19this one as a homework to all of you. So
  21161. 13:34:22this is about the uh list. Next uh what
  21162. 13:34:26we have I think we also have to discuss
  21163. 13:34:28about. So let's uh discuss about set. So
  21164. 13:34:31now I'm just going to do this one as
  21165. 13:34:32set. So what actually set? So set will
  21166. 13:34:35be or okay let me just quickly write it
  21167. 13:34:37here. It will be easy. So loose right.
  21168. 13:34:39So loose will be adds a new item at
  21169. 13:34:44beginning.
  21170. 13:34:47Then you're going to have l range. L
  21171. 13:34:49range is going to do what? It's going to
  21172. 13:34:51reads item items from the list. Simple.
  21173. 13:34:56And then we going to have uh we also
  21174. 13:34:58going to have what? L arpus. So we also
  21175. 13:35:02have the arpus. Arpus is going to will
  21176. 13:35:04be what adds the item
  21177. 13:35:07at end. Okay, we also have something
  21178. 13:35:10called L stream, right? These all are
  21179. 13:35:13very important one. So it's going to do
  21180. 13:35:15what? It's going to keep
  21181. 13:35:17only part of the list. You can also
  21182. 13:35:21verify. So if you go here and let's do L
  21183. 13:35:24trim.
  21184. 13:35:26So L trim is a command to trim an
  21185. 13:35:29existing list. So it contain only the
  21186. 13:35:31specific range of elements. Okay. So you
  21187. 13:35:33have to give the key the start and the
  21188. 13:35:35stop. So this is about the list. So now
  21189. 13:35:40let's move to the set. So what will be
  21190. 13:35:41set? Set is like if you have have some
  21191. 13:35:44idea on JavaScript sets how it usually
  21192. 13:35:46works. So set is usually stores unique
  21193. 13:35:49values only. Okay. So it is going to
  21194. 13:35:51sets unique unique
  21195. 13:35:54sets of values only. Okay. So now uh
  21196. 13:35:59what will happen? Uh this can be
  21197. 13:36:01interview question but this will be very
  21198. 13:36:02beginner level of questions. What will
  21199. 13:36:04happen if I try to add same element
  21200. 13:36:06multiple times? So it's going to ignore
  21201. 13:36:08the one that you have already added.
  21202. 13:36:11Very very simple concept. So how it will
  21203. 13:36:14work? So let's go here and then I'm just
  21204. 13:36:15going to do con. Let's do a set key. And
  21205. 13:36:18I'm just going to let's say I have some
  21206. 13:36:20tags and I wanted to get the tags only.
  21207. 13:36:23And here I'm just simply going to add
  21208. 13:36:24it. And you're going to do radius. And
  21209. 13:36:26now this will be not loose. This will be
  21210. 13:36:29add. So s add is going to add and it's
  21211. 13:36:33going to create a set and it's going to
  21212. 13:36:36add that unique value. So what I mean by
  21213. 13:36:38unique value here. So now let's first of
  21214. 13:36:40all I'm going to give set key and I'm
  21215. 13:36:41adding here node JS. And now I'll just
  21216. 13:36:45uh let's paste it. Let's do next JS and
  21217. 13:36:49I'm just going to do uh next JS again.
  21218. 13:36:52Okay. So there is a duplicate. Okay. So
  21219. 13:36:54what will happen here? So first of all
  21220. 13:36:56let's say I wanted to get or I wanted to
  21221. 13:37:00uh return how many unique members exist
  21222. 13:37:03in this particular set. So how I can do
  21223. 13:37:06that? So first of all let's say I wanted
  21224. 13:37:07to get the tag count and this will be a
  21225. 13:37:10we have to use something called s card.
  21226. 13:37:13Okay. And here we're going to pass a set
  21227. 13:37:15key. Now let's see what is happening.
  21228. 13:37:17Right. So I'm just going to log this.
  21229. 13:37:23So do tag count save.
  21230. 13:37:30Now you can see that you're getting only
  21231. 13:37:31two. Why? Because even though you've
  21232. 13:37:34added n just two times, it is going to
  21233. 13:37:36consider the nes unique. Okay. The first
  21234. 13:37:39one. Next one it's going to ignore.
  21235. 13:37:41Okay. Then we have something called
  21236. 13:37:43sorted set also. Okay. So let's say uh
  21237. 13:37:46I'm just going to do con rank key
  21238. 13:37:49something like this. Rank key. And then
  21239. 13:37:52I'm I'll do demo and let's I just going
  21240. 13:37:54to take some kind of leader board and
  21241. 13:37:57I'll do a radius dot we have something
  21242. 13:38:01called Z add. Okay. So what J uh Z add
  21243. 13:38:05is going to do is going to store your
  21244. 13:38:07member plus scores. When you have some
  21245. 13:38:10kind of uh condition or feature that you
  21246. 13:38:13have to implement where you need to
  21247. 13:38:15actually get the data based on ranking
  21248. 13:38:17it's very very very important concept.
  21249. 13:38:20Okay. So now what we going to do? We'll
  21250. 13:38:22lose Z at. So you have to pass the key.
  21251. 13:38:24So we'll do rank key and I'm just going
  21252. 13:38:26to give a score here. See score will be
  21253. 13:38:29100. Okay. And you have to give a value
  21254. 13:38:32here also. So now let's say this is a
  21255. 13:38:34player A. Now I'm going to copy this and
  21256. 13:38:37paste it here. So this will be score uh
  21257. 13:38:41200 and this will be my player B. So now
  21258. 13:38:44what we going to do right? So let's say
  21259. 13:38:46uh I wanted to do something. I want to
  21260. 13:38:49increase this particular score that I
  21261. 13:38:52have by a particular amount. So to do
  21262. 13:38:55this one, we have something called Z
  21263. 13:38:57increment by that I'm talking about full
  21264. 13:38:59form but in short we have to do
  21265. 13:39:01something like this. So let's say I
  21266. 13:39:02wanted to get the new score. So first I
  21267. 13:39:06will do a so this will be Z. Sorry again
  21268. 13:39:09I'm doing Z. So this will be radius dot
  21269. 13:39:12Z increment by
  21270. 13:39:17you have to pass the key here and let's
  21271. 13:39:19say you have to pass the increment
  21272. 13:39:20number. So let's say by 50 and also you
  21273. 13:39:22have to pass the member. What will be
  21274. 13:39:24your member? Member will be your player
  21275. 13:39:25A. Okay. So here you have to give this
  21276. 13:39:28one as player A.
  21277. 13:39:31So now let's log this. So this will be
  21278. 13:39:35the new score. So now let's save this
  21279. 13:39:40and if I run it again. So what we'll be
  21280. 13:39:43getting? We'll be getting 150. Very very
  21281. 13:39:45simple. So we are just increasing this
  21282. 13:39:47one by 50. Now if you wanted to get the
  21283. 13:39:50rank from highest score to lowest score
  21284. 13:39:54there you have to use something called Z
  21285. 13:39:56rev rank. So I'll do con rank
  21286. 13:40:03and this will be a radius dot z uh we
  21287. 13:40:08have the rev rank okay so here you have
  21288. 13:40:12to pass the rank key and let's say what
  21289. 13:40:15I wanted to do I wanted to pass the
  21290. 13:40:17player B
  21291. 13:40:20okay and here let's I'm just going to do
  21292. 13:40:22a log and maybe I'll do a rank here so
  21293. 13:40:27if I Save this.
  21294. 13:40:31So you can see that you're getting zero.
  21295. 13:40:33Okay. Now why we are getting zero? The
  21296. 13:40:36reason is because zero will be always
  21297. 13:40:38considered as top rank. Okay. So what
  21298. 13:40:41this is going to do? This is going to
  21299. 13:40:43give the rank from highest score to the
  21300. 13:40:45lowest score. So now I think this is
  21301. 13:40:48done. If you are getting confused, let
  21302. 13:40:49me just uh give you one example. Right?
  21303. 13:40:52So I'm just going to copy this and I'll
  21304. 13:40:54just do here. Okay. Tell me why the last
  21305. 13:40:57rank
  21306. 13:40:59console is giving zero. Okay. So let's
  21307. 13:41:03see
  21308. 13:41:05now see it's printing zero because
  21309. 13:41:07calculate zero to index rank order from
  21310. 13:41:09highest score to lowest score. So this
  21311. 13:41:11is I was talking about. Now this might
  21312. 13:41:12be little confusing because when uh I
  21313. 13:41:16I remember I learned for the first time
  21314. 13:41:18right I was like oh my okay what is
  21315. 13:41:20happening here. So this is the reason I
  21316. 13:41:22mentioned. So if you see okay first of
  21317. 13:41:24all see this is going to add this one so
  21318. 13:41:26it's going to get first time when I'm
  21319. 13:41:28going to do Z add it's going to get C
  21320. 13:41:30score of 100 player B is going to get a
  21321. 13:41:32score of 200 fine I added 50 okay so
  21322. 13:41:35it's become 150 right so player A is
  21323. 13:41:37become 150 so now the final reader will
  21324. 13:41:40be what 200 150 so what Z rank is going
  21325. 13:41:44to do I already mean it's going to give
  21326. 13:41:46you the rank from highest score the
  21327. 13:41:48lowest score so who is the lowest sorry
  21328. 13:41:50highest score here still player Correct.
  21329. 13:41:53So that is the reason you are getting
  21330. 13:41:54this. So now it's going to do what? It's
  21331. 13:41:56going to sort the leaderboard in the
  21332. 13:41:57descending order. So highest order is
  21333. 13:41:59going to get the top uh position. So now
  21334. 13:42:02I what I mentioned here that zero will
  21335. 13:42:04be equal to always what? Highest score.
  21336. 13:42:06Okay. Or top rank. Zero will be
  21337. 13:42:10I'm just going to do like this. So this
  21338. 13:42:12is how I don't I remember this one. So
  21339. 13:42:14zero will be considered as top rank.
  21340. 13:42:16Right? So now this will be your uh rank
  21341. 13:42:19zero, first place highest score, player
  21342. 13:42:21B and rank one because we are passing
  21343. 13:42:24player B has the absolute highest score.
  21344. 13:42:27So their reverse rank is exactly zero.
  21345. 13:42:30So when you're passing player B, so
  21346. 13:42:31you're getting this one as zero. What
  21347. 13:42:33will happen if I pass this one as player
  21348. 13:42:34A? I think you can actually guess it. So
  21349. 13:42:37it will become one because this will be
  21350. 13:42:38the second player. Okay, because B is
  21351. 13:42:41first here. Now I'll give you one
  21352. 13:42:43homework because we're adding by 50
  21353. 13:42:45right here. players could do this. Uh
  21354. 13:42:48you can just add one more time, right?
  21355. 13:42:50Let's say you added 100 or 200. Now see
  21356. 13:42:53that the moment player will be increase
  21357. 13:42:56by player player B. So now let's say
  21358. 13:42:58player A score is 300. You'll see that
  21359. 13:43:00when you're going to uh console the
  21360. 13:43:03rank, player A will become the highest
  21361. 13:43:05rank and then the player B something
  21362. 13:43:06like that. Okay. So this is about the uh
  21363. 13:43:09uh how you can work with sorted set.
  21364. 13:43:12Okay. Very important concept. uh what
  21365. 13:43:14else we have? I think we have already
  21366. 13:43:17complete most of the things. So the last
  21367. 13:43:19I think we have to also understand some
  21368. 13:43:20of the very basic concept of TTL. Okay.
  21369. 13:43:24So first uh what I will do right? So
  21370. 13:43:26let's understand very basic concept here
  21371. 13:43:28what actually TTL means. So TTL will be
  21372. 13:43:31as much I can recolct it will be time to
  21373. 13:43:35leave. Okay. Or you can also consider
  21374. 13:43:38this one as expiry. Right. So this is
  21375. 13:43:40expiry. So we can just verify. So TTL
  21376. 13:43:43means
  21377. 13:43:44time to leave, right?
  21378. 13:43:51Okay. Okay. So let's take one. So this
  21379. 13:43:54will be TTL or XY. So what this uh is
  21380. 13:43:58going to be? Sorry, why this is going to
  21381. 13:44:00be helpful? What this is going to tell
  21382. 13:44:02radius? So it is uh it's going to tells
  21383. 13:44:05radius that how long Okay, you can note
  21384. 13:44:07it down. how long a particular key
  21385. 13:44:09should exist before it's going to
  21386. 13:44:12deleted automatically. This is a very
  21387. 13:44:13important concept. Okay. So, it's going
  21388. 13:44:15to tell it tells radius that how long a
  21389. 13:44:20key how long a key should exist
  21390. 13:44:24before being deleted automatically.
  21391. 13:44:31Okay, you're getting so now [snorts]
  21392. 13:44:33maybe let's say I have a key here,
  21393. 13:44:35right? So I have a key of a and then
  21394. 13:44:37this key has a value of something maybe
  21395. 13:44:40any value you can take and I'm giving a
  21396. 13:44:42ttl of 300 seconds. So what will happen
  21397. 13:44:46that means after 5 minutes radius is
  21398. 13:44:49going to radius is going to delete this
  21399. 13:44:54key automatically.
  21400. 13:44:57Okay, I hope this is clear. So now let's
  21401. 13:45:00understand maybe uh we'll just do like
  21402. 13:45:03this, right? So I'm just going to do
  21403. 13:45:04cons. Let's take a OTP key or whatever
  21404. 13:45:07key you wanted to take. So I'll do demo
  21405. 13:45:09OTP. And here I'm simply going to do a
  21406. 13:45:12I'll do radius. And you can use simply
  21407. 13:45:14set.
  21408. 13:45:16And you're going to pass the OTP key.
  21409. 13:45:18And I'll just do here 1 2 3 4 5 6. We're
  21410. 13:45:21just setting the value here. Okay. Now
  21411. 13:45:24I'm going to add a expired date here. So
  21412. 13:45:26how you can do? So do radius dot expire.
  21413. 13:45:30And you have to pass a key here. OTB
  21414. 13:45:32key. And this will become 60. Okay. So
  21415. 13:45:36once this TTL will end, radius is going
  21416. 13:45:38to delete this automatically. So here
  21417. 13:45:40what will be the expired time? The
  21418. 13:45:42expired time will be 60 seconds. Okay.
  21419. 13:45:45Because we are using 60. Okay. Now what
  21420. 13:45:48you can do right? We can use TTL. So
  21421. 13:45:50what TTL is going to do? TTL is going to
  21422. 13:45:52show you the remaining seconds before
  21423. 13:45:54it's going to expire. You're getting
  21424. 13:45:56time to what I mentioned time to leave.
  21425. 13:45:59Right?
  21426. 13:46:00So now if I simply do con what will be
  21427. 13:46:03the TTL
  21428. 13:46:05so I'll do a radius TTL
  21429. 13:46:09and you'll simply pass OTP key and then
  21430. 13:46:12you'll just do console
  21431. 13:46:14dot log and I can log this DTL okay so
  21432. 13:46:18now if I go here and let's say I wanted
  21433. 13:46:20to run this so it will be showing as 60
  21434. 13:46:23because now we just started so once this
  21435. 13:46:25detail is going to end radius is going
  21436. 13:46:28to delete this particular automatically.
  21437. 13:46:30Okay. So I think this is done. Now one
  21438. 13:46:33last thing we have to understand that's
  21439. 13:46:34I kept this one intentionally and that
  21440. 13:46:37is how we are going to close the radius
  21441. 13:46:38connection because you notice that it's
  21442. 13:46:40actually not closing and to close the
  21443. 13:46:42radius connection it is very very
  21444. 13:46:44simple. I just have to do a and I'll do
  21445. 13:46:46radius dotquit. Okay. And this is done.
  21446. 13:46:49So now the moment I'll save this and
  21447. 13:46:51you'll see that now if I going to run
  21448. 13:46:52this it's closing. So that is the reason
  21449. 13:46:54I kept that part intentional so that you
  21450. 13:46:57can understand. Okay. So now this is how
  21451. 13:46:59you are going to quit or how you're
  21452. 13:47:02going to close your radius connection.
  21453. 13:47:04So with this I think we have covered
  21454. 13:47:06most of the things. These things are
  21455. 13:47:07very very important. So now what will be
  21456. 13:47:09the next step? Next step will be now we
  21457. 13:47:10need to start working on our actual
  21458. 13:47:12project. So now we're going to set up
  21459. 13:47:14our radius and after that we'll move to
  21460. 13:47:16the next section. All right. So moving
  21461. 13:47:18to the next one. So now we have some
  21462. 13:47:19basic understanding of the data types
  21463. 13:47:21and how you can run and see the output
  21464. 13:47:24and everything. Right. So what will be
  21465. 13:47:26the next step? Now remember there are
  21466. 13:47:27some more examples that we need to
  21467. 13:47:29understand. Okay. But this time what we
  21468. 13:47:31going to do right because now we already
  21469. 13:47:33have some very basic idea. So some of
  21470. 13:47:35the practical things now we are going to
  21471. 13:47:37apply in our project. Okay. So this is
  21472. 13:47:39how we are going to understand all the
  21473. 13:47:41things. So what we have to do? So first
  21474. 13:47:43thing we will start with creating the
  21475. 13:47:45radius connection in our project. Now
  21476. 13:47:47remember this is now not example one.
  21477. 13:47:50This is actually how we are going to
  21478. 13:47:52apply in a project. Okay. So first what
  21479. 13:47:54we need to do? So first we need to see
  21480. 13:47:55that some of the things whether we have
  21481. 13:47:57completed or not. So what will be the
  21482. 13:47:58first thing? First thing we need to see
  21483. 13:48:00in your project. Now why I'm explaining
  21484. 13:48:02all of this? Now forget about this
  21485. 13:48:03example folder. This example folder is
  21486. 13:48:05the dummy code snippet kind of thing
  21487. 13:48:07that will give you the basic idea. Now
  21488. 13:48:10when you implement in a project so first
  21489. 13:48:12thing you have to understand that okay
  21490. 13:48:13whether the radius is installed or not.
  21491. 13:48:15So yes we have already installed radius
  21492. 13:48:17right. So we have the radius package
  21493. 13:48:19here. Second because we using docker we
  21494. 13:48:22have the docker uh service is running or
  21495. 13:48:24not. Yes docker service sorry not yeah
  21496. 13:48:26docker radius service is running correct
  21497. 13:48:29also we have the radius volumes or
  21498. 13:48:30radius data. So first thing we need to
  21499. 13:48:33do what? So first we need to create our
  21500. 13:48:35actual radius connection for this
  21501. 13:48:36particular project. Second we are going
  21502. 13:48:38to use that connection where in our main
  21503. 13:48:40serverts file. So these two things we
  21504. 13:48:43are going to do. So first maybe what we
  21505. 13:48:46can do right. So uh let's create one
  21506. 13:48:48radius folder here.
  21507. 13:48:51Inside this let's create a client ds.
  21508. 13:48:55Okay. So now first of all what we going
  21509. 13:48:57to do now you see that once you
  21510. 13:48:59understand some of the concepts how much
  21511. 13:49:00easier it will be for you to know. So
  21512. 13:49:02first of all you need to get something.
  21513. 13:49:05So what I need to get I need to get the
  21514. 13:49:07create client. This is what we have
  21515. 13:49:08learned right in the previous section.
  21516. 13:49:11So first I'm just going to do con radius
  21517. 13:49:14url. So this will be process env dot.
  21518. 13:49:19So rad or so we are going to use that
  21519. 13:49:23whatever we have here. So copy this and
  21520. 13:49:26then paste it here. So now I'm just
  21521. 13:49:28going to do so we need to create a
  21522. 13:49:30radius client. Correct. So I'm just
  21523. 13:49:32going to do export cons. Why we need to
  21524. 13:49:33do export? Because we can or we need to
  21525. 13:49:36use this radius client in our other
  21526. 13:49:38places. So I'm just going to do radius
  21527. 13:49:40client and this will be equal to create
  21528. 13:49:43client. And here we are going to pass
  21529. 13:49:45the URL and this will be your radius
  21530. 13:49:48URL. Sorry not radius URL we have to
  21531. 13:49:51use. Now what we need to do we need to
  21532. 13:49:54fire some of the events. So what are the
  21533. 13:49:56things we need to fire? So first I
  21534. 13:49:58wanted to see that uh whether the
  21535. 13:50:00connection or the TCP connection is
  21536. 13:50:02established or not. Okay. So how we can
  21537. 13:50:04do that? So we have radiusclient do on
  21538. 13:50:07and here we have a method called
  21539. 13:50:08connect. Now this is going to fire when
  21540. 13:50:13the TCP connection to radius is
  21541. 13:50:15established. Okay. And here what we can
  21542. 13:50:18do we don't have to do anything else in
  21543. 13:50:19the call back. We can just log the
  21544. 13:50:22error. So what you can do right you'll
  21545. 13:50:23just do radius client
  21546. 13:50:26connected something like this. So this
  21547. 13:50:28is the first one. Then we need to see so
  21548. 13:50:32this is only the connect. Okay. One we
  21549. 13:50:34need to take for ready. So when the
  21550. 13:50:36connection is ready. So here I'm just
  21551. 13:50:37going to do this one as ready. So this
  21552. 13:50:40will be radius client ready. Okay. Also
  21553. 13:50:43we can take one more which will be
  21554. 13:50:45error. So if there are any kind of
  21555. 13:50:46error. So I'm just going to do this one
  21556. 13:50:48as error. And this will be radius client
  21557. 13:50:51error because this is a call back. You
  21558. 13:50:53can take a error here. And that error
  21559. 13:50:54you can just log here. Okay. That's it.
  21560. 13:50:58And then we are going to take last one
  21561. 13:51:00which will be end. So this will be end.
  21562. 13:51:03And this will be when the connection
  21563. 13:51:05will be closed. We don't need this one.
  21564. 13:51:06We can remove this. and we'll do radits
  21565. 13:51:09client connection closed. So this is
  21566. 13:51:12done. So now what are we going to do
  21567. 13:51:14right? So first of all I'm just going to
  21568. 13:51:16create one more function here. So I'll
  21569. 13:51:18just do export async function and I'll
  21570. 13:51:22just do connect radius.
  21571. 13:51:26So what will be the return? So this will
  21572. 13:51:28be promise and then we just have to take
  21573. 13:51:30a hide here. So for this one, first of
  21574. 13:51:33all, I'm just going to check if the
  21575. 13:51:35radius client that we have is open. If
  21576. 13:51:38this is false, so why? So what we need
  21577. 13:51:40to do here? So we are going to only
  21578. 13:51:42connect if the client is not already
  21579. 13:51:44open. You're getting so this is what
  21580. 13:51:46you're checking. If the client is
  21581. 13:51:47already open, we don't have to do
  21582. 13:51:48anything. So this is not so in that case
  21583. 13:51:51we have our radius client and here you
  21584. 13:51:53have to do connect. Okay. So here now
  21585. 13:51:55only wanted to call connect. So this is
  21586. 13:51:57the first thing and then ping pong that
  21587. 13:51:59concept right ping means we are going to
  21588. 13:52:02give ping and we need to see whether we
  21589. 13:52:04are getting the ping response or not as
  21590. 13:52:06pong. So I'm just going to do here con
  21591. 13:52:09uh let's do pong and I'll do a
  21592. 13:52:12radiusclient dotping
  21593. 13:52:15and here I'm just going to do so let's
  21594. 13:52:18do this one maybe uh let's log it only I
  21595. 13:52:22think that is fine so I'll just do here
  21596. 13:52:25radius
  21597. 13:52:27ping response and this will be and I
  21598. 13:52:31think this is done and then I'm also
  21599. 13:52:33going to take one more here so I'll do
  21600. 13:52:34export as sync function and I'll Just do
  21601. 13:52:37this one as uh disconnect
  21602. 13:52:40radius. So this will be again promise
  21603. 13:52:45void and here we are going to check. Now
  21604. 13:52:48what you have to check if the uh
  21605. 13:52:50connection is already open then you're
  21606. 13:52:51going to close it. You're getting if the
  21607. 13:52:53connection is not open how we are going
  21608. 13:52:55to close it. So to check if the radius
  21609. 13:52:57client dot is open that means if it is
  21610. 13:53:00already open then how I'm going to close
  21611. 13:53:03it. Now see this concept we have learned
  21612. 13:53:04just now. So this is what I'm talking
  21613. 13:53:06about repeating every time not only this
  21614. 13:53:09video in all the videos in last 2 three
  21615. 13:53:11years whatever videos I'm doing right.
  21616. 13:53:13So what I mean by this whenever you
  21617. 13:53:15learn learn any concept you have to see
  21618. 13:53:17whether you are able to apply that
  21619. 13:53:19concept practically in a project or not.
  21620. 13:53:21If you're not able to do it that means
  21621. 13:53:22there is no point of learning that
  21622. 13:53:24concept you're getting. So let's say
  21623. 13:53:25you're learning a nodejs concept called
  21624. 13:53:28file system and you're building a real
  21625. 13:53:31practical project and you have to see
  21626. 13:53:32whether those file system concepts you
  21627. 13:53:34can apply in that project or not. Okay,
  21628. 13:53:37no one in in any project you are not
  21629. 13:53:39going to get like this. Okay, there will
  21630. 13:53:40be some simple thing and then you're
  21631. 13:53:42just going to apply file system read
  21632. 13:53:44this method there will be situational uh
  21633. 13:53:46concept right so in that particular
  21634. 13:53:48situation you have to understand whether
  21635. 13:53:49you can apply that concept or not so
  21636. 13:53:52here if it is opened so I need to close
  21637. 13:53:54this so how I'm going to close it so we
  21638. 13:53:56have to call that quit method that we
  21639. 13:53:58have learned right so this is going to
  21640. 13:54:00close that connection so I think this is
  21641. 13:54:02done right so this is clear now what
  21642. 13:54:04will be the next step next will be now
  21643. 13:54:06this is not yet completed because in the
  21644. 13:54:08serverts we need to use this. So now
  21645. 13:54:11here you can see that we are calling the
  21646. 13:54:13test connection. So first of all I need
  21647. 13:54:14to connect to my radius. Okay. So I'm
  21648. 13:54:17just going to do a and I'll do connect
  21649. 13:54:20radius. So we need to check here on the
  21650. 13:54:22startup and also let's do one more thing
  21651. 13:54:26here. I'm just going to do process dot
  21652. 13:54:28on and what we need to do right see when
  21653. 13:54:31we going to stop the server we have to
  21654. 13:54:34close the credit connection also. So how
  21655. 13:54:36we can do that right? we have the sign
  21656. 13:54:40uh sign s it okay and here I'm just
  21657. 13:54:43going to take async and then I'm simply
  21658. 13:54:46going to do a and we have the disconnect
  21659. 13:54:49radius and then you have to exit so we
  21660. 13:54:51do process doexit
  21661. 13:54:55no process doexit of zero and I think
  21662. 13:54:58this is done so this what we need to do
  21663. 13:55:00so now if I go here I think it's already
  21664. 13:55:03running so you can see it's already
  21665. 13:55:05connected which is fantastic but still
  21666. 13:55:07I'm just going to run once again and if
  21667. 13:55:09you run here you see the postgress is
  21668. 13:55:10connected successfully ready is client
  21669. 13:55:12connected ready and then you're getting
  21670. 13:55:14the response also and uh this is the
  21671. 13:55:17pong response that you're getting right
  21672. 13:55:19so and the server is running so that
  21673. 13:55:20means now we successfully
  21674. 13:55:23uh initiated the radius connection and
  21675. 13:55:26also we are able to we will be able to
  21676. 13:55:29disconnect the radius also whenever
  21677. 13:55:30you'll be stopping the server okay so
  21678. 13:55:33this this is one small section that we
  21679. 13:55:35have to do now again what going to do
  21680. 13:55:37right let's move back to our examples
  21681. 13:55:40we'll understand some more concepts then
  21682. 13:55:41we'll again come back and then we are
  21683. 13:55:42going to apply those concepts in this
  21684. 13:55:44wheel all right so now let's move on to
  21685. 13:55:46the next one which will be so we need to
  21686. 13:55:47understand some of the more concepts
  21687. 13:55:49okay so first so let's understand that
  21688. 13:55:53what actually casting is right so
  21689. 13:55:55because this is one of the main core
  21690. 13:55:57concept that we need to understand and
  21691. 13:55:59also you're going to understand what is
  21692. 13:56:00radius caching that I mentioned and also
  21693. 13:56:02invalidation both seeing means uh like
  21694. 13:56:06If I wanted to explain a very simpler
  21695. 13:56:08way. So let's say you are storing some
  21696. 13:56:11uh data. I will not say data but storing
  21697. 13:56:13the copy of data. Okay. In a fast place
  21698. 13:56:16so that whenever you're going to do
  21699. 13:56:18future request it can read that
  21700. 13:56:21particular data from that place very
  21701. 13:56:23quickly. Okay. So this will be very
  21702. 13:56:24simple uh human like definition not like
  21703. 13:56:27you are checking from charge but but uh
  21704. 13:56:30this is what I will explain. Okay. So
  21705. 13:56:31the casting will be it is like storing
  21706. 13:56:34but the difference is see storing the
  21707. 13:56:36copy of data storing the copy of data uh
  21708. 13:56:40in a first place. So this can be any
  21709. 13:56:42place. Now in this case we are using
  21710. 13:56:44radius. So it will be in memory. So that
  21711. 13:56:47uh so it will be stored in a first
  21712. 13:56:48place. So future request
  21713. 13:56:53can be can be read very quickly. So this
  21714. 13:56:57will be a very basic definition and
  21715. 13:56:59proper definition of caching right. So
  21716. 13:57:01instead of fing the same data again and
  21717. 13:57:03again uh we are actually reading the
  21718. 13:57:06data from a cast. Now I already
  21719. 13:57:09mentioned right that why we using radius
  21720. 13:57:11because radius is in memory. So reads
  21721. 13:57:13will be very very fast right it should
  21722. 13:57:15be very very very fast extremely fast
  21723. 13:57:18right now some of the reason that why we
  21724. 13:57:22need to use casting. Now this is there
  21725. 13:57:23can be so many reasons okay but I'm
  21726. 13:57:26going to give you some of the very uh
  21727. 13:57:27simple ones. So for example let's say
  21728. 13:57:29you want to reduce your database load.
  21729. 13:57:32This is one of the reason want to
  21730. 13:57:34improve the response time. I think this
  21731. 13:57:36will automatically come because uh the
  21732. 13:57:38uh radius is very fast right so it's
  21733. 13:57:40definitely going to improve your
  21734. 13:57:41response time and also it's going to
  21735. 13:57:44handle the repetitive reads properly or
  21736. 13:57:48efficiently. Now there are some patterns
  21737. 13:57:50that you have to understand. So we have
  21738. 13:57:52something called cast aside pattern. So
  21739. 13:57:55how this will work right in a simpler
  21740. 13:57:56way. So first it is going to check the
  21741. 13:57:59cast. So let's see if you have something
  21742. 13:58:01cash if this is stored or not. Two. So
  21743. 13:58:04this will be I'll do one
  21744. 13:58:08here
  21745. 13:58:10and then in the two this will be if the
  21746. 13:58:13cast has already data. So it's going to
  21747. 13:58:16return it if not if the cast doesn't
  21748. 13:58:19have data.
  21749. 13:58:22If the C doesn't have data so what I'll
  21750. 13:58:26do it's going to read from your read
  21751. 13:58:28from main DV okay and then once you read
  21752. 13:58:32it then [clears throat] you're going to
  21753. 13:58:33save it save in cash so what will happen
  21754. 13:58:37from the next time it's going to read
  21755. 13:58:38from the cache because that time cast
  21756. 13:58:40will be having the data okay so this
  21757. 13:58:43will be a cast aside pattern there is
  21758. 13:58:46another concept called cash heat so what
  21759. 13:58:49what is cast Right? So C hit will happen
  21760. 13:58:53whatever data that you were requesting.
  21761. 13:58:54So the requested data is already in C.
  21762. 13:58:57Correct? So now consider you have
  21763. 13:58:59something called a products endpoint,
  21764. 13:59:01right? And here what will happen right?
  21765. 13:59:04So let's say radius already has
  21766. 13:59:06something called products colon all. So
  21767. 13:59:10that means these products data is
  21768. 13:59:12already present in this particular key.
  21769. 13:59:14So what will happen? It's going to
  21770. 13:59:15return the cache data immediately. So
  21771. 13:59:18return the cash data immediately. Now
  21772. 13:59:22one thing uh so let's understand here.
  21773. 13:59:24So I'll do what is cast
  21774. 13:59:27aside and cash hit.
  21775. 13:59:33So see the C the application looks for
  21776. 13:59:36data finds it and return it instantly.
  21777. 13:59:39Right? Uh this is the optimal path
  21778. 13:59:41because it bypasses the slow main
  21779. 13:59:43databases entirely. Correct? Then you
  21780. 13:59:45have cash miss also something cash asset
  21781. 13:59:48pattern. So this is called lazy loading.
  21782. 13:59:50So you can check it or I'll just read it
  21783. 13:59:53properly that in this pattern the
  21784. 13:59:54application logic directly manages both
  21785. 13:59:56the cache and the database. Correct? I
  21786. 13:59:57already mentioned that if it is present
  21787. 13:59:59you give me the data back right you have
  21788. 14:00:01the client request data application
  21789. 14:00:03right and then you have the cash hit so
  21790. 14:00:05it's going to return the data. So you
  21791. 14:00:07have a radius cast right? If there is a
  21792. 14:00:08miss so it's going to take it from DB
  21793. 14:00:10and then it's going to return the data.
  21794. 14:00:12So this will be your cash aside pattern.
  21795. 14:00:14Cash M will be nothing but when your
  21796. 14:00:16cash doesn't have that particular data.
  21797. 14:00:18Okay. So in this case let's say you're
  21798. 14:00:20getting the list of products and this
  21799. 14:00:22cash is not present. So what will
  21800. 14:00:24happen? It needs to return the data
  21801. 14:00:26right. So in that case it's going to get
  21802. 14:00:28the data from your DB and then you have
  21803. 14:00:30something called TTL. TTL I've already
  21804. 14:00:32mentioned that that will be time to
  21805. 14:00:33leave. Right? So it's going to tell that
  21806. 14:00:35how long that C data should stay before
  21807. 14:00:38we are going to delete it automatically.
  21808. 14:00:40So these will be all the concepts. Now
  21809. 14:00:42because we already know all this, right?
  21810. 14:00:44Let's see how much easier it will be for
  21811. 14:00:45us to understand these things
  21812. 14:00:47practically. So what I'll do right so
  21813. 14:00:49we'll go here and let's do this one as
  21814. 14:00:52radius caching. We'll do all at once.
  21815. 14:00:54Okay, all the concepts. So I'll do
  21816. 14:00:56radius caching. Let's go to package.json
  21817. 14:00:59and I'm just going to copy this so that
  21818. 14:01:01we can run it
  21819. 14:01:03and I'll do radius
  21820. 14:01:06caching and this will be radius casting
  21821. 14:01:10right casting.
  21822. 14:01:13Okay. So let's go here. First of all,
  21823. 14:01:15what I'll do? So we'll go to this.
  21824. 14:01:20I'll close this. So let's go here. I'm
  21825. 14:01:23going to copy till this part. Not going
  21826. 14:01:26to write. Okay. So these five things we
  21827. 14:01:28need. So we we are creating this. So now
  21828. 14:01:31first of all, we need to have our cast
  21829. 14:01:33key. So you should always have your cast
  21830. 14:01:35key, right? Cash key is there. Something
  21831. 14:01:37like C key there. So what is your cast
  21832. 14:01:40key? So I'm just going to take this one
  21833. 14:01:41as let's say cash key. So this will be
  21834. 14:01:44your demo colon products. So this is
  21835. 14:01:48where I want to store my list of
  21836. 14:01:49products in my cash. Okay. Then you're
  21837. 14:01:52going to have a cash DTL in seconds. So
  21838. 14:01:58I'm just going to do second. So what
  21839. 14:01:59this means? This means cash is going to
  21840. 14:02:02expire after 60 seconds. cash
  21841. 14:02:07will expire
  21842. 14:02:09uh after 60
  21843. 14:02:13seconds. This is also very important
  21844. 14:02:15because you see you can't give your TTL
  21845. 14:02:19log also you have to invalidate the
  21846. 14:02:21cash. Why? So let's say you have 10
  21847. 14:02:24products [snorts]
  21848. 14:02:25and you're getting the data from cash.
  21849. 14:02:27It's fine. Now let's say there is some
  21850. 14:02:29update in the data or there is a new
  21851. 14:02:31product added. So now your 10 products
  21852. 14:02:34will become 11. So if your data is
  21853. 14:02:37already present in the cash and it's
  21854. 14:02:39getting returned from the cash, it
  21855. 14:02:40always going to return you the 10 data,
  21856. 14:02:42not the 11 data and also not the updated
  21857. 14:02:44data. For example, let's say there is
  21858. 14:02:46one product and the product price is
  21859. 14:02:48changed from 100 to 500. So in that case
  21860. 14:02:51it will always return you to 100. It's
  21861. 14:02:53not going to return the updated data. So
  21862. 14:02:55that is the reason you have concept of
  21863. 14:02:56invalidation. So you are going to
  21864. 14:02:58invalidate that cast so that it's going
  21865. 14:03:00to return the updated data back. So here
  21866. 14:03:03I'm just going to do con products
  21867. 14:03:06and I'm just going to let's take
  21868. 14:03:07something like this right. So I'll do
  21869. 14:03:08keyboard and then mouse
  21870. 14:03:14and laptop something like this. So this
  21871. 14:03:18is my fake database data. Now what I'm
  21872. 14:03:21going to do I'll just do async function
  21873. 14:03:25run and here first we're going to
  21874. 14:03:28connect I'm just doing little first
  21875. 14:03:33and then what I will do also let's do
  21876. 14:03:36this right so I'm just going to close it
  21877. 14:03:38also
  21878. 14:03:40at the end
  21879. 14:03:42so now first of all I uh I'm doing some
  21880. 14:03:46request okay so this is my first
  21881. 14:03:50request request. So what will happen in
  21882. 14:03:53the first request? Pause the video and
  21883. 14:03:55try to understand. See in the first
  21884. 14:03:56request you it will not be cash hit it
  21885. 14:04:00will be cash miss. Why? Because in the
  21886. 14:04:03first request this data is not present
  21887. 14:04:06in your radius cache. Correct? So what
  21888. 14:04:08will happen every time whenever there
  21889. 14:04:10will be first request will be made it is
  21890. 14:04:13going to get the data from your DB. It's
  21891. 14:04:15very simple concept. So that means first
  21892. 14:04:17time it will be cast miss. Second time
  21893. 14:04:21once it is cach means it's going to get
  21894. 14:04:22the data from your DB we are going to
  21895. 14:04:25store the data in your radius. So next
  21896. 14:04:26time it's going to read the data from
  21897. 14:04:28your cache. So now let's I'm just going
  21898. 14:04:30to do let cast
  21899. 14:04:33and this will be my ait and then I'll do
  21900. 14:04:35radius dot get
  21901. 14:04:38and I'm just going to pass the cast key.
  21902. 14:04:40So I'm just going I'm trying to get the
  21903. 14:04:41data from my cast. So first time this
  21904. 14:04:44data will not be present. Okay. So then
  21905. 14:04:46I'm simply checking. Okay. Now see this
  21906. 14:04:48is the cash aside pattern. Okay, this is
  21907. 14:04:50the pattern that we have discussed. So
  21908. 14:04:52this is the cash aside
  21909. 14:04:55pattern. So I'm just going to do if cast
  21910. 14:04:59data is present.
  21911. 14:05:01So you're going to return me that data.
  21912. 14:05:03Okay. So I'm just going to do a
  21913. 14:05:05console.log
  21914. 14:05:06and I'll do C hit.
  21915. 14:05:10Okay. And then log this. And then you
  21916. 14:05:13have to get the data also, right? So I'm
  21917. 14:05:16just going to get the data which will be
  21918. 14:05:17JSON dot par and you're going to get the
  21919. 14:05:21data here cast if this data is not
  21920. 14:05:24present. So what we have to do? We have
  21921. 14:05:26to get the data still right. So in that
  21922. 14:05:29case this will be cast miss.
  21923. 14:05:33So this time it's going to read the data
  21924. 14:05:34from DV read from main DV.
  21925. 14:05:38So in that case first of all what we
  21926. 14:05:40going to do? So I'm simply going to do
  21927. 14:05:41con uh products.
  21928. 14:05:44I'll do DB products. DB products. Now
  21929. 14:05:47let's do one thing. Let's change this
  21930. 14:05:49one to DB products. And I'm just simply
  21931. 14:05:52going to do this one as products. So
  21932. 14:05:54this will be simply my DB products that
  21933. 14:05:56I have. And now I'm going to set this
  21934. 14:05:58one in my C. So now set or save the
  21935. 14:06:04products in radius C.
  21936. 14:06:08So in that case I'm just going to do
  21937. 14:06:10radius dot set
  21938. 14:06:13ex. So what this is going to do right
  21939. 14:06:15the set x is going to also save your
  21940. 14:06:18ttl.
  21941. 14:06:19Why we need this? So that whatever cash
  21942. 14:06:22that you are saving it is going to it
  21943. 14:06:24doesn't leave forever. Okay. So what I
  21944. 14:06:27mean by this? So this say tax is going
  21945. 14:06:29to do like this. So this is also going
  21946. 14:06:32to save also saves
  21947. 14:06:36detail so that your
  21948. 14:06:40cash doesn't
  21949. 14:06:44leave forever. Okay, you can also check
  21950. 14:06:48it here, right? So I always we are
  21951. 14:06:50checking like this and it is actually
  21952. 14:06:51easier.
  21953. 14:06:55Okay, so it sets a value that is fine
  21954. 14:06:59string value to a key assign uh
  21955. 14:07:01expression time TTL in seconds. Okay, so
  21956. 14:07:04it's going to have one uh time here. So
  21957. 14:07:07how we can do that? So we are simply
  21958. 14:07:09going to do what? So first I have to
  21959. 14:07:10give the key. So you can see that key
  21960. 14:07:12seconds and the value. So this will be
  21961. 14:07:14my cast key. This will be my cast DTL in
  21962. 14:07:18seconds. So 60 seconds. And then I'm
  21963. 14:07:20going to store the data. So this will be
  21964. 14:07:22JSON stringify. Okay. And then what data
  21965. 14:07:24you want to store in a C the products
  21966. 14:07:27data. So now this will be stored in your
  21967. 14:07:29C. All right. So now so first of all
  21968. 14:07:31let's run this. Okay. So what I have
  21969. 14:07:33done? So first we are checking that if
  21970. 14:07:35if it is present. So give me the data or
  21971. 14:07:38else we're going to render cash means
  21972. 14:07:40and then we're going to read it from our
  21973. 14:07:41DB and then we're going to set into C.
  21974. 14:07:44So now first of all let's uh go here and
  21975. 14:07:47[snorts] then I'm just going to copy
  21976. 14:07:49this and I'll do npm run and let's see
  21977. 14:07:54what we getting right. So first of all
  21978. 14:07:55you see that we are getting cast miss.
  21979. 14:07:57Okay. So why we are getting castm?
  21980. 14:08:01That's because okay here we have to
  21981. 14:08:03give. So it's coming here. Makes sense
  21982. 14:08:05also if you log the data you'll be
  21983. 14:08:06getting the data from here. But next
  21984. 14:08:09time now we should be getting the data
  21985. 14:08:10from my our cast. So now let's run it
  21986. 14:08:13once again. And we are getting cash hit.
  21987. 14:08:16Okay.
  21988. 14:08:18And you can see that this is the data
  21989. 14:08:19that you're getting. Okay. So that means
  21990. 14:08:22next time this is already present. This
  21991. 14:08:25is very very important. So first time
  21992. 14:08:27what happened? So your data is not
  21993. 14:08:29present in the cache. You're reading
  21994. 14:08:31data from your DB and you're setting the
  21995. 14:08:33data in your radius cache. Next time
  21996. 14:08:36this will be uh data is already present
  21997. 14:08:38in your C. So this will be cast hit. So
  21998. 14:08:40you're going to get the data right?
  21999. 14:08:42Every time you're going to run you're
  22000. 14:08:43going to get from your cast only. Okay.
  22001. 14:08:46See. All right. So now uh what I have to
  22002. 14:08:49do right first of all let's understand
  22003. 14:08:51another problem and that is called I
  22004. 14:08:54will uh that will be like still cash
  22005. 14:08:57problem. So what I mean by this? So now
  22006. 14:09:00consider that I have my DB products and
  22007. 14:09:03this has all these products correct. So
  22008. 14:09:05we have these products. Copy this
  22009. 14:09:09and here I'm just going to give one more
  22010. 14:09:11which will be my desktop. So now what I
  22011. 14:09:15need to do right so now I'm just going
  22012. 14:09:16to do this one as where you're getting
  22013. 14:09:19this one. Oh, I think I've used con,
  22014. 14:09:21right? Maybe. Yes. So to use this one as
  22015. 14:09:24late.
  22016. 14:09:26Okay. So first what I'll do? So let's
  22017. 14:09:28log this. So what I mean by this? So
  22018. 14:09:32consider this right. So why I'm
  22019. 14:09:35explaining because for some of you it
  22020. 14:09:36will be difficult. So this is not like a
  22021. 14:09:38dummy data right. This is actual
  22022. 14:09:39operation that you are doing in your
  22023. 14:09:41project. you're building some fullstack
  22024. 14:09:43application and let's say you have a API
  22025. 14:09:46or you're creating some product actual
  22026. 14:09:47product and that data the new data this
  22027. 14:09:50desktop is the new data is added in your
  22028. 14:09:52DB correct so now what I'm going to do
  22029. 14:09:54right so I'm just going to do a log and
  22030. 14:09:57here I'm just going to do DB products
  22031. 14:09:59okay save this and then I'm just going
  22032. 14:10:01to run again so you can see that we are
  22033. 14:10:03getting four right so cast me and then
  22034. 14:10:05we getting keyboard mouse and all these
  22035. 14:10:07thing so now uh what we need to do right
  22036. 14:10:10and also if you See we are getting this
  22037. 14:10:12log from here.
  22038. 14:10:15Okay. So this is the log that we are
  22039. 14:10:17getting. So now what I going to do
  22040. 14:10:20right? So first of all I'm just going to
  22041. 14:10:23uh do something like this maybe. So
  22042. 14:10:25let's do here cast
  22043. 14:10:29and I have it radius.get get and I'm
  22044. 14:10:33just going to do this one as cast key
  22045. 14:10:36and then I'm simply going to do log
  22046. 14:10:39and here I'm just going to do here cast
  22047. 14:10:43data and this will be JSON dot parts and
  22048. 14:10:48then I'm just going to get the cast
  22049. 14:10:52data. So let's save this and we'll see
  22050. 14:10:55what is happening here. So we'll clear
  22051. 14:10:57this.
  22052. 14:10:59Okay. So what are you getting? You're
  22053. 14:11:01still getting the old data. Now this is
  22054. 14:11:04a huge problem. So this is called stale
  22055. 14:11:06cache problem. So that means even though
  22056. 14:11:08your DB actual DB has more products or
  22057. 14:11:11newly added products or let's say there
  22058. 14:11:13is a update maybe laptop uh spelling is
  22059. 14:11:15changed from laptop to big laptop
  22060. 14:11:18something like this still your cache
  22061. 14:11:19data has the old data right and this
  22062. 14:11:22makes sense right because you haven't
  22063. 14:11:23invalidated or deleted your cash. So in
  22064. 14:11:26that case what will happen even though
  22065. 14:11:27you can have more data you are still
  22066. 14:11:29going to get same cast data every single
  22067. 14:11:32time. So this is a problem. So now to
  22068. 14:11:34solve this we have a concept called cash
  22069. 14:11:36invalidation. This is a extremely
  22070. 14:11:38important concept. Okay. These are the
  22071. 14:11:39concepts that you are going to use
  22072. 14:11:41actually in your project. Okay. So we'll
  22073. 14:11:43do C invalidation.
  22074. 14:11:46So what this is going to do right. So
  22075. 14:11:48this is going to so we are going to have
  22076. 14:11:50some kind of mechanism in our code in
  22077. 14:11:52our actual API that okay whenever I'm
  22078. 14:11:54going to create a new product or
  22079. 14:11:56whenever I'm going to update a new
  22080. 14:11:57product or whenever I'm going to delete
  22081. 14:11:59a product I need to do what I need to
  22082. 14:12:01delete the old cash so I'm just going to
  22083. 14:12:04give a sort that okay whenever I will do
  22084. 14:12:06any so or let's put it in this way when
  22085. 14:12:09DB changes very simple whenever your DB
  22086. 14:12:11changes you're going to delete the cache
  22087. 14:12:13this is the best way to understand now
  22088. 14:12:16for beginners whenever DB changes is
  22089. 14:12:18you're going to delete
  22090. 14:12:20delete your old cach immediately right
  22091. 14:12:24so you're going to so that you'll get
  22092. 14:12:25the uh updated data here so how you're
  22093. 14:12:28going to do that so I'm just going to
  22094. 14:12:29have it radius dot delete
  22095. 14:12:32and here we're going to pass the cast
  22096. 14:12:33key and I'm just going to do a log
  22097. 14:12:37and I'll do cast deleted
  22098. 14:12:40and then spelling this wrong so now
  22099. 14:12:44again I'm just going to check if not
  22100. 14:12:46equal do cast.
  22101. 14:12:49So I'm just going to do a log here. I'll
  22102. 14:12:53do cast data
  22103. 14:12:56after delete. And here what we going to
  22104. 14:13:00do, right? So I'm just going to do con
  22105. 14:13:02fresh
  22106. 14:13:04products.
  22107. 14:13:06So this will be again your DB products.
  22108. 14:13:08But in actual scenario, this will be
  22109. 14:13:09again your data from your DB. So this
  22110. 14:13:12will be again your data from your DB
  22111. 14:13:14after changes, right? After changes
  22112. 14:13:18after data changes and then again I'm
  22113. 14:13:20just going to set it. So I'll do radius
  22114. 14:13:22dot set
  22115. 14:13:24X and here I'm just going to do this one
  22116. 14:13:27as C key I'll do C DTL in seconds and
  22117. 14:13:32again you're going to do JSON dot
  22118. 14:13:33stringify and you're going to pass the
  22119. 14:13:35phrase products you're getting. So now
  22120. 14:13:38again I'm just going to do a log here.
  22121. 14:13:44Okay, after this I'm just going to do a
  22122. 14:13:46log and I'm just going to do phrase data
  22123. 14:13:50and this will be phrase products.
  22124. 14:13:55So now if I save this so now notice what
  22125. 14:13:57we are doing we doing a invalidation
  22126. 14:13:59cash invalidation by deleting the old
  22127. 14:14:01cash and then again we are setting here
  22128. 14:14:03that if the cash is not present now
  22129. 14:14:05again you give me the data from the DB
  22130. 14:14:06and then again you're going to set it in
  22131. 14:14:08your C. So let's run it again. So you
  22132. 14:14:11can see that we are getting what
  22133. 14:14:14cash data and cash deleted and then we
  22134. 14:14:17are not getting this one. Okay, let's
  22135. 14:14:18run it once again
  22136. 14:14:21and you can see that what we are getting
  22137. 14:14:23cash deleted.
  22138. 14:14:26Something we are doing wrong here. If
  22139. 14:14:28not oh okay we have to actually
  22140. 14:14:32get the data man that's why. So first we
  22141. 14:14:34have to get the cache data which will be
  22142. 14:14:35have radius dot get and this will be
  22143. 14:14:38cash key. then only you'll be getting
  22144. 14:14:40that if this is present because we've
  22145. 14:14:41already deleting it. So first we need to
  22146. 14:14:43see if the cache is present. So first we
  22147. 14:14:45need to get the data and this is not
  22148. 14:14:47going to find because we've already
  22149. 14:14:48deleted it. So then it's going to read
  22150. 14:14:50it from the DB. So now I think this will
  22151. 14:14:52work
  22152. 14:14:55that was the mistake huh see so now
  22153. 14:14:57let's go one by one. So first we have
  22154. 14:14:59what? Cast deleted. So now we are
  22155. 14:15:02getting what? So this is not going to
  22156. 14:15:03give you any data. So that mean cash
  22157. 14:15:05data after delete. So this is your
  22158. 14:15:07phrase data. So this phrase data is
  22159. 14:15:09nothing but your four data
  22160. 14:15:11these four because this is the four data
  22161. 14:15:13that we have added. Okay. So this is how
  22162. 14:15:16you are going to use the concept of cash
  22163. 14:15:18aside castl and invalidate cash and to
  22164. 14:15:21solve this still cash problem. Okay.
  22165. 14:15:23These are very important concept and
  22166. 14:15:25most of the times you're going to use
  22167. 14:15:26this.
  22168. 14:15:28So with this now let's move to the next
  22169. 14:15:30we going to uh apply some of the things
  22170. 14:15:32in our actual project in the next
  22171. 14:15:33section. All right. So now let's
  22172. 14:15:34implement some of this concept in our
  22173. 14:15:36project. Okay. So for that what you're
  22174. 14:15:38going to do right if you go to services.
  22175. 14:15:40So let's go to product service and let's
  22176. 14:15:42close everything else. So first of all
  22177. 14:15:44we'll see that what we are doing here.
  22178. 14:15:46So first you can see that we have one
  22179. 14:15:48method which is going to get all the
  22180. 14:15:50products. Now here one thing I wanted to
  22181. 14:15:51mention we are not focusing on filter.
  22182. 14:15:54Okay for now and I will explain the I'll
  22183. 14:15:56explain the reason why. So for now you
  22184. 14:15:59just see that we are getting uh all
  22185. 14:16:01products there is a method uh service.
  22186. 14:16:03So we just creating one on query and at
  22187. 14:16:05the end what simply we are doing we are
  22188. 14:16:07simply getting the result from your DB
  22189. 14:16:09and then we are returning that and if
  22190. 14:16:11you go to your controllers in the
  22191. 14:16:12controllers we are using that uh what is
  22192. 14:16:15that get product so this is the method
  22193. 14:16:17that we are using and if I go here right
  22194. 14:16:19so let's go to products and if I send
  22195. 14:16:21this so we getting the whatever products
  22196. 14:16:23that we have added in the poss DB so now
  22197. 14:16:26what we need to do right we need to
  22198. 14:16:28implement a caching for this particular
  22199. 14:16:30for all these methods that we have so
  22200. 14:16:32first we have the get all products then
  22201. 14:16:34we have get product by ID create product
  22202. 14:16:36and update product all these methods so
  22203. 14:16:39first let's start with uh taking our
  22204. 14:16:41cast key now remember whatever concept
  22205. 14:16:43that we have learned so far that
  22206. 14:16:44whenever you're going to implement some
  22207. 14:16:46kind of cash there has to be some key
  22208. 14:16:48right so that key is going to hold that
  22209. 14:16:50information or hold that cash
  22210. 14:16:51information so here I'm just going to
  22211. 14:16:53con let's give this one as products
  22212. 14:16:57all cash key now we are only focusing on
  22213. 14:17:00getting all products for now and I'm
  22214. 14:17:02just going to give this one as products
  22215. 14:17:04colon all. So [snorts] now what also
  22216. 14:17:07okay let's take one more thing and that
  22217. 14:17:08is the TTL. So I'm just going to do this
  22218. 14:17:10one con and I'll do products
  22219. 14:17:14cash and I'll do TTL seconds
  22220. 14:17:18and this will be 60. Okay. So now what
  22221. 14:17:22we are going to do first of all these
  22222. 14:17:24get all products now we are going to
  22223. 14:17:26change this one from this particular
  22224. 14:17:28method to a different method. Why? Now
  22225. 14:17:31first of all let's do one thing right?
  22226. 14:17:33Uh maybe let's create one more method
  22227. 14:17:36here. Uh we'll create [snorts] one more
  22228. 14:17:38method like get all products here after
  22229. 14:17:40this and I'll explain or else it will be
  22230. 14:17:42little tricky to understand what we are
  22231. 14:17:44doing. So first of all I'm just going to
  22232. 14:17:45do async and I'll do function
  22233. 14:17:49and here I'm just going to do this one
  22234. 14:17:50as get all products. You're going to get
  22235. 14:17:52error which is fine and then here also
  22236. 14:17:55you're going to get the filters. Right?
  22237. 14:17:56So we have all these filters. So we'll
  22238. 14:17:58copy this filters
  22239. 14:18:01and let's paste it here. And what will
  22240. 14:18:04be the return type? So this is going to
  22241. 14:18:07return a promise
  22242. 14:18:10and this will will return the type of
  22243. 14:18:13product.
  22244. 14:18:15Okay.
  22245. 14:18:16So now first of all what we need to do
  22246. 14:18:18right. So first of all we need to see
  22247. 14:18:21whenever I'm going to call this
  22248. 14:18:23particular service whenever I'm going to
  22249. 14:18:25hit that particular endpoint to get all
  22250. 14:18:27the products I need to see that whether
  22251. 14:18:28that product is present in our cash or
  22252. 14:18:30not. So this is again come back to the
  22253. 14:18:32same concept that we have learned so far
  22254. 14:18:34that is the cash aside pattern. If the
  22255. 14:18:36cash is present you're going to return
  22256. 14:18:38the data from your cache immediately. If
  22257. 14:18:40the cash is not present, you're going to
  22258. 14:18:42get the actual data from your postgress
  22259. 14:18:44DB and you're going to return. But there
  22260. 14:18:46can be some exception. For example, the
  22261. 14:18:48stale cache problem that we have
  22262. 14:18:49discussed that let's say now I added a
  22263. 14:18:51new product. Still we are going to get
  22264. 14:18:53the whole product, right? That part I
  22265. 14:18:54have discussed. So that we're going to
  22266. 14:18:56see later. So for now what we going to
  22267. 14:18:58do right first of all I'm simply going
  22268. 14:19:00to check that con and I'll do has
  22269. 14:19:03filters. I'm simply going to check that
  22270. 14:19:05if filter is present. So for now we are
  22271. 14:19:07not focusing on filters and I'll explain
  22272. 14:19:08why. So I'm just going to give boolean
  22273. 14:19:11and I'll do filters dot
  22274. 14:19:13category this is present or we also have
  22275. 14:19:16the filter dot search right so I'll do
  22276. 14:19:18filter dot search if this is present
  22277. 14:19:20that means there is a filter and I'm
  22278. 14:19:22simply going to check that if this has
  22279. 14:19:24filter is present so in that case I'm
  22280. 14:19:26just going to do console.log log and I'm
  22281. 14:19:28going to do cash bypass for now. I'll do
  22282. 14:19:31C bypass and this will be filtered
  22283. 14:19:38product list. Now why why we are doing
  22284. 14:19:41this right? So see when we have filter
  22285. 14:19:44right so every filter combination
  22286. 14:19:46actually we need a different cascade and
  22287. 14:19:48this is a very important thing. So every
  22288. 14:19:51filter combination that we have need a
  22289. 14:19:56separate cast key. Why that is? Now
  22290. 14:19:59consider that you are doing a filter. In
  22291. 14:20:01the first filter you are only passing
  22292. 14:20:03the category. In the second filter you
  22293. 14:20:05are passing the search. In the third
  22294. 14:20:07filter you're passing both category and
  22295. 14:20:09search. So in that case you can't
  22296. 14:20:10implement this using only one key. So
  22297. 14:20:12there has to be a combination. Okay. For
  22298. 14:20:15example, so let's say we have our
  22299. 14:20:17products right? So we need to do
  22300. 14:20:19something like this. We'll do products
  22301. 14:20:21colon all and then here you're going to
  22302. 14:20:24do a search. So let's say I'm applying a
  22303. 14:20:26search and the keyword is keyboard
  22304. 14:20:28something like this. Okay. So and then
  22305. 14:20:30maybe let's say you are doing a category
  22306. 14:20:32also. Let's I'll do products colon all
  22307. 14:20:34colon category. And then here I'm just
  22308. 14:20:38going to do accessories something like
  22309. 14:20:39this.
  22310. 14:20:43Okay. So [snorts] that is the reason for
  22311. 14:20:45now we are bypassing. But what we are
  22312. 14:20:46going to do right so first we are going
  22313. 14:20:48to change this particular method to
  22314. 14:20:50fetch all
  22315. 14:20:53products
  22316. 14:20:54from database. Okay. So that means this
  22317. 14:20:58method will never change because we need
  22318. 14:21:01to have this right because we don't know
  22319. 14:21:02whether we have the cash all the time.
  22320. 14:21:04If the cache is not present it going to
  22321. 14:21:06it's going to fet the data as usual from
  22322. 14:21:08your database all the time. So that
  22323. 14:21:10means if filter is present so we are
  22324. 14:21:12going to simply return the fetch all
  22325. 14:21:15products from DB and then we're going to
  22326. 14:21:17pass the filters here. Okay. But now
  22327. 14:21:20first of all here we have to implement
  22328. 14:21:22the cast. So that means when the filter
  22329. 14:21:24is not present let's say I'm just
  22330. 14:21:26getting the list of products here. This
  22331. 14:21:28is just for uh explanation in your real
  22332. 14:21:30project. You also have to implement for
  22333. 14:21:32this particular
  22334. 14:21:34uh scenario also. So now what we going
  22335. 14:21:36to do right? So first we need to check
  22336. 14:21:39that before we are going to ask our
  22337. 14:21:41postgress for our products we need to
  22338. 14:21:43check radius first that whether that is
  22339. 14:21:45present or not. If radius already has
  22340. 14:21:47the product list we are not going to
  22341. 14:21:50fetch the same product list from our DB.
  22342. 14:21:52Okay, I'm repeating this thing because
  22343. 14:21:54if you understand the core concept and
  22344. 14:21:56even if you do only two three examples
  22345. 14:21:58right most of the things you'll be able
  22346. 14:22:00to do it I can 100% guarantee even
  22347. 14:22:03though if let's say you're not your
  22348. 14:22:04basics are not clear and you're uh doing
  22349. 14:22:07some very complex project some
  22350. 14:22:09microservices and blah blah but you'll
  22351. 14:22:11see that you'll be getting some problem
  22352. 14:22:13so basic has to be very very clear so
  22353. 14:22:15that you can understand what you're
  22354. 14:22:16doing so now here I wanted to uh check
  22355. 14:22:20that okay if the radius cash is present
  22356. 14:22:22You also have to remember radius
  22357. 14:22:24actually is not the source of truth
  22358. 14:22:25here. Okay. So this is a very important
  22359. 14:22:27point and most of the uh like whoever is
  22360. 14:22:30teaching I'm not sure if they're
  22361. 14:22:31covering this or someone may be skipping
  22362. 14:22:33this but this is actually very
  22363. 14:22:34important. Why radius is not the source
  22364. 14:22:38of truth here. Okay. What actually it is
  22365. 14:22:43uh storing? Remember I already mentioned
  22366. 14:22:45that radius actually is going to store a
  22367. 14:22:47temporary copy of data. that data
  22368. 14:22:50originally come from your posgress DB
  22369. 14:22:52only. So what is your source of truth?
  22370. 14:22:54The postgress DB right. So now in this
  22371. 14:22:57case I'm just going to do con cast
  22372. 14:23:01products
  22373. 14:23:03and here I'm just going to do a and I'll
  22374. 14:23:06do radius client dot get. So first I
  22375. 14:23:08need to check if the this is present or
  22376. 14:23:10not and I'll do product all cash. So I'm
  22377. 14:23:13simply checking if that uh product is
  22378. 14:23:16present
  22379. 14:23:18uh in cash radius cash or not. if this
  22380. 14:23:20is present. So I'm simply going to do if
  22381. 14:23:22the cash products is present. So let's
  22382. 14:23:25do
  22383. 14:23:27log
  22384. 14:23:29and I'm just going to do this on a cash
  22385. 14:23:31hit
  22386. 14:23:33and I'm just going to do products
  22387. 14:23:35all okay and then what are you going to
  22388. 14:23:39do right? So here we are simply going to
  22389. 14:23:42return
  22390. 14:23:43JSON dot parts and here I'm just going
  22391. 14:23:47to do C products as so type will be
  22392. 14:23:51product. So this is what we're going to
  22393. 14:23:53return. Okay. So this is radius is
  22394. 14:23:55actually returning your cash data as a
  22395. 14:23:57string. Why you need JSON parse? Because
  22396. 14:23:59it's going to convert the string back to
  22397. 14:24:01your actual array. Right? This is normal
  22398. 14:24:03concept. If this is not the case, if the
  22399. 14:24:06cash product is not present, so that
  22400. 14:24:08means what? There is a cash miss right.
  22401. 14:24:11So we are going to do cash miss
  22402. 14:24:14and here now we are going to first do
  22403. 14:24:16what? Whenever there is a cast me you
  22404. 14:24:19need to fetch the products from where?
  22405. 14:24:20From your DV. So we simply going to do
  22406. 14:24:23con products
  22407. 14:24:25and this will be a fetch all products
  22408. 14:24:29from DV and you're going to pass the
  22409. 14:24:31filters right. So we have to pass the
  22410. 14:24:33filters. So now here we are going to do
  22411. 14:24:37what? We're going to set our set our
  22412. 14:24:40actual products
  22413. 14:24:44data in radius cast.
  22414. 14:24:48So now I'm simply going to divide radius
  22415. 14:24:50client dot set x. We have already
  22416. 14:24:53discussed see how we are doing right. We
  22417. 14:24:55have already understood all of these. So
  22418. 14:24:57it will be easy. So we have cast key. We
  22419. 14:24:59have the cast ttl in seconds. And then
  22420. 14:25:01we're going to store the data. So do
  22421. 14:25:02JSON.stringify and this will be the
  22422. 14:25:04products. Now these products is the
  22423. 14:25:06products that we are getting from our
  22424. 14:25:07DB. And then we are simply going to do a
  22425. 14:25:10log and I'm just going to do cash set
  22426. 14:25:14products colon all and what you have to
  22427. 14:25:18return you have to return the products
  22428. 14:25:19here and this is done. Okay. So now I
  22429. 14:25:23think this should work. I hope this is
  22430. 14:25:26clear. Now at the end your product
  22431. 14:25:28controller doesn't know what is
  22432. 14:25:29happening in the business logic. Right?
  22433. 14:25:30So here it's going to return the product
  22434. 14:25:33as usual. What you're doing is simply
  22435. 14:25:34doing the main logic here that if the C
  22436. 14:25:37is present, return the data. If this is
  22437. 14:25:39not present, you return the data from DB
  22438. 14:25:41first. Set it in your C and from the
  22439. 14:25:43next time you're going to get from your
  22440. 14:25:45C only. So now let's save this. Let's
  22441. 14:25:47see if this is running or not. I think
  22442. 14:25:48it's running.
  22443. 14:25:50So now I'm just going to run it here. So
  22444. 14:25:52let's run it. So let's see what you're
  22445. 14:25:54getting. We're getting cash miss and
  22446. 14:25:56cash set. And now we're going to run
  22447. 14:25:57once again. And you can see that what
  22448. 14:25:59will happen. It's you can see that cash
  22449. 14:26:01hit. So now it's getting from your cash.
  22450. 14:26:03Okay, you see immediately you're getting
  22451. 14:26:05right. So this is from your radius guys.
  22452. 14:26:07This is fantastic. Now see how much
  22453. 14:26:10cleaner it is to understand. So that is
  22454. 14:26:12the reason I always mention and not I
  22455. 14:26:14always mention but that is how I usually
  22456. 14:26:16teach right in my YouTube videos. I
  22457. 14:26:18don't go into too many complicated
  22458. 14:26:20things because at the end if you working
  22459. 14:26:22in a company right let's say you are in
  22460. 14:26:23college or you you have less than 2
  22461. 14:26:26years of experience you'll see that even
  22462. 14:26:28the whatever work you are doing in your
  22463. 14:26:30company let's say product or service at
  22464. 14:26:32the end they're not doing so many
  22465. 14:26:34complicated things they're doing the
  22466. 14:26:36normal basic things only but the concept
  22467. 14:26:38has to be very clear although there can
  22468. 14:26:40be 1,000 line lines of code but at the
  22469. 14:26:43end you you will see that they're doing
  22470. 14:26:44this cash aside cash invalidation as set
  22471. 14:26:48and fetging the data from C or your
  22472. 14:26:50fetch the data from DB and then set it
  22473. 14:26:52in C. Now this [snorts] is a very basic
  22474. 14:26:54level. There are so many advanced things
  22475. 14:26:56is also there in radius but this is how
  22476. 14:26:58this has to be done. Now see that every
  22477. 14:27:00time it's going to hit the C and it's
  22478. 14:27:02going to get the data from your C right.
  22479. 14:27:04So if you run it here it's going to get
  22480. 14:27:06and sometime it's going to miss. Why?
  22481. 14:27:08Okay, this is another see it's working.
  22482. 14:27:10Why? Because we've given 60 seconds
  22483. 14:27:12here. The TTL is 60 seconds, right? You
  22484. 14:27:14remember this one. So after 60 seconds
  22485. 14:27:16it's going to delete that or invalidate
  22486. 14:27:18that cash. So that is the reason you're
  22487. 14:27:19getting. But if I run once again you see
  22488. 14:27:21that it's cast it. So that means this is
  22489. 14:27:24working absolutely fine. So now what
  22490. 14:27:26will be the next step? Next let's do one
  22491. 14:27:28thing. Let's uh do some more important
  22492. 14:27:30things here only in this particular file
  22493. 14:27:32in the next part because we also have to
  22494. 14:27:34see that when we are going to this cash
  22495. 14:27:37data is present and let's added a new
  22496. 14:27:38product this data should be updated.
  22497. 14:27:41Right? you are getting the the problem
  22498. 14:27:43that I discussed the stale cash problem.
  22499. 14:27:45So let's how we are going to do that in
  22500. 14:27:47the next part. All right. So moving to
  22501. 14:27:48the next one. So get all products we
  22502. 14:27:50have done. So now let's do this one. Get
  22503. 14:27:52product by ID. So what I uh what you
  22504. 14:27:55mean by this right. So let's say
  22505. 14:27:56whenever we are going to fetch a single
  22506. 14:27:58product by ID. So here uh what you're
  22507. 14:28:01going to do right? So also I will
  22508. 14:28:04suggest right this one try to implement
  22509. 14:28:06using the same concept. So what I mean
  22510. 14:28:08by this this will be exactly same
  22511. 14:28:09concept where we have discussed about
  22512. 14:28:10the cast asset pattern right. So first
  22513. 14:28:12we are going to see so let's see I'm
  22514. 14:28:14hitting this particular endpoint from my
  22515. 14:28:16UI side. So I need to check that if this
  22516. 14:28:19particular product information let's say
  22517. 14:28:21I have a product ID of product 10. So ID
  22518. 14:28:24is 10. So if that 10 product is
  22519. 14:28:26currently actually present in my C or
  22520. 14:28:28not. Okay. If that is not present in the
  22521. 14:28:32C. So what you're going to do? You're
  22522. 14:28:33going to face the data from my DB as
  22523. 14:28:35usual. If this is present in C, we're
  22524. 14:28:37going to get from our radius C. So first
  22525. 14:28:40uh what we'll do uh as usual. So let's
  22526. 14:28:42create a export as sync function and I'm
  22527. 14:28:45simply going to do same get product by
  22528. 14:28:47ID and this one I'm just going to do
  22529. 14:28:49this one as fetch
  22530. 14:28:51uh let's do single product. It will be
  22531. 14:28:53easy to understand fetch single product
  22532. 14:28:56from DB, right? Or database whatever you
  22533. 14:28:59can do. So first of all what we need to
  22534. 14:29:01do here, right? So okay, these will be
  22535. 14:29:03same. So let's copy this
  22536. 14:29:07and we'll just paste it here.
  22537. 14:29:10So first we need to actually see that if
  22538. 14:29:12that is present in the radius cast or
  22539. 14:29:15not. So how we can do that? So we have
  22540. 14:29:17to take a cast key, right? So I'm just
  22541. 14:29:19going to do cash key and for this one
  22542. 14:29:22let's do one thing. Let's uh create one
  22543. 14:29:24method. Okay. So I'm simply going to do
  22544. 14:29:27so let's go to the top maybe here. So
  22545. 14:29:31here I'm just going to do a function and
  22546. 14:29:33I'll do get product gas key and here
  22547. 14:29:38we're going to get the product id. So
  22548. 14:29:40this will be number and uh we are going
  22549. 14:29:43to return a string. So here I'm simply
  22550. 14:29:46going to create a key which will be
  22551. 14:29:49products
  22552. 14:29:50colon id because this will be single
  22553. 14:29:53product but how we are going to get the
  22554. 14:29:54single product. So this single product
  22555. 14:29:56is nothing but your product ID. So that
  22556. 14:29:59means let's say I have 1,000 products
  22557. 14:30:01and I am storing the uh product with of
  22558. 14:30:05ID 500. So it will become like this,
  22559. 14:30:07right? So the products colon id colon
  22560. 14:30:10500 something like this. So now this is
  22561. 14:30:13done. So what will be the next step? So
  22562. 14:30:15next will be first we need to see that
  22563. 14:30:16okay this is present or not. If this is
  22564. 14:30:18present we're going to return it. So we
  22565. 14:30:20simply going to do get product by
  22566. 14:30:24product cast key right and here we are
  22567. 14:30:26going to pass the id and then I'm simply
  22568. 14:30:28going to do con cast product. So this
  22569. 14:30:32cast product will be a radius client dot
  22570. 14:30:36get and this will be this cast key. So
  22571. 14:30:39we simply checking if that id product.
  22572. 14:30:42So this is a dynamic value right? So
  22573. 14:30:44this can be 1 2 3 4. Consider let's take
  22574. 14:30:47the example of 10. So if the id of 10
  22575. 14:30:51product was not 10 product ID of product
  22576. 14:30:5310 opposite id of product 10 the product
  22577. 14:30:57which id is 10 if that is present in
  22578. 14:31:00cash or not okay we simply checking that
  22579. 14:31:03part if this is present so that means it
  22580. 14:31:05will be a cash hit right so that data is
  22581. 14:31:07present so I'm simply going to do if
  22582. 14:31:09cash product if this is present so I'm
  22583. 14:31:11just going to do a log and this will be
  22584. 14:31:14a cash
  22585. 14:31:16cast cash hit and And let's also pass
  22586. 14:31:19the uh cast key here. Right. So I'm just
  22587. 14:31:21going to do here uh what I'm doing cash
  22588. 14:31:25key. So this will be cash hit. And then
  22589. 14:31:28we are simply going to as usual return
  22590. 14:31:30the response. So I'm just going to do
  22591. 14:31:31return JSON dot parse.
  22592. 14:31:35So this will be my cash product as
  22593. 14:31:38product. This is done. Now if this is
  22594. 14:31:42not present so as usual this will be
  22595. 14:31:44cash miss.
  22596. 14:31:46And then we're going to first get the
  22597. 14:31:48product from our DB. So we have it
  22598. 14:31:53fetch single product from DB
  22599. 14:31:57pass the ID. First we're going to check
  22600. 14:31:59that if the product is not present.
  22601. 14:32:02We're going to return null. Okay. And
  22602. 14:32:04then we're going to set this one in our
  22603. 14:32:06cast. So now I'm just going to direct
  22604. 14:32:08radisclient dot setx. And here we are
  22605. 14:32:11going to pass the cast key. We're going
  22606. 14:32:13to pass the TTL and also then I'm going
  22607. 14:32:16to pass the actual product that I need
  22608. 14:32:18to uh need to store. So this will be the
  22609. 14:32:20product and this is done. And then I'm
  22610. 14:32:22again going to do a log here and this
  22611. 14:32:24will be catch
  22612. 14:32:27set.
  22613. 14:32:29Okay. What you have to return? You have
  22614. 14:32:31to return the product here. And this is
  22615. 14:32:33done. Okay. All right. So now let's run
  22616. 14:32:35this. And also one thing I just paused
  22617. 14:32:37it and I see that we make a mistake and
  22618. 14:32:39that is uh here right if this is cast so
  22619. 14:32:43it will be cast hit or this will be cast
  22620. 14:32:45miss. Okay so this is one change we have
  22621. 14:32:47to do. So now let's go here and then if
  22622. 14:32:50I try to send this so you'll see that
  22623. 14:32:53what you're getting we're getting cash
  22624. 14:32:54missed and cash is now set. Now next
  22625. 14:32:57time let's try to send once again and
  22626. 14:33:00you'll see that now cash is hit. So that
  22627. 14:33:02means the data is now present in your
  22628. 14:33:04radius cast. So this is another example
  22629. 14:33:07that we have done. Now another very very
  22630. 14:33:08important one that you have to
  22631. 14:33:09understand. Okay. So first let's see how
  22632. 14:33:12I'm going to do it. So what I'll do
  22633. 14:33:14right. So let's go to products endpoint
  22634. 14:33:18and you will see that what we are
  22635. 14:33:20getting right. So first of all let's do
  22636. 14:33:21here. So I'm do 1 2 3 4 5 6 7. Okay. So
  22637. 14:33:28we have seven data. So now uh I'll try
  22638. 14:33:30to send this. So you can see that we
  22639. 14:33:33getting this. Now let's go to the
  22640. 14:33:35postman test that I have created and I'm
  22641. 14:33:38just going to copy this. Okay, let's go
  22642. 14:33:40to the uh add here. So I'm going to add
  22643. 14:33:43the post and then I'll go to body and
  22644. 14:33:46then I'm simply going to send this one.
  22645. 14:33:48So this is added ID 8 and if I try to go
  22646. 14:33:51here and try to send this you'll see
  22647. 14:33:53that how many data you are getting.
  22648. 14:33:55You're getting the ID 7. So you're not
  22649. 14:33:57getting the ID
  22650. 14:33:59uh the ID8 data. So what I mean the new
  22651. 14:34:02data that we have added it's not coming
  22652. 14:34:04here and the reason is very simple this
  22653. 14:34:07is the concept of the stale caches you
  22654. 14:34:09remember I just done this one
  22655. 14:34:10practically so that means it is getting
  22656. 14:34:12from a radius cast until unless that
  22657. 14:34:14cash will be invalidated that will not
  22658. 14:34:17come or else until unless the TTL will
  22659. 14:34:19cross then only the new data will come
  22660. 14:34:22but that is not the solution right so
  22661. 14:34:23that means we have to have some kind of
  22662. 14:34:26mechanism that whenever I'm going to
  22663. 14:34:28create the data or whenever I'm going to
  22664. 14:34:30update the data remember I mentioned
  22665. 14:34:32that whenever there will be some update
  22666. 14:34:34in the DB you need to invalidate the
  22667. 14:34:36cast very very important concept so how
  22668. 14:34:38we are going to do that to do this one
  22669. 14:34:40let's go to our create product and you
  22670. 14:34:42can see [snorts] that we are doing all
  22671. 14:34:43of this whatever blah blah that is fine
  22672. 14:34:45we don't have to do anything here so
  22673. 14:34:47first what I'll do right so I'm just
  22674. 14:34:49going to create a const and I'm just
  22675. 14:34:52going to do newly created product
  22676. 14:34:55so I'll do newly created product
  22677. 14:34:58and this will be these whatever whatever
  22678. 14:35:00we are doing here and now what I need to
  22679. 14:35:03do right I need to invalidate the cash
  22680. 14:35:06now remember I mentioned what the
  22681. 14:35:07postgress is the source of truth right
  22682. 14:35:09so whenever we going to create a product
  22683. 14:35:10we need to delete the old list that is
  22684. 14:35:14stored in our cast so that next time
  22685. 14:35:16whenever we are going to face the latest
  22686. 14:35:18data it is going to get the data from
  22687. 14:35:20your postgress it's going to get the
  22688. 14:35:22fresh data from postgress so that means
  22689. 14:35:24we have to have a method which is going
  22690. 14:35:27to delete the products
  22691. 14:35:29from now delete all products or products
  22692. 14:35:32all
  22693. 14:35:36so this is what we have to do and at the
  22694. 14:35:38end we're going to return the newly
  22695. 14:35:39created product here which is fine so
  22696. 14:35:41what this is going to do so this is
  22697. 14:35:43going to actually delete that cash so
  22698. 14:35:46now copy this and maybe let's do it here
  22699. 14:35:49only right so I'm just going to do async
  22700. 14:35:52function and I'll do delete product cash
  22701. 14:35:56what this is going to return so this
  22702. 14:35:57will be a promise
  22703. 14:35:59void and here I'm simply going to do a
  22704. 14:36:03and I'll do radius client dot delete and
  22705. 14:36:07then I'm simply going to delete the
  22706. 14:36:08product
  22707. 14:36:10all cash key and then I'm simply going
  22708. 14:36:13to do a log and you'll see that I'm just
  22709. 14:36:15going to do a cash delete and then I'm
  22710. 14:36:19just going to do products all and you'll
  22711. 14:36:21see that whenever you're going to do
  22712. 14:36:23that right now notice this cash delete
  22713. 14:36:25will be triggered so now let's do in
  22714. 14:36:28different way so First I will go to
  22715. 14:36:30let's start with first one. So go to
  22716. 14:36:31products. I'm going to send here. We are
  22717. 14:36:34getting the new data because cash is now
  22718. 14:36:35automatically invalidated using TTL.
  22719. 14:36:38That is fine. Now I'll [snorts] go to
  22720. 14:36:40post and I'm going to create one more
  22721. 14:36:41data ID9. And you'll see that here it's
  22722. 14:36:44getting cash is actually now deleted.
  22723. 14:36:46And now if you go to immediately here
  22724. 14:36:48right and try to send this you are
  22725. 14:36:50getting the newly created data also.
  22726. 14:36:52Fantastic right? So that means whenever
  22727. 14:36:54you created a data whenever we updated
  22728. 14:36:57the database we actually deleted our old
  22729. 14:36:59radius cache that is present. So what
  22730. 14:37:01will happen next time it's going to read
  22731. 14:37:04the data immediately from your DB. It's
  22732. 14:37:06not going to wait. So this is how you
  22733. 14:37:08have to invalidate cash. So so see this
  22734. 14:37:12part you have to master that when I'm
  22735. 14:37:14going to invalidate the cash. No one is
  22736. 14:37:15going to come and then tell you that
  22737. 14:37:16okay whenever you are going to create
  22738. 14:37:18product every time you have to delete
  22739. 14:37:19cash. It's not like that you are
  22740. 14:37:21getting. So what I'm trying to explain
  22741. 14:37:22that based on that situation that or
  22742. 14:37:25based on the project or based on the
  22743. 14:37:26feature that you are creating you have
  22744. 14:37:28to decide that when I have to delete the
  22745. 14:37:30cash this example I'm just showing you
  22746. 14:37:32based on the project that I have created
  22747. 14:37:34this doesn't mean that every time you're
  22748. 14:37:36going to create the product you have to
  22749. 14:37:37delete the cash okay maybe yes but what
  22750. 14:37:40I'm trying to say that based on project
  22751. 14:37:42to project this will change but at the
  22752. 14:37:44end the main concept will remain same so
  22753. 14:37:47this is one now the same thing will be
  22754. 14:37:50for what for will be for your uh update
  22755. 14:37:52also. So whenever you're going to update
  22756. 14:37:54any data right that time also you need
  22757. 14:37:57to do what you need to actually
  22758. 14:37:59uh delete or the cache okay because we
  22759. 14:38:03have this update product this one right
  22760. 14:38:05this so whenever I'm going to update so
  22761. 14:38:07there is a possibility that this data
  22762. 14:38:09will not be immediately updated so let's
  22763. 14:38:11see if this is happening right so maybe
  22764. 14:38:13I'm trying to send it here and this is
  22765. 14:38:16now we are getting from caset whatever
  22766. 14:38:19and then let's go to patch and what I'm
  22767. 14:38:22going to I will try to update the first
  22768. 14:38:25product stock from 30 to 5,000 and then
  22769. 14:38:28I'll send it here and go to products and
  22770. 14:38:32let's see what we getting. We are
  22771. 14:38:33getting stock as 30 because this is
  22772. 14:38:36going to still taking from your cash
  22773. 14:38:38only. So that means whenever you're
  22774. 14:38:39going to update the data that time also
  22775. 14:38:42you need to invalidate the cash. So that
  22776. 14:38:44means first of all what I need to do
  22777. 14:38:45when I'm going to update so instead of
  22778. 14:38:48get product by id first of all I need to
  22779. 14:38:50get fetch product single product from
  22780. 14:38:52database. So this is the first thing and
  22781. 14:38:54then whenever I'm going to update the
  22782. 14:38:56product that time also I need to do
  22783. 14:38:58that. So I'm just going to do con
  22784. 14:39:00product equal to this. [snorts]
  22785. 14:39:02So first I'm going to do a and here I
  22786. 14:39:05need to first delete the all product
  22787. 14:39:08cash and at the same time I also need to
  22788. 14:39:10delete the single product. Now you might
  22789. 14:39:13be thinking okay what is this single
  22790. 14:39:14product cache? This single product cache
  22791. 14:39:16is nothing but this get product cash
  22792. 14:39:18that we have created. So this also we
  22793. 14:39:21need to uh delete. Now you might be
  22794. 14:39:22asking why.
  22795. 14:39:24Okay. Now consider that when we are
  22796. 14:39:26updating a product this product with ID
  22797. 14:39:29this we are updating from 5,000 stock to
  22798. 14:39:3210,000. And if you try to go to this
  22799. 14:39:35fetch product by ID now let's say this
  22800. 14:39:37data is now currently stored in your H
  22801. 14:39:40sorry in your C. When you are updating
  22802. 14:39:42this product uh stock from 5,000 to
  22803. 14:39:4510,000, there is a possibility that when
  22804. 14:39:46you're going to send this, you are not
  22805. 14:39:48going to get it. Now you're getting but
  22806. 14:39:50immediately you're not going to get it
  22807. 14:39:51because this data will be stored in your
  22808. 14:39:53single product cache you're getting. So
  22809. 14:39:55that means not only all product cash we
  22810. 14:39:58need to delete which is this one single
  22811. 14:40:00product cash also we need to delete
  22812. 14:40:02because update will happen on a single
  22813. 14:40:03product and it doesn't know that this
  22814. 14:40:06product ID data is present in cash or
  22815. 14:40:08not and at the same time you're updating
  22816. 14:40:10the same product uh stock from 5,000 to
  22817. 14:40:1310,000 means this cash also you need to
  22818. 14:40:15delete you are getting what I'm trying
  22819. 14:40:16to say so that mean this also you need
  22820. 14:40:18to delete so to do this one let's go
  22821. 14:40:21here right so I'm just going to do async
  22822. 14:40:24function
  22823. 14:40:25and I'll do delete
  22824. 14:40:28single
  22825. 14:40:30product cash. So here I'm just going to
  22826. 14:40:33get the product ID which will be a
  22827. 14:40:35number and here what will be the uh
  22828. 14:40:38return type. So this also will be
  22829. 14:40:39promise right? So it will be promise
  22830. 14:40:42of void and here first of all what we
  22831. 14:40:46need to check. So I'm just going to
  22832. 14:40:48check the cast key and this will be get
  22833. 14:40:51product cast key pass the product ID
  22834. 14:40:56and then you're going to do a and I'll
  22835. 14:40:58do radiusclient dot delete
  22836. 14:41:01and then you're going to delete the cast
  22837. 14:41:03key and then I'm simply going to do
  22838. 14:41:05console dot log and I'll do cast delete
  22839. 14:41:10and I'm simply going to do here cast key
  22840. 14:41:13and this also we are going to call it
  22841. 14:41:16here. So I'm just going to the a delete
  22842. 14:41:18single product cash and here we need to
  22843. 14:41:21pass the ID right what is the ID id is
  22844. 14:41:24this ID we need to pass and then we
  22845. 14:41:26going to return the product here
  22846. 14:41:30and this is done so now to verify this
  22847. 14:41:33first what I'll do right see what I'm
  22848. 14:41:35doing first I'm going to send this okay
  22849. 14:41:37so I'm going to request this so now it's
  22850. 14:41:40going to first miss the cash next time
  22851. 14:41:42it's going to hit the cash now I'm going
  22852. 14:41:45to go to update and change this product
  22853. 14:41:47ID stock from 5,000 to uh let's do
  22854. 14:41:5115,000
  22855. 14:41:53and I'm going to update this. Now if I
  22856. 14:41:55go here right and try to send this
  22857. 14:41:57you'll see that immediately you are
  22858. 14:41:59getting 15,000. Why? Because this line
  22859. 14:42:01is now deleting that product cash
  22860. 14:42:03immediately. Also if you go to products
  22861. 14:42:06and try to send this you see that you
  22862. 14:42:07are getting immediately 15,000. You're
  22863. 14:42:09getting this one. So that means both the
  22864. 14:42:11whatever in both the endpoints we have
  22865. 14:42:13added this one in cash we are
  22866. 14:42:15invalidating both the cache whenever we
  22867. 14:42:18are going to update this particular
  22868. 14:42:19product in your DB. So this is how you
  22869. 14:42:22have to understand the concept of cash
  22870. 14:42:24aside pattern still cash isue and cash
  22871. 14:42:27invalidation. I think you might not be
  22872. 14:42:30thinking like these are very important
  22873. 14:42:31concept. Okay these things you are going
  22874. 14:42:33to use mostly whenever you're going to
  22875. 14:42:34create any kind of grant. As I already
  22876. 14:42:37mentioned in my previous videos, at the
  22877. 14:42:39end of the day, whatever complex project
  22878. 14:42:41you are creating, everything is CRUD. So
  22879. 14:42:44what I mean by this, either you're going
  22880. 14:42:45to create something or you're going to
  22881. 14:42:47update something or you are going to
  22882. 14:42:48list something or you're going to delete
  22883. 14:42:50something. So all the things is CRUD. If
  22884. 14:42:53you can master all of this right, you
  22885. 14:42:55will be able to tackle any kind of
  22886. 14:42:56complex project. So with this, I think
  22887. 14:42:58we have almost covered most of the
  22888. 14:43:00things. Now let's move to the next one
  22889. 14:43:01which will be I'm going to give you some
  22890. 14:43:02very basic idea how you can implement
  22891. 14:43:04rate limiting in radius. All right. So
  22892. 14:43:06now let's understand how we can create a
  22893. 14:43:08simple rate limiter using radius. Okay.
  22894. 14:43:11So first what I will do right. So let's
  22895. 14:43:13go to middleware and maybe I'm just
  22896. 14:43:15going to do rate uh limit dot middleware
  22897. 14:43:23uh ts. Okay. So first of all what we
  22898. 14:43:27need to uh do here. So first you have to
  22899. 14:43:29decide that okay what will be your rate
  22900. 14:43:31limiting rules. So what I mean by this
  22901. 14:43:33so let's say we have a endpoint consider
  22902. 14:43:36products endpoint or whatever endpoint
  22903. 14:43:37that you're using that as a user that
  22904. 14:43:40whenever your users will be using that
  22905. 14:43:43particular endpoint is going to hit that
  22906. 14:43:45particular endpoint how much or or how
  22907. 14:43:48many request they can do in a certain
  22908. 14:43:51time frame. So for example let's say
  22909. 14:43:53they can do [snorts] five requests only
  22910. 14:43:56within 30 seconds. So that will be your
  22911. 14:43:59configuration and that will depend based
  22912. 14:44:01on the app you're building based on the
  22913. 14:44:03security and then there are lot of other
  22914. 14:44:05factors. Okay. So in this case first we
  22915. 14:44:07are going to take something like this.
  22916. 14:44:09I'm just going to do a rate limit
  22917. 14:44:13rate limit
  22918. 14:44:15window seconds.
  22919. 14:44:19So this will be I'm just going to give
  22920. 14:44:21this one as 60 seconds. And also I'm
  22921. 14:44:22just going to take one more which will
  22922. 14:44:24be rate limit and I'll do this one as
  22923. 14:44:27max requests.
  22924. 14:44:30This will be let's do maximum 10 request
  22925. 14:44:32or maybe five requests whatever you want
  22926. 14:44:34to do. So now I'm just going to create a
  22927. 14:44:36middleware. So I'm just going to do
  22928. 14:44:38export async function and here I'm
  22929. 14:44:41simply going to do this one as product
  22930. 14:44:43rate limiter because we are doing
  22931. 14:44:46something related to product and here we
  22932. 14:44:49are going to have a request. So this
  22933. 14:44:51will be the request from express.
  22934. 14:44:57So let's import this, right? So I'm just
  22935. 14:44:59going to import from
  22936. 14:45:02express
  22937. 14:45:09and then I'm also going to take a
  22938. 14:45:11response which will be response right.
  22939. 14:45:14So let's import the response also and
  22940. 14:45:17also I'm going to import the next
  22941. 14:45:19function
  22942. 14:45:20and then I'm going to have a next
  22943. 14:45:22middleware which is going to call the
  22944. 14:45:23next middleware or next endpoint
  22945. 14:45:26whatever we have. So here I'm simply
  22946. 14:45:28going to do a try. Okay. So first of all
  22947. 14:45:31what we need to do now here couple of
  22948. 14:45:32things I wanted to mention. Okay. So you
  22949. 14:45:34have to understand all of these each and
  22950. 14:45:36every line what we are doing here. Also
  22951. 14:45:38let's do a catch and I'm going to do
  22952. 14:45:40error and I'll just going to log error
  22953. 14:45:42here. Let's do rate limit
  22954. 14:45:46radius error.
  22955. 14:45:49And what we'll do right, I'm going to
  22956. 14:45:51call the next with our error. So it's
  22957. 14:45:54going to call this error middleware
  22958. 14:45:56because if you go to the server.ts,
  22959. 14:45:58we're using the middleware here. I think
  22960. 14:46:00this one. Okay. So the so this is done.
  22961. 14:46:04So now first of all, let's do one thing.
  22962. 14:46:06Let's always at the end what we have to
  22963. 14:46:08do. We have to call the next middleware
  22964. 14:46:09or next route handler whatever we we
  22965. 14:46:12have. So you have to call the next. This
  22966. 14:46:14is important because if you don't do it,
  22967. 14:46:16it will it will be stuck. Okay, this is
  22968. 14:46:18a middle middleware. So it needs to call
  22969. 14:46:20the next middleware or it needs to call
  22970. 14:46:21the next what to say the next route
  22971. 14:46:24handler. [snorts] Okay, first of all we
  22972. 14:46:27need to understand that what we need to
  22973. 14:46:28do right see whenever you're going to
  22974. 14:46:30implement a real products and level of
  22975. 14:46:32application this uh application this is
  22976. 14:46:34what my understanding right or from my
  22977. 14:46:36practical experience whenever you are
  22978. 14:46:38going to implement this kind of rate
  22979. 14:46:39limiting right. So these uh so first of
  22980. 14:46:42all you have to work with IP right
  22981. 14:46:44because whenever you're going to uh
  22982. 14:46:47implement rate [snorts] limiting there
  22983. 14:46:48has to be something with IP. So what I
  22984. 14:46:50mean by IP? So let's say every user is
  22985. 14:46:53going to have some IP, right? And each
  22986. 14:46:54IP is going to get uh its own counter in
  22987. 14:46:58radius because we using radius. And what
  22988. 14:47:00will happen in the actual real
  22989. 14:47:02production level pattern will be that
  22990. 14:47:04will actually go behind a proxy or let's
  22991. 14:47:06say there is a load balancer. So what I
  22992. 14:47:09mean by this? So first of all, let's
  22993. 14:47:10write all of this because this is
  22994. 14:47:11important. So each IP is going to get or
  22995. 14:47:14will get will get its own counter in
  22996. 14:47:19radius. Okay. So this is the first
  22997. 14:47:21thing. So let's say I have a rate limit,
  22998. 14:47:23right? So I'm just going to do rate
  22999. 14:47:25limit. I think this we've already this
  23000. 14:47:26pattern colon products because we are
  23001. 14:47:28creating a products middleware colon and
  23002. 14:47:30let's say there is a IP. So something
  23003. 14:47:32like this 127.0 blah blah whatever. And
  23004. 14:47:35then [snorts]
  23005. 14:47:36I'm also going to have a rate limit
  23006. 14:47:40colon products
  23007. 14:47:44products
  23008. 14:47:46and then we have one something like
  23009. 14:47:48this. So here what will happen right? So
  23010. 14:47:51let's say there are multiple users will
  23011. 14:47:53be using your application at the same
  23012. 14:47:55time. What will happen that if one user
  23013. 14:47:58crossing the limit? So let's say one
  23014. 14:48:00user So let's say one user is crossing
  23015. 14:48:05the limit. It's possible it will not
  23016. 14:48:09going to it will not going to block
  23017. 14:48:12everyone. Okay. So this is the main
  23018. 14:48:14reason it's not going to block
  23019. 14:48:17everyone.
  23020. 14:48:19So that is the reason we need to have
  23021. 14:48:21some it and this in real application or
  23022. 14:48:25real products and pattern I'm going to
  23023. 14:48:27do will be this will go behind a proxy
  23024. 14:48:32or it can be a load balancer. Okay.
  23025. 14:48:36So this IP handling whatever you have to
  23026. 14:48:38do it needs extra configuration also
  23027. 14:48:40it's not that much simple but for now
  23028. 14:48:42what how we are going to do it. So for
  23029. 14:48:44now I'm just going to do IP and I'm just
  23030. 14:48:45going to take the request ip or else
  23031. 14:48:48this will be unknown. Okay but I already
  23032. 14:48:50mentioned this is how you have to do it.
  23033. 14:48:52There can be lot of other configuration
  23034. 14:48:53that you have to make sure that you're
  23035. 14:48:55using also there is a possible that
  23036. 14:48:57maybe that application that you're using
  23037. 14:48:59uh that also has overseas audiences
  23038. 14:49:02maybe the country that application you
  23039. 14:49:04have maybe some from some other country
  23040. 14:49:06you have a user. So lot of other things
  23041. 14:49:08is there. So you have to [snorts]
  23042. 14:49:10consider all of this but at the end the
  23043. 14:49:12main idea is that if one user is
  23044. 14:49:14crossing the limit it should not block
  23045. 14:49:16other users because there is a
  23046. 14:49:17possibility that 100 users thousand or
  23047. 14:49:19lakh users are using your um application
  23048. 14:49:23at the same time and then now I'm just
  23049. 14:49:25going to take as usual rate limiter key.
  23050. 14:49:29So I'm just going to do this one as
  23051. 14:49:31let's do same only. So I'll do rate
  23052. 14:49:33limit colon products
  23053. 14:49:37colon and then here I'm going to pass
  23054. 14:49:39the IP. So this will be my IP. Okay. So
  23055. 14:49:42now first of [snorts] all what I need to
  23056. 14:49:43do right I need to have a request count
  23057. 14:49:45how many requests that particular user
  23058. 14:49:48is doing. So here I'm just going to do a
  23059. 14:49:50radius client dot and then I'm just
  23060. 14:49:53going to do increment and here I'm going
  23061. 14:49:54to have the rate limiter key. Okay. What
  23062. 14:49:57increment is going to do? It's going to
  23063. 14:49:59increase the request counter by one. And
  23064. 14:50:01now this is where radius is very very
  23065. 14:50:03good. Okay. So this is where radius is
  23066. 14:50:06very very good
  23067. 14:50:09here. Why? Because let's say you have
  23068. 14:50:12multiple backend servers right? What
  23069. 14:50:15will happen right? If you have multiple
  23070. 14:50:16backend servers those many backend
  23071. 14:50:19servers can share the same exact radius
  23072. 14:50:22counter. [snorts]
  23073. 14:50:22Okay. So this is actually very important
  23074. 14:50:24concept. So now consider that we are
  23075. 14:50:26storing this particular count in a
  23076. 14:50:28normal JavaScript variable. Okay. not
  23077. 14:50:30like this. So what will happen even
  23078. 14:50:33consider you have multiple back end
  23079. 14:50:34server that part I already mentioned
  23080. 14:50:36each server is going to have its own
  23081. 14:50:39separate count you're getting what I'm
  23082. 14:50:41talking about so what will what is the
  23083. 14:50:43downside so if each server is going to
  23084. 14:50:45have it own separate count it's going to
  23085. 14:50:47break the rate limiting okay because you
  23086. 14:50:50consider your app is running on multiple
  23087. 14:50:52servers and you're using some normal
  23088. 14:50:55JavaScript variable so in that case it
  23089. 14:50:58can actually break why because each
  23090. 14:51:00server Let's say there is one server,
  23091. 14:51:02second server, first server is going to
  23092. 14:51:04have own separate count. Second server
  23093. 14:51:06is going to have own separate count. And
  23094. 14:51:07this is actually extremely helpful
  23095. 14:51:09because radius is going to do what this
  23096. 14:51:11multiple back end servers actually can
  23097. 14:51:12share the same radius counter. So this
  23098. 14:51:15will be the request count that we are
  23099. 14:51:16going to increment. So now what I'm
  23100. 14:51:18going to do right I'm simply going to do
  23101. 14:51:20the request count is equal to equal to
  23102. 14:51:22one and I'm going to explain what we are
  23103. 14:51:24doing. So I'm just going to dive it and
  23104. 14:51:26I'll do radiusclient doxpire.
  23105. 14:51:29So I'm just going to give the rate
  23106. 14:51:31limiter let limiter key and also I'm
  23107. 14:51:34just going to give the rate limiter
  23108. 14:51:36window second. So what we are doing
  23109. 14:51:38here? So first of what expert is going
  23110. 14:51:39to do is going to create a fixed time
  23111. 14:51:41window for this particular counter. So
  23112. 14:51:43we have a counter right. So now we are
  23113. 14:51:46actually setting this expiry when the
  23114. 14:51:48count is one. Okay. Now now think in
  23115. 14:51:51this way right? So let's say if we reset
  23116. 14:51:53this expiry on every request. So every
  23117. 14:51:55time they're going to hit that request
  23118. 14:51:57and we are going to reset this expiry.
  23119. 14:51:59What will happen? This time window that
  23120. 14:52:01we have, it's going to keeps moving. We
  23121. 14:52:03can't do that. Okay? Because we are
  23122. 14:52:05implementing a fixed window limiter. And
  23123. 14:52:07this is also very very simple level,
  23124. 14:52:08right? This is not even complex for this
  23125. 14:52:12kind of simple fixed window limiter.
  23126. 14:52:15What should happen right? Expiry should
  23127. 14:52:17start with the first request. So that is
  23128. 14:52:19the reason when the request count is
  23129. 14:52:20one, we are actually setting this
  23130. 14:52:22expiry. I hope this is clear. What will
  23131. 14:52:24happen? Because this is actually 60. So
  23132. 14:52:26after 60cond radius is going to delete
  23133. 14:52:28this particular key and it's going to
  23134. 14:52:31count again from phrase. Okay. So after
  23135. 14:52:3360 it's going to delete and it's going
  23136. 14:52:35to start counting from phrase. Very
  23137. 14:52:37important. So I'm just going to write it
  23138. 14:52:39here that after 60 seconds
  23139. 14:52:43after 60 seconds
  23140. 14:52:47radius will deletes this key and start
  23141. 14:52:52counting from fresh. So I think this is
  23142. 14:52:56done and then I'm just going to set some
  23143. 14:52:58header. So I'm just going to do race dot
  23144. 14:52:59set header and let's give this one as x
  23145. 14:53:04rate limit
  23146. 14:53:06limit
  23147. 14:53:08limit
  23148. 14:53:11and this will be your rate limiting max
  23149. 14:53:13request. Also we are going to set one
  23150. 14:53:16more. So I'll do set header and this
  23151. 14:53:18will be x
  23152. 14:53:21rate limit. And let's do remaining
  23153. 14:53:24because we also need to check that how
  23154. 14:53:26many is remaining. So I'm just going to
  23155. 14:53:28do remaining. And how we going to get
  23156. 14:53:30the remaining? So this will be very
  23157. 14:53:32simple. So we do math do max. So I'm
  23158. 14:53:34going to take the zero zero. And you
  23159. 14:53:37have the rate limit max request correct
  23160. 14:53:40versus your request count. Simple
  23161. 14:53:43because this request count we are using
  23162. 14:53:44here, right? So you're increasing this
  23163. 14:53:45one. So every time they're going to do a
  23164. 14:53:47request, it's going to increase the uh
  23165. 14:53:49counter. So that means this is going to
  23166. 14:53:51check that how many request is remaining
  23167. 14:53:53within that time frame. If this request
  23168. 14:53:56count that you have is greater than the
  23169. 14:53:59rate limit max request. So what will
  23170. 14:54:01happen? That means you've already
  23171. 14:54:02exceeded the limit. So in that case we
  23172. 14:54:04have to return. So I'm just going to do
  23173. 14:54:06raise status and what will be the return
  23174. 14:54:084 to9 because you're doing too many
  23175. 14:54:10request and you'll see that in so many
  23176. 14:54:12uh websites also you're going to get
  23177. 14:54:14this kind of thing. So I'm going to do
  23178. 14:54:16success will be false and message will
  23179. 14:54:19be I'm just going to do this one as too
  23180. 14:54:22many requests please try and later
  23181. 14:54:26something like this
  23182. 14:54:28right this is done and then if this is
  23183. 14:54:30not the case we anyway is going to call
  23184. 14:54:32the next so this is how you can create I
  23185. 14:54:35think we have uh discussed each and
  23186. 14:54:37every line so how we going to do that so
  23187. 14:54:40go to your products uh sorry routes not
  23188. 14:54:42products go to product router and here
  23189. 14:54:44you simply going to do router dot use
  23190. 14:54:47and for now I'm just going to do product
  23191. 14:54:49rate limiter and this is done. So now
  23192. 14:54:52let's save this and if you go here right
  23193. 14:54:55maybe let's I'm just going to hit this
  23194. 14:54:56endpoint here. So one time and let's do
  23195. 14:54:592 3 4 5 6. So you're getting too many
  23196. 14:55:04request please try again later. So you
  23197. 14:55:07are going to immed uh is going to get it
  23198. 14:55:09right. So until is going to reset again.
  23199. 14:55:12All right. So I hope this is clear. This
  23200. 14:55:13is very important concept. But again one
  23201. 14:55:15thing I wanted to mention for now we are
  23202. 14:55:17actually applying this for all the
  23203. 14:55:18routes. That should not be the case.
  23204. 14:55:20Maybe the uh application that you are
  23205. 14:55:22building. Let's say in your case only
  23206. 14:55:24one particular
  23207. 14:55:26endpoint you have to apply product red
  23208. 14:55:28limiter. So you can also apply in that
  23209. 14:55:30particular uh endpoint label also. But
  23210. 14:55:32for now we are applying on the overall
  23211. 14:55:34but again the concept you have to
  23212. 14:55:35understand. So with this I think that's
  23213. 14:55:37all for this particular section. Now
  23214. 14:55:39let's move on to the next one. All
  23215. 14:55:40right. So now we are going to end this
  23216. 14:55:42particular video with our last concept
  23217. 14:55:44which will be pops up. So what is popsup
  23218. 14:55:46right? So in so in SW we call that one
  23219. 14:55:49as popsup but that actually means
  23220. 14:55:51publish and subscribe. So first let's do
  23221. 14:55:54one thing right. What are you going to
  23222. 14:55:55do? Let's go here and I'm just going to
  23223. 14:55:58do this one as radius popsup.
  23224. 14:56:03DS. So as you uh I already mentioned
  23225. 14:56:06that this will be publish and subscribe.
  23226. 14:56:11So what publisher is going to do?
  23227. 14:56:13Publisher is going to send a particular
  23228. 14:56:15message. So here we are going to do
  23229. 14:56:17publisher sends a message.
  23230. 14:56:21What subscriber is going to do?
  23231. 14:56:23Subscriber is going to listens
  23232. 14:56:27and receives the message
  23233. 14:56:31and there is another concept or you can
  23234. 14:56:33consider this one as a mediator and that
  23235. 14:56:35we call as channel. What's channel?
  23236. 14:56:37Channel is actually the topic name.
  23237. 14:56:39Okay, from both sides
  23238. 14:56:43both sides use both side means publisher
  23239. 14:56:46and the subscriber. So this is a very
  23240. 14:56:48overview. Okay. Now uh it's kind of like
  23241. 14:56:51a live messaging. Okay, it's not like
  23242. 14:56:54any long-term storage or anything like
  23243. 14:56:55that. So let's say in this particular
  23244. 14:56:58project we're going to create a very
  23245. 14:56:59simple popsup where we are going to
  23246. 14:57:01let's say I'm just going to give you a
  23247. 14:57:03very simple example like we're going to
  23248. 14:57:04create a notification. So once uh admin
  23249. 14:57:08or API is going to create the
  23250. 14:57:09notification express is going to publish
  23251. 14:57:11the event to the radius channel and then
  23252. 14:57:14subscriber is going to receive it
  23253. 14:57:16instantly. Okay. And then uh maybe we
  23254. 14:57:19can do something like this. We are going
  23255. 14:57:20to log that notification in another
  23256. 14:57:22terminal. This is like very basic back
  23257. 14:57:24end to back end live event. Uh we are
  23258. 14:57:26going to implement. Okay. Now here uh
  23259. 14:57:29when you're giving some interview right
  23260. 14:57:31there is a possibility that you are
  23261. 14:57:32going to get query from this particular
  23262. 14:57:34concept because this is actually very
  23263. 14:57:35important. Okay. Now what kind of uh
  23264. 14:57:39questions that you might get okay that
  23265. 14:57:40okay you mention that okay P mean
  23266. 14:57:42publish right. So what publisher
  23267. 14:57:45actually means and what publisher is
  23268. 14:57:47going to send a message where. So you
  23269. 14:57:49have to mention that publisher is going
  23270. 14:57:50to send a message to a radius channel.
  23271. 14:57:52Okay, it is going to send a message to a
  23272. 14:57:55radius channel. Subscriber is going to
  23273. 14:57:57listen and receive the message from that
  23274. 14:57:59particular channel. So that is a listen
  23275. 14:58:01channel is like a topic name that both
  23276. 14:58:02side is going to use or what I when I
  23277. 14:58:06learned I remember I usually used to
  23278. 14:58:08remember this one like this the channel
  23279. 14:58:10is like a mediator. So you have one
  23280. 14:58:11person
  23281. 14:58:13within two person and that one person is
  23282. 14:58:15actually taking the message from one
  23283. 14:58:17person and it's going to sending the
  23284. 14:58:19message to another person. The second
  23285. 14:58:20person who is actually receiving the
  23286. 14:58:22message.
  23287. 14:58:24So now uh let's uh do this one with a
  23288. 14:58:26very simple example first. So first
  23289. 14:58:29let's do one thing. I'm just going to
  23290. 14:58:31copy this one from here. Right? So let's
  23291. 14:58:33go
  23292. 14:58:35till this part. We need
  23293. 14:58:39this one and then I'll paste it here. So
  23294. 14:58:42first of all what I will do we going to
  23295. 14:58:44have a channel as I already mentioned.
  23296. 14:58:46So there will be a channel and I'm just
  23297. 14:58:47going to do this one as demo
  23298. 14:58:49notifications.
  23299. 14:58:53Okay. So here I'll do async function
  23300. 14:58:56run.
  23301. 14:58:58So first uh when you're going to
  23302. 14:59:01implement a pub sub right it actually
  23303. 14:59:04needs two clients. So needs two clients.
  23304. 14:59:08One client
  23305. 14:59:10will be to publish
  23306. 14:59:15and second one to subscribe. This is
  23307. 14:59:19also important concept and prerequisite
  23308. 14:59:22like that you need to know all of these.
  23309. 14:59:24So there will be publisher. So publisher
  23310. 14:59:26will be create client.
  23311. 14:59:29This create client you're going to get
  23312. 14:59:30from radius. And here you're going to
  23313. 14:59:32pass the URL which will be your radius
  23314. 14:59:34URL. Then you're going to have a
  23315. 14:59:36subscriber. Right? So this will be your
  23316. 14:59:38subscriber and this also will be your
  23317. 14:59:40create client and this will be your URL
  23318. 14:59:42which will be your radius URL. Okay. So
  23319. 14:59:45now I have two client. Now publisher can
  23320. 14:59:47send a message or publish a message via
  23321. 14:59:50a channel and subscriber is going to
  23322. 14:59:52receive that particular uh message from
  23323. 14:59:54that particular channel. So now here I'm
  23324. 14:59:57just going to have it. So I do publisher
  23325. 14:59:59dot connect. So first we need to
  23326. 15:00:01connect. Same we need to do for
  23327. 15:00:03subscriber. So I'll do subscriber dot
  23328. 15:00:06connect.
  23329. 15:00:08Okay. So now let's do a log here. So I'm
  23330. 15:00:11just going to do a log and I'll do
  23331. 15:00:13publisher
  23332. 15:00:15connected
  23333. 15:00:17and subscriber connected.
  23334. 15:00:22Okay. Also you can check the ping also.
  23335. 15:00:24Right. So let's say you want to check
  23336. 15:00:26the ping. So I'm just going to do ping.
  23337. 15:00:30So, and here I'm simply going to do a
  23338. 15:00:33publisher
  23339. 15:00:34dotping.
  23340. 15:00:38Okay. And then maybe what else? Maybe
  23341. 15:00:40let's do one more here. And I'm just
  23342. 15:00:42going to do subscriber
  23343. 15:00:48subscriber
  23344. 15:00:50listens.
  23345. 15:00:52Okay. So this is the first step that we
  23346. 15:00:54need to do. Now one very very important
  23347. 15:00:57concept when subscriber is going to
  23348. 15:00:58listen that subscriber
  23349. 15:01:03must be
  23350. 15:01:06active
  23351. 15:01:07before publish.
  23352. 15:01:10Okay why this is very important because
  23353. 15:01:12if nobody nobody is listening right so
  23354. 15:01:15let's say there is no one to listen when
  23355. 15:01:18publisher is going to publish that
  23356. 15:01:19message it might be missed. So it's
  23357. 15:01:21actually very important as that whatever
  23358. 15:01:23subscriber that you're using that
  23359. 15:01:25subscriber is actually responsible for
  23360. 15:01:29consuming that message via that channel
  23361. 15:01:31from your publisher. So that subscriber
  23362. 15:01:33must be active and now what we going to
  23363. 15:01:36do right so
  23364. 15:01:38we'll take subscriber and this is going
  23365. 15:01:40to give this subscribe method and it's
  23366. 15:01:43going to receive the channels you can
  23367. 15:01:44see that you have to pass the channel
  23368. 15:01:45and it's going to receive the message
  23369. 15:01:47correct. So now what you're going to do
  23370. 15:01:49simply so I'm just going to do con data
  23371. 15:01:53and I'm simply going to do JSON.parts.
  23372. 15:01:55I'm going to get the message and then
  23373. 15:01:58I'll just do some log here. So do
  23374. 15:02:00subscriber
  23375. 15:02:02received and also let's do some more
  23376. 15:02:05logging here. And I'm just going to log.
  23377. 15:02:08So let's do here title. So I'm just
  23378. 15:02:11going to log a title. And I'm just going
  23379. 15:02:12to do data dot title.
  23380. 15:02:15And here also let's do one more which
  23381. 15:02:17will be data dot message.
  23382. 15:02:22So this will be data dot message. Okay.
  23383. 15:02:26So now what we're going to do so I'm
  23384. 15:02:28just going to do another log here and
  23385. 15:02:30let's do this one as subscribed
  23386. 15:02:34to channel and then we're going to pass
  23387. 15:02:37the channel here. Okay. So now this is
  23388. 15:02:40how subscriber is going to receive. Now
  23389. 15:02:43this is not a done. Why? because
  23390. 15:02:45publisher haven't sent the event yet,
  23391. 15:02:47right? So publisher needs to send the
  23392. 15:02:49event. So now this is what we're going
  23393. 15:02:51to just start. So I'm just going to do
  23394. 15:02:53publisher
  23395. 15:02:54is now sending the event.
  23396. 15:02:59Okay. And then I'm simply going to take
  23397. 15:03:01the event data here. So this will be our
  23398. 15:03:03actual data. So here I'm just going to
  23399. 15:03:05take a title and this will be radius
  23400. 15:03:09course.
  23401. 15:03:10And then I'm just going to have a
  23402. 15:03:12message and I'm going to do pubsub
  23403. 15:03:16demo.
  23404. 15:03:18Okay, so this is the event. So now
  23405. 15:03:20publisher is going to send the message
  23406. 15:03:23to the channel immediately. Okay, how
  23407. 15:03:25it's going to do that? So I'm just going
  23408. 15:03:26to do con. So I'll do receivers and this
  23409. 15:03:30will be have it. So we're going to have
  23410. 15:03:32the publisher. This publisher is going
  23411. 15:03:34to have now publish. Okay. So this will
  23412. 15:03:36be a publish method not subscribe. So
  23413. 15:03:39this will be publish then you're going
  23414. 15:03:41to pass the channel and then you're
  23415. 15:03:43going to do JSON.stify what you have to
  23416. 15:03:46pass you have to pass this event and
  23417. 15:03:48this event this subscriber is going to
  23418. 15:03:50receive via this particular channel
  23419. 15:03:52which is the message that it's receiving
  23420. 15:03:54here. Okay. And then we can also do some
  23421. 15:03:57log here and then I'm simply going to do
  23422. 15:03:59publish event
  23423. 15:04:03and also let's log here. I'm just going
  23424. 15:04:07to do active subscribers.
  23425. 15:04:13Okay. And these will be receivers.
  23426. 15:04:17Now what we are going to do right, we
  23427. 15:04:19are going to so let's do one thing. We
  23428. 15:04:21are going to have some small weight so
  23429. 15:04:24that whenever subscriber is going to
  23430. 15:04:26receive it, it's going to log okay
  23431. 15:04:28before it's going to exit. So I'm just
  23432. 15:04:30going to grab it. So you can simply do a
  23433. 15:04:31new promise here. normal concept. Take a
  23434. 15:04:34resolve and you're simply going to do a
  23435. 15:04:36resolve. Sorry, set time out. Set time
  23436. 15:04:39out. And I'm just going to resolve
  23437. 15:04:42and I'm just going to [clears throat]
  23438. 15:04:43give 300. Okay. So now I think this is
  23439. 15:04:47done also. Uh what else? Maybe also we
  23440. 15:04:51need to unsubscribe now. So I'm just
  23441. 15:04:52going to do subscribe dot unsubscribe.
  23442. 15:04:55We're going to pass the channel
  23443. 15:04:58and then we're going to quit. Quit is
  23444. 15:04:59again the same thing. So you'll do
  23445. 15:05:01subscriber doquit and you're also going
  23446. 15:05:04to do publisher doquit
  23447. 15:05:07and then let's [clears throat] do one
  23448. 15:05:09more log here and this will be pubsub
  23449. 15:05:15demo done. So the main thing you have to
  23450. 15:05:18understand there are a couple of key
  23451. 15:05:20points. First one is that pubs sub is
  23452. 15:05:22nothing but it's like a live messaging.
  23453. 15:05:24Subscriber must be active before publish
  23454. 15:05:28because you see that subscriber is going
  23455. 15:05:29to subscribe. It's going to get the
  23456. 15:05:31event data before publisher is going to
  23457. 15:05:34publish. It has to be active or else
  23458. 15:05:36once it's going to publish if the
  23459. 15:05:37subscriber is not active it's not going
  23460. 15:05:39to work because there is no no one is to
  23461. 15:05:42receive who is going to receive the
  23462. 15:05:44data. Okay. So this is one important
  23463. 15:05:45concept and another important concept
  23464. 15:05:48popsub is going to or popsub doesn't
  23465. 15:05:51store messages permanently. Okay, this
  23466. 15:05:53is another important concept. So go to
  23467. 15:05:55package dojson and I'm just going to
  23468. 15:05:58copy this
  23469. 15:06:00run it. So I'll do radius popsub
  23470. 15:06:04and this will be let's copy this.
  23471. 15:06:10Okay. And here I'm just going to run
  23472. 15:06:14this.
  23473. 15:06:16So npm run
  23474. 15:06:18and then enter.
  23475. 15:06:21See again I've done mistake. We have to
  23476. 15:06:23run this right. So let's go here. We
  23477. 15:06:26have to just run this. We'll copy this.
  23478. 15:06:32I'll do popsub.
  23479. 15:06:36Okay. So let's see what is happening
  23480. 15:06:38here. So I'll do clear.
  23481. 15:06:41All right. So fantastic. So you can see
  23482. 15:06:43that we have publisher connected. Let's
  23483. 15:06:44go one by one. Subscriber connected
  23484. 15:06:49ping pong. You are getting subscriber
  23485. 15:06:51listens. Now here you will notice one
  23486. 15:06:54thing subscriber listen. Now you say
  23487. 15:06:56subscribe to channel which is this this
  23488. 15:06:58one. So before even though this is
  23489. 15:07:00active it's not going to receive the
  23490. 15:07:02data. Why? Publisher needs to publish.
  23491. 15:07:04Correct. So that's why getting
  23492. 15:07:07subscribed to channel publisher is now
  23493. 15:07:09sending event. Now you can see that
  23494. 15:07:11you're going to get subscriber received.
  23495. 15:07:13So title ready course blah blah message
  23496. 15:07:16whatever you're sending publish the
  23497. 15:07:17event active subscriber is one and pops
  23498. 15:07:20up demo is done. The main point is again
  23499. 15:07:22I'm repeating again and again when
  23500. 15:07:23you're going to implement this one in a
  23501. 15:07:25real project your subscriber has to be
  23502. 15:07:27active before publisher is going to
  23503. 15:07:28publish. If this is not the case you're
  23504. 15:07:30not going to work. Okay. So this is one
  23505. 15:07:32important concept. So with this I think
  23506. 15:07:35uh this part is also completed. So let's
  23507. 15:07:38do one thing right in the next section
  23508. 15:07:39I'm going to show you one very simple
  23509. 15:07:41example on this project context that how
  23510. 15:07:44we are going to implement this one
  23511. 15:07:45practically. So let's do this one in the
  23512. 15:07:47next part. All right. So now let's end
  23513. 15:07:49this particular video with the one
  23514. 15:07:51practical example that I'm going to
  23515. 15:07:53implement here. So first of all I think
  23516. 15:07:54we have already discussed the concept
  23517. 15:07:56right subscriber. I'm not going to
  23518. 15:07:57repeat the same thing again and again.
  23519. 15:07:59So first thing let's create one folder
  23520. 15:08:01here and I'm just going to give this one
  23521. 15:08:02as subscribers.
  23522. 15:08:04Okay. So this will be the first thing
  23523. 15:08:06and let's create this one as so do
  23524. 15:08:08notification
  23525. 15:08:13dots subscriber dot ts. So this is the
  23526. 15:08:17first thing and then also let's rename
  23527. 15:08:19copy this go to controllers and I'm just
  23528. 15:08:23going to do notification dot controller
  23529. 15:08:27ts also we'll go to routes I'll do
  23530. 15:08:30notification dot routes dot ts so first
  23531. 15:08:35let's start with the subscriber right so
  23532. 15:08:37first of all what we need so let's go
  23533. 15:08:39here I need to copy some of the things
  23534. 15:08:41we need the create client
  23535. 15:08:43uh also okay let's copy these to first
  23536. 15:08:47I'm going to paste it here also you need
  23537. 15:08:50to have a notification channel okay so
  23538. 15:08:52for this one what we are going to do
  23539. 15:08:54right so let's create here only maybe we
  23540. 15:08:56don't need another file so I'm just
  23541. 15:08:58going to do con and I'll do notification
  23542. 15:09:03channel and this will be notifications
  23543. 15:09:06so let's see what actually we are doing
  23544. 15:09:08here and I'm just going to take export
  23545. 15:09:12not export I think let's take in case in
  23546. 15:09:14other uh file else we need it. So I'll
  23547. 15:09:17do notifications
  23548. 15:09:19payload.
  23549. 15:09:20So for now I'm going to keep very very
  23550. 15:09:22simple. So I'll do id. So this will be a
  23551. 15:09:25string and also let's take a title. This
  23552. 15:09:28also will be a string. Let's take a
  23553. 15:09:31message.
  23554. 15:09:33This will be string. And let's take a
  23555. 15:09:36created
  23556. 15:09:37at this also will be a string. Okay. Now
  23557. 15:09:42uh what we are going to do right first
  23558. 15:09:43of all uh let's create one more method
  23559. 15:09:46here. So I'm just going to do async
  23560. 15:09:48function and I'm just going to do
  23561. 15:09:51publish notification.
  23562. 15:09:55So here I'm just going to get the
  23563. 15:09:56notification. The type will be
  23564. 15:09:58notification payload and this will be
  23565. 15:10:01the promise void.
  23566. 15:10:04What we are going to do simply so
  23567. 15:10:06publisher is going to do what? publisher
  23568. 15:10:08is going to send the live message to
  23569. 15:10:10radius. Okay. So here we are going to do
  23570. 15:10:12ait. So I'm just going to take the
  23571. 15:10:14radius client dot. So it's going to do a
  23572. 15:10:18publish. So it's going to publish to
  23573. 15:10:20your notification channel.
  23574. 15:10:23And here I'm just going to pass the
  23575. 15:10:25JSON.stringify. What notification is
  23576. 15:10:27going to pass this notification. If you
  23577. 15:10:29want you can also do notification.
  23578. 15:10:31Right?
  23579. 15:10:36That's it. So this will be to publish
  23580. 15:10:39it, right? So now let's save this. So
  23581. 15:10:41now we need to work on the subscriber.
  23582. 15:10:45So here uh we already have
  23583. 15:10:49create radius. We haven't
  23584. 15:10:53created uh radius. So we going to use
  23585. 15:10:55now. So first of all, let's do one
  23586. 15:10:57thing. So here we are going to create
  23587. 15:10:58our subscriber client. So I'm just going
  23588. 15:11:00to do subscriber client. So this will be
  23589. 15:11:03here. We need the create client and I'm
  23590. 15:11:05just going to do URL and this will be
  23591. 15:11:07your radius URL. So radius URL we need
  23592. 15:11:10to just copy this from here
  23593. 15:11:20we can do and this will be ready. Okay.
  23594. 15:11:24So first I'm just going to do
  23595. 15:11:25subscriberclient
  23596. 15:11:26dot on let's give one error in case any
  23597. 15:11:30error and here I'm simply going to
  23598. 15:11:34log one error
  23599. 15:11:39okay we'll do subs radius error
  23600. 15:11:43and this will be error so this is the
  23601. 15:11:45first thing now what we're going to do
  23602. 15:11:47right so we are going to start our
  23603. 15:11:49notific sorry subscriber notification so
  23604. 15:11:52I'll do as sync function
  23605. 15:11:53and I'll do start notification
  23606. 15:11:57subscriber.
  23607. 15:12:00So first what we need to do just see
  23608. 15:12:03this whatever we have done here. So what
  23609. 15:12:05we have done we have done the connect.
  23610. 15:12:07So here first [snorts] we need to
  23611. 15:12:08connect. So I'll do a subscriber client
  23612. 15:12:12dot connect.
  23613. 15:12:14So the first thing now what I need to do
  23614. 15:12:17right this subscriber needs to tell
  23615. 15:12:20radius okay I want to listen to this
  23616. 15:12:22particular channel which channel the
  23617. 15:12:23notification channel that we have
  23618. 15:12:25created this is the channel so here I'm
  23619. 15:12:28simply going to do and now it's your
  23620. 15:12:30task is that you have to subscribe to
  23621. 15:12:33this particular notification channel
  23622. 15:12:35right and also you're going to receive a
  23623. 15:12:38message
  23624. 15:12:40and here I'm just going to do a try and
  23625. 15:12:43uh in the try. So let in the catch what
  23626. 15:12:46you can do right in the catch you can
  23627. 15:12:47maybe just uh do something like this log
  23628. 15:12:52and then we can just do new notification
  23629. 15:12:55in sort I'm doing received
  23630. 15:12:59right so this will be new and here we
  23631. 15:13:01can just get the render the message here
  23632. 15:13:04in the try first of all I'm just going
  23633. 15:13:05to do con notification
  23634. 15:13:09and this will be JSON dotparse so here
  23635. 15:13:13we are going to get the message and this
  23636. 15:13:15will be as and we're going to pass this
  23637. 15:13:18right. So you can pass all of these or
  23638. 15:13:20you can pass as notification payload.
  23639. 15:13:25We can directly pass it here.
  23640. 15:13:28Okay. So this is done and then I'm going
  23641. 15:13:31to do a couple of logs so that we can
  23642. 15:13:33understand. So first I'm going to do new
  23643. 15:13:34notification
  23644. 15:13:36received.
  23645. 15:13:38Then I'll log it here. So I simply going
  23646. 15:13:41to do title. So this will be your
  23647. 15:13:46this notification dot title.
  23648. 15:13:50Copy this. We'll do message and created
  23649. 15:13:52at right. So do message and this will be
  23650. 15:13:56created at.
  23651. 15:14:01That's it. Okay. So this is done. So now
  23652. 15:14:04uh what else? I think this should be
  23653. 15:14:06fine. Now we have to call this. So now
  23654. 15:14:09we are going to do start notification
  23655. 15:14:12subscriber dot catch. Here I'm going to
  23656. 15:14:15get error and I'll simply just do
  23657. 15:14:18console dot error. And here I'm just
  23658. 15:14:22going to do fail to start notification
  23659. 15:14:25and I'll do error and then we'll do
  23660. 15:14:27process do.exit
  23661. 15:14:31of one.
  23662. 15:14:33So this is what we need to do. Okay. So
  23663. 15:14:36now not yet completed. Why? We need to
  23664. 15:14:39publish the notification. We we haven't
  23665. 15:14:40the publish. This is what we have
  23666. 15:14:42learned so far, right? Subscriber is
  23667. 15:14:43going to subscribe and subscribe is must
  23668. 15:14:46be active but publisher is going to
  23669. 15:14:48publish. So where we are going to do
  23670. 15:14:50that for this one? We need to implement
  23671. 15:14:52our uh this uh controller. So let's go
  23672. 15:14:54here and then I'm just going to copy
  23673. 15:14:57this
  23674. 15:14:58paste it here and then I'm do export as
  23675. 15:15:01sync function and this will be my
  23676. 15:15:04publish notification
  23677. 15:15:08controller.
  23678. 15:15:09Okay. So here we're going to get the
  23679. 15:15:12request which will be request response.
  23680. 15:15:15This will be response and we also need
  23681. 15:15:17the next which will be your next
  23682. 15:15:22function.
  23683. 15:15:24So now here what we have to do first. So
  23684. 15:15:26we'll do try and this is the place where
  23685. 15:15:30you have to publish it.
  23686. 15:15:33So I'm just going to take the error and
  23687. 15:15:35then we're going to pass in next with
  23688. 15:15:38error. So in the try what are the things
  23689. 15:15:41you have to do? First you're going to
  23690. 15:15:43get the title and body. Let uh title and
  23691. 15:15:45message. Let's say you have a UI and
  23692. 15:15:47from the UI you're going to receive it.
  23693. 15:15:48Correct? So you're going to get what?
  23694. 15:15:50Title and message. these two things from
  23695. 15:15:53request dot body this title dot request
  23696. 15:15:57see title dot message we are having in
  23697. 15:15:59the payload and then if you do some some
  23698. 15:16:02uh check here that if title is not
  23699. 15:16:03missing I'm not doing all of this now so
  23700. 15:16:05here I'm simply going to do now create
  23701. 15:16:07the notification and this notification
  23702. 15:16:10will be id so this will be your date dot
  23703. 15:16:12now
  23704. 15:16:14dot to string
  23705. 15:16:17then you're going to pass the title
  23706. 15:16:18you're going to pass the message and
  23707. 15:16:21also So created at we have to also pass
  23708. 15:16:23the created at this one and this will be
  23709. 15:16:27new date
  23710. 15:16:30to ISO string. So this will be your
  23711. 15:16:33notification and this is the place now
  23712. 15:16:34we are going to publish. So this is
  23713. 15:16:37where publisher is going to publish is
  23714. 15:16:39going to publish. So do have it. So we
  23715. 15:16:44do publisher notification. Sorry publish
  23716. 15:16:46notification. I think we have to do
  23717. 15:16:48publish notification. We have to export
  23718. 15:16:50it. So we'll do export publish
  23719. 15:16:53notification. So this publish
  23720. 15:16:56notification
  23721. 15:16:58is going to
  23722. 15:17:00get the notification. This is the
  23723. 15:17:01notification you're getting the flow. So
  23724. 15:17:04this one you're going to let's say you
  23725. 15:17:05have a UI you're going to get from UI.
  23726. 15:17:06You are publishing the notification.
  23727. 15:17:08This is going to [snorts] do what? It's
  23728. 15:17:09going to publish. See we are using
  23729. 15:17:11latest client to publish this one
  23730. 15:17:14here. Right? This part this this one.
  23731. 15:17:18and then subscriber is going to
  23732. 15:17:19subscribe. So now I think this is done
  23733. 15:17:22and then at the end what you can do you
  23734. 15:17:23can simply just do array dot status of
  23735. 15:17:25201
  23736. 15:17:28dot JSON and you're going to do success
  23737. 15:17:33will be true
  23738. 15:17:35message
  23739. 15:17:37I'm going to do notification
  23740. 15:17:40published
  23741. 15:17:42successfully
  23742. 15:17:45and you can also pass the data. So let's
  23743. 15:17:47say id will be notification do id and
  23744. 15:17:51this is done. Okay. So this is what I
  23745. 15:17:53need. So now what we can do right. So
  23746. 15:17:56let's go to routes.
  23747. 15:17:59So let's copy this. Let's copy this
  23748. 15:18:02part. What notification routes and this
  23749. 15:18:05we don't need. And this will be my
  23750. 15:18:09notification controller. And I'm just
  23751. 15:18:12going to do this one as notification
  23752. 15:18:14controller.
  23753. 15:18:17Now I'm simply going to do router dot
  23754. 15:18:20post
  23755. 15:18:22and this will be notification controller
  23756. 15:18:24dotpublish notification and you're just
  23757. 15:18:27going to do export default and this will
  23758. 15:18:29be your router and this is done but this
  23759. 15:18:32is not yet completed why in your root
  23760. 15:18:34you have to use this. So go here and
  23761. 15:18:37then simply copy this
  23762. 15:18:40and this will be your notifications
  23763. 15:18:45right and this view notification
  23764. 15:18:49what we are using here
  23765. 15:18:53okay let's import this I think we have
  23766. 15:18:54to import
  23767. 15:18:57so these will be your / notification
  23768. 15:19:00routes
  23769. 15:19:04notification and loads.
  23770. 15:19:08That's it. So I think this is done. So
  23771. 15:19:11now how we are going to check it? First
  23772. 15:19:12let's see if we are getting any error or
  23773. 15:19:14not. I think this is working right. It's
  23774. 15:19:16working. So first let me just copy that.
  23775. 15:19:20I'm just going to copy this
  23776. 15:19:23and I'll go to my package.json
  23777. 15:19:28and then I'll just add this here. So
  23778. 15:19:30we'll do uh source/subscribers
  23779. 15:19:34right this is what you're using
  23780. 15:19:37subscribers notification dot subscriber
  23781. 15:19:40ts so this is one and now what I can do
  23782. 15:19:44right so let's run it here so I'm just
  23783. 15:19:46going to do npm run subscriber
  23784. 15:19:54okay so now this is running so first
  23785. 15:19:56what I going to do right so let's copy
  23786. 15:19:58that curl here. So, I'm just going to
  23787. 15:20:00copy this.
  23788. 15:20:02Go to Postman and I'm going to do
  23789. 15:20:05import. Let's copy and then I'm just
  23790. 15:20:08going to run it on my radius full quotes
  23791. 15:20:13and then let's send it here. We're
  23792. 15:20:16getting four. Okay, what we are using?
  23793. 15:20:18Let's see.
  23794. 15:20:20Our API will not be there.
  23795. 15:20:24So, notification published successfully.
  23796. 15:20:26And you can see what you are getting.
  23797. 15:20:28New notification received. Fantastic.
  23798. 15:20:30Right here. So this title order created
  23799. 15:20:33message. This has been created
  23800. 15:20:34successfully. So from where you are
  23801. 15:20:36getting this one.
  23802. 15:20:38So if you go here right new notification
  23803. 15:20:40received title message and created at.
  23804. 15:20:43Okay. So that means once you're
  23805. 15:20:45publishing subscriber is able to
  23806. 15:20:47subscribe that one and it's going to
  23807. 15:20:50actually getting the data from publisher
  23808. 15:20:51via this notification channel. So this
  23809. 15:20:53is how we have to practically implement
  23810. 15:20:55this logic. Okay. This is very very
  23811. 15:20:57simple way I've implemented and if you
  23812. 15:20:59just do this process multiple times you
  23813. 15:21:01see that how much easier it will be for
  23814. 15:21:03you to implement all of these. Okay. So
  23815. 15:21:05with this I think we have completed this
  23816. 15:21:07particular video. So let me know what
  23817. 15:21:09you think. Uh as I already mentioned in
  23818. 15:21:11the intro if I'm getting good response
  23819. 15:21:13as I already mentioned I'm not going to
  23820. 15:21:15do see part two. If I get good response
  23821. 15:21:17from this particular video I'm going to
  23822. 15:21:19create advance part is very very simple.
  23823. 15:21:22If this video let's say getting 5,000 or
  23824. 15:21:2410,000 views, there is no point of
  23825. 15:21:26spending this much effort of creating a
  23826. 15:21:28part two, right? Uh so with that that's
  23827. 15:21:30all for this particular video. I will
  23828. 15:21:33see you in my next video. Till then good
  23829. 15:21:34luck and peace. All right, I hope that
  23830. 15:21:37you have completed the previous section
  23831. 15:21:39uh which was on radics. So with this now
  23832. 15:21:41let's start. So this will be very
  23833. 15:21:43important one. Okay. And in this one
  23834. 15:21:44you're going to actually understand some
  23835. 15:21:45of the very important concept that you
  23836. 15:21:47are going to use in lot of projects. So
  23837. 15:21:49because file upload, image upload and
  23838. 15:21:51all of these right you are actually
  23839. 15:21:52going to use and these are very very
  23840. 15:21:55practical. So what you're going to do
  23841. 15:21:56right consider you are admin right. So
  23842. 15:21:59what we are going to do right so admin
  23843. 15:22:01will be upload some images right now in
  23844. 15:22:03this one we will actually take the
  23845. 15:22:05functionality simple but we are going to
  23846. 15:22:06understand the concept uh better way.
  23847. 15:22:09Concept means the flow. So admin will be
  23848. 15:22:11able to upload images and then admin
  23849. 15:22:14will be able to see all your images.
  23850. 15:22:16Correct? So admin will be create images
  23851. 15:22:18and admin will be able to fetch all your
  23852. 15:22:20images. So these two routes we have and
  23853. 15:22:22then we have something called delete.
  23854. 15:22:24But how this is going to work? Consider
  23855. 15:22:26this is your application right and see
  23856. 15:22:29how easier I will explain this one. So
  23857. 15:22:31this is your application and this is
  23858. 15:22:32your DB. Okay. So this is let's say your
  23859. 15:22:35DB. What we are going to see till this
  23860. 15:22:38time whatever we have done this CR what
  23861. 15:22:40we used to do. So we usually do some
  23862. 15:22:42functionality. we interact with our DB
  23863. 15:22:45and then we directly uh store this
  23864. 15:22:47particular information in our DB. But
  23865. 15:22:49when we work with assets, it doesn't
  23866. 15:22:51work like that. Instead of directly
  23867. 15:22:54storing the image in your uh DB, what
  23868. 15:22:57you're going to do right, we not going
  23869. 15:22:59to do that. So we have something called
  23870. 15:23:00cloud storage CS. I'm just writing this
  23871. 15:23:04cloud storage can be anything. See in
  23872. 15:23:05our case we're using cloud unit because
  23873. 15:23:07it easier and also you can use AWS
  23874. 15:23:10S3 or you can use cloudfare lot of other
  23875. 15:23:13things. So at the end the concept will
  23876. 15:23:15be same. So you have something called a
  23877. 15:23:17storage. What you're going to do right
  23878. 15:23:19you are actually going to store your
  23879. 15:23:21images in your uh storage. So it will be
  23880. 15:23:24like this. So this let me select this
  23881. 15:23:26one. So from your application it is
  23882. 15:23:29going to interact with your storage.
  23883. 15:23:31Storage is going to do what? It's
  23884. 15:23:32actually going to store all your images.
  23885. 15:23:34So storage is going to keep all your
  23886. 15:23:36images and then what storage is going to
  23887. 15:23:39do right storage is going to give you
  23888. 15:23:40some information that we are going to
  23889. 15:23:42store in our DB. So what will be that
  23890. 15:23:43information? That information will be
  23891. 15:23:45our image URL because see at the end you
  23892. 15:23:48need to keep a track of your assets.
  23893. 15:23:50Right? So now what will be uh the
  23894. 15:23:53responsibility of your DB?
  23895. 15:23:54Responsibility of your DB will be to
  23896. 15:23:56hold the information that is provided by
  23897. 15:23:58the cloud storage. So that means once we
  23898. 15:24:01store the image to our cloud storage
  23899. 15:24:03cloud storage is going to give you two
  23900. 15:24:04things. So first one let's say it's
  23901. 15:24:06going to give a URL and this will be
  23902. 15:24:08your actual image URL that we are going
  23903. 15:24:09to store. So this will be your image
  23904. 15:24:11URL. Okay. Second it's going to give you
  23905. 15:24:14a unique ID. Now this unique ID can
  23906. 15:24:16differ. For example in cloudary we call
  23907. 15:24:18that one as cloudinary public ID. In S
  23908. 15:24:20S3 we call something else. In other uh
  23909. 15:24:23storage we call something else. That
  23910. 15:24:24doesn't matter. But at the end it's
  23911. 15:24:26going to give you a unique ID. And this
  23912. 15:24:28unique ID will be attached to this
  23913. 15:24:30particular asset. So that means how why
  23914. 15:24:32actually you need this unique ID and you
  23915. 15:24:34are going to understand this one in a
  23916. 15:24:36minute. So that means this cloud storage
  23917. 15:24:38is going to give you two information.
  23918. 15:24:39First one will be your image URL and
  23919. 15:24:41second one will be one unique ID that is
  23920. 15:24:43related to this particular asset. And
  23921. 15:24:46then what we will do we are going to
  23922. 15:24:48store this URL and this ID to our DB. So
  23923. 15:24:51that means what we are doing that we are
  23924. 15:24:52not directly storing the image or videos
  23925. 15:24:54in our DB. We are storing the image and
  23926. 15:24:56videos in our cloud storage. Cloud
  23927. 15:24:58storage is going to send that
  23928. 15:25:00information and that information we're
  23929. 15:25:01going to store in our DB. Now why
  23930. 15:25:04actually we need this unique ID.
  23931. 15:25:06Consider this is your application and
  23932. 15:25:09this is your DB. In your DB you have 10
  23933. 15:25:11storage uh saved. So you have 10 storage
  23934. 15:25:15and this is let's say your cloud
  23935. 15:25:16storage. Right? Now what actually these
  23936. 15:25:1910 asset is going to consist? So each
  23937. 15:25:21and every asset is going to have your
  23938. 15:25:23URL and it's going to have your ID.
  23939. 15:25:25Right?
  23940. 15:25:27Now let's say what we have to do right
  23941. 15:25:28from this side admin needs to delete one
  23942. 15:25:31particular asset. So when we are going
  23943. 15:25:33to delete the asset we can't just
  23944. 15:25:35directly delete that particular asset
  23945. 15:25:36from our DB. Why? Because at the same
  23946. 15:25:39time we need to delete this particular
  23947. 15:25:41uh asset from our cloud also. But in
  23948. 15:25:44that case how you're going to understand
  23949. 15:25:45okay this particular image URL actually
  23950. 15:25:48stored in which part in which image or
  23951. 15:25:50basically what I'm trying to say. So
  23952. 15:25:52let's say let's uh understand in a
  23953. 15:25:54simpler way. Let's say you have 10
  23954. 15:25:55assets uh stored. Correct. So each and
  23955. 15:25:57every asset is going to have your URL
  23956. 15:25:59and the ID. So you have 10 assets URL 10
  23957. 15:26:03assets ID. Okay. When we are going to
  23958. 15:26:06delete this particular asset from our DB
  23959. 15:26:08which you can directly do this one.
  23960. 15:26:09Let's say you directly deleted this one
  23961. 15:26:10from your DB. So from DB it is not
  23962. 15:26:13present. But see the problem is in your
  23963. 15:26:15cloudinary storage or in your cloud
  23964. 15:26:17storage that asset is still present. So
  23965. 15:26:19now to under identify that okay this
  23966. 15:26:21particular asset is related to this
  23967. 15:26:23particular asset in our DB. We need this
  23968. 15:26:26ID. So we're getting so using this
  23969. 15:26:28particular ID now what we going to do
  23970. 15:26:29right? Two things we going to do. We are
  23971. 15:26:31going to delete this particular asset
  23972. 15:26:32from our DB. At the same time using this
  23973. 15:26:36ID we are going to delete this
  23974. 15:26:37particular asset from our storage also.
  23975. 15:26:39So that means when we're going to
  23976. 15:26:40delete, we are going to delete from two
  23977. 15:26:41places. We are going to delete from our
  23978. 15:26:43DB at the same time we are going to
  23979. 15:26:45delete from our cloud storage and to
  23980. 15:26:46delete from the cloud storage we need
  23981. 15:26:48this particular unique ID. So this will
  23982. 15:26:51be the order flow and radius one you
  23983. 15:26:53will understand once you implement
  23984. 15:26:54because I hope you have complete again
  23985. 15:26:55I'm repeating okay you have to complete
  23986. 15:26:57the previous video that is mandatory
  23987. 15:26:59then only you can understand because I'm
  23988. 15:27:00not going to cover radius concept in
  23989. 15:27:02this one. This will be completely
  23990. 15:27:03practical. So first this will be the
  23991. 15:27:05overall flow. First what we're going to
  23992. 15:27:07do right to go to cloudinary.com
  23993. 15:27:10and then just sign up for free. Okay. So
  23994. 15:27:12I'm just going to sign up using Google.
  23995. 15:27:14I'll select this account and you'll see
  23996. 15:27:15that I already have so many assets uh
  23997. 15:27:18already uploaded here because I usually
  23998. 15:27:20use this one from my YouTube uh videos.
  23999. 15:27:22One thing I wanted to mention please do
  24000. 15:27:24not use my credences because I've seen
  24001. 15:27:25this one in the past. Some of you
  24002. 15:27:27actually used which is not correct.
  24003. 15:27:28Okay. So please create free account and
  24004. 15:27:31then use. So you see that for example
  24005. 15:27:33this one. I haven't even uploaded all of
  24006. 15:27:35this right. I don't know from where it
  24007. 15:27:36is all these are coming. So don't do
  24008. 15:27:39[snorts] not use this. What you're going
  24009. 15:27:40to do, right? You have to go to go to
  24010. 15:27:43home
  24011. 15:27:46and you'll see that you have this API
  24012. 15:27:47key, right? So you have to go to this
  24013. 15:27:49API key and you're going to get three
  24014. 15:27:50things. Okay, the first one will be your
  24015. 15:27:52cloud name. So this will be your cloud
  24016. 15:27:54name. So if you go back, I wanted to
  24017. 15:27:56show this. This is important because you
  24018. 15:27:58need the cloud name. Okay, so this is
  24019. 15:28:00your cloud name. Then you go to API
  24020. 15:28:02keys. You need the API key and the API
  24021. 15:28:05secret. So three things we need. Okay,
  24022. 15:28:07first of all, let's do one thing. Let's
  24023. 15:28:09go to our application and let's do one
  24024. 15:28:10by one. Go to event uh sorry env.
  24025. 15:28:14And I'm just going to create cloudinary
  24026. 15:28:17cloud name
  24027. 15:28:20cloudinary
  24028. 15:28:22cloudinary
  24029. 15:28:24uh what is that? Cloud uh no cloudinary
  24030. 15:28:28API key.
  24031. 15:28:30And then I'm just going to do cloudinary
  24032. 15:28:33API secret. So these three things we
  24033. 15:28:35need. So now let's go here. So I'm just
  24034. 15:28:38going to copy. So this is my cloud name.
  24035. 15:28:40Okay, this will be your cloud name. You
  24036. 15:28:42can also check it. So here you can see
  24037. 15:28:44this is cloud name. So just paste it.
  24038. 15:28:46Copy the API key or I'll copy from here.
  24039. 15:28:49Paste it. And then this will be the API
  24040. 15:28:51secret. So I'm going to use this. So
  24041. 15:28:53I'll copy and then I'm going to paste it
  24042. 15:28:56here. Okay. So now what I'm going to do,
  24043. 15:28:58right? So this will be the prerequisite.
  24044. 15:29:01Now what I'll do, right? So let's close
  24045. 15:29:03this and then I will give you one
  24046. 15:29:05homework and this homework will be uh
  24047. 15:29:08you have to do this go to your env
  24048. 15:29:11here okay cloudinary API key cloudinary
  24049. 15:29:14secret and then try to use using env dot
  24050. 15:29:16this one I'm intentionally not doing
  24051. 15:29:18this one so this one will be for your
  24052. 15:29:20practice so add these three here and
  24053. 15:29:22then we you are going to use this one
  24054. 15:29:23using envi
  24055. 15:29:28cloudname okay so this will be the first
  24056. 15:29:30thing second what you're going to do
  24057. 15:29:32right so Let's go here and we need a
  24058. 15:29:35cloudinary package. So I do npm install
  24059. 15:29:38cloudinary and also we need malter.
  24060. 15:29:41Okay. So molter is a package that we
  24061. 15:29:44need for our uh asset related uh work
  24062. 15:29:46whenever we are going to do. So let me
  24063. 15:29:48just quickly so this is very slow man.
  24064. 15:29:51So I'll do molter in express node
  24065. 15:29:54project.
  24066. 15:29:57R this will be okay. So this is a
  24067. 15:30:00middleware that handles multipart form
  24068. 15:30:02data right it usually used for uploading
  24069. 15:30:04files images PDF videos and all these
  24070. 15:30:06things right so when we are going to use
  24071. 15:30:08this so first of all we need to install
  24072. 15:30:10the malter and also you need the multter
  24073. 15:30:11type so do npm install- d types malter
  24074. 15:30:14so copy this also and then we going to
  24075. 15:30:17install this also
  24076. 15:30:19all right so this is done next what we
  24077. 15:30:22need we also going to need our IO so I'm
  24078. 15:30:25just going to do IO radius.
  24079. 15:30:29Okay. And also we need B MQ. So this
  24080. 15:30:32will be for our background jobs that
  24081. 15:30:34we're going to do later. But let's
  24082. 15:30:35install this one at once. So we're going
  24083. 15:30:37to do this one.
  24084. 15:30:41Okay. So this is done. All right. So now
  24085. 15:30:44what we're going to do right, let's do
  24086. 15:30:46uh uh let's do this particular section,
  24087. 15:30:48right? Uh slowly. Before doing this one,
  24088. 15:30:52let's go to our docker compose. And we
  24089. 15:30:55actually need to add some more uh one
  24090. 15:30:57more service here. So what I'm going to
  24091. 15:30:59do right, let's go here. Enter. And I'll
  24092. 15:31:04add one more. So I'll do radius.
  24093. 15:31:07And here we are going to do this one
  24094. 15:31:09will be image.
  24095. 15:31:11So image will be radius colon 7. And let
  24096. 15:31:14me check the name alpine.
  24097. 15:31:18Okay. Then we need the container name.
  24098. 15:31:20So I'm going to do container name.
  24099. 15:31:22Container name. Let's give this one as
  24100. 15:31:24what I'll do right uh node js capstone
  24101. 15:31:28project radius.
  24102. 15:31:33Okay. And then I'll do restart
  24103. 15:31:36unless stopped
  24104. 15:31:40and also then we're going to do ports,
  24105. 15:31:42right? So we need to do ports and this
  24106. 15:31:44will be let's see. So I'm just going to
  24107. 15:31:46use a different port. I'll do 6385
  24108. 15:31:49and this is going to run on okay this
  24109. 15:31:51will be in this. So do 63 85 and this
  24110. 15:31:54will be 6379. This is the port usually
  24111. 15:31:57radius runs. Save this. Now what I'm
  24112. 15:32:00going to do right, we have to go to our
  24113. 15:32:01env. And then I'm just going to add one
  24114. 15:32:04more here and this will be my radius url
  24115. 15:32:08and this will be radius and then where
  24116. 15:32:11it is going to run. So it will run
  24117. 15:32:13localhost colon. So you're using what?
  24118. 15:32:156385 right? So 6385.
  24119. 15:32:20That's it. So now what I'm going to do
  24120. 15:32:22right, we'll go here and I'm just going
  24121. 15:32:25to do uh I think in the package dojson
  24122. 15:32:28we have doc npm or or let's directly do
  24123. 15:32:30it. So do docker compose down
  24124. 15:32:34and I'll do docker compose upd
  24125. 15:32:40started. So let me just verify once.
  24126. 15:32:42Okay. So I'll do okay. So you have
  24127. 15:32:44radius this one one will be radius and
  24128. 15:32:47one is postress. Fine. So I think this
  24129. 15:32:49is done. So now what will be the next
  24130. 15:32:51step? So next step first let's start
  24131. 15:32:53with creating our cloudinary
  24132. 15:32:56service right. So cloudinary helper we
  24133. 15:32:58need to create and to create this one
  24134. 15:32:59let's uh keep this one in leave folder.
  24135. 15:33:02So I'll do leave and let's create
  24136. 15:33:04cloudinary
  24137. 15:33:07ts. Okay.
  24138. 15:33:09All right. So first of all let's close
  24139. 15:33:11everything. And this is where you are
  24140. 15:33:13going to understand each and everything
  24141. 15:33:15step by step. Okay. Because this is
  24142. 15:33:17actually very important. So now first of
  24143. 15:33:19all I'm just going to import cloudinary
  24144. 15:33:25cloudinary
  24145. 15:33:27from cloudinary
  24146. 15:33:30and here I'm just going to take a upload
  24147. 15:33:32result means the result that we are
  24148. 15:33:34going to receive and this is going to
  24149. 15:33:36give you two things right so one will be
  24150. 15:33:38your url so this will be secure url
  24151. 15:33:40which will be string and second one will
  24152. 15:33:42be your public ID and this will be your
  24153. 15:33:44cloudinary public ID and this also will
  24154. 15:33:46be string so first will be so we are
  24155. 15:33:49going to create export as sync function
  24156. 15:33:52and I'm simply going to do upload banner
  24157. 15:33:57image to cloudinary
  24158. 15:34:01okay so now let's see how we going to do
  24159. 15:34:04this so first of all what actually we
  24160. 15:34:07need here now remember so we have
  24161. 15:34:09covered one code module that is called
  24162. 15:34:11buffer you remember so here what we
  24163. 15:34:12going to use right we are going to use
  24164. 15:34:14our file buffer okay so whenever we
  24165. 15:34:16going to upload the image which we are
  24166. 15:34:17going to receive a buffer and that
  24167. 15:34:19buffer we are going to handle. So first
  24168. 15:34:21of all as input we are going to receive
  24169. 15:34:22a buffer. So buffer type will be buffer
  24170. 15:34:26and then I'm also going to receive one
  24171. 15:34:28more options and this is optional.
  24172. 15:34:29Option means let's say if I go to my
  24173. 15:34:31cloudinary and let's say uh I'll go to
  24174. 15:34:33my assets and I wanted to create this
  24175. 15:34:35particular asset inside a folder. Okay.
  24176. 15:34:38And that folder name you can give here.
  24177. 15:34:39I don't know why this is slow man.
  24178. 15:34:46So you'll see that you have this folder
  24179. 15:34:47here, right? So you can create a couple
  24180. 15:34:49of folders. See, we have multiple
  24181. 15:34:51folders that I have created. Correct? So
  24182. 15:34:53now what I can do, right? So I can pass
  24183. 15:34:55options and this option is going to
  24184. 15:34:57receive one folder that in which folder
  24185. 15:34:59I wanted to store this and let's keep
  24186. 15:35:01this one optional. Okay. So this is the
  24187. 15:35:04first thing what this is going to
  24188. 15:35:05return. So this is going to return a
  24189. 15:35:06promise and this is going to be my
  24190. 15:35:08upload result. So I need the data in
  24191. 15:35:10this particular format. So I need a URL.
  24192. 15:35:12I need a public ID and this what we have
  24193. 15:35:14already covered here, right? that why we
  24194. 15:35:16need the URL and why we need the public
  24195. 15:35:18ID. Public ID is actually very important
  24196. 15:35:19because at the end this public ID is
  24197. 15:35:22going to help us to identify which asset
  24198. 15:35:24and then we can delete that particular
  24199. 15:35:26asset from our cloudinary storage or
  24200. 15:35:28whatever storage you are using doesn't
  24201. 15:35:30matter. So here now I'm just going to
  24202. 15:35:31take three things. So I'm just going to
  24203. 15:35:33do cloud name
  24204. 15:35:35and this will be my process. Again I
  24205. 15:35:38already mentioned I hope that you have
  24206. 15:35:39done this one. So keep this one in your
  24207. 15:35:41and you can use dot like that. Okay, for
  24208. 15:35:43now I'm just going to keep it like this.
  24209. 15:35:45So we'll do env and then I'll do
  24210. 15:35:47cloudinary cloud name. Then I'm just
  24211. 15:35:49going to take con. Let's do API key. So
  24212. 15:35:52this will be process
  24213. 15:35:55process env dot. So you have cloudinary
  24214. 15:35:58API key
  24215. 15:36:00and I'll do con and this will be API
  24216. 15:36:03secret and this will be process dot env
  24217. 15:36:06dot cloudinary API secret. So three
  24218. 15:36:09things we need. So now first of all I'm
  24219. 15:36:11going to give a check that if not equal
  24220. 15:36:13to cloud name or if not equals to API
  24221. 15:36:17key or if not equals to API secret if
  24222. 15:36:21these are not present. So in that case
  24223. 15:36:22I'm going to throw a new app error. So
  24224. 15:36:25I'm going to give a 500 error and I'll
  24225. 15:36:27do cloudinary is not configured
  24226. 15:36:28properly. So cloudinary is not
  24227. 15:36:31configured properly. So this is the
  24228. 15:36:34first thing and now what you can do
  24229. 15:36:36right you can also I think you can
  24230. 15:36:37import like this. You can also use a
  24231. 15:36:39version two but for now it's fine. You
  24232. 15:36:41can import like this and then what I'll
  24233. 15:36:43do right. So now this is where we are
  24234. 15:36:45going to do what we are going to
  24235. 15:36:46configure our cloudinary because see you
  24236. 15:36:48have to configure or else cloudinary
  24237. 15:36:50will not understand what is your cloud
  24238. 15:36:52name what is your API secret you're
  24239. 15:36:55getting. So that is the reason we need
  24240. 15:36:56this one or else maybe let's do this. I
  24241. 15:36:58think better will be we can import
  24242. 15:36:59version two and let's do this one as
  24243. 15:37:01cloudinary. Okay I think this is better.
  24244. 15:37:03And now I can configure this one. So how
  24245. 15:37:05I'm going to configure? So do configure
  24246. 15:37:07cloudinary. So I do cloudinary and if
  24247. 15:37:09you do dot you'll see that you're going
  24248. 15:37:11to get this config
  24249. 15:37:13or sorry why cloud name I need
  24250. 15:37:15cloudinary. So cloudinary doconfig and
  24251. 15:37:17this is where you're going to pass all
  24252. 15:37:19the three things. So you pass the cloud
  24253. 15:37:21name. So this will be your cloud name.
  24254. 15:37:23You have the API key and this will be
  24255. 15:37:24your API key and then you have API
  24256. 15:37:26secret and this will be your API secret.
  24257. 15:37:30Okay. So these three things cloud cloud
  24258. 15:37:32name API key and API secret. So now my
  24259. 15:37:35cloud ML is configured. So what I need
  24260. 15:37:37to do here see before because we are
  24261. 15:37:39working with buffer you remember that
  24262. 15:37:41section that how we have implemented our
  24263. 15:37:44stream you remember readable stream
  24264. 15:37:46writable stream transform stream all
  24265. 15:37:47these things right. So what we need to
  24266. 15:37:50do right whenever we are going to upload
  24267. 15:37:51our image we are receiving a buffer and
  24268. 15:37:54what we are going to send to cloudinary
  24269. 15:37:56we are going to send our raw image bytes
  24270. 15:37:58raw image bytes to cloudinary. Okay. So
  24271. 15:38:01how we are going to do that? Because
  24272. 15:38:02it's returning promise. So we need to
  24273. 15:38:05return promise here. So I'm just going
  24274. 15:38:06to do new promise. And promise is going
  24275. 15:38:08to give you what? So it's going to give
  24276. 15:38:10resolve and reject. Resolve and reject.
  24277. 15:38:13So now first of all, so I do const. So
  24278. 15:38:16we need a stream here. So we have upload
  24279. 15:38:18stream. Now I'm just simply going to do
  24280. 15:38:19cloudinary dot version. What I'm doing
  24281. 15:38:23cloudinary dot not version
  24282. 15:38:25cloudinary.uploader dot upload. And then
  24283. 15:38:29you're going to get this upload stream.
  24284. 15:38:31So we are uploading a stream. Now here
  24285. 15:38:33what you can do right you can pass
  24286. 15:38:34option and there is a call back. Now you
  24287. 15:38:37see each and every module that we have
  24288. 15:38:39done so far. If you have that much
  24289. 15:38:40patience and completed all the section
  24290. 15:38:42previously all these things would be way
  24291. 15:38:45easier because now remember we have
  24292. 15:38:46discussed one particular thing that what
  24293. 15:38:47is our call back what is the call back
  24294. 15:38:50uh format in classic nodej. So call back
  24295. 15:38:53is going to give you error and it's
  24296. 15:38:55going to give you a result. And now see
  24297. 15:38:56this particular concept we are going to
  24298. 15:38:58apply here. So what I mean by this if
  24299. 15:39:00you learn those individual concepts or
  24300. 15:39:02code modules properly you can combine
  24301. 15:39:04all of this and you can create or use
  24302. 15:39:06those concepts in a project. Okay. So
  24303. 15:39:10now what I can do right I can simply
  24304. 15:39:11give a resource type. So resource type
  24305. 15:39:13will be I'm uploading a image here. So
  24306. 15:39:15if you if you uploading image video and
  24307. 15:39:17everything you can do auto or let's say
  24308. 15:39:19you can if you're uploading raw give
  24309. 15:39:21raw. If you're uploading video you can
  24310. 15:39:22give video. We are uploading image. And
  24311. 15:39:24then I'm just going to do a folder and
  24312. 15:39:26folder will be my options dotfolder.
  24313. 15:39:28This options dot ffolder I'm receiving
  24314. 15:39:29from this option. So this is the first
  24315. 15:39:31thing. Now what is this is going to
  24316. 15:39:33give? So this is going to give you a
  24317. 15:39:34call back. Okay. So this call back is
  24318. 15:39:37now going to give you error and result.
  24319. 15:39:39Okay. So this is the classic call back
  24320. 15:39:42uh format. Now what [snorts] I have to
  24321. 15:39:44do? So if error means what this also I
  24322. 15:39:46have covered. You can't say that I
  24323. 15:39:48haven't covered everything I have
  24324. 15:39:49covered. So it will reject your reject
  24325. 15:39:52the error and you're going to return if
  24326. 15:39:54there is no error. So in that case I'm
  24327. 15:39:56going to resolve. So what I'm going to
  24328. 15:39:58resolve so I'm going to resolve my SQL
  24329. 15:40:00URL and this URL you're going to get
  24330. 15:40:02from a result and see that if I do
  24331. 15:40:04result dot you're going to get
  24332. 15:40:05everything. So you're going to get see
  24333. 15:40:06bytes colors uh context created created
  24334. 15:40:10at and here you're going to get public
  24335. 15:40:13ID see and you have security URL. So I
  24336. 15:40:15need this one and I need the public ID.
  24337. 15:40:18So I'll do result
  24338. 15:40:21dot public id.
  24339. 15:40:23So these two things I need and now what
  24340. 15:40:26I'm going to do right. So let me see
  24341. 15:40:28where it is closing here. And this is
  24342. 15:40:30where I need to do upload stream and I'm
  24343. 15:40:33going to end the upload stream. So look
  24344. 15:40:34dot end. And what you have to pass you
  24345. 15:40:36have to pass your buffer here. Buffer is
  24346. 15:40:38nothing but we are sending a raw image
  24347. 15:40:40bytes to the cloud. So we're going to
  24348. 15:40:42pass the buffer and this buffer we going
  24349. 15:40:44to receive. Okay this one. So now we are
  24350. 15:40:47sending
  24351. 15:40:49uh here so we'll do sending
  24352. 15:40:53raw buffer
  24353. 15:40:55not raw buffer sending raw image sending
  24354. 15:40:59raw image bytes to pluginary okay
  24355. 15:41:03and then I think this is done we don't
  24356. 15:41:05have to do anything uh okay let's see so
  24357. 15:41:08we are taking this one checking
  24358. 15:41:09configure returning a promise okay I
  24359. 15:41:12think this is completed okay so this is
  24360. 15:41:14the first thing so first helper function
  24361. 15:41:16that we need. So now what we're going to
  24362. 15:41:18do right so first of all we are going to
  24363. 15:41:21create our routes then we're going to
  24364. 15:41:23create our services create our
  24365. 15:41:25repository we also going to create our
  24366. 15:41:27multter middleware remember I mentioned
  24367. 15:41:29that malter is a middleware that is
  24368. 15:41:30going to help us to handle our multiart
  24369. 15:41:32form data so we're going to create our
  24370. 15:41:34multter middleware and then we're going
  24371. 15:41:35to combine everything and then create
  24372. 15:41:37the upload logic so let's see how we
  24373. 15:41:39going to do that in the next part okay
  24374. 15:41:40so now let's create all the files so
  24375. 15:41:42first I'll go here and let's do admin
  24376. 15:41:45dotbanners
  24377. 15:41:46admin admin.banner.outes.ts.
  24378. 15:41:49So this is the first one. Then go to
  24379. 15:41:51repository. So I'll do admin dotbanner
  24380. 15:41:54dot repository.ts
  24381. 15:41:57and I'll do admin
  24382. 15:42:00dotbanner
  24383. 15:42:02dotservices.ts.
  24384. 15:42:05So first we need to work with these
  24385. 15:42:06three right. So let's start with our
  24386. 15:42:08admin
  24387. 15:42:10routes. So first as usual we are going
  24388. 15:42:12to import something from express.
  24389. 15:42:15And here we are simply going to do
  24390. 15:42:19what name I'll give. So let's give
  24391. 15:42:20simply export
  24392. 15:42:22con and I'll do admin banner route. We
  24393. 15:42:27need to import router.
  24394. 15:42:30So here uh what I'm going to do right
  24395. 15:42:32let's give router. So I'll do router and
  24396. 15:42:34now first of all let's start with admin
  24397. 15:42:36banner router. Okay let's uh do this
  24398. 15:42:39also. to go to index and I'll do api
  24399. 15:42:42router dot
  24400. 15:42:45use / admin/banner
  24401. 15:42:49banners
  24402. 15:42:51uh and then we're going to do admin
  24403. 15:42:53banner router save this for now so now
  24404. 15:42:56what I'll do so I'll do admin router dot
  24405. 15:42:58use and this I think I don't have to uh
  24406. 15:43:01explain now because now we know that we
  24407. 15:43:03need authenticate and require admin both
  24408. 15:43:06right because this is for admin so now
  24409. 15:43:08let's start with first answer admin
  24410. 15:43:10route dot post
  24411. 15:43:13and here what I'm going to do right now
  24412. 15:43:15this is very important so I'll explain
  24413. 15:43:16so first I'll do request response
  24414. 15:43:21next and here first let's take a try
  24415. 15:43:25catch
  24416. 15:43:30so pass the error let's give error like
  24417. 15:43:33this
  24418. 15:43:34and here first let's create our banner
  24419. 15:43:37so do banner this will be have it I'll
  24420. 15:43:39do create admin banner service
  24421. 15:43:45and here what you have to pass so first
  24422. 15:43:48before doing this one also right so
  24423. 15:43:49let's return this as usual we have done
  24424. 15:43:52so now remember again here what what
  24425. 15:43:54you're doing you're creating something
  24426. 15:43:56correct so that means you have to use
  24427. 15:43:582011 so you have to make sure all small
  24428. 15:44:00small things so do success will be true
  24429. 15:44:04and data you're going to return the
  24430. 15:44:06banner okay So what will be next? So
  24431. 15:44:09next let's create our middleware because
  24432. 15:44:11as I already mentioned that we are going
  24433. 15:44:12to use malter middleware to handle our
  24434. 15:44:14multiart found data. So go to middleware
  24435. 15:44:17and let's do here. So I'm just going to
  24436. 15:44:19do banner uh dot middleware
  24437. 15:44:23dot ts. So first of all we need to do
  24438. 15:44:26something. We are going to import malter
  24439. 15:44:27from malar because we need malar. And
  24440. 15:44:30then let's take one max file size that
  24441. 15:44:34what will be your file size that you
  24442. 15:44:36will allow user to upload. So in this
  24443. 15:44:38case I'm just going to keep let's keep
  24444. 15:44:40either 5 MB or 10 MB. Let's keep 10 MB
  24445. 15:44:42only. So I'll do 1024 into 1024. So this
  24446. 15:44:45will be 10 MB. So now we are going to do
  24447. 15:44:47export const and let's do upload banner
  24448. 15:44:51image.
  24449. 15:44:53So we are going to take malt and here
  24450. 15:44:55you're going to pass couple of things.
  24451. 15:44:57So first of all what you need right? So
  24452. 15:44:58we are actually going to store our file
  24453. 15:45:00in memory because at the end you will
  24454. 15:45:03notice that we are actually not storing
  24455. 15:45:04our file in our local right we already
  24456. 15:45:06have a cloud binary cloud. So that is
  24457. 15:45:08the reason we are going to simply store
  24458. 15:45:10this particular file in memory and that
  24459. 15:45:11is more than enough. So we are going to
  24460. 15:45:14take something called storage and this
  24461. 15:45:15is going to give you this multmory
  24462. 15:45:18storage. Okay. Next what we going to do?
  24463. 15:45:20We are going to pass a limits and in
  24464. 15:45:22this limits you're going to pass a file
  24465. 15:45:24size. This file size will be this max
  24466. 15:45:26file size. Okay. And also [snorts] very
  24467. 15:45:29very important. So what will happen,
  24468. 15:45:30right? So you're going to give a file
  24469. 15:45:31filter. Now this file filter is simply
  24470. 15:45:34going to tell that okay what actually
  24471. 15:45:36what what kind of asset you are
  24472. 15:45:37uploading. So this is going to take your
  24473. 15:45:40request and also you're going to pass
  24474. 15:45:42our file. So you can see that we have
  24475. 15:45:44the file and also it is going to give
  24476. 15:45:46you a call back if I'm not wrong. Okay.
  24477. 15:45:48It is going to give you call back. Okay.
  24478. 15:45:50So request then you have the file and
  24479. 15:45:52then it's going to give you a call back.
  24480. 15:45:53So I'm just going to take call back as
  24481. 15:45:55CV. Okay. Or you can take this one as
  24482. 15:45:57call back also. So now what I going to
  24483. 15:46:00do right here? So I'm simply going to
  24484. 15:46:02check if this file not equal dot mine
  24485. 15:46:06type dot starts with if this is not
  24486. 15:46:09image. So in that case we will not allow
  24487. 15:46:11because we only want image to be
  24488. 15:46:12uploaded. So what [snorts] we going to
  24489. 15:46:14do right we're going to call the call
  24490. 15:46:15back and you already know the call back
  24491. 15:46:16pattern. See I think I already mentioned
  24492. 15:46:19this is multiple times and for this
  24493. 15:46:20reason we've actually covered our core
  24494. 15:46:22modules. So here what I have to pass
  24495. 15:46:24we're going to pass a new app error.
  24496. 15:46:25Okay. First we going to do new app
  24497. 15:46:27error. Give a 400 and I'll do only image
  24498. 15:46:31upload is allowed.
  24499. 15:46:34Okay. And then uh don't forget to
  24500. 15:46:36return. So we're going to do return. So
  24501. 15:46:38now what I have to do if this is not the
  24502. 15:46:40case, we're going to call the call back.
  24503. 15:46:41Error will be null. And what will be
  24504. 15:46:43result? Result will be true. That's it.
  24505. 15:46:46Okay. And now what I'm going to do
  24506. 15:46:48right, I'm simply going to do export con
  24507. 15:46:51because for now we are have single uh
  24508. 15:46:54image upload. So I'm just going to do
  24509. 15:46:55single banner image and this will be
  24510. 15:46:59your upload banner image and here you
  24511. 15:47:01can do single and now this is where I'm
  24512. 15:47:03going to pass a field name. Now this is
  24513. 15:47:05very important. Okay, this will be your
  24514. 15:47:07actual field name when you're going to
  24515. 15:47:09send the request. So this is where
  24516. 15:47:10you're going to pass it like this. So
  24517. 15:47:12for example, let's say I wanted to pass
  24518. 15:47:13like this image. So image will be my
  24519. 15:47:15field name and understand when we are
  24520. 15:47:16going to test it. So for now this is
  24521. 15:47:18more than enough. So this is what
  24522. 15:47:20actually we need. Now what will be next
  24523. 15:47:22step? Next step will be first of all
  24524. 15:47:23we'll go to our routes. Now this is
  24525. 15:47:26where okay this will be actually
  24526. 15:47:27important because we are using a
  24527. 15:47:28middleware right. So what you're going
  24528. 15:47:30to do before you're going to run this
  24529. 15:47:31particular thing we are going to use our
  24530. 15:47:33middleware. So this is where you're
  24531. 15:47:34going to use this upload single banner
  24532. 15:47:36image and this will be from your
  24533. 15:47:38middleware. Okay. So this is done. So
  24534. 15:47:40now what we're going to do right now we
  24535. 15:47:42will be creating our service. So service
  24536. 15:47:44what service is going to do? Service is
  24537. 15:47:46going to do a couple of things. So here
  24538. 15:47:47first of all we are going to check or
  24539. 15:47:49give some check. Let's say if file is
  24540. 15:47:51not present so it's going to return some
  24541. 15:47:53error. Then it's going to upload your
  24542. 15:47:55image to cloudinary. Once the upload is
  24543. 15:47:57completed then what we are going to do
  24544. 15:47:58then we are going to simply store that
  24545. 15:48:00URL and the public ID in our uh DB.
  24546. 15:48:04Okay. So that will be the overall flow.
  24547. 15:48:05So now let's copy this and then let's go
  24548. 15:48:07to our ad main banner service. So here
  24549. 15:48:10I'm just going to do export as sync
  24550. 15:48:12function and I'll do create admin banner
  24551. 15:48:15service. So here you need a file. What
  24552. 15:48:17will be your type? type will be your
  24553. 15:48:18express dot maltter dot file or else
  24554. 15:48:21this will be undefined. Okay, what this
  24555. 15:48:24is going to return? So this is going to
  24556. 15:48:25return a promise and here let's do one
  24557. 15:48:27thing. I think we haven't created our uh
  24558. 15:48:29banner type, right? So let's go to the
  24559. 15:48:31types and let's create one more here. So
  24560. 15:48:33I'm just going to do banner.ts.
  24561. 15:48:36So I'm simply going to do [snorts]
  24562. 15:48:37export type banner and this will be so
  24563. 15:48:40first we need the ID. We also need our
  24564. 15:48:42image URL. This will be string. We also
  24565. 15:48:45need our cloudinary public ID. I think
  24566. 15:48:47that is what we have taken here image
  24567. 15:48:49URL and cloudinary public id this also
  24568. 15:48:51will be string then you need the created
  24569. 15:48:54at
  24570. 15:48:56which will be what date and then you're
  24571. 15:48:58going to have your updated at this also
  24572. 15:49:01will be date so this will be your type
  24573. 15:49:04this is what we're going to use so now
  24574. 15:49:06let's go to our service so first what
  24575. 15:49:08this is going to return this is going to
  24576. 15:49:09return this banner type so first we have
  24577. 15:49:11to check that if this file is not
  24578. 15:49:13present so we are going to throw error
  24579. 15:49:15so I'm just going to do throw new app
  24580. 15:49:17error and here I'm going to do 400 and
  24581. 15:49:20I'll do images needed or images required
  24582. 15:49:22something like this so this is the first
  24583. 15:49:24thing next if the file buffer is not
  24584. 15:49:27present we are going to also return so
  24585. 15:49:29I'll do file buffer is not present we
  24586. 15:49:31are going to throw another error
  24587. 15:49:34and this will be invalid image type or
  24588. 15:49:36image type is invalid something like
  24589. 15:49:38this
  24590. 15:49:41so now what are we going to do right so
  24591. 15:49:43now this is the place where we are
  24592. 15:49:44actually going to upload our image to
  24593. 15:49:47cloud. Remember when [snorts] you're
  24594. 15:49:49going to upload the image to cloud,
  24595. 15:49:50right? First you have to do it before
  24596. 15:49:51you're going to set because first you'll
  24597. 15:49:53upload this one to your cloud then
  24598. 15:49:55you're going to get your URL back and
  24599. 15:49:56then you are going to store that URL in
  24600. 15:49:58your DB. Okay, so this is how it's going
  24601. 15:50:00to work. So first what I'm going to do
  24602. 15:50:02so I'm going to first do a and now I'm
  24603. 15:50:05just going to call that upload banner
  24604. 15:50:06image to cloud remember and this is
  24605. 15:50:08where you're going to pass a buffer. So
  24606. 15:50:10buffer will be your file dot buffer and
  24607. 15:50:12then you're going to pass a folder. So
  24608. 15:50:14here I'm going to pass a folder and
  24609. 15:50:15let's give this one as NodeJS capstone
  24610. 15:50:18project.
  24611. 15:50:20So this will be my folder. What we're
  24612. 15:50:22going to get we're going to get our
  24613. 15:50:23secure URL and we're going to get our
  24614. 15:50:24public ID. Now it makes sense, right?
  24615. 15:50:26Because once we're going to upload this,
  24616. 15:50:27we're going to get these two things. Now
  24617. 15:50:30we're going to again we're going to
  24618. 15:50:31check if this secure URL is not present
  24619. 15:50:33or this public ID is not present. So
  24620. 15:50:35again throw a new error. So I'll do
  24621. 15:50:37throw new app error give a 500 and I'll
  24622. 15:50:40do cloudinary error or something like
  24623. 15:50:41this. whatever you wanted to give.
  24624. 15:50:46Now what we have to do right? So now we
  24625. 15:50:47already have our URL public ID. So what
  24626. 15:50:49will be next step? Next step will be to
  24627. 15:50:51store this one in your DB. So DB means
  24628. 15:50:53now we are going to start with our
  24629. 15:50:54repository. So this is where we are
  24630. 15:50:56going to simply do what? So I'm just
  24631. 15:50:57simply going to do con banner
  24632. 15:51:01and this will have it. So I do create
  24633. 15:51:03admin banner and then we are going to
  24634. 15:51:06pass
  24635. 15:51:08secure URL and the uh what is that
  24636. 15:51:10public id these two things and at the
  24637. 15:51:13end we are going to return the banner.
  24638. 15:51:14Now this uh return we are going to
  24639. 15:51:16return to our routes. So if you go to
  24640. 15:51:19your routes where is that here we are
  24641. 15:51:21going to return. Okay so now what you
  24642. 15:51:23can do right so first you can import
  24643. 15:51:24this one from our service and we have to
  24644. 15:51:26pass our uh file. So what will be your
  24645. 15:51:28file? But how we are going to get the
  24646. 15:51:30file? That is the important part here.
  24647. 15:51:32So you're going to get this one from
  24648. 15:51:33request and this request is going to
  24649. 15:51:35give you this file. Okay. So this is
  24650. 15:51:36what you have to do. So now it will go
  24651. 15:51:38to your service. It's going to do all
  24652. 15:51:40this step. It is going to upload to
  24653. 15:51:42cloudinary and then we are going to
  24654. 15:51:44create this in our repository. So this
  24655. 15:51:46is how it will work. So now we'll create
  24656. 15:51:48copy this and go to repository and then
  24657. 15:51:50I'm simply going to do export as sync
  24658. 15:51:52function. [snorts]
  24659. 15:51:54So create this. So here we are going to
  24660. 15:51:56receive what? So what we are sending? We
  24661. 15:51:58are sending the image URL which will be
  24662. 15:52:00string and we're also going to get
  24663. 15:52:02cloudinary public id and this will be
  24664. 15:52:06your string. What this is going to
  24665. 15:52:09return this is going to return a promise
  24666. 15:52:10and it's going to return the banner
  24667. 15:52:12type. So what I can do we can so for
  24668. 15:52:15this one I think we just have to write
  24669. 15:52:16simple query right. So lupool doquery
  24670. 15:52:19here will be your so let's take one
  24671. 15:52:22more. So I'm just going to do type
  24672. 15:52:24banner row and this will be banner and
  24673. 15:52:28we're going to pass this.
  24674. 15:52:32Okay. So first what we have to do we
  24675. 15:52:34have to insert correct.
  24676. 15:52:36So do insert into so what is the name?
  24677. 15:52:41So banners. So banners is the
  24678. 15:52:44uh what is that table name. So do insert
  24679. 15:52:46into banners. So what are you going to
  24680. 15:52:48insert? We're going to insert three
  24681. 15:52:49things right.
  24682. 15:52:52uh image URL cloudinary public id these
  24683. 15:52:55two things I think we have to do so
  24684. 15:52:57let's go to our uh repository we'll do
  24685. 15:53:01insert image URL and also insert the
  24686. 15:53:04cloudinary
  24687. 15:53:06public id
  24688. 15:53:10okay and then we are going to pass the
  24689. 15:53:12values so I'll do values
  24690. 15:53:15so dollar one dollar2 and then we're
  24691. 15:53:18going to do a returning so we're going
  24692. 15:53:20to return the ID
  24693. 15:53:22image URL
  24694. 15:53:24cloudinary public ID
  24695. 15:53:27created ad and also let's copy the
  24696. 15:53:29updated ad also
  24697. 15:53:35okay and then you're going to pass this
  24698. 15:53:37so first what will be the first value
  24699. 15:53:38first value is image URL second will be
  24700. 15:53:41your cloud so here this will be image
  24701. 15:53:42URL and this will be your cloud public
  24702. 15:53:44ID and then you're going to just return
  24703. 15:53:47result dot rows of zero and that's set.
  24704. 15:53:51So save this. So this is going to save.
  24705. 15:53:53Now we can simply import this from our
  24706. 15:53:55repository and this is completed. So now
  24707. 15:53:57before we going to test this one, right?
  24708. 15:53:59So let's do one thing. Let's get all the
  24709. 15:54:00banners also because then we can see
  24710. 15:54:02that whether actually it is getting
  24711. 15:54:03stored in our DB or not. Correct? So
  24712. 15:54:05what I'm going to do, right? So let's
  24713. 15:54:06create admin banner dot get. So I'm
  24714. 15:54:09going to do slash and let's copy this
  24715. 15:54:12from here.
  24716. 15:54:18And then I'm simply going to do a try
  24717. 15:54:20catch
  24718. 15:54:24and I'm just going to do next error. So
  24719. 15:54:27in the try so simply I'm just going to
  24720. 15:54:29do con banners and this will be have it
  24721. 15:54:31fetch
  24722. 15:54:33admin b man b man b man b man b man b
  24723. 15:54:35man b man b man b man b man banners
  24724. 15:54:36and then I'm simply going to raise dot
  24725. 15:54:38status of 200. I'm doing little first
  24726. 15:54:40now I think we have already done so many
  24727. 15:54:42times the same thing. will be success
  24728. 15:54:44will be true and let's pass the data and
  24729. 15:54:47data will be your manners. So now we can
  24730. 15:54:49simply create this where in our service
  24731. 15:54:53same thing right it's like repetative
  24732. 15:54:55same thing you're going to do. So now
  24733. 15:54:57let's go to our services first and then
  24734. 15:55:00I'll do export as sync function and I'll
  24735. 15:55:04do fetch admin banners what this is
  24736. 15:55:07going to return this is going to return
  24737. 15:55:08promise but remember you have to move
  24738. 15:55:11all of this so it's going to return of
  24739. 15:55:12array right keep because you can have
  24740. 15:55:14multiple uh banners. So first what I'll
  24741. 15:55:16do right so I'll do con banners this
  24742. 15:55:19will be ait and I'll do find all admin
  24743. 15:55:23banners
  24744. 15:55:26from div something like this and then at
  24745. 15:55:30the end we're going to return this
  24746. 15:55:32banners so now first copy this and then
  24747. 15:55:35we're going to create this one. So we'll
  24748. 15:55:37go to our repository
  24749. 15:55:40and I'm simply going to do export as
  24750. 15:55:41sync function find on admin banners from
  24751. 15:55:45DB.
  24752. 15:55:47So here we are going to return promise
  24753. 15:55:50of banner
  24754. 15:55:54and then we are simply going to do con
  24755. 15:55:57result and this will be a pull dot
  24756. 15:56:00query. So this will be your banner row
  24757. 15:56:04and what you going to do? So we're going
  24758. 15:56:06to just select all this right. So we are
  24759. 15:56:08going to do select ID
  24760. 15:56:11image URL we have cloudinary
  24761. 15:56:16public ID correct. So public
  24762. 15:56:19id
  24763. 15:56:20created at and updated at and then we're
  24764. 15:56:25going to do from banners table because
  24765. 15:56:27we have the banners table and let's do
  24766. 15:56:30order by
  24767. 15:56:32uh created
  24768. 15:56:34at disk.
  24769. 15:56:37That's it. And then you're simply going
  24770. 15:56:38to do what? Return result
  24771. 15:56:41dot rows.
  24772. 15:56:44What happened man? Oh, we have to do
  24773. 15:56:48outside.
  24774. 15:56:49Okay, save this. So now we going to
  24775. 15:56:52simply import this from here and go to
  24776. 15:56:55our routes and we're going to import
  24777. 15:56:57this one from our service. Save this. So
  24778. 15:57:00I think this is done and I think we've
  24779. 15:57:02already added it. So this one. So now
  24780. 15:57:04we'll see what is actually happening.
  24781. 15:57:07So let's run it again.
  24782. 15:57:10Fantastic. So now I'll go here. Okay. So
  24783. 15:57:14first let's open one more. So I'm just
  24784. 15:57:16going to do copy this.
  24785. 15:57:19So this will be post first and this will
  24786. 15:57:22be admin/banners.
  24787. 15:57:24Correct? And first let's go to body and
  24788. 15:57:27what you have to do right you have to do
  24789. 15:57:29form data. See here we have form data
  24790. 15:57:31and this is the key you have to use the
  24791. 15:57:32key. This key will be your image. Okay.
  24792. 15:57:34This key will be the one that you are
  24793. 15:57:37using here. So you have your middleware.
  24794. 15:57:40See you're passing image here. Right? So
  24795. 15:57:42this is your field name. So this will be
  24796. 15:57:44image. Now what you have to do right you
  24797. 15:57:45have to change this one to file and
  24798. 15:57:47you're going to select file. Okay. So
  24799. 15:57:49I'm just going to select I think I have
  24800. 15:57:51downloaded one simple uh which one I can
  24801. 15:57:55use man. Maybe I think I also have some
  24802. 15:57:57thumbnails designed.
  24803. 15:58:00Okay. Let's do this one. Okay. So I'm
  24804. 15:58:02just going to select this one. So first
  24805. 15:58:04let's try to send access token is
  24806. 15:58:07needed. Fantastic. So we'll go to this
  24807. 15:58:10one. So let's uh log in or maybe first
  24808. 15:58:13I'm going to check. So I'll do sum get
  24809. 15:58:16the token copy this
  24810. 15:58:19and then I'll go to headers add
  24811. 15:58:21authorization
  24812. 15:58:24and I'll do b error
  24813. 15:58:26and you'll see that you're going going
  24814. 15:58:28to get admin access is required.
  24815. 15:58:30Awesome. So that means you have to use
  24816. 15:58:32admin token send.
  24817. 15:58:35Uh we'll copy this. Go here. go to
  24818. 15:58:39headers. So we'll do be error
  24819. 15:58:43and then try to send. All right. So
  24820. 15:58:46happening. So let's see what is
  24821. 15:58:47happening. That's it. So now you have ID
  24822. 15:58:50image URL cloudinary public ID are
  24823. 15:58:52getting created at updated. Now let's go
  24824. 15:58:55here and I'll go to my assets.
  24825. 15:59:00That's it. So you have your image here.
  24826. 15:59:02So that [snorts] means this image is now
  24827. 15:59:04already added here. And at the same time
  24828. 15:59:06you are getting the uh URL and this URL
  24829. 15:59:08you are now saving in your DV. So now
  24830. 15:59:10let's say if I just wanted to copy this
  24831. 15:59:12and if I go here and uh this one or
  24832. 15:59:15let's do one thing. I'm also going to
  24833. 15:59:17add one more. Okay. So I'm just going to
  24834. 15:59:19close this and add one more here. Maybe
  24835. 15:59:21I'll just do this one advanc type script
  24836. 15:59:25and then I'll just send.
  24837. 15:59:31So this is done. So now let's go here.
  24838. 15:59:33Refresh this.
  24839. 15:59:35Save two. And maybe let's do one more
  24840. 15:59:38because we need this one. You will
  24841. 15:59:40understand why I'm doing this. I will
  24842. 15:59:42use this one.
  24843. 15:59:50So this is done also. Let's check once.
  24844. 15:59:53Okay. So this is added now. So now what
  24845. 15:59:56we can do, right, we can go here and
  24846. 15:59:57first I'll go here. Go to headers. I'll
  24847. 16:00:00copy this entire part and then go to
  24848. 16:00:02headers, add authorization
  24849. 16:00:05and paste this one. Try to send and you
  24850. 16:00:08can see that you already have banners 1
  24851. 16:00:102 and three. Fantastic. That's it. So
  24852. 16:00:13that means now what you can do right you
  24853. 16:00:15are actually fetching the banners
  24854. 16:00:17directly from your DB. You don't have to
  24855. 16:00:19interact with your cloud now because
  24856. 16:00:20you're already storing your assets in
  24857. 16:00:22your cloud. But the main point will come
  24858. 16:00:24that when you're going to delete this
  24859. 16:00:25one, right? So you just can't directly
  24860. 16:00:28delete from your DV. you also have to
  24861. 16:00:29delete this one from your cloud. So this
  24862. 16:00:32one you're going to do in the next part.
  24863. 16:00:33All right. So now let's implement some
  24864. 16:00:35caching uh strategy. So what are you
  24865. 16:00:38going to do right? So where I'm going to
  24866. 16:00:39implement caching. So let me show you
  24867. 16:00:41and I'm just going to close those files
  24868. 16:00:43that are not needed. So you see that
  24869. 16:00:45we're feting the admin banners right. So
  24870. 16:00:47what you're going to do right every time
  24871. 16:00:48we are not going to f directly from our
  24872. 16:00:50DB. I think you have already checked
  24873. 16:00:52this one in the radius section. I hope
  24874. 16:00:53that you have covered this one. So what
  24875. 16:00:55you're going to do right? So if I'm not
  24876. 16:00:57wrong, let me just quickly check. That
  24877. 16:00:58is called the cast aside pattern, right?
  24878. 16:01:00So we have the cash
  24879. 16:01:03aside
  24880. 16:01:04pattern.
  24881. 16:01:06I think it's called cash aside only.
  24882. 16:01:11Uh okay, let me just check what is that
  24883. 16:01:13called. Citiz
  24884. 16:01:22going to record database immediately
  24885. 16:01:24save the result. Okay. So this is what
  24886. 16:01:25actually we we need. Okay. So we're
  24887. 16:01:27going to implement right. So if the data
  24888. 16:01:29is already present in the cache. So in
  24889. 16:01:31that case we are not going to fetch from
  24890. 16:01:33our DV or else we are going to fetch
  24891. 16:01:36from our DV. But before that we also
  24892. 16:01:37have to do couple of things. So first
  24893. 16:01:39what we going to do right? We'll go to
  24894. 16:01:40our leave
  24895. 16:01:43folder and then I'm just going to create
  24896. 16:01:44a radius. TS file. And now first of all
  24897. 16:01:47what I'm going to do right also let's do
  24898. 16:01:49one thing. So let's add our radius curl
  24899. 16:01:52also here. So I'll do rad CL.
  24900. 16:01:55So this will be check required NV
  24901. 16:01:58variables and go to our env. And then we
  24902. 16:02:01going to do radius CL.
  24903. 16:02:04Save this. So now first of all what I'll
  24904. 16:02:06do right I'm simply going to import
  24905. 16:02:09radius from my radius. So this is the
  24906. 16:02:12first thing and then I'm simply going to
  24907. 16:02:14do export const. So I'll do radius and
  24908. 16:02:16this will be so I'll do new radius and
  24909. 16:02:20here we going to pass env
  24910. 16:02:22dot radius url. Now because we using
  24911. 16:02:26bool uh sorry we are using bool mq
  24912. 16:02:29right. So for bool mq when we are going
  24913. 16:02:30to implement our background job and
  24914. 16:02:32background worker
  24915. 16:02:33bool [snorts] mq actually needs its own
  24916. 16:02:36connection request okay or own
  24917. 16:02:38connection request opsense object. So in
  24918. 16:02:41that case what we going to do right we
  24919. 16:02:42not going to pass our IO radius
  24920. 16:02:44instance. Okay. So what we going to do
  24921. 16:02:46right we are simply going to do con and
  24922. 16:02:49then I'm simply going going to do a
  24923. 16:02:50radius URL and this will be new and here
  24924. 16:02:53we're going to use the URL and here we
  24925. 16:02:56going to pass the env dot radius URL.
  24926. 16:02:58Okay. So that means we're creating what
  24927. 16:03:00we are creating here our own connection
  24928. 16:03:03object request and then what you can do
  24929. 16:03:05right we are simply going to do export
  24930. 16:03:07con and here you can do bull
  24931. 16:03:10uh mq connection
  24932. 16:03:14and this will be you have to pass a
  24933. 16:03:16host. So host will be your radius url
  24934. 16:03:19dot host name. This part also you have
  24935. 16:03:21covered in the URL code modules in the
  24936. 16:03:23NodeJS code modules on this port you're
  24937. 16:03:25simply going to get your number and
  24938. 16:03:27you'll get is radius URL and this is
  24939. 16:03:30going to give you the port okay and then
  24940. 16:03:32we are also going to take let's take a
  24941. 16:03:34max uh retries max retries per request
  24942. 16:03:42and this will be null for no okay so
  24943. 16:03:44save this so first before we'll
  24944. 16:03:46implement our background job logic what
  24945. 16:03:49are going to do right so Let's implement
  24946. 16:03:50our banner cache. So let's do this one
  24947. 16:03:53in leave only. So I'm just going to uh
  24948. 16:03:56do here banner cache
  24949. 16:03:59dot yes. So first of all what we are
  24950. 16:04:01going to do. So first here uh also we
  24951. 16:04:04need to do couple of things. If you have
  24952. 16:04:06already covered the uh ready section we
  24953. 16:04:08know that we actually need two things.
  24954. 16:04:10We need the cast key right and that cast
  24955. 16:04:13key will be using that cast key will be
  24956. 16:04:15able to identify. Second we need a TTL.
  24957. 16:04:17Correct? So this is what you're going to
  24958. 16:04:19do. So, I'm just going to do a const and
  24959. 16:04:21let's do admin banners
  24960. 16:04:24cask key. You can always uh make sure to
  24961. 16:04:27move all of this in your uh what is that
  24962. 16:04:29called? Uh
  24963. 16:04:31uh constant file. So, admin banners also
  24964. 16:04:34I need con and I'll do admin banners
  24965. 16:04:38cash
  24966. 16:04:40t seconds and this will be 60. So, this
  24967. 16:04:46is the first thing. So, now we are going
  24968. 16:04:48to create our cash. So first we are
  24969. 16:04:49going to create how many methods you
  24970. 16:04:51need. So first of all one will be to
  24971. 16:04:54check that whether the cache is present
  24972. 16:04:56or not because if the cash data is
  24973. 16:04:57present we going to return from the
  24974. 16:04:59cache. Second we need to set the data in
  24975. 16:05:02our cache and third will be to
  24976. 16:05:04invalidate the cache. Remember
  24977. 16:05:05invalidate technique I mentioned. So
  24978. 16:05:07let's say you have two assets added or
  24979. 16:05:09two images added in your cloudinary and
  24980. 16:05:11you already storing in your cash. Now
  24981. 16:05:13next time when you're going to add a new
  24982. 16:05:14asset so now the number will become 2 to
  24983. 16:05:17three. So if you're not invalidating
  24984. 16:05:18your cash, you are always going to
  24985. 16:05:20receive two data until unless the TTL
  24986. 16:05:22expires. Okay. So that is the reason you
  24987. 16:05:24need uh that clear method also. So I'm
  24988. 16:05:28simply going to do function and here I'm
  24989. 16:05:30simply going to do get banners from gas.
  24990. 16:05:34So what this is going to return? So this
  24991. 16:05:36is going to return a promise and the
  24992. 16:05:38type will be what it's going to return
  24993. 16:05:40banner of because it there can be
  24994. 16:05:43multiple banners. So here I'm simply
  24995. 16:05:45going to do con
  24996. 16:05:48data and this will be a so we're going
  24997. 16:05:51to use radius from radius and this is
  24998. 16:05:54going to do get method and this get
  24999. 16:05:56method what you have to pass you have to
  25000. 16:05:57pass the cast key right that's it and if
  25001. 16:06:00this data is not present so what you're
  25002. 16:06:02going to do you're going to return null
  25003. 16:06:06and if this data is present so you're
  25004. 16:06:07going to do a JSON doparts it's like
  25005. 16:06:09simple and then you're going to do what
  25006. 16:06:11cache data and what will be the type
  25007. 16:06:13will be as banner of array. So this will
  25008. 16:06:16simply get the data from your C. You
  25009. 16:06:18don't have to do anything. Next I have
  25010. 16:06:21to set. So I'll do export as sync
  25011. 16:06:23function and I'll do set banner C. This
  25012. 16:06:28also what? So it's going to return the
  25013. 16:06:29banners data. The type will be banner of
  25014. 16:06:32array and this is not going to return
  25015. 16:06:35anything because we don't have to do
  25016. 16:06:36anything here. We're just going to set
  25017. 16:06:38what you're going to set. So a radius we
  25018. 16:06:40have a set method. So you're going to
  25019. 16:06:42set what this is going to receive. This
  25020. 16:06:44is going to receive your key. So this
  25021. 16:06:45will be your admin b maners cast key.
  25022. 16:06:48Then you're going to set the data the
  25023. 16:06:50value. So what will be the value? Value
  25024. 16:06:52will be JSON dot stringify the banners
  25025. 16:06:55data.
  25026. 16:06:57So this banners data that you're going
  25027. 16:06:58to receive. And then uh we are also
  25028. 16:07:01going to pass. So let's pass a x and
  25029. 16:07:04then I'm also going to pass the ttl. TTL
  25030. 16:07:07will be my TTL.
  25031. 16:07:09Save. I think this is done. And then
  25032. 16:07:12last we need to validate invalidate. So
  25033. 16:07:14do export as sync function
  25034. 16:07:16and I do look clear banners
  25035. 16:07:21and this will be promise of
  25036. 16:07:26void. So what are we going to do here?
  25037. 16:07:28We're simply going to do a radius dot
  25038. 16:07:31delete and you're going to pass the cask
  25039. 16:07:33key. That's it. So these three you need.
  25040. 16:07:36Now how you're going to use this? This
  25041. 16:07:37is the important part right. So where
  25042. 16:07:39you are going to use this one or first
  25043. 16:07:41of all where you are going to set and
  25044. 16:07:43when you're going to delete or when
  25045. 16:07:45you're going to receive. So you have to
  25046. 16:07:46do all this in your service. The first
  25047. 16:07:49one very very important when you're
  25048. 16:07:50going to create a new asset. So whenever
  25049. 16:07:53we are going to create a new asset let's
  25050. 16:07:55say this one after this we need to
  25051. 16:07:57invalidate the cash and I think this one
  25052. 16:08:00you're clearly understanding why we are
  25053. 16:08:01doing right. So every time you're going
  25054. 16:08:02to do we need to do what? We're going to
  25055. 16:08:04do a and we're going to do clear
  25056. 16:08:08dash banners cash. So this is the first
  25057. 16:08:11thing. Okay. Now next will be how you're
  25058. 16:08:14going to fetch. Now fetch is actually
  25059. 16:08:15very simple. So first what you'll do? So
  25060. 16:08:17we're going to do con
  25061. 16:08:19do get cast banners. See radius is not
  25062. 16:08:24that much difficult. Okay. If you
  25063. 16:08:25practice you will be able to understand
  25064. 16:08:27very easily. And I'm simply going to do
  25065. 16:08:28get banners from cast. And then we'll
  25066. 16:08:31check if this get banner from cash is
  25067. 16:08:34present. You simply return this data. So
  25068. 16:08:36you're going to return oh sorry get cash
  25069. 16:08:39banners present you return this. If this
  25070. 16:08:42is not so you're going to fetch from
  25071. 16:08:43your DB. So you already have this one
  25072. 16:08:45from DB and then you're going to just
  25073. 16:08:47set the data to cache
  25074. 16:08:50because if your data is already present
  25075. 16:08:52is cast you don't have to set it
  25076. 16:08:54correct. If it is not then you're going
  25077. 16:08:55to set. So here you're going to do a do
  25078. 16:08:58set banner cast and you're going to pass
  25079. 16:09:01these banners. That's it. And this is
  25080. 16:09:03done. So now what will happen right? So
  25081. 16:09:05now this radius functionality should
  25082. 16:09:07work. So every time so let's say if I
  25083. 16:09:09try to send this right and next time I'm
  25084. 16:09:11going to send now see the timing here.
  25085. 16:09:13So it's actually receiving from the
  25086. 16:09:15cash. Okay. So it's actually receiving
  25087. 16:09:17from your cash very very clearly. You
  25088. 16:09:19can see this one right? So also you can
  25089. 16:09:21also verify. Let's say if I try to I'm
  25090. 16:09:24not sure if this will work. So let's do
  25091. 16:09:26one thing. Let's try to check. So I'm
  25092. 16:09:28just going to remove this clear banner
  25093. 16:09:30cast. Okay. And I'll go here. And then
  25094. 16:09:33let's say if I try to send this one.
  25095. 16:09:38So this is now done. And then let's see
  25096. 16:09:41what you're getting.
  25097. 16:09:43So you can see that you are getting 1 2
  25098. 16:09:453. Three only. But if you go here, you
  25099. 16:09:48see that you have one more here.
  25100. 16:09:511 2 3 4. So you're getting only three
  25101. 16:09:53because you are not clearing the cash.
  25102. 16:09:55But now if I remove this right and next
  25103. 16:09:57time let's say better wait to send I'll
  25104. 16:09:59see that you should be getting. So you
  25105. 16:10:00are still getting three only here.
  25106. 16:10:08Okay I have to create I got it. So I
  25107. 16:10:10have to create here one more. So then it
  25108. 16:10:12will be cleared. My bad. So now once I
  25109. 16:10:14create uh clear uh sorry clear right and
  25110. 16:10:16if I go here now try to send. So you'll
  25111. 16:10:18be getting five. I see that 1 2 3 4 5
  25112. 16:10:23simple. So that means when we'll create
  25113. 16:10:26that time we're going to clear the
  25114. 16:10:27cache. So with this now caching is also
  25115. 16:10:30completed. Now try to implement some of
  25116. 16:10:32the caching in other parts if you want
  25117. 16:10:34like in the CL in the previous sections.
  25118. 16:10:36So now what we have to do we are going
  25119. 16:10:37to do very very important section which
  25120. 16:10:38will be how we going to delete. So in
  25121. 16:10:40this case we're going to use bool MQ and
  25122. 16:10:42we are going to implement our background
  25123. 16:10:44job and background worker. So let's see
  25124. 16:10:45how we going to do that in the next
  25125. 16:10:46part. All right. So now this one will be
  25126. 16:10:48very very important section. Okay. So in
  25127. 16:10:50this one you are going to understand
  25128. 16:10:51that how we are going to implement a
  25129. 16:10:53background job feature. So whenever we
  25130. 16:10:55are going to delete a image from our
  25131. 16:10:56cloudinary. So how we are going to
  25132. 16:10:58delete from DB and at the same time the
  25133. 16:11:00delete from cloud or delete from your
  25134. 16:11:02cloudinary will happen in the
  25135. 16:11:04background. Okay. So we are going to
  25136. 16:11:06return our response immediately but in
  25137. 16:11:08the background bool MQ and radius is
  25138. 16:11:10going to take care of deleting these
  25139. 16:11:12assets. Okay. So first let's do one by
  25140. 16:11:14one. Okay. And this will be very very
  25141. 16:11:16interesting because you need to
  25142. 16:11:17understand the flow first. So go to
  25143. 16:11:19[snorts] leave and cloudinary and we
  25144. 16:11:20need one more helper function here and
  25145. 16:11:22that will be to delete. Okay. So first
  25146. 16:11:24I'm just going to do export as sync
  25147. 16:11:26function and this will be my delete
  25148. 16:11:30banner image from cloudinary.
  25149. 16:11:37So first of all what we need we need
  25150. 16:11:39public ID. I think I don't have to
  25151. 16:11:40explain I already mentioned this one
  25152. 16:11:42multiple times that that is the reason
  25153. 16:11:45we are actually keeping the public ID
  25154. 16:11:47because this will help us to delete this
  25155. 16:11:49one from cloudinary
  25156. 16:11:52so we'll do void and now
  25157. 16:11:56let's take these two uh let's take this
  25158. 16:11:59line and then we are also going to I
  25159. 16:12:00think we can take this part
  25160. 16:12:06this will be same so we paste this one
  25161. 16:12:08and Now we can simply do return. So
  25162. 16:12:11we'll have cloudy dot uploader and then
  25163. 16:12:14you have something called destroy and
  25164. 16:12:16then you're simply going to do public
  25165. 16:12:17ID. Now this cloud is going to take care
  25166. 16:12:20of that particular asset deletion based
  25167. 16:12:22on the public ID and it's going to
  25168. 16:12:23delete from here. Okay. So this is the
  25169. 16:12:25first thing. So now what you're going to
  25170. 16:12:27do right? So first okay we also need to
  25171. 16:12:30do one uh important thing and that is we
  25172. 16:12:32are going to create one more route here.
  25173. 16:12:34Correct. So let's go to routes admin
  25174. 16:12:36banner routes and let's create our route
  25175. 16:12:38here. So admin banner route dot delete
  25176. 16:12:41and here I'm simply going to do banner
  25177. 16:12:43ID. Why? Because you need the dynamic
  25178. 16:12:45value because you need to understand
  25179. 16:12:46that which banner you're going to
  25180. 16:12:48delete. Correct? So here
  25181. 16:12:52so this will be next.
  25182. 16:12:54And now I'm simply going to do try catch
  25183. 16:12:58and in this catch you're going to take
  25184. 16:13:00error. And now we're going to do next.
  25185. 16:13:03And then you're going to pass the error.
  25186. 16:13:04Okay. Now this one I think we have
  25187. 16:13:06already covered why we are doing the
  25188. 16:13:07same thing so that it will be uniform.
  25189. 16:13:08But you can do lot of other way also
  25190. 16:13:10here. Okay. So but this is fine. So here
  25191. 16:13:13what we're going to do. First we're
  25192. 16:13:14going to call a service. Okay. This
  25193. 16:13:16service will be delete admin banner
  25194. 16:13:19service. Now let's understand this flow
  25195. 16:13:21one by one. What you're going to pass?
  25196. 16:13:23You're going to pass a request dot
  25197. 16:13:25params and then banner ID. Simple. So
  25198. 16:13:27you're going to get the banner ID.
  25199. 16:13:28That's it. And then you're going to
  25200. 16:13:30return a response. So raise dot status
  25201. 16:13:32200 dot JSON. And now you're going to do
  25202. 16:13:36success will be true. And let's do
  25203. 16:13:39message.
  25204. 16:13:41So this will be banner deleted
  25205. 16:13:44successfully.
  25206. 16:13:47What? Okay. So this will be banner
  25207. 16:13:50deleted successfully.
  25208. 16:13:54Okay. But what will be in this service?
  25209. 16:13:57Now this is where it will be very
  25210. 16:13:58important. Okay. So this is where you
  25211. 16:14:00have to understand that how this will
  25212. 16:14:01work. If you do this one properly one
  25213. 16:14:05you'll be able to create multiple. Okay.
  25214. 16:14:07So if you go to service and let's create
  25215. 16:14:10this service. So first of all I'm just
  25216. 16:14:11going to do export as sync function and
  25217. 16:14:14we have got a delete what is it? Delete
  25218. 16:14:16admin banner. What this is going to
  25219. 16:14:18receive? This is going to receive a
  25220. 16:14:19banner ID. This will be a string. What
  25221. 16:14:23this is going to return nothing. So it's
  25222. 16:14:24going to return promise and then void.
  25223. 16:14:26So now you have to understand the flow
  25224. 16:14:28here. Now what I mean by flow. So first
  25225. 16:14:31as I already mentioned that when we
  25226. 16:14:32going to delete we are going to delete
  25227. 16:14:34from your DB immediately. We are going
  25228. 16:14:37to delete from our postgress first and
  25229. 16:14:39we are going to get our cloud. So do
  25230. 16:14:41delete from our DB
  25231. 16:14:44and get the and get the public
  25232. 16:14:50ID. So now I'm simply going to do con
  25233. 16:14:52public ID. This will be a delete
  25234. 16:14:57admin banner by id. How you going to
  25235. 16:15:01get? We're going to pass the banner ID.
  25236. 16:15:03Okay. So then I'm simply going to check
  25237. 16:15:05if this public ID is not present. So
  25238. 16:15:08I'll do throw new app error 404 and I'll
  25239. 16:15:12do banner not found. And now what I have
  25240. 16:15:16to do here can you guess this one we're
  25241. 16:15:19going to create. So this we just have to
  25242. 16:15:20delete from our posgress DB. Now once
  25243. 16:15:24you delete then what you have to do see
  25244. 16:15:26remember let's say you have a five
  25245. 16:15:27assets added correct once you delete one
  25246. 16:15:30asset it will become four now let's say
  25247. 16:15:32those five assets assets are stored in
  25248. 16:15:35your radius cache so that means when
  25249. 16:15:37you're going to delete so now your DB
  25250. 16:15:39becomes four next time when when you're
  25251. 16:15:41going to fetch we are going to get four
  25252. 16:15:43data correct but if you're not
  25253. 16:15:46invalidating your cast you are always
  25254. 16:15:48going to get five data that we have
  25255. 16:15:50checked this one so that means here also
  25256. 16:15:51you're going to do private clear better
  25257. 16:15:54case now this is extremely important
  25258. 16:15:56okay very important now why I'm
  25259. 16:15:57repeating this thing very important and
  25260. 16:16:00I'm telling this one same thing again
  25261. 16:16:01and again because when you're
  25262. 16:16:03implementing radius and all you need to
  25263. 16:16:04know where you are going to use this
  25264. 16:16:06radius
  25265. 16:16:07methods right you can't just like
  25266. 16:16:10blindly use this based on the feature
  25267. 16:16:11you have to use for example here we are
  25268. 16:16:13using maybe in some other functionality
  25269. 16:16:15we don't have to use this one so you you
  25270. 16:16:16have to understand no one is going to
  25271. 16:16:18come and teach you in every project okay
  25272. 16:16:20so now what you have to do
  25273. 16:16:22This is where we're going to add our BMQ
  25274. 16:16:24job. So I'll do add MQ.
  25275. 16:16:28So this is going to happen in the
  25276. 16:16:29background. So first we're going to
  25277. 16:16:31delete from our posgress get the public
  25278. 16:16:32ID and then add this one to our
  25279. 16:16:34background job. But before this first
  25280. 16:16:37let's delete this one from our DB. So
  25281. 16:16:38I'll go to our repository and let's
  25282. 16:16:42create one more. So I'll do export async
  25283. 16:16:45function. See night time also this uh
  25284. 16:16:49bikes and everything right? sound. Okay.
  25285. 16:16:52So you'll see banner ID we are going to
  25286. 16:16:54get this will be string and what this is
  25287. 16:16:57going to return. So this is going to
  25288. 16:16:58return a promise. Let's do whether it's
  25289. 16:17:00return string or null because we are
  25290. 16:17:02going to return the uh what is that?
  25291. 16:17:05What is that called? Public id. So we do
  25292. 16:17:08con result. So do ait cool dot query.
  25293. 16:17:14So this is going to give you the
  25294. 16:17:15cloudinary
  25295. 16:17:18public ID. you can directly directly
  25296. 16:17:20take here only and this will be a string
  25297. 16:17:23and here what I'm going to do oh
  25298. 16:17:27here we are going to write our qu so now
  25299. 16:17:29I'm simply going to do that delete from
  25300. 16:17:34what what is the name banners where id
  25301. 16:17:38equal to 1 and then I'm going to return
  25302. 16:17:42so I'll do returning cloudinary public
  25303. 16:17:44ID because see we at the end we are
  25304. 16:17:47going to have the cloudinary public ID
  25305. 16:17:49here. Correct? And then you're going to
  25306. 16:17:50pass your value which will be your
  25307. 16:17:52banner ID. And now what are we going to
  25308. 16:17:55do? So do return
  25309. 16:17:58result dot rows of zero
  25310. 16:18:03dot cloud public ID or else this will be
  25311. 16:18:06null. So this is going to delete from
  25312. 16:18:08your DB. It's going to return the public
  25313. 16:18:10ID. Now we are going to add this one to
  25314. 16:18:12our boolean key job. So I'm going to add
  25315. 16:18:14it. We'll do add delete
  25316. 16:18:18cloudinary
  25317. 16:18:21B uh let's do cloudinary image job
  25318. 16:18:25and this will be public ID.
  25319. 16:18:29So first let's import this from our
  25320. 16:18:31repository. So we're going to copy this.
  25321. 16:18:33Now let's create our Q. Okay. So here we
  25322. 16:18:35are going to create our Q's. So we're
  25323. 16:18:37going to create Q's and workers. So I'm
  25324. 16:18:39going to add a Q here. Okay. And let's
  25325. 16:18:42give this one name as delete
  25326. 16:18:44cloudinary. No, let's keep this one
  25327. 16:18:46capsule cloudinary
  25328. 16:18:50image
  25329. 16:18:52dot q
  25330. 16:18:55not this much dot q.ts.
  25331. 16:18:58Okay. So now here we are going to first
  25332. 16:19:02of all we have to take our q name. So
  25333. 16:19:05I'll do a Q name and this will be your
  25334. 16:19:08cloudinary
  25335. 16:19:11jobs. So this will be your preview name
  25336. 16:19:13for handling audio cloudinary related
  25337. 16:19:16job. So first what I'm going to do right
  25338. 16:19:18I'm going to do export as sync function
  25339. 16:19:21and I'll do add delete
  25340. 16:19:24cloudinary image job. What this is going
  25341. 16:19:27to receive? So let's import this one
  25342. 16:19:30from our job. So this is going to
  25343. 16:19:32receive a public ID. Correct? So here
  25344. 16:19:34you're going to receive [snorts] a
  25345. 16:19:35public id and this public id will be
  25346. 16:19:38string and here we're going to return a
  25347. 16:19:40promise of
  25348. 16:19:43void. So now what are we going to do
  25349. 16:19:46first? So first of all let's do one
  25350. 16:19:48thing what you have to do right. So we
  25351. 16:19:50are going to add one job. So this is
  25352. 16:19:51what we're going to do. We going to add
  25353. 16:19:53one job to our que. Okay. So there will
  25354. 16:19:58be two things. So first will be your
  25355. 16:19:59job.name mean this is going to tell that
  25356. 16:20:01okay what kind of job you are going to
  25357. 16:20:03do. Second there will be data means the
  25358. 16:20:05data that you need to pass to your
  25359. 16:20:08worker. So you have a Q and then you're
  25360. 16:20:10going to have a worker two things. Okay.
  25361. 16:20:12So first what I'll do right? So let's do
  25362. 16:20:14Q and then I'm simply going to do type.
  25363. 16:20:17So I'm just going to do a delete
  25364. 16:20:20cloudinary
  25365. 16:20:22image job data and here I'm simply going
  25366. 16:20:26to have a public ID. Correct? So do
  25367. 16:20:28public ID and this will be string.
  25368. 16:20:31So now what I'll do right? So first I'm
  25369. 16:20:32going to do con this will be my job data
  25370. 16:20:36and this will be my this one uh delete
  25371. 16:20:38cloudinary job data and this is where
  25372. 16:20:40you're going to have your public ID all
  25373. 16:20:41right and this public ID is the public
  25374. 16:20:43ID that you're receiving so now we are
  25375. 16:20:45going to add our job to our que how you
  25376. 16:20:47can do that so I'm just going to add it
  25377. 16:20:50and here we are going to create a que so
  25378. 16:20:53how you're going to create a que to
  25379. 16:20:54create a simply going to do const and
  25380. 16:20:57you do delete cloudinary image q and
  25381. 16:21:01this will be a new Q from BM MQ you're
  25382. 16:21:05going to import you have to pass the
  25383. 16:21:07name right and then you have see you
  25384. 16:21:09have a connection type of radius
  25385. 16:21:11connection correct so you need the
  25386. 16:21:12radius connection so here you're going
  25387. 16:21:13to pass a Q name and your connection
  25388. 16:21:15will be your B MQ connection that you
  25389. 16:21:18have created remember in the radius here
  25390. 16:21:20host port and max retries per request so
  25391. 16:21:23this is going to create a Q and this Q
  25392. 16:21:25[snorts] you're going to add here so I'm
  25393. 16:21:27just going to this dot add and now
  25394. 16:21:30you're going to pass that what will be
  25395. 16:21:32name and what will be data data will be
  25396. 16:21:34that what data sorry what work you are
  25397. 16:21:36actually doing so what we we are doing
  25398. 16:21:38here we actually doing a deleting right
  25399. 16:21:40so what you can do right you can create
  25400. 16:21:42one more con here and I'm simply going
  25401. 16:21:44to do delete
  25402. 16:21:46cloudinary
  25403. 16:21:48image job and this will be uh delete
  25404. 16:21:56cloudinary
  25405. 16:21:58image so this would be my job Right. So
  25406. 16:22:01this is what we are doing here. So I'm
  25407. 16:22:03going to pass this job and then you're
  25408. 16:22:04going to pass the job data. So job data
  25409. 16:22:06will be your public ID. This will be
  25410. 16:22:08your job data and then you can pass
  25411. 16:22:11couple of properties. So first one will
  25412. 16:22:12be let's try attempts. So three times we
  25413. 16:22:14are going to check. Let's add some back
  25414. 16:22:17off. So here I'm just going to do type
  25415. 16:22:18and this will be exponential and let's
  25416. 16:22:21do a delay of 3,000. Right? So what will
  25417. 16:22:25happen if this fails by any chance?
  25418. 16:22:27Sorry. Oh not this one. If cloudinary
  25419. 16:22:30fails. So what we're going to do we're
  25420. 16:22:31going to retrace automatically and also
  25421. 16:22:33let's do one thing. So what we're going
  25422. 16:22:34to do right we're going to keep our
  25423. 16:22:36radius connection clean once it is done.
  25424. 16:22:38So we'll do remove on complete. So this
  25425. 16:22:40will be true and let's do remove on
  25426. 16:22:43fail. This will be false.
  25427. 16:22:45So this will be our job that we are
  25428. 16:22:48going to do. So simply important thing
  25429. 16:22:49you have to understand that what data
  25430. 16:22:50you are sending. So we are sending our
  25431. 16:22:52job data which receives the public ID.
  25432. 16:22:55What will be your job right? What
  25433. 16:22:57actually you're doing that you have to
  25434. 16:22:59explain right? And then how we going to
  25435. 16:23:00create the latest connection. So now we
  25436. 16:23:03can save this. Now this is not yet
  25437. 16:23:05completed. Why? Because you need to
  25438. 16:23:06create the worker. Worker is going to
  25439. 16:23:09actually do the things right. So it's
  25440. 16:23:10going to delete your image. How it's
  25441. 16:23:13going to delete your image? Because if
  25442. 16:23:14you go to your cloudinary what we are
  25443. 16:23:16doing here. Okay. Let's import this one
  25444. 16:23:19here. So if you go to your cloudinary uh
  25445. 16:23:23here, right? So we have this delete
  25446. 16:23:25image from cloudinary. This one we are
  25447. 16:23:27not using any place. See we have only
  25448. 16:23:29one place. So this is what we going to
  25449. 16:23:31use in our worker and this is where
  25450. 16:23:33actually we are returning or actually we
  25451. 16:23:35are deleting the asset. So let's see how
  25452. 16:23:37you're going to do that in the worker
  25453. 16:23:38part. Okay. So now let's go to our
  25454. 16:23:40source and let's create our workers. So
  25455. 16:23:43here we are going to do delete
  25456. 16:23:47plinary
  25457. 16:23:49image dotworker
  25458. 16:23:52ds. So first what you have to do right
  25459. 16:23:55uh let's close everything else. We need
  25460. 16:23:57our Q's. Let's go to Q's file. And we
  25461. 16:24:01need the Q name. Now this Q name has to
  25462. 16:24:03be same. Okay. The the Q name that
  25463. 16:24:05you're using in Q and the Q name you're
  25464. 16:24:07using in worker both has to be same. So
  25465. 16:24:10now what also let's take this also
  25466. 16:24:12because we have this type delete
  25467. 16:24:14cloudinary image job data. This is also
  25468. 16:24:16we need the public ID.
  25469. 16:24:18So first let's create our export con and
  25470. 16:24:21this will be our delete cloudinary
  25471. 16:24:24image worker. Here
  25472. 16:24:28what you're going to do right we're
  25473. 16:24:29going to import new worker from our
  25474. 16:24:32boolean MQ. So this will be from your
  25475. 16:24:34boolean MQ what you need? So first of
  25476. 16:24:36all you need see you need the name. Okay
  25477. 16:24:38so this will be the Q name and then
  25478. 16:24:40you're going to do what? It's going we
  25479. 16:24:42are going to create one async job in
  25480. 16:24:45this. So first I'm going to do a Q name
  25481. 16:24:47and then I'm going to take async
  25482. 16:24:49function and this is will be our job. So
  25483. 16:24:52first what I'll do right simply I'm
  25484. 16:24:54going to check if this job dot name if
  25485. 16:24:57this not equal to this delete I think
  25486. 16:25:00let's create this one where is that
  25487. 16:25:02delete uh uh this one delete cloudinary
  25488. 16:25:06image job both has to be same also so if
  25489. 16:25:09this is not the case so in that case
  25490. 16:25:13what I'll do we're going to return so
  25491. 16:25:14this is the first thing and then what I
  25492. 16:25:16going to do I'll do con data and this
  25493. 16:25:19will be my job dot data
  25494. 16:25:21as so what you're sending here so you're
  25495. 16:25:24sending in the data right in the data
  25496. 16:25:26you're sending public ID correct this
  25497. 16:25:28job data so that means this will be the
  25498. 16:25:30this type that you're using so you're
  25499. 16:25:32receiving public ID so this will be as
  25500. 16:25:34delete cloudinary image job data and now
  25501. 16:25:36what you're going to do you'll do a and
  25502. 16:25:38this is where you're going to do delete
  25503. 16:25:40banner image from cloudinary
  25504. 16:25:43now here this is where you are actually
  25505. 16:25:46going to delete this one so that means
  25506. 16:25:47this is the place we are actually
  25507. 16:25:49calling this delete function which is
  25508. 16:25:51actually going to delete the asset from
  25509. 16:25:53your uh cloud ID. Now this one you see
  25510. 16:25:56that we are not doing in our route we
  25511. 16:25:58are doing in the background. Okay. So
  25512. 16:25:59this is what you're going to do and then
  25513. 16:26:00pass the public ID. What will be public
  25514. 16:26:03ID? Public ID will be data dot public
  25515. 16:26:05ID. That's it. So this is done and then
  25516. 16:26:08what you're going to do right you have
  25517. 16:26:09to pass a connection here also. If I go
  25518. 16:26:10over here right so you see that uh we
  25519. 16:26:13have the worker see we have to pass the
  25520. 16:26:15worker option and the connection also.
  25521. 16:26:16So here we have to do what right? We're
  25522. 16:26:18going to pass a connection and this
  25523. 16:26:20connection will be your BMQ
  25524. 16:26:23connection. So this is done but this is
  25525. 16:26:25not yet completed. Now what we're going
  25526. 16:26:27to do right we're going to call this
  25527. 16:26:28delete uh cloudinate image worker and
  25528. 16:26:30we're going to take a on event and this
  25529. 16:26:32will be once this is completed. So you
  25530. 16:26:34see that once this is completed so what
  25531. 16:26:36we're going to do right we're going to
  25532. 16:26:38do something. So let's say I'm going to
  25533. 16:26:40log some data here. So what I'll do
  25534. 16:26:42right, I'm going to take our logger doin
  25535. 16:26:44info and I'm simply going to do
  25536. 16:26:47cloudinary
  25537. 16:26:50delete job completed. Okay. And uh let's
  25538. 16:26:56do assets removed something like this.
  25539. 16:26:59Now let's give a completed.
  25540. 16:27:03And here we are going to do this. Let's
  25541. 16:27:04do job do ID. So this will be when this
  25542. 16:27:08is completed. if this is failed. So I'm
  25543. 16:27:10just going to do cloudinary sorry delete
  25544. 16:27:13cloudinary worker dot on and this will
  25545. 16:27:16be when this is failed. So in this case
  25546. 16:27:19we're going to get uh error also right?
  25547. 16:27:22So we have a failed so we have error
  25548. 16:27:23also. So in this case we're going to get
  25549. 16:27:24a job and error. So in this case I'm
  25550. 16:27:27simply going to do logger dot error.
  25551. 16:27:30So let's pass error. So this will be an
  25552. 16:27:34error and I'm simply going to pass a job
  25553. 16:27:37id which will be job dot id
  25554. 16:27:41and I'm simply going to do cloudinary
  25555. 16:27:47delete
  25556. 16:27:48job field and then at the end we're
  25557. 16:27:52going to do a log.info and I'll do
  25558. 16:27:55delete cloudinary
  25559. 16:27:58image worker started.
  25560. 16:28:02So this is done but this is still not
  25561. 16:28:05yet completed. What we are going to do
  25562. 16:28:06right go to package dojson and then we
  25563. 16:28:10are going to add one more script here.
  25564. 16:28:12So I'm simply going to do a worker and
  25565. 16:28:15also do tsx source/
  25566. 16:28:18So we have workers right? So we have
  25567. 16:28:20workers and then let's run uh copy this.
  25568. 16:28:24So do workers/
  25569. 16:28:26this. So now what we'll do right we'll
  25570. 16:28:29go here open one more and I'm just going
  25571. 16:28:32to do npm run
  25572. 16:28:34worker.
  25573. 16:28:37So this is started now see so first
  25574. 16:28:40we'll go here and then first let's do
  25575. 16:28:43one thing let's go to our login right.
  25576. 16:28:45So I'm just going to go to login.
  25577. 16:28:48Let's take create one uh token. We'll
  25578. 16:28:52copy this. We'll go here. We'll change
  25579. 16:28:54this one to delete. And then uh what
  25580. 16:28:57also you're going to do first also let's
  25581. 16:28:59see one thing man. Okay, let's go to our
  25582. 16:29:02uh where is that? Uh routes.
  25583. 16:29:05Okay, we're using delete. That's fine.
  25584. 16:29:07So, we'll do banners slash and let's say
  25585. 16:29:09I wanted to delete which one we wanted
  25586. 16:29:12to delete. Maybe let's check this image,
  25587. 16:29:14right? So, I wanted to check this image
  25588. 16:29:15backend system and maybe I wanted to
  25589. 16:29:17delete this one. So, what I'll do right,
  25590. 16:29:19I'm going to take the ID
  25591. 16:29:22and then I'm going to paste it here. Go
  25592. 16:29:24to headers, add authorization,
  25593. 16:29:27and then I'm going to take the token.
  25594. 16:29:36Okay. And then I'll just add it here. So
  25595. 16:29:38do beer
  25596. 16:29:41space and see what will happen. Now I
  25597. 16:29:43wanted to wait. I wanted to keep this
  25598. 16:29:46one open. So you'll see that this is
  25599. 16:29:49started. Now I'm just going to delete
  25600. 16:29:50send this. Delete it successfully. And
  25601. 16:29:52you'll see that cloudinary delete job
  25602. 16:29:54completed one immediately. Now if you go
  25603. 16:29:56here we have four and refresh this.
  25604. 16:30:00That's it. And you'll see that uh I
  25605. 16:30:03think we have added four rightes. Okay
  25606. 16:30:05let's do one thing. I think let's delete
  25607. 16:30:09uh I think that is the reason I should
  25608. 16:30:10should not have added all of these here
  25609. 16:30:12man. So maybe what I'm going to do
  25610. 16:30:14right. Okay I'll do a different way. So
  25611. 16:30:16first I'll go to I'll copy this. Okay
  25612. 16:30:19I'll copy this. Let's go to post. I'll
  25613. 16:30:21add one more here. So, paste this one.
  25614. 16:30:24Go to body and let's close this and
  25615. 16:30:27select a different one. Maybe let's add
  25616. 16:30:29this one. And then I'm going to send
  25617. 16:30:31this one.
  25618. 16:30:36Okay. This is done. Let's go here,
  25619. 16:30:38refresh this.
  25620. 16:30:41Okay. So, we have this one correct. Now,
  25621. 16:30:43what I'll do, right, I will copy this
  25622. 16:30:45ID.
  25623. 16:30:46Go to delete. And you'll see that we
  25624. 16:30:48have this one open. Okay. So, one job is
  25625. 16:30:50completed. And now I'm going to just
  25626. 16:30:52paste this one here. Delete this.
  25627. 16:30:55Immediately deleted. And you see
  25628. 16:30:57cloudinary delete job. See how fast this
  25629. 16:30:59is. Okay. Crazy. So this is already
  25630. 16:31:02deleted. And if you go here, you don't
  25631. 16:31:05have that image. That's it. So that
  25632. 16:31:07means this is how you're going to
  25633. 16:31:08implement background fe uh background
  25634. 16:31:10jobs. Okay. So you're going to have a
  25635. 16:31:11queue. You're going to have a worker.
  25636. 16:31:13The important part is that you have to
  25637. 16:31:15understand that how when you're going to
  25638. 16:31:16add the job in your queue because we
  25639. 16:31:19have added this one where in our
  25640. 16:31:20services correct this is where we are
  25641. 16:31:21adding this one and then your worker is
  25642. 16:31:23going to pick it up and based on that
  25643. 16:31:25he's going to do the job in the
  25644. 16:31:26background so with this now this
  25645. 16:31:28particular section is completed okay all
  25646. 16:31:31right so now what will be our next step
  25647. 16:31:33next step now we are going to implement
  25648. 16:31:34our Google o login so first of all see I
  25649. 16:31:38think some of you already have seen in
  25650. 16:31:39some other website that whenever you
  25651. 16:31:41you'll go to their login page or signup
  25652. 16:31:43pages. See that you're going to have
  25653. 16:31:45[snorts] like uh consider like you're
  25654. 16:31:47going to have that you sign in with
  25655. 16:31:50email or password or you just sign in
  25656. 16:31:53with Google directly. So how this
  25657. 16:31:56particular flow work? Now see I'm not
  25658. 16:31:57going to go into too much technical but
  25659. 16:31:59I will try to explain this flow in very
  25660. 16:32:01very simpler way. So what usually happen
  25661. 16:32:03let's see if you have I think some I'm
  25662. 16:32:05pretty much sure almost all of you have
  25663. 16:32:08seen this particular flow that whenever
  25664. 16:32:10you click on this Google button or
  25665. 16:32:12whatever it is there let's say link
  25666. 16:32:14you'll see that it will go to a Google
  25667. 16:32:16login page correct and that makes sense
  25668. 16:32:18because we are actually doing the login
  25669. 16:32:20using Google and it's the task of Google
  25670. 16:32:22who is going to actually do the
  25671. 16:32:23verification
  25672. 16:32:25whether this particular user is a valid
  25673. 16:32:27Google user or not. So in that case what
  25674. 16:32:29you have to do right we don't have to
  25675. 16:32:30again manage our password in this case
  25676. 16:32:33because Google is already taking care of
  25677. 16:32:34all of this. So how this flow will work.
  25678. 16:32:37So let's say so first of all you are
  25679. 16:32:39going to have your application right and
  25680. 16:32:41in this application you're going to have
  25681. 16:32:42a button right? So let's say continue
  25682. 16:32:44with Google once you're going to click
  25683. 16:32:45on Google right. So what will happen
  25684. 16:32:47right? So we are going to create a
  25685. 16:32:48Google project for this. First of all we
  25686. 16:32:50need some Google credentials. Why?
  25687. 16:32:52because to navigate that particular user
  25688. 16:32:54from your application to the Google
  25689. 16:32:56login page, you need to configure
  25690. 16:32:58something in your application and that
  25691. 16:33:00is called we need two things. So first
  25692. 16:33:02of all you need actually three things we
  25693. 16:33:04need uh one Google cloud project which
  25694. 16:33:06is going to give you the Google client
  25695. 16:33:08ID and client secret and then it's also
  25696. 16:33:11going to give you something called
  25697. 16:33:12redirect URL. But why actually we need
  25698. 16:33:14Google client and Google secret. So
  25699. 16:33:16first of all what are you going to do
  25700. 16:33:17right? we need to create a new Google
  25701. 16:33:18instance using that client ID and client
  25702. 16:33:21secret and then once user is going to
  25703. 16:33:23click on this particular button. So now
  25704. 16:33:25we are going to use some packages or you
  25705. 16:33:27can directly use that URL also. So what
  25706. 16:33:28will happen? It will navigate this
  25707. 16:33:30particular user to the Google login
  25708. 16:33:32page. So that will be the first step and
  25709. 16:33:34then in the Google login page what we
  25710. 16:33:36are going to do right. So normally we
  25711. 16:33:37are going to create here uh that uh
  25712. 16:33:40something called we have a uh concept
  25713. 16:33:42called select account. So what will
  25714. 16:33:44happen? Let's say if you go to any
  25715. 16:33:45website right and let's say you have
  25716. 16:33:47click on this Google login page uh
  25717. 16:33:49Google login button you'll see that it
  25718. 16:33:50will go to the Google login page and
  25719. 16:33:52there let's say you have logged in using
  25720. 16:33:54two email accounts so what will happen
  25721. 16:33:56right you'll be able to see that okay
  25722. 16:33:57whatever email accounts that are
  25723. 16:33:59available for you that is going to show
  25724. 16:34:01you in this particular login page
  25725. 16:34:03correct and then let's say I wanted to
  25726. 16:34:04click on the first login uh first email
  25727. 16:34:07so once you click on this first email
  25728. 16:34:09what will happen right so it is going to
  25729. 16:34:11do some kind of authentication right and
  25730. 16:34:13then what will happen right? So you are
  25731. 16:34:15going to have some kind of a call back
  25732. 16:34:17URL. Now you might be thinking okay what
  25733. 16:34:18actually this call back URL or why
  25734. 16:34:20actually you need this call back URL.
  25735. 16:34:22Now to explain this one in very simpler
  25736. 16:34:23way you consider that actually you are
  25737. 16:34:26doing the work in your application but
  25738. 16:34:28when you're clicking on this Google
  25739. 16:34:30button you are actually moving away from
  25740. 16:34:32your application means you are not there
  25741. 16:34:33in this application. So what will happen
  25742. 16:34:35once the Google login is successful or
  25743. 16:34:37let's say it is failed. So this Google
  25744. 16:34:39login right or Google cloud console is
  25745. 16:34:42going to give you a call back URL and
  25746. 16:34:43that call back URL is going to come back
  25747. 16:34:45to your application and this call back
  25748. 16:34:47URL is going to hold some information.
  25749. 16:34:49So what information is going to hold?
  25750. 16:34:51It's actually going to actually verify
  25751. 16:34:52that whether this login is successful or
  25752. 16:34:55not. Okay, if the login is successful,
  25753. 16:34:57so what you're going to do, you will
  25754. 16:34:58then redirect the user back to your
  25755. 16:35:00application and let's redirect the user
  25756. 16:35:02to the success page. If this login is
  25757. 16:35:05failed, so in that case what you're
  25758. 16:35:06going to do, it will come back to your
  25759. 16:35:07application and then you're going to
  25760. 16:35:09redirect the user to the failure page.
  25761. 16:35:11So that means logically you need three
  25762. 16:35:13things. You need a client ID, you need a
  25763. 16:35:14client secret and then you need a
  25764. 16:35:16redirect URL. And that redirect URL we
  25765. 16:35:19are going to configure in our cloud
  25766. 16:35:20console. So to sum up in a simpler way,
  25767. 16:35:23we are going to have a Google login
  25768. 16:35:24button. We click on this Google login
  25769. 16:35:26button. We are going to configure this
  25770. 16:35:28one using Google client ID and client
  25771. 16:35:30secret. it will go to the Google uh
  25772. 16:35:32login page. There you'll be able to see
  25773. 16:35:34all the emails that is available for
  25774. 16:35:36you. Let's say you have three emails
  25775. 16:35:37that's possible and you want to register
  25776. 16:35:39using the first email or second email.
  25777. 16:35:41So you click on the first email. Then
  25778. 16:35:42what is going to do? It's going to do
  25779. 16:35:44your authentication process and then it
  25780. 16:35:46is going to go to your callback URL.
  25781. 16:35:48Your call back URL is going to actually
  25782. 16:35:50verify that authentication if that is
  25783. 16:35:51successful or not. And based on that you
  25784. 16:35:53will be back to your application again.
  25785. 16:35:55And then whatever the result if it is
  25786. 16:35:57success you are going to redirect the
  25787. 16:35:59user to the success page. If it is
  25788. 16:36:01failed you redirect the user to the
  25789. 16:36:03failed page. So this is what you're
  25790. 16:36:05going to do. So first of all what we
  25791. 16:36:06have to do right we have to search
  25792. 16:36:08something like Google cloud console.
  25793. 16:36:09Okay. So this is what you have to search
  25794. 16:36:11Google on Google and then you click on
  25795. 16:36:13this Google cloud console and first of
  25796. 16:36:16all what you have to do right you have
  25797. 16:36:17to create a new project here. Now I
  25798. 16:36:19already have so many projects. I'm going
  25799. 16:36:20to use this for NodeJS. This is I
  25800. 16:36:23usually use for my YouTube demos. So
  25801. 16:36:25what you're going to do right once you
  25802. 16:36:27create a simple project and then you're
  25803. 16:36:28going to go to this API services and
  25804. 16:36:31here what you have to do right. So first
  25805. 16:36:32of all you'll see that you're going to
  25806. 16:36:34get this credentials and in this uh
  25807. 16:36:37credentials what you have to do you have
  25808. 16:36:39to create a you see that you have to
  25809. 16:36:40create credentials and you have to
  25810. 16:36:42create a O client ID this client ID you
  25811. 16:36:44have to do. Okay. So now I already have
  25812. 16:36:46this one. So once you click on this O
  25813. 16:36:49client ID right? So you have to click on
  25814. 16:36:51this web client and here you can see
  25815. 16:36:54that you're going to have your client ID
  25816. 16:36:56and this will be your client secret and
  25817. 16:36:58you'll see that you have your origin. So
  25818. 16:36:59for now because we are actually so you
  25819. 16:37:01see that this is for use with request
  25820. 16:37:03from our browser mean when we're going
  25821. 16:37:05to have our UI and whatever UI you are
  25822. 16:37:07running let's say whatever port and you
  25823. 16:37:09are going to use that but in our case
  25824. 16:37:10because we are creating only back end
  25825. 16:37:12only projects what you can do right you
  25826. 16:37:14can change this one to local host 4,000
  25827. 16:37:16because we are running this one on 4,000
  25828. 16:37:18right so you see that we are running on
  25829. 16:37:204,000 the same we have to change this
  25830. 16:37:22one to 4,000 and this will be our
  25831. 16:37:25callback keyword. So in our callback U
  25832. 16:37:27model when you're going to configure we
  25833. 16:37:28are going to do this one as local host
  25834. 16:37:294,000 let's say if you're running this
  25835. 16:37:32on local host 5,000 so you have to just
  25836. 16:37:34configure based on that. So it will be
  25837. 16:37:36SL API/ Google/ callback. Okay so now
  25838. 16:37:40this will be a redirect URLs. Okay user
  25839. 16:37:42will be redirect to this path after they
  25840. 16:37:44have authenticated with Google and the
  25841. 16:37:45same thing I've already explained.
  25842. 16:37:47Correct. Now let's [snorts] save this.
  25843. 16:37:49So this is the first thing we are going
  25844. 16:37:50to do. So now we'll go here once again
  25845. 16:37:53and then we'll go to our URL sorry env.
  25846. 16:37:56Let's add our Google
  25847. 16:37:59client ID Google
  25848. 16:38:03client
  25849. 16:38:05secret and Google
  25850. 16:38:08call back
  25851. 16:38:10URL. Okay. So now first of all let's
  25852. 16:38:14copy this
  25853. 16:38:16paste it here. Then I'm just going to uh
  25854. 16:38:19disable this
  25855. 16:38:21and this en sorry I have to disable this
  25856. 16:38:24one and I'll delete this one.
  25857. 16:38:28And then let's add one more. And then
  25858. 16:38:30I'm just going to copy this. Okay. And
  25859. 16:38:34then I'll paste it here. And then here
  25860. 16:38:36I'm just going to use this one. So this
  25861. 16:38:39will be my uh redirect UR. So these
  25862. 16:38:42three things we need client ID, client
  25863. 16:38:43secret and callback URL. This I'm going
  25864. 16:38:45to delete later. uh so that you guys
  25865. 16:38:47can't use it but just create once uh
  25866. 16:38:50okay it's not that much difficult
  25867. 16:38:52because once you create it will be
  25868. 16:38:53easier for you to also test right so we
  25869. 16:38:56need client ID client secret and call by
  25870. 16:38:57keyword so now we'll go to our config uh
  25871. 16:39:01let's add all of this right so we'll do
  25872. 16:39:03Google client ID so I'll do check
  25873. 16:39:06require env
  25874. 16:39:09client secret so this this will be this
  25875. 16:39:14and then what we have we have Google
  25876. 16:39:16redirect
  25877. 16:39:18redirect URL.
  25878. 16:39:21So now let's go here and copy this
  25879. 16:39:26uh copy client ID
  25880. 16:39:30and we have copy client secret.
  25881. 16:39:34So save this. So this is the first step.
  25882. 16:39:37So now what I going to do right? So we
  25883. 16:39:39are going to use a package called Google
  25884. 16:39:41O library. So I'm just going to do
  25885. 16:39:43Google O library and let's see.
  25886. 16:39:48Now this is much more easier. Okay. And
  25887. 16:39:50that is the reason and see that it's
  25888. 16:39:51actually official supported official sub
  25889. 16:39:53authentication client library for
  25890. 16:39:55NodeJS. And this is the reason we are
  25891. 16:39:57going to use this and you'll see that
  25892. 16:39:58it's much more easier to use. So I'm
  25893. 16:40:00just going to copy this. Let's go here.
  25894. 16:40:03Right. So do npm
  25895. 16:40:08and this is official. Right. Okay. So,
  25896. 16:40:11we'll copy this and then we'll go and
  25897. 16:40:14let's install this one.
  25898. 16:40:21That's it. So, this is done. So, now
  25899. 16:40:23what are we going to do? So, first we're
  25900. 16:40:25going to create our Google helpers. So,
  25901. 16:40:28do Google dot ds. So first let's close
  25902. 16:40:31everything else and I'm going to import
  25903. 16:40:35something from so Google o library and
  25904. 16:40:39this will be my o to client. So first of
  25905. 16:40:42all what will be our Google user profile
  25906. 16:40:45means what all info we need. So first we
  25907. 16:40:47need a Google ID because see remember
  25908. 16:40:48when you're going to do a login
  25909. 16:40:50definitely Google is going to give you a
  25910. 16:40:51Google ID. Second we need a Google
  25911. 16:40:53email. Google email means the actual
  25912. 16:40:55email that the user is going to
  25913. 16:40:56register. Okay. So that means I'm just
  25914. 16:40:59going to create a type and you'll see
  25915. 16:41:00that this is actually not that much is
  25916. 16:41:03uh difficult to understand. So I'll do
  25917. 16:41:04Google user profile and I'm simply going
  25918. 16:41:07to do here Google ID
  25919. 16:41:11and this will be string and we are also
  25920. 16:41:14going to have a email and this will be
  25921. 16:41:15string. Okay. So next what will be the
  25922. 16:41:19uh step? So next we're going to create a
  25923. 16:41:20Google o client. So do Google o
  25924. 16:41:24client. So this will be new o client and
  25925. 16:41:28you need your uh here you're going to
  25926. 16:41:30pass sorry
  25927. 16:41:32you're simply going to pass directly. So
  25928. 16:41:34here you're going to do env that we have
  25929. 16:41:37dot google client id. So env dot google
  25930. 16:41:42client secret and your env dot google
  25931. 16:41:45client
  25932. 16:41:47google redirect.
  25933. 16:41:49Okay. So now let's save this. So first
  25934. 16:41:53of all what you have to do? So first uh
  25935. 16:41:55let's go to routes. Okay. So we are
  25936. 16:41:57going to come from routes because it
  25937. 16:41:59will be easier. So I'll go to O routes
  25938. 16:42:01because we are using O only here. So
  25939. 16:42:04first what I have to do right? So when
  25940. 16:42:06I'm going to go to this URL. So let's
  25941. 16:42:08I'll do / API/
  25942. 16:42:11Google. Now what will happen? See first
  25943. 16:42:14of all we don't have any UI correct but
  25944. 16:42:16in UI you are going to click on a button
  25945. 16:42:18or you're going to click on a link or
  25946. 16:42:19whatever you're going to do. Okay, let's
  25947. 16:42:21say clicking on any text whatever
  25948. 16:42:22continue with Google. So it will go to
  25949. 16:42:25the Google login page. See in our case
  25950. 16:42:26whenever we'll go to this particular or
  25951. 16:42:28we are going to hit this particular in
  25952. 16:42:30our browser localhost 4000/ API/op/
  25953. 16:42:33Google it should go to your Google login
  25954. 16:42:36page. So that means we are going to
  25955. 16:42:38redirect that particular uh page to the
  25956. 16:42:41Google login page. Correct? So now what
  25957. 16:42:43basically this we call as Google consent
  25958. 16:42:46screen. Right? So this is where you're
  25959. 16:42:48going to see your Google login page. But
  25960. 16:42:49how we are going to do this? So first
  25961. 16:42:51I'm just going to do a router dot get
  25962. 16:42:54and this will be / google and then I'm
  25963. 16:42:57just going to take so do request
  25964. 16:42:59response
  25965. 16:43:01and here we going to do con and I have
  25966. 16:43:03to create my Google or URL. Okay. So
  25967. 16:43:07here I'm just going to do start Google
  25968. 16:43:10login
  25969. 16:43:12and now what I have to do right this is
  25970. 16:43:14important. So we're going to do race and
  25971. 16:43:16it's going to give you this redirect. So
  25972. 16:43:17that means I'm actually redirecting from
  25973. 16:43:19my application to the Google login page
  25974. 16:43:22and this is what we're going to create.
  25975. 16:43:23So first we will copy this. We'll go
  25976. 16:43:25here and let's create this. So we'll do
  25977. 16:43:27export function
  25978. 16:43:29Google
  25979. 16:43:31what I'm doing man. So first we are
  25980. 16:43:34going to create start login. Right? So
  25981. 16:43:35in this one maybe let's do one thing. Uh
  25982. 16:43:38okay let's okay let's keep it here only.
  25983. 16:43:41Maybe I think this only makes sense. So
  25984. 16:43:43here I'm just going to do a string and
  25985. 16:43:45here I'm simply going to do return and
  25986. 16:43:48let's do get Google
  25987. 16:43:50O URL. So I'm going to create this. So
  25988. 16:43:54this is going to return a string and
  25989. 16:43:55here I'm simply going to create this
  25990. 16:43:57now. So what we going to do right? So
  25991. 16:43:59first maybe I'm doing wrong man. No no I
  25992. 16:44:02think this is not correct. Why? We
  25993. 16:44:04should not keep this one in this Google
  25994. 16:44:06page. So let's do one thing. This one I
  25995. 16:44:08think let's keep in the service file.
  25996. 16:44:10Okay. I'll tell you why. because this is
  25997. 16:44:12where you're calling the service right
  25998. 16:44:14so go to service go to all service sorry
  25999. 16:44:16my bad here we are going to do this so
  26000. 16:44:18we are going to keep this one here okay
  26001. 16:44:20so this is my service and now this one
  26002. 16:44:22we going to create in our helper so I'm
  26003. 16:44:25just going to do here export function
  26004. 16:44:28get google o url so here we'll take this
  26005. 16:44:31one as string and now what actually this
  26006. 16:44:33is going to return right so we are going
  26007. 16:44:35to take our Google o client and then
  26008. 16:44:37we're going to use the generate o url
  26009. 16:44:39okay now couple of things we need to uh
  26010. 16:44:42set here. So first we have to set
  26011. 16:44:43something called scope. So what we going
  26012. 16:44:45to set right? So here we are going to
  26013. 16:44:47basically tell that we need email plus
  26014. 16:44:50the profile information to identify the
  26015. 16:44:52user. Okay. And that user we are going
  26016. 16:44:54to store in our DB. Right? So that means
  26017. 16:44:56we are going to simply tell okay this
  26018. 16:44:58will be open ID. We also need email and
  26019. 16:45:00this will be profile.
  26020. 16:45:02Then we also need something called
  26021. 16:45:04access type. In this access type will be
  26022. 16:45:06online.
  26023. 16:45:08And then okay this is also optional.
  26024. 16:45:10Okay. So and this is optional actually
  26025. 16:45:12needed for this sorry this is optional
  26026. 16:45:14for refresh token. Okay now you actually
  26027. 16:45:16don't need in this particular one but
  26028. 16:45:18still you can keep it then we have
  26029. 16:45:19something called prompt. Now this is
  26030. 16:45:21important in this prompt means simply
  26031. 16:45:22you are going to tell this will be
  26032. 16:45:23select account. What this is going to do
  26033. 16:45:25right this is going to enforce the user
  26034. 16:45:27to select your email account. I already
  26035. 16:45:29mentioned it. Let's say you have
  26036. 16:45:30multiple email accounts. So it's going
  26037. 16:45:31to give you that which one you want to
  26038. 16:45:33select. So this is going to generate our
  26039. 16:45:35o URL. So this is the first thing we
  26040. 16:45:37have to do. So now we'll go to our o
  26041. 16:45:39routes right. So we have this start
  26042. 16:45:41google login that we are going to import
  26043. 16:45:42from our service. So from this
  26044. 16:45:44particular file and then in this service
  26045. 16:45:46we're going to get our o URL. So that
  26046. 16:45:49means this particular one we are going
  26047. 16:45:51to do. So this is the first thing we
  26048. 16:45:52have to do here. Now what will be the
  26049. 16:45:54next step? Next step will be we need to
  26050. 16:45:56handle our call back because I already
  26051. 16:45:58mentioned once we're going to click on
  26052. 16:45:59this account it's going to come back to
  26053. 16:46:01your call back URL and it's going to do
  26054. 16:46:03the authentication. Right? But before
  26055. 16:46:04this let's do one thing. Let's go here.
  26056. 16:46:06Right. I think it's already running. So
  26057. 16:46:08I'm just going to close and rerun rerun
  26058. 16:46:10once again. And then I'm going to copy
  26059. 16:46:12this and let's go here and then do this
  26060. 16:46:16slash. So you have API/
  26061. 16:46:20Google.
  26062. 16:46:21That's it. So this is where you're going
  26063. 16:46:23to see your selector means which account
  26064. 16:46:26sorry which account you are going to
  26065. 16:46:27select you're getting. So that means
  26066. 16:46:29this is now redirecting to your Google
  26067. 16:46:32login page. Okay. So now what will be
  26068. 16:46:35the next step? Next step will be we're
  26069. 16:46:36going to create our second route which
  26070. 16:46:39will be to handle our call back and this
  26071. 16:46:40is where actual work we are going to do
  26072. 16:46:42and this is where we are actually going
  26073. 16:46:44to get our access token and based on
  26074. 16:46:46this access token we are going to
  26075. 16:46:47authenticate the user. So now next will
  26076. 16:46:50be so I'm just going to do a router get
  26077. 16:46:53so this will be / google/allback
  26078. 16:46:57right we are using callback and then
  26079. 16:46:59we're going to do async. So this will be
  26080. 16:47:01request response
  26081. 16:47:04next
  26082. 16:47:06and first what we're going to do right
  26083. 16:47:08we're going to do try catch
  26084. 16:47:11so let's take error let's take like this
  26085. 16:47:16and here we're going to do next we're
  26086. 16:47:18going to pass error and now what we'll
  26087. 16:47:20do right so first I'm just going to do
  26088. 16:47:22code and this one you're going to get
  26089. 16:47:24from your query so I'll do request
  26090. 16:47:25doquery doc code so this is going to
  26091. 16:47:27give you a code as string or this will
  26092. 16:47:30be undefined. Okay. So now what we're
  26093. 16:47:33going to do, right? We are going to
  26094. 16:47:35create our another helper function where
  26095. 16:47:38we are actually going to do what? So we
  26096. 16:47:40are going to so this callback URL
  26097. 16:47:42whatever we are implementing this is
  26098. 16:47:45going to use that code that whatever we
  26099. 16:47:47are going to receive right and it's
  26100. 16:47:49going to return your profile data or
  26101. 16:47:50profile information.
  26102. 16:47:52So now let's do this one like this. So
  26103. 16:47:54I'm just going to do export sync
  26104. 16:47:56function and I'll do this one as uh what
  26105. 16:47:59are we going to do? So first of all
  26106. 16:48:01let's do one thing I think again I'm
  26107. 16:48:02doing wrong maybe let's uh first create
  26108. 16:48:05one more service because if first I'll
  26109. 16:48:07create the service and then we'll create
  26110. 16:48:09the helper. So first I'll do export as
  26111. 16:48:12sync
  26112. 16:48:13function and let's do login with Google.
  26113. 16:48:17Okay, login with Google and this is
  26114. 16:48:20going to receive the code which will be
  26115. 16:48:22string and here the return type will be
  26116. 16:48:24this will be promise of so we're going
  26117. 16:48:26to receive access token the actual
  26118. 16:48:28access token we're going to receive. So
  26119. 16:48:29access token and this will be string. So
  26120. 16:48:33now what are we going to do right? So
  26121. 16:48:35first let's do con Google profile and
  26122. 16:48:38this will be so first what are we going
  26123. 16:48:40to do right so first we need to verify
  26124. 16:48:42Google code and we need to get our
  26125. 16:48:44Google profile back right so we need to
  26126. 16:48:46extract the Google profile from our code
  26127. 16:48:48so here I'm just going to do a and let's
  26128. 16:48:51do get Google user
  26129. 16:48:55from o code okay so this will be our
  26130. 16:48:58method where we're going to pass this
  26131. 16:48:59code here now this one we're going to
  26132. 16:49:01create here okay so I was doing wrong
  26133. 16:49:03because first we should create the
  26134. 16:49:04service and then we will create here. So
  26135. 16:49:06this is going to receive the code and
  26136. 16:49:08this will be string and what this is
  26137. 16:49:10going to return now this is going to
  26138. 16:49:12return a promise and here we are going
  26139. 16:49:13to use this Google user profile because
  26140. 16:49:15we need the Google ID and the email.
  26141. 16:49:17Okay. So here we're going to use this
  26142. 16:49:19Google user profile type. So how we are
  26143. 16:49:21going to get this? So first you have to
  26144. 16:49:23give a check that if this code is not
  26145. 16:49:24present. So I'm just going to throw new
  26146. 16:49:27error
  26147. 16:49:29not new error what I'm doing app error.
  26148. 16:49:31So we're going to do an app error and
  26149. 16:49:32here we're going to do 400 and let's do
  26150. 16:49:35Google O code is required. So this will
  26151. 16:49:40be the first step. Okay. So now next
  26152. 16:49:42step will be first we're going to get
  26153. 16:49:44our something from this method. So I'm
  26154. 16:49:46just going to have it. So Google client
  26155. 16:49:49and this is going to give you this get
  26156. 16:49:50token. So you so what will happen right?
  26157. 16:49:52So that is the advantage of using that
  26158. 16:49:54package. So directly use this get token
  26159. 16:49:56method and you just have to pass the
  26160. 16:49:58code right. So here you have to get the
  26161. 16:50:00code right. This is going to get the
  26162. 16:50:01access token for the given code and this
  26163. 16:50:03code is nothing but the authorization
  26164. 16:50:05code. Okay. So here you're going to pass
  26165. 16:50:06this code and this is going to give you
  26166. 16:50:08the access token. So that means here
  26167. 16:50:10you're going to get the tokens. Okay. So
  26168. 16:50:12now what you'll do right this is
  26169. 16:50:13actually very important. This first of
  26170. 16:50:15all you have to check that in this token
  26171. 16:50:18if the [snorts] ID token is sorry if the
  26172. 16:50:19ID token is present or not. If this is
  26173. 16:50:22not present so that that means that
  26174. 16:50:24Google login failed. So we are going to
  26175. 16:50:26do app error.
  26176. 16:50:29So what will be the error code? So this
  26177. 16:50:30will be 401 not because I I thought it
  26178. 16:50:33will be 400. No, it will be 401. And
  26179. 16:50:36here we're going to do Google login
  26180. 16:50:37failed.
  26181. 16:50:39Uh what I can do ID token missing
  26182. 16:50:41something like this.
  26183. 16:50:43So this is the first thing and then
  26184. 16:50:45we're going to do what? So we're going
  26185. 16:50:46to verify the token and we're going to
  26186. 16:50:48read the user information from Google.
  26187. 16:50:50But how we are going to do this? So I'm
  26188. 16:50:52just going to do something like ticket
  26189. 16:50:54info. Okay. So here we'll do a Google O
  26190. 16:50:58client and then we'll do verify ID
  26191. 16:51:00token. Okay. And this is where you're
  26192. 16:51:02going to pass two things. So first will
  26193. 16:51:03be ID token which will be what tokens do
  26194. 16:51:05ID token and you're going to pass
  26195. 16:51:07audience. What will be your audience? So
  26196. 16:51:09audience you're going to verify from
  26197. 16:51:11your env. So in your env you have this
  26198. 16:51:13Google client ID. So from this client
  26199. 16:51:15ID, this will be your actual unique
  26200. 16:51:17client ID from where you're going to
  26201. 16:51:19verify this. So this will be your Google
  26202. 16:51:21client ID. Okay. So this is going to
  26203. 16:51:24give you the ticket information and then
  26204. 16:51:25what you're going to do right you're
  26205. 16:51:26going to do con payload and this will be
  26206. 16:51:30ticket info dot get payload and this is
  26207. 16:51:33is going to give you the information.
  26208. 16:51:35Okay now what we're going to do right
  26209. 16:51:37simply I'm simply going to check if this
  26210. 16:51:39payload dot sub or if this payload do
  26211. 16:51:44both are not present. So I'm simply
  26212. 16:51:46going to do again throw new app error
  26213. 16:51:50401
  26214. 16:51:51and let's do Google login field
  26215. 16:51:56Google login field and this will be
  26216. 16:51:58invalid user profile
  26217. 16:52:02and we're going to return Google ID
  26218. 16:52:05which will be your payload dot sub and
  26219. 16:52:08you're going to pass the email and this
  26220. 16:52:10will be your payload email but what you
  26221. 16:52:12should do right you should do it too
  26222. 16:52:14lower case I usually to do all this and
  26223. 16:52:16then also you're going to pass a claim
  26224. 16:52:18here. So now what will happen right? So
  26225. 16:52:20first of all let's go through. So first
  26226. 16:52:22is checking the code then it's getting
  26227. 16:52:24the token using this particular method.
  26228. 16:52:25It will easily give you the token. We
  26229. 16:52:27are simply checking the ID token present
  26230. 16:52:29or not. If it is present we are
  26231. 16:52:30verifying the token. We getting the
  26232. 16:52:32ticket info the user info. This is
  26233. 16:52:34nothing but your user info. Okay. So
  26234. 16:52:36this will be your user info
  26235. 16:52:39received from Google. And then we're
  26236. 16:52:42going to get our payload. If this
  26237. 16:52:45payload sub and email is not present. So
  26238. 16:52:47we're going to return this or else. So
  26239. 16:52:49here what we so you're going to return
  26240. 16:52:51the Google ID and the email. So save
  26241. 16:52:53this. So now we'll go to our o routes.
  26242. 16:52:56So first of all what we doing right? So
  26243. 16:52:58first so in this callback we're getting
  26244. 16:53:00the code and then what I'll do right. So
  26245. 16:53:02first I'll do con
  26246. 16:53:04something and we're going to call this
  26247. 16:53:06service right. So this login with
  26248. 16:53:08Google. So do a so login with Google and
  26249. 16:53:12here we're going to pass the code or
  26250. 16:53:14else this will be empty and what you're
  26251. 16:53:16going to receive you're going to receive
  26252. 16:53:17the access token right and this access
  26253. 16:53:19token you're going to return at the end
  26254. 16:53:21so do raise dot status so this will be
  26255. 16:53:24200 dot JSON and here we're going to do
  26256. 16:53:28success will be true and data will be
  26257. 16:53:32this access token so this is done but
  26258. 16:53:35not yet done because first of all we
  26259. 16:53:36have to create our service right So
  26260. 16:53:38first we're going to get our Google
  26261. 16:53:40information from this method that we
  26262. 16:53:42created. So to get use Google user from
  26263. 16:53:44O code. So just import this.
  26264. 16:53:48And now first of all let's do one thing.
  26265. 16:53:49So a couple of things you have to do. So
  26266. 16:53:51first see this is where you have to
  26267. 16:53:55understand some of the things. So first
  26268. 16:53:56we have to what we have to do right. So
  26269. 16:53:58first you have to find this particular
  26270. 16:54:00user from uh find this particular user
  26271. 16:54:03by Google ID first. And what I mean by
  26272. 16:54:05this and this particular method we are
  26273. 16:54:07going to write in our repository. So
  26274. 16:54:08first we have to see if that particular
  26275. 16:54:10thing so not particular thing if that
  26276. 16:54:12particular user is already present or
  26277. 16:54:14not. Okay. So first what I'll do right
  26278. 16:54:16simply so let's do one thing. So I'll do
  26279. 16:54:19let user and I'm going to have it I'll
  26280. 16:54:23do find user by Google ID. So the first
  26281. 16:54:28thing and here what I'll do I'll do
  26282. 16:54:30Google profile and see this is going to
  26283. 16:54:32give you the Google ID right. So now
  26284. 16:54:35what I can do right I can copy this and
  26285. 16:54:37let's go to the repository
  26286. 16:54:42and this is where we are going to
  26287. 16:54:44implement this one right so first let's
  26288. 16:54:46start with the so this will be export as
  26289. 16:54:49sync function this will be find user by
  26290. 16:54:53Google ID and you're going to receive
  26291. 16:54:54the Google ID
  26292. 16:54:57this will be string and this is going to
  26293. 16:54:59return promise so either it's going to
  26294. 16:55:01return your user information or else
  26295. 16:55:03it's going to return null. So first I'm
  26296. 16:55:05just going to do con result. So this
  26297. 16:55:07will be a pull dot query.
  26298. 16:55:11So this will be my user row. No, I think
  26299. 16:55:14we have DB user row. Correct? We have DB
  26300. 16:55:16user row. And here we are going to use.
  26301. 16:55:22So we'll do select id email ro
  26302. 16:55:28and let's do created at all these from
  26303. 16:55:32users and now what we'll do where so
  26304. 16:55:35let's go here and let's go to user and
  26305. 16:55:38see that we have this Google ID see
  26306. 16:55:39everything is linked so now I can simply
  26307. 16:55:41check if this Google ID is equal to this
  26308. 16:55:45right and then we going to pass this
  26309. 16:55:47Google ID here so this is going to give
  26310. 16:55:48me the information if that is present or
  26311. 16:55:50not
  26312. 16:55:52So I'm going to return result dot rows
  26313. 16:55:54of zero
  26314. 16:55:56or else null.
  26315. 16:55:59So save this. Now I'll go to our
  26316. 16:56:01service. So this will be first we are
  26317. 16:56:03finding the user right. If this user is
  26318. 16:56:06not present. So what are we going to do?
  26319. 16:56:08So first first let's see if this is not
  26320. 16:56:11present. So in that case what we're
  26321. 16:56:13going to do right. So we are actually
  26322. 16:56:14going to check if this particular email
  26323. 16:56:17is already exist or not. So that means
  26324. 16:56:20what I mean by [snorts] this now this is
  26325. 16:56:21actually very important what I'm trying
  26326. 16:56:23to say. So let's say that is one
  26327. 16:56:25particular user right and if this
  26328. 16:56:28particular user already logged in with a
  26329. 16:56:31certain email and the same user try to
  26330. 16:56:34log in another time using Google login.
  26331. 16:56:37So that means what I'm trying to say. So
  26332. 16:56:39let's say if I go here I'm not sure if
  26333. 16:56:41you're getting so let's say same user a
  26334. 16:56:44user already registered with one
  26335. 16:56:46particular email. Okay. So it's like
  26336. 16:56:49email ABC.com
  26337. 16:56:50whatever it is and the same user is try
  26338. 16:56:54to use or try to use the Google login
  26339. 16:56:57and try to login with the same email. So
  26340. 16:56:59that should not happen. So in that case
  26341. 16:57:01what I'm going to do right. So first
  26342. 16:57:02let's import this. So here I'm just
  26343. 16:57:04going to do user and this will be a and
  26344. 16:57:07I'm just going to do find user by email
  26345. 16:57:10now. So this is the first thing and here
  26346. 16:57:12I'm just going to do Google profile do
  26347. 16:57:14email and this will be another method we
  26348. 16:57:16are going to create where we are going
  26349. 16:57:18to create in our find user by email
  26350. 16:57:20right I think find user by email we
  26351. 16:57:22already have
  26352. 16:57:24find user by email okay this one we can
  26353. 16:57:26use so this is what we going to use here
  26354. 16:57:28so I'm simply going to do user will be
  26355. 16:57:30find user by email and now what I'm
  26356. 16:57:33going to do I'm simply going to check if
  26357. 16:57:34this user is present so in this case
  26358. 16:57:38there are two option so first one will
  26359. 16:57:40be we need to link this Google account
  26360. 16:57:42to the existing user or second if this
  26361. 16:57:46is a first time login so in that case we
  26362. 16:57:48need to create the user in the DB I know
  26363. 16:57:50this is little complicated so what I
  26364. 16:57:51mean by this if this user you are
  26365. 16:57:53already getting so you need to link this
  26366. 16:57:56user because this user is again try to
  26367. 16:57:58login using Google so you need to link
  26368. 16:58:00this user or link this Google account
  26369. 16:58:04to the existing
  26370. 16:58:07email user Okay. And the second scenario
  26371. 16:58:11will be if this is not the case. So in
  26372. 16:58:13that case if it is a first time Google
  26373. 16:58:15login
  26374. 16:58:16so we need to create the user
  26375. 16:58:22in postgress. So that means it's the
  26376. 16:58:24first time login and it say u new email.
  26377. 16:58:28So in that case we have to create this
  26378. 16:58:29user. So that means the first one will
  26379. 16:58:31be user will be equal to a and I'll do
  26380. 16:58:34link Google
  26381. 16:58:36ID to user. And here we're going to pass
  26382. 16:58:40this user do ID and I'm going to pass
  26383. 16:58:42the Google profile dot Google ID both.
  26384. 16:58:46Okay. So this is if the user is pres
  26385. 16:58:48already present. If this is not present
  26386. 16:58:50so else case. So we're going to create a
  26387. 16:58:52new user. So here I'm just going to do
  26388. 16:58:54user will be have it create Google
  26389. 16:58:59user. So here I need the Google profile
  26390. 16:59:02email and Google profile. Google ID.
  26391. 16:59:06Okay. So this is what we going to do. So
  26392. 16:59:08now what you can do right you can create
  26393. 16:59:10both. So I'll do link Google to user.
  26394. 16:59:15So we'll do export as sync function. So
  26395. 16:59:18I'll do link
  26396. 16:59:20uh Google to user. So for this one what
  26397. 16:59:23we going to do right? We're going to
  26398. 16:59:24what we're sending here user ID and
  26399. 16:59:27Google ID. So do user ID. This will be
  26400. 16:59:30string and this will be Google ID. This
  26401. 16:59:33will be also string. And here we're
  26402. 16:59:35going to return promise and this will be
  26403. 16:59:38user.
  26404. 16:59:40So I'll do con this will be result and
  26405. 16:59:43here we're going to do a pool dot query.
  26406. 16:59:47So this will be DB user
  26407. 16:59:51no pool dotquery DV user row and here
  26408. 16:59:55we're going to now do what we going to
  26409. 16:59:57link this user. How we going to link?
  26410. 16:59:59We're going to update. So we're going to
  26411. 17:00:00update the user and then I'm just going
  26412. 17:00:03to do set. So I'll do set Google ID
  26413. 17:00:08equal to this and also I'm just going to
  26414. 17:00:12do updated at. So I'll do updated at
  26415. 17:00:15equal to now
  26416. 17:00:18where we need to check right. So where
  26417. 17:00:20what where the user ID is this. So that
  26418. 17:00:22means where your ID is equal to this. So
  26419. 17:00:25this ID is nothing but your user ID this
  26420. 17:00:28one. Okay. So this is what and then at
  26421. 17:00:31the end I'm just going to do return in
  26422. 17:00:33ID email
  26423. 17:00:36role and here I'm just going to do
  26424. 17:00:38created add
  26425. 17:00:40and now I can simply pass this. So first
  26426. 17:00:42one will be my Google ID and second one
  26427. 17:00:45will be my user ID
  26428. 17:00:47and then we're going to return result
  26429. 17:00:49dot rows of zero. So save this. So now
  26430. 17:00:53this is going to link your existing user
  26431. 17:00:56sorry link this Google account to the
  26432. 17:00:58existing email user. if this user is
  26433. 17:01:00already present. Okay, so this is what
  26434. 17:01:02we're going to import. If this user is
  26435. 17:01:03not present, if this is a first time
  26436. 17:01:05login, we're going to create the Google
  26437. 17:01:06user. Okay, so here this will be. So
  26438. 17:01:09let's go here. So I'm just going to do
  26439. 17:01:11export as sync function. So do create
  26440. 17:01:16Google user. So what are the things we
  26441. 17:01:19are sending? We sending email and Google
  26442. 17:01:22ID. So do email string
  26443. 17:01:26Google id string. What this is going to
  26444. 17:01:30return again it's going to return
  26445. 17:01:32promise user.
  26446. 17:01:34So now so this will be con result.
  26447. 17:01:38So this will be a pull dot query
  26448. 17:01:44and here we are going to simply do.
  26449. 17:01:47So this will be add right. So we are
  26450. 17:01:49going to insert. So insert into users
  26451. 17:01:54uh no sorry insert into users. So you
  26452. 17:01:56need to mention what you want to insert.
  26453. 17:01:58So you need to insert email and the
  26454. 17:02:01Google ID. Right? So you have to do
  26455. 17:02:02Google ID. I'm not sure what is that
  26456. 17:02:05Google ID.
  26457. 17:02:08Okay. And then we're going to pass the
  26458. 17:02:10values. So values will be your 1 comma
  26459. 17:02:122. And then you're going to do
  26460. 17:02:14returning. So we're going to do id email
  26461. 17:02:17role
  26462. 17:02:20and we're going to do created add.
  26463. 17:02:24So I think that's it. And then we're
  26464. 17:02:26going to pass the email
  26465. 17:02:28and the Google ID
  26466. 17:02:31and again the same thing. So do result
  26467. 17:02:34dot rows of zero. So save this. So now
  26468. 17:02:39what we're going to do right we're going
  26469. 17:02:40to simply import this one here. So if
  26470. 17:02:43you can understand all of this right. So
  26471. 17:02:45I think you'll see that it's not that
  26472. 17:02:47much difficult. Okay, what we getting?
  26473. 17:02:48We have to return the token that is
  26474. 17:02:50fine. So that means first we're checking
  26475. 17:02:51if this uh so here first we are finding
  26476. 17:02:54this user right and here if this is this
  26477. 17:02:57not equal to true then we have try to
  26478. 17:02:59find the user from email. So first we
  26479. 17:03:01try to find the user using Google ID. If
  26480. 17:03:04this is not the case so in that case
  26481. 17:03:06we'll try to find the user using email.
  26482. 17:03:08If this user is already present we're
  26483. 17:03:10going to link that user. If that not
  26484. 17:03:12present first time login we're going to
  26485. 17:03:13create the user. Okay. So now what I can
  26486. 17:03:16do right where is that this one. So
  26487. 17:03:18after this I'm just going to do con. So
  26488. 17:03:21do access token and this will be
  26489. 17:03:25so create access token. I think we don't
  26490. 17:03:27have created this one. Right? So we're
  26491. 17:03:29going to create this one. So let's do
  26492. 17:03:31one thing. I'm simply going to do
  26493. 17:03:33function. Let's do create access token
  26494. 17:03:38for user. And here I'm just going to get
  26495. 17:03:41the user. And this will be my user type.
  26496. 17:03:44And this is going to return a string.
  26497. 17:03:46And here I'm just going to do return
  26498. 17:03:49sign access token.
  26499. 17:03:51Sign access token will be
  26500. 17:03:54this one. Right? So here we're going to
  26501. 17:03:55pass the sign access token. And we're
  26502. 17:03:58going to pass your user ID which will be
  26503. 17:04:00your user do ID. Email will be user do
  26504. 17:04:05email and role will be user dot ro.
  26505. 17:04:11Okay. So this will be what we're going
  26506. 17:04:12to do. So this is going to create your
  26507. 17:04:14user information, pass the user and at
  26508. 17:04:17the end you're going to return the
  26509. 17:04:18access token.
  26510. 17:04:21And this is done. Now this is actually a
  26511. 17:04:24long process but if you take some time
  26512. 17:04:26and try to understand everything is
  26513. 17:04:28linked. So if you go here right I try to
  26514. 17:04:30understand. So let's see it will go to
  26515. 17:04:32Google with login sorry Google with
  26516. 17:04:34login with Google first. Try to f the
  26517. 17:04:37Google profile. So here we are simply
  26518. 17:04:40checking based on the authorization code
  26519. 17:04:42we're going to take our tokens get the
  26520. 17:04:44ticket info or the user info uh get the
  26521. 17:04:47payload and return the Google ID and the
  26522. 17:04:49email once it will get the Google uh
  26523. 17:04:52Google ID and the email it will try to
  26524. 17:04:53find the user if this user is not
  26525. 17:04:56present it will try to find the user by
  26526. 17:04:58the email if this user is present with
  26527. 17:05:00the email it's going to link this Google
  26528. 17:05:02account or else it's going to create the
  26529. 17:05:04user for the first step then it will
  26530. 17:05:06create the token and return the token
  26531. 17:05:08And that's it. So now let's see if I try
  26532. 17:05:11to go to let's say localhost 4,000
  26533. 17:05:16uh 4,000/ API/
  26534. 17:05:19Google and let's I try to click on this
  26535. 17:05:22email.
  26536. 17:05:25That's it. And you can see that you're
  26537. 17:05:27getting data and this is your access
  26538. 17:05:29token, right? And what you can do that
  26539. 17:05:31you can verify this also if this is
  26540. 17:05:33working or not. So what I'll do, right?
  26541. 17:05:35So let's see if I go here. Not this.
  26542. 17:05:38Let's clear this. I think I've added
  26543. 17:05:40this one in some place. Let me just
  26544. 17:05:42check. Okay, this one. Let's copy this
  26545. 17:05:45and let's execute this here. And now I'm
  26546. 17:05:48just going to do select ID, email, and
  26547. 17:05:50ro from user. Okay, paste this. Enter.
  26548. 17:05:54And now we'll see that you have a
  26549. 17:05:56samukhargi 2022
  26550. 17:05:58at thegmail.com. So that means these
  26551. 17:06:01user created using Google login. These
  26552. 17:06:03two user created using normal login and
  26553. 17:06:05this is manual admin. So that means with
  26554. 17:06:07this you can understand every possible
  26555. 17:06:10way that how you can create user. Okay.
  26556. 17:06:13So with this now Google login is
  26557. 17:06:15completed and with this we have finally
  26558. 17:06:18completed this particular video. I think
  26559. 17:06:20we don't have that much to cover as I
  26560. 17:06:22already mentioned that we I'm going to
  26561. 17:06:24cover uh so many other things also in
  26562. 17:06:27the part three but see that is not going
  26563. 17:06:28to come anytime soon but that will come
  26564. 17:06:31in the part three. uh I'm going to cover
  26565. 17:06:33very advanced concept like worker
  26566. 17:06:35threads and all these child processes.
  26567. 17:06:37Uh there will be some more advanced
  26568. 17:06:39topic like cookies and everything
  26569. 17:06:40deployment and everything. But for now I
  26570. 17:06:43already have multiple deployment videos.
  26571. 17:06:44You can watch those videos. But with
  26572. 17:06:47this that's all for this particular
  26573. 17:06:49video. I will see you in my next video.
  26574. 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.