All language subtitles for [English (auto-generated)] Learn NestJS – Complete Course [DownSub.com]

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,040 --> 00:00:02,159 learn the fundamentals of nestjs by 2 00:00:02,159 --> 00:00:05,279 building the back end of a Spotify clone 3 00:00:05,279 --> 00:00:07,720 nestjs is a framework for building 4 00:00:07,720 --> 00:00:10,040 efficient scalable node.js web 5 00:00:10,040 --> 00:00:12,080 applications you'll learn how to design 6 00:00:12,080 --> 00:00:14,799 a database create rest apis and deploy 7 00:00:14,799 --> 00:00:17,400 the apis to production how did Alie 8 00:00:17,400 --> 00:00:18,880 develop this 9 00:00:18,880 --> 00:00:22,080 course welcome to my course 10 00:00:22,080 --> 00:00:25,599 nestjs let's talk about what is 11 00:00:25,599 --> 00:00:29,199 nestjs nestjs is used to build server 12 00:00:29,199 --> 00:00:32,640 side application in node.js it is built 13 00:00:32,640 --> 00:00:34,760 on the top of node.js 14 00:00:34,760 --> 00:00:37,640 expressjs and 15 00:00:37,640 --> 00:00:40,239 typescript the major difference between 16 00:00:40,239 --> 00:00:44,239 nestjs and other nodejs framework is 17 00:00:44,239 --> 00:00:46,960 this framework is using 18 00:00:46,960 --> 00:00:50,559 typescript and it is using the angular 19 00:00:50,559 --> 00:00:54,079 architecture angular Concepts on the 20 00:00:54,079 --> 00:00:57,120 pack if you familiar with angular 21 00:00:57,120 --> 00:01:00,600 concept you can work with nestjs is very 22 00:01:00,600 --> 00:01:03,680 easily why they are using the angular 23 00:01:03,680 --> 00:01:08,240 concept because if you use the angular 24 00:01:08,240 --> 00:01:11,479 architecture Concept in the back end you 25 00:01:11,479 --> 00:01:16,080 can organize your project in a easy way 26 00:01:16,080 --> 00:01:18,000 the development would be 27 00:01:18,000 --> 00:01:20,759 easy the contribution for other 28 00:01:20,759 --> 00:01:23,920 developers would be easy in 29 00:01:23,920 --> 00:01:26,240 sjs look at the 30 00:01:26,240 --> 00:01:28,119 documentation you will find the 31 00:01:28,119 --> 00:01:32,159 controllers providers modules middleware 32 00:01:32,159 --> 00:01:35,079 exception filter pipes guards inter 33 00:01:35,079 --> 00:01:37,479 interceptors and custom decorators all 34 00:01:37,479 --> 00:01:40,040 of these are Concepts in 35 00:01:40,040 --> 00:01:43,640 angular why they created nests if you 36 00:01:43,640 --> 00:01:47,320 have worked with Express you don't have 37 00:01:47,320 --> 00:01:50,560 any structure like you don't have any 38 00:01:50,560 --> 00:01:53,960 best practice to organize the backend 39 00:01:53,960 --> 00:01:57,079 project so they created the structure 40 00:01:57,079 --> 00:01:59,960 architecture level they designed the 41 00:01:59,960 --> 00:02:02,399 architecture to build server side 42 00:02:02,399 --> 00:02:05,479 applications in nodejs and 43 00:02:05,479 --> 00:02:08,479 typescript under the it is using 44 00:02:08,479 --> 00:02:11,759 expressjs and 45 00:02:14,640 --> 00:02:17,480 typescript so they are just solving the 46 00:02:17,480 --> 00:02:20,040 architecture problem when you build the 47 00:02:20,040 --> 00:02:23,959 large application in nodejs we backend 48 00:02:23,959 --> 00:02:25,760 web application in 49 00:02:25,760 --> 00:02:28,599 nodejs when your code base is increasing 50 00:02:28,599 --> 00:02:32,640 day by day it's hard to follow specific 51 00:02:32,640 --> 00:02:35,879 architecture that's why they have 52 00:02:35,879 --> 00:02:37,000 created the 53 00:02:37,000 --> 00:02:39,560 nestjs if you follow the nests you will 54 00:02:39,560 --> 00:02:41,560 see hey we need a controller we need a 55 00:02:41,560 --> 00:02:44,640 provider we need modules we can break 56 00:02:44,640 --> 00:02:46,840 application into multiple modules we 57 00:02:46,840 --> 00:02:48,720 need a middleware we need exception 58 00:02:48,720 --> 00:02:51,840 filters so this is how you can architect 59 00:02:51,840 --> 00:02:53,800 your backend 60 00:02:53,800 --> 00:02:56,640 application this is exciting project I'm 61 00:02:56,640 --> 00:02:58,840 going to share you all of these Concepts 62 00:02:58,840 --> 00:03:01,840 in this course 63 00:03:02,319 --> 00:03:05,840 let's create a new project in 64 00:03:05,840 --> 00:03:09,319 njs first of all you need to install ljs 65 00:03:09,319 --> 00:03:11,920 globally on your machine I'm going to 66 00:03:11,920 --> 00:03:15,920 install nestjs CLI this this command 67 00:03:15,920 --> 00:03:20,360 will help us to generate nests 68 00:03:20,840 --> 00:03:25,080 project my CLI has installed now we need 69 00:03:25,080 --> 00:03:27,519 to create a new project you can use this 70 00:03:27,519 --> 00:03:31,120 command Nest new the name of the project 71 00:03:31,120 --> 00:03:34,840 you can choose any name 72 00:03:37,879 --> 00:03:41,439 here I just use in fundamentals dpro I'm 73 00:03:41,439 --> 00:03:42,250 going to use 74 00:03:42,250 --> 00:03:45,000 [Music] 75 00:03:45,000 --> 00:03:47,159 npm now it is going to it's going to 76 00:03:47,159 --> 00:03:50,920 install all of these packages for 77 00:03:50,920 --> 00:03:54,480 me now my project has created 78 00:03:54,480 --> 00:03:57,120 successfully so I'm going to navigate to 79 00:03:57,120 --> 00:03:58,400 my project 80 00:03:58,400 --> 00:04:02,640 directory this is end fundamentals Das 81 00:04:02,640 --> 00:04:05,959 Pro now I'm going to open that into my 82 00:04:05,959 --> 00:04:10,120 code editor I'm going to use vs 83 00:04:11,200 --> 00:04:14,879 code so when you open the package.json 84 00:04:14,879 --> 00:04:17,560 file you can run your project in 85 00:04:17,560 --> 00:04:21,040 development mode by using this command 86 00:04:21,040 --> 00:04:22,600 npm start 87 00:04:22,600 --> 00:04:25,479 Dev npm Run 88 00:04:25,479 --> 00:04:28,479 start 89 00:04:28,720 --> 00:04:31,639 D 90 00:04:31,639 --> 00:04:34,919 you can see my project has started 91 00:04:34,919 --> 00:04:39,080 successfully uh by default njs uses 3000 92 00:04:39,080 --> 00:04:42,639 Port you can verify that from the main. 93 00:04:42,639 --> 00:04:45,680 file I'm going to go to the Local Host 94 00:04:45,680 --> 00:04:46,639 call in 95 00:04:46,639 --> 00:04:50,479 3000 let's see what will 96 00:04:52,000 --> 00:04:55,440 happen you can see Hello 97 00:04:55,440 --> 00:05:00,240 World so our project is running fine now 98 00:05:00,240 --> 00:05:02,440 in the app controller you will have a 99 00:05:02,440 --> 00:05:05,479 simple message the simple controller 100 00:05:05,479 --> 00:05:08,919 method it's a route get 101 00:05:08,919 --> 00:05:12,000 hello and you will see the hello world 102 00:05:12,000 --> 00:05:16,479 here hello world or you can say hello 103 00:05:16,479 --> 00:05:19,080 I'm learning 104 00:05:19,080 --> 00:05:21,800 nestjs I saved the application it has 105 00:05:21,800 --> 00:05:23,240 reloaded 106 00:05:23,240 --> 00:05:25,680 successfully let me send the request 107 00:05:25,680 --> 00:05:28,639 again you will see hello I'm learning 108 00:05:28,639 --> 00:05:31,639 njs 109 00:05:31,680 --> 00:05:35,720 so this is our source folder you will 110 00:05:35,720 --> 00:05:38,160 place all of your Source folder source 111 00:05:38,160 --> 00:05:40,960 code inside the source folder this is 112 00:05:40,960 --> 00:05:44,160 our main.ts file when you run this 113 00:05:44,160 --> 00:05:48,880 command npm run Start Da njs is going to 114 00:05:48,880 --> 00:05:52,120 call this bootstrap function and inside 115 00:05:52,120 --> 00:05:55,240 the bootstrap we have a factory function 116 00:05:55,240 --> 00:05:57,960 and we are initializing the app module 117 00:05:57,960 --> 00:06:00,199 this is our root module in here we have 118 00:06:00,199 --> 00:06:03,240 set the port we have the service we have 119 00:06:03,240 --> 00:06:06,880 module controller and the test file for 120 00:06:06,880 --> 00:06:09,800 a controller this is our module remember 121 00:06:09,800 --> 00:06:12,520 this is the angular structure and each 122 00:06:12,520 --> 00:06:15,840 module is consist each module consist of 123 00:06:15,840 --> 00:06:19,840 controllers services and providers you 124 00:06:19,840 --> 00:06:23,319 can import multiple modules here we have 125 00:06:23,319 --> 00:06:23,780 the 126 00:06:23,780 --> 00:06:25,240 [Music] 127 00:06:25,240 --> 00:06:28,120 service and uh you will place all of 128 00:06:28,120 --> 00:06:30,680 your spec file inside the test end to 129 00:06:30,680 --> 00:06:34,960 end test should be uh saved here and 130 00:06:34,960 --> 00:06:37,759 unit testing you unit testing files will 131 00:06:37,759 --> 00:06:41,680 be located here this is eslint RC rules 132 00:06:41,680 --> 00:06:44,639 for eslint prettier 133 00:06:44,639 --> 00:06:48,560 n if you want to customize the nest C 134 00:06:48,560 --> 00:06:50,919 nestjs project you will do inside the 135 00:06:50,919 --> 00:06:54,360 nest CLI we have package.json file we 136 00:06:54,360 --> 00:06:57,280 have TS config and TS config build this 137 00:06:57,280 --> 00:07:01,520 is used for typescript configurations 138 00:07:01,520 --> 00:07:04,759 let's talk about the concept of module 139 00:07:04,759 --> 00:07:06,039 in 140 00:07:06,039 --> 00:07:11,240 njs in njs each module is consist of 141 00:07:11,240 --> 00:07:15,680 providers controllers imports and 142 00:07:15,680 --> 00:07:18,680 exports so this is the building block of 143 00:07:18,680 --> 00:07:21,720 nestjs application you can read my notes 144 00:07:21,720 --> 00:07:24,639 I have provided you the PDF 145 00:07:24,639 --> 00:07:27,919 documentation nest in providers nestjs 146 00:07:27,919 --> 00:07:30,440 are classes that act as a Services 147 00:07:30,440 --> 00:07:32,199 factories or 148 00:07:32,199 --> 00:07:34,479 repositories if you have worked with 149 00:07:34,479 --> 00:07:37,879 angular you know the 150 00:07:37,879 --> 00:07:40,599 providers so you can encapsulate your 151 00:07:40,599 --> 00:07:43,240 business logic and it can be injected 152 00:07:43,240 --> 00:07:46,400 into controller or other 153 00:07:46,400 --> 00:07:49,000 services so controller serve as the 154 00:07:49,000 --> 00:07:52,599 function of handling HTTP request 155 00:07:52,599 --> 00:07:55,479 whenever you create a new endpoint you 156 00:07:55,479 --> 00:07:58,440 will register it inside the 157 00:07:58,440 --> 00:08:00,960 controller I'll teach you more about 158 00:08:00,960 --> 00:08:04,440 controllers in the next video Imports is 159 00:08:04,440 --> 00:08:07,919 an array specify the external modules 160 00:08:07,919 --> 00:08:10,720 needed for the current module you can 161 00:08:10,720 --> 00:08:13,919 import other module let's say you have 162 00:08:13,919 --> 00:08:16,240 break down your application e-commerce 163 00:08:16,240 --> 00:08:19,280 application into multiple modules you 164 00:08:19,280 --> 00:08:21,720 have post module you have products 165 00:08:21,720 --> 00:08:24,520 module you have ERS module you have 166 00:08:24,520 --> 00:08:29,080 users module let's say a user module can 167 00:08:29,080 --> 00:08:30,240 use 168 00:08:30,240 --> 00:08:33,039 the product module this is how you think 169 00:08:33,039 --> 00:08:36,599 in terms of modules if you want to 170 00:08:36,599 --> 00:08:40,120 export you can also export controllers 171 00:08:40,120 --> 00:08:43,559 and services from the current 172 00:08:43,559 --> 00:08:47,480 modules so each nestjs application will 173 00:08:47,480 --> 00:08:50,839 have a root module just like in angular 174 00:08:50,839 --> 00:08:53,320 root module serve as the entry point and 175 00:08:53,320 --> 00:08:55,600 is responsible for instantiating 176 00:08:55,600 --> 00:08:58,440 controllers providers and other core 177 00:08:58,440 --> 00:09:00,959 elements 178 00:09:00,959 --> 00:09:03,920 nextjs follow the module isolation 179 00:09:03,920 --> 00:09:07,000 principle ensuring that application is 180 00:09:07,000 --> 00:09:09,880 organized into distinct functional or 181 00:09:09,880 --> 00:09:12,920 feature based modules so you will have 182 00:09:12,920 --> 00:09:16,680 root module app module let me check the 183 00:09:16,680 --> 00:09:19,440 application you will have the app module 184 00:09:19,440 --> 00:09:23,079 you can see that you have Imports array 185 00:09:23,079 --> 00:09:28,079 this module has app controller and app 186 00:09:28,079 --> 00:09:32,040 service let's say we're going to build a 187 00:09:32,040 --> 00:09:35,120 back end of Spotify application we can 188 00:09:35,120 --> 00:09:37,680 divide our application into multiple 189 00:09:37,680 --> 00:09:41,120 modules you will have a root module and 190 00:09:41,120 --> 00:09:43,680 I'll have the artist which is feature 191 00:09:43,680 --> 00:09:47,320 module and we will have songs module to 192 00:09:47,320 --> 00:09:50,120 handle the songs functionality we have o 193 00:09:50,120 --> 00:09:52,800 module to handle authentication these 194 00:09:52,800 --> 00:09:56,600 three are all the feature 195 00:09:56,640 --> 00:10:00,079 modules SS modules are a functional 196 00:10:00,079 --> 00:10:03,320 organizational unit that follow the 197 00:10:03,320 --> 00:10:07,360 modularization principle it helps to do 198 00:10:07,360 --> 00:10:09,680 code free usability and separation of 199 00:10:09,680 --> 00:10:12,160 concerns principle under the hood they 200 00:10:12,160 --> 00:10:15,360 have implemented the principle design 201 00:10:15,360 --> 00:10:19,000 pattern to organize your code that's it 202 00:10:19,000 --> 00:10:22,040 let's create a new module I'm going to 203 00:10:22,040 --> 00:10:24,360 create a song 204 00:10:24,360 --> 00:10:29,839 module you can use CLI Nest G4 generate 205 00:10:29,839 --> 00:10:32,000 in the name of the module which is the 206 00:10:32,000 --> 00:10:35,240 module keyword I want to generate the SS 207 00:10:35,240 --> 00:10:38,680 module here you can provide any name I'm 208 00:10:38,680 --> 00:10:41,160 I'm interested in the songs module so 209 00:10:41,160 --> 00:10:45,920 you can see in the app. module it has 210 00:10:45,920 --> 00:10:49,399 added the entry automatically for you in 211 00:10:49,399 --> 00:10:52,880 the Imports array and you can see you 212 00:10:52,880 --> 00:10:56,200 have a songs module that's it so this 213 00:10:56,200 --> 00:11:00,360 songs module will have controllers 214 00:11:00,360 --> 00:11:05,160 providers and imports and exports 215 00:11:06,320 --> 00:11:10,000 array controllers are responsible for 216 00:11:10,000 --> 00:11:13,519 handling incoming request and sending 217 00:11:13,519 --> 00:11:16,519 response back to the client we are 218 00:11:16,519 --> 00:11:18,839 building the Spotify clone 219 00:11:18,839 --> 00:11:21,760 application let's imagine or let's say 220 00:11:21,760 --> 00:11:24,279 you want to fetch all the songs or you 221 00:11:24,279 --> 00:11:27,720 want to see all the songs of Martin gck 222 00:11:27,720 --> 00:11:31,079 artist your browser will send the find 223 00:11:31,079 --> 00:11:34,639 all songs request to the nestjs 224 00:11:34,639 --> 00:11:37,959 application inside the nestjs 225 00:11:37,959 --> 00:11:40,720 application you will have songs 226 00:11:40,720 --> 00:11:43,639 controller inside the songs controller 227 00:11:43,639 --> 00:11:47,399 you can see we have find all 228 00:11:47,639 --> 00:11:50,519 method the find all method will be 229 00:11:50,519 --> 00:11:54,279 called and this method will fetch the 230 00:11:54,279 --> 00:11:57,360 songs record from the 231 00:11:57,360 --> 00:12:00,000 database and send respon response back 232 00:12:00,000 --> 00:12:01,120 to the 233 00:12:01,120 --> 00:12:03,880 client similarly you can send the same 234 00:12:03,880 --> 00:12:07,000 request from your mobile application so 235 00:12:07,000 --> 00:12:09,480 controller will be responsible for 236 00:12:09,480 --> 00:12:12,959 handling request and sending response 237 00:12:12,959 --> 00:12:15,560 back to the 238 00:12:15,560 --> 00:12:18,399 client you can create a controller very 239 00:12:18,399 --> 00:12:21,160 easily all you need to do is 240 00:12:21,160 --> 00:12:23,160 controller 241 00:12:23,160 --> 00:12:25,760 decorator and inside The Decorator you 242 00:12:25,760 --> 00:12:31,160 can provide the prefix route prefix 243 00:12:32,519 --> 00:12:34,760 if you want to define the request 244 00:12:34,760 --> 00:12:37,199 Handler inside the controller you have 245 00:12:37,199 --> 00:12:40,680 to use the HTTP get decorator if you 246 00:12:40,680 --> 00:12:42,360 want to create the post endpoint you 247 00:12:42,360 --> 00:12:44,240 have to use the post 248 00:12:44,240 --> 00:12:46,600 decorator it's just returning a simple 249 00:12:46,600 --> 00:12:50,000 string message fetch all the songs and 250 00:12:50,000 --> 00:12:51,959 this method will be converted into this 251 00:12:51,959 --> 00:12:53,000 API 252 00:12:53,000 --> 00:12:58,079 endpoint Local Host callon 3000 / 253 00:12:58,120 --> 00:12:59,920 songs 254 00:12:59,920 --> 00:13:03,199 we are going to build these API 255 00:13:03,199 --> 00:13:06,680 endpoints to fetch all the 256 00:13:06,680 --> 00:13:10,720 songs to fetch a single song I created 257 00:13:10,720 --> 00:13:14,800 these routes to create a new song to 258 00:13:14,800 --> 00:13:18,320 update a song on the based on ID and 259 00:13:18,320 --> 00:13:20,580 delete a song on the based on 260 00:13:20,580 --> 00:13:21,720 [Music] 261 00:13:21,720 --> 00:13:24,880 ID I'm using the rest client extension 262 00:13:24,880 --> 00:13:28,560 to send the API calls it is similar to 263 00:13:28,560 --> 00:13:30,320 The Post 264 00:13:30,320 --> 00:13:32,920 men if you don't have rest client you 265 00:13:32,920 --> 00:13:35,639 can find from the vs code extensions and 266 00:13:35,639 --> 00:13:38,480 find the rest client the rest client 267 00:13:38,480 --> 00:13:40,800 package is saying You must have a file 268 00:13:40,800 --> 00:13:42,600 with 269 00:13:42,600 --> 00:13:47,440 HTTP I created the HTTP 270 00:13:48,279 --> 00:13:51,759 extension now my application is running 271 00:13:51,759 --> 00:13:54,519 at Port 3000 my development project is 272 00:13:54,519 --> 00:13:57,320 running at 30001 you should not aware 273 00:13:57,320 --> 00:13:59,839 you should not care about this project 274 00:13:59,839 --> 00:14:02,720 we are building project on Port 275 00:14:02,720 --> 00:14:05,680 3000 if I send the request to fetch all 276 00:14:05,680 --> 00:14:08,839 the songs we don't have any rout yet it 277 00:14:08,839 --> 00:14:10,600 is giving me the warning message or 278 00:14:10,600 --> 00:14:13,519 error 44 you don't have all these end 279 00:14:13,519 --> 00:14:16,120 points I want you to accept the 280 00:14:16,120 --> 00:14:20,720 challenge and start building these end 281 00:14:21,160 --> 00:14:24,279 points we don't have any 282 00:14:24,279 --> 00:14:28,040 controller let's stop the 283 00:14:28,040 --> 00:14:30,480 application and I'm going to create a 284 00:14:30,480 --> 00:14:33,320 controller 285 00:14:40,279 --> 00:14:43,639 songs you can see that songs controller 286 00:14:43,639 --> 00:14:47,040 has created inside the songs 287 00:14:47,040 --> 00:14:50,959 folder we have spec file testing file 288 00:14:50,959 --> 00:14:53,279 and controller. TS file this is the 289 00:14:53,279 --> 00:14:55,680 controller decorator this is the 290 00:14:55,680 --> 00:14:58,680 prefix one more thing it has added the 291 00:14:58,680 --> 00:15:03,160 end entry in the controllers 292 00:15:05,360 --> 00:15:09,040 array now let's create the request 293 00:15:09,040 --> 00:15:11,880 Handler first of all I'm going to 294 00:15:11,880 --> 00:15:14,800 implement the 295 00:15:17,360 --> 00:15:19,720 get find 296 00:15:19,720 --> 00:15:24,759 all it is going to return a simple 297 00:15:24,800 --> 00:15:27,959 message find all 298 00:15:27,959 --> 00:15:30,079 songs 299 00:15:30,079 --> 00:15:34,040 let's restart or run the 300 00:15:36,040 --> 00:15:38,319 application you can see that our 301 00:15:38,319 --> 00:15:40,160 application is running 302 00:15:40,160 --> 00:15:44,399 now let's fetch all the songs I got find 303 00:15:44,399 --> 00:15:45,399 all the 304 00:15:45,399 --> 00:15:49,759 songs find all songs endpoint it means 305 00:15:49,759 --> 00:15:51,839 we successfully created the route to 306 00:15:51,839 --> 00:15:54,759 fetch all the 307 00:15:57,759 --> 00:16:01,319 songs now now I can send the 308 00:16:01,319 --> 00:16:04,800 request to fetch the songs watch all 309 00:16:04,800 --> 00:16:07,959 songs end 310 00:16:09,959 --> 00:16:13,560 point let's create a 311 00:16:13,560 --> 00:16:15,399 single 312 00:16:15,399 --> 00:16:18,160 song endp 313 00:16:18,160 --> 00:16:21,720 point you have to use the ID as a 314 00:16:21,720 --> 00:16:24,319 dynamic 315 00:16:25,040 --> 00:16:27,920 parameter I can say find 316 00:16:27,920 --> 00:16:29,680 one 317 00:16:29,680 --> 00:16:31,720 here I can 318 00:16:31,720 --> 00:16:37,800 say fetch song on the based on 319 00:16:38,480 --> 00:16:41,759 ID similarly we 320 00:16:41,759 --> 00:16:46,399 need update put for update I'm going to 321 00:16:46,399 --> 00:16:49,519 use the put HTTP 322 00:16:49,519 --> 00:16:52,120 verb 323 00:16:52,120 --> 00:16:55,680 update update song on the based on ID we 324 00:16:55,680 --> 00:16:57,839 also need 325 00:16:57,839 --> 00:17:00,839 delete 326 00:17:07,039 --> 00:17:10,559 I can get the delete from 327 00:17:10,559 --> 00:17:15,199 here let me replace the update to 328 00:17:15,199 --> 00:17:18,640 delete delete song on the based on ID 329 00:17:18,640 --> 00:17:21,520 and finally we need a post 330 00:17:21,520 --> 00:17:24,480 endpoint to create a new 331 00:17:24,480 --> 00:17:27,839 song I can say the 332 00:17:27,839 --> 00:17:30,840 post 333 00:17:31,160 --> 00:17:35,200 create create a new 334 00:17:37,960 --> 00:17:43,280 song so we created the CED API end 335 00:17:43,280 --> 00:17:47,520 points now it's time to send all the 336 00:17:47,520 --> 00:17:50,799 request it's working fine now create a 337 00:17:50,799 --> 00:17:53,480 new song endpoint update song on the 338 00:17:53,480 --> 00:17:57,840 based on ID delete song on the based on 339 00:17:57,840 --> 00:18:00,840 ID 340 00:18:02,039 --> 00:18:04,679 now you got the basic understanding of 341 00:18:04,679 --> 00:18:07,320 controller and how to create route in 342 00:18:07,320 --> 00:18:09,559 njs 343 00:18:09,559 --> 00:18:12,919 application let's talk about Services 344 00:18:12,919 --> 00:18:16,480 services are providers it means you can 345 00:18:16,480 --> 00:18:20,320 inject into another class it will be 346 00:18:20,320 --> 00:18:22,600 treated as a 347 00:18:22,600 --> 00:18:25,400 dependency we will have a 348 00:18:25,400 --> 00:18:28,840 service and the service is responsible 349 00:18:28,840 --> 00:18:32,039 for fetching data from the database and 350 00:18:32,039 --> 00:18:35,159 saving data to the 351 00:18:35,320 --> 00:18:39,640 database if you have to interact with 352 00:18:39,640 --> 00:18:42,200 database you have to write the source 353 00:18:42,200 --> 00:18:44,919 code inside the service you will create 354 00:18:44,919 --> 00:18:46,840 a method to interact with 355 00:18:46,840 --> 00:18:51,360 database we will use repositories don't 356 00:18:51,360 --> 00:18:54,520 worry and this service can be injected 357 00:18:54,520 --> 00:18:57,320 into controllers if you have multiple 358 00:18:57,320 --> 00:19:00,159 controllers into module you can inject 359 00:19:00,159 --> 00:19:03,919 the same service into that controller or 360 00:19:03,919 --> 00:19:07,840 multiple controllers you can also export 361 00:19:07,840 --> 00:19:10,960 the service from a current module and 362 00:19:10,960 --> 00:19:15,360 you can use that service into another 363 00:19:15,520 --> 00:19:18,799 module we will have the song service 364 00:19:18,799 --> 00:19:21,320 it's nothing more it's just a class and 365 00:19:21,320 --> 00:19:23,640 all you need to do is to injectable 366 00:19:23,640 --> 00:19:26,520 decorator it means you can inject as a 367 00:19:26,520 --> 00:19:30,720 dependency into another class 368 00:19:30,880 --> 00:19:33,080 this is how you will inject the service 369 00:19:33,080 --> 00:19:36,679 into controller or into a class you have 370 00:19:36,679 --> 00:19:40,280 to inject it in the Constructor it will 371 00:19:40,280 --> 00:19:42,799 be treated as a dependency for the songs 372 00:19:42,799 --> 00:19:45,600 controller now the songs controller is 373 00:19:45,600 --> 00:19:48,120 dependent on song controller is 374 00:19:48,120 --> 00:19:51,280 dependent on the song 375 00:19:56,159 --> 00:20:00,640 service let's create a new service 376 00:20:01,039 --> 00:20:04,679 I'm going to use n CLI to generate a 377 00:20:04,679 --> 00:20:07,919 service let's write the name which is 378 00:20:07,919 --> 00:20:10,919 songs it will create a class and it will 379 00:20:10,919 --> 00:20:15,320 inject into our songs 380 00:20:18,559 --> 00:20:20,760 module you can see 381 00:20:20,760 --> 00:20:24,280 that we have the song service spec file 382 00:20:24,280 --> 00:20:27,640 which is testing file and we have the 383 00:20:27,640 --> 00:20:29,159 surver 384 00:20:29,159 --> 00:20:32,480 we we have used the injectable 385 00:20:32,480 --> 00:20:34,960 decorator one more 386 00:20:34,960 --> 00:20:38,840 thing it has registered as a provider 387 00:20:38,840 --> 00:20:41,960 inside the songs module I told you 388 00:20:41,960 --> 00:20:45,080 services are provider providers could be 389 00:20:45,080 --> 00:20:47,720 factories helper 390 00:20:47,720 --> 00:20:50,240 function you will learn more about 391 00:20:50,240 --> 00:20:52,679 dependency injection in more details in 392 00:20:52,679 --> 00:20:55,120 the later 393 00:20:55,400 --> 00:20:59,960 mod so I told you service services are 394 00:20:59,960 --> 00:21:03,520 responsible to interact with database 395 00:21:03,520 --> 00:21:05,720 right now we are not going to interact 396 00:21:05,720 --> 00:21:10,480 with database we'll use the postris SQL 397 00:21:10,480 --> 00:21:14,120 postris database in the later videos for 398 00:21:14,120 --> 00:21:17,159 that here I'm going to create a local DB 399 00:21:17,159 --> 00:21:20,320 which it should be array local 400 00:21:20,320 --> 00:21:25,120 array saving saving record in the 401 00:21:25,240 --> 00:21:30,080 disk private read only songs array I'm 402 00:21:30,080 --> 00:21:32,640 going to create the empty array let's 403 00:21:32,640 --> 00:21:36,120 define a new method to create a new song 404 00:21:36,120 --> 00:21:39,240 and it is going to 405 00:21:39,640 --> 00:21:41,520 return 406 00:21:41,520 --> 00:21:44,799 let's push the song make sure you get 407 00:21:44,799 --> 00:21:48,440 the song from the create method there is 408 00:21:48,440 --> 00:21:51,400 a method push and I we need we need to 409 00:21:51,400 --> 00:21:53,200 provide a new 410 00:21:53,200 --> 00:21:56,520 song and let's create another method 411 00:21:56,520 --> 00:22:00,159 find all and and it is going to return a 412 00:22:00,159 --> 00:22:04,640 simple array this. songs and I can 413 00:22:04,640 --> 00:22:07,960 return this. songs 414 00:22:11,600 --> 00:22:14,760 array so we have created two method now 415 00:22:14,760 --> 00:22:17,240 it's time to inject this 416 00:22:17,240 --> 00:22:20,640 service into the controller I want you 417 00:22:20,640 --> 00:22:24,559 to do it accept the challenge and do 418 00:22:24,559 --> 00:22:28,520 it all right I'm going to do it for you 419 00:22:28,520 --> 00:22:31,000 song 420 00:22:35,080 --> 00:22:38,799 service now inside the create I can use 421 00:22:38,799 --> 00:22:42,840 the song service by using the this this 422 00:22:42,840 --> 00:22:46,400 do song service please create a new song 423 00:22:46,400 --> 00:22:48,400 I'm going to provide a dumy 424 00:22:48,400 --> 00:22:50,920 name Animals by 425 00:22:50,920 --> 00:22:52,120 [Music] 426 00:22:52,120 --> 00:22:54,480 Martin you can choose any name don't 427 00:22:54,480 --> 00:22:56,520 worry we will create a separate song 428 00:22:56,520 --> 00:23:01,039 model and separate interface for the 429 00:23:08,960 --> 00:23:12,840 song let's call the 430 00:23:14,520 --> 00:23:17,679 return and for the find all I am going 431 00:23:17,679 --> 00:23:22,000 to use this do song service. 432 00:23:22,000 --> 00:23:25,919 findall let's run the 433 00:23:27,440 --> 00:23:29,840 application 434 00:23:29,840 --> 00:23:32,360 our application is running let's send 435 00:23:32,360 --> 00:23:35,919 the API request to create a new 436 00:23:35,919 --> 00:23:40,200 song you can see I got a new 437 00:23:40,200 --> 00:23:43,960 song if you sent all the songs 438 00:23:43,960 --> 00:23:47,720 request I got all the songs 439 00:23:47,720 --> 00:23:50,559 let's save another 440 00:23:50,559 --> 00:23:55,840 song you can see I got five songs let's 441 00:23:55,840 --> 00:24:00,159 fetch all the songs I got all the 442 00:24:00,159 --> 00:24:03,320 songs so our service is working now in 443 00:24:03,320 --> 00:24:04,720 the real world 444 00:24:04,720 --> 00:24:07,159 application we will write the database 445 00:24:07,159 --> 00:24:14,080 code code here here save the song in the 446 00:24:15,080 --> 00:24:18,000 database and here what we will do fetch 447 00:24:18,000 --> 00:24:18,840 the 448 00:24:18,840 --> 00:24:21,520 songs from the 449 00:24:21,520 --> 00:24:24,240 DB but we will implement this feature in 450 00:24:24,240 --> 00:24:26,640 later 451 00:24:26,640 --> 00:24:29,480 videos in this video I'm going to teach 452 00:24:29,480 --> 00:24:33,600 you how to validate the request 453 00:24:33,600 --> 00:24:36,159 body when you send request to the Local 454 00:24:36,159 --> 00:24:40,799 Host call in 3000 3000 SL song we are 455 00:24:40,799 --> 00:24:43,120 trying to create a new 456 00:24:43,120 --> 00:24:46,399 song but let's say if I miss the title 457 00:24:46,399 --> 00:24:48,240 field it is going to give me the 458 00:24:48,240 --> 00:24:49,840 validation 459 00:24:49,840 --> 00:24:53,000 message the title should not be empty 460 00:24:53,000 --> 00:24:55,000 and title must be 461 00:24:55,000 --> 00:25:00,320 string let me REM know the artist 462 00:25:01,200 --> 00:25:03,559 now you can see title should not be 463 00:25:03,559 --> 00:25:07,799 empty and artist should not be empty so 464 00:25:07,799 --> 00:25:10,200 I would like to add this 465 00:25:10,200 --> 00:25:15,799 validation to our create new song API 466 00:25:15,960 --> 00:25:18,799 endpoint when you go to the 467 00:25:18,799 --> 00:25:21,640 Spotify if I click on any 468 00:25:21,640 --> 00:25:23,200 [Music] 469 00:25:23,200 --> 00:25:27,320 song this is the title of the song and 470 00:25:27,320 --> 00:25:30,120 this is the published date or release 471 00:25:30,120 --> 00:25:33,360 date and this is the duration so we're 472 00:25:33,360 --> 00:25:36,080 going to pick title duration and the 473 00:25:36,080 --> 00:25:37,720 date of the 474 00:25:37,720 --> 00:25:40,559 song let's do 475 00:25:40,559 --> 00:25:43,520 it this time I'm going to use class 476 00:25:43,520 --> 00:25:46,080 validator let's install the class 477 00:25:46,080 --> 00:25:48,720 validator 478 00:25:49,799 --> 00:25:54,679 package and we also need class 479 00:25:57,240 --> 00:26:00,240 Transformer 480 00:26:04,440 --> 00:26:08,799 you can see both packages have 481 00:26:14,080 --> 00:26:19,120 installed nexts provide validation pipe 482 00:26:19,120 --> 00:26:22,000 you can use validation pipe for specific 483 00:26:22,000 --> 00:26:24,679 controller method you can use it here or 484 00:26:24,679 --> 00:26:28,360 you can use it here it's your choice but 485 00:26:28,360 --> 00:26:31,080 but I'm going to create a global I'm 486 00:26:31,080 --> 00:26:33,679 going to register it as a global so I 487 00:26:33,679 --> 00:26:36,080 can use in any 488 00:26:36,080 --> 00:26:40,520 controller so you can call use 489 00:26:40,520 --> 00:26:43,520 method there is a method which is use 490 00:26:43,520 --> 00:26:44,640 Global 491 00:26:44,640 --> 00:26:47,799 pipes nexts already created the 492 00:26:47,799 --> 00:26:49,120 validation 493 00:26:49,120 --> 00:26:53,480 pipe you don't need to create it 494 00:26:54,279 --> 00:26:57,520 again I have imported the validation P 495 00:26:57,520 --> 00:27:02,039 from next sjs common 496 00:27:02,720 --> 00:27:06,720 package and now if you don't have dto 497 00:27:06,720 --> 00:27:09,840 which is data transfer object you have 498 00:27:09,840 --> 00:27:13,240 to specify the network data transfer 499 00:27:13,240 --> 00:27:16,919 object properties let's create a new 500 00:27:16,919 --> 00:27:22,840 song create a new file create song 501 00:27:23,760 --> 00:27:27,919 d. now we need to add decorator paste 502 00:27:27,919 --> 00:27:29,919 very 503 00:27:29,919 --> 00:27:33,640 adds let's create a new 504 00:27:33,640 --> 00:27:36,440 class and I'm going to name it to create 505 00:27:36,440 --> 00:27:39,440 song 506 00:27:39,919 --> 00:27:43,320 dto the first property is title I'm 507 00:27:43,320 --> 00:27:46,039 going to make it read 508 00:27:46,039 --> 00:27:49,480 only and we have the second property 509 00:27:49,480 --> 00:27:51,200 which is artist 510 00:27:51,200 --> 00:27:54,399 array and we have another property which 511 00:27:54,399 --> 00:27:57,120 is release 512 00:27:57,120 --> 00:28:00,120 dat 513 00:28:05,159 --> 00:28:09,320 or you can specify the type which is 514 00:28:10,360 --> 00:28:14,240 string and the type of part is it's an 515 00:28:14,240 --> 00:28:18,039 array and we have the 516 00:28:18,080 --> 00:28:21,880 DAT there is another 517 00:28:21,880 --> 00:28:24,600 property which 518 00:28:24,600 --> 00:28:27,120 is 519 00:28:27,120 --> 00:28:30,120 duration 520 00:28:30,240 --> 00:28:33,000 we need to store the duration in the 521 00:28:33,000 --> 00:28:35,440 time 522 00:28:35,440 --> 00:28:39,240 format now I'm going to use the 523 00:28:39,240 --> 00:28:42,840 decorator from class validator the type 524 00:28:42,840 --> 00:28:46,360 of title should be string and it's a 525 00:28:46,360 --> 00:28:48,799 required property so you can say it's 526 00:28:48,799 --> 00:28:50,399 not an 527 00:28:50,399 --> 00:28:55,159 empty we can follow the same stuff for 528 00:28:55,159 --> 00:28:58,799 artist not empty 529 00:28:58,799 --> 00:29:00,080 it's an 530 00:29:00,080 --> 00:29:03,519 array it's an 531 00:29:12,200 --> 00:29:16,919 array and you can say type 532 00:29:17,720 --> 00:29:21,799 string let's add decoration decorator 533 00:29:21,799 --> 00:29:25,000 for release 534 00:29:25,120 --> 00:29:28,559 date type should be string and 535 00:29:28,559 --> 00:29:31,279 class validator provides a 536 00:29:31,279 --> 00:29:35,440 decorator is date 537 00:29:40,760 --> 00:29:45,039 string now we have for if you want to 538 00:29:45,039 --> 00:29:47,600 store the time or add the validation for 539 00:29:47,600 --> 00:29:49,880 the time there is a there is a decorator 540 00:29:49,880 --> 00:29:52,440 which is military 541 00:29:52,440 --> 00:29:56,120 time and it's also required property so 542 00:29:56,120 --> 00:30:00,039 I'm going to add is not empty 543 00:30:00,120 --> 00:30:02,159 so is date string if you want to apply 544 00:30:02,159 --> 00:30:04,399 that you have to add the date in this 545 00:30:04,399 --> 00:30:07,120 format and you have to add the time in 546 00:30:07,120 --> 00:30:08,440 this 547 00:30:08,440 --> 00:30:11,960 format now we're going to apply we have 548 00:30:11,960 --> 00:30:13,480 already 549 00:30:13,480 --> 00:30:17,080 registered validation pipe here and we 550 00:30:17,080 --> 00:30:19,559 created the data transfer object which 551 00:30:19,559 --> 00:30:23,000 is create song dto now I would like to 552 00:30:23,000 --> 00:30:27,279 apply this create song dto in the create 553 00:30:27,279 --> 00:30:29,960 endpoint 554 00:30:30,320 --> 00:30:32,880 you can register it or you can apply it 555 00:30:32,880 --> 00:30:35,279 by using the body parameter please apply 556 00:30:35,279 --> 00:30:37,480 it on the request 557 00:30:37,480 --> 00:30:41,159 body and I can say create 558 00:30:41,159 --> 00:30:43,360 song create 559 00:30:43,360 --> 00:30:48,399 song dto the type is create song 560 00:30:48,399 --> 00:30:51,039 dto and this time I'm going to push 561 00:30:51,039 --> 00:30:52,440 create 562 00:30:52,440 --> 00:30:54,639 song 563 00:30:54,639 --> 00:30:58,360 dto let's try to test it out and we need 564 00:30:58,360 --> 00:31:00,960 to run the 565 00:31:13,480 --> 00:31:15,760 application so we have to send the 566 00:31:15,760 --> 00:31:20,559 request or add the Json body in the 567 00:31:24,519 --> 00:31:29,440 response let me open the rest c. HTTP 568 00:31:29,440 --> 00:31:32,720 file and we need to add the Chason body 569 00:31:32,720 --> 00:31:34,399 let's send the 570 00:31:34,399 --> 00:31:37,519 request you can see title should not be 571 00:31:37,519 --> 00:31:40,559 empty title must be string artist should 572 00:31:40,559 --> 00:31:43,880 not be empty and artist must be an array 573 00:31:43,880 --> 00:31:47,120 so validations have applied 574 00:31:47,120 --> 00:31:51,279 successfully I can say let's say lasting 575 00:31:51,279 --> 00:31:53,480 lover the 576 00:31:53,480 --> 00:31:56,840 title and we need the 577 00:31:56,840 --> 00:31:59,480 artists 578 00:31:59,480 --> 00:32:01,000 s 579 00:32:01,000 --> 00:32:03,679 let's now we're going to test it 580 00:32:03,679 --> 00:32:07,320 out so we have this 581 00:32:07,320 --> 00:32:11,399 record if you will add the date in 582 00:32:11,399 --> 00:32:13,240 different 583 00:32:13,240 --> 00:32:17,760 format you can see dat should be valid 584 00:32:17,760 --> 00:32:22,799 ISO 860 date string for date string 585 00:32:23,240 --> 00:32:26,960 format if I do in this way you will get 586 00:32:26,960 --> 00:32:29,559 the error dur must be valid 587 00:32:29,559 --> 00:32:31,840 representation of military time which is 588 00:32:31,840 --> 00:32:37,559 hours and minutes so it's working fine 589 00:32:39,919 --> 00:32:43,159 now let's talk about the 590 00:32:43,159 --> 00:32:46,240 middleware a middleware function runs 591 00:32:46,240 --> 00:32:48,679 before executing the route 592 00:32:48,679 --> 00:32:51,480 Handler inside the controller songs 593 00:32:51,480 --> 00:32:54,159 controller I have the find all route 594 00:32:54,159 --> 00:32:57,679 Handler and the create route Handler if 595 00:32:57,679 --> 00:33:00,000 you if you want to run any function 596 00:33:00,000 --> 00:33:03,039 before executing the route Handler you 597 00:33:03,039 --> 00:33:05,720 can write your code inside the 598 00:33:05,720 --> 00:33:09,120 middleware it's a 599 00:33:11,039 --> 00:33:14,760 function a browser wants to fetch the 600 00:33:14,760 --> 00:33:17,840 songs before fetching the songs would 601 00:33:17,840 --> 00:33:21,200 you like to call any method would you 602 00:33:21,200 --> 00:33:24,200 like to run your logic would you like to 603 00:33:24,200 --> 00:33:26,760 execute any type of 604 00:33:26,760 --> 00:33:30,679 code middleware can have request object 605 00:33:30,679 --> 00:33:32,960 response object and next 606 00:33:32,960 --> 00:33:35,600 function it means you can customize the 607 00:33:35,600 --> 00:33:39,559 request object if you want to add header 608 00:33:39,559 --> 00:33:41,799 if you want to log the request time you 609 00:33:41,799 --> 00:33:44,320 can do 610 00:33:45,600 --> 00:33:48,960 that a middleware can execute any type 611 00:33:48,960 --> 00:33:49,720 of 612 00:33:49,720 --> 00:33:53,519 code you can modify the request object 613 00:33:53,519 --> 00:33:55,760 if you want to add the request header 614 00:33:55,760 --> 00:33:57,960 authentication token to the request you 615 00:33:57,960 --> 00:34:00,559 can do in the 616 00:34:00,559 --> 00:34:03,000 middleware you can end the request and 617 00:34:03,000 --> 00:34:06,399 response cycle you can call the next 618 00:34:06,399 --> 00:34:10,520 middleware function by calling the next 619 00:34:11,239 --> 00:34:13,879 method we're going to build the logger 620 00:34:13,879 --> 00:34:15,760 middleware let's 621 00:34:15,760 --> 00:34:19,040 imagine you want to send request from 622 00:34:19,040 --> 00:34:21,359 your browser to 623 00:34:21,359 --> 00:34:25,599 server before executing the route 624 00:34:25,599 --> 00:34:28,199 Handler I would like to log the request 625 00:34:28,199 --> 00:34:31,760 message or the DAT 626 00:34:31,760 --> 00:34:35,719 time let's implement the logger 627 00:34:35,719 --> 00:34:38,560 middleware it's a class we're going to 628 00:34:38,560 --> 00:34:40,960 build a class based 629 00:34:40,960 --> 00:34:44,800 middleware and to create a middleware 630 00:34:44,800 --> 00:34:46,960 you need to implement the nextest 631 00:34:46,960 --> 00:34:48,480 middleware 632 00:34:48,480 --> 00:34:52,480 interface it provides a use method and 633 00:34:52,480 --> 00:34:55,320 use method will have the request 634 00:34:55,320 --> 00:34:59,640 response and the next function 635 00:35:02,800 --> 00:35:07,200 here I'm just logging the request dat 636 00:35:07,200 --> 00:35:10,200 time and at the end I'm also calling the 637 00:35:10,200 --> 00:35:12,079 next 638 00:35:12,079 --> 00:35:14,599 function it will if you will not call 639 00:35:14,599 --> 00:35:16,640 the next function it will not go to the 640 00:35:16,640 --> 00:35:18,960 next 641 00:35:19,000 --> 00:35:22,119 step one more thing middleware it's is 642 00:35:22,119 --> 00:35:24,200 like a 643 00:35:24,200 --> 00:35:29,640 dependency you can inject into module 644 00:35:29,839 --> 00:35:32,200 it will act as a 645 00:35:32,200 --> 00:35:34,920 dependency here we are injecting the 646 00:35:34,920 --> 00:35:37,280 middleware as a 647 00:35:37,280 --> 00:35:40,079 dependency to inject the middleware as a 648 00:35:40,079 --> 00:35:42,440 dependency we need to implement the nest 649 00:35:42,440 --> 00:35:45,280 module interface it will provide the 650 00:35:45,280 --> 00:35:47,960 configure method and the consumer will 651 00:35:47,960 --> 00:35:50,760 be middleware consumer and we just need 652 00:35:50,760 --> 00:35:52,880 to call the apply method from the 653 00:35:52,880 --> 00:35:55,400 consumer 654 00:35:55,400 --> 00:35:57,640 object and you need to provide the 655 00:35:57,640 --> 00:35:59,160 logger 656 00:35:59,160 --> 00:36:01,520 middleware here you need to specify 657 00:36:01,520 --> 00:36:03,040 where do you want to implement the 658 00:36:03,040 --> 00:36:05,400 logger middleware I would like to 659 00:36:05,400 --> 00:36:08,720 implement for songs 660 00:36:08,720 --> 00:36:14,280 API let's implement the locker 661 00:36:14,280 --> 00:36:17,720 middleware before creating a middleware 662 00:36:17,720 --> 00:36:21,400 I'm going to use Nest 663 00:36:21,400 --> 00:36:25,720 CLI please give me the help for nest 664 00:36:25,720 --> 00:36:29,560 generate command 665 00:36:35,280 --> 00:36:39,880 you can see that I got all the commands 666 00:36:39,880 --> 00:36:41,319 related to Nest 667 00:36:41,319 --> 00:36:44,560 generate you can generate class 668 00:36:44,560 --> 00:36:49,000 controller decorator filter 669 00:36:49,000 --> 00:36:52,720 guard middleware pipe 670 00:36:52,720 --> 00:36:58,040 provider we also have other options like 671 00:36:58,040 --> 00:37:01,599 no flat please do not create directory 672 00:37:01,599 --> 00:37:07,000 or no spec please do not create testing 673 00:37:07,000 --> 00:37:12,680 file let's create a new folder 674 00:37:13,560 --> 00:37:16,480 common I'm going to place all my middle 675 00:37:16,480 --> 00:37:20,520 Wares inside the common 676 00:37:26,240 --> 00:37:29,040 directory next generate 677 00:37:29,040 --> 00:37:31,760 Mi common 678 00:37:31,760 --> 00:37:33,720 SL 679 00:37:33,720 --> 00:37:35,800 middleware 680 00:37:35,800 --> 00:37:38,680 and the name of the middleware which is 681 00:37:38,680 --> 00:37:42,240 logger and I don't want a spec 682 00:37:42,240 --> 00:37:46,560 file please do not create the 683 00:37:52,440 --> 00:37:55,480 directory let's try with no flat and 684 00:37:55,480 --> 00:37:58,599 dash dash dry run 685 00:37:58,599 --> 00:38:01,520 let's see what will happen dry run it 686 00:38:01,520 --> 00:38:03,400 will not 687 00:38:03,400 --> 00:38:07,839 create the actual file but got the steps 688 00:38:07,839 --> 00:38:10,560 you will get the understanding how it 689 00:38:10,560 --> 00:38:12,800 will 690 00:38:20,319 --> 00:38:23,920 run it's working fine now no files 691 00:38:23,920 --> 00:38:28,440 return to dis but when you EX cute this 692 00:38:28,440 --> 00:38:31,119 code it will create the logger 693 00:38:31,119 --> 00:38:34,280 middleware file inside the middleware 694 00:38:34,280 --> 00:38:37,599 directory that looks good so I'm going 695 00:38:37,599 --> 00:38:42,319 to remove the dry run let's write the 696 00:38:47,040 --> 00:38:48,720 file 697 00:38:48,720 --> 00:38:52,280 perfect we have created the logger 698 00:38:52,280 --> 00:38:55,520 middleware you can see that it has 699 00:38:55,520 --> 00:38:57,400 already implemented the nest mid 700 00:38:57,400 --> 00:38:58,920 middleware 701 00:38:58,920 --> 00:39:02,760 interface and I got the use method with 702 00:39:02,760 --> 00:39:06,359 these three arguments request response 703 00:39:06,359 --> 00:39:09,160 and the next function here you can 704 00:39:09,160 --> 00:39:11,319 customize the request object if you want 705 00:39:11,319 --> 00:39:13,800 to add your custom logic you can do it 706 00:39:13,800 --> 00:39:17,119 here I can say request dot dot 707 00:39:17,119 --> 00:39:21,520 dot new date whatever you want to do do 708 00:39:21,520 --> 00:39:24,000 to dat 709 00:39:25,359 --> 00:39:28,160 string so we created the 710 00:39:28,160 --> 00:39:30,040 middleware 711 00:39:30,040 --> 00:39:34,200 now I told you or I taught you how to 712 00:39:34,200 --> 00:39:37,079 apply the middleware I want you to do it 713 00:39:37,079 --> 00:39:39,880 in the app 714 00:39:40,599 --> 00:39:46,319 modle we need to implement the nest 715 00:39:50,560 --> 00:39:53,040 module you can see that I got the 716 00:39:53,040 --> 00:39:55,599 configure 717 00:39:55,599 --> 00:40:00,280 method and we need to call consumer. 718 00:40:00,280 --> 00:40:03,040 apply the name of our middleware which 719 00:40:03,040 --> 00:40:05,640 is logger 720 00:40:07,920 --> 00:40:10,640 middleware and we have the four 721 00:40:10,640 --> 00:40:13,079 roots or 722 00:40:13,079 --> 00:40:16,480 routes I can say the 723 00:40:16,480 --> 00:40:20,040 songs now we need to test it out let's 724 00:40:20,040 --> 00:40:22,599 run the 725 00:40:26,319 --> 00:40:29,319 application 726 00:40:30,599 --> 00:40:33,960 my application is running now we need to 727 00:40:33,960 --> 00:40:37,319 send the API 728 00:40:37,480 --> 00:40:40,000 request if you will send the request to 729 00:40:40,000 --> 00:40:43,280 the root URL which is Local Host colum 730 00:40:43,280 --> 00:40:46,040 3000 our middleware function will not 731 00:40:46,040 --> 00:40:48,800 run because we applied the logic to 732 00:40:48,800 --> 00:40:51,040 songs 733 00:40:51,040 --> 00:40:56,119 route fetch all the songs and I 734 00:40:56,119 --> 00:41:00,160 got the the log here request log send 735 00:41:00,160 --> 00:41:02,640 the post request I got the request log 736 00:41:02,640 --> 00:41:05,040 send the put request or send the delete 737 00:41:05,040 --> 00:41:07,960 request I got the 738 00:41:07,960 --> 00:41:12,839 log there are two other options to 739 00:41:12,839 --> 00:41:16,079 implement the or to apply logger 740 00:41:16,079 --> 00:41:18,880 middleware I can say option number 741 00:41:18,880 --> 00:41:24,160 one consumer. apply logger 742 00:41:24,160 --> 00:41:26,240 middleware for 743 00:41:26,240 --> 00:41:29,240 routes 744 00:41:30,119 --> 00:41:33,520 please run for only 745 00:41:33,520 --> 00:41:36,520 songs path 746 00:41:36,520 --> 00:41:39,119 and for only 747 00:41:39,119 --> 00:41:43,680 run request method. 748 00:41:44,400 --> 00:41:48,760 poost it could be option number 749 00:41:55,000 --> 00:41:58,040 two if you send request 750 00:41:58,040 --> 00:42:01,359 get request to fetch all the songs it 751 00:42:01,359 --> 00:42:04,560 will not run if you will send the post 752 00:42:04,560 --> 00:42:08,319 request for the songs I got the request 753 00:42:08,319 --> 00:42:10,319 if you send the put you will not see the 754 00:42:10,319 --> 00:42:11,800 request 755 00:42:11,800 --> 00:42:16,480 log another option we 756 00:42:16,599 --> 00:42:18,880 have let's do 757 00:42:18,880 --> 00:42:21,880 it 758 00:42:22,079 --> 00:42:28,440 consumer zor dot apply logger middleware 759 00:42:28,440 --> 00:42:29,920 for 760 00:42:29,920 --> 00:42:34,520 routes you just need to provide the 761 00:42:34,520 --> 00:42:37,200 controller it could be option number 762 00:42:37,200 --> 00:42:40,200 three it depends on what use case you 763 00:42:40,200 --> 00:42:42,680 like to 764 00:42:46,240 --> 00:42:49,599 use now I can fetch all the songs and I 765 00:42:49,599 --> 00:42:51,760 got the request 766 00:42:51,760 --> 00:42:55,119 log which create a new song I got the 767 00:42:55,119 --> 00:42:56,119 request 768 00:42:56,119 --> 00:42:59,240 log update a new song and I got the 769 00:42:59,240 --> 00:43:01,079 request 770 00:43:01,079 --> 00:43:05,880 log this is how you will create a 771 00:43:08,160 --> 00:43:10,760 middleware did you think about the error 772 00:43:10,760 --> 00:43:12,000 handling in 773 00:43:12,000 --> 00:43:15,520 njs if error comes from your application 774 00:43:15,520 --> 00:43:21,800 code nestjs provide builtin HTTP ex 775 00:43:23,280 --> 00:43:28,800 exception so let's say error comes 776 00:43:28,800 --> 00:43:30,520 while 777 00:43:30,520 --> 00:43:35,079 fetching the data from DB but we did we 778 00:43:35,079 --> 00:43:37,680 did not add any logic to handle the 779 00:43:37,680 --> 00:43:39,319 errors 780 00:43:39,319 --> 00:43:42,800 gracefully let's 781 00:43:42,800 --> 00:43:46,680 imagine we got the error while fetching 782 00:43:46,680 --> 00:43:49,119 the data from the 783 00:43:49,119 --> 00:43:51,800 DB error in 784 00:43:51,800 --> 00:43:56,040 DB while fetching record let's imagine 785 00:43:56,040 --> 00:44:01,119 that when error will happen nestjs will 786 00:44:01,119 --> 00:44:05,240 send this error to http exception this 787 00:44:05,240 --> 00:44:09,280 is the built-in exception in 788 00:44:14,480 --> 00:44:17,480 nestjs let me let me send the request to 789 00:44:17,480 --> 00:44:19,680 fetch all the 790 00:44:19,680 --> 00:44:23,520 songs you will see the server error with 791 00:44:23,520 --> 00:44:25,599 500 status 792 00:44:25,599 --> 00:44:29,440 code next s has sent this message from 793 00:44:29,440 --> 00:44:32,359 HTTP 794 00:44:36,040 --> 00:44:40,400 exception but you can use the 795 00:44:40,400 --> 00:44:44,520 exception in your controller 796 00:44:44,599 --> 00:44:48,079 functions let's say I can 797 00:44:48,079 --> 00:44:49,880 say 798 00:44:49,880 --> 00:44:54,160 or did you think about how can you catch 799 00:44:54,160 --> 00:44:55,359 the 800 00:44:55,359 --> 00:45:00,200 errors you have to use the try catch 801 00:45:03,839 --> 00:45:07,079 block I can log a simple 802 00:45:07,079 --> 00:45:09,800 message I am in 803 00:45:09,800 --> 00:45:13,240 the catch 804 00:45:13,920 --> 00:45:18,040 block and this is my 805 00:45:25,960 --> 00:45:27,760 error 806 00:45:27,760 --> 00:45:30,960 let's try to fetch all the 807 00:45:30,960 --> 00:45:36,240 songs you can see that I got the error 808 00:45:36,240 --> 00:45:40,119 message I'm in the catch block and this 809 00:45:40,119 --> 00:45:42,200 is the 810 00:45:42,200 --> 00:45:46,040 error it means we successfully got 811 00:45:46,040 --> 00:45:47,680 caught the 812 00:45:47,680 --> 00:45:51,200 exception but we want to send the useful 813 00:45:51,200 --> 00:45:55,599 method in the useful status code in the 814 00:45:55,599 --> 00:45:59,559 response I can not see anything 815 00:45:59,559 --> 00:46:03,720 here for that we need to use the HTTP 816 00:46:03,720 --> 00:46:08,040 exception I can say through new HTTP 817 00:46:08,040 --> 00:46:11,040 exception it's a 818 00:46:11,800 --> 00:46:16,040 class and we need to provide the 819 00:46:16,040 --> 00:46:19,359 message can say server error or whatever 820 00:46:19,359 --> 00:46:23,720 you want to say I can say server 821 00:46:23,720 --> 00:46:26,480 error and we have to provide the status 822 00:46:26,480 --> 00:46:27,680 code 823 00:46:27,680 --> 00:46:30,920 HTTP status provides a lot of status 824 00:46:30,920 --> 00:46:34,119 codes you can use 825 00:46:34,119 --> 00:46:37,400 anything whatever you want to do for me 826 00:46:37,400 --> 00:46:38,359 server 827 00:46:38,359 --> 00:46:42,440 error internal server 828 00:46:42,880 --> 00:46:46,000 error if you want to send the message or 829 00:46:46,000 --> 00:46:50,240 cause which should be the error this 830 00:46:53,720 --> 00:46:57,760 one let's try to fetch all the songs 831 00:46:57,760 --> 00:47:00,520 you can see that I got the message 832 00:47:00,520 --> 00:47:04,880 server error with 500 status code which 833 00:47:04,880 --> 00:47:07,040 successfully caught the 834 00:47:07,040 --> 00:47:09,319 exception but if you want to use 835 00:47:09,319 --> 00:47:11,559 different error message you can do that 836 00:47:11,559 --> 00:47:16,640 you can use forbidden status 837 00:47:17,359 --> 00:47:20,160 code it will 838 00:47:20,160 --> 00:47:24,000 work let's restart the 839 00:47:24,000 --> 00:47:28,839 application now I got it 840 00:47:31,200 --> 00:47:34,240 so we have the status code 403 instead 841 00:47:34,240 --> 00:47:36,440 of 842 00:47:39,559 --> 00:47:42,480 500 I'm going to replace forbidden to 843 00:47:42,480 --> 00:47:45,040 internal server 844 00:47:45,040 --> 00:47:48,000 error this is how you will cach the 845 00:47:48,000 --> 00:47:50,960 exception in your source 846 00:47:50,960 --> 00:47:54,240 code if error will come from this 847 00:47:54,240 --> 00:47:57,920 service this this method 848 00:47:57,920 --> 00:48:02,040 TR cach block will cat the 849 00:48:04,359 --> 00:48:07,559 eror got 850 00:48:09,880 --> 00:48:14,240 it let's talk about the pipes in 851 00:48:14,240 --> 00:48:17,760 njs there are two use cases to use the 852 00:48:17,760 --> 00:48:21,839 pipes the first one is to transform the 853 00:48:21,839 --> 00:48:25,240 value here in our example application we 854 00:48:25,240 --> 00:48:27,440 have the find one method 855 00:48:27,440 --> 00:48:32,280 we are expecting the ID as a input 856 00:48:32,280 --> 00:48:35,040 parameter by default it is going to give 857 00:48:35,040 --> 00:48:39,200 me the string so I can use the parse int 858 00:48:39,200 --> 00:48:44,400 pipe to transform the string value to 859 00:48:45,079 --> 00:48:48,240 number here you will see the type of ID 860 00:48:48,240 --> 00:48:50,040 it should be 861 00:48:50,040 --> 00:48:54,400 number another use case for pipes are to 862 00:48:54,400 --> 00:48:58,720 validate the input parameter 863 00:49:05,920 --> 00:49:08,559 in the create method we will accept the 864 00:49:08,559 --> 00:49:12,799 input argument like song title song 865 00:49:12,799 --> 00:49:16,400 artist we will validate the all the 866 00:49:16,400 --> 00:49:19,920 input arguments by using the validation 867 00:49:19,920 --> 00:49:24,079 pipe let's implement this transform 868 00:49:24,079 --> 00:49:28,720 value in our demo application 869 00:49:30,280 --> 00:49:32,280 so I'm going to remove 870 00:49:32,280 --> 00:49:36,160 it let's start it 871 00:49:40,680 --> 00:49:44,400 again it is giving me this 872 00:49:44,400 --> 00:49:47,640 response in nestjs you get you can get 873 00:49:47,640 --> 00:49:53,079 the dynamic parameter by using the ram 874 00:49:53,640 --> 00:49:57,680 decorator Ram decorator and the name of 875 00:49:57,680 --> 00:50:00,119 our argument which is 876 00:50:00,119 --> 00:50:04,119 ID I'm expecting the 877 00:50:04,359 --> 00:50:07,359 ID 878 00:50:07,960 --> 00:50:11,079 and I use the template 879 00:50:11,079 --> 00:50:16,040 string let's define the type of 880 00:50:24,079 --> 00:50:27,119 ID I'm going to send the request you can 881 00:50:27,119 --> 00:50:30,480 see the type of the ID is 882 00:50:30,480 --> 00:50:35,160 string we were expecting the number 883 00:50:35,559 --> 00:50:41,240 type but nestjs got us the string 884 00:50:41,240 --> 00:50:45,319 type you can convert this string into 885 00:50:45,319 --> 00:50:49,960 integer or the number by using the pipe 886 00:50:49,960 --> 00:50:54,760 Pars in pipe that's 887 00:50:55,640 --> 00:50:58,559 it let's send the request to fetch the 888 00:50:58,559 --> 00:51:02,520 song on the based on ID you can see I 889 00:51:02,520 --> 00:51:06,079 got the number ID number type if I 890 00:51:06,079 --> 00:51:08,240 provide the string value 891 00:51:08,240 --> 00:51:11,440 here you will get the validation error 892 00:51:11,440 --> 00:51:13,799 validation failed numeric string is 893 00:51:13,799 --> 00:51:16,240 expected bad request 894 00:51:16,240 --> 00:51:20,079 error if you want to customize the error 895 00:51:20,079 --> 00:51:24,799 message you can instantiate 896 00:51:25,559 --> 00:51:33,400 it eror status codee HTTP status dot not 897 00:51:38,240 --> 00:51:41,760 acceptable now I got not acceptable if I 898 00:51:41,760 --> 00:51:42,880 provide 899 00:51:42,880 --> 00:51:46,720 the numeric string I got the ID which is 900 00:51:46,720 --> 00:51:49,799 number type of the ID is 901 00:51:49,799 --> 00:51:53,559 number we successfully transformed the 902 00:51:53,559 --> 00:51:58,799 string value to number value in the next 903 00:51:58,799 --> 00:52:02,640 video we will implement the input 904 00:52:02,640 --> 00:52:06,280 validation using pipe we will create the 905 00:52:06,280 --> 00:52:09,440 custom validation 906 00:52:10,640 --> 00:52:14,040 pipe you have learned how to inject 907 00:52:14,040 --> 00:52:16,599 dependency by using the Constructor 908 00:52:16,599 --> 00:52:19,319 based dependency injection 909 00:52:19,319 --> 00:52:22,400 pattern we have injected the song 910 00:52:22,400 --> 00:52:25,359 service into the song 911 00:52:25,359 --> 00:52:28,200 controller what what will happen when 912 00:52:28,200 --> 00:52:32,960 songs controller will inject or 913 00:52:32,960 --> 00:52:37,000 instantiate the song service let me open 914 00:52:37,000 --> 00:52:39,680 that songs 915 00:52:39,680 --> 00:52:43,240 controller here we are injecting the 916 00:52:43,240 --> 00:52:46,280 dependency song 917 00:52:46,280 --> 00:52:50,440 service and we have provided as a 918 00:52:50,440 --> 00:52:53,440 dependency 919 00:52:54,160 --> 00:52:58,920 here this one this song 920 00:53:03,160 --> 00:53:06,680 service when Nest container instantiate 921 00:53:06,680 --> 00:53:09,720 the songs controller it first looks for 922 00:53:09,720 --> 00:53:13,799 any dependencies in our case we have the 923 00:53:13,799 --> 00:53:15,760 song 924 00:53:15,760 --> 00:53:19,319 service when it finds the song service 925 00:53:19,319 --> 00:53:22,359 dependency Nest will create the instance 926 00:53:22,359 --> 00:53:25,960 of the song service and cach it and it 927 00:53:25,960 --> 00:53:27,000 will return 928 00:53:27,000 --> 00:53:31,760 it if already created the cache then it 929 00:53:31,760 --> 00:53:34,480 will return from from the cache it will 930 00:53:34,480 --> 00:53:37,400 not create the new 931 00:53:37,400 --> 00:53:41,400 instance let's imagine you are using the 932 00:53:41,400 --> 00:53:44,680 song service inside the multiple 933 00:53:44,680 --> 00:53:47,079 controllers the first controller let's 934 00:53:47,079 --> 00:53:49,280 say we have the songs controller I'm 935 00:53:49,280 --> 00:53:51,359 using it first time 936 00:53:51,359 --> 00:53:55,040 here if I use song service inside the 937 00:53:55,040 --> 00:53:58,440 song service dos back file it would be 938 00:53:58,440 --> 00:54:02,160 treated as a second time now the second 939 00:54:02,160 --> 00:54:06,000 time it will not create any instance it 940 00:54:06,000 --> 00:54:08,480 will get the instance of the song 941 00:54:08,480 --> 00:54:11,960 service from the cache this is the 942 00:54:11,960 --> 00:54:15,760 Singleton pattern applied here next 943 00:54:15,760 --> 00:54:19,440 under the hood created a Singleton 944 00:54:19,440 --> 00:54:22,319 single instance of the song service and 945 00:54:22,319 --> 00:54:24,640 it is going to share the same same 946 00:54:24,640 --> 00:54:28,359 single instance throughout our songs 947 00:54:28,359 --> 00:54:30,359 module but you're going to learn 948 00:54:30,359 --> 00:54:34,000 multiple scope if you want to use the 949 00:54:34,000 --> 00:54:37,680 scop scope strategy if you want to 950 00:54:37,680 --> 00:54:41,799 instantiate a new instance after for 951 00:54:41,799 --> 00:54:45,079 after every incoming request you can use 952 00:54:45,079 --> 00:54:49,799 the scops but I'll talk about the scops 953 00:54:50,040 --> 00:54:52,640 later there are many techniques to use 954 00:54:52,640 --> 00:54:54,799 the providers some of them are standard 955 00:54:54,799 --> 00:54:58,200 providers value Prov providers known 956 00:54:58,200 --> 00:55:00,280 class based provider 957 00:55:00,280 --> 00:55:05,240 tokens class providers we use that class 958 00:55:05,240 --> 00:55:08,559 providers Factory providers used Factory 959 00:55:08,559 --> 00:55:11,920 providers and nonn service providers 960 00:55:11,920 --> 00:55:13,520 we're going to play around with all 961 00:55:13,520 --> 00:55:14,640 these six 962 00:55:14,640 --> 00:55:17,359 techniques let me explain with the help 963 00:55:17,359 --> 00:55:19,000 of 964 00:55:19,000 --> 00:55:21,599 examples this is the standard provider 965 00:55:21,599 --> 00:55:25,200 inside the S module we are using it as a 966 00:55:25,200 --> 00:55:28,280 standard provider but you can convert 967 00:55:28,280 --> 00:55:29,720 the above 968 00:55:29,720 --> 00:55:34,480 syntax into this one let's use 969 00:55:34,480 --> 00:55:38,799 it I need to open my 970 00:55:45,240 --> 00:55:49,680 project we need to create a new 971 00:55:50,200 --> 00:55:55,240 Branch model 3 lesson number 972 00:55:55,240 --> 00:55:56,960 one 973 00:55:56,960 --> 00:55:59,359 open the socks 974 00:55:59,359 --> 00:56:02,160 module so we are using the standard 975 00:56:02,160 --> 00:56:05,000 provider technique but you can write the 976 00:56:05,000 --> 00:56:08,200 above syntax or above 977 00:56:08,200 --> 00:56:12,839 code by using this syntax I can say this 978 00:56:12,839 --> 00:56:15,160 is 979 00:56:17,680 --> 00:56:21,839 provider just this 980 00:56:25,240 --> 00:56:28,240 it 981 00:56:28,839 --> 00:56:31,880 now we have the song service and the 982 00:56:31,880 --> 00:56:36,599 song service please use it as a class 983 00:56:36,599 --> 00:56:41,119 when you inject it into another 984 00:56:41,119 --> 00:56:44,760 class we are using inside the class in 985 00:56:44,760 --> 00:56:47,359 injecting as a 986 00:56:47,359 --> 00:56:48,920 dependency 987 00:56:48,920 --> 00:56:52,880 now you can test the 988 00:56:55,240 --> 00:56:58,240 application 989 00:56:59,680 --> 00:57:03,880 I'm going to send request to any 990 00:57:18,960 --> 00:57:23,079 URL let's create a new song artist must 991 00:57:23,079 --> 00:57:23,470 be 992 00:57:23,470 --> 00:57:24,920 [Music] 993 00:57:24,920 --> 00:57:28,559 stream this is this 994 00:57:38,319 --> 00:57:43,400 true why artist I 995 00:57:48,640 --> 00:57:53,520 think so I got the 996 00:57:53,559 --> 00:57:57,039 issue in the create song d 997 00:57:57,039 --> 00:58:01,760 I did add the H should be true I'm 998 00:58:01,760 --> 00:58:03,520 telling to class 999 00:58:03,520 --> 00:58:07,039 validator the elements of the array type 1000 00:58:07,039 --> 00:58:10,480 should be string each element of the 1001 00:58:10,480 --> 00:58:11,740 array should be 1002 00:58:11,740 --> 00:58:12,880 [Music] 1003 00:58:12,880 --> 00:58:16,440 string now it will it will be working 1004 00:58:16,440 --> 00:58:22,240 fine if we have three articles or three 1005 00:58:23,640 --> 00:58:28,119 artist Martin 1006 00:58:29,400 --> 00:58:32,400 and 1007 00:58:32,760 --> 00:58:38,440 John you can see that it's working fine 1008 00:58:40,400 --> 00:58:42,000 now 1009 00:58:42,000 --> 00:58:46,640 so we also tested this syntax this is 1010 00:58:46,640 --> 00:58:48,079 the standard 1011 00:58:48,079 --> 00:58:51,000 provider but if you like this syntax you 1012 00:58:51,000 --> 00:58:53,520 can keep this syntax but I just want to 1013 00:58:53,520 --> 00:58:54,440 show 1014 00:58:54,440 --> 00:58:58,599 you I'm going to this out uncomment this 1015 00:58:58,599 --> 00:59:02,920 one now I got the song 1016 00:59:10,680 --> 00:59:14,400 service now I'm going to talk about the 1017 00:59:14,400 --> 00:59:16,920 value 1018 00:59:19,400 --> 00:59:21,960 providers if you want to create the 1019 00:59:21,960 --> 00:59:25,440 moing service instead of the original 1020 00:59:25,440 --> 00:59:27,520 service 1021 00:59:27,520 --> 00:59:30,480 you can do that with the help of use 1022 00:59:30,480 --> 00:59:34,680 value the use value syntax is useful for 1023 00:59:34,680 --> 00:59:38,039 injecting a constant value or you want 1024 00:59:38,039 --> 00:59:40,799 to put an external Library into Nest 1025 00:59:40,799 --> 00:59:43,480 container or replacing a real 1026 00:59:43,480 --> 00:59:46,680 implementation with more 1027 00:59:46,680 --> 00:59:49,640 object we are using this 1028 00:59:49,640 --> 00:59:52,359 one we are replacing a real 1029 00:59:52,359 --> 00:59:55,599 implementation of song service with Mo 1030 00:59:55,599 --> 00:59:58,039 object 1031 01:00:00,039 --> 01:00:04,280 so when you need this use case you have 1032 01:00:04,280 --> 01:00:06,720 to use the use value or if you want to 1033 01:00:06,720 --> 01:00:08,920 inject a constant value you can use the 1034 01:00:08,920 --> 01:00:11,760 use value or if you want to inject 1035 01:00:11,760 --> 01:00:14,520 external Library into the nest container 1036 01:00:14,520 --> 01:00:17,640 you can use the use 1037 01:00:19,359 --> 01:00:22,260 value let's do 1038 01:00:22,260 --> 01:00:24,960 [Music] 1039 01:00:24,960 --> 01:00:27,960 it 1040 01:00:33,119 --> 01:00:35,920 I'm going to create a mocking object 1041 01:00:35,920 --> 01:00:38,960 here we have a find all method inside 1042 01:00:38,960 --> 01:00:40,599 the song 1043 01:00:40,599 --> 01:00:43,520 service when you open the song 1044 01:00:43,520 --> 01:00:49,119 service you will see the find all 1045 01:00:49,960 --> 01:00:52,960 method but we have to return the songs 1046 01:00:52,960 --> 01:00:54,920 I'm not going to throw the 1047 01:00:54,920 --> 01:00:57,920 arrow 1048 01:01:03,280 --> 01:01:06,599 would be song 1049 01:01:08,079 --> 01:01:10,599 service and use 1050 01:01:10,599 --> 01:01:14,760 value which is songs moing object Mo 1051 01:01:14,760 --> 01:01:16,599 song 1052 01:01:16,599 --> 01:01:20,359 service that is it 1053 01:01:24,920 --> 01:01:27,920 now 1054 01:01:29,000 --> 01:01:30,599 now we need to test 1055 01:01:30,599 --> 01:01:34,160 it I'm going to fetch all the 1056 01:01:34,160 --> 01:01:37,480 songs and this time you are getting the 1057 01:01:37,480 --> 01:01:41,119 response from the mo object not original 1058 01:01:41,119 --> 01:01:43,520 song 1059 01:01:48,480 --> 01:01:52,039 service so we have the find all method 1060 01:01:52,039 --> 01:01:54,400 and it is returning this 1061 01:01:54,400 --> 01:01:59,039 one if I will app add the 1062 01:02:07,400 --> 01:02:10,839 artist I'm going to fetch all the songs 1063 01:02:10,839 --> 01:02:13,920 from the mo service object mock 1064 01:02:13,920 --> 01:02:17,559 service and you can see I got the record 1065 01:02:17,559 --> 01:02:19,880 from the mock service instead of 1066 01:02:19,880 --> 01:02:23,680 original song service find all 1067 01:02:23,680 --> 01:02:26,559 method we have learned how to use the 1068 01:02:26,559 --> 01:02:28,319 use 1069 01:02:28,319 --> 01:02:31,319 value 1070 01:02:31,920 --> 01:02:34,720 now if you want to inject the known 1071 01:02:34,720 --> 01:02:39,039 class based provider you can do that 1072 01:02:39,039 --> 01:02:43,240 let's say I want to inject an object 1073 01:02:43,240 --> 01:02:45,319 with 1074 01:02:45,319 --> 01:02:47,920 connection we have three 1075 01:02:47,920 --> 01:02:51,440 properties connection 1076 01:02:52,480 --> 01:02:55,559 string if you would like to inject any 1077 01:02:55,559 --> 01:02:58,799 object object as a dependency you can do 1078 01:02:58,799 --> 01:03:02,279 it well with use value I told you you 1079 01:03:02,279 --> 01:03:06,520 can inject constant by using use value 1080 01:03:06,520 --> 01:03:09,960 you can object an you can inject an 1081 01:03:09,960 --> 01:03:14,240 object or you can inject the working 1082 01:03:14,240 --> 01:03:17,680 object let's create a new file with 1083 01:03:17,680 --> 01:03:19,440 connection. 1084 01:03:19,440 --> 01:03:22,000 file and I'm going to specify some 1085 01:03:22,000 --> 01:03:23,760 connection 1086 01:03:23,760 --> 01:03:27,119 properties you have to create an new 1087 01:03:27,119 --> 01:03:30,400 constant. file inside the common folder 1088 01:03:30,400 --> 01:03:33,000 and I'm going to add the constants 1089 01:03:33,000 --> 01:03:35,960 directory here you will have constant. 1090 01:03:35,960 --> 01:03:38,440 TS 1091 01:03:39,090 --> 01:03:45,760 [Music] 1092 01:03:45,760 --> 01:03:48,000 file I can say 1093 01:03:48,000 --> 01:03:52,200 connection. and I'm going to copy the 1094 01:03:52,200 --> 01:03:55,480 code I just defined three variables and 1095 01:03:55,480 --> 01:03:58,599 I have Define the type which is 1096 01:03:58,599 --> 01:04:02,079 connection now I would like to register 1097 01:04:02,079 --> 01:04:05,000 it as a 1098 01:04:08,480 --> 01:04:11,079 provider you can register inside the 1099 01:04:11,079 --> 01:04:13,920 songs module or you want to register it 1100 01:04:13,920 --> 01:04:18,680 to app module it depends on you provide 1101 01:04:18,680 --> 01:04:22,000 this is the name or the token 1102 01:04:22,000 --> 01:04:24,200 value this is 1103 01:04:24,200 --> 01:04:26,559 how I will 1104 01:04:26,559 --> 01:04:30,599 inject in the class when I will inject 1105 01:04:30,599 --> 01:04:33,310 in the class I'll use this token 1106 01:04:33,310 --> 01:04:34,599 [Music] 1107 01:04:34,599 --> 01:04:39,160 key use value let me 1108 01:04:39,160 --> 01:04:41,550 provide which is 1109 01:04:41,550 --> 01:04:44,869 [Music] 1110 01:04:49,920 --> 01:04:54,079 connection so we have to inject it 1111 01:04:54,079 --> 01:04:56,480 inside the 1112 01:04:56,480 --> 01:05:02,279 song service I can use the add inject 1113 01:05:02,279 --> 01:05:05,640 decorator so 1114 01:05:05,640 --> 01:05:08,640 songs 1115 01:05:09,039 --> 01:05:12,160 controller I think I injected inside the 1116 01:05:12,160 --> 01:05:13,760 song 1117 01:05:13,760 --> 01:05:17,160 service so I'm going to inject here 1118 01:05:17,160 --> 01:05:18,760 inside the song 1119 01:05:18,760 --> 01:05:22,200 service or let me inject in the 1120 01:05:22,200 --> 01:05:24,640 Constructor 1121 01:05:24,640 --> 01:05:26,599 function 1122 01:05:26,599 --> 01:05:29,240 here I would like to 1123 01:05:29,240 --> 01:05:32,760 inject I can 1124 01:05:38,960 --> 01:05:41,279 say 1125 01:05:41,279 --> 01:05:44,680 inject let's provide the 1126 01:05:44,680 --> 01:05:46,960 key 1127 01:05:46,960 --> 01:05:49,960 private 1128 01:05:50,000 --> 01:05:53,599 connection here we need to provide the 1129 01:05:53,599 --> 01:05:58,559 type I have defined the type this is the 1130 01:05:58,559 --> 01:06:01,920 type so I can 1131 01:06:01,920 --> 01:06:04,680 log the 1132 01:06:04,680 --> 01:06:08,279 connection this. 1133 01:06:08,319 --> 01:06:10,960 connection if you will not add the 1134 01:06:10,960 --> 01:06:14,400 private so you can add directly without 1135 01:06:14,400 --> 01:06:17,720 adding the this keyword I did not add 1136 01:06:17,720 --> 01:06:21,720 the provider so I access the connection 1137 01:06:21,720 --> 01:06:24,559 string without adding the 1138 01:06:24,559 --> 01:06:28,559 this so let's say this is I can 1139 01:06:28,559 --> 01:06:33,039 say I'm going to use the template 1140 01:06:33,440 --> 01:06:35,920 string this 1141 01:06:35,920 --> 01:06:37,860 is connection 1142 01:06:37,860 --> 01:06:41,360 [Music] 1143 01:06:47,799 --> 01:06:52,480 string when songs controller will be 1144 01:06:52,480 --> 01:06:55,000 instantiated this Constructor function 1145 01:06:55,000 --> 01:07:00,559 will be called so it will log this 1146 01:07:00,559 --> 01:07:03,920 message you can see we have this is the 1147 01:07:03,920 --> 01:07:07,359 connection string so I can get the 1148 01:07:07,359 --> 01:07:09,839 connection 1149 01:07:10,119 --> 01:07:12,720 string now I can see this is connection 1150 01:07:12,720 --> 01:07:15,090 string and I can say the connection 1151 01:07:15,090 --> 01:07:18,219 [Music] 1152 01:07:18,480 --> 01:07:21,079 string or you can 1153 01:07:21,079 --> 01:07:24,520 say my SQL dot 1154 01:07:24,520 --> 01:07:26,920 dot col do 1155 01:07:26,920 --> 01:07:31,880 dot 1 2 3 6 or maybe 1156 01:07:34,079 --> 01:07:38,640 Port I created the fake or dummy 1157 01:07:38,640 --> 01:07:40,640 connection 1158 01:07:40,640 --> 01:07:44,960 stren you have learned how to inject the 1159 01:07:44,960 --> 01:07:46,960 object as a 1160 01:07:46,960 --> 01:07:52,119 provider and used it as a 1161 01:07:54,440 --> 01:07:56,319 dependency 1162 01:07:56,319 --> 01:07:58,559 now we're going to talk about class 1163 01:07:58,559 --> 01:08:01,920 based provider we have already used the 1164 01:08:01,920 --> 01:08:06,240 class based provider here this 1165 01:08:06,240 --> 01:08:11,119 one not this one this one used Class so 1166 01:08:11,119 --> 01:08:12,520 I'm going to 1167 01:08:12,520 --> 01:08:14,839 create a 1168 01:08:14,839 --> 01:08:17,600 new 1169 01:08:17,600 --> 01:08:21,439 service this this will be called da 1170 01:08:21,439 --> 01:08:22,560 config 1171 01:08:22,560 --> 01:08:25,839 service inside the dep config service 1172 01:08:25,839 --> 01:08:28,759 I'll have one property and a one 1173 01:08:28,759 --> 01:08:32,479 function DB host and get DB 1174 01:08:32,479 --> 01:08:37,318 host remember it's an injectable 1175 01:08:38,520 --> 01:08:43,319 service so you can register it as a 1176 01:08:43,319 --> 01:08:46,319 provider inside the app module by using 1177 01:08:46,319 --> 01:08:48,759 the use class syntax and you have to 1178 01:08:48,759 --> 01:08:51,238 provide the name of your service in our 1179 01:08:51,238 --> 01:08:54,279 case we have the da config 1180 01:08:54,279 --> 01:08:57,158 service and and then and then I can use 1181 01:08:57,158 --> 01:09:01,919 the dev config service inside the app 1182 01:09:05,479 --> 01:09:08,479 service 1183 01:09:12,399 --> 01:09:14,960 so I can 1184 01:09:14,960 --> 01:09:18,440 create a 1185 01:09:18,839 --> 01:09:22,198 providers hold up let's 1186 01:09:22,198 --> 01:09:24,120 rename 1187 01:09:24,120 --> 01:09:27,640 providers let's create a new 1188 01:09:27,640 --> 01:09:29,880 class def 1189 01:09:29,880 --> 01:09:32,279 config 1190 01:09:32,279 --> 01:09:35,279 service.ts 1191 01:09:36,319 --> 01:09:38,839 and we 1192 01:09:38,839 --> 01:09:43,198 have these properties we have TB host in 1193 01:09:43,198 --> 01:09:45,560 the get TB 1194 01:09:45,560 --> 01:09:51,480 host so let me register inside the app 1195 01:09:54,320 --> 01:09:56,000 module 1196 01:09:56,000 --> 01:10:01,120 we have provider and I can say please 1197 01:10:01,120 --> 01:10:03,120 provide Dev 1198 01:10:03,120 --> 01:10:07,320 config service in the use 1199 01:10:07,320 --> 01:10:11,159 class Dev confli 1200 01:10:24,159 --> 01:10:28,120 service and and now I can inject inside 1201 01:10:28,120 --> 01:10:29,600 the app 1202 01:10:29,600 --> 01:10:34,880 module let me inject it 1203 01:10:35,440 --> 01:10:38,679 here I can say 1204 01:10:38,679 --> 01:10:41,000 private 1205 01:10:41,000 --> 01:10:43,040 Dev 1206 01:10:43,040 --> 01:10:45,000 config 1207 01:10:45,000 --> 01:10:50,600 service and I can say def config 1208 01:10:51,159 --> 01:10:54,920 service and I can call the method from 1209 01:10:54,920 --> 01:10:57,960 the D config let me convert into the 1210 01:10:57,960 --> 01:11:00,440 template 1211 01:11:01,480 --> 01:11:04,080 string da config 1212 01:11:04,080 --> 01:11:07,400 service. DB 1213 01:11:23,719 --> 01:11:25,760 host so 1214 01:11:25,760 --> 01:11:28,760 we have to send the request to the root 1215 01:11:28,760 --> 01:11:31,400 URL to test 1216 01:11:31,400 --> 01:11:34,320 it 1217 01:11:34,320 --> 01:11:38,159 rest let me send the 1218 01:11:39,360 --> 01:11:42,120 request hello I'm learning NS 1219 01:11:42,120 --> 01:11:44,639 fundamental and this is the DB host 1220 01:11:44,639 --> 01:11:47,880 which is St config which is Local Host 1221 01:11:47,880 --> 01:11:50,280 DB 1222 01:11:54,239 --> 01:11:57,239 host 1223 01:12:00,520 --> 01:12:03,440 so if you want to inject the known 1224 01:12:03,440 --> 01:12:07,120 service provider like a simple 1225 01:12:07,120 --> 01:12:09,560 object and you want 1226 01:12:09,560 --> 01:12:14,280 to inject Dynamic 1227 01:12:14,280 --> 01:12:19,239 value you have added the check or if 1228 01:12:19,239 --> 01:12:23,880 condition if node development equals to 1229 01:12:23,880 --> 01:12:26,199 development then you you have to inject 1230 01:12:26,199 --> 01:12:28,719 the dev config otherwise you have to 1231 01:12:28,719 --> 01:12:32,480 inject the pro config if if you have two 1232 01:12:32,480 --> 01:12:35,520 objects and you want to inject on the 1233 01:12:35,520 --> 01:12:38,360 based on condition this is the known 1234 01:12:38,360 --> 01:12:41,400 service provider it means these will not 1235 01:12:41,400 --> 01:12:43,679 be 1236 01:12:43,800 --> 01:12:46,360 classes if you have this type of use 1237 01:12:46,360 --> 01:12:49,560 case you can inject by using the use 1238 01:12:49,560 --> 01:12:52,360 Factory so I'm going to 1239 01:12:52,360 --> 01:12:56,120 call inside the app module let me let me 1240 01:12:56,120 --> 01:12:57,360 show the 1241 01:12:57,360 --> 01:13:01,840 demo I just created the two 1242 01:13:01,840 --> 01:13:05,679 object and in the 1243 01:13:08,639 --> 01:13:12,280 provider we have used the key which is 1244 01:13:12,280 --> 01:13:15,440 config and I use the used Factory and we 1245 01:13:15,440 --> 01:13:18,920 have to provide the arrow function it's 1246 01:13:18,920 --> 01:13:22,040 just returning a value Dev config and 1247 01:13:22,040 --> 01:13:25,679 the pro config if you want to in inject 1248 01:13:25,679 --> 01:13:28,400 dependency or inject provider only based 1249 01:13:28,400 --> 01:13:31,600 on condition then you can use the US 1250 01:13:31,600 --> 01:13:35,560 Factory and now the same way you can 1251 01:13:35,560 --> 01:13:38,360 inject config 1252 01:13:38,360 --> 01:13:42,239 value in the app 1253 01:13:42,360 --> 01:13:46,800 service here we have to inject 1254 01:13:49,639 --> 01:13:52,280 it by using the 1255 01:13:52,280 --> 01:13:55,600 inject decorator provide the key which 1256 01:13:55,600 --> 01:13:57,320 is 1257 01:13:57,320 --> 01:14:00,280 config in 1258 01:14:00,280 --> 01:14:02,320 private 1259 01:14:02,320 --> 01:14:06,490 config we have Port value and type a 1260 01:14:06,490 --> 01:14:08,880 [Music] 1261 01:14:08,880 --> 01:14:15,600 string and you can say Port should 1262 01:14:15,600 --> 01:14:20,760 be this do config do 1263 01:14:21,280 --> 01:14:24,600 Port so we have to test it to send 1264 01:14:24,600 --> 01:14:27,440 request to the root 1265 01:14:27,440 --> 01:14:29,960 URL now I got 1266 01:14:29,960 --> 01:14:33,639 the host which is Local Host and the 1267 01:14:33,639 --> 01:14:36,320 port which is 1268 01:14:41,560 --> 01:14:45,639 400 so we have to add 1269 01:14:52,520 --> 01:14:56,520 4,000 you have learned how to inject 1270 01:14:56,520 --> 01:14:59,679 provider as in the standard way you have 1271 01:14:59,679 --> 01:15:02,760 learned the use value you have learned 1272 01:15:02,760 --> 01:15:06,400 how to add non class based provider you 1273 01:15:06,400 --> 01:15:09,520 have learned how to use use class 1274 01:15:09,520 --> 01:15:12,960 provider how to use the dynamic how to 1275 01:15:12,960 --> 01:15:15,880 in how to register the dynamic 1276 01:15:15,880 --> 01:15:18,800 provider and finally you have learned 1277 01:15:18,800 --> 01:15:23,960 how to how to register known service 1278 01:15:23,960 --> 01:15:26,960 providers 1279 01:15:27,080 --> 01:15:29,880 the previous video you have learned 1280 01:15:29,880 --> 01:15:34,199 multiple techniques to register a 1281 01:15:34,520 --> 01:15:37,840 providers when you register a provider 1282 01:15:37,840 --> 01:15:44,040 by default nestjs use this scope default 1283 01:15:44,040 --> 01:15:47,000 it means a single instance will be 1284 01:15:47,000 --> 01:15:50,840 created which is called the Singleton 1285 01:15:50,840 --> 01:15:54,199 pattern and this single instance of the 1286 01:15:54,199 --> 01:15:57,800 provider is is shared across the entire 1287 01:15:57,800 --> 01:16:01,360 application nextest cach the instance 1288 01:16:01,360 --> 01:16:04,159 when application request for the second 1289 01:16:04,159 --> 01:16:07,520 time it will not create a new instance 1290 01:16:07,520 --> 01:16:10,960 second time it will return from the 1291 01:16:10,960 --> 01:16:14,679 cash another injection scope technique 1292 01:16:14,679 --> 01:16:15,719 which is 1293 01:16:15,719 --> 01:16:18,800 request a new instance of the provider 1294 01:16:18,800 --> 01:16:23,920 is created exclusively for each incoming 1295 01:16:23,920 --> 01:16:27,440 request and we have the third technique 1296 01:16:27,440 --> 01:16:30,679 injection scope technique transient 1297 01:16:30,679 --> 01:16:34,040 providers are not shared across consumer 1298 01:16:34,040 --> 01:16:36,440 each consumer that inject a transer 1299 01:16:36,440 --> 01:16:39,920 provider will provide a new dedicated 1300 01:16:39,920 --> 01:16:42,560 instance most of the time you need 1301 01:16:42,560 --> 01:16:45,239 default scope because single turn 1302 01:16:45,239 --> 01:16:47,480 instance is recommended to improve the 1303 01:16:47,480 --> 01:16:49,120 performance of your 1304 01:16:49,120 --> 01:16:51,120 [Music] 1305 01:16:51,120 --> 01:16:54,320 application when you get the instance 1306 01:16:54,320 --> 01:16:55,560 from the C 1307 01:16:55,560 --> 01:16:58,679 it will give you the instance in the 1308 01:16:58,679 --> 01:17:01,719 more faster way instead of creating the 1309 01:17:01,719 --> 01:17:05,159 complete object it just give you the 1310 01:17:05,159 --> 01:17:08,040 copy of the already created 1311 01:17:08,040 --> 01:17:10,600 object so you can play around with 1312 01:17:10,600 --> 01:17:11,880 injection 1313 01:17:11,880 --> 01:17:13,840 Scopes let's 1314 01:17:13,840 --> 01:17:16,639 say if you want to use it inside the 1315 01:17:16,639 --> 01:17:19,600 song service I didn't I could not find 1316 01:17:19,600 --> 01:17:23,840 how to debug the dependency injection 1317 01:17:23,840 --> 01:17:28,760 so you have to use 1318 01:17:28,760 --> 01:17:30,170 here 1319 01:17:30,170 --> 01:17:31,280 [Music] 1320 01:17:31,280 --> 01:17:33,639 scope injection 1321 01:17:33,639 --> 01:17:37,040 scope what we have to say 1322 01:17:37,040 --> 01:17:40,120 scope scope 1323 01:17:40,120 --> 01:17:43,120 to 1324 01:17:45,800 --> 01:17:48,360 transient the performance of your 1325 01:17:48,360 --> 01:17:51,400 application will be 1326 01:17:51,400 --> 01:17:54,639 hard it will decrease the performance 1327 01:17:54,639 --> 01:17:59,159 but but it depends on your use case what 1328 01:17:59,159 --> 01:18:01,719 are your 1329 01:18:04,360 --> 01:18:07,440 requirements similarly when every 1330 01:18:07,440 --> 01:18:08,719 incoming 1331 01:18:08,719 --> 01:18:12,080 request if you have use case you want to 1332 01:18:12,080 --> 01:18:14,000 create a new 1333 01:18:14,000 --> 01:18:17,719 instance you can use it well for the 1334 01:18:17,719 --> 01:18:21,679 requesting purpose for testing 1335 01:18:21,920 --> 01:18:24,159 purpose I'm going to open the songs 1336 01:18:24,159 --> 01:18:26,760 controller 1337 01:18:28,440 --> 01:18:32,840 and here we have to 1338 01:18:33,870 --> 01:18:37,149 [Music] 1339 01:18:53,719 --> 01:18:56,719 say 1340 01:18:58,000 --> 01:19:00,639 it's an 1341 01:19:02,840 --> 01:19:07,040 object let's import this 1342 01:19:19,639 --> 01:19:23,520 scope I'm going to send the API request 1343 01:19:23,520 --> 01:19:26,719 it's working fine 1344 01:19:39,360 --> 01:19:42,880 one thing you did not 1345 01:19:45,440 --> 01:19:50,960 notice when I will create a new song it 1346 01:19:50,960 --> 01:19:55,239 should add the song in the array 1347 01:19:55,239 --> 01:19:59,440 so I have created three 1348 01:19:59,440 --> 01:20:04,480 objects three songs object in the songs 1349 01:20:04,480 --> 01:20:07,440 array when I will send the request to 1350 01:20:07,440 --> 01:20:10,600 fetch a single song or all 1351 01:20:10,600 --> 01:20:13,600 songs it should give me the empty 1352 01:20:13,600 --> 01:20:16,840 response because on every 1353 01:20:16,840 --> 01:20:21,239 request it will delete it the complete 1354 01:20:21,239 --> 01:20:23,639 data complete 1355 01:20:23,639 --> 01:20:26,639 songs 1356 01:20:27,239 --> 01:20:29,400 because it will also re instantiate the 1357 01:20:29,400 --> 01:20:32,679 song service inside the song service we 1358 01:20:32,679 --> 01:20:34,880 we have the 1359 01:20:34,880 --> 01:20:38,760 are we have the songs 1360 01:20:46,600 --> 01:20:50,960 are you can see I created the song now 1361 01:20:50,960 --> 01:20:53,159 when I try to fix all the songs it 1362 01:20:53,159 --> 01:20:56,199 should give me the first song or all 1363 01:20:56,199 --> 01:20:59,880 songs you can see the empty 1364 01:20:59,880 --> 01:21:02,199 response in this video I'm going to 1365 01:21:02,199 --> 01:21:05,920 teach you how to add one to many or many 1366 01:21:05,920 --> 01:21:10,000 to one relationship between two entities 1367 01:21:10,000 --> 01:21:13,639 where a contains multiple instances of B 1368 01:21:13,639 --> 01:21:17,239 entity and B entity contains only one 1369 01:21:17,239 --> 01:21:19,800 instance of a 1370 01:21:19,800 --> 01:21:22,600 entity we have two use 1371 01:21:22,600 --> 01:21:25,920 cases we are building this Spotify 1372 01:21:25,920 --> 01:21:30,719 clone if you are using or you use 1373 01:21:30,719 --> 01:21:34,679 Spotify each user has its own playlist 1374 01:21:34,679 --> 01:21:35,840 and each 1375 01:21:35,840 --> 01:21:41,120 playlist can have multiple songs so you 1376 01:21:41,120 --> 01:21:44,320 as a user can store multiple songs or 1377 01:21:44,320 --> 01:21:47,840 save multiple songs in the playlist so 1378 01:21:47,840 --> 01:21:49,840 there is a one to one one to many 1379 01:21:49,840 --> 01:21:52,600 relationship between playlist entity and 1380 01:21:52,600 --> 01:21:55,480 the songs entity see similarly we have 1381 01:21:55,480 --> 01:21:58,400 each user can have multiple playlist 1382 01:21:58,400 --> 01:22:01,880 many playlist can belong to a single 1383 01:22:01,880 --> 01:22:05,840 user so this is also a many to one 1384 01:22:05,840 --> 01:22:08,679 relationship between playlist and the 1385 01:22:08,679 --> 01:22:11,639 users let's create the playlist entity 1386 01:22:11,639 --> 01:22:13,000 and add 1387 01:22:13,000 --> 01:22:16,000 relationship we don't have any 1388 01:22:16,000 --> 01:22:19,239 playlist entity go ahead and create a 1389 01:22:19,239 --> 01:22:21,760 playlist 1390 01:22:23,520 --> 01:22:28,080 entity I have created a new folder 1391 01:22:28,080 --> 01:22:32,239 playlist. entity. TS 1392 01:22:32,239 --> 01:22:34,719 file so I'm going to copy couple of the 1393 01:22:34,719 --> 01:22:37,719 code from 1394 01:22:37,719 --> 01:22:40,719 my 1395 01:22:45,440 --> 01:22:50,760 notes we have created the playlist 1396 01:22:53,440 --> 01:22:55,840 entity 1397 01:22:55,840 --> 01:22:59,520 now we have to make make a relationship 1398 01:22:59,520 --> 01:23:02,760 between songs and the 1399 01:23:02,760 --> 01:23:05,560 users 1400 01:23:05,560 --> 01:23:10,480 similarly type orm provides one to many 1401 01:23:10,480 --> 01:23:13,960 decorator and this represents the target 1402 01:23:13,960 --> 01:23:17,159 entity relation entity we are making a 1403 01:23:17,159 --> 01:23:20,040 relationship with song entity this 1404 01:23:20,040 --> 01:23:22,199 representing the inverse side of the 1405 01:23:22,199 --> 01:23:25,120 relation in the song model we have a 1406 01:23:25,120 --> 01:23:26,639 playlist 1407 01:23:26,639 --> 01:23:30,159 property and we have the array of the 1408 01:23:30,159 --> 01:23:33,199 songs I'm going to copy this code and 1409 01:23:33,199 --> 01:23:36,080 I'm going to paste it 1410 01:23:36,400 --> 01:23:40,199 here we don't have song. 1411 01:23:40,199 --> 01:23:43,159 playlist open the song 1412 01:23:43,159 --> 01:23:45,800 entity here we can add a 1413 01:23:45,800 --> 01:23:49,000 relationship but we also going to add a 1414 01:23:49,000 --> 01:23:51,639 relationship between user so I'm going 1415 01:23:51,639 --> 01:23:56,440 to copy this code and paste it 1416 01:23:58,840 --> 01:24:02,360 here now we have to add many to one 1417 01:24:02,360 --> 01:24:05,800 relationship in the song 1418 01:24:07,400 --> 01:24:10,400 entity so many songs can belong to a 1419 01:24:10,400 --> 01:24:12,400 playlist for each unique 1420 01:24:12,400 --> 01:24:16,679 user we have to import the playlist 1421 01:24:16,679 --> 01:24:20,280 import many to one 1422 01:24:20,280 --> 01:24:23,199 relation and finally we have to add one 1423 01:24:23,199 --> 01:24:25,560 to many relation in inside the song 1424 01:24:25,560 --> 01:24:29,320 inside the users user 1425 01:24:34,239 --> 01:24:38,440 entity so I'm going to copy that 1426 01:24:38,440 --> 01:24:41,679 code inside the user 1427 01:24:41,679 --> 01:24:45,960 model and I'm going to paste it 1428 01:24:53,320 --> 01:24:56,320 here 1429 01:24:58,119 --> 01:25:00,639 now everything is fine 1430 01:25:00,639 --> 01:25:04,719 now I have added many to one relation in 1431 01:25:04,719 --> 01:25:08,239 the song it means it will have the 1432 01:25:08,239 --> 01:25:12,520 primary key of the playlist entity and 1433 01:25:12,520 --> 01:25:15,480 save it as a reference key or the 1434 01:25:15,480 --> 01:25:18,159 foreign 1435 01:25:21,560 --> 01:25:24,320 key now we need to create the clip 1436 01:25:24,320 --> 01:25:25,639 playlist 1437 01:25:25,639 --> 01:25:27,840 module I also want 1438 01:25:27,840 --> 01:25:30,000 [Music] 1439 01:25:30,000 --> 01:25:33,320 to create a new playlist I'm going to 1440 01:25:33,320 --> 01:25:35,320 create a new 1441 01:25:35,320 --> 01:25:40,040 endpoint but first of but first we need 1442 01:25:40,040 --> 01:25:44,760 to register our new entity I'm saying to 1443 01:25:44,760 --> 01:25:48,800 type orm please convert or make relation 1444 01:25:48,800 --> 01:25:51,440 between playlist and the user playlist 1445 01:25:51,440 --> 01:25:53,239 and the 1446 01:25:53,239 --> 01:25:56,239 song 1447 01:26:05,480 --> 01:26:07,719 every everything is fine now when you go 1448 01:26:07,719 --> 01:26:10,400 to the pg 1449 01:26:10,679 --> 01:26:13,480 admin refresh your 1450 01:26:13,480 --> 01:26:17,920 tables you will see playlist 1451 01:26:18,000 --> 01:26:21,920 table this is the playlist 1452 01:26:22,800 --> 01:26:25,560 table and we have the user 1453 01:26:25,560 --> 01:26:29,960 ID when you open 1454 01:26:29,960 --> 01:26:33,119 playlist entity we have added the many 1455 01:26:33,119 --> 01:26:36,480 to one relation it means it will have 1456 01:26:36,480 --> 01:26:41,600 the user ID as a foreign key and inside 1457 01:26:41,600 --> 01:26:45,280 this song entity we have added many to 1458 01:26:45,280 --> 01:26:49,239 one relationship as for the playlist so 1459 01:26:49,239 --> 01:26:52,040 this song will have 1460 01:26:52,040 --> 01:26:56,560 the primary key as a foreign key of the 1461 01:26:56,560 --> 01:26:59,960 playlist entity let me show 1462 01:26:59,960 --> 01:27:02,679 you inside these 1463 01:27:02,679 --> 01:27:07,760 songs we have a playlist 1464 01:27:12,080 --> 01:27:15,360 ID relations have created successfully 1465 01:27:15,360 --> 01:27:18,040 we created one to many and many to one 1466 01:27:18,040 --> 01:27:19,960 now I want to show you how can you save 1467 01:27:19,960 --> 01:27:23,119 a record with relation 1468 01:27:23,119 --> 01:27:24,639 entities 1469 01:27:24,639 --> 01:27:27,000 go ahead and create a playlist module 1470 01:27:27,000 --> 01:27:30,560 you can use with n CLI to create a 1471 01:27:30,560 --> 01:27:32,560 playlist module but I'm going to use my 1472 01:27:32,560 --> 01:27:33,800 not 1473 01:27:33,800 --> 01:27:36,440 notes 1474 01:27:36,440 --> 01:27:39,440 playlists 1475 01:27:41,280 --> 01:27:43,920 module. and I'm going to paste my 1476 01:27:43,920 --> 01:27:47,119 playlist module we don't have playlist 1477 01:27:47,119 --> 01:27:50,320 service and we don't have playlist 1478 01:27:50,320 --> 01:27:56,119 controller I can create these two files 1479 01:27:57,719 --> 01:27:59,639 controller. let's 1480 01:27:59,639 --> 01:28:01,920 create playlist 1481 01:28:01,920 --> 01:28:04,080 dot 1482 01:28:04,080 --> 01:28:06,960 service. 1483 01:28:06,960 --> 01:28:10,639 file I'm going to copy 1484 01:28:10,639 --> 01:28:14,119 my playlist 1485 01:28:20,800 --> 01:28:26,119 service we have to import couple of tip 1486 01:28:28,600 --> 01:28:32,159 dependencies inject 1487 01:28:32,560 --> 01:28:36,400 dependency and we need 1488 01:28:37,480 --> 01:28:41,080 playlist we need a 1489 01:28:42,119 --> 01:28:43,840 user 1490 01:28:43,840 --> 01:28:47,679 we we need a song that is good right 1491 01:28:47,679 --> 01:28:51,360 now also need to get the 1492 01:28:51,360 --> 01:28:54,080 injectable we also need to import report 1493 01:28:54,080 --> 01:28:56,560 osit 1494 01:28:58,520 --> 01:29:04,760 Tre I don't have any user so import the 1495 01:29:04,920 --> 01:29:09,239 user we have service right now and we 1496 01:29:09,239 --> 01:29:12,400 going to create 1497 01:29:12,400 --> 01:29:14,080 controller 1498 01:29:14,080 --> 01:29:16,719 or we can 1499 01:29:16,719 --> 01:29:20,920 build create playlist 1500 01:29:23,040 --> 01:29:28,199 function I'm going to create a new 1501 01:29:28,560 --> 01:29:32,000 method you can see that we don't have 1502 01:29:32,000 --> 01:29:33,360 create playlist 1503 01:29:33,360 --> 01:29:37,400 dto we are just creating the instance of 1504 01:29:37,400 --> 01:29:39,800 playlist I'm providing the name of the 1505 01:29:39,800 --> 01:29:41,639 playlist when you go to the playlist 1506 01:29:41,639 --> 01:29:45,800 entity you have the name 1507 01:29:47,440 --> 01:29:51,080 property each playlist can have multiple 1508 01:29:51,080 --> 01:29:55,000 songs so I will get IDs of the songs 1509 01:29:55,000 --> 01:29:57,080 from the request 1510 01:29:57,080 --> 01:30:00,000 body we are using the same logic in the 1511 01:30:00,000 --> 01:30:01,440 previous video we 1512 01:30:01,440 --> 01:30:04,679 used here I'm setting the relation with 1513 01:30:04,679 --> 01:30:06,199 songs 1514 01:30:06,199 --> 01:30:09,119 entity I am creating the playlist when 1515 01:30:09,119 --> 01:30:10,639 you are when you need to create a 1516 01:30:10,639 --> 01:30:13,199 playlist you have to provide the 1517 01:30:13,199 --> 01:30:15,880 name and you can set the relation for 1518 01:30:15,880 --> 01:30:17,080 the 1519 01:30:17,080 --> 01:30:19,679 songs and you can set the 1520 01:30:19,679 --> 01:30:22,560 relationship for the 1521 01:30:22,560 --> 01:30:25,080 user when we will implement the 1522 01:30:25,080 --> 01:30:27,800 authentication I will not find the user 1523 01:30:27,800 --> 01:30:30,719 directly from the database I'll get it 1524 01:30:30,719 --> 01:30:32,880 from the logged in 1525 01:30:32,880 --> 01:30:35,400 user we have set the relationship with 1526 01:30:35,400 --> 01:30:39,320 user and finally we are saving the 1527 01:30:39,320 --> 01:30:42,679 playlist we don't have create playlist 1528 01:30:42,679 --> 01:30:46,440 D go ahead and create a playlist 1529 01:30:46,440 --> 01:30:49,360 D we have used that 1530 01:30:49,360 --> 01:30:52,920 logic I'm going to create a new file 1531 01:30:52,920 --> 01:30:55,360 playlist 1532 01:30:55,360 --> 01:30:57,840 dot or you can 1533 01:30:57,840 --> 01:31:01,679 say it should be new 1534 01:31:01,719 --> 01:31:05,320 folder dto inside the 1535 01:31:05,320 --> 01:31:08,560 dto playlist create playlist 1536 01:31:08,560 --> 01:31:12,000 dto create 1537 01:31:12,000 --> 01:31:16,040 playlist do 1538 01:31:21,400 --> 01:31:25,320 D.S we are expecting the name type 1539 01:31:25,320 --> 01:31:28,719 should be string and I'm getting the IDS 1540 01:31:28,719 --> 01:31:32,080 of the song from the request body that's 1541 01:31:32,080 --> 01:31:34,440 why I specified the number type the 1542 01:31:34,440 --> 01:31:37,280 array of the numbers and we have the 1543 01:31:37,280 --> 01:31:39,840 user it should be a number it should be 1544 01:31:39,840 --> 01:31:42,080 the 1545 01:31:42,800 --> 01:31:46,119 ID and finally we have to use the create 1546 01:31:46,119 --> 01:31:47,560 playlist 1547 01:31:47,560 --> 01:31:51,560 dto we have to import 1548 01:31:52,840 --> 01:31:56,119 it and now we have to build our 1549 01:31:56,119 --> 01:31:59,360 controller function and the 1550 01:31:59,360 --> 01:32:02,280 endpoint do we have a controller yes we 1551 01:32:02,280 --> 01:32:04,199 have a 1552 01:32:04,199 --> 01:32:06,639 controller everything is good to go I've 1553 01:32:06,639 --> 01:32:08,520 have specified the playlist with this 1554 01:32:08,520 --> 01:32:11,600 endpoint we injected the playlist 1555 01:32:11,600 --> 01:32:14,639 service we have used that logic and I'm 1556 01:32:14,639 --> 01:32:20,119 returning a simple playlist in the 1557 01:32:20,119 --> 01:32:23,000 response we have to register the 1558 01:32:23,000 --> 01:32:26,360 playlist module inside the app module 1559 01:32:26,360 --> 01:32:30,040 and it will also be treated as a feature 1560 01:32:30,040 --> 01:32:32,280 module just like we did with songs 1561 01:32:32,280 --> 01:32:36,080 module we have playlist 1562 01:32:36,679 --> 01:32:40,679 module now I have to test the 1563 01:32:40,679 --> 01:32:43,199 application you can test it by sending 1564 01:32:43,199 --> 01:32:45,440 the 1565 01:32:46,639 --> 01:32:49,679 request do we have a user I already 1566 01:32:49,679 --> 01:32:52,119 created the user in the previous 1567 01:32:52,119 --> 01:32:56,040 video I can double check check it my 1568 01:32:56,040 --> 01:32:59,400 users you can see that we have three 1569 01:32:59,400 --> 01:33:02,080 users I would like to save the record 1570 01:33:02,080 --> 01:33:06,880 for the second user with ID number 1571 01:33:06,880 --> 01:33:10,159 two rest 1572 01:33:10,159 --> 01:33:12,920 client we have to create a new playlist 1573 01:33:12,920 --> 01:33:15,600 I can say create new 1574 01:33:15,600 --> 01:33:19,800 playlist we have to send the post 1575 01:33:20,040 --> 01:33:25,760 request Local Host column 3000 playlists 1576 01:33:26,560 --> 01:33:29,400 content type is equals to 1577 01:33:29,400 --> 01:33:30,960 Json 1578 01:33:30,960 --> 01:33:33,320 application 1579 01:33:33,320 --> 01:33:37,239 Json let's provide the 1580 01:33:40,480 --> 01:33:43,400 data I'm going to create a new playlist 1581 01:33:43,400 --> 01:33:46,440 with Feelgood now I would like to save 1582 01:33:46,440 --> 01:33:49,520 the song with ID number six do we have 1583 01:33:49,520 --> 01:33:52,360 the song with ID number six let me 1584 01:33:52,360 --> 01:33:55,719 double check it 1585 01:34:00,199 --> 01:34:03,920 yes we have the song with ID number 1586 01:34:03,920 --> 01:34:06,840 six and this is our user let's say I'm 1587 01:34:06,840 --> 01:34:08,199 going to say I'm going to create with 1588 01:34:08,199 --> 01:34:11,080 user ID number 1589 01:34:15,400 --> 01:34:18,920 two it's not a 1590 01:34:18,920 --> 01:34:21,920 valid 1591 01:34:22,719 --> 01:34:25,719 request 1592 01:34:29,119 --> 01:34:32,480 now it looks fine we need to add a space 1593 01:34:32,480 --> 01:34:36,560 here we have a playlist feel good now 1594 01:34:36,560 --> 01:34:39,520 and we have a one song with ID number 1595 01:34:39,520 --> 01:34:43,280 six and we have this user so this user 1596 01:34:43,280 --> 01:34:46,239 has playlist feel good now and it has 1597 01:34:46,239 --> 01:34:48,800 only one 1598 01:34:48,840 --> 01:34:52,639 song you can double check it from the 1599 01:34:52,639 --> 01:34:55,639 playlist 1600 01:34:59,320 --> 01:35:02,280 this is the playlist 1601 01:35:05,960 --> 01:35:08,400 record in this video I'm going to teach 1602 01:35:08,400 --> 01:35:11,040 you how to connect nestjs application 1603 01:35:11,040 --> 01:35:13,360 with type orm and 1604 01:35:13,360 --> 01:35:16,920 post nestjs provide multiple drivers to 1605 01:35:16,920 --> 01:35:19,719 connect with database if you want to use 1606 01:35:19,719 --> 01:35:22,320 mongod DB you can also do that if you 1607 01:35:22,320 --> 01:35:24,719 want to use my 1608 01:35:24,719 --> 01:35:27,440 if you want to use post you can do that 1609 01:35:27,440 --> 01:35:29,800 in this video I'm going to implement the 1610 01:35:29,800 --> 01:35:32,400 post with 1611 01:35:32,400 --> 01:35:35,440 nestjs uh typ orm is a object relational 1612 01:35:35,440 --> 01:35:37,639 mapper you will write the code in 1613 01:35:37,639 --> 01:35:40,639 JavaScript objects and these objects or 1614 01:35:40,639 --> 01:35:43,360 classes will be converted into database 1615 01:35:43,360 --> 01:35:46,159 tables if you want to write SQL queries 1616 01:35:46,159 --> 01:35:49,159 you don't need to write plain SQL cury 1617 01:35:49,159 --> 01:35:52,639 you can write queries in the form of 1618 01:35:52,639 --> 01:35:55,159 objects 1619 01:35:55,159 --> 01:35:56,600 first of all you have to install a 1620 01:35:56,600 --> 01:36:00,000 couple of dependencies we need nestjs 1621 01:36:00,000 --> 01:36:05,159 typ orm package and post driver and type 1622 01:36:05,159 --> 01:36:08,719 orm uh open the package tojson file and 1623 01:36:08,719 --> 01:36:11,119 you have to install these dependencies 1624 01:36:11,119 --> 01:36:13,040 I'm going to add them 1625 01:36:13,040 --> 01:36:17,679 here let me install all of these 1626 01:36:22,560 --> 01:36:25,560 dependencies 1627 01:36:28,040 --> 01:36:32,239 we also need typing for 1628 01:36:32,840 --> 01:36:37,199 nestjs we have to install this 1629 01:36:38,119 --> 01:36:42,800 package I'm going to install as a def 1630 01:36:45,560 --> 01:36:47,520 dependency we 1631 01:36:47,520 --> 01:36:52,520 already have this package types at 1632 01:36:52,520 --> 01:36:55,520 node 1633 01:36:59,920 --> 01:37:03,199 we installed these packages njs type orm 1634 01:37:03,199 --> 01:37:05,800 post and type 1635 01:37:05,800 --> 01:37:09,639 orm so now we need to import the type 1636 01:37:09,639 --> 01:37:13,679 orm module into our root module in our 1637 01:37:13,679 --> 01:37:16,840 case root module is our app 1638 01:37:16,840 --> 01:37:20,440 module so we have to call this method 1639 01:37:20,440 --> 01:37:23,600 from type or a module and you have to 1640 01:37:23,600 --> 01:37:25,280 provide the connection strings or 1641 01:37:25,280 --> 01:37:28,159 connection properties like type host 1642 01:37:28,159 --> 01:37:32,639 Port username password database entities 1643 01:37:32,639 --> 01:37:33,400 and 1644 01:37:33,400 --> 01:37:35,880 synchronize whenever you will create a 1645 01:37:35,880 --> 01:37:38,920 new entity you have to register it here 1646 01:37:38,920 --> 01:37:41,239 for synchronize means whenever I will 1647 01:37:41,239 --> 01:37:43,639 run the application please delete the 1648 01:37:43,639 --> 01:37:47,199 data from all the all the tables 1649 01:37:47,199 --> 01:37:49,360 synchronize please delete everything 1650 01:37:49,360 --> 01:37:50,920 from the table but do not use 1651 01:37:50,920 --> 01:37:53,520 synchronize in production otherwise you 1652 01:37:53,520 --> 01:37:56,159 will lose the production 1653 01:37:56,159 --> 01:37:58,480 data whenever you have to create a new 1654 01:37:58,480 --> 01:38:00,560 entity you have to add it here inside 1655 01:38:00,560 --> 01:38:03,239 the entities array so the for root 1656 01:38:03,239 --> 01:38:05,599 method Sports all the configuration 1657 01:38:05,599 --> 01:38:07,960 properties exposed by the data source 1658 01:38:07,960 --> 01:38:11,960 Constructor from the type or a 1659 01:38:14,040 --> 01:38:17,199 package I have installed PG admin if you 1660 01:38:17,199 --> 01:38:19,199 don't have you can install the post 1661 01:38:19,199 --> 01:38:22,719 admin I'm using PG admin 4 you have to 1662 01:38:22,719 --> 01:38:27,719 provide your password let's connect with 1663 01:38:28,639 --> 01:38:31,679 server we have to create a new database 1664 01:38:31,679 --> 01:38:32,639 right 1665 01:38:32,639 --> 01:38:35,719 now I'm going to create a new database 1666 01:38:35,719 --> 01:38:38,440 Spotify 1667 01:38:40,000 --> 01:38:43,639 clone this is my 1668 01:38:50,280 --> 01:38:54,920 user go to the app module 1669 01:38:54,920 --> 01:38:58,440 and we have to import the type type or 1670 01:38:58,440 --> 01:39:00,960 module 1671 01:39:22,119 --> 01:39:24,960 here we have have to install manually 1672 01:39:24,960 --> 01:39:27,679 it's not giving me 1673 01:39:27,679 --> 01:39:30,679 suggestion 1674 01:39:33,320 --> 01:39:34,960 from 1675 01:39:34,960 --> 01:39:38,080 Nest JS type 1676 01:39:38,080 --> 01:39:43,320 orm and we need to get type omm 1677 01:39:44,239 --> 01:39:47,719 module so I'm going to call a method 1678 01:39:47,719 --> 01:39:50,480 type orm 1679 01:39:51,080 --> 01:39:53,719 module do for root with we have to 1680 01:39:53,719 --> 01:39:56,000 provide the 1681 01:39:56,000 --> 01:39:58,480 options database which 1682 01:39:58,480 --> 01:40:01,760 is Spotify 1683 01:40:01,760 --> 01:40:07,360 clone host which should be Local 1684 01:40:08,639 --> 01:40:11,880 Host and we need to specify the port for 1685 01:40:11,880 --> 01:40:15,119 post gr I think 4 1686 01:40:15,119 --> 01:40:18,400 5432 by 1687 01:40:18,840 --> 01:40:23,599 default username for me it's postcript 1688 01:40:23,599 --> 01:40:26,599 add you have to provide your password 1689 01:40:26,599 --> 01:40:29,800 for me I think it's 1690 01:40:31,239 --> 01:40:35,199 root I specified the database name and 1691 01:40:35,199 --> 01:40:38,920 we have empty entities right now I don't 1692 01:40:38,920 --> 01:40:40,119 have any 1693 01:40:40,119 --> 01:40:43,679 entity and synchronize set to 1694 01:40:43,679 --> 01:40:48,239 true it will create the database 1695 01:40:49,599 --> 01:40:52,239 tables 1696 01:40:52,239 --> 01:40:55,239 po 1697 01:40:59,159 --> 01:41:03,080 it should be usern name not a 1698 01:41:07,920 --> 01:41:11,639 user so how can you verify we have 1699 01:41:11,639 --> 01:41:14,679 successfully connected to type 1700 01:41:14,679 --> 01:41:19,599 orm you can use data source object in 1701 01:41:19,599 --> 01:41:21,440 the app 1702 01:41:21,440 --> 01:41:25,320 module now you can inject 1703 01:41:25,320 --> 01:41:27,960 it and I can 1704 01:41:27,960 --> 01:41:31,760 log let's add 1705 01:41:33,639 --> 01:41:37,199 private you can log anything 1706 01:41:37,199 --> 01:41:41,159 from DB name let's 1707 01:41:41,400 --> 01:41:44,400 say and I can get it from the data 1708 01:41:44,400 --> 01:41:46,880 source 1709 01:41:47,040 --> 01:41:50,400 dot data 1710 01:41:51,840 --> 01:41:54,280 source. database 1711 01:41:54,280 --> 01:41:57,280 this do I have that property let me 1712 01:41:57,280 --> 01:41:58,159 check 1713 01:41:58,159 --> 01:42:05,080 it dat source. driver dot 1714 01:42:07,000 --> 01:42:10,760 database Let's test it 1715 01:42:22,159 --> 01:42:26,000 out I'm getting the 1716 01:42:26,320 --> 01:42:29,840 error wrong 1717 01:42:31,199 --> 01:42:36,880 driver so I think I did not provide the 1718 01:42:38,320 --> 01:42:41,679 type let's add the 1719 01:42:41,679 --> 01:42:46,480 type the type of the driver which is 1720 01:42:52,119 --> 01:42:55,119 post 1721 01:43:06,159 --> 01:43:08,400 now we have made the connection 1722 01:43:08,400 --> 01:43:11,080 successfully you can see DB name which 1723 01:43:11,080 --> 01:43:13,320 is Spotify 1724 01:43:13,320 --> 01:43:16,080 clone you can also 1725 01:43:16,080 --> 01:43:20,760 check the songs table inside the Spotify 1726 01:43:20,760 --> 01:43:23,760 Let me refresh it and I would like to 1727 01:43:23,760 --> 01:43:25,400 look at the 1728 01:43:25,400 --> 01:43:29,000 tables we have the 1729 01:43:29,199 --> 01:43:32,440 tables I don't have any song because we 1730 01:43:32,440 --> 01:43:35,800 did not create any entity right 1731 01:43:35,800 --> 01:43:38,440 now but we have successfully 1732 01:43:38,440 --> 01:43:42,040 connected to the postris 1733 01:43:42,040 --> 01:43:44,440 database in this lesson I'm going to 1734 01:43:44,440 --> 01:43:48,159 teach you how to create an entity entity 1735 01:43:48,159 --> 01:43:51,800 is a class that maps to database table 1736 01:43:51,800 --> 01:43:54,119 or collection when us using 1737 01:43:54,119 --> 01:43:56,880 mongodb you can create entity by 1738 01:43:56,880 --> 01:43:59,840 defining a new class and Mark it with an 1739 01:43:59,840 --> 01:44:01,880 entity 1740 01:44:01,880 --> 01:44:04,719 metadata and inside the metadata you can 1741 01:44:04,719 --> 01:44:08,400 provide the name of the table in our 1742 01:44:08,400 --> 01:44:10,520 case I'm going to provide the songs 1743 01:44:10,520 --> 01:44:11,400 table 1744 01:44:11,400 --> 01:44:15,080 name an ID which is the primary 1745 01:44:15,080 --> 01:44:18,159 generated column the ID will be 1746 01:44:18,159 --> 01:44:22,040 generated automatically it's an auto 1747 01:44:22,040 --> 01:44:24,920 increment and we have the title I use 1748 01:44:24,920 --> 01:44:26,320 the column 1749 01:44:26,320 --> 01:44:31,400 decorator for artist array postris also 1750 01:44:31,400 --> 01:44:34,719 Sports error data type you have to 1751 01:44:34,719 --> 01:44:37,639 provide the column and I'm using the V 1752 01:44:37,639 --> 01:44:41,000 character you have to say this column is 1753 01:44:41,000 --> 01:44:43,440 an array type you have to specify the 1754 01:44:43,440 --> 01:44:45,159 array should be 1755 01:44:45,159 --> 01:44:48,159 true and we have a release dat property 1756 01:44:48,159 --> 01:44:50,760 or release dat field in the database 1757 01:44:50,760 --> 01:44:53,280 table I'm specifying the call colum 1758 01:44:53,280 --> 01:44:57,080 decorator and we have and we are seeing 1759 01:44:57,080 --> 01:45:00,440 this the type of this field which is 1760 01:45:00,440 --> 01:45:04,719 State we also have time type in the 1761 01:45:04,719 --> 01:45:07,760 post you can say we have the duration I 1762 01:45:07,760 --> 01:45:11,560 specified the date data type in the post 1763 01:45:11,560 --> 01:45:13,560 Cas you can Define the time I don't want 1764 01:45:13,560 --> 01:45:16,280 to save extra date field here I only 1765 01:45:16,280 --> 01:45:19,360 need Time Properties time related 1766 01:45:19,360 --> 01:45:22,040 metadata and lyrics lyrics could be the 1767 01:45:22,040 --> 01:45:25,360 long text that that's why Post sports 1768 01:45:25,360 --> 01:45:28,840 text type if you want a long if you want 1769 01:45:28,840 --> 01:45:31,840 to store a string with long content or 1770 01:45:31,840 --> 01:45:34,560 long long 1771 01:45:34,560 --> 01:45:37,920 length entity songs which is the name of 1772 01:45:37,920 --> 01:45:40,119 the 1773 01:45:41,199 --> 01:45:44,239 table and you can say I spec I 1774 01:45:44,239 --> 01:45:47,520 have I I have specified the description 1775 01:45:47,520 --> 01:45:50,119 for each 1776 01:45:51,920 --> 01:45:53,760 field 1777 01:45:53,760 --> 01:45:57,520 when you will create the entity you also 1778 01:45:57,520 --> 01:46:00,520 need to update the lyrics in the create 1779 01:46:00,520 --> 01:46:03,360 song datto we did not we did not add 1780 01:46:03,360 --> 01:46:06,119 lyrics property in the previous videos 1781 01:46:06,119 --> 01:46:09,080 that's why I use the lyrics it's an 1782 01:46:09,080 --> 01:46:11,920 optional property it depends on the user 1783 01:46:11,920 --> 01:46:15,960 does user want to add lyrics or 1784 01:46:16,360 --> 01:46:19,520 not and finally we need to register the 1785 01:46:19,520 --> 01:46:22,760 entity to the app module and then you 1786 01:46:22,760 --> 01:46:25,199 can can test the 1787 01:46:25,199 --> 01:46:28,840 application let's try to implement 1788 01:46:28,840 --> 01:46:33,080 it go ahead and create an entity I can 1789 01:46:33,080 --> 01:46:34,000 say 1790 01:46:34,000 --> 01:46:37,280 songs or song. 1791 01:46:37,280 --> 01:46:40,239 entity songs. entity or song whatever 1792 01:46:40,239 --> 01:46:44,320 you want to call let me say what I 1793 01:46:46,000 --> 01:46:49,679 called song. 1794 01:46:51,840 --> 01:46:54,840 entity 1795 01:46:56,520 --> 01:47:00,400 dots let's specify the 1796 01:47:00,400 --> 01:47:04,040 class I can say 1797 01:47:06,119 --> 01:47:10,199 song I can provide the 1798 01:47:11,040 --> 01:47:15,040 entity the name of the table which is 1799 01:47:15,040 --> 01:47:17,239 songs now we 1800 01:47:17,239 --> 01:47:20,280 need primary generated 1801 01:47:20,280 --> 01:47:24,400 column ID the type should be 1802 01:47:24,400 --> 01:47:28,599 number and we have the title 1803 01:47:28,599 --> 01:47:32,490 field let's specify the title 1804 01:47:32,490 --> 01:47:35,930 [Music] 1805 01:47:36,360 --> 01:47:38,400 column 1806 01:47:38,400 --> 01:47:41,440 title type should be 1807 01:47:41,440 --> 01:47:45,320 string and we have another column this 1808 01:47:45,320 --> 01:47:47,760 time we have the artist 1809 01:47:47,760 --> 01:47:51,520 array you have to specify it as a string 1810 01:47:51,520 --> 01:47:56,520 array here I I can say the V 1811 01:47:58,280 --> 01:48:02,199 character V character and 1812 01:48:02,199 --> 01:48:05,320 the type should be 1813 01:48:05,320 --> 01:48:08,320 aray and we 1814 01:48:08,320 --> 01:48:13,440 have release date duration release 1815 01:48:13,520 --> 01:48:15,280 date 1816 01:48:15,280 --> 01:48:19,320 let's it should be 1817 01:48:19,560 --> 01:48:24,880 date release date type should be 1818 01:48:24,880 --> 01:48:28,119 date and we have 1819 01:48:28,119 --> 01:48:30,960 duration type should be 1820 01:48:30,960 --> 01:48:32,480 time 1821 01:48:32,480 --> 01:48:35,639 duration and the 1822 01:48:35,639 --> 01:48:38,840 date and finally we have the 1823 01:48:38,840 --> 01:48:42,840 lyrics type should be 1824 01:48:44,480 --> 01:48:47,480 text 1825 01:48:48,320 --> 01:48:51,719 lyrics type should be 1826 01:48:51,719 --> 01:48:54,719 string 1827 01:48:55,520 --> 01:48:58,880 now we need to test this 1828 01:48:58,880 --> 01:49:01,679 entity you have to 1829 01:49:01,679 --> 01:49:05,199 add register the entity in the song in 1830 01:49:05,199 --> 01:49:07,040 the entities 1831 01:49:07,040 --> 01:49:10,400 array now it should create the songs 1832 01:49:10,400 --> 01:49:13,119 table for 1833 01:49:15,119 --> 01:49:18,920 us let me test it out from the PG 1834 01:49:18,920 --> 01:49:22,400 admin refresh the tables I got the songs 1835 01:49:22,400 --> 01:49:24,280 table 1836 01:49:24,280 --> 01:49:28,119 with these columns 1837 01:49:28,199 --> 01:49:34,360 ID title artist release date and the 1838 01:49:35,880 --> 01:49:40,239 duration we don't have any 1839 01:49:42,270 --> 01:49:45,339 [Music] 1840 01:49:46,400 --> 01:49:49,960 record so 1841 01:49:51,639 --> 01:49:54,639 let's 1842 01:49:54,679 --> 01:49:57,880 update the create song dto and I'm going 1843 01:49:57,880 --> 01:49:59,280 to copy 1844 01:49:59,280 --> 01:50:03,520 it and I'm going to paste it 1845 01:50:03,520 --> 01:50:07,080 here create song dto let's add another 1846 01:50:07,080 --> 01:50:10,840 field the optional which is 1847 01:50:15,320 --> 01:50:18,080 lyrics everything is fine 1848 01:50:18,080 --> 01:50:21,000 now we have tested 1849 01:50:21,000 --> 01:50:25,639 it in the next video you are going to 1850 01:50:25,639 --> 01:50:29,520 learn how to perform C 1851 01:50:29,520 --> 01:50:32,599 operation we're going to fill these 1852 01:50:32,599 --> 01:50:36,080 methods update delete find one find all 1853 01:50:36,080 --> 01:50:39,080 and the create this time we going to 1854 01:50:39,080 --> 01:50:43,920 fetch and save data to the 1855 01:50:43,920 --> 01:50:47,480 database let's perform cred operations 1856 01:50:47,480 --> 01:50:50,520 type orm Sports repository 1857 01:50:50,520 --> 01:50:53,599 pattern for each entity type orm 1858 01:50:53,599 --> 01:50:56,760 provides repository and this repository 1859 01:50:56,760 --> 01:50:59,719 has CR features like create create 1860 01:50:59,719 --> 01:51:03,800 record update record find record and 1861 01:51:03,800 --> 01:51:08,280 delete record repository provides the 1862 01:51:10,280 --> 01:51:14,480 method first of all you have to register 1863 01:51:14,480 --> 01:51:17,320 the type or a module as a feature into 1864 01:51:17,320 --> 01:51:19,960 the songs 1865 01:51:21,560 --> 01:51:23,679 module 1866 01:51:23,679 --> 01:51:26,639 we providing the 1867 01:51:29,560 --> 01:51:32,480 entity like these entities I would like 1868 01:51:32,480 --> 01:51:34,639 to use inside the songs 1869 01:51:34,639 --> 01:51:37,800 module and it it will provide the 1870 01:51:37,800 --> 01:51:41,360 repository songs repository here then 1871 01:51:41,360 --> 01:51:43,560 you can inject the songs 1872 01:51:43,560 --> 01:51:46,920 repository to use songs repository you 1873 01:51:46,920 --> 01:51:49,360 need to import the type or a module into 1874 01:51:49,360 --> 01:51:52,280 songs module just like we did it here 1875 01:51:52,280 --> 01:51:54,920 this this module uses the four feature 1876 01:51:54,920 --> 01:51:58,159 method to Define which repositories are 1877 01:51:58,159 --> 01:52:01,040 registered in the current scope we only 1878 01:52:01,040 --> 01:52:03,880 need song entity that's why we use the 1879 01:52:03,880 --> 01:52:08,000 we have imported or added the song 1880 01:52:08,000 --> 01:52:10,560 entity now we can inject the songs 1881 01:52:10,560 --> 01:52:12,920 repository into the song service just 1882 01:52:12,920 --> 01:52:15,880 like we did we did it as a dependency 1883 01:52:15,880 --> 01:52:18,119 injection please inject the song's 1884 01:52:18,119 --> 01:52:20,320 repository and the type of the entity 1885 01:52:20,320 --> 01:52:23,599 which is song and you can in you can 1886 01:52:23,599 --> 01:52:28,960 import repository from nestjs type or M 1887 01:52:30,400 --> 01:52:33,560 package now I told you songs repository 1888 01:52:33,560 --> 01:52:38,199 provides scrud method to create delete 1889 01:52:38,360 --> 01:52:42,079 update here you can look at 1890 01:52:42,079 --> 01:52:44,800 that create delete fetch records from 1891 01:52:44,800 --> 01:52:46,239 the songs 1892 01:52:46,239 --> 01:52:50,119 table let's implement this create song 1893 01:52:50,119 --> 01:52:51,960 method this time we don't need to add a 1894 01:52:51,960 --> 01:52:53,800 record in the local DB you're going to 1895 01:52:53,800 --> 01:52:56,599 save the new song by using the song 1896 01:52:56,599 --> 01:52:58,560 repository. save 1897 01:52:58,560 --> 01:53:02,239 method let's Implement 1898 01:53:02,239 --> 01:53:06,199 here in the songs 1899 01:53:07,880 --> 01:53:10,360 module we have to 1900 01:53:10,360 --> 01:53:15,159 import type orm module as a 1901 01:53:17,840 --> 01:53:21,440 feature provide the song entity I would 1902 01:53:21,440 --> 01:53:24,679 like to use this entity into the songs 1903 01:53:24,679 --> 01:53:27,320 model in songs 1904 01:53:27,320 --> 01:53:31,119 module now you you are able to inject 1905 01:53:31,119 --> 01:53:34,599 songs repository into the song 1906 01:53:34,599 --> 01:53:37,960 service so I want you to inject this 1907 01:53:37,960 --> 01:53:40,719 songs 1908 01:53:44,159 --> 01:53:47,880 repository Constructor 1909 01:53:48,520 --> 01:53:51,440 pattern songs 1910 01:53:51,440 --> 01:53:54,440 Repository 1911 01:54:00,040 --> 01:54:02,840 repository and I can provide the song 1912 01:54:02,840 --> 01:54:05,679 type which is 1913 01:54:06,760 --> 01:54:13,239 entity now you have the exess of crud 1914 01:54:13,239 --> 01:54:17,280 methods so song repository provides this 1915 01:54:17,280 --> 01:54:20,400 safe method just like you learned here 1916 01:54:20,400 --> 01:54:22,639 it provides this safe method for first 1917 01:54:22,639 --> 01:54:24,000 of 1918 01:54:24,000 --> 01:54:29,000 all we have to create a new 1919 01:54:29,239 --> 01:54:32,199 song and it should 1920 01:54:32,199 --> 01:54:34,719 return a 1921 01:54:34,719 --> 01:54:38,400 promise with 1922 01:54:39,920 --> 01:54:43,880 song so I just 1923 01:54:43,880 --> 01:54:46,599 created should say 1924 01:54:46,599 --> 01:54:50,320 create song 1925 01:54:50,320 --> 01:54:54,320 dto the type should be 1926 01:54:54,440 --> 01:54:57,320 create Song 1927 01:54:59,599 --> 01:55:03,079 D so I just created the instance from a 1928 01:55:03,079 --> 01:55:06,679 song class I specified the title I got 1929 01:55:06,679 --> 01:55:09,400 the title from D I got the artist from 1930 01:55:09,400 --> 01:55:12,119 dto duration lyrics and release date 1931 01:55:12,119 --> 01:55:15,679 from the dto and finally you can 1932 01:55:15,679 --> 01:55:20,639 call Safe method this. songs 1933 01:55:20,639 --> 01:55:22,960 repository and I will would like to call 1934 01:55:22,960 --> 01:55:25,760 this safe method and provide a newly 1935 01:55:25,760 --> 01:55:28,560 created song and it should return the 1936 01:55:28,560 --> 01:55:30,679 object get rid of the songs array I 1937 01:55:30,679 --> 01:55:33,360 don't want to use 1938 01:55:33,960 --> 01:55:38,159 it we also need to remove everything 1939 01:55:38,159 --> 01:55:40,599 from 1940 01:55:47,960 --> 01:55:54,239 here now it's time to test this G 1941 01:56:04,840 --> 01:56:10,280 method make sure you have 1942 01:56:11,360 --> 01:56:14,239 called 1943 01:56:14,239 --> 01:56:19,520 yeah it should use the up 1944 01:56:19,520 --> 01:56:21,239 expression 1945 01:56:21,239 --> 01:56:24,239 here 1946 01:56:48,199 --> 01:56:51,239 in the controller let's look 1947 01:56:51,239 --> 01:56:52,760 at 1948 01:56:52,760 --> 01:56:55,800 issue here it 1949 01:56:55,800 --> 01:57:00,239 should promise and the 1950 01:57:07,960 --> 01:57:11,719 song inject the 1951 01:57:15,199 --> 01:57:18,760 repository inject 1952 01:57:20,760 --> 01:57:24,040 repository and we we have to provide the 1953 01:57:24,040 --> 01:57:26,760 entity which is 1954 01:57:41,199 --> 01:57:44,040 song so everything is fine you can test 1955 01:57:44,040 --> 01:57:48,440 without a sync V by default it should 1956 01:57:48,440 --> 01:57:50,840 return the 1957 01:57:51,119 --> 01:57:54,119 promise 1958 01:58:10,599 --> 01:58:15,199 HTTP let's create a new 1959 01:58:17,960 --> 01:58:21,119 song we also need 1960 01:58:21,119 --> 01:58:24,119 lyrics 1961 01:58:37,920 --> 01:58:40,480 I'm going to send the 1962 01:58:40,480 --> 01:58:42,360 request 1963 01:58:42,360 --> 01:58:46,880 amazing we have a song you can verify 1964 01:58:46,880 --> 01:58:49,880 from the 1965 01:58:50,440 --> 01:58:53,480 database you can see that that I got the 1966 01:58:53,480 --> 01:58:57,280 first record this is the lyrics duration 1967 01:58:57,280 --> 01:59:00,119 and the release 1968 01:59:07,159 --> 01:59:12,560 date now it's time to implement find 1969 01:59:13,760 --> 01:59:17,239 all it's easy to implement the find all 1970 01:59:17,239 --> 01:59:19,119 all you need to 1971 01:59:19,119 --> 01:59:23,639 do is provide the return type 1972 01:59:23,639 --> 01:59:26,599 promise with song 1973 01:59:26,599 --> 01:59:30,320 array return this do songs 1974 01:59:30,320 --> 01:59:32,810 repository do find that's all you 1975 01:59:32,810 --> 01:59:33,920 [Music] 1976 01:59:33,920 --> 01:59:36,920 need 1977 01:59:39,960 --> 01:59:43,400 and in the 1978 01:59:45,679 --> 01:59:49,280 controller songs 1979 01:59:51,040 --> 01:59:52,599 controller 1980 01:59:52,599 --> 01:59:56,520 you can also add 1981 02:00:01,480 --> 02:00:05,599 promise with song 1982 02:00:20,960 --> 02:00:23,960 aray 1983 02:00:29,800 --> 02:00:33,159 let's fetch all the songs from the DB I 1984 02:00:33,159 --> 02:00:38,040 got the array right now we have only one 1985 02:00:39,800 --> 02:00:42,639 record now we need to implement the find 1986 02:00:42,639 --> 02:00:45,560 one find by ID you can create a new 1987 02:00:45,560 --> 02:00:49,400 method inside the song 1988 02:00:50,199 --> 02:00:54,159 service and it is taking the ID as a 1989 02:00:54,159 --> 02:00:57,880 parameter and returning the promise with 1990 02:00:57,880 --> 02:01:00,560 song you can 1991 02:01:00,560 --> 02:01:05,679 use song repository. point1 1992 02:01:12,520 --> 02:01:16,079 method and in the 1993 02:01:16,440 --> 02:01:20,599 controller we need we need to pass the 1994 02:01:20,599 --> 02:01:22,960 ID as a integer 1995 02:01:22,960 --> 02:01:24,760 and return the 1996 02:01:24,760 --> 02:01:28,199 song in the 1997 02:01:36,400 --> 02:01:40,320 response let me open the songs 1998 02:01:40,320 --> 02:01:43,000 controller we already did 1999 02:01:43,000 --> 02:01:45,920 it I'm getting the 2000 02:01:45,920 --> 02:01:49,639 ID all you need to 2001 02:01:49,840 --> 02:01:53,239 do is return 2002 02:01:53,239 --> 02:01:55,360 this. song. 2003 02:01:55,360 --> 02:01:58,400 find1 here I can 2004 02:01:58,400 --> 02:02:02,880 say promise with 2005 02:02:09,119 --> 02:02:12,159 song Let's test it out by sending the 2006 02:02:12,159 --> 02:02:15,280 request only the based on ID you can see 2007 02:02:15,280 --> 02:02:18,480 I got the first record if I try to fetch 2008 02:02:18,480 --> 02:02:20,320 the second record I don't have any 2009 02:02:20,320 --> 02:02:23,840 record that's why I got the 2010 02:02:28,159 --> 02:02:33,719 empty let's implement the remove 2011 02:02:37,679 --> 02:02:41,360 method I'm going to create a new method 2012 02:02:41,360 --> 02:02:44,239 inside the song 2013 02:02:45,079 --> 02:02:49,360 service it's just returning 2014 02:02:50,840 --> 02:02:53,719 nothing 2015 02:02:53,719 --> 02:02:57,000 or you can say delete 2016 02:02:57,000 --> 02:03:00,679 result you can use 2017 02:03:01,360 --> 02:03:05,040 that and 2018 02:03:07,360 --> 02:03:10,360 return 2019 02:03:15,400 --> 02:03:17,800 here and we need 2020 02:03:17,800 --> 02:03:21,400 to use this method into the songs 2021 02:03:21,400 --> 02:03:23,760 control 2022 02:03:24,560 --> 02:03:27,599 ER we have to call 2023 02:03:27,599 --> 02:03:32,639 it this Do song service. 2024 02:03:32,639 --> 02:03:35,599 remot and we have to provide the 2025 02:03:35,599 --> 02:03:40,280 ID we did not pass the ID or get the ID 2026 02:03:40,280 --> 02:03:41,920 as a 2027 02:03:41,920 --> 02:03:46,239 Prem I'm going to use Prem 2028 02:03:46,440 --> 02:03:50,480 decorator and get it here 2029 02:03:50,719 --> 02:03:53,719 ID 2030 02:03:57,960 --> 02:04:01,400 and it's returning 2031 02:04:01,400 --> 02:04:05,440 promise with delayed 2032 02:04:16,960 --> 02:04:20,440 result we need to test it out first of 2033 02:04:20,440 --> 02:04:23,559 all let's create a new 2034 02:04:23,559 --> 02:04:26,320 song 2035 02:04:26,320 --> 02:04:28,920 to we have a 2036 02:04:28,920 --> 02:04:31,760 song lasting lever 2037 02:04:31,760 --> 02:04:35,400 two the ID of the song which is two now 2038 02:04:35,400 --> 02:04:37,360 you can perform the delete operation I 2039 02:04:37,360 --> 02:04:40,000 would like to delete this 2040 02:04:40,000 --> 02:04:44,800 record so I got the delete result we 2041 02:04:44,800 --> 02:04:46,239 have successfully 2042 02:04:46,239 --> 02:04:50,639 deleted if I try to fetch all the 2043 02:04:50,639 --> 02:04:52,960 songs 2044 02:04:52,960 --> 02:04:56,090 [Music] 2045 02:04:56,320 --> 02:05:00,599 no I think I deleted with ID number one 2046 02:05:00,599 --> 02:05:03,719 so first record has 2047 02:05:04,679 --> 02:05:09,679 deleted and we need to use the 2048 02:05:09,679 --> 02:05:14,280 update let's perform the update 2049 02:05:16,520 --> 02:05:19,159 record so we 2050 02:05:19,159 --> 02:05:22,320 need update method I'm going to create a 2051 02:05:22,320 --> 02:05:24,040 new 2052 02:05:24,040 --> 02:05:26,840 method inside the song service we don't 2053 02:05:26,840 --> 02:05:30,079 have update DTU data transfer object you 2054 02:05:30,079 --> 02:05:33,840 need a tip you need a new data transfer 2055 02:05:33,840 --> 02:05:36,040 object for update the 2056 02:05:36,040 --> 02:05:38,960 record because when you update the 2057 02:05:38,960 --> 02:05:42,679 record all the fields are optional but I 2058 02:05:42,679 --> 02:05:45,440 can get update result from type 2059 02:05:45,440 --> 02:05:50,400 orm we don't have update song dto we 2060 02:05:50,400 --> 02:05:53,239 have to create it 2061 02:05:53,239 --> 02:05:56,239 I'm going to create in the 2062 02:05:56,239 --> 02:05:58,239 dto update 2063 02:05:58,239 --> 02:06:01,800 song Das 2064 02:06:07,840 --> 02:06:11,400 d. so this time we have to copy 2065 02:06:11,400 --> 02:06:15,000 everything all the fields are optional 2066 02:06:15,000 --> 02:06:19,440 all I did I copied the create Song D and 2067 02:06:19,440 --> 02:06:22,760 put the optional for every title for 2068 02:06:22,760 --> 02:06:23,599 every 2069 02:06:23,599 --> 02:06:26,599 field 2070 02:06:27,000 --> 02:06:31,440 is title should be optional so I can say 2071 02:06:31,440 --> 02:06:35,000 instead of empty it should be 2072 02:06:35,000 --> 02:06:39,760 optional it should also cre it 2073 02:06:39,760 --> 02:06:43,239 here is 2074 02:06:46,480 --> 02:06:49,320 optional is 2075 02:06:49,320 --> 02:06:53,520 optional now it looks good 2076 02:06:56,920 --> 02:07:01,599 so we have to use the updates s 2077 02:07:07,159 --> 02:07:10,159 video now we need to implement the 2078 02:07:10,159 --> 02:07:11,760 controller 2079 02:07:11,760 --> 02:07:15,760 function songs. 2080 02:07:16,280 --> 02:07:19,800 controller that we need to get the Prem 2081 02:07:19,800 --> 02:07:23,360 and we need to get the 2082 02:07:23,360 --> 02:07:26,679 body need to get the pram and update 2083 02:07:26,679 --> 02:07:30,719 song dto and I'm going to call this do 2084 02:07:30,719 --> 02:07:33,360 song service. 2085 02:07:33,360 --> 02:07:36,239 update the first one is ID and the 2086 02:07:36,239 --> 02:07:40,440 second argument is update song 2087 02:07:44,079 --> 02:07:47,440 tto you have to import 2088 02:07:47,440 --> 02:07:52,000 it the return type should be promise 2089 02:07:52,000 --> 02:07:56,400 with update result that's 2090 02:08:05,159 --> 02:08:08,199 it I have found the 2091 02:08:08,199 --> 02:08:12,320 issue which is this space content 2092 02:08:12,320 --> 02:08:17,000 type let's try to send the update 2093 02:08:18,880 --> 02:08:23,440 request duration must be valid 2094 02:08:25,719 --> 02:08:29,159 presentation now it looks good so you 2095 02:08:29,159 --> 02:08:32,199 can double check it from the 2096 02:08:32,199 --> 02:08:36,199 database this is the record with 2097 02:08:36,199 --> 02:08:39,119 id2 we have successfully 2098 02:08:39,119 --> 02:08:41,840 updated the 2099 02:08:41,840 --> 02:08:45,880 record we have tested the 2100 02:08:47,920 --> 02:08:50,719 application in the next next lesson you 2101 02:08:50,719 --> 02:08:54,000 will learn how how to implement the 2102 02:08:54,000 --> 02:08:56,400 pagination now we're going to implement 2103 02:08:56,400 --> 02:08:57,960 the 2104 02:08:57,960 --> 02:09:00,719 pagination I'm going to use external 2105 02:09:00,719 --> 02:09:04,480 package to implement pagination in 2106 02:09:04,480 --> 02:09:06,840 nestjs uh first of all you have to 2107 02:09:06,840 --> 02:09:09,599 install this 2108 02:09:20,360 --> 02:09:22,119 package 2109 02:09:22,119 --> 02:09:25,440 I'm going to register it 2110 02:09:26,119 --> 02:09:30,840 here and we have to install 2111 02:09:40,960 --> 02:09:44,840 it and now I'm going to add a new method 2112 02:09:44,840 --> 02:09:47,960 inside the song service this time I'm 2113 02:09:47,960 --> 02:09:49,920 going to call it 2114 02:09:49,920 --> 02:09:54,320 paginate and I'll use paginate 2115 02:09:54,320 --> 02:09:58,840 method from nestjs type or M paginate 2116 02:09:58,840 --> 02:10:01,360 and it will accept the pagination 2117 02:10:01,360 --> 02:10:05,760 options like page metadata when you will 2118 02:10:05,760 --> 02:10:09,320 fetch the record on the pation it should 2119 02:10:09,320 --> 02:10:13,079 give me the items array and we have this 2120 02:10:13,079 --> 02:10:16,559 metadata total items item count items 2121 02:10:16,559 --> 02:10:20,040 per page total pages and current page 2122 02:10:20,040 --> 02:10:22,000 these are all the options 2123 02:10:22,000 --> 02:10:24,960 inside the options object and finally it 2124 02:10:24,960 --> 02:10:27,559 will return the pation and we have to 2125 02:10:27,559 --> 02:10:30,920 provide our model which is 2126 02:10:32,000 --> 02:10:34,840 song and we have to call the page unit 2127 02:10:34,840 --> 02:10:36,280 with song 2128 02:10:36,280 --> 02:10:39,239 entity we have to provide the songs 2129 02:10:39,239 --> 02:10:41,960 repository and the options if you need 2130 02:10:41,960 --> 02:10:44,880 to add a cury builder like filtering or 2131 02:10:44,880 --> 02:10:47,760 sorting you can also do 2132 02:10:47,760 --> 02:10:51,639 that but here is our control rer 2133 02:10:51,639 --> 02:10:55,400 function we use the Prem now we're going 2134 02:10:55,400 --> 02:10:58,320 to use security decorator and it should 2135 02:10:58,320 --> 02:11:02,320 name of the field which is Page you can 2136 02:11:02,320 --> 02:11:05,679 add a by default default value which is 2137 02:11:05,679 --> 02:11:10,000 one and I applied the pars int and I 2138 02:11:10,000 --> 02:11:13,199 have added the default 2139 02:11:13,199 --> 02:11:14,960 value 2140 02:11:14,960 --> 02:11:20,119 and we have another field or another 2141 02:11:20,119 --> 02:11:22,719 property and I'm getting it from the 2142 02:11:22,719 --> 02:11:26,000 cury prams this is the limit and default 2143 02:11:26,000 --> 02:11:29,040 limit which is 10 and I applyed the pars 2144 02:11:29,040 --> 02:11:31,239 into pipe operator and finally it should 2145 02:11:31,239 --> 02:11:34,000 return the pagination with song entity 2146 02:11:34,000 --> 02:11:37,520 if limit is greater than 100 please do 2147 02:11:37,520 --> 02:11:40,360 it 100 otherwise we have to specify the 2148 02:11:40,360 --> 02:11:43,880 limit or tell or add the 2149 02:11:43,880 --> 02:11:46,679 limit there is a page unit method we 2150 02:11:46,679 --> 02:11:49,199 created inside the song service and you 2151 02:11:49,199 --> 02:11:51,800 have to provide the page and the limit 2152 02:11:51,800 --> 02:11:53,480 we're getting the page and limit from 2153 02:11:53,480 --> 02:11:55,639 the 2154 02:11:59,159 --> 02:12:03,000 cury so you have to test it by sending 2155 02:12:03,000 --> 02:12:06,800 the cury parameter page which is two and 2156 02:12:06,800 --> 02:12:08,719 the limit whiches to I want to see two 2157 02:12:08,719 --> 02:12:11,320 records per 2158 02:12:11,320 --> 02:12:15,880 page and I want to see the page number 2159 02:12:16,320 --> 02:12:18,920 two if you want to add the Sorting you 2160 02:12:18,920 --> 02:12:20,800 can also do that by using the query 2161 02:12:20,800 --> 02:12:23,159 Builder 2162 02:12:23,760 --> 02:12:28,320 please apply sorting on release date I 2163 02:12:28,320 --> 02:12:30,920 want to see the latest song on the based 2164 02:12:30,920 --> 02:12:34,320 on date let's implement it or you can 2165 02:12:34,320 --> 02:12:36,960 Implement by your 2166 02:12:36,960 --> 02:12:40,679 own first step we have to 2167 02:12:40,679 --> 02:12:44,880 create a new method 2168 02:12:48,639 --> 02:12:52,199 paginate and we need these 2169 02:12:52,199 --> 02:12:56,800 options I can import all of these from 2170 02:12:56,800 --> 02:12:59,079 the 2171 02:13:05,159 --> 02:13:09,559 method so we have the pagate 2172 02:13:09,599 --> 02:13:13,239 method now we have to call it inside the 2173 02:13:13,239 --> 02:13:15,719 songs 2174 02:13:16,119 --> 02:13:20,040 controller you can it depends on your 2175 02:13:20,040 --> 02:13:21,920 use case but I I'm going to call the 2176 02:13:21,920 --> 02:13:25,239 paginate method in the find 2177 02:13:25,239 --> 02:13:29,920 all so I'm going to replace my find all 2178 02:13:29,920 --> 02:13:34,639 method to this find 2179 02:13:43,840 --> 02:13:48,719 all we also need cury 2180 02:13:50,040 --> 02:13:53,040 Prem 2181 02:13:54,400 --> 02:13:59,800 it's just getting the es lint warning 2182 02:14:04,079 --> 02:14:07,360 messages we also need 2183 02:14:07,360 --> 02:14:10,960 default value 2184 02:14:12,239 --> 02:14:17,599 pipe we need pagination from 2185 02:14:17,599 --> 02:14:22,239 nestjs that looks good 2186 02:14:22,239 --> 02:14:25,079 we can test 2187 02:14:34,599 --> 02:14:37,320 it this time I'm going to send the 2188 02:14:37,320 --> 02:14:39,559 request to fetch all the 2189 02:14:39,559 --> 02:14:42,960 songs it has applied the limit 10 and 2190 02:14:42,960 --> 02:14:46,920 default value which is page which is one 2191 02:14:46,920 --> 02:14:48,800 over the first 2192 02:14:48,800 --> 02:14:53,040 page total items item count items per 2193 02:14:53,040 --> 02:14:56,639 page 10 and total Pages we have one and 2194 02:14:56,639 --> 02:14:59,840 current page which is one let's add 2195 02:14:59,840 --> 02:15:02,199 another 2196 02:15:02,199 --> 02:15:05,880 record new song 2197 02:15:07,000 --> 02:15:11,840 to let's add another record new song 2198 02:15:11,840 --> 02:15:17,440 3 let's add another record new song four 2199 02:15:17,440 --> 02:15:19,960 now we have four 2200 02:15:19,960 --> 02:15:21,760 songs 2201 02:15:21,760 --> 02:15:24,239 you can 2202 02:15:27,960 --> 02:15:32,159 say I want to see the page number one 2203 02:15:32,159 --> 02:15:34,480 this 2204 02:15:35,880 --> 02:15:39,880 time I want to set the limit to two I 2205 02:15:39,880 --> 02:15:43,599 want to see only two records per 2206 02:15:43,599 --> 02:15:47,719 page we have the items array you can see 2207 02:15:47,719 --> 02:15:49,920 we have only two 2208 02:15:49,920 --> 02:15:53,760 records there are total two pages we are 2209 02:15:53,760 --> 02:15:58,000 on the first page and items per page two 2210 02:15:58,000 --> 02:16:01,880 item count two total items we have the 2211 02:16:01,880 --> 02:16:04,679 four I told you if you want to add 2212 02:16:04,679 --> 02:16:08,639 sorting you can also do 2213 02:16:09,440 --> 02:16:13,440 that by using the query 2214 02:16:14,320 --> 02:16:18,199 Builder inside the song 2215 02:16:18,840 --> 02:16:23,320 service I'm going to add curil fer 2216 02:16:29,840 --> 02:16:33,679 here and we have to provide cury Builder 2217 02:16:33,679 --> 02:16:38,040 not songs repository we have to fix 2218 02:16:38,040 --> 02:16:41,840 it cury 2219 02:16:45,280 --> 02:16:49,719 Builder let's provide the cury 2220 02:16:49,840 --> 02:16:52,840 Builder 2221 02:17:00,519 --> 02:17:02,240 please 2222 02:17:02,240 --> 02:17:05,000 create a new song 2223 02:17:05,000 --> 02:17:10,679 with let's say a new Five song latest 2224 02:17:19,478 --> 02:17:23,679 date it should should give this record 2225 02:17:23,679 --> 02:17:29,160 first let's create it created a new 2226 02:17:30,040 --> 02:17:35,240 song I got a latest song Only the based 2227 02:17:35,240 --> 02:17:37,840 on release date it means it's working 2228 02:17:37,840 --> 02:17:40,200 fine 2229 02:17:45,478 --> 02:17:48,519 now in this video I'm going to teach you 2230 02:17:48,519 --> 02:17:50,920 how to build one toone relationship 2231 02:17:50,920 --> 02:17:53,479 between two 2232 02:17:53,879 --> 02:17:57,478 models one to one relation where a 2233 02:17:57,478 --> 02:18:01,519 entity contains only one instance of B 2234 02:18:01,519 --> 02:18:05,120 and B contains only one instance of a 2235 02:18:05,120 --> 02:18:07,478 let's take an example a user can become 2236 02:18:07,478 --> 02:18:11,240 an artist or an artist can have only 2237 02:18:11,240 --> 02:18:13,879 single user profile so there is a one 2238 02:18:13,879 --> 02:18:16,959 toone relationship between user and the 2239 02:18:16,959 --> 02:18:20,799 artist we don't have an artist model or 2240 02:18:20,799 --> 02:18:21,799 artist 2241 02:18:21,799 --> 02:18:24,679 entity first of all you got to create 2242 02:18:24,679 --> 02:18:27,439 artist entity with primary 2243 02:18:27,439 --> 02:18:31,478 key and then you have to create the user 2244 02:18:31,478 --> 02:18:33,599 entity because we're going to make one 2245 02:18:33,599 --> 02:18:35,920 to one relationship between artist and 2246 02:18:35,920 --> 02:18:36,840 the 2247 02:18:36,840 --> 02:18:41,638 user I'm going to have ID field or First 2248 02:18:41,638 --> 02:18:43,879 Column first name column last name 2249 02:18:43,879 --> 02:18:46,040 column email column and the password 2250 02:18:46,040 --> 02:18:49,479 column for user 2251 02:18:49,718 --> 02:18:52,959 entity now we're going to add one to one 2252 02:18:52,959 --> 02:18:55,959 relationship type or provides one to one 2253 02:18:55,959 --> 02:18:58,080 decorator to add relationship between 2254 02:18:58,080 --> 02:19:00,920 two two 2255 02:19:01,160 --> 02:19:04,760 entities uh in the first argument you 2256 02:19:04,760 --> 02:19:07,760 specify the relation type I'm telling 2257 02:19:07,760 --> 02:19:10,080 type orm I would like to make a 2258 02:19:10,080 --> 02:19:13,760 relationship with with user 2259 02:19:13,760 --> 02:19:16,120 enti this is the type of the relation 2260 02:19:16,120 --> 02:19:19,040 you specify the relation entity type by 2261 02:19:19,040 --> 02:19:22,718 following this syntax AR 2262 02:19:22,718 --> 02:19:26,359 function and this join column will rep 2263 02:19:26,359 --> 02:19:27,280 will 2264 02:19:27,280 --> 02:19:31,080 create the reference key or a foreign 2265 02:19:31,080 --> 02:19:37,160 key of the user entity inside the artist 2266 02:19:37,160 --> 02:19:40,799 entity so we have so we will have a user 2267 02:19:40,799 --> 02:19:43,478 ID as a foreign key inside the 2268 02:19:43,478 --> 02:19:46,519 artist and finally you have to register 2269 02:19:46,519 --> 02:19:51,479 the user and artist entity in the app 2270 02:19:51,479 --> 02:19:53,560 module now we're going to implement 2271 02:19:53,560 --> 02:19:55,800 these 2272 02:19:56,280 --> 02:20:01,200 steps let's create the first a new 2273 02:20:04,319 --> 02:20:06,720 Branch 2274 02:20:06,720 --> 02:20:09,720 so I'm going to create a new artist 2275 02:20:09,720 --> 02:20:14,960 entity if you like you can use CLI 2276 02:20:14,960 --> 02:20:19,200 command to generate a new entity so I'm 2277 02:20:19,200 --> 02:20:22,280 going to say artist do 2278 02:20:22,280 --> 02:20:25,800 entity. file I'm going to copy my 2279 02:20:25,800 --> 02:20:28,399 code there is nothing here you already 2280 02:20:28,399 --> 02:20:32,000 learned how to create an 2281 02:20:32,000 --> 02:20:36,359 entity and next we have to create a user 2282 02:20:36,359 --> 02:20:40,560 model a user entity go ahead and create 2283 02:20:40,560 --> 02:20:44,240 a new folder with 2284 02:20:44,240 --> 02:20:47,640 user or 2285 02:20:47,960 --> 02:20:52,640 users let's keep it to 2286 02:20:54,960 --> 02:20:57,240 artist so I'm going to create a new 2287 02:20:57,240 --> 02:21:00,920 entity user. 2288 02:21:02,319 --> 02:21:06,720 entity. P the code we have ID field 2289 02:21:06,720 --> 02:21:10,960 first name last name email and the 2290 02:21:10,960 --> 02:21:13,160 password and now we're going to make a 2291 02:21:13,160 --> 02:21:15,600 one to one relationship I would like to 2292 02:21:15,600 --> 02:21:19,040 store the user ID inside the artist 2293 02:21:19,040 --> 02:21:22,720 model if you want to store artist ID 2294 02:21:22,720 --> 02:21:25,640 inside the user you can also do that it 2295 02:21:25,640 --> 02:21:27,760 depends on your use 2296 02:21:27,760 --> 02:21:32,960 case like I want to make a relationship 2297 02:21:41,600 --> 02:21:44,359 here let me do it 2298 02:21:44,359 --> 02:21:46,520 here we 2299 02:21:46,520 --> 02:21:49,520 need one to one 2300 02:21:49,520 --> 02:21:52,520 decorator 2301 02:21:53,080 --> 02:21:55,840 specify the type of relationship by 2302 02:21:55,840 --> 02:21:58,200 following the syntax we would like to 2303 02:21:58,200 --> 02:22:01,359 make a relationship with 2304 02:22:06,880 --> 02:22:11,080 user and here we will have the user and 2305 02:22:11,080 --> 02:22:16,200 the user and I also need a join 2306 02:22:17,200 --> 02:22:19,520 column that's it for 2307 02:22:19,520 --> 02:22:26,000 now cannot find a module user. .ts 2308 02:22:29,960 --> 02:22:34,680 file so we have to import it 2309 02:22:35,359 --> 02:22:39,120 manually now it looks 2310 02:22:39,359 --> 02:22:42,960 good and now we have to register these 2311 02:22:42,960 --> 02:22:46,399 entities inside the app module we 2312 02:22:46,399 --> 02:22:48,319 created couple of new 2313 02:22:48,319 --> 02:22:51,760 entities please convert these entities 2314 02:22:51,760 --> 02:22:54,920 into tables I can say 2315 02:22:54,920 --> 02:22:58,840 artist and we have 2316 02:22:59,160 --> 02:23:03,960 user our last step is to test the 2317 02:23:06,680 --> 02:23:12,040 application and I have to run my PG 2318 02:23:17,120 --> 02:23:19,960 admin let me connect with post this 2319 02:23:19,960 --> 02:23:22,439 database 2320 02:23:30,600 --> 02:23:34,200 so it has successfully let me check my 2321 02:23:34,200 --> 02:23:37,560 schema which is 45 2322 02:23:37,560 --> 02:23:41,240 clone I have to connect 2323 02:23:41,240 --> 02:23:45,680 it inside the tables we have the artist 2324 02:23:45,680 --> 02:23:49,080 and the users 2325 02:23:49,080 --> 02:23:52,359 table we have have the user ID inside 2326 02:23:52,359 --> 02:23:53,760 the 2327 02:23:53,760 --> 02:23:56,560 artist and inside the 2328 02:23:56,560 --> 02:24:00,359 users we have for columns or ID first 2329 02:24:00,359 --> 02:24:03,960 name last name email and the 2330 02:24:03,960 --> 02:24:07,920 password our application is running fine 2331 02:24:07,920 --> 02:24:10,680 now this is how you will make one toone 2332 02:24:10,680 --> 02:24:13,920 relationship between two 2333 02:24:15,800 --> 02:24:18,120 entities now we're going to talk about 2334 02:24:18,120 --> 02:24:19,960 how to implement many to many 2335 02:24:19,960 --> 02:24:21,120 relationship 2336 02:24:21,120 --> 02:24:24,160 ship many to many relationship is a 2337 02:24:24,160 --> 02:24:27,479 relation where a contains multiple 2338 02:24:27,479 --> 02:24:31,319 instances of B entity and B entity 2339 02:24:31,319 --> 02:24:35,359 contains multiple instances of 2340 02:24:38,600 --> 02:24:42,359 a let's take an example many artists can 2341 02:24:42,359 --> 02:24:46,399 publish many songs and multiple artist 2342 02:24:46,399 --> 02:24:49,560 can belong to one song or multiple 2343 02:24:49,560 --> 02:24:53,439 artist can publish multiple 2344 02:24:53,439 --> 02:24:58,479 songs we have a multiple many to many 2345 02:24:58,479 --> 02:25:02,479 relationship inside the artist nestjs or 2346 02:25:02,479 --> 02:25:05,160 type orm provides many to many 2347 02:25:05,160 --> 02:25:07,840 decorator this will specify the target 2348 02:25:07,840 --> 02:25:09,439 relation 2349 02:25:09,439 --> 02:25:12,520 entity and this is the inverse side of 2350 02:25:12,520 --> 02:25:14,720 the 2351 02:25:14,760 --> 02:25:19,160 relationship and it will have the songs 2352 02:25:19,319 --> 02:25:23,279 array and on the song we will have the 2353 02:25:23,279 --> 02:25:27,120 artist array we have a many to many 2354 02:25:27,120 --> 02:25:30,760 decorator the target entity in the song 2355 02:25:30,760 --> 02:25:33,160 we have Target entity which is 2356 02:25:33,160 --> 02:25:37,359 artist and the second argument 2357 02:25:37,359 --> 02:25:40,080 specify the inverse side of the 2358 02:25:40,080 --> 02:25:44,000 relationship inside the artist we have 2359 02:25:44,000 --> 02:25:46,920 songs property that's why I can access 2360 02:25:46,920 --> 02:25:51,240 the artist. songs and inside the 2361 02:25:51,240 --> 02:25:54,920 artist entity we have song. artist 2362 02:25:54,920 --> 02:25:58,800 that's why I can access the artist from 2363 02:25:58,800 --> 02:26:02,920 this song entity song. artist that is 2364 02:26:02,920 --> 02:26:05,479 the inverse side of the relation when 2365 02:26:05,479 --> 02:26:08,760 you set the casc to 2366 02:26:08,760 --> 02:26:13,600 true it means you can create or update 2367 02:26:13,600 --> 02:26:17,720 the record while creating a new song or 2368 02:26:17,720 --> 02:26:20,319 an artist I'll teach you the benefits of 2369 02:26:20,319 --> 02:26:21,920 ask 2370 02:26:21,920 --> 02:26:24,840 getting when you implement the many to 2371 02:26:24,840 --> 02:26:28,000 many relationship you you need a joint 2372 02:26:28,000 --> 02:26:31,720 table many to many imple uh relationship 2373 02:26:31,720 --> 02:26:35,200 always work with third table which 2374 02:26:35,200 --> 02:26:37,520 should be joint table and I rename it to 2375 02:26:37,520 --> 02:26:41,279 songs artist and this songs artist will 2376 02:26:41,279 --> 02:26:44,800 have primary key of the song and the 2377 02:26:44,800 --> 02:26:49,240 primary key of the Artist as a foreign 2378 02:26:49,240 --> 02:26:52,240 key 2379 02:26:55,279 --> 02:26:57,880 and we also need to refactor some create 2380 02:26:57,880 --> 02:27:01,600 song dto instead of using EST string EST 2381 02:27:01,600 --> 02:27:04,120 string we going to we are going to get 2382 02:27:04,120 --> 02:27:07,880 IDs from the request parameter IDs of 2383 02:27:07,880 --> 02:27:10,920 the artist and IDs of the artist in the 2384 02:27:10,920 --> 02:27:13,920 update song video and then you have to 2385 02:27:13,920 --> 02:27:17,200 register the artist entity in the songs 2386 02:27:17,200 --> 02:27:20,920 module that's because I want to the 2387 02:27:20,920 --> 02:27:24,680 artist repository inside the songs 2388 02:27:24,680 --> 02:27:27,800 modle and finally you need to refactor 2389 02:27:27,800 --> 02:27:30,840 the create method we're going to get 2390 02:27:30,840 --> 02:27:33,279 artist here the first 2391 02:27:33,279 --> 02:27:37,840 step we are going to get this artist IDs 2392 02:27:37,840 --> 02:27:40,200 from the request object or request 2393 02:27:40,200 --> 02:27:42,240 parameter or the request 2394 02:27:42,240 --> 02:27:45,160 body and then we will find all the 2395 02:27:45,160 --> 02:27:48,680 artist only based on IDs and finally 2396 02:27:48,680 --> 02:27:50,640 here we are going to set the song do 2397 02:27:50,640 --> 02:27:54,479 artist equals to artist if you have set 2398 02:27:54,479 --> 02:27:56,680 the casket to true just like we have 2399 02:27:56,680 --> 02:28:00,040 done it done it here cascading we set to 2400 02:28:00,040 --> 02:28:02,920 true now it will set the relationship 2401 02:28:02,920 --> 02:28:06,800 between songs and the artist it will 2402 02:28:06,800 --> 02:28:09,000 automatically create the record with 2403 02:28:09,000 --> 02:28:12,600 third table which is the joint table 2404 02:28:12,600 --> 02:28:16,479 here here it will create a record it 2405 02:28:16,479 --> 02:28:19,920 create records in the songs artist table 2406 02:28:19,920 --> 02:28:21,800 by by setting the relation with this one 2407 02:28:21,800 --> 02:28:25,520 songs. song do artist should be artist 2408 02:28:25,520 --> 02:28:27,960 and finally we're going to save the song 2409 02:28:27,960 --> 02:28:30,479 in the using song 2410 02:28:30,479 --> 02:28:32,479 repository and you can test the 2411 02:28:32,479 --> 02:28:37,000 application now um let's implement it 2412 02:28:40,720 --> 02:28:46,279 first so we have many to many 2413 02:28:46,279 --> 02:28:48,800 relationship I'm going to add this 2414 02:28:48,800 --> 02:28:52,720 relationship in inser the 2415 02:28:55,800 --> 02:28:59,040 artist I'm going to do it 2416 02:28:59,040 --> 02:29:04,520 here this specify the target relation 2417 02:29:06,080 --> 02:29:09,359 type and we don't have we we did not 2418 02:29:09,359 --> 02:29:14,800 create the song. artist inside the song 2419 02:29:15,720 --> 02:29:18,760 entity now we have the songs array let's 2420 02:29:18,760 --> 02:29:22,040 import many to many 2421 02:29:23,279 --> 02:29:25,800 I'm also going to implement it 2422 02:29:25,800 --> 02:29:28,640 here many to many 2423 02:29:28,640 --> 02:29:32,120 decorator the target entity in our case 2424 02:29:32,120 --> 02:29:34,640 it's an 2425 02:29:34,880 --> 02:29:38,399 artist and it's going to take the 2426 02:29:38,399 --> 02:29:41,359 artist let's specify the inverse side of 2427 02:29:41,359 --> 02:29:46,240 the relation artist dot 2428 02:29:48,000 --> 02:29:52,399 songs and we will have join 2429 02:29:52,399 --> 02:29:56,279 table I'm going to name it 2430 02:29:56,479 --> 02:30:00,399 to songs artist or artist song let's 2431 02:30:00,399 --> 02:30:03,359 take it the songs 2432 02:30:03,359 --> 02:30:06,080 artist and finally I'm going to specify 2433 02:30:06,080 --> 02:30:08,720 the artist 2434 02:30:18,960 --> 02:30:21,960 array 2435 02:30:22,800 --> 02:30:25,359 duplicate 2436 02:30:25,359 --> 02:30:31,319 identify yeah I don't need this type 2437 02:30:31,319 --> 02:30:34,319 because I'm not getting the string array 2438 02:30:34,319 --> 02:30:36,600 from the artist instead of I'm getting 2439 02:30:36,600 --> 02:30:41,200 the array of the artist from the request 2440 02:30:48,920 --> 02:30:51,920 parameter 2441 02:30:53,800 --> 02:30:56,319 we added many to many relationship 2442 02:30:56,319 --> 02:30:59,840 inside the song model or song 2443 02:30:59,840 --> 02:31:02,560 entity and we forgot to set the 2444 02:31:02,560 --> 02:31:05,359 cascading to 2445 02:31:05,640 --> 02:31:11,160 True here you can do it inside 2446 02:31:12,800 --> 02:31:16,600 here G SC to 2447 02:31:18,920 --> 02:31:21,920 true 2448 02:31:24,720 --> 02:31:28,840 now we need to refactor the create song 2449 02:31:33,880 --> 02:31:36,200 dto I have to 2450 02:31:36,200 --> 02:31:38,439 refactor 2451 02:31:38,439 --> 02:31:41,800 from it should be 2452 02:31:41,800 --> 02:31:44,960 number and the 2453 02:31:44,960 --> 02:31:48,680 first argument is an 2454 02:31:48,680 --> 02:31:52,760 empty and and it looks 2455 02:31:53,240 --> 02:31:56,439 good similarly we have to refactor the 2456 02:31:56,439 --> 02:31:58,960 update song 2457 02:31:58,960 --> 02:32:04,720 dtoo it should be number array of 2458 02:32:04,720 --> 02:32:09,040 numbers the first argument is 2459 02:32:09,040 --> 02:32:12,520 empty looks good now we need to register 2460 02:32:12,520 --> 02:32:15,200 the artist entity inside the songs 2461 02:32:15,200 --> 02:32:17,680 module so we can access the artist 2462 02:32:17,680 --> 02:32:21,840 repository inside the song service 2463 02:32:22,720 --> 02:32:25,439 I I'm saying to type orm I would like to 2464 02:32:25,439 --> 02:32:28,040 access the artist repository inside the 2465 02:32:28,040 --> 02:32:30,439 songs module or the song 2466 02:32:30,439 --> 02:32:35,359 service that's why I have to register it 2467 02:32:35,800 --> 02:32:39,479 here and finally we need to refactor 2468 02:32:39,479 --> 02:32:41,160 create 2469 02:32:41,160 --> 02:32:47,399 song I can say create uh inside the song 2470 02:32:47,399 --> 02:32:51,600 service so you can do it too if you want 2471 02:32:51,600 --> 02:32:53,200 here we have 2472 02:32:53,200 --> 02:32:57,600 to find all the 2473 02:32:59,200 --> 02:33:03,359 artists on The based on 2474 02:33:03,840 --> 02:33:07,479 ID then we have to set the 2475 02:33:07,479 --> 02:33:11,240 relation with artist and 2476 02:33:11,240 --> 02:33:14,840 songs so you can do it but first of all 2477 02:33:14,840 --> 02:33:17,080 we have to 2478 02:33:17,080 --> 02:33:21,680 inject the Repository which is 2479 02:33:21,680 --> 02:33:24,080 artist I can say 2480 02:33:24,080 --> 02:33:27,680 private artist 2481 02:33:27,960 --> 02:33:30,600 repository and 2482 02:33:30,600 --> 02:33:34,279 repository I can say 2483 02:33:46,760 --> 02:33:51,720 artist let's get all the artist 2484 02:33:55,319 --> 02:33:59,080 you have to add a Sync 2485 02:33:59,439 --> 02:34:04,120 here this do song artist 2486 02:34:04,120 --> 02:34:08,640 repository find by IDs I can use that 2487 02:34:08,640 --> 02:34:12,880 method provide the artist ID from the 2488 02:34:12,880 --> 02:34:14,240 song 2489 02:34:14,240 --> 02:34:16,840 dto 2490 02:34:16,840 --> 02:34:20,880 artist now I can set the relation 2491 02:34:20,880 --> 02:34:25,359 with song. artist it should be artist so 2492 02:34:25,359 --> 02:34:28,000 we have set the cascading it will create 2493 02:34:28,000 --> 02:34:30,240 a relation many to many relation with 2494 02:34:30,240 --> 02:34:32,120 songs and the 2495 02:34:32,120 --> 02:34:37,160 artist now it's time to test our 2496 02:34:37,279 --> 02:34:40,720 application rest 2497 02:34:41,080 --> 02:34:43,880 client here we have to send the post 2498 02:34:43,880 --> 02:34:47,640 instead of artist we need to send the 2499 02:34:47,640 --> 02:34:50,319 IDS of the artist 2500 02:34:50,319 --> 02:34:53,000 a user can become the artist so we don't 2501 02:34:53,000 --> 02:34:55,600 have any record in the artist and the 2502 02:34:55,600 --> 02:35:00,520 user model you have to create it 2503 02:35:01,439 --> 02:35:05,279 manually so I'm going to 2504 02:35:05,359 --> 02:35:08,000 refresh you can see we have the third 2505 02:35:08,000 --> 02:35:10,640 table which is songs 2506 02:35:10,640 --> 02:35:13,040 artsts is 2507 02:35:13,040 --> 02:35:18,040 not artist we have to fix 2508 02:35:18,640 --> 02:35:21,640 it 2509 02:35:31,560 --> 02:35:32,880 inside 2510 02:35:32,880 --> 02:35:35,479 this inside the 2511 02:35:35,479 --> 02:35:38,479 songs 2512 02:35:40,520 --> 02:35:44,319 entity here we have to fix 2513 02:35:44,319 --> 02:35:47,319 it 2514 02:35:48,640 --> 02:35:51,640 this 2515 02:36:09,920 --> 02:36:12,120 I'm going to refresh the 2516 02:36:12,120 --> 02:36:17,840 table get it off this table you can drop 2517 02:36:17,920 --> 02:36:23,479 it delete or drop yes now we have song 2518 02:36:23,479 --> 02:36:27,040 artist with song ID as a foreign key of 2519 02:36:27,040 --> 02:36:28,840 the song 2520 02:36:28,840 --> 02:36:32,399 model artist 2521 02:36:36,840 --> 02:36:40,840 ID we don't have any user right now I 2522 02:36:40,840 --> 02:36:46,479 want you to create it manually let me do 2523 02:36:48,520 --> 02:36:51,520 that 2524 02:36:52,680 --> 02:36:56,840 I'm going to add a new record with ID 2525 02:36:56,840 --> 02:36:59,640 one it should be 2526 02:36:59,640 --> 02:37:03,040 Jane or 2527 02:37:09,880 --> 02:37:11,880 saga 2528 02:37:11,880 --> 02:37:14,880 Saga 2529 02:37:16,800 --> 02:37:19,319 gmail.com I'm going to add anything here 2530 02:37:19,319 --> 02:37:25,439 pass password let's add another 2531 02:37:25,680 --> 02:37:29,240 user I can say 2532 02:37:29,240 --> 02:37:33,080 Jane and I can say 2533 02:37:33,080 --> 02:37:36,960 do Jan at 2534 02:37:38,040 --> 02:37:41,600 gmail.com password 1 2 3 4 2535 02:37:41,600 --> 02:37:46,720 5 now you can save the record so we have 2536 02:37:46,720 --> 02:37:50,439 two users right now 2537 02:37:50,439 --> 02:37:52,840 if you want you can also add another 2538 02:37:52,840 --> 02:37:58,000 user or artist I can say 2539 02:37:58,720 --> 02:38:01,680 Martin 2540 02:38:01,680 --> 02:38:03,399 gar 2541 02:38:03,399 --> 02:38:07,040 Martin at 2542 02:38:07,520 --> 02:38:10,760 gmail.com 1 2 3 4 2543 02:38:10,760 --> 02:38:15,040 5 let me save the record so these two 2544 02:38:15,040 --> 02:38:17,560 users are the artists but we need to 2545 02:38:17,560 --> 02:38:20,040 make a relation inside the 2546 02:38:20,040 --> 02:38:22,600 artist 2547 02:38:28,279 --> 02:38:31,880 table let's add a new record with ID and 2548 02:38:31,880 --> 02:38:36,439 here we have to add a artist 2549 02:38:37,000 --> 02:38:41,120 ID let me double check 2550 02:38:41,560 --> 02:38:45,439 it yes one and the three 2551 02:38:45,439 --> 02:38:48,439 one we have to add 2552 02:38:48,439 --> 02:38:52,439 another artist one the ID should be 2553 02:38:52,439 --> 02:38:57,160 three so these two are the 2554 02:38:57,160 --> 02:39:00,960 artist ID should be here we have to set 2555 02:39:00,960 --> 02:39:03,800 the ID should be 2556 02:39:04,240 --> 02:39:09,000 two now it looks 2557 02:39:09,640 --> 02:39:13,920 good so what I'm saying 2558 02:39:14,479 --> 02:39:20,240 that a user with ID3 is an artist a user 2559 02:39:20,240 --> 02:39:24,160 with ID one is also an 2560 02:39:24,160 --> 02:39:28,160 artist so one Martin and seag are an 2561 02:39:28,160 --> 02:39:32,760 artist and Jane is a just a simple 2562 02:39:35,600 --> 02:39:38,640 user so when you create a new record I 2563 02:39:38,640 --> 02:39:39,680 can 2564 02:39:39,680 --> 02:39:42,359 say let's add a 2565 02:39:42,359 --> 02:39:45,880 new song so these two are all the artist 2566 02:39:45,880 --> 02:39:48,279 they are going to create they have 2567 02:39:48,279 --> 02:39:51,319 created a new song 2568 02:39:51,319 --> 02:39:54,359 I can say you for 2569 02:39:54,359 --> 02:39:56,960 me that is it right now I'm going to 2570 02:39:56,960 --> 02:39:58,560 send the 2571 02:39:58,560 --> 02:40:03,600 request sounds good we have a new record 2572 02:40:03,600 --> 02:40:06,720 title this is the artist 2573 02:40:06,720 --> 02:40:10,160 ID this is the complete 2574 02:40:10,160 --> 02:40:14,359 artist so we have an art ID 2575 02:40:14,359 --> 02:40:16,960 one and we 2576 02:40:16,960 --> 02:40:21,319 have the relationship 2577 02:40:21,560 --> 02:40:23,680 let me double check it from the artist 2578 02:40:23,680 --> 02:40:26,120 and the 2579 02:40:34,840 --> 02:40:39,040 songs it just created a single record it 2580 02:40:39,040 --> 02:40:43,040 did not get the ID 1 and three I don't 2581 02:40:43,040 --> 02:40:46,200 know what is wrong with 2582 02:40:48,240 --> 02:40:51,240 it 2583 02:40:59,040 --> 02:41:03,880 let's do it here one and three you for 2584 02:41:03,880 --> 02:41:06,160 me 2585 02:41:09,640 --> 02:41:15,319 to again I am getting the ID only 2586 02:41:16,240 --> 02:41:21,160 one let's log 2587 02:41:21,160 --> 02:41:23,720 the song 2588 02:41:23,720 --> 02:41:26,720 d. 2589 02:41:46,960 --> 02:41:51,240 artist 1 and three then what 2590 02:41:51,920 --> 02:41:55,080 why 1 and three it looks 2591 02:41:55,080 --> 02:41:58,040 good maybe the issue 2592 02:41:58,040 --> 02:42:00,920 with 2593 02:42:00,920 --> 02:42:03,920 here 2594 02:42:18,160 --> 02:42:19,960 console.log 2595 02:42:19,960 --> 02:42:24,439 so when you look at the artist 2596 02:42:24,439 --> 02:42:29,319 record oh we have one and the 2597 02:42:31,399 --> 02:42:36,600 two so one and the two instead of 1 and 2598 02:42:48,120 --> 02:42:51,120 three 2599 02:43:10,439 --> 02:43:13,240 let's provide the 2600 02:43:13,240 --> 02:43:17,560 two you and for me 2601 02:43:18,120 --> 02:43:19,720 three 2602 02:43:19,720 --> 02:43:24,880 now I got the artist ID one and the 2603 02:43:24,960 --> 02:43:29,080 two it will also addit a relationship 2604 02:43:29,080 --> 02:43:32,200 here inside the songs and the artist I'm 2605 02:43:32,200 --> 02:43:33,520 going to 2606 02:43:33,520 --> 02:43:36,319 find now you can see 2607 02:43:36,319 --> 02:43:43,520 that a song ID with 10 has two 2608 02:43:44,520 --> 02:43:47,120 artist these two 2609 02:43:47,120 --> 02:43:49,960 users user ID number three and user ID 2610 02:43:49,960 --> 02:43:52,240 number 2611 02:43:53,000 --> 02:43:56,200 one in this video you're going to learn 2612 02:43:56,200 --> 02:43:59,200 how to implement this signup 2613 02:43:59,200 --> 02:44:01,840 functionality a user can create an 2614 02:44:01,840 --> 02:44:04,720 account in our application and we need 2615 02:44:04,720 --> 02:44:07,120 to save the user in the 2616 02:44:07,120 --> 02:44:11,600 database first of all we have to install 2617 02:44:11,600 --> 02:44:15,439 dependencies I'm going to use bcrypt JS 2618 02:44:15,439 --> 02:44:18,680 password bcrypt JS package to save the 2619 02:44:18,680 --> 02:44:19,760 user 2620 02:44:19,760 --> 02:44:22,840 password in encrypted format I don't 2621 02:44:22,840 --> 02:44:25,200 want to save the password in plain text 2622 02:44:25,200 --> 02:44:28,359 like 1 2 3 4 5 we have to save the 2623 02:44:28,359 --> 02:44:31,760 password in encrypted format that's why 2624 02:44:31,760 --> 02:44:35,000 you have to install this 2625 02:44:35,760 --> 02:44:39,560 dependencies let me open my package.json 2626 02:44:39,560 --> 02:44:43,120 file I'm going to add a new entry let me 2627 02:44:43,120 --> 02:44:45,479 copy 2628 02:44:46,960 --> 02:44:50,560 it and I'm going to paste paste it 2629 02:44:50,560 --> 02:44:54,080 here we also needed to install the 2630 02:44:54,080 --> 02:44:57,640 typing for 2631 02:44:58,359 --> 02:45:01,520 typescript I have to add the typing in 2632 02:45:01,520 --> 02:45:02,720 the dep 2633 02:45:02,720 --> 02:45:06,640 dependency I'm going to put it 2634 02:45:14,840 --> 02:45:18,160 here so I'm going to run npm install 2635 02:45:18,160 --> 02:45:21,880 please install dependencies for 2636 02:45:25,560 --> 02:45:28,439 me you can see two packages have 2637 02:45:28,439 --> 02:45:32,279 installed added new two 2638 02:45:35,479 --> 02:45:38,560 packages now we have to create a user 2639 02:45:38,560 --> 02:45:42,439 module and the Au module we don't have a 2640 02:45:42,439 --> 02:45:46,439 user module and Au module inside our 2641 02:45:46,439 --> 02:45:50,920 application I'm going to use nest 2642 02:45:51,720 --> 02:45:57,800 CLI to generate a new module I can say 2643 02:46:09,720 --> 02:46:13,840 o we have created a new o 2644 02:46:13,840 --> 02:46:16,160 module you can see 2645 02:46:16,160 --> 02:46:20,920 that we also need to generate the ser 2646 02:46:21,600 --> 02:46:24,960 service for 2647 02:46:33,640 --> 02:46:38,040 art you can see that Au service has 2648 02:46:38,040 --> 02:46:40,760 generated we also need 2649 02:46:40,760 --> 02:46:44,359 controller I can say 2650 02:46:47,840 --> 02:46:50,840 AU 2651 02:46:54,920 --> 02:46:59,040 an au controller has created it has also 2652 02:46:59,040 --> 02:47:03,760 added the entry inside the controller's 2653 02:47:04,479 --> 02:47:07,640 part we have it has automatically 2654 02:47:07,640 --> 02:47:12,520 imported Au module inside my app 2655 02:47:16,000 --> 02:47:20,840 module here is the O module 2656 02:47:21,920 --> 02:47:23,640 now we have to 2657 02:47:23,640 --> 02:47:27,160 create the users module one more thing 2658 02:47:27,160 --> 02:47:30,160 we did not export the odd service 2659 02:47:30,160 --> 02:47:34,240 yet here I I can say I would like to 2660 02:47:34,240 --> 02:47:37,040 export the Au 2661 02:47:37,040 --> 02:47:40,200 service it means whenever you will 2662 02:47:40,200 --> 02:47:44,399 import the Au module into another module 2663 02:47:44,399 --> 02:47:47,359 like we have imported the Au module into 2664 02:47:47,359 --> 02:47:49,640 app module it means 2665 02:47:49,640 --> 02:47:52,640 I can inject the OD service inside the 2666 02:47:52,640 --> 02:47:54,240 app 2667 02:47:54,240 --> 02:47:59,479 module that is why I exported the OD 2668 02:48:00,200 --> 02:48:03,319 service let's create the user 2669 02:48:03,319 --> 02:48:05,880 module I'm going to create 2670 02:48:05,880 --> 02:48:07,600 manually 2671 02:48:07,600 --> 02:48:11,359 users. module. 2672 02:48:11,840 --> 02:48:13,960 TS I paste 2673 02:48:13,960 --> 02:48:17,720 it we don't have user service 2674 02:48:17,720 --> 02:48:19,359 yet 2675 02:48:19,359 --> 02:48:22,960 we have to create the user 2676 02:48:25,960 --> 02:48:29,960 service let's use the 2677 02:48:37,240 --> 02:48:42,080 nli a user service has 2678 02:48:42,080 --> 02:48:44,840 created we have to import the users 2679 02:48:44,840 --> 02:48:47,720 module inside the app 2680 02:48:47,720 --> 02:48:50,720 module 2681 02:48:50,800 --> 02:48:53,560 so we have the user service and we have 2682 02:48:53,560 --> 02:48:56,680 the user 2683 02:49:04,120 --> 02:49:08,000 module I've also added the type or a 2684 02:49:08,000 --> 02:49:11,600 module and registered the user entity 2685 02:49:11,600 --> 02:49:14,200 because I would like to use the users 2686 02:49:14,200 --> 02:49:17,160 repository inside my users module I 2687 02:49:17,160 --> 02:49:19,040 would like to inject the user users 2688 02:49:19,040 --> 02:49:22,560 repository into the user 2689 02:49:23,960 --> 02:49:27,120 service now we have to register a new 2690 02:49:27,120 --> 02:49:30,840 route inside the O 2691 02:49:32,880 --> 02:49:35,200 controller we're going to register the 2692 02:49:35,200 --> 02:49:40,399 signup and the login route inside the O 2693 02:49:41,840 --> 02:49:46,760 controller we don't have a user dto yet 2694 02:49:46,760 --> 02:49:50,080 but we do not we also don't have a user 2695 02:49:50,080 --> 02:49:54,479 service do create 2696 02:50:00,040 --> 02:50:03,960 method I want you to create the user D 2697 02:50:03,960 --> 02:50:06,359 you 2698 02:50:06,479 --> 02:50:09,479 first I can say I would like to add my 2699 02:50:09,479 --> 02:50:13,479 DS inside the dto 2700 02:50:13,720 --> 02:50:15,960 folder 2701 02:50:15,960 --> 02:50:19,600 create user. dto 2702 02:50:19,600 --> 02:50:23,319 file I'm going to paste my code we are 2703 02:50:23,319 --> 02:50:27,359 expecting the first name last name email 2704 02:50:27,359 --> 02:50:28,520 and the 2705 02:50:28,520 --> 02:50:31,800 password from Network 2706 02:50:31,800 --> 02:50:37,120 request this is the network data request 2707 02:50:39,359 --> 02:50:42,439 object now we got to create the create 2708 02:50:42,439 --> 02:50:45,800 function inside the user service it is 2709 02:50:45,800 --> 02:50:50,120 saving the user inside the database 2710 02:50:50,120 --> 02:50:53,960 do we have a user 2711 02:50:58,279 --> 02:51:00,760 entity now we got to do 2712 02:51:00,760 --> 02:51:03,279 it inside the user 2713 02:51:03,279 --> 02:51:07,160 service here we have to add the 2714 02:51:07,160 --> 02:51:11,000 logic we need to inject the user 2715 02:51:11,000 --> 02:51:13,880 repository I've already told you how can 2716 02:51:13,880 --> 02:51:16,319 you do 2717 02:51:17,520 --> 02:51:20,520 it 2718 02:51:28,040 --> 02:51:32,080 I have created the create 2719 02:51:32,439 --> 02:51:36,000 method what this create method is doing 2720 02:51:36,000 --> 02:51:39,560 remember we have installed bcrypt JS 2721 02:51:39,560 --> 02:51:44,319 package let's import it 2722 02:51:47,520 --> 02:51:50,200 first 2723 02:51:50,200 --> 02:51:53,279 I don't want to copy the code inside the 2724 02:51:53,279 --> 02:51:56,080 Constructor 2725 02:52:04,080 --> 02:52:08,080 function now it looks 2726 02:52:16,479 --> 02:52:20,120 good we have to import it first create 2727 02:52:20,120 --> 02:52:22,840 BPT 2728 02:52:24,319 --> 02:52:27,640 package here we are just injecting the 2729 02:52:27,640 --> 02:52:28,840 user 2730 02:52:28,840 --> 02:52:32,160 repository we are generating Assa it is 2731 02:52:32,160 --> 02:52:34,520 nothing more it's just a 2732 02:52:34,520 --> 02:52:37,920 number we need a salt to encrypt the 2733 02:52:37,920 --> 02:52:41,319 user password there is a hash method 2734 02:52:41,319 --> 02:52:43,640 inside the bcrypt library and we are 2735 02:52:43,640 --> 02:52:45,279 using this hash 2736 02:52:45,279 --> 02:52:48,680 method it is going to return the promise 2737 02:52:48,680 --> 02:52:51,479 first you first argument would be PL 2738 02:52:51,479 --> 02:52:54,399 text password we are getting the plain 2739 02:52:54,399 --> 02:52:57,560 text password from the network request 2740 02:52:57,560 --> 02:53:01,080 and this is our salt and finally we are 2741 02:53:01,080 --> 02:53:03,920 just saving the user depository user 2742 02:53:03,920 --> 02:53:05,160 inside the 2743 02:53:05,160 --> 02:53:09,600 repository and we are deleting the 2744 02:53:09,920 --> 02:53:13,720 password it's an extra remove 2745 02:53:13,720 --> 02:53:16,080 them I don't want to send the user 2746 02:53:16,080 --> 02:53:18,000 password in the response that's why I 2747 02:53:18,000 --> 02:53:20,160 have deleted deleted the user password 2748 02:53:20,160 --> 02:53:22,680 and finally we are returning the user 2749 02:53:22,680 --> 02:53:26,880 from the response in the in the 2750 02:53:26,880 --> 02:53:29,800 response we have called the user 2751 02:53:29,800 --> 02:53:32,920 service. create method here now it's 2752 02:53:32,920 --> 02:53:35,279 time to test the application one more 2753 02:53:35,279 --> 02:53:39,800 thing we have to refactor a little bit 2754 02:53:39,800 --> 02:53:43,120 our users 2755 02:53:43,760 --> 02:53:46,520 entity I'm going to make it to 2756 02:53:46,520 --> 02:53:48,560 Unique should be true because because 2757 02:53:48,560 --> 02:53:51,200 email should be unique for each 2758 02:53:51,200 --> 02:53:54,520 user and I don't want to save the pass I 2759 02:53:54,520 --> 02:53:56,880 I don't want to send the 2760 02:53:56,880 --> 02:54:00,120 password in the in the 2761 02:54:00,120 --> 02:54:03,120 response it is saying that when working 2762 02:54:03,120 --> 02:54:06,479 with type orm there might be cases where 2763 02:54:06,479 --> 02:54:10,120 you want to exclude one or multiple 2764 02:54:10,120 --> 02:54:12,479 columns field from being 2765 02:54:12,479 --> 02:54:14,720 selected that's why we have used the 2766 02:54:14,720 --> 02:54:16,200 exlode 2767 02:54:16,200 --> 02:54:21,160 now let's run our 2768 02:54:22,399 --> 02:54:26,359 application and we have to save the 2769 02:54:26,359 --> 02:54:30,920 user we're going to send this 2770 02:54:34,399 --> 02:54:37,399 request 2771 02:54:41,960 --> 02:54:44,479 HTTP we have to send request to this 2772 02:54:44,479 --> 02:54:47,560 route because we created this odd /up 2773 02:54:47,560 --> 02:54:49,399 route 2774 02:54:49,399 --> 02:54:51,960 we have to provide first name last name 2775 02:54:51,960 --> 02:54:54,720 email and the password now we're going 2776 02:54:54,720 --> 02:54:57,279 to test it 2777 02:55:05,399 --> 02:55:09,680 out I have caught the 2778 02:55:11,560 --> 02:55:16,840 error if user service is a 2779 02:55:17,319 --> 02:55:19,080 provider 2780 02:55:19,080 --> 02:55:22,720 if user service is a provider it is is 2781 02:55:22,720 --> 02:55:24,760 it the part of au 2782 02:55:24,760 --> 02:55:28,359 module you should learn from the error 2783 02:55:28,359 --> 02:55:31,399 inside the Au 2784 02:55:33,040 --> 02:55:36,439 module we have to import the user module 2785 02:55:36,439 --> 02:55:40,120 because I am using the user service 2786 02:55:40,120 --> 02:55:41,760 inside my o 2787 02:55:41,760 --> 02:55:45,840 controller or inside the a 2788 02:55:45,840 --> 02:55:50,239 service let say user 2789 02:56:02,040 --> 02:56:05,760 module here we are injecting the user 2790 02:56:05,760 --> 02:56:09,880 service inside the O 2791 02:56:13,359 --> 02:56:15,840 controller everything is good to go 2792 02:56:15,840 --> 02:56:17,239 right 2793 02:56:17,239 --> 02:56:21,160 now we have to test our 2794 02:56:21,160 --> 02:56:24,479 application what I would like to send 2795 02:56:24,479 --> 02:56:26,399 the signup 2796 02:56:26,399 --> 02:56:30,399 request let's create a 2797 02:56:30,399 --> 02:56:33,000 new 2798 02:56:33,000 --> 02:56:37,960 account I got the error 500 internal 2799 02:56:37,960 --> 02:56:42,720 error duplicate key value 2800 02:56:42,720 --> 02:56:46,359 Violet unique 2801 02:56:46,560 --> 02:56:49,160 key do we have a 2802 02:56:49,160 --> 02:56:53,239 user inside our database can say John 2803 02:56:53,239 --> 02:56:54,640 one 2804 02:56:54,640 --> 02:56:59,120 two still we are getting the same value 2805 02:56:59,120 --> 02:57:00,000 same 2806 02:57:00,000 --> 02:57:02,800 error cury 2807 02:57:02,800 --> 02:57:07,359 failed error duplicate Cube value viate 2808 02:57:07,359 --> 02:57:11,840 constraint we have changed a data entity 2809 02:57:11,840 --> 02:57:14,600 and the table we have 2810 02:57:14,600 --> 02:57:18,560 to we have changed the database Act I 2811 02:57:18,560 --> 02:57:21,560 have added two new entries unique 2812 02:57:21,560 --> 02:57:23,600 constraint and the 2813 02:57:23,600 --> 02:57:29,359 exclude so we need to drop our 2814 02:57:29,359 --> 02:57:33,439 schema and let me do 2815 02:57:35,239 --> 02:57:38,960 it I cannot 2816 02:57:39,200 --> 02:57:42,359 drop we did not use 2817 02:57:42,359 --> 02:57:45,680 migrations at the end of this module 2818 02:57:45,680 --> 02:57:49,040 I'll show you how to use the migration 2819 02:57:49,040 --> 02:57:51,960 whenever you need to update a database 2820 02:57:51,960 --> 02:57:55,200 you have to create a new 2821 02:57:58,040 --> 02:58:00,590 migration what I want to 2822 02:58:00,590 --> 02:58:03,680 [Music] 2823 02:58:09,840 --> 02:58:14,120 do I want to disconnect from the 2824 02:58:14,120 --> 02:58:16,399 database Spotify 2825 02:58:16,399 --> 02:58:20,560 clone now I would like to 2826 02:58:29,239 --> 02:58:34,120 delete it is used by some 2827 02:58:38,319 --> 02:58:41,279 other let's use 2828 02:58:41,279 --> 02:58:45,720 another database I can say 2829 02:58:47,040 --> 02:58:50,040 one 2830 02:58:52,279 --> 02:58:54,319 I found this is the shortcut to handle 2831 02:58:54,319 --> 02:58:57,000 this error because we did not use 2832 02:58:57,000 --> 02:59:01,520 migration so I can say that now I got 2833 02:59:01,520 --> 02:59:04,279 the data from 2834 02:59:14,600 --> 02:59:17,239 scratch where application is running 2835 02:59:17,239 --> 02:59:19,120 fine now 2836 02:59:19,120 --> 02:59:20,840 let's sign 2837 02:59:20,840 --> 02:59:24,600 up you can see that it has resolved the 2838 02:59:24,600 --> 02:59:28,399 issue the issue was with database update 2839 02:59:28,399 --> 02:59:30,319 I got a 2840 02:59:30,319 --> 02:59:33,279 user when you go to the Spotify clone 2841 02:59:33,279 --> 02:59:35,399 one get rid of this 2842 02:59:35,399 --> 02:59:39,319 one Spotify clone 2843 02:59:40,560 --> 02:59:43,920 one and we have to go to the 2844 02:59:43,920 --> 02:59:47,040 tables inside the 2845 02:59:47,040 --> 02:59:50,040 users 2846 02:59:55,160 --> 02:59:56,479 we have a de 2847 02:59:56,479 --> 03:00:00,760 cord email john1 12@gmail.com and this 2848 03:00:00,760 --> 03:00:04,439 is the password in encrypted 2849 03:00:08,920 --> 03:00:12,200 format in this lesson you are going to 2850 03:00:12,200 --> 03:00:17,479 learn how to perform login request when 2851 03:00:17,479 --> 03:00:21,359 user sent the login request we have to 2852 03:00:21,359 --> 03:00:24,560 verify the user from our database we 2853 03:00:24,560 --> 03:00:27,279 have to decrypt the user password and 2854 03:00:27,279 --> 03:00:30,239 compare the user password we're going to 2855 03:00:30,239 --> 03:00:33,600 implement Json web token 2856 03:00:34,960 --> 03:00:37,200 authentication a user provides the 2857 03:00:37,200 --> 03:00:39,319 credential like username and password to 2858 03:00:39,319 --> 03:00:40,200 the 2859 03:00:40,200 --> 03:00:44,840 server then our server will generate the 2860 03:00:44,840 --> 03:00:49,680 Json web token and send back to the user 2861 03:00:49,680 --> 03:00:52,359 it will have the header payload and 2862 03:00:52,359 --> 03:00:55,080 signature don't worry I'll teach you 2863 03:00:55,080 --> 03:00:58,800 everything when client reive the Json 2864 03:00:58,800 --> 03:00:59,720 web 2865 03:00:59,720 --> 03:01:02,520 token if you are building front-end web 2866 03:01:02,520 --> 03:01:05,160 application you can save the Json web 2867 03:01:05,160 --> 03:01:08,040 token in the local 2868 03:01:08,040 --> 03:01:12,880 storage when user want to receive or 2869 03:01:12,880 --> 03:01:16,840 user wants to receive protected rout he 2870 03:01:16,840 --> 03:01:20,319 must need to provide JWT token in the 2871 03:01:20,319 --> 03:01:22,960 request 2872 03:01:22,960 --> 03:01:27,319 header and server will receive the JWT 2873 03:01:27,319 --> 03:01:29,960 to token from the request 2874 03:01:29,960 --> 03:01:33,439 header and it will verify or validate 2875 03:01:33,439 --> 03:01:36,080 the token if token validated 2876 03:01:36,080 --> 03:01:38,800 successfully it will allow you to access 2877 03:01:38,800 --> 03:01:42,160 the protected route you will learn how 2878 03:01:42,160 --> 03:01:44,920 we are going to implement Json web token 2879 03:01:44,920 --> 03:01:46,840 authentication we're going to use 2880 03:01:46,840 --> 03:01:49,720 passport passport 2881 03:01:49,720 --> 03:01:52,680 strategy or passwort package you have to 2882 03:01:52,680 --> 03:01:55,720 install these two 2883 03:01:56,640 --> 03:02:01,120 packages I'm going to install it in my 2884 03:02:01,120 --> 03:02:05,040 dependencies we have to run npm let 2885 03:02:05,040 --> 03:02:10,760 me hold on we did not create a separate 2886 03:02:12,040 --> 03:02:15,760 Branch module number 2887 03:02:15,760 --> 03:02:17,900 six lesson number two 2888 03:02:17,900 --> 03:02:20,239 [Music] 2889 03:02:20,239 --> 03:02:21,640 now we have 2890 03:02:21,640 --> 03:02:25,200 it npm 2891 03:02:37,760 --> 03:02:41,520 install we have installed two 2892 03:02:41,520 --> 03:02:45,120 packages the next step we have to create 2893 03:02:45,120 --> 03:02:46,359 a login 2894 03:02:46,359 --> 03:02:49,840 route I can create inside my o 2895 03:02:49,840 --> 03:02:52,800 controller let's create a new login 2896 03:02:52,800 --> 03:02:56,800 route we don't have a login D yet we 2897 03:02:56,800 --> 03:02:59,680 also don't have a login function inside 2898 03:02:59,680 --> 03:03:02,359 the OD service 2899 03:03:02,359 --> 03:03:07,800 yet let's create a login 2900 03:03:08,560 --> 03:03:12,200 function inside the OD 2901 03:03:12,200 --> 03:03:16,279 service au. service here we have to 2902 03:03:16,279 --> 03:03:18,680 create the log 2903 03:03:18,680 --> 03:03:20,840 function and we 2904 03:03:20,840 --> 03:03:27,359 also need to get user service as a 2905 03:03:31,239 --> 03:03:36,239 dependency let's create a new 2906 03:03:37,160 --> 03:03:39,920 method first of all we are trying to 2907 03:03:39,920 --> 03:03:42,160 find the 2908 03:03:42,160 --> 03:03:46,000 user on the based on 2909 03:03:46,000 --> 03:03:49,439 email if user found it should give me 2910 03:03:49,439 --> 03:03:51,680 this 2911 03:03:53,840 --> 03:03:57,319 user let's complete this step first we 2912 03:03:57,319 --> 03:03:58,880 have to find the 2913 03:03:58,880 --> 03:04:03,359 user we did not create the find one 2914 03:04:03,359 --> 03:04:09,359 method I can say that inside the user 2915 03:04:09,359 --> 03:04:12,800 service let's create a find one 2916 03:04:12,800 --> 03:04:16,479 method it is going to take the partial 2917 03:04:16,479 --> 03:04:19,120 as a user 2918 03:04:19,120 --> 03:04:21,960 and we're getting the email or we can 2919 03:04:21,960 --> 03:04:24,359 let's try to use the login 2920 03:04:24,359 --> 03:04:27,840 dto login 2921 03:04:29,120 --> 03:04:32,160 dto if it did not find the user it 2922 03:04:32,160 --> 03:04:35,560 should return the unauthorized access we 2923 03:04:35,560 --> 03:04:37,920 have to find the user only based on 2924 03:04:37,920 --> 03:04:41,200 email we don't have login dto let's 2925 03:04:41,200 --> 03:04:44,319 create a login 2926 03:04:45,239 --> 03:04:49,640 dto inside the AU 2927 03:04:50,080 --> 03:04:54,399 dtos I can create 2928 03:04:54,399 --> 03:04:57,720 login. D.S 2929 03:04:57,720 --> 03:05:01,760 file so we have the login 2930 03:05:06,279 --> 03:05:09,800 dto I got the 2931 03:05:09,800 --> 03:05:14,720 email we also need to import login 2932 03:05:16,640 --> 03:05:22,000 dto we also need to import user 2933 03:05:22,960 --> 03:05:26,239 entity and it should return the user 2934 03:05:26,239 --> 03:05:28,239 that's 2935 03:05:28,239 --> 03:05:31,560 it now the second step is we have to 2936 03:05:31,560 --> 03:05:33,319 compare the user 2937 03:05:33,319 --> 03:05:36,680 password inside the OD service the 2938 03:05:36,680 --> 03:05:39,960 second step we have to compare the user 2939 03:05:39,960 --> 03:05:43,359 password I have to import the 2940 03:05:43,359 --> 03:05:49,640 bcrypt import Star as bcrypt from bcrypt 2941 03:05:50,880 --> 03:05:53,399 now I can compare the password with 2942 03:05:53,399 --> 03:05:57,520 plain text password and user encrypted 2943 03:05:57,520 --> 03:06:00,040 password if password 2944 03:06:00,040 --> 03:06:03,200 matched then we have to return the 2945 03:06:03,200 --> 03:06:06,640 user and delete the user password if 2946 03:06:06,640 --> 03:06:09,239 password did not match we have to send 2947 03:06:09,239 --> 03:06:10,880 unauthorized 2948 03:06:10,880 --> 03:06:14,920 exception that is it right 2949 03:06:16,560 --> 03:06:19,560 now 2950 03:06:30,040 --> 03:06:35,120 we did not inject private Au 2951 03:06:38,600 --> 03:06:41,319 service now I have called the 2952 03:06:41,319 --> 03:06:46,359 dto login method and provided the login 2953 03:06:46,520 --> 03:06:49,520 dto 2954 03:06:49,720 --> 03:06:54,760 let's try to send the login 2955 03:06:55,279 --> 03:06:57,760 request rest 2956 03:06:57,760 --> 03:07:00,439 client. 2957 03:07:00,439 --> 03:07:06,040 HTTP we have created a user with John 2958 03:07:06,040 --> 03:07:11,160 12 John 12 run the 2959 03:07:16,520 --> 03:07:19,160 application 2960 03:07:19,160 --> 03:07:23,880 let's send the API request to login 2961 03:07:24,800 --> 03:07:31,040 user it should be 3,000 not a 3,000 2962 03:07:31,040 --> 03:07:35,000 one we successfully got the user back in 2963 03:07:35,000 --> 03:07:37,080 the response let's say I'm going to 2964 03:07:37,080 --> 03:07:41,160 provide the invalid email I got error 2965 03:07:41,160 --> 03:07:44,319 cannot find user if I provide the 2966 03:07:44,319 --> 03:07:46,080 invalid 2967 03:07:46,080 --> 03:07:48,920 password I got password did not match 2968 03:07:48,920 --> 03:07:52,600 and unauthorized it's working fine 2969 03:07:52,600 --> 03:07:57,279 now but our goal is to send the Json web 2970 03:07:57,279 --> 03:08:00,560 token when user made successfully login 2971 03:08:00,560 --> 03:08:03,120 request it should send the Json web 2972 03:08:03,120 --> 03:08:06,000 token in the response in the next lesson 2973 03:08:06,000 --> 03:08:08,080 I'll teach you how to create Json web 2974 03:08:08,080 --> 03:08:10,399 token and how to send the Json web token 2975 03:08:10,399 --> 03:08:12,239 in the 2976 03:08:12,239 --> 03:08:15,120 response we have found the user on the 2977 03:08:15,120 --> 03:08:18,439 based on email and we have encrypted the 2978 03:08:18,439 --> 03:08:20,040 user 2979 03:08:20,040 --> 03:08:22,680 password if user has logged in 2980 03:08:22,680 --> 03:08:24,960 successfully we need to send the Json 2981 03:08:24,960 --> 03:08:26,479 web 2982 03:08:26,479 --> 03:08:29,840 token and when you will apply 2983 03:08:29,840 --> 03:08:32,640 authentication on protected 2984 03:08:32,640 --> 03:08:35,560 route you have to send the Json web 2985 03:08:35,560 --> 03:08:37,600 token in the 2986 03:08:37,600 --> 03:08:41,880 header we're going to use nestjs JWT 2987 03:08:41,880 --> 03:08:45,279 package and passport JWT 2988 03:08:45,279 --> 03:08:49,920 package and then we will import Port jwd 2989 03:08:49,920 --> 03:08:54,720 module you have to provide a unique 2990 03:08:54,720 --> 03:08:57,640 key you have learned how to create the 2991 03:08:57,640 --> 03:09:00,040 login function and 2992 03:09:00,040 --> 03:09:03,640 we you have to refactor it right now you 2993 03:09:03,640 --> 03:09:05,840 have to send the access 2994 03:09:05,840 --> 03:09:10,000 token you can create Json web token by 2995 03:09:10,000 --> 03:09:12,279 using JWT 2996 03:09:12,279 --> 03:09:14,880 service this is the provider you can 2997 03:09:14,880 --> 03:09:19,359 inject it here as a service provider 2998 03:09:19,640 --> 03:09:23,000 then I will move my constants into a 2999 03:09:23,000 --> 03:09:25,680 separate file au. constants 3000 03:09:25,680 --> 03:09:29,200 does and you have to register the JWT 3001 03:09:29,200 --> 03:09:34,760 module and update o constant Secrets I 3002 03:09:34,760 --> 03:09:40,479 also updated the expiry date of Json web 3003 03:09:40,479 --> 03:09:45,439 token it will be expired after 1 3004 03:09:46,080 --> 03:09:50,960 day we going to have JWT service we are 3005 03:09:50,960 --> 03:09:54,600 extending it from the passport 3006 03:09:54,600 --> 03:09:57,560 strategy remember I'm using the nestjs 3007 03:09:57,560 --> 03:09:58,800 passport 3008 03:09:58,800 --> 03:10:03,160 package and we have passport jwd 3009 03:10:03,160 --> 03:10:07,279 strategy here it is just extracting the 3010 03:10:07,279 --> 03:10:09,000 token from the 3011 03:10:09,000 --> 03:10:12,040 header we are checking the Json web 3012 03:10:12,040 --> 03:10:14,439 token expiry 3013 03:10:14,439 --> 03:10:18,600 date or expiry validation or expire 3014 03:10:18,600 --> 03:10:21,120 duration here you have to provide your 3015 03:10:21,120 --> 03:10:23,760 secret key which we have 3016 03:10:23,760 --> 03:10:27,000 saved and then finally we're going to 3017 03:10:27,000 --> 03:10:29,960 have validate method it is returning the 3018 03:10:29,960 --> 03:10:32,720 user ID and the 3019 03:10:32,720 --> 03:10:37,160 email when you apply the 3020 03:10:37,160 --> 03:10:42,160 oard oard JWT oard will call this 3021 03:10:42,160 --> 03:10:43,439 validate 3022 03:10:43,439 --> 03:10:46,319 function you have to register the JWT as 3023 03:10:46,319 --> 03:10:50,439 a provider then this is how you will 3024 03:10:50,439 --> 03:10:52,600 Implement authentication you got to 3025 03:10:52,600 --> 03:10:56,120 create a guard it's like a middleware in 3026 03:10:56,120 --> 03:10:58,640 expressjs if you want to implement role 3027 03:10:58,640 --> 03:11:02,640 based authentication you need 3028 03:11:02,960 --> 03:11:07,439 guards we have jwd odard it is extending 3029 03:11:07,439 --> 03:11:09,120 oard from the 3030 03:11:09,120 --> 03:11:11,600 passport if you want to add your own 3031 03:11:11,600 --> 03:11:13,600 custom logic you can do it 3032 03:11:13,600 --> 03:11:16,760 here this is how you will make your 3033 03:11:16,760 --> 03:11:18,000 route 3034 03:11:18,000 --> 03:11:23,319 protected by using the gwd O card and 3035 03:11:23,319 --> 03:11:27,319 finally you have to apply you have to 3036 03:11:27,319 --> 03:11:31,359 send the authentication request like 3037 03:11:31,359 --> 03:11:33,319 first of all you got a 3038 03:11:33,319 --> 03:11:37,399 login login route will give you the Json 3039 03:11:37,399 --> 03:11:38,479 web 3040 03:11:38,479 --> 03:11:42,239 token then you can access your protected 3041 03:11:42,239 --> 03:11:45,239 route like this is the protected route 3042 03:11:45,239 --> 03:11:47,760 profile is the protected route 3043 03:11:47,760 --> 03:11:50,399 only authenticated user can access this 3044 03:11:50,399 --> 03:11:52,800 profile route so you have to provide the 3045 03:11:52,800 --> 03:11:55,239 validation token which is this one Json 3046 03:11:55,239 --> 03:11:58,279 web token in the authorization barrier 3047 03:11:58,279 --> 03:11:59,800 it's a 3048 03:11:59,800 --> 03:12:02,520 scheme and it will allow you to access 3049 03:12:02,520 --> 03:12:03,479 the 3050 03:12:03,479 --> 03:12:06,720 route so let's implement it first first 3051 03:12:06,720 --> 03:12:12,000 of all we have to install a couple of 3052 03:12:12,520 --> 03:12:15,239 dependencies 3053 03:12:15,239 --> 03:12:19,160 here passport 3054 03:12:20,000 --> 03:12:25,479 JWT nestjs JWT and passport JWT strategy 3055 03:12:25,479 --> 03:12:28,399 I also need dep dependency which is 3056 03:12:28,399 --> 03:12:30,000 typing of 3057 03:12:30,000 --> 03:12:33,880 typescript let's do it 3058 03:12:36,080 --> 03:12:40,920 here and we have to install the 3059 03:12:46,120 --> 03:12:49,120 dependencies 3060 03:12:54,399 --> 03:12:57,600 these packages have 3061 03:12:57,600 --> 03:13:01,920 installed now you are able to use JWT 3062 03:13:01,920 --> 03:13:06,880 module let's use inside the app 3063 03:13:15,720 --> 03:13:19,840 module we have to register 3064 03:13:20,319 --> 03:13:23,960 it we will move this constant into 3065 03:13:23,960 --> 03:13:26,439 separate file but right now I'm going to 3066 03:13:26,439 --> 03:13:29,359 do it in this 3067 03:13:30,040 --> 03:13:33,840 way JWT module. register and this is the 3068 03:13:33,840 --> 03:13:36,319 secret 3069 03:13:37,960 --> 03:13:41,160 key and finally we have to refactor our 3070 03:13:41,160 --> 03:13:43,560 OD 3071 03:13:46,040 --> 03:13:48,160 service 3072 03:13:48,160 --> 03:13:50,479 if everything is 3073 03:13:50,479 --> 03:13:57,880 fine then we can use Au or JWT 3074 03:14:07,160 --> 03:14:10,600 service you have to add this return type 3075 03:14:10,600 --> 03:14:12,880 return 3076 03:14:16,040 --> 03:14:18,520 type 3077 03:14:18,520 --> 03:14:23,080 if password mashed generate adjacent web 3078 03:14:23,080 --> 03:14:27,439 token or create a pad 3079 03:14:28,560 --> 03:14:33,040 object I'm going to create a payload 3080 03:14:43,319 --> 03:14:48,760 object now we are just adding email 3081 03:14:48,760 --> 03:14:52,720 and the user ID inside the payload and 3082 03:14:52,720 --> 03:14:56,439 it is generating the Json web 3083 03:14:59,359 --> 03:15:02,399 token let's create a separate file for 3084 03:15:02,399 --> 03:15:03,760 the 3085 03:15:03,760 --> 03:15:08,359 constants can say o/c 3086 03:15:08,359 --> 03:15:11,359 constants 3087 03:15:11,359 --> 03:15:15,920 do let me refactor it 3088 03:15:15,960 --> 03:15:18,239 here 3089 03:15:18,239 --> 03:15:20,600 one more thing you got to register the 3090 03:15:20,600 --> 03:15:24,640 JWT module in the Au module instead 3091 03:15:24,640 --> 03:15:28,359 of app module that was a 3092 03:15:28,359 --> 03:15:31,480 mistake so this is our authentication 3093 03:15:31,480 --> 03:15:32,760 [Music] 3094 03:15:32,760 --> 03:15:37,040 module we have to register it 3095 03:15:45,479 --> 03:15:49,760 here out constants do 3096 03:15:59,840 --> 03:16:03,640 Secret and you have to 3097 03:16:03,880 --> 03:16:07,560 provide sign in 3098 03:16:13,920 --> 03:16:15,920 options 3099 03:16:15,920 --> 03:16:20,960 options we have expiry date which is one 3100 03:16:20,960 --> 03:16:26,000 day so we have registered the jwd 3101 03:16:26,920 --> 03:16:31,040 module now you have to create jwd 3102 03:16:31,040 --> 03:16:33,399 strategy I can 3103 03:16:33,399 --> 03:16:37,399 say JWT 3104 03:16:37,399 --> 03:16:42,000 strategy dos it's nothing more it's a 3105 03:16:42,000 --> 03:16:45,840 service or provider you can inject 3106 03:16:45,840 --> 03:16:48,840 it 3107 03:16:54,040 --> 03:16:59,239 injectable export class JWT 3108 03:17:01,880 --> 03:17:04,600 strategy passport strategy it is going 3109 03:17:04,600 --> 03:17:06,040 to provide the 3110 03:17:06,040 --> 03:17:09,479 strategy passport 3111 03:17:15,840 --> 03:17:17,880 JWT 3112 03:17:17,880 --> 03:17:22,439 now we have to call Super 3113 03:17:22,439 --> 03:17:24,760 Constructor and you can 3114 03:17:24,760 --> 03:17:27,359 extract the 3115 03:17:27,359 --> 03:17:33,080 header from Au header as a barrier 3116 03:17:36,520 --> 03:17:40,040 token and we have to provide the ignore 3117 03:17:40,040 --> 03:17:43,680 expiration check the expiry 3118 03:17:43,680 --> 03:17:47,479 date secret which is our secret key 3119 03:17:47,479 --> 03:17:52,439 ke and we will have the validate 3120 03:17:53,439 --> 03:17:56,680 function whenever you apply at the O 3121 03:17:56,680 --> 03:18:01,720 card this validate method will be 3122 03:18:15,760 --> 03:18:17,279 called 3123 03:18:17,279 --> 03:18:22,040 our next step is to register GWT 3124 03:18:22,040 --> 03:18:25,720 strategy in the O 3125 03:18:25,720 --> 03:18:30,960 module I have to register it here JWT 3126 03:18:33,720 --> 03:18:37,399 strategy now you have to create the 3127 03:18:37,399 --> 03:18:39,239 guard which is 3128 03:18:39,239 --> 03:18:42,760 JWT guard 3129 03:18:45,720 --> 03:18:48,720 dos 3130 03:18:50,120 --> 03:18:55,279 export class JWT 3131 03:18:57,319 --> 03:19:01,359 guard it is also a class or service 3132 03:19:01,359 --> 03:19:06,640 provider that's why I I'm using the 3133 03:19:07,399 --> 03:19:11,319 injectable and we have to extend it from 3134 03:19:11,319 --> 03:19:12,600 the 3135 03:19:12,600 --> 03:19:16,560 passport from the odard which is from 3136 03:19:16,560 --> 03:19:19,920 the the njs passport 3137 03:19:26,640 --> 03:19:31,600 package make sure you say this is the 3138 03:19:32,000 --> 03:19:36,359 JWT if you want to extend this JWT guard 3139 03:19:36,359 --> 03:19:38,960 you can do 3140 03:19:40,160 --> 03:19:45,040 this our final step is to apply the O 3141 03:19:45,040 --> 03:19:48,040 card to the public public route let's 3142 03:19:48,040 --> 03:19:52,680 say I want to protect this route I did 3143 03:19:52,680 --> 03:19:54,359 not create it 3144 03:19:54,359 --> 03:19:58,279 yet let's say get 3145 03:20:02,080 --> 03:20:06,239 profile and it is going to take 3146 03:20:10,600 --> 03:20:14,840 request return request. 3147 03:20:15,640 --> 03:20:18,640 user 3148 03:20:21,319 --> 03:20:23,040 should be 3149 03:20:23,040 --> 03:20:25,319 decorator now we have 3150 03:20:25,319 --> 03:20:29,520 to let's do it without 3151 03:20:29,520 --> 03:20:34,960 card npm Run start 3152 03:20:45,640 --> 03:20:48,640 f 3153 03:20:49,920 --> 03:20:52,880 our application is running now let's 3154 03:20:52,880 --> 03:20:56,359 send the login request you will receive 3155 03:20:56,359 --> 03:20:57,800 the access 3156 03:20:57,800 --> 03:21:01,000 token I'm going to copy 3157 03:21:01,000 --> 03:21:04,640 it and I going to paste it 3158 03:21:04,640 --> 03:21:08,120 here access 3159 03:21:09,239 --> 03:21:13,800 token now let's access the 3160 03:21:15,560 --> 03:21:18,560 profile 3161 03:21:26,319 --> 03:21:29,000 slash profile because we created the 3162 03:21:29,000 --> 03:21:32,439 profile route in app 3163 03:21:33,239 --> 03:21:35,520 controller I'm going to send the request 3164 03:21:35,520 --> 03:21:38,760 cannot access the 3165 03:21:45,520 --> 03:21:48,520 profile 3166 03:21:52,279 --> 03:21:56,319 I do not add the 3167 03:22:06,359 --> 03:22:10,760 route I can access the profile route I 3168 03:22:10,760 --> 03:22:13,279 got the 200 status 3169 03:22:13,279 --> 03:22:17,000 code it means route is working fine if I 3170 03:22:17,000 --> 03:22:20,600 apply the authentication 3171 03:22:20,680 --> 03:22:25,199 guard I can say use roles 3172 03:22:25,199 --> 03:22:28,880 or use 3173 03:22:29,199 --> 03:22:32,239 roles use guards 3174 03:22:32,239 --> 03:22:37,560 yep I can mention my guard which is or 3175 03:22:40,880 --> 03:22:44,720 guard if you have what in Express JS 3176 03:22:44,720 --> 03:22:47,199 it's nothing more it's like a 3177 03:22:47,199 --> 03:22:50,600 middleware this oard function will run 3178 03:22:50,600 --> 03:22:54,720 before executing the get profile 3179 03:22:54,720 --> 03:22:57,319 function this function runs before 3180 03:22:57,319 --> 03:23:01,040 request Handler which is get 3181 03:23:05,120 --> 03:23:09,920 profile oh it should be jwd o guard jwd 3182 03:23:09,920 --> 03:23:15,040 guard not and this not this one o 3183 03:23:15,439 --> 03:23:18,439 card 3184 03:23:23,800 --> 03:23:30,160 let's rename it to JWT o guard not a JWT 3185 03:23:31,760 --> 03:23:36,880 guard JWT oard it looks 3186 03:23:45,439 --> 03:23:48,439 good 3187 03:23:53,399 --> 03:23:56,600 let's try to access the profile 3188 03:23:56,600 --> 03:24:01,640 route status 401 which means you did not 3189 03:24:01,640 --> 03:24:04,840 provide the access token please provide 3190 03:24:04,840 --> 03:24:07,160 the access token if you want to access 3191 03:24:07,160 --> 03:24:10,920 this private route so you can provide 3192 03:24:10,920 --> 03:24:14,040 the exess token by using this format I 3193 03:24:14,040 --> 03:24:15,359 pasted the 3194 03:24:15,359 --> 03:24:21,640 token awesome I got the user ID and the 3195 03:24:23,760 --> 03:24:27,880 email I have applied the OD card and 3196 03:24:27,880 --> 03:24:31,199 this JWT 3197 03:24:31,199 --> 03:24:34,199 strategy method will be called first 3198 03:24:34,199 --> 03:24:37,000 they are going to run the Constructor 3199 03:24:37,000 --> 03:24:39,000 and then they are going to use the 3200 03:24:39,000 --> 03:24:41,680 validate 3201 03:24:44,080 --> 03:24:46,439 function in this lesson I'm going to 3202 03:24:46,439 --> 03:24:49,160 teach you how to implement role-based 3203 03:24:49,160 --> 03:24:52,000 authentication what is role-based 3204 03:24:52,000 --> 03:24:54,640 authentication if your application has 3205 03:24:54,640 --> 03:24:59,760 multiple roles like artist manager user 3206 03:24:59,760 --> 03:25:02,760 accountant CFO person you can have a 3207 03:25:02,760 --> 03:25:05,399 different functionalities for each role 3208 03:25:05,399 --> 03:25:07,760 in Spotify clone when you look at the 3209 03:25:07,760 --> 03:25:11,640 Spotify clone only artist can upload a 3210 03:25:11,640 --> 03:25:16,199 song If you are a user like let me show 3211 03:25:16,199 --> 03:25:17,920 you you with the help of 3212 03:25:17,920 --> 03:25:19,439 Spotify 3213 03:25:19,439 --> 03:25:22,439 web. 3214 03:25:31,239 --> 03:25:32,840 Spotify 3215 03:25:32,840 --> 03:25:37,040 like if I go to my 3216 03:25:38,600 --> 03:25:41,560 profile I don't have any feature to 3217 03:25:41,560 --> 03:25:43,439 upload a 3218 03:25:43,439 --> 03:25:47,520 song Only approved artic can upload the 3219 03:25:47,520 --> 03:25:49,560 song or delete the 3220 03:25:49,560 --> 03:25:52,040 song in this way you can divide your 3221 03:25:52,040 --> 03:25:55,399 application into multiple 3222 03:25:59,840 --> 03:26:03,520 roles we have create songs 3223 03:26:03,520 --> 03:26:06,279 endpoint I would like to restrict this 3224 03:26:06,279 --> 03:26:10,120 endpoint for other users only artists 3225 03:26:10,120 --> 03:26:14,080 should access this create song 3226 03:26:14,080 --> 03:26:17,040 endpoint we don't have artist module we 3227 03:26:17,040 --> 03:26:19,560 have to create artist module then we're 3228 03:26:19,560 --> 03:26:21,960 going to implement the artist module 3229 03:26:21,960 --> 03:26:25,800 into import the artist module into app 3230 03:26:25,800 --> 03:26:30,000 module we have to write a method find 3231 03:26:30,000 --> 03:26:33,319 artist on the B on user ID then we got 3232 03:26:33,319 --> 03:26:37,319 to add a logic here inside the login 3233 03:26:37,319 --> 03:26:40,720 method we will save the artist ID in the 3234 03:26:40,720 --> 03:26:43,080 payload object and then you have to 3235 03:26:43,080 --> 03:26:44,720 create a new payload 3236 03:26:44,720 --> 03:26:49,880 type and finally you need JWT artist 3237 03:26:49,880 --> 03:26:54,640 card we will have a separate card for 3238 03:26:54,640 --> 03:26:58,279 artist we are extending the jw2 remember 3239 03:26:58,279 --> 03:27:02,199 I told you if you want to customize or 3240 03:27:02,199 --> 03:27:04,680 add a logic in the guard you can do that 3241 03:27:04,680 --> 03:27:07,520 it's just returning a super 3242 03:27:07,520 --> 03:27:10,920 Constructor and here we are using the 3243 03:27:10,920 --> 03:27:14,199 handle request method this function will 3244 03:27:14,199 --> 03:27:17,279 check if you have art artist if you are 3245 03:27:17,279 --> 03:27:20,040 an artist then it is going to return the 3246 03:27:20,040 --> 03:27:22,680 user otherwise it will throw the 3247 03:27:22,680 --> 03:27:25,279 exception we have to refactor the 3248 03:27:25,279 --> 03:27:28,040 validate method in GWT strategy we need 3249 03:27:28,040 --> 03:27:29,920 an artist 3250 03:27:29,920 --> 03:27:35,120 ID then here you will have to apply JWT 3251 03:27:35,120 --> 03:27:38,720 artist guard on the create song 3252 03:27:38,720 --> 03:27:41,439 endpoint you can do that as well or you 3253 03:27:41,439 --> 03:27:45,120 can follow me let me implement 3254 03:27:45,120 --> 03:27:48,120 it 3255 03:27:48,880 --> 03:27:52,640 let's create the artist 3256 03:27:57,120 --> 03:28:00,279 module here we have the artist module it 3257 03:28:00,279 --> 03:28:04,439 has already imported into my app 3258 03:28:04,800 --> 03:28:09,560 module this one the artist 3259 03:28:12,279 --> 03:28:18,120 module we also need R service 3260 03:28:19,040 --> 03:28:24,680 next generate service and I can say 3261 03:28:35,720 --> 03:28:39,600 artist you have to import type orm 3262 03:28:39,600 --> 03:28:41,080 artist 3263 03:28:41,080 --> 03:28:44,760 entity module. for feature because I 3264 03:28:44,760 --> 03:28:46,840 would like to use 3265 03:28:46,840 --> 03:28:49,680 repository here artist repository we 3266 03:28:49,680 --> 03:28:53,640 have to provide the Artist as an 3267 03:28:56,000 --> 03:28:59,359 entity so we have the artist 3268 03:28:59,359 --> 03:29:03,399 service and we also need artist 3269 03:29:03,399 --> 03:29:06,399 controller 3270 03:29:06,800 --> 03:29:10,560 artist I can use a nest generate 3271 03:29:10,560 --> 03:29:13,560 controller 3272 03:29:15,040 --> 03:29:17,520 artist 3273 03:29:17,520 --> 03:29:21,279 now we have the artist 3274 03:29:24,720 --> 03:29:27,080 controller now we're going to create the 3275 03:29:27,080 --> 03:29:30,199 find artist 3276 03:29:30,199 --> 03:29:33,760 method I'm going to copy my 3277 03:29:33,760 --> 03:29:37,960 code inside the artist 3278 03:29:39,560 --> 03:29:42,279 service let's do it 3279 03:29:42,279 --> 03:29:45,040 here we need to import couple of 3280 03:29:45,040 --> 03:29:48,040 dependencies 3281 03:29:53,080 --> 03:29:56,279 we have to find the artist on the based 3282 03:29:56,279 --> 03:29:59,160 on user 3283 03:30:04,160 --> 03:30:08,080 ID our next step is to refactor the 3284 03:30:08,080 --> 03:30:10,720 login 3285 03:30:14,120 --> 03:30:18,439 method inside the art 3286 03:30:19,239 --> 03:30:22,960 service we have to add R 3287 03:30:22,960 --> 03:30:25,960 service 3288 03:30:44,920 --> 03:30:50,279 injected we need to add a artist 3289 03:30:50,279 --> 03:30:55,080 ID inside the payload type inside the 3290 03:30:55,080 --> 03:30:58,800 payload if password 3291 03:30:58,800 --> 03:31:02,520 matched find if user is an 3292 03:31:02,520 --> 03:31:07,120 artist then we need to add the artist ID 3293 03:31:07,120 --> 03:31:09,640 into the 3294 03:31:10,920 --> 03:31:14,600 payload what I can 3295 03:31:14,640 --> 03:31:18,479 say because course I can save the artist 3296 03:31:18,479 --> 03:31:22,040 ID in the payload 3297 03:31:22,319 --> 03:31:28,600 object user do p. artist 3298 03:31:28,920 --> 03:31:33,120 ID should be it is saying that you don't 3299 03:31:33,120 --> 03:31:37,000 have artist ID in the 3300 03:31:37,000 --> 03:31:40,960 payload you can create a payload type 3301 03:31:40,960 --> 03:31:44,840 inside the types p. 3302 03:31:44,840 --> 03:31:47,840 type 3303 03:31:53,439 --> 03:31:57,359 I can say types. 3304 03:32:03,000 --> 03:32:07,479 yes now I can access it I can Define the 3305 03:32:07,479 --> 03:32:09,920 P 3306 03:32:13,920 --> 03:32:18,600 type it should be user 3307 03:32:28,960 --> 03:32:32,760 ID we have mentioned the artist ID is an 3308 03:32:32,760 --> 03:32:34,239 optional 3309 03:32:34,239 --> 03:32:38,960 property now we need to have JWT oard I 3310 03:32:38,960 --> 03:32:42,040 told you how can you implement it we 3311 03:32:42,040 --> 03:32:44,840 already implemented the O 3312 03:32:44,840 --> 03:32:49,239 card let's say I'm going to create a new 3313 03:32:49,239 --> 03:32:52,199 guard inside the 3314 03:32:52,199 --> 03:32:54,960 O I can say 3315 03:32:54,960 --> 03:32:58,880 artist JWT 3316 03:32:59,960 --> 03:33:03,640 godts export class 3317 03:33:03,640 --> 03:33:07,479 artist JWT 3318 03:33:13,359 --> 03:33:17,760 guard extends o guard 3319 03:33:24,680 --> 03:33:29,160 JWT it's a Pur it's a service that's why 3320 03:33:29,160 --> 03:33:31,840 I have to use the injectable it's a 3321 03:33:31,840 --> 03:33:34,199 service 3322 03:33:44,720 --> 03:33:46,800 provider 3323 03:33:46,800 --> 03:33:49,680 if I will write the can 3324 03:33:49,680 --> 03:33:53,920 activate you can see that I got the code 3325 03:33:53,920 --> 03:33:56,800 all I need to call the super 3326 03:33:56,800 --> 03:33:59,479 Constructor super do can activate and 3327 03:33:59,479 --> 03:34:02,319 provide the 3328 03:34:02,359 --> 03:34:05,640 context yep everything is fine you also 3329 03:34:05,640 --> 03:34:08,439 have handle 3330 03:34:08,439 --> 03:34:12,239 request now you can write your logic 3331 03:34:12,239 --> 03:34:15,800 here if there is an error or there is a 3332 03:34:15,800 --> 03:34:18,359 no user then you have to throw the 3333 03:34:18,359 --> 03:34:20,180 unauthorized 3334 03:34:20,180 --> 03:34:23,589 [Music] 3335 03:34:24,199 --> 03:34:27,199 exception if everything is good to go 3336 03:34:27,199 --> 03:34:29,800 you have 3337 03:34:30,439 --> 03:34:33,840 to return the 3338 03:34:33,840 --> 03:34:37,239 user if there is an artist ID then we 3339 03:34:37,239 --> 03:34:40,199 should return the 3340 03:34:42,160 --> 03:34:46,359 user get rid of the extra arguments I'm 3341 03:34:46,359 --> 03:34:49,120 not using 3342 03:34:52,680 --> 03:34:57,160 them when you apply the artist JWT card 3343 03:34:57,160 --> 03:35:00,239 it should call the handle request and it 3344 03:35:00,239 --> 03:35:05,000 will extract the user and try to check 3345 03:35:05,000 --> 03:35:08,239 the artist ID from the 3346 03:35:08,239 --> 03:35:11,800 user if you want to log the user you can 3347 03:35:11,800 --> 03:35:14,479 also do that I will show you what will 3348 03:35:14,479 --> 03:35:17,640 happen inside the user 3349 03:35:17,640 --> 03:35:20,399 we created the JWT AR 3350 03:35:20,399 --> 03:35:23,160 card JWT o 3351 03:35:23,160 --> 03:35:27,279 card jwd arst 3352 03:35:27,680 --> 03:35:30,880 card then we have to refactor a little 3353 03:35:30,880 --> 03:35:35,359 bit validate method in the JWT 3354 03:35:35,359 --> 03:35:39,720 strategy we can add the 3355 03:35:39,960 --> 03:35:44,000 artist it should be P 3356 03:35:44,640 --> 03:35:47,640 type 3357 03:36:11,560 --> 03:36:14,880 now it looks good we have the user ID p. 3358 03:36:14,880 --> 03:36:18,520 email and p. artist 3359 03:36:18,520 --> 03:36:20,640 ID and 3360 03:36:20,640 --> 03:36:25,920 finally we have to apply the jwd artist 3361 03:36:25,920 --> 03:36:30,279 card on your protected endpoint in our 3362 03:36:30,279 --> 03:36:34,439 case we have the songs create endpoint 3363 03:36:34,439 --> 03:36:39,239 we have to protect it for other 3364 03:36:39,239 --> 03:36:44,520 users all you need to apply JWT artist 3365 03:36:44,520 --> 03:36:47,160 card 3366 03:36:47,160 --> 03:36:51,239 and inside the songs 3367 03:36:51,840 --> 03:36:55,239 controller I can restrict this endpoint 3368 03:36:55,239 --> 03:36:57,840 from other 3369 03:36:59,120 --> 03:37:02,840 users I can say that 3370 03:37:03,279 --> 03:37:07,479 jwd artist JWT 3371 03:37:14,520 --> 03:37:16,520 C 3372 03:37:16,520 --> 03:37:19,120 if you want to log in the request you 3373 03:37:19,120 --> 03:37:21,640 can do 3374 03:37:25,760 --> 03:37:30,479 that let's try to do it 3375 03:37:35,560 --> 03:37:40,720 here request decorator and 3376 03:37:44,439 --> 03:37:47,439 request 3377 03:38:09,319 --> 03:38:12,479 I think it should be 3378 03:38:13,479 --> 03:38:18,359 request it's not a requ request this 3379 03:38:19,120 --> 03:38:21,319 one 3380 03:38:21,319 --> 03:38:24,359 request nexts commment and then we have 3381 03:38:24,359 --> 03:38:27,199 to access the 3382 03:38:37,840 --> 03:38:41,960 request why it's giving me the 3383 03:38:44,439 --> 03:38:47,439 error 3384 03:38:55,239 --> 03:38:58,199 I have to do it 3385 03:38:58,520 --> 03:39:01,880 here and 3386 03:39:03,279 --> 03:39:05,920 request now it looks 3387 03:39:05,920 --> 03:39:11,199 good I can log request. 3388 03:39:14,359 --> 03:39:16,279 user 3389 03:39:16,279 --> 03:39:19,239 let's start the 3390 03:39:32,560 --> 03:39:36,160 application I got the 3391 03:39:36,160 --> 03:39:41,040 error the O JWT 3392 03:39:41,040 --> 03:39:47,319 strategy JWT strategy what I did wrong 3393 03:39:51,040 --> 03:39:54,840 oops forgot to 3394 03:39:56,239 --> 03:40:00,760 add I forgot to add closing 3395 03:40:14,319 --> 03:40:17,120 parenthesis 3396 03:40:17,120 --> 03:40:19,920 if you go to the artist table you will 3397 03:40:19,920 --> 03:40:23,600 not see any record because we don't have 3398 03:40:23,600 --> 03:40:27,040 any artist record it's an 3399 03:40:27,040 --> 03:40:30,800 empty if you go to the 3400 03:40:32,120 --> 03:40:36,239 users how many users we 3401 03:40:36,520 --> 03:40:40,120 have I have only single user now I would 3402 03:40:40,120 --> 03:40:42,199 like to make the 3403 03:40:42,199 --> 03:40:46,359 artist what I can say what is wrong 3404 03:40:46,359 --> 03:40:50,880 is au module is a valid njs 3405 03:40:50,880 --> 03:40:55,319 module Artist Artist 3406 03:40:55,319 --> 03:40:59,000 service I think we have to 3407 03:40:59,000 --> 03:41:03,239 import the artist module here inside 3408 03:41:03,239 --> 03:41:05,439 the are we 3409 03:41:05,439 --> 03:41:07,080 using 3410 03:41:07,080 --> 03:41:10,080 the rst 3411 03:41:10,080 --> 03:41:13,560 service yes we are using the RT 3412 03:41:13,560 --> 03:41:17,000 service that is why I need to 3413 03:41:17,000 --> 03:41:20,800 import the artist 3414 03:41:20,960 --> 03:41:24,279 module and inside the artist 3415 03:41:24,279 --> 03:41:30,720 module I can say I have to export artist 3416 03:41:30,720 --> 03:41:33,000 service I want to teach you how to 3417 03:41:33,000 --> 03:41:35,800 handle errors you must know you must 3418 03:41:35,800 --> 03:41:38,840 familiar with error 3419 03:41:44,239 --> 03:41:47,239 handling 3420 03:41:48,000 --> 03:41:51,479 our application is running 3421 03:41:52,239 --> 03:41:55,640 now first of 3422 03:41:56,000 --> 03:42:00,560 all we have to create a new 3423 03:42:01,159 --> 03:42:04,800 artist I can say let's sign up a new 3424 03:42:04,800 --> 03:42:09,840 user or I can say that sign up artist 3425 03:42:09,840 --> 03:42:12,279 let's say 3426 03:42:12,279 --> 03:42:17,159 that it should be let's say c 3427 03:42:18,120 --> 03:42:21,000 Martin 3428 03:42:25,239 --> 03:42:29,080 Garrick Martin 3429 03:42:31,000 --> 03:42:34,760 Garrick we have registered a user which 3430 03:42:34,760 --> 03:42:37,479 which should be which could be an artist 3431 03:42:37,479 --> 03:42:39,720 so if you want to make this user Martin 3432 03:42:39,720 --> 03:42:42,960 Garrick to artist you have to update it 3433 03:42:42,960 --> 03:42:44,680 manually but I did not create the 3434 03:42:44,680 --> 03:42:46,840 endpoint but you can do that I taught 3435 03:42:46,840 --> 03:42:49,040 you how to do 3436 03:42:49,040 --> 03:42:54,800 it let's first of all the ID is 3437 03:42:54,800 --> 03:42:59,520 to I'm going to open all the 3438 03:43:00,359 --> 03:43:04,680 artist and we have to add the 3439 03:43:06,319 --> 03:43:08,920 entry this is user number two and 3440 03:43:08,920 --> 03:43:11,439 finally you have to save 3441 03:43:11,439 --> 03:43:16,359 it now Martin is an artist 3442 03:43:16,359 --> 03:43:19,640 so I have to log in as in Martin 3443 03:43:19,640 --> 03:43:24,159 gar I can say sign up artist 3444 03:43:24,159 --> 03:43:27,640 or login 3445 03:43:28,080 --> 03:43:30,520 artist get rid of first name and the 3446 03:43:30,520 --> 03:43:34,800 last name let's send the login 3447 03:43:34,800 --> 03:43:36,890 request it should be 3448 03:43:36,890 --> 03:43:39,940 [Music] 3449 03:43:40,359 --> 03:43:44,040 login I got the 3450 03:43:44,040 --> 03:43:46,239 token 3451 03:43:46,239 --> 03:43:49,640 now if you check the 3452 03:43:52,279 --> 03:43:56,600 profile it should have the artist ID you 3453 03:43:56,600 --> 03:43:59,640 can see that this is the artist 3454 03:43:59,640 --> 03:44:02,840 ID this is the user 3455 03:44:02,840 --> 03:44:06,920 ID this is the artist ID from the artist 3456 03:44:06,920 --> 03:44:10,120 table this 3457 03:44:10,960 --> 03:44:14,520 one so it's working fine now when you 3458 03:44:14,520 --> 03:44:16,520 have to 3459 03:44:16,520 --> 03:44:20,319 create I for I got I I have to get the 3460 03:44:20,319 --> 03:44:21,520 token 3461 03:44:21,520 --> 03:44:25,240 again let's save the 3462 03:44:25,240 --> 03:44:28,000 [Music] 3463 03:44:28,000 --> 03:44:30,880 token the token will be deleted after 3464 03:44:30,880 --> 03:44:37,479 one day I can say that arst token 3465 03:44:43,000 --> 03:44:45,640 temporary if I try to create create a 3466 03:44:45,640 --> 03:44:48,120 new song it should give me the 3467 03:44:48,120 --> 03:44:50,199 unauthorized because you are not an 3468 03:44:50,199 --> 03:44:51,920 artist and you did not provide the 3469 03:44:51,920 --> 03:44:53,560 authorization 3470 03:44:53,560 --> 03:44:55,319 token 3471 03:44:55,319 --> 03:44:59,199 authorization b e a 3472 03:44:59,640 --> 03:45:03,479 r a r e 3473 03:45:03,680 --> 03:45:08,840 r now I can say we have artist which is 3474 03:45:08,840 --> 03:45:12,479 one Mart Martin Garrick has an animal 3475 03:45:12,479 --> 03:45:14,560 song let's say they want to publish this 3476 03:45:14,560 --> 03:45:16,960 song 3477 03:45:18,359 --> 03:45:21,159 I have the 3478 03:45:21,600 --> 03:45:26,159 artist now it's working fine this is the 3479 03:45:26,159 --> 03:45:29,640 user ID email and the artist ID user ID 3480 03:45:29,640 --> 03:45:33,560 email and the artist ID so we got this 3481 03:45:33,560 --> 03:45:40,239 one artist and the ID colon 3482 03:45:43,920 --> 03:45:46,920 one 3483 03:45:54,720 --> 03:45:58,520 we have successfully created the 3484 03:46:08,399 --> 03:46:13,279 endpoint I can say request. 3485 03:46:13,920 --> 03:46:16,920 user 3486 03:46:34,920 --> 03:46:39,359 let's create a new song from Martin 3487 03:46:39,359 --> 03:46:43,439 Garrick say love 3488 03:46:43,560 --> 03:46:47,520 again so this is the request. user user 3489 03:46:47,520 --> 03:46:51,120 ID email and the artist 3490 03:46:51,920 --> 03:46:55,279 ID in this lesson I'm going to teach you 3491 03:46:55,279 --> 03:46:58,479 how to use or how to enable how to 3492 03:46:58,479 --> 03:47:00,720 create two Factor 3493 03:47:00,720 --> 03:47:02,760 authentication you're going to learn how 3494 03:47:02,760 --> 03:47:05,680 to implement two Factor 3495 03:47:05,680 --> 03:47:07,680 authentication if you don't know about 3496 03:47:07,680 --> 03:47:09,760 two Factor authentication you can read 3497 03:47:09,760 --> 03:47:13,600 this text a user enters the username and 3498 03:47:13,600 --> 03:47:16,760 password on a login page page after 3499 03:47:16,760 --> 03:47:19,520 successful initi initial 3500 03:47:19,520 --> 03:47:22,000 authentication the system sends you the 3501 03:47:22,000 --> 03:47:25,000 onetime password or onetime 3502 03:47:25,000 --> 03:47:28,640 token for the verification a user may 3503 03:47:28,640 --> 03:47:31,239 provide the onetime code generated by 3504 03:47:31,239 --> 03:47:33,279 the authenticator 3505 03:47:33,279 --> 03:47:36,560 app and send to the system then system 3506 03:47:36,560 --> 03:47:39,520 validate this token if token has 3507 03:47:39,520 --> 03:47:41,840 validated then you are able to 3508 03:47:41,840 --> 03:47:44,359 authenticate then you are able to access 3509 03:47:44,359 --> 03:47:46,840 the route 3510 03:47:48,600 --> 03:47:50,600 we're going to use third party package 3511 03:47:50,600 --> 03:47:52,800 to implement two Factor authentication 3512 03:47:52,800 --> 03:47:56,120 Speak Easy and we need a typing first of 3513 03:47:56,120 --> 03:47:58,960 all I would like to show you the demo 3514 03:47:58,960 --> 03:48:02,040 how two Factor authentication works I 3515 03:48:02,040 --> 03:48:04,159 already implemented in my development 3516 03:48:04,159 --> 03:48:06,720 project I can show you the demo first of 3517 03:48:06,720 --> 03:48:09,239 all you got to log to the user I got the 3518 03:48:09,239 --> 03:48:13,199 access token we're going to create a new 3519 03:48:13,199 --> 03:48:16,279 endpoint to enable the 3520 03:48:16,279 --> 03:48:19,319 authentication this endpoint will enable 3521 03:48:19,319 --> 03:48:22,760 the authentication for the user and it 3522 03:48:22,760 --> 03:48:26,239 will create a secret key let me show you 3523 03:48:26,239 --> 03:48:29,479 I'm going to provide this text I 3524 03:48:29,479 --> 03:48:30,920 provided the 3525 03:48:30,920 --> 03:48:34,880 token now I got this secret key and I 3526 03:48:34,880 --> 03:48:37,840 got this response from the Speak Easy 3527 03:48:37,840 --> 03:48:41,720 package base 32 I saved this base 32 3528 03:48:41,720 --> 03:48:44,880 string to my database table for the user 3529 03:48:44,880 --> 03:48:46,800 is for the user 3530 03:48:46,800 --> 03:48:50,600 table a user has this 3531 03:48:50,600 --> 03:48:55,720 record now I I I got this secret key if 3532 03:48:55,720 --> 03:48:57,880 you have authenticator app Google 3533 03:48:57,880 --> 03:48:59,920 Authenticator you 3534 03:48:59,920 --> 03:49:04,720 can you can place the secret 3535 03:49:04,720 --> 03:49:08,359 key I have enabled the Google Chrome 3536 03:49:08,359 --> 03:49:11,800 extension I want you to do it when you 3537 03:49:11,800 --> 03:49:15,040 look at Chrome Google Authenticator you 3538 03:49:15,040 --> 03:49:17,680 have to enable this 3539 03:49:17,680 --> 03:49:20,520 extension now I 3540 03:49:20,520 --> 03:49:23,239 got this authenticator if you have 3541 03:49:23,239 --> 03:49:25,359 Google Authenticator on your phone or 3542 03:49:25,359 --> 03:49:28,199 you can install it all you need to do is 3543 03:49:28,199 --> 03:49:33,040 to add a new app you can also generate 3544 03:49:33,040 --> 03:49:34,600 the QR 3545 03:49:34,600 --> 03:49:40,800 code with this OTP path Ot Ot paath OTP 3546 03:49:40,800 --> 03:49:44,000 o URL you can generate QR Code by using 3547 03:49:44,000 --> 03:49:46,439 this this 3548 03:49:46,439 --> 03:49:50,520 URL but you can add manual entry for T 3549 03:49:50,520 --> 03:49:53,520 testing Sam profile this is the same 3550 03:49:53,520 --> 03:49:56,840 user the name of the user this is the 3551 03:49:56,840 --> 03:50:01,040 secret key now I started getting a 3552 03:50:01,040 --> 03:50:04,479 unique onetime 3553 03:50:04,479 --> 03:50:08,840 password so I have to copy this 3554 03:50:08,840 --> 03:50:11,920 password let me do it again it's going 3555 03:50:11,920 --> 03:50:16,880 to be removed after 1 minute I copied 3556 03:50:16,880 --> 03:50:20,640 it it's going to be 3557 03:50:21,840 --> 03:50:26,439 removed yep now I have to provide this 3558 03:50:26,439 --> 03:50:30,080 token to validate the user I have Pro 3559 03:50:30,080 --> 03:50:33,239 I'm going to provide it here we also 3560 03:50:33,239 --> 03:50:40,359 need to have updated token so copy this 3561 03:50:41,600 --> 03:50:43,640 one 3562 03:50:43,640 --> 03:50:45,640 and 3563 03:50:45,640 --> 03:50:47,320 I have to validate the 3564 03:50:47,320 --> 03:50:50,780 [Music] 3565 03:50:50,960 --> 03:50:55,399 token yes we have to add a new token 3566 03:50:55,399 --> 03:50:58,000 because it will be 3567 03:50:58,000 --> 03:51:02,800 expired now you can see you are 3568 03:51:02,800 --> 03:51:06,359 verified the token has 3569 03:51:06,359 --> 03:51:09,720 validated now this user has enabled the 3570 03:51:09,720 --> 03:51:11,720 two Factor 3571 03:51:11,720 --> 03:51:14,080 authentication if you send the login 3572 03:51:14,080 --> 03:51:16,080 request 3573 03:51:16,080 --> 03:51:18,560 you see this message hey you have 3574 03:51:18,560 --> 03:51:20,520 enabled two Factor 3575 03:51:20,520 --> 03:51:22,720 authentication you can implement this 3576 03:51:22,720 --> 03:51:24,840 Logic on your front end application 3577 03:51:24,840 --> 03:51:30,199 please send the token at this URL please 3578 03:51:30,199 --> 03:51:31,800 send the onetime password token from 3579 03:51:31,800 --> 03:51:34,399 your Google Authenticator app on the 3580 03:51:34,399 --> 03:51:37,640 front end side you can send request to 3581 03:51:37,640 --> 03:51:40,920 this endpoint and provide the token 3582 03:51:40,920 --> 03:51:43,520 that's a complete flow of two Factor 3583 03:51:43,520 --> 03:51:46,520 Authentication 3584 03:51:48,880 --> 03:51:51,479 let's start implementing two Factor 3585 03:51:51,479 --> 03:51:54,000 authentication we have to install couple 3586 03:51:54,000 --> 03:51:55,000 of 3587 03:51:55,000 --> 03:51:58,800 packages we need a Speak Easy 3588 03:51:58,800 --> 03:52:01,399 package we will use this package to 3589 03:52:01,399 --> 03:52:04,560 generate secret key or validate the 3590 03:52:04,560 --> 03:52:07,920 token onetime password we also need 3591 03:52:07,920 --> 03:52:11,680 typing add it as a def 3592 03:52:11,680 --> 03:52:16,520 dependency let's run npm install 3593 03:52:26,279 --> 03:52:30,080 packages have installed the first step 3594 03:52:30,080 --> 03:52:34,479 you have to add two new columns inside 3595 03:52:34,479 --> 03:52:35,920 the user 3596 03:52:35,920 --> 03:52:38,040 entity do 3597 03:52:38,040 --> 03:52:41,040 entity I'm going to add them here by 3598 03:52:41,040 --> 03:52:46,439 default two factor string should be null 3599 03:52:47,600 --> 03:52:50,120 and it's a Boolean property by default I 3600 03:52:50,120 --> 03:52:52,560 have set to default to 3601 03:52:52,560 --> 03:52:57,279 false two Factor secret could be empty 3602 03:52:57,279 --> 03:53:00,520 and this is the Boolean 3603 03:53:01,279 --> 03:53:04,040 value when you run the 3604 03:53:04,040 --> 03:53:06,520 application it should create two new 3605 03:53:06,520 --> 03:53:09,080 columns for the user property for the 3606 03:53:09,080 --> 03:53:11,520 user 3607 03:53:13,439 --> 03:53:16,439 table 3608 03:53:23,840 --> 03:53:26,920 our application is running 3609 03:53:26,920 --> 03:53:32,199 now let's refresh the user 3610 03:53:43,439 --> 03:53:47,319 table I have the 2 FAA secret and enable 3611 03:53:47,319 --> 03:53:51,199 2 FAA which is a Boolean 3612 03:53:52,359 --> 03:53:55,560 column now we're going to create a new 3613 03:53:55,560 --> 03:53:59,199 method inside the O service to enable 3614 03:53:59,199 --> 03:54:00,680 the two Factor 3615 03:54:00,680 --> 03:54:02,920 authentication first of all we need 3616 03:54:02,920 --> 03:54:04,319 maybe we need a 3617 03:54:04,319 --> 03:54:11,600 type I can add my type here enable to F 3618 03:54:11,600 --> 03:54:16,359 type or you can create an interface 3619 03:54:17,359 --> 03:54:21,159 we have to type and then we need a 3620 03:54:21,159 --> 03:54:23,640 method you have to create a new method 3621 03:54:23,640 --> 03:54:26,840 to find the user on the based on user 3622 03:54:26,840 --> 03:54:30,439 ID if user has already enabled to factor 3623 03:54:30,439 --> 03:54:32,279 authentication we have to return the 3624 03:54:32,279 --> 03:54:35,880 secret key if user did not enable the 2 3625 03:54:35,880 --> 03:54:36,960 FAA 3626 03:54:36,960 --> 03:54:39,159 authentication then we have to generate 3627 03:54:39,159 --> 03:54:41,800 the secret key this is we use the secret 3628 03:54:41,800 --> 03:54:44,760 key and we're going to use the base 32 3629 03:54:44,760 --> 03:54:47,479 secret key and finally we have to update 3630 03:54:47,479 --> 03:54:51,000 the 2fa secret 2fa key for the specific 3631 03:54:51,000 --> 03:54:54,000 user and then you have to write the do 3632 03:54:54,000 --> 03:54:56,920 you have to return the secret key in the 3633 03:54:56,920 --> 03:55:00,680 response we got to find the user if user 3634 03:55:00,680 --> 03:55:03,520 already enabled the secret two Factor 3635 03:55:03,520 --> 03:55:05,880 authentication we have to return the 3636 03:55:05,880 --> 03:55:10,120 saved to to f secret otherwise we have 3637 03:55:10,120 --> 03:55:12,199 to generate a new secret and then you 3638 03:55:12,199 --> 03:55:14,319 have to set this secret for the user 3639 03:55:14,319 --> 03:55:20,159 user and finally you have to update the 3640 03:55:28,920 --> 03:55:32,319 user I'm going to copy the 3641 03:55:32,319 --> 03:55:34,750 code let's do 3642 03:55:34,750 --> 03:55:37,000 [Music] 3643 03:55:37,000 --> 03:55:41,040 everything inside the OD 3644 03:55:43,239 --> 03:55:46,120 service 3645 03:55:46,120 --> 03:55:48,479 I have to create a new 3646 03:55:48,479 --> 03:55:52,479 method import Speak 3647 03:55:55,760 --> 03:56:00,120 Easy we also need to import Toof enable 3648 03:56:00,120 --> 03:56:03,239 type we don't have a method find by ID 3649 03:56:03,239 --> 03:56:04,560 do we have a 3650 03:56:04,560 --> 03:56:07,680 method find by ID inside the user 3651 03:56:07,680 --> 03:56:09,600 service 3652 03:56:09,600 --> 03:56:13,239 not we have to create the find by ID 3653 03:56:13,239 --> 03:56:16,239 method 3654 03:56:36,199 --> 03:56:41,040 you can also do the secret key or unable 3655 03:56:41,040 --> 03:56:46,600 to factor authentication here use user 3656 03:56:47,399 --> 03:56:52,479 dot let me first add a new method inside 3657 03:56:52,479 --> 03:56:54,319 the user 3658 03:56:54,319 --> 03:56:58,920 service let's go to our next 3659 03:56:58,920 --> 03:57:02,439 step user 3660 03:57:03,040 --> 03:57:07,600 service here we need this 3661 03:57:07,760 --> 03:57:11,319 method I'm going to copy this method now 3662 03:57:11,319 --> 03:57:12,439 we have 3663 03:57:12,439 --> 03:57:16,159 defin by ID 3664 03:57:20,600 --> 03:57:23,120 it is going to accept the 3665 03:57:23,120 --> 03:57:28,520 ID so we have to provide the user 3666 03:57:37,359 --> 03:57:40,279 ID you can 3667 03:57:40,279 --> 03:57:43,120 enable two Factor 3668 03:57:43,120 --> 03:57:46,720 Authentication to 3669 03:57:47,399 --> 03:57:52,920 true and finally you can call user. Save 3670 03:57:52,920 --> 03:57:56,080 method or no we need a 3671 03:57:56,080 --> 03:57:59,760 repository to save 3672 03:58:00,960 --> 03:58:05,399 it we also need another method it will 3673 03:58:05,399 --> 03:58:10,279 update the secret key and enable the 3674 03:58:10,279 --> 03:58:13,120 authentication you can create a new 3675 03:58:13,120 --> 03:58:15,000 method 3676 03:58:15,000 --> 03:58:17,279 inside the user 3677 03:58:17,279 --> 03:58:21,840 service let me import the update 3678 03:58:22,920 --> 03:58:26,359 results we have completed the enable to 3679 03:58:26,359 --> 03:58:28,120 factor 3680 03:58:28,120 --> 03:58:32,000 authentication now it's time to test 3681 03:58:32,000 --> 03:58:37,560 it let's create an O endpoint inside the 3682 03:58:37,560 --> 03:58:39,920 O 3683 03:58:43,040 --> 03:58:46,040 controller 3684 03:58:51,040 --> 03:58:55,960 I I'm going to use the guard JWT o 3685 03:59:01,800 --> 03:59:06,040 guard if you if you don't 3686 03:59:06,040 --> 03:59:10,040 need anybody you can create it as a get 3687 03:59:10,040 --> 03:59:11,159 end 3688 03:59:11,159 --> 03:59:16,640 point I can say that JW the O 3689 03:59:43,040 --> 03:59:46,040 card 3690 03:59:57,020 --> 04:00:00,149 [Music] 3691 04:00:05,840 --> 04:00:09,760 I just imported missing 3692 04:00:12,960 --> 04:00:15,960 Imports 3693 04:00:34,080 --> 04:00:37,720 we have a login user sign up user sign 3694 04:00:37,720 --> 04:00:42,920 up artist login user here I can 3695 04:00:42,920 --> 04:00:44,760 say 3696 04:00:44,760 --> 04:00:48,479 enable two Factor 3697 04:00:50,720 --> 04:00:52,359 authentication 3698 04:00:52,359 --> 04:00:54,439 profile 3699 04:00:54,439 --> 04:00:59,120 Au enable two Factor 3700 04:01:01,279 --> 04:01:03,800 authentication let me double check that 3701 04:01:03,800 --> 04:01:07,479 enable to fa we also need to provide the 3702 04:01:07,479 --> 04:01:10,319 authorization token so we need to get 3703 04:01:10,319 --> 04:01:12,239 get the 3704 04:01:12,239 --> 04:01:16,520 token I got the the access 3705 04:01:17,439 --> 04:01:21,239 token provide it 3706 04:01:25,319 --> 04:01:28,920 here let's send the request I got the 3707 04:01:28,920 --> 04:01:31,720 secret key that's 3708 04:01:31,720 --> 04:01:34,159 amazing what is the name of the user 3709 04:01:34,159 --> 04:01:35,520 which is 3710 04:01:35,520 --> 04:01:38,680 John if you have Google Authenticator on 3711 04:01:38,680 --> 04:01:42,479 your phone you can install it manually 3712 04:01:42,479 --> 04:01:45,159 I'm going to add a new 3713 04:01:45,159 --> 04:01:49,680 profiles which is John 3714 04:01:51,880 --> 04:01:55,680 user add your secret 3715 04:01:55,680 --> 04:01:59,520 key it is generating onetime password 3716 04:01:59,520 --> 04:02:01,520 for 1 minute 3717 04:02:01,520 --> 04:02:06,760 only we have also saved the key into the 3718 04:02:06,760 --> 04:02:10,159 user users 3719 04:02:11,399 --> 04:02:13,960 table let's fetch all the US 3720 04:02:13,960 --> 04:02:17,050 [Music] 3721 04:02:18,080 --> 04:02:22,199 users here we have the secret key and it 3722 04:02:22,199 --> 04:02:26,239 has enabled two Factor 3723 04:02:26,520 --> 04:02:29,080 authentication our next step is to 3724 04:02:29,080 --> 04:02:32,760 validate the onetime password 3725 04:02:32,760 --> 04:02:36,680 token let me open 3726 04:02:38,880 --> 04:02:41,840 it we have created a new user or 3727 04:02:41,840 --> 04:02:44,720 registered a new user in the Google 3728 04:02:44,720 --> 04:02:48,479 authenticator it is generating onetime 3729 04:02:48,479 --> 04:02:52,159 password we have to take this token or 3730 04:02:52,159 --> 04:02:55,279 onetime password and try to validate 3731 04:02:55,279 --> 04:02:56,600 with our 3732 04:02:56,600 --> 04:02:58,600 application we're going to implement 3733 04:02:58,600 --> 04:03:01,520 this logic first of all you may you may 3734 04:03:01,520 --> 04:03:04,439 need another 3735 04:03:04,439 --> 04:03:08,159 endpoint validate to fa we are also 3736 04:03:08,159 --> 04:03:11,239 using a JWT o card we are getting the 3737 04:03:11,239 --> 04:03:14,479 request here we will have a method 3738 04:03:14,479 --> 04:03:17,080 validated to f a token it is going to 3739 04:03:17,080 --> 04:03:20,600 provide the user ID the logged in user 3740 04:03:20,600 --> 04:03:23,720 ID and we also need to get the token 3741 04:03:23,720 --> 04:03:26,760 valid validate token dto we will create 3742 04:03:26,760 --> 04:03:29,000 validate token 3743 04:03:29,000 --> 04:03:32,319 dto I'm going to create a new 3744 04:03:32,319 --> 04:03:36,239 endpoint inside the O 3745 04:03:39,159 --> 04:03:41,840 controller I created the post because we 3746 04:03:41,840 --> 04:03:44,560 need to get the token you can also use 3747 04:03:44,560 --> 04:03:47,960 the get and get the token 3748 04:03:48,600 --> 04:03:52,720 prams JWT 3749 04:03:52,960 --> 04:03:58,000 oard let's create the validate token 3750 04:03:59,600 --> 04:04:03,000 dto inside the 3751 04:04:03,000 --> 04:04:05,359 dto I can 3752 04:04:05,359 --> 04:04:07,920 have validate 3753 04:04:07,920 --> 04:04:10,920 token. 3754 04:04:12,720 --> 04:04:15,720 D.S 3755 04:04:16,279 --> 04:04:19,720 now we have 3756 04:04:21,479 --> 04:04:27,720 to create a new method to validate to fa 3757 04:04:40,560 --> 04:04:44,800 token here we need to create this method 3758 04:04:44,800 --> 04:04:46,920 it is going to accept the user ID the 3759 04:04:46,920 --> 04:04:51,199 token the onetime password like this 3760 04:04:52,840 --> 04:04:55,800 one this one this 3761 04:04:55,800 --> 04:04:59,040 one and we will find the user on the 3762 04:04:59,040 --> 04:05:01,159 based on 3763 04:05:01,159 --> 04:05:04,120 ID and then we have 3764 04:05:04,120 --> 04:05:08,120 to get this secret key which we saved 3765 04:05:08,120 --> 04:05:10,319 earlier and then you have to provide the 3766 04:05:10,319 --> 04:05:13,000 token and we have to mention encoding 3767 04:05:13,000 --> 04:05:14,439 which is based 3768 04:05:14,439 --> 04:05:18,359 32 if token is 3769 04:05:18,439 --> 04:05:20,760 verified where is 3770 04:05:20,760 --> 04:05:23,920 that if token is verified then we have 3771 04:05:23,920 --> 04:05:26,800 to return verified to True otherwise we 3772 04:05:26,800 --> 04:05:29,800 have to return verified to false you can 3773 04:05:29,800 --> 04:05:34,000 also rapar code inside the TR catch 3774 04:05:34,000 --> 04:05:37,560 block I'm going to copy my complete 3775 04:05:37,560 --> 04:05:40,319 code and I'm going to paste it here 3776 04:05:40,319 --> 04:05:42,640 inside the OD 3777 04:05:42,640 --> 04:05:45,640 service 3778 04:05:45,920 --> 04:05:49,000 the code is complete 3779 04:06:02,520 --> 04:06:05,840 self-explanatory now we have to test it 3780 04:06:05,840 --> 04:06:09,040 we already created the 3781 04:06:09,040 --> 04:06:12,560 route inside the rest 3782 04:06:12,560 --> 04:06:15,560 client 3783 04:06:17,040 --> 04:06:18,920 I can use 3784 04:06:18,920 --> 04:06:21,880 that and I'm going to paste it 3785 04:06:21,880 --> 04:06:25,359 here I can say 3786 04:06:25,359 --> 04:06:29,080 validate to a 3787 04:06:32,000 --> 04:06:35,720 token validate to 3788 04:06:35,720 --> 04:06:38,359 fa let me double check my controller 3789 04:06:38,359 --> 04:06:40,840 validate to 3790 04:06:40,840 --> 04:06:45,520 fa it's a post end point 3791 04:06:46,880 --> 04:06:52,600 point and we have to app content 3792 04:06:52,600 --> 04:06:56,279 type application 3793 04:06:56,880 --> 04:07:00,760 Json let's provide the 3794 04:07:02,720 --> 04:07:05,800 token let me get the 3795 04:07:05,800 --> 04:07:09,199 token John 3796 04:07:11,279 --> 04:07:14,720 user amazing we got got the verifi to 3797 04:07:14,720 --> 04:07:17,520 true if I provide the invalid 3798 04:07:17,520 --> 04:07:21,840 token I got the verifi to 3799 04:07:32,000 --> 04:07:35,520 false I got the verified false 3800 04:07:35,520 --> 04:07:39,760 because because token has expired there 3801 04:07:39,760 --> 04:07:41,560 is a new 3802 04:07:41,560 --> 04:07:45,080 token we successfully enabled two Factor 3803 04:07:45,080 --> 04:07:47,800 authentication validated with 3804 04:07:47,800 --> 04:07:51,520 token you can 3805 04:07:52,319 --> 04:07:55,920 add you can also add disabled 3806 04:07:55,920 --> 04:07:58,680 authentication logic or you can have 3807 04:07:58,680 --> 04:08:00,399 this 3808 04:08:00,399 --> 04:08:03,560 feature let's implement it by creating a 3809 04:08:03,560 --> 04:08:06,960 new function inside the OD 3810 04:08:06,960 --> 04:08:10,560 service it's nothing more just disable 3811 04:08:10,560 --> 04:08:16,040 the authentication and or set this 3812 04:08:20,080 --> 04:08:22,920 value we need to have that function 3813 04:08:22,920 --> 04:08:25,080 inside the user service because we have 3814 04:08:25,080 --> 04:08:28,920 repository inside the user 3815 04:08:30,479 --> 04:08:34,479 service I can say 3816 04:08:37,760 --> 04:08:41,560 that so we have this method disabled to 3817 04:08:41,560 --> 04:08:44,439 fa now it's time to create a new route 3818 04:08:44,439 --> 04:08:47,399 to disable the 3819 04:08:48,840 --> 04:08:52,840 authentication inside the O 3820 04:08:57,600 --> 04:08:59,880 controller I'm going to create a new 3821 04:08:59,880 --> 04:09:02,560 Endo 3822 04:09:10,279 --> 04:09:11,800 here 3823 04:09:11,800 --> 04:09:15,359 JWT o 3824 04:09:26,319 --> 04:09:29,359 there is a typo here spelling issue for 3825 04:09:29,359 --> 04:09:31,880 the O 3826 04:09:31,880 --> 04:09:37,199 guard J WD o 3827 04:09:42,319 --> 04:09:45,319 guard 3828 04:10:12,319 --> 04:10:15,319 e 3829 04:10:26,760 --> 04:10:29,920 everything is fixed 3830 04:10:42,239 --> 04:10:44,920 now 3831 04:10:44,920 --> 04:10:50,119 let's try to send disable authentication 3832 04:10:50,680 --> 04:10:53,520 request inside the Au controller this is 3833 04:10:53,520 --> 04:10:55,520 the get 3834 04:10:55,520 --> 04:10:59,520 endpoint I can have 3835 04:10:59,600 --> 04:11:03,880 that let me paste it here 3836 04:11:03,880 --> 04:11:08,600 disable authentication disable to 3837 04:11:09,359 --> 04:11:15,239 fa it should be disable not an enable 3838 04:11:18,960 --> 04:11:22,720 so it has disabled authentication you 3839 04:11:22,720 --> 04:11:25,800 can double check it by 3840 04:11:25,800 --> 04:11:28,840 login I got the 3841 04:11:28,840 --> 04:11:34,119 token and let me check it from my 3842 04:11:34,239 --> 04:11:38,359 DB you can see that it has dis enable 3843 04:11:38,359 --> 04:11:41,399 false set the 3844 04:11:42,239 --> 04:11:45,239 false 3845 04:11:55,199 --> 04:11:57,279 one more thing you can do if you 3846 04:11:57,279 --> 04:11:59,479 disabled the authentication you can 3847 04:11:59,479 --> 04:12:03,520 delete the secret key or you can use the 3848 04:12:03,520 --> 04:12:07,040 existing secret key let me let me do 3849 04:12:07,040 --> 04:12:11,159 it yeah we have to disable or deleted 3850 04:12:11,159 --> 04:12:12,159 the 3851 04:12:12,159 --> 04:12:13,680 key 3852 04:12:13,680 --> 04:12:16,439 inside the 3853 04:12:16,479 --> 04:12:20,199 disable validate 3854 04:12:23,479 --> 04:12:26,479 token 3855 04:12:27,159 --> 04:12:30,520 disabled I can 3856 04:12:30,520 --> 04:12:35,439 say to FS secret to set to 3857 04:12:42,119 --> 04:12:43,840 false 3858 04:12:43,840 --> 04:12:46,960 or you can set the null 3859 04:12:46,960 --> 04:12:52,479 value let's try to do the null 3860 04:13:05,520 --> 04:13:09,080 value let's enable the authentication I 3861 04:13:09,080 --> 04:13:11,520 got the 3862 04:13:11,520 --> 04:13:15,439 secret double check it from the DB we 3863 04:13:15,439 --> 04:13:19,479 have enabled and we have a different 3864 04:13:19,479 --> 04:13:22,239 secret 3865 04:13:24,520 --> 04:13:26,880 key I don't want to save the I don't 3866 04:13:26,880 --> 04:13:29,560 want to change the secret 3867 04:13:29,560 --> 04:13:34,159 key you should not change the secret 3868 04:13:40,000 --> 04:13:43,640 key maybe it depends on your use 3869 04:13:43,640 --> 04:13:46,359 case if client disabl 3870 04:13:46,359 --> 04:13:50,680 this to fa maybe he has to scan the QR 3871 04:13:50,680 --> 04:13:53,000 code 3872 04:13:57,560 --> 04:14:02,800 again let's disable it it looks 3873 04:14:04,119 --> 04:14:08,359 good we have empty to fa and it has set 3874 04:14:08,359 --> 04:14:10,560 to 3875 04:14:12,040 --> 04:14:15,040 false 3876 04:14:24,279 --> 04:14:29,640 I also have to add this property into my 3877 04:14:42,040 --> 04:14:45,040 notes 3878 04:14:46,680 --> 04:14:49,720 if user has enabled to factor 3879 04:14:49,720 --> 04:14:52,359 authentication we have to customize a 3880 04:14:52,359 --> 04:14:55,880 little bit Logic for the login 3881 04:14:55,880 --> 04:15:00,000 method we have to send the 3882 04:15:00,399 --> 04:15:05,600 link to user this one validate to F 3883 04:15:05,600 --> 04:15:08,800 token this 3884 04:15:11,720 --> 04:15:15,279 one if user has enabled to fa and have 3885 04:15:15,279 --> 04:15:16,600 the secret 3886 04:15:16,600 --> 04:15:21,119 key then you have to return this 3887 04:15:22,800 --> 04:15:25,359 type go to the 3888 04:15:25,359 --> 04:15:28,040 login or 3889 04:15:28,040 --> 04:15:32,159 service go to the login 3890 04:15:33,080 --> 04:15:37,199 method here I can add a 3891 04:15:37,199 --> 04:15:41,159 logic we have to add return type new 3892 04:15:41,159 --> 04:15:44,399 return type 3893 04:15:44,399 --> 04:15:45,640 this 3894 04:15:45,640 --> 04:15:48,760 [Music] 3895 04:15:59,960 --> 04:16:03,359 one let's try to access the login 3896 04:16:03,359 --> 04:16:07,199 function by sending the login 3897 04:16:07,199 --> 04:16:09,840 request we have enable two Factor 3898 04:16:09,840 --> 04:16:11,920 authentication it should give you the 3899 04:16:11,920 --> 04:16:15,920 link not an access 3900 04:16:15,960 --> 04:16:19,479 token we have to log in the 3901 04:16:19,479 --> 04:16:23,920 user where is the login 3902 04:16:25,520 --> 04:16:28,920 user here we 3903 04:16:34,000 --> 04:16:37,329 [Music] 3904 04:16:41,840 --> 04:16:44,520 have 3905 04:16:44,520 --> 04:16:47,760 let's double check 3906 04:16:47,760 --> 04:16:53,439 it we have empty2 FAA that's why I got 3907 04:16:53,439 --> 04:16:54,680 the access 3908 04:16:54,680 --> 04:16:57,920 token let's enable 3909 04:16:57,920 --> 04:17:02,279 it we enabled the two Factor 3910 04:17:06,359 --> 04:17:10,119 authentication let's send the 3911 04:17:11,840 --> 04:17:14,279 login 3912 04:17:14,279 --> 04:17:17,520 now I got the link validate to fa please 3913 04:17:17,520 --> 04:17:20,880 send the one onetime password or token 3914 04:17:20,880 --> 04:17:23,880 from your Google Authenticator app you 3915 04:17:23,880 --> 04:17:26,520 can implement this Logic on the front 3916 04:17:26,520 --> 04:17:28,960 end on front end you can send the 3917 04:17:28,960 --> 04:17:31,199 request to this URL and you have to 3918 04:17:31,199 --> 04:17:34,159 provide the token a user user has to 3919 04:17:34,159 --> 04:17:35,920 provide the 3920 04:17:35,920 --> 04:17:38,199 token so we have completed the two 3921 04:17:38,199 --> 04:17:40,479 Factor 3922 04:17:41,840 --> 04:17:44,840 Authentication 3923 04:17:45,359 --> 04:17:47,920 in this lesson you're going to learn how 3924 04:17:47,920 --> 04:17:51,920 to implement API key 3925 04:17:55,000 --> 04:17:57,399 authentication each user will have its 3926 04:17:57,399 --> 04:18:01,640 own API Keys when he wants to access 3927 04:18:01,640 --> 04:18:04,359 authenticated route or protected route 3928 04:18:04,359 --> 04:18:08,119 he has to provide the API 3929 04:18:09,840 --> 04:18:12,439 Keys when you need to control the number 3930 04:18:12,439 --> 04:18:15,439 of calls made to your API you can check 3931 04:18:15,439 --> 04:18:19,159 from your API Keys how many times API 3932 04:18:19,159 --> 04:18:21,119 Keys has 3933 04:18:21,119 --> 04:18:24,319 hit if you want to identify the usage 3934 04:18:24,319 --> 04:18:27,080 patterns in your API traffic you can do 3935 04:18:27,080 --> 04:18:30,399 with the help of API 3936 04:18:30,960 --> 04:18:34,080 Keys API key authentication is typically 3937 04:18:34,080 --> 04:18:37,000 used in scenarios where multiple users 3938 04:18:37,000 --> 04:18:40,199 or application need to access an 3939 04:18:40,199 --> 04:18:42,880 API you can read this text you can read 3940 04:18:42,880 --> 04:18:43,880 the complete 3941 04:18:43,880 --> 04:18:46,800 flow we're going to use this step we're 3942 04:18:46,800 --> 04:18:49,840 going to generate API Keys we can create 3943 04:18:49,840 --> 04:18:53,479 and store API key for each user we have 3944 04:18:53,479 --> 04:18:56,680 to create an API key 3945 04:18:56,680 --> 04:18:59,199 strategy strategy service just like we 3946 04:18:59,199 --> 04:19:01,239 did for the JWT 3947 04:19:01,239 --> 04:19:04,600 strategy and we have to register API key 3948 04:19:04,600 --> 04:19:06,439 strategy in a 3949 04:19:06,439 --> 04:19:09,279 module and the step number five validate 3950 04:19:09,279 --> 04:19:13,399 the user by API key and apply API key 3951 04:19:13,399 --> 04:19:16,640 authentication on protected route I have 3952 04:19:16,640 --> 04:19:20,279 to use external package which is uu ID 3953 04:19:20,279 --> 04:19:22,000 to generate API 3954 04:19:22,000 --> 04:19:25,960 key and we will have API key column in 3955 04:19:25,960 --> 04:19:27,359 the user 3956 04:19:27,359 --> 04:19:32,159 entity and then when we have to create a 3957 04:19:32,159 --> 04:19:33,399 new 3958 04:19:33,399 --> 04:19:37,920 user what you have to do you got to set 3959 04:19:37,920 --> 04:19:41,359 the API key which is U ID by calling 3960 04:19:41,359 --> 04:19:46,279 this method it will generate a unique 3961 04:19:46,279 --> 04:19:49,600 key when you send the when you create a 3962 04:19:49,600 --> 04:19:54,960 new account it should give you this API 3963 04:19:55,800 --> 04:19:59,880 key and Next Step you have to create API 3964 04:19:59,880 --> 04:20:00,840 key 3965 04:20:00,840 --> 04:20:04,119 strategy when you provide the API key it 3966 04:20:04,119 --> 04:20:06,119 is going to call this method the 3967 04:20:06,119 --> 04:20:10,000 validate function and inside the 3968 04:20:10,000 --> 04:20:13,399 validate you have to call validate user 3969 04:20:13,399 --> 04:20:15,800 by API key and you have to provide your 3970 04:20:15,800 --> 04:20:16,960 API 3971 04:20:16,960 --> 04:20:20,920 key if user is validated this is how you 3972 04:20:20,920 --> 04:20:23,359 will provide the API key authorization 3973 04:20:23,359 --> 04:20:26,319 barrier this is my API key we're going 3974 04:20:26,319 --> 04:20:29,159 to use external package passport HTTP 3975 04:20:29,159 --> 04:20:31,239 barrier to apply this strategy to 3976 04:20:31,239 --> 04:20:34,840 validate API Keys finally you have to 3977 04:20:34,840 --> 04:20:38,880 register as a provider API key strategy 3978 04:20:38,880 --> 04:20:41,159 this is our method to validate the user 3979 04:20:41,159 --> 04:20:43,279 on the based on API 3980 04:20:43,279 --> 04:20:46,239 key if user validated with the help of 3981 04:20:46,239 --> 04:20:48,800 API 3982 04:20:49,760 --> 04:20:53,680 key this is our Endo here I'm I can say 3983 04:20:53,680 --> 04:20:55,720 I want to access the 3984 04:20:55,720 --> 04:21:00,399 profile on the based on API key or you 3985 04:21:00,399 --> 04:21:03,720 can create in the O controller use guard 3986 04:21:03,720 --> 04:21:06,920 o guard you have to provide the 3987 04:21:06,920 --> 04:21:12,199 barrier it means I want to apply API key 3988 04:21:12,199 --> 04:21:14,800 valid ation or API key authentication on 3989 04:21:14,800 --> 04:21:16,159 this 3990 04:21:16,159 --> 04:21:18,920 route and I don't want to set the send 3991 04:21:18,920 --> 04:21:22,399 the user back password back in the 3992 04:21:22,399 --> 04:21:24,680 response that's why I deleted the user 3993 04:21:24,680 --> 04:21:27,720 password and you can send the message 3994 04:21:27,720 --> 04:21:31,080 authenticated with API key this is the 3995 04:21:31,080 --> 04:21:34,720 user this is how you will provide the 3996 04:21:34,720 --> 04:21:37,080 API key when you make the authenticated 3997 04:21:37,080 --> 04:21:39,239 request I want you to implement this 3998 04:21:39,239 --> 04:21:42,800 complete flow if you stuck you can 3999 04:21:42,800 --> 04:21:45,680 follow my 4000 04:22:00,920 --> 04:22:05,720 video Let's install the package 4001 04:22:11,520 --> 04:22:14,520 first 4002 04:22:18,680 --> 04:22:20,640 let's run the 4003 04:22:20,640 --> 04:22:23,720 application you will get the error 4004 04:22:23,720 --> 04:22:26,560 because we have two to three records in 4005 04:22:26,560 --> 04:22:31,119 users table but it will find null value 4006 04:22:31,119 --> 04:22:34,000 for API key we did not set the 4007 04:22:34,000 --> 04:22:36,920 constraint API key should be null if 4008 04:22:36,920 --> 04:22:41,000 user has signed up we can have the API 4009 04:22:41,000 --> 04:22:43,760 key or if if you set the null Lael to 4010 04:22:43,760 --> 04:22:46,640 true you can have a separate function to 4011 04:22:46,640 --> 04:22:49,119 create the API key for the user it 4012 04:22:49,119 --> 04:22:51,640 depends on your use case your company or 4013 04:22:51,640 --> 04:22:53,920 your business 4014 04:22:53,920 --> 04:22:58,199 logic I'm expecting the null value error 4015 04:22:58,199 --> 04:23:00,960 from the type 4016 04:23:02,720 --> 04:23:06,760 orm we are using this 4017 04:23:11,439 --> 04:23:14,439 database 4018 04:23:15,680 --> 04:23:16,960 we have all the 4019 04:23:16,960 --> 04:23:20,040 [Music] 4020 04:23:31,279 --> 04:23:34,159 users you can see I got the eror 4021 04:23:34,159 --> 04:23:36,520 relation user contains null value which 4022 04:23:36,520 --> 04:23:39,159 is API 4023 04:23:39,159 --> 04:23:44,960 key you can fix two or three 4024 04:23:44,960 --> 04:23:50,439 ways you can set API key value manually 4025 04:23:50,439 --> 04:23:54,560 but I don't have API key 4026 04:23:55,840 --> 04:23:59,040 column there is a concept we didn't use 4027 04:23:59,040 --> 04:24:00,560 the 4028 04:24:00,560 --> 04:24:04,760 migration but I found the hack to use to 4029 04:24:04,760 --> 04:24:06,439 resolve this 4030 04:24:06,439 --> 04:24:10,720 issue I can say Spotify clone 2 let's 4031 04:24:10,720 --> 04:24:14,359 change the database 4032 04:24:14,800 --> 04:24:17,520 Run start 4033 04:24:17,520 --> 04:24:20,239 there it will take some time to create 4034 04:24:20,239 --> 04:24:22,800 all the 4035 04:24:30,199 --> 04:24:34,520 tables database does not exist let's 4036 04:24:34,520 --> 04:24:37,199 create a 4037 04:24:41,319 --> 04:24:44,319 DB 4038 04:24:59,119 --> 04:25:01,920 restart the 4039 04:25:07,520 --> 04:25:10,239 application the application is running 4040 04:25:10,239 --> 04:25:11,319 fine 4041 04:25:11,319 --> 04:25:14,050 now 4042 04:25:14,050 --> 04:25:17,430 [Music] 4043 04:25:27,239 --> 04:25:31,520 we have this database Spotify clone 4044 04:25:41,239 --> 04:25:44,239 2 4045 04:25:47,840 --> 04:25:51,479 so we have the API 4046 04:25:58,239 --> 04:26:03,479 key now we need to add the API key when 4047 04:26:03,479 --> 04:26:06,920 you create a new 4048 04:26:08,399 --> 04:26:13,960 account I can do it inside the 4049 04:26:13,960 --> 04:26:17,199 user service or odd 4050 04:26:17,199 --> 04:26:19,890 service do we have the sign up function 4051 04:26:19,890 --> 04:26:22,969 [Music] 4052 04:26:24,760 --> 04:26:28,720 here I think inside the 4053 04:26:29,720 --> 04:26:32,430 users yes we have the create 4054 04:26:32,430 --> 04:26:35,899 [Music] 4055 04:26:41,239 --> 04:26:44,239 function 4056 04:26:44,359 --> 04:26:47,239 I have to change a little bit logic I 4057 04:26:47,239 --> 04:26:51,319 did not create a new user so I'm going 4058 04:26:51,319 --> 04:26:54,359 to do it 4059 04:26:57,840 --> 04:27:02,199 here we have to import this 4060 04:27:10,040 --> 04:27:14,159 package we are getting the 4061 04:27:17,239 --> 04:27:20,239 user. 4062 04:27:20,720 --> 04:27:23,479 password delete this 4063 04:27:23,479 --> 04:27:28,359 one we got it we got the API 4064 04:27:28,359 --> 04:27:32,920 key yeah I can say saved 4065 04:27:41,119 --> 04:27:44,119 password 4066 04:27:56,960 --> 04:28:01,560 I have updated user D to user password 4067 04:28:01,560 --> 04:28:03,479 and we have saved the 4068 04:28:03,479 --> 04:28:06,439 password and we have deleted the saved 4069 04:28:06,439 --> 04:28:09,520 password now we can we have to test it 4070 04:28:09,520 --> 04:28:13,760 by sending this sign up request 4071 04:28:13,760 --> 04:28:15,359 open the r 4072 04:28:15,359 --> 04:28:18,640 client I'm going to send the request 4073 04:28:18,640 --> 04:28:21,800 sign up request sign up 4074 04:28:21,800 --> 04:28:25,760 user so it has generated the API key it 4075 04:28:25,760 --> 04:28:28,279 looks 4076 04:28:30,760 --> 04:28:33,640 good we have encrypted password it's 4077 04:28:33,640 --> 04:28:36,040 fine 4078 04:28:38,399 --> 04:28:44,040 now our next step is to create API key 4079 04:28:48,620 --> 04:28:50,399 [Music] 4080 04:28:50,399 --> 04:28:54,800 strategy we have to install this 4081 04:29:09,279 --> 04:29:12,239 package I'm going to copy the same code 4082 04:29:12,239 --> 04:29:15,920 and create a new file API key strategy 4083 04:29:15,920 --> 04:29:17,000 inside the 4084 04:29:17,000 --> 04:29:20,279 O API Das 4085 04:29:20,279 --> 04:29:22,359 key 4086 04:29:22,359 --> 04:29:24,159 Dash 4087 04:29:24,159 --> 04:29:29,159 strategy. DS strategy. 4088 04:29:29,159 --> 04:29:33,080 DS going to copy the 4089 04:29:41,040 --> 04:29:43,920 code 4090 04:29:43,920 --> 04:29:47,640 we are using the passport strategy and 4091 04:29:47,640 --> 04:29:50,159 we are providing this passport HTTP 4092 04:29:50,159 --> 04:29:53,040 barrier OD 4093 04:29:59,439 --> 04:30:03,399 strategy we have to import or 4094 04:30:03,399 --> 04:30:09,040 provide API key strategy inside the O 4095 04:30:09,040 --> 04:30:13,960 module register it API key 4096 04:30:15,080 --> 04:30:19,119 strategy and we have to validate the 4097 04:30:19,119 --> 04:30:23,439 user on the based on API 4098 04:30:23,439 --> 04:30:26,199 key I can create a new 4099 04:30:26,199 --> 04:30:29,880 method I also need to find user on the 4100 04:30:29,880 --> 04:30:34,119 based on API key let's add a new method 4101 04:30:34,119 --> 04:30:37,119 inside the user 4102 04:30:38,279 --> 04:30:42,040 service now it looks good let's create a 4103 04:30:42,040 --> 04:30:44,119 new controller 4104 04:30:44,119 --> 04:30:47,640 function into the O 4105 04:30:47,640 --> 04:30:51,640 controller it means I'm telling to a or 4106 04:30:51,640 --> 04:30:54,319 nests application I would like to 4107 04:30:54,319 --> 04:30:58,319 validate this route on the based 4108 04:30:58,319 --> 04:31:01,000 on API 4109 04:31:01,000 --> 04:31:04,040 Key njs 4110 04:31:04,040 --> 04:31:08,479 password everything is good to go right 4111 04:31:08,479 --> 04:31:12,439 now we have to test the application 4112 04:31:12,439 --> 04:31:13,590 run the 4113 04:31:13,590 --> 04:31:16,649 [Music] 4114 04:31:22,439 --> 04:31:24,680 application first of all we have to 4115 04:31:24,680 --> 04:31:26,520 create a new 4116 04:31:26,520 --> 04:31:30,239 account I change the email John 13 we 4117 04:31:30,239 --> 04:31:33,439 got the API 4118 04:31:34,159 --> 04:31:38,040 key I'm going to store the API 4119 04:31:38,040 --> 04:31:40,840 key key John 4120 04:31:40,840 --> 04:31:43,840 13 4121 04:31:46,159 --> 04:31:49,920 now we have to provide this API key to 4122 04:31:49,920 --> 04:31:53,040 access the profile 4123 04:31:53,359 --> 04:31:57,479 route access 4124 04:32:00,359 --> 04:32:04,359 profile we have a get end 4125 04:32:04,359 --> 04:32:07,920 point get 4126 04:32:09,800 --> 04:32:13,840 http or/ 4127 04:32:14,600 --> 04:32:18,359 profile authorization 4128 04:32:18,359 --> 04:32:21,320 basic here we have to provide the API 4129 04:32:21,320 --> 04:32:24,469 [Music] 4130 04:32:27,000 --> 04:32:30,460 key it's not a basic we are using this 4131 04:32:30,460 --> 04:32:32,080 [Music] 4132 04:32:32,080 --> 04:32:35,040 scheme barrier 4133 04:32:35,040 --> 04:32:38,080 scheme b e a r e 4134 04:32:38,080 --> 04:32:42,359 r now I got the user back in the 4135 04:32:42,359 --> 04:32:44,680 response and it has authenticated with 4136 04:32:44,680 --> 04:32:47,359 API 4137 04:32:53,199 --> 04:32:55,760 key in this lesson I'm going to teach 4138 04:32:55,760 --> 04:32:58,920 you how to debug nestjs application 4139 04:32:58,920 --> 04:33:01,680 using VSS 4140 04:33:01,680 --> 04:33:05,879 code if you don't have launch. Json file 4141 04:33:05,879 --> 04:33:10,400 in vs code folder you can create 4142 04:33:10,400 --> 04:33:14,118 it let's create the launch. jsn file if 4143 04:33:14,118 --> 04:33:16,718 you go to the 4144 04:33:19,118 --> 04:33:22,199 debug I'm going to create a new folder 4145 04:33:22,199 --> 04:33:23,639 vs 4146 04:33:23,639 --> 04:33:28,438 code and you can have launch. Json file 4147 04:33:28,438 --> 04:33:31,679 let me copy these 4148 04:33:33,400 --> 04:33:37,160 configurations so I have to start the 4149 04:33:37,160 --> 04:33:40,561 application in debug mode nestjs has a 4150 04:33:40,561 --> 04:33:43,879 script in package Chon 4151 04:33:43,879 --> 04:33:46,879 file now you can start the application 4152 04:33:46,879 --> 04:33:51,359 by running npm start Run start 4153 04:33:52,199 --> 04:33:56,080 debug you can see that our debugger has 4154 04:33:56,080 --> 04:33:59,438 started now I want you 4155 04:33:59,438 --> 04:34:03,080 to attach a process when you go to the 4156 04:34:03,080 --> 04:34:07,119 debug now you have to attach 4157 04:34:07,680 --> 04:34:13,719 it you can put break points let me 4158 04:34:13,799 --> 04:34:15,650 to here the O 4159 04:34:15,650 --> 04:34:18,480 [Music] 4160 04:34:18,480 --> 04:34:22,160 controller I'm going to put 4161 04:34:23,759 --> 04:34:28,958 breakpoint let's try to send the API 4162 04:34:35,000 --> 04:34:37,680 request it has stopped at this break 4163 04:34:37,680 --> 04:34:40,639 point so I can see what is inside the 4164 04:34:40,639 --> 04:34:43,320 request 4165 04:34:43,320 --> 04:34:45,359 I have request 4166 04:34:45,359 --> 04:34:47,958 body request 4167 04:34:47,958 --> 04:34:52,080 client request. user you can also watch 4168 04:34:52,080 --> 04:34:55,320 on a specific property the request. 4169 04:34:55,320 --> 04:35:00,438 user you can see that I got the API key 4170 04:35:00,438 --> 04:35:04,240 email enable to fa to fa secret this is 4171 04:35:04,240 --> 04:35:07,118 a great way to debug the application you 4172 04:35:07,118 --> 04:35:10,639 can find errors very easily by using 4173 04:35:10,639 --> 04:35:12,958 debugging if you want to debug the 4174 04:35:12,958 --> 04:35:16,759 complete flow you can do that into the 4175 04:35:16,759 --> 04:35:18,438 app 4176 04:35:18,438 --> 04:35:22,719 strategy what I was API API key strategy 4177 04:35:22,719 --> 04:35:24,160 or API 4178 04:35:24,160 --> 04:35:29,160 strategy yep we have the API key 4179 04:35:30,840 --> 04:35:33,160 strategy I'm going to put break point 4180 04:35:33,160 --> 04:35:36,599 here let me put the break point here and 4181 04:35:36,599 --> 04:35:39,199 I would like to put the break point here 4182 04:35:39,199 --> 04:35:43,799 let's send the request you you can see 4183 04:35:43,799 --> 04:35:49,879 that it has first called this validate 4184 04:35:49,879 --> 04:35:53,080 method it is going to find the user on 4185 04:35:53,080 --> 04:35:57,759 the based on API key it has found the 4186 04:35:57,759 --> 04:36:01,400 user now it is going to return execute 4187 04:36:01,400 --> 04:36:04,320 this line return 4188 04:36:04,480 --> 04:36:06,438 user it 4189 04:36:06,438 --> 04:36:11,680 has now it has added the request. user 4190 04:36:11,680 --> 04:36:17,080 you have returned API user from the API 4191 04:36:17,080 --> 04:36:21,919 key strategy like this one 4192 04:36:21,919 --> 04:36:24,480 nestjs has 4193 04:36:24,480 --> 04:36:28,680 stored user inside the request object 4194 04:36:28,680 --> 04:36:30,719 like request. 4195 04:36:30,719 --> 04:36:33,840 user like this 4196 04:36:33,840 --> 04:36:37,160 one and finally we have deleted it you 4197 04:36:37,160 --> 04:36:39,520 can step over 4198 04:36:39,520 --> 04:36:42,160 it go to the next line 4199 04:36:42,160 --> 04:36:45,320 if you need to step into a specific 4200 04:36:45,320 --> 04:36:48,118 function you have to use this one step 4201 04:36:48,118 --> 04:36:51,799 in if you want to go out from this 4202 04:36:51,799 --> 04:36:55,480 function you can press step 4203 04:37:01,039 --> 04:37:03,639 out in this lesson I'm going to teach 4204 04:37:03,639 --> 04:37:06,639 you the concept of 4205 04:37:06,639 --> 04:37:09,719 migration migrations is like a Version 4206 04:37:09,719 --> 04:37:13,520 Control for your database 4207 04:37:15,118 --> 04:37:19,039 you can manage your database 4208 04:37:19,039 --> 04:37:22,719 changes by using migrations migration is 4209 04:37:22,719 --> 04:37:25,199 a file that contains a set of 4210 04:37:25,199 --> 04:37:28,240 instructions for creating modifying or 4211 04:37:28,240 --> 04:37:31,240 deleting database 4212 04:37:34,958 --> 04:37:39,080 tables whenever you make changes in your 4213 04:37:39,080 --> 04:37:42,759 entity you have to create a new 4214 04:37:42,759 --> 04:37:45,240 migration if you want to go back to the 4215 04:37:45,240 --> 04:37:47,520 previous changes there is a command for 4216 04:37:47,520 --> 04:37:51,199 revert you can use that 4217 04:37:52,080 --> 04:37:55,520 command once you get into the 4218 04:37:55,520 --> 04:37:58,320 production right now we are building our 4219 04:37:58,320 --> 04:38:01,199 application in development environment 4220 04:38:01,199 --> 04:38:03,160 when we push the application into 4221 04:38:03,160 --> 04:38:05,639 production you will set the synchronize 4222 04:38:05,639 --> 04:38:06,680 to 4223 04:38:06,680 --> 04:38:10,000 false so you have to 4224 04:38:10,000 --> 04:38:14,278 manually update the changes to for your 4225 04:38:14,278 --> 04:38:16,561 entities that's why I have set the 4226 04:38:16,561 --> 04:38:19,480 synchronized to 4227 04:38:19,919 --> 04:38:22,799 false in production you will not set the 4228 04:38:22,799 --> 04:38:24,599 synchronized property to 4229 04:38:24,599 --> 04:38:27,680 True migration is just a file with SQL 4230 04:38:27,680 --> 04:38:30,680 queries to update database and apply new 4231 04:38:30,680 --> 04:38:32,958 changes to an existing 4232 04:38:32,958 --> 04:38:35,680 database I got the simple definition of 4233 04:38:35,680 --> 04:38:37,719 migration it's called it's like a 4234 04:38:37,719 --> 04:38:40,958 Version Control for your database you 4235 04:38:40,958 --> 04:38:43,240 will keep keep track of your changes 4236 04:38:43,240 --> 04:38:45,480 toward your 4237 04:38:45,480 --> 04:38:48,278 schema first of all you have to move 4238 04:38:48,278 --> 04:38:50,639 type orm configuration into a separate 4239 04:38:50,639 --> 04:38:54,561 file these are all my type type orm 4240 04:38:54,561 --> 04:38:56,639 configurations now you don't need to 4241 04:38:56,639 --> 04:38:59,639 write your entities 4242 04:38:59,639 --> 04:39:02,759 manually when you have to create a new 4243 04:39:02,759 --> 04:39:05,919 entity you add a new entry for the 4244 04:39:05,919 --> 04:39:09,039 entity now you don't need 4245 04:39:09,039 --> 04:39:12,320 that I have added the regular expression 4246 04:39:12,320 --> 04:39:15,879 for this path we also using the 4247 04:39:15,879 --> 04:39:17,840 JavaScript because we're going to use 4248 04:39:17,840 --> 04:39:20,879 migration I found it's easier to work 4249 04:39:20,879 --> 04:39:22,520 with JavaScript when you are working 4250 04:39:22,520 --> 04:39:24,000 with with 4251 04:39:24,000 --> 04:39:26,240 migrations when you build the project 4252 04:39:26,240 --> 04:39:29,759 nestjs will generate the disc 4253 04:39:29,759 --> 04:39:33,039 folder this disc folder I can provide 4254 04:39:33,039 --> 04:39:35,958 that path this is the JS 4255 04:39:35,958 --> 04:39:38,520 file we're going to provide this type of 4256 04:39:38,520 --> 04:39:40,718 file I have set this synchronized to 4257 04:39:40,718 --> 04:39:42,400 false 4258 04:39:42,400 --> 04:39:44,718 we have created a new data 4259 04:39:44,718 --> 04:39:48,080 source when you write the script to 4260 04:39:48,080 --> 04:39:51,480 migrate in package or Json file you have 4261 04:39:51,480 --> 04:39:53,958 to provide the data source file this 4262 04:39:53,958 --> 04:39:56,040 this data source and this data source 4263 04:39:56,040 --> 04:39:58,760 has a data source object this data 4264 04:39:58,760 --> 04:40:01,120 source 4265 04:40:01,760 --> 04:40:04,000 object step number 4266 04:40:04,000 --> 04:40:07,120 two you have to refactor type or M 4267 04:40:07,120 --> 04:40:09,120 config into app module you got to 4268 04:40:09,120 --> 04:40:11,320 provide data source options this dat 4269 04:40:11,320 --> 04:40:13,240 data source 4270 04:40:13,240 --> 04:40:15,760 options and then you have to write the 4271 04:40:15,760 --> 04:40:20,280 script for type or M generate run and 4272 04:40:20,280 --> 04:40:22,480 revert we're going to use all these 4273 04:40:22,480 --> 04:40:26,718 scripts we will add a new column in the 4274 04:40:26,718 --> 04:40:28,480 user 4275 04:40:28,480 --> 04:40:31,520 entity and then we will run run the 4276 04:40:31,520 --> 04:40:35,240 generate the migration based on this 4277 04:40:35,240 --> 04:40:38,240 column this is the name of the migration 4278 04:40:38,240 --> 04:40:42,120 at user phone where we have St about the 4279 04:40:42,120 --> 04:40:46,040 migration here we have specified the 4280 04:40:46,040 --> 04:40:49,040 path there is a DB folder and inside the 4281 04:40:49,040 --> 04:40:52,760 DB we have migrations 4282 04:40:55,718 --> 04:40:58,520 directory and finally you will run the 4283 04:40:58,520 --> 04:41:00,920 migration when after creating the file 4284 04:41:00,920 --> 04:41:03,480 you have to run the migration if you 4285 04:41:03,480 --> 04:41:05,400 want you can implement this complete 4286 04:41:05,400 --> 04:41:09,480 steps or you can follow 4287 04:41:10,240 --> 04:41:12,480 me 4288 04:41:12,480 --> 04:41:14,638 I'm going to create a new file inside 4289 04:41:14,638 --> 04:41:18,520 the DB create a new folder inside the 4290 04:41:18,520 --> 04:41:19,360 root 4291 04:41:19,360 --> 04:41:21,040 directory 4292 04:41:21,040 --> 04:41:23,600 DB data 4293 04:41:23,600 --> 04:41:28,680 source. TS I'm going to paste this 4294 04:41:28,680 --> 04:41:34,798 code my database is which is Spotify 4295 04:41:36,400 --> 04:41:40,240 clone Spotify clone 4296 04:41:40,240 --> 04:41:43,240 2 4297 04:41:47,600 --> 04:41:51,320 so I have to delete 4298 04:41:51,600 --> 04:41:54,000 everything here we have to 4299 04:41:54,000 --> 04:41:57,520 provide a data source 4300 04:41:57,520 --> 04:42:01,440 options this options 4301 04:42:03,200 --> 04:42:05,840 object I have completed this step number 4302 04:42:05,840 --> 04:42:09,718 two now we need to add couple of scripts 4303 04:42:09,718 --> 04:42:12,160 there is no logic logic here 4304 04:42:12,160 --> 04:42:14,560 specifically you can copy the 4305 04:42:14,560 --> 04:42:18,200 scripts first of all please build the 4306 04:42:18,200 --> 04:42:24,080 project generate the JavaScript files 4307 04:42:24,600 --> 04:42:28,600 then and then we are using the type orm 4308 04:42:28,600 --> 04:42:30,480 npx and 4309 04:42:30,480 --> 04:42:34,440 dashd I'm telling to type orm I have 4310 04:42:34,440 --> 04:42:38,638 stored my database configuration inside 4311 04:42:38,638 --> 04:42:39,920 this 4312 04:42:39,920 --> 04:42:43,638 file if if you have to go back or revert 4313 04:42:43,638 --> 04:42:46,440 the changes you will run this 4314 04:42:46,440 --> 04:42:50,200 command I'm going to copy this script 4315 04:42:50,200 --> 04:42:54,760 and paste it here inside the 4316 04:42:56,360 --> 04:43:01,280 scripts now let's try to add a new 4317 04:43:05,080 --> 04:43:08,400 column user. 4318 04:43:08,400 --> 04:43:10,958 entity I'm going to add a new column 4319 04:43:10,958 --> 04:43:13,120 here 4320 04:43:15,560 --> 04:43:18,560 here type A 4321 04:43:18,560 --> 04:43:23,520 String now finally we have to run the 4322 04:43:24,760 --> 04:43:28,600 migrations npm run migration generate 4323 04:43:28,600 --> 04:43:33,798 I'm using this script here you are 4324 04:43:33,798 --> 04:43:36,560 specifying I have I want to store 4325 04:43:36,560 --> 04:43:40,320 migration in this file migrations and 4326 04:43:40,320 --> 04:43:43,040 this is the name of the file it will 4327 04:43:43,040 --> 04:43:45,840 also add the 4328 04:43:49,240 --> 04:43:52,360 timestamp first it is going to build the 4329 04:43:52,360 --> 04:43:57,200 project then it is going to execute this 4330 04:44:01,160 --> 04:44:03,958 command meanwhile I have to start it 4331 04:44:03,958 --> 04:44:08,120 from the scratch I don't want existing 4332 04:44:08,120 --> 04:44:11,600 database so I would like to delete 4333 04:44:11,600 --> 04:44:14,878 Del my 4334 04:44:14,878 --> 04:44:18,480 database I'm also going to delete this 4335 04:44:18,480 --> 04:44:20,760 one because we have set this 4336 04:44:20,760 --> 04:44:24,840 synchronized to false we have to add 4337 04:44:24,840 --> 04:44:30,000 changes manually that's why we are using 4338 04:44:33,878 --> 04:44:37,040 migrations and new file has created 4339 04:44:37,040 --> 04:44:39,520 inside the DB inside the 4340 04:44:39,520 --> 04:44:42,000 migrations we have this file this is a 4341 04:44:42,000 --> 04:44:44,798 Tim stamp and the name of the file add 4342 04:44:44,798 --> 04:44:47,200 user 4343 04:44:54,480 --> 04:44:59,560 phone. so we have to create a new 4344 04:45:01,480 --> 04:45:06,600 database I can say Spotify 4345 04:45:08,400 --> 04:45:11,920 clone now I want to 4346 04:45:11,920 --> 04:45:15,878 add all this stuff 4347 04:45:21,200 --> 04:45:25,480 like inside the data source Spotify 4348 04:45:25,480 --> 04:45:29,240 clone let's generate the migrations I 4349 04:45:29,240 --> 04:45:31,718 can say 4350 04:45:31,718 --> 04:45:36,560 that my migrations you can choose any 4351 04:45:36,560 --> 04:45:39,920 name or in 4352 04:45:39,920 --> 04:45:41,878 it 4353 04:45:41,878 --> 04:45:45,718 it is going to take all these entities 4354 04:45:45,718 --> 04:45:49,160 and creating creating the SQL 4355 04:45:49,160 --> 04:45:53,040 files SQL queries for all these entities 4356 04:45:53,040 --> 04:45:55,638 because right now our database is 4357 04:45:55,638 --> 04:45:58,638 empty 4358 04:46:01,160 --> 04:46:04,878 in we don't have 4359 04:46:09,920 --> 04:46:12,920 tables 4360 04:46:24,000 --> 04:46:27,680 now you can see my 4361 04:46:31,480 --> 04:46:33,920 migrations if you check the database you 4362 04:46:33,920 --> 04:46:37,440 will not see any table because we did 4363 04:46:37,440 --> 04:46:40,760 not run the migration yet now we going 4364 04:46:40,760 --> 04:46:43,440 to run the 4365 04:46:50,480 --> 04:46:54,920 migration npm run 4366 04:46:57,080 --> 04:47:00,080 migration 4367 04:47:07,080 --> 04:47:12,160 run now it is going to execute these 4368 04:47:12,160 --> 04:47:13,920 queries 4369 04:47:13,920 --> 04:47:17,400 these creating all the tables and all 4370 04:47:17,400 --> 04:47:19,638 the 4371 04:47:34,160 --> 04:47:37,958 relationships we have the 4372 04:47:39,840 --> 04:47:42,840 migrations 4373 04:47:49,718 --> 04:47:54,000 there is no record inside the 4374 04:47:55,840 --> 04:47:59,000 migrations I'm going to 4375 04:47:59,000 --> 04:48:01,958 delete this 4376 04:48:01,958 --> 04:48:06,200 migrations and this migration file 4377 04:48:06,200 --> 04:48:09,760 let's create it from 4378 04:48:09,760 --> 04:48:12,760 scratch 4379 04:48:18,400 --> 04:48:21,040 it has created all the migration now we 4380 04:48:21,040 --> 04:48:23,600 have to run 4381 04:48:27,080 --> 04:48:31,120 it everything is good right 4382 04:48:31,120 --> 04:48:36,160 now it has executed the queries and now 4383 04:48:36,160 --> 04:48:40,000 if I refresh my tables you will see all 4384 04:48:40,000 --> 04:48:44,360 the tables we have songs users but if I 4385 04:48:44,360 --> 04:48:46,920 go to the 4386 04:48:48,440 --> 04:48:52,480 users I will have font property I don't 4387 04:48:52,480 --> 04:48:55,320 want font 4388 04:48:56,958 --> 04:49:01,520 property let's edit a user entity I'm 4389 04:49:01,520 --> 04:49:03,240 going to remove 4390 04:49:03,240 --> 04:49:06,840 phone now let's generate the 4391 04:49:06,840 --> 04:49:12,718 migration I can say removed phone 4392 04:49:18,680 --> 04:49:21,718 you can see that it has created the 4393 04:49:21,718 --> 04:49:24,840 migration file removed phone it is going 4394 04:49:24,840 --> 04:49:26,558 to drop this 4395 04:49:26,558 --> 04:49:30,040 column now we need to run this 4396 04:49:30,040 --> 04:49:35,120 migration to update changes in the 4397 04:49:39,638 --> 04:49:41,760 database 4398 04:49:41,760 --> 04:49:43,920 let's try to check the 4399 04:49:43,920 --> 04:49:47,240 column there is no phone column here 4400 04:49:47,240 --> 04:49:50,878 this is how the migrations 4401 04:49:50,878 --> 04:49:53,520 work this lesson I'm going to teach you 4402 04:49:53,520 --> 04:49:56,360 the concept of data 4403 04:49:56,360 --> 04:49:59,240 seeding data seting is a process of 4404 04:49:59,240 --> 04:50:01,638 populating a database with an initial 4405 04:50:01,638 --> 04:50:02,718 set of 4406 04:50:02,718 --> 04:50:06,400 data when you create a new 4407 04:50:06,400 --> 04:50:09,638 column inside 4408 04:50:09,638 --> 04:50:12,638 here 4409 04:50:14,040 --> 04:50:17,840 when you create a new column in the user 4410 04:50:17,840 --> 04:50:21,200 entity let's say I would like to add a 4411 04:50:21,200 --> 04:50:23,520 new 4412 04:50:23,840 --> 04:50:27,558 column and here I can say this is this 4413 04:50:27,558 --> 04:50:30,280 could be the phone 4414 04:50:30,280 --> 04:50:32,240 number 4415 04:50:32,240 --> 04:50:36,200 but when you run the 4416 04:50:36,200 --> 04:50:39,120 migrations the phone number will be 4417 04:50:39,120 --> 04:50:41,360 empty in the 4418 04:50:41,360 --> 04:50:45,040 database so you can load your initial 4419 04:50:45,040 --> 04:50:49,040 data by running the 4420 04:50:50,320 --> 04:50:53,440 seeds applying CED data to database 4421 04:50:53,440 --> 04:50:56,240 refers to the process of inserting 4422 04:50:56,240 --> 04:50:59,718 initial data into the database usually 4423 04:50:59,718 --> 04:51:02,200 when the database is first created let 4424 04:51:02,200 --> 04:51:06,080 me show you the demo of 4425 04:51:09,558 --> 04:51:11,840 cing 4426 04:51:11,840 --> 04:51:14,040 you are going to learn how to create the 4427 04:51:14,040 --> 04:51:17,440 seed service and we have a seed method 4428 04:51:17,440 --> 04:51:19,160 inside the seed 4429 04:51:19,160 --> 04:51:21,280 service we 4430 04:51:21,280 --> 04:51:22,798 are 4431 04:51:22,798 --> 04:51:27,920 running type orm queries by using 4432 04:51:27,920 --> 04:51:30,718 transaction here we have placed all my C 4433 04:51:30,718 --> 04:51:34,040 data when my application bootstrap 4434 04:51:34,040 --> 04:51:39,520 loads it will create a new user a fake 4435 04:51:39,520 --> 04:51:43,840 user and it will save into the database 4436 04:51:43,840 --> 04:51:47,040 and then it will create the artist and 4437 04:51:47,040 --> 04:51:50,360 save artist into the user into the 4438 04:51:50,360 --> 04:51:53,600 database we also have a c data for 4439 04:51:53,600 --> 04:51:55,558 playlist first of all we're going to 4440 04:51:55,558 --> 04:51:57,840 create a new user then we're going to 4441 04:51:57,840 --> 04:51:59,920 create a new playlist and we have 4442 04:51:59,920 --> 04:52:02,840 assigned a relation between playlist and 4443 04:52:02,840 --> 04:52:04,958 the user so we're going to save couple 4444 04:52:04,958 --> 04:52:05,718 of 4445 04:52:05,718 --> 04:52:09,160 data when you run the application when 4446 04:52:09,160 --> 04:52:11,120 application will be completed 4447 04:52:11,120 --> 04:52:14,600 you can verify from your 4448 04:52:14,600 --> 04:52:18,200 database let me show 4449 04:52:27,200 --> 04:52:31,160 you in your users table you will see a 4450 04:52:31,160 --> 04:52:34,440 new user will be created so these are 4451 04:52:34,440 --> 04:52:36,600 all the 4452 04:52:36,600 --> 04:52:39,600 users these are all the users I created 4453 04:52:39,600 --> 04:52:42,798 with fake data by using the 4454 04:52:42,798 --> 04:52:45,280 seaing I also have a 4455 04:52:45,280 --> 04:52:48,520 playlist it is going to pick the random 4456 04:52:48,520 --> 04:52:51,360 name for a 4457 04:52:53,520 --> 04:52:55,600 playlist Rap 4458 04:52:55,600 --> 04:52:58,440 playlist World playlist or country 4459 04:52:58,440 --> 04:53:04,040 playlist I've also have a seed for 4460 04:53:06,440 --> 04:53:09,680 artist you can see that now my 4461 04:53:09,680 --> 04:53:12,000 application is is running 4462 04:53:12,000 --> 04:53:16,000 now when you look at the 4463 04:53:18,240 --> 04:53:22,080 users you will see another users couple 4464 04:53:22,080 --> 04:53:24,400 of another 4465 04:53:24,400 --> 04:53:27,440 users if you look at the playlist you 4466 04:53:27,440 --> 04:53:29,718 will find one more 4467 04:53:29,718 --> 04:53:34,160 playlist you can also look at the 4468 04:53:34,680 --> 04:53:38,320 artist you will have more artist this is 4469 04:53:38,320 --> 04:53:43,120 how you can increase your development 4470 04:53:43,120 --> 04:53:45,120 workflow or testing 4471 04:53:45,120 --> 04:53:48,480 workflow now I want you to implement it 4472 04:53:48,480 --> 04:53:50,280 by looking at the documentation I 4473 04:53:50,280 --> 04:53:51,760 created for 4474 04:53:51,760 --> 04:53:54,760 you 4475 04:53:59,958 --> 04:54:03,760 here I don't want a new 4476 04:54:06,360 --> 04:54:09,360 column first of all we have to 4477 04:54:09,360 --> 04:54:12,680 install another package to generate the 4478 04:54:12,680 --> 04:54:15,160 fake 4479 04:54:22,320 --> 04:54:26,920 data and next step we have to create a 4480 04:54:26,920 --> 04:54:30,878 seeds folder inside the 4481 04:54:30,878 --> 04:54:32,718 DB 4482 04:54:32,718 --> 04:54:37,920 seeds I can data seed. TS 4483 04:54:37,920 --> 04:54:41,120 file I have created a new file while 4484 04:54:41,120 --> 04:54:42,280 here we're going 4485 04:54:42,280 --> 04:54:47,240 to do initial 4486 04:54:48,080 --> 04:54:50,600 seaing you can create a separate 4487 04:54:50,600 --> 04:54:53,760 function for each entity like you can 4488 04:54:53,760 --> 04:54:56,760 create a separate function for seed 4489 04:54:56,760 --> 04:55:01,320 songs I did not create this seed songs 4490 04:55:01,320 --> 04:55:05,200 method here you can create 4491 04:55:05,200 --> 04:55:09,080 that if you want to look up more 4492 04:55:09,080 --> 04:55:12,400 documentation of of Faker package you 4493 04:55:12,400 --> 04:55:15,638 can find from your from the official 4494 04:55:15,638 --> 04:55:18,360 documentation 4495 04:55:18,360 --> 04:55:22,920 page it is giving me the 4496 04:55:29,040 --> 04:55:32,638 error entities Source 4497 04:55:32,638 --> 04:55:37,040 playlist here I can find the 4498 04:55:39,240 --> 04:55:42,240 entity 4499 04:55:43,840 --> 04:55:47,080 now our next step is to create a new 4500 04:55:47,080 --> 04:55:48,480 seed 4501 04:55:48,480 --> 04:55:51,240 module I'm going to use type orm to 4502 04:55:51,240 --> 04:55:54,240 create a new seed 4503 04:55:54,240 --> 04:55:57,440 module and 4504 04:56:03,040 --> 04:56:05,200 then what we are doing here we are 4505 04:56:05,200 --> 04:56:09,638 generating the encrypted password I use 4506 04:56:09,638 --> 04:56:12,558 the same password for each user here we 4507 04:56:12,558 --> 04:56:16,320 are creating the user I got uu ID from 4508 04:56:16,320 --> 04:56:21,760 API key and I got repository from the 4509 04:56:21,798 --> 04:56:25,160 manager we will call the seed data in 4510 04:56:25,160 --> 04:56:28,160 main.ts 4511 04:56:39,240 --> 04:56:43,320 file seed module has 4512 04:56:43,320 --> 04:56:47,000 created we also need a seed service what 4513 04:56:47,000 --> 04:56:51,360 I can do I'm going to copy this 4514 04:56:52,040 --> 04:56:55,638 code and create a new service inside the 4515 04:56:55,638 --> 04:56:57,958 seed 4516 04:56:57,958 --> 04:57:00,280 module see do 4517 04:57:00,280 --> 04:57:03,080 service if you want you can use CLI to 4518 04:57:03,080 --> 04:57:06,958 generate service we also have to import 4519 04:57:06,958 --> 04:57:09,160 or register as a 4520 04:57:09,160 --> 04:57:13,200 provider seed 4521 04:57:19,958 --> 04:57:24,920 service cannot find um oh it should be 4522 04:57:24,920 --> 04:57:27,958 SE data seat should 4523 04:57:27,958 --> 04:57:34,360 be I can say this seat data seat 4524 04:57:37,558 --> 04:57:42,120 data and it should be gone 4525 04:57:48,400 --> 04:57:51,600 we are making connection with our 4526 04:57:51,600 --> 04:57:56,360 database here I got the manager from the 4527 04:57:56,360 --> 04:57:59,878 quy manager and I called this C data and 4528 04:57:59,878 --> 04:58:01,920 finally we are we have started the 4529 04:58:01,920 --> 04:58:05,120 transaction if transaction if any query 4530 04:58:05,120 --> 04:58:09,120 has failed it is going to roll back the 4531 04:58:09,120 --> 04:58:11,080 transaction 4532 04:58:11,080 --> 04:58:15,360 so our final step you can read this text 4533 04:58:15,360 --> 04:58:17,160 if you want to learn more about what is 4534 04:58:17,160 --> 04:58:20,638 cury Runner what is 4535 04:58:22,520 --> 04:58:25,718 transaction here you have to boot you 4536 04:58:25,718 --> 04:58:31,320 have to call the seed method here 4537 04:58:31,320 --> 04:58:35,798 whenever you want to save a data in the 4538 04:58:35,798 --> 04:58:39,718 database if you don't want to save a new 4539 04:58:39,718 --> 04:58:43,958 fake data you can just disable these two 4540 04:58:43,958 --> 04:58:46,798 lines but let me show you the demo I'm 4541 04:58:46,798 --> 04:58:49,798 going to start the 4542 04:58:52,400 --> 04:58:55,878 application it is going to run these 4543 04:58:55,878 --> 04:58:58,878 methods seed user it is going to create 4544 04:58:58,878 --> 04:59:02,200 a new fake user seed artist and Seed 4545 04:59:02,200 --> 04:59:04,600 playlist let me show you the current 4546 04:59:04,600 --> 04:59:07,680 look of my 4547 04:59:07,680 --> 04:59:12,320 schema I'm using spotify clone on 4548 04:59:13,520 --> 04:59:17,360 database Let me refresh my 4549 04:59:17,480 --> 04:59:22,080 tables uh I would like to see all the 4550 04:59:27,480 --> 04:59:31,280 users there is nothing here I don't have 4551 04:59:31,280 --> 04:59:33,520 any 4552 04:59:34,200 --> 04:59:38,280 user you can create many users if you 4553 04:59:38,280 --> 04:59:40,558 want you can use fake 4554 04:59:40,558 --> 04:59:45,760 package to generate 20 15 50 55 records 4555 04:59:45,760 --> 04:59:48,718 and you can and then you can provide 4556 04:59:48,718 --> 04:59:51,000 into the repository user repository it 4557 04:59:51,000 --> 04:59:54,280 will create all these records but I use 4558 04:59:54,280 --> 04:59:57,320 a single record I just want to show you 4559 04:59:57,320 --> 05:00:00,280 the process you can manipulate with your 4560 05:00:00,280 --> 05:00:01,920 own use 4561 05:00:01,920 --> 05:00:05,638 case one more thing I could not find the 4562 05:00:05,638 --> 05:00:08,558 official package or official 4563 05:00:08,558 --> 05:00:12,360 documentation or in typ orm seeding I 4564 05:00:12,360 --> 05:00:16,600 got I have implemented my own 4565 05:00:16,600 --> 05:00:20,840 strategy now my application is 4566 05:00:20,958 --> 05:00:25,240 running you can see I have couple of new 4567 05:00:25,240 --> 05:00:29,520 users you can see I have couple of new 4568 05:00:29,520 --> 05:00:34,240 artist I think I will have only single 4569 05:00:34,240 --> 05:00:38,920 artist yes but if you look at the 4570 05:00:38,920 --> 05:00:43,160 users it has picked a random first name 4571 05:00:43,160 --> 05:00:46,798 random random last name random last name 4572 05:00:46,798 --> 05:00:50,280 and the email and the password and the 4573 05:00:50,280 --> 05:00:53,718 API key it has generated one more thing 4574 05:00:53,718 --> 05:00:56,120 if you want to look at the playlist I 4575 05:00:56,120 --> 05:01:00,120 will also have a one 4576 05:01:02,040 --> 05:01:06,520 playlist electronic music 4577 05:01:06,638 --> 05:01:11,558 song that's how the seeding work 4578 05:01:11,558 --> 05:01:15,638 works whenever you create a new entity I 4579 05:01:15,638 --> 05:01:19,798 would recommend you to create a seed 4580 05:01:19,798 --> 05:01:22,798 function whenever you run the 4581 05:01:22,798 --> 05:01:26,040 application it is it is going to create 4582 05:01:26,040 --> 05:01:30,440 a new data but I'm going to comment this 4583 05:01:36,520 --> 05:01:38,840 code you can 4584 05:01:38,840 --> 05:01:41,760 enable 4585 05:01:41,760 --> 05:01:44,000 the 4586 05:01:44,200 --> 05:01:46,638 seaing 4587 05:01:46,638 --> 05:01:49,760 here if you want to make it more Super 4588 05:01:49,760 --> 05:01:52,400 productive you can create a console 4589 05:01:52,400 --> 05:01:53,958 command in 4590 05:01:53,958 --> 05:01:58,878 njs I'll teach you in the advanced 4591 05:02:02,040 --> 05:02:04,480 Concept in this lesson I'm going to 4592 05:02:04,480 --> 05:02:06,958 teach you the concept of 4593 05:02:06,958 --> 05:02:09,040 configurations we're going to create 4594 05:02:09,040 --> 05:02:11,878 custom configur ation file with the help 4595 05:02:11,878 --> 05:02:14,878 of nestjs 4596 05:02:15,760 --> 05:02:19,760 config if you see our main. file I have 4597 05:02:19,760 --> 05:02:22,280 mentioned the port number 4598 05:02:22,280 --> 05:02:24,920 manually if you want to have a separate 4599 05:02:24,920 --> 05:02:27,558 port for development project or for 4600 05:02:27,558 --> 05:02:30,718 production environment you can specify 4601 05:02:30,718 --> 05:02:33,160 in the environment variable we don't 4602 05:02:33,160 --> 05:02:35,760 have any EnV file in if you have worked 4603 05:02:35,760 --> 05:02:36,718 with 4604 05:02:36,718 --> 05:02:40,520 nodejs you will have separate EnV 4605 05:02:40,520 --> 05:02:42,878 environment file. EnV file for 4606 05:02:42,878 --> 05:02:45,080 development and you you will have 4607 05:02:45,080 --> 05:02:47,760 separate. EnV file for 4608 05:02:47,760 --> 05:02:51,480 production one more thing if you look at 4609 05:02:51,480 --> 05:02:55,000 our type orm 4610 05:02:59,480 --> 05:03:03,080 configurations here we have type orm 4611 05:03:03,080 --> 05:03:05,840 configurations I have specified a 4612 05:03:05,840 --> 05:03:07,280 username 4613 05:03:07,280 --> 05:03:11,200 manually password manually database 4614 05:03:11,200 --> 05:03:15,080 manually I also specify the host 4615 05:03:15,080 --> 05:03:19,240 manually what if you want a different 4616 05:03:19,240 --> 05:03:21,920 environment variables for production 4617 05:03:21,920 --> 05:03:23,680 let's say you want to deply the project 4618 05:03:23,680 --> 05:03:27,798 to AWS or Hoku you need a separate en 4619 05:03:27,798 --> 05:03:30,200 andv environment variables for 4620 05:03:30,200 --> 05:03:32,520 production level but we did not 4621 05:03:32,520 --> 05:03:37,000 Implement that logic now we need to do 4622 05:03:38,400 --> 05:03:41,798 it if want to read what is configuration 4623 05:03:41,798 --> 05:03:45,440 you can read this complete text I have 4624 05:03:45,440 --> 05:03:47,558 explained 4625 05:03:47,558 --> 05:03:50,320 configurations you may need a database 4626 05:03:50,320 --> 05:03:54,040 setting for in for development and 4627 05:03:54,040 --> 05:03:57,280 production you may have different API 4628 05:03:57,280 --> 05:04:01,920 keys for production and API keys for 4629 05:04:02,360 --> 05:04:04,958 development you can have multiple 4630 05:04:04,958 --> 05:04:07,558 environment files for production 4631 05:04:07,558 --> 05:04:11,718 production and testing 4632 05:04:12,480 --> 05:04:16,360 nestjs provide provide config module 4633 05:04:16,360 --> 05:04:19,280 we're going to use this package I'm 4634 05:04:19,280 --> 05:04:21,120 going to copy 4635 05:04:21,120 --> 05:04:24,080 it and I'm going to paste in my 4636 05:04:24,080 --> 05:04:26,958 dependency section or if you want to 4637 05:04:26,958 --> 05:04:29,920 install manually you can do that I have 4638 05:04:29,920 --> 05:04:32,840 to do npm 4639 05:04:32,958 --> 05:04:35,920 install in The Next Step you have to 4640 05:04:35,920 --> 05:04:40,040 import config module in the app module 4641 05:04:40,040 --> 05:04:43,240 this config module will have config 4642 05:04:43,240 --> 05:04:45,760 service and we're going to use the 4643 05:04:45,760 --> 05:04:48,520 config service to access the environment 4644 05:04:48,520 --> 05:04:50,958 variables let me show you how can we do 4645 05:04:50,958 --> 05:04:53,240 it package has 4646 05:04:53,240 --> 05:04:56,638 installed now we need to import config 4647 05:04:56,638 --> 05:05:00,280 module into the app 4648 05:05:02,360 --> 05:05:07,240 module geted of these extra 4649 05:05:08,638 --> 05:05:11,638 dependencies 4650 05:05:16,240 --> 05:05:19,200 we have to import it here let's import 4651 05:05:19,200 --> 05:05:21,718 config 4652 05:05:26,638 --> 05:05:29,550 module so here we have config 4653 05:05:29,550 --> 05:05:32,640 [Music] 4654 05:05:33,958 --> 05:05:38,600 module after importing config module you 4655 05:05:38,600 --> 05:05:42,280 have to provide the path of your EnV 4656 05:05:42,280 --> 05:05:46,000 file here you have to provide the 4657 05:05:46,000 --> 05:05:49,920 path we don't have any EnV file go to 4658 05:05:49,920 --> 05:05:53,360 your root directory and create two files 4659 05:05:53,360 --> 05:05:58,558 the first one for env. 4660 05:05:59,760 --> 05:06:03,160 development for production 4661 05:06:03,160 --> 05:06:07,480 level I can say en EnV for 4662 05:06:07,480 --> 05:06:10,040 production this one is for development 4663 05:06:10,040 --> 05:06:12,920 and this one is for 4664 05:06:13,718 --> 05:06:16,000 production if you want to have a 4665 05:06:16,000 --> 05:06:19,320 separate port for different environment 4666 05:06:19,320 --> 05:06:21,200 you can do that but I'm going to keep 4667 05:06:21,200 --> 05:06:22,080 with 4668 05:06:22,080 --> 05:06:26,638 3,000 for production you can place it 4669 05:06:26,638 --> 05:06:30,200 here so we have created the EnV file so 4670 05:06:30,200 --> 05:06:33,760 how can you load this EnV file we have 4671 05:06:33,760 --> 05:06:36,200 to set the path in the app 4672 05:06:36,200 --> 05:06:38,878 module here I have to provide the 4673 05:06:38,878 --> 05:06:41,638 options configurations I can say EnV 4674 05:06:41,638 --> 05:06:45,000 file path it can access the array the 4675 05:06:45,000 --> 05:06:49,400 first one is do env. 4676 05:06:52,200 --> 05:06:55,040 development and do 4677 05:06:55,040 --> 05:06:58,000 EnV do 4678 05:06:58,000 --> 05:07:00,200 production one more thing I would like 4679 05:07:00,200 --> 05:07:03,558 to mention we have used the config 4680 05:07:03,558 --> 05:07:06,840 module inside the config module config 4681 05:07:06,840 --> 05:07:10,760 module is using do EnV 4682 05:07:10,760 --> 05:07:15,480 package if you Google the 4683 05:07:17,200 --> 05:07:20,680 envv this package is used to define 4684 05:07:20,680 --> 05:07:22,878 environment variables for different 4685 05:07:22,878 --> 05:07:25,000 environments for development and per for 4686 05:07:25,000 --> 05:07:29,200 production but by default nestjs config 4687 05:07:29,200 --> 05:07:32,558 package is using the 4688 05:07:38,440 --> 05:07:44,000 EnV we have used this load ENB 4689 05:07:49,760 --> 05:07:53,680 property so what you have to make it 4690 05:07:53,680 --> 05:07:56,718 Global let's say I would like to import 4691 05:07:56,718 --> 05:08:01,000 EnV module into my Au module into the 4692 05:08:01,000 --> 05:08:03,840 artist module let's say I want to use 4693 05:08:03,840 --> 05:08:10,040 API keys from env. development file to 4694 05:08:10,040 --> 05:08:13,400 artist module you don't need to import 4695 05:08:13,400 --> 05:08:16,120 config module you can use config service 4696 05:08:16,120 --> 05:08:19,600 automatically but if you use if you make 4697 05:08:19,600 --> 05:08:21,878 a 4698 05:08:25,558 --> 05:08:27,958 global when you want to use config 4699 05:08:27,958 --> 05:08:31,280 module in other modules you will need to 4700 05:08:31,280 --> 05:08:35,320 import it as it standard with any Nest 4701 05:08:35,320 --> 05:08:37,878 module or you can make it Global I have 4702 05:08:37,878 --> 05:08:41,400 set it to Global 4703 05:08:41,520 --> 05:08:43,798 so I can use config service without 4704 05:08:43,798 --> 05:08:46,000 importing the config 4705 05:08:46,000 --> 05:08:49,160 module now we have to create the custom 4706 05:08:49,160 --> 05:08:52,000 configuration 4707 05:08:52,680 --> 05:08:55,840 file you have to create a new folder 4708 05:08:55,840 --> 05:08:59,080 inside the source folder I can say 4709 05:08:59,080 --> 05:09:03,120 config let's create the 4710 05:09:06,840 --> 05:09:09,040 configurations here you have to place 4711 05:09:09,040 --> 05:09:12,798 all your 4712 05:09:13,840 --> 05:09:16,400 configuration you have to define 4713 05:09:16,400 --> 05:09:19,638 configuration inside the EnV 4714 05:09:19,638 --> 05:09:24,520 file and this configuration dots file is 4715 05:09:24,520 --> 05:09:29,120 is going to fetch or add 4716 05:09:29,120 --> 05:09:32,360 validations for environment variables 4717 05:09:32,360 --> 05:09:33,760 inside the 4718 05:09:33,760 --> 05:09:36,878 configuration so config module is going 4719 05:09:36,878 --> 05:09:41,760 to use this configuration .s file it is 4720 05:09:41,760 --> 05:09:45,080 going to to extract all these values 4721 05:09:45,080 --> 05:09:48,320 from config does let me show you 4722 05:09:48,320 --> 05:09:51,718 how we have to load our configuration 4723 05:09:51,718 --> 05:09:55,798 file custom configuration file I can say 4724 05:09:55,798 --> 05:09:58,920 load you can have multiple configuration 4725 05:09:58,920 --> 05:10:01,280 file you can have different 4726 05:10:01,280 --> 05:10:04,120 configuration file for database you can 4727 05:10:04,120 --> 05:10:06,200 have different configuration file for 4728 05:10:06,200 --> 05:10:09,600 app settings or user profile settings 4729 05:10:09,600 --> 05:10:12,000 something like that but I'm going to 4730 05:10:12,000 --> 05:10:14,600 keep it simple let's use a simple 4731 05:10:14,600 --> 05:10:16,280 configuration 4732 05:10:16,280 --> 05:10:19,718 file we have to import the 4733 05:10:19,718 --> 05:10:23,240 configuration now our next 4734 05:10:23,240 --> 05:10:27,200 step is to test the EnV 4735 05:10:27,200 --> 05:10:30,520 variable we have created the port and we 4736 05:10:30,520 --> 05:10:34,798 have defined or getting the port 4737 05:10:34,798 --> 05:10:39,600 from uhv and here now let's say if you 4738 05:10:39,600 --> 05:10:42,400 want to access this port variable I'm 4739 05:10:42,400 --> 05:10:45,680 going to do it for testing purpose don't 4740 05:10:45,680 --> 05:10:47,440 do it in 4741 05:10:47,440 --> 05:10:51,200 production inside the O service let's 4742 05:10:51,200 --> 05:10:54,798 say I can define a new Vari new function 4743 05:10:54,798 --> 05:10:57,200 get EnV 4744 05:10:57,200 --> 05:11:01,680 variable you can call any function 4745 05:11:03,558 --> 05:11:06,878 here here how can you use 4746 05:11:06,878 --> 05:11:09,920 the configuration dots file while how 4747 05:11:09,920 --> 05:11:13,240 can you get the this 4748 05:11:13,240 --> 05:11:16,600 port into your a 4749 05:11:16,600 --> 05:11:19,320 service like we have to inject the 4750 05:11:19,320 --> 05:11:22,440 config service we have to inject the con 4751 05:11:22,440 --> 05:11:24,878 config 4752 05:11:26,440 --> 05:11:29,958 service config 4753 05:11:30,000 --> 05:11:33,240 service remember I did not import the 4754 05:11:33,240 --> 05:11:37,558 config module into the Au module because 4755 05:11:37,558 --> 05:11:39,480 we have set it to Global in the app 4756 05:11:39,480 --> 05:11:43,080 module so we can use config 4757 05:11:45,200 --> 05:11:48,878 service config service. 4758 05:11:48,878 --> 05:11:53,000 get pass it into as a integer you have 4759 05:11:53,000 --> 05:11:56,718 to provide the name of your key which is 4760 05:11:56,718 --> 05:11:59,920 Port when you open the configuration. ES 4761 05:11:59,920 --> 05:12:03,440 file we have specified the key which is 4762 05:12:03,440 --> 05:12:06,320 Port so how can you use this function 4763 05:12:06,320 --> 05:12:10,878 let's say I'm going to create 4764 05:12:10,878 --> 05:12:13,878 a controller function for testing don't 4765 05:12:13,878 --> 05:12:15,878 do it in production I just want to show 4766 05:12:15,878 --> 05:12:20,360 you the demo I can say 4767 05:12:20,360 --> 05:12:23,760 test get 4768 05:12:24,160 --> 05:12:26,840 EnV or you can say 4769 05:12:26,840 --> 05:12:30,440 test EnV 4770 05:12:31,240 --> 05:12:34,120 variable it is going to return this do 4771 05:12:34,120 --> 05:12:34,958 out 4772 05:12:34,958 --> 05:12:37,760 service calling the get EnV variable 4773 05:12:37,760 --> 05:12:42,440 that's it do not do any fancy stuff here 4774 05:12:42,440 --> 05:12:45,000 let's start the application and we have 4775 05:12:45,000 --> 05:12:48,600 to test this 4776 05:13:02,280 --> 05:13:05,120 route now we need to test 4777 05:13:05,120 --> 05:13:09,120 it I have to send the request to this 4778 05:13:09,120 --> 05:13:11,638 URL this 4779 05:13:11,638 --> 05:13:15,360 endpoint we have to send the get 4780 05:13:15,360 --> 05:13:19,840 request can say test 4781 05:13:35,080 --> 05:13:41,040 EnV 3000 sl/ test 4782 05:13:53,798 --> 05:13:56,840 amazing I got the Port Port value which 4783 05:13:56,840 --> 05:14:01,160 is 3,000 it's working fine 4784 05:14:01,680 --> 05:14:06,360 now now if you check the main.ts 4785 05:14:06,360 --> 05:14:10,400 file we have defined the port into 4786 05:14:10,400 --> 05:14:13,120 configuration we are using the port 4787 05:14:13,120 --> 05:14:16,558 value manually I would like to use the 4788 05:14:16,558 --> 05:14:19,200 config service here I want to get the 4789 05:14:19,200 --> 05:14:22,320 Value Port value from the config service 4790 05:14:22,320 --> 05:14:24,878 and I would like to use it here this is 4791 05:14:24,878 --> 05:14:27,920 how you can do it you can get the config 4792 05:14:27,920 --> 05:14:30,160 service instance from the by calling the 4793 05:14:30,160 --> 05:14:32,638 app.get method and providing the 4794 05:14:32,638 --> 05:14:34,920 instance providing the config service 4795 05:14:34,920 --> 05:14:37,360 class it is going to give me the 4796 05:14:37,360 --> 05:14:39,520 instance config service 4797 05:14:39,520 --> 05:14:43,878 then I can use the method 4798 05:14:46,920 --> 05:14:49,798 here so you can do it 4799 05:14:49,798 --> 05:14:53,760 here config service you can import 4800 05:14:53,760 --> 05:14:56,400 it now I 4801 05:14:56,400 --> 05:14:59,680 can get the port 4802 05:14:59,680 --> 05:15:02,400 value service. 4803 05:15:02,400 --> 05:15:05,718 getet let's pass it as a 4804 05:15:05,718 --> 05:15:11,280 number here I can say the port which is 4805 05:15:14,638 --> 05:15:18,160 key get rid off this no I'm not using 4806 05:15:18,160 --> 05:15:21,760 seed service we 4807 05:15:33,320 --> 05:15:36,878 have our application is running fine now 4808 05:15:36,878 --> 05:15:39,638 let's send the test test request test I 4809 05:15:39,638 --> 05:15:42,718 got the port 3000 we successfully 4810 05:15:42,718 --> 05:15:46,760 configured the port in the main.ts 4811 05:15:46,760 --> 05:15:50,878 file now we also need to add our secret 4812 05:15:50,878 --> 05:15:55,240 key inside the EnV 4813 05:15:55,878 --> 05:15:59,160 file we are using getting the value from 4814 05:15:59,160 --> 05:16:02,200 the Au constants but I don't want to get 4815 05:16:02,200 --> 05:16:05,600 it from the Au constants I would like to 4816 05:16:05,600 --> 05:16:10,920 add secret key inside my EnV file 4817 05:16:11,000 --> 05:16:14,040 I have to place it here secret now I 4818 05:16:14,040 --> 05:16:16,440 would like to use this 4819 05:16:16,440 --> 05:16:20,160 secret we are using this secret inser 4820 05:16:20,160 --> 05:16:22,600 the app 4821 05:16:23,040 --> 05:16:27,000 module inser the Au module not app 4822 05:16:27,000 --> 05:16:31,638 module here we are using the O constant. 4823 05:16:31,638 --> 05:16:34,240 secret but I want to get it from the 4824 05:16:34,240 --> 05:16:36,760 config 4825 05:16:37,840 --> 05:16:41,120 service I want you to think about it how 4826 05:16:41,120 --> 05:16:44,920 can you add or how can you get the value 4827 05:16:44,920 --> 05:16:47,638 from config 4828 05:16:47,878 --> 05:16:51,480 service we also need to add a property 4829 05:16:51,480 --> 05:16:53,200 in the 4830 05:16:53,200 --> 05:16:55,958 configuration I'm going to do it 4831 05:16:55,958 --> 05:16:58,280 here and this is 4832 05:16:58,280 --> 05:17:01,160 how you will get the 4833 05:17:01,160 --> 05:17:04,958 value from config service we have to 4834 05:17:04,958 --> 05:17:07,680 register this module as a 4835 05:17:07,680 --> 05:17:10,200 async and and it is going to return the 4836 05:17:10,200 --> 05:17:11,920 dynamic 4837 05:17:11,920 --> 05:17:15,280 module so you have to follow this 4838 05:17:15,280 --> 05:17:19,400 pattern if you want to configure Dynamic 4839 05:17:19,400 --> 05:17:22,040 module Dynamic module is a feature that 4840 05:17:22,040 --> 05:17:24,320 allows you to dynamically configure and 4841 05:17:24,320 --> 05:17:28,120 register modules at runtime based on 4842 05:17:28,120 --> 05:17:29,638 Dynamic 4843 05:17:29,638 --> 05:17:34,840 conditions so we have this Dynamic 4844 05:17:35,558 --> 05:17:38,200 value so what I want you also have to 4845 05:17:38,200 --> 05:17:41,200 inject the config service you have to 4846 05:17:41,200 --> 05:17:44,718 import the config module this use 4847 05:17:44,718 --> 05:17:47,280 Factory is used to inject the config 4848 05:17:47,280 --> 05:17:50,480 service and we are getting the value 4849 05:17:50,480 --> 05:17:55,120 here so I'm going to copy 4850 05:17:55,400 --> 05:17:59,000 that I'm going to replace my Au module 4851 05:17:59,000 --> 05:18:00,080 with this 4852 05:18:00,080 --> 05:18:06,120 code import config service and config 4853 05:18:06,160 --> 05:18:09,638 module Imports I don't want to use the O 4854 05:18:09,638 --> 05:18:12,760 constant get rid of the 4855 05:18:12,760 --> 05:18:16,280 Hat we have to import config 4856 05:18:16,280 --> 05:18:19,400 module we also need to import config 4857 05:18:19,400 --> 05:18:23,280 service that is it 4858 05:18:23,400 --> 05:18:26,958 now we have specified The Secret inside 4859 05:18:26,958 --> 05:18:29,160 the 4860 05:18:29,958 --> 05:18:32,878 configuration did I Define the secret in 4861 05:18:32,878 --> 05:18:36,000 the production I think I did 4862 05:18:36,000 --> 05:18:39,760 not if you want you can do it 4863 05:18:39,760 --> 05:18:42,080 env. 4864 05:18:42,080 --> 05:18:45,958 production let's restart the 4865 05:19:00,320 --> 05:19:03,120 server our application is running 4866 05:19:03,120 --> 05:19:05,638 successfully it means we successfully 4867 05:19:05,638 --> 05:19:09,440 configured the secret key you can test 4868 05:19:09,440 --> 05:19:12,680 it by sending the API 4869 05:19:12,680 --> 05:19:15,798 request I have to open it 4870 05:19:15,798 --> 05:19:19,120 here rest 4871 05:19:19,120 --> 05:19:23,638 client I can send this signup 4872 05:19:28,280 --> 05:19:29,958 request 4873 05:19:29,958 --> 05:19:33,200 it's oh I got I sent the login request 4874 05:19:33,200 --> 05:19:36,400 we have to send the signup request user 4875 05:19:36,400 --> 05:19:40,638 has created successfully 4876 05:19:40,718 --> 05:19:44,280 it's working fine now now our next step 4877 05:19:44,280 --> 05:19:47,080 is to configure the DB configurations 4878 05:19:47,080 --> 05:19:49,360 database 4879 05:19:49,360 --> 05:19:53,718 configuration I'm going to add the 4880 05:19:53,718 --> 05:19:57,000 configurations inside the my EnV file 4881 05:19:57,000 --> 05:19:59,558 development 4882 05:19:59,558 --> 05:20:03,080 file if you want to add a 4883 05:20:03,080 --> 05:20:06,360 separate EnV file separate configuration 4884 05:20:06,360 --> 05:20:09,718 for database you can do it can say that 4885 05:20:09,718 --> 05:20:12,200 DB 4886 05:20:15,480 --> 05:20:20,160 configuration my database is end 4887 05:20:23,080 --> 05:20:27,520 test now we have to define the values 4888 05:20:27,520 --> 05:20:31,360 inside the configuration. ES 4889 05:20:35,920 --> 05:20:40,320 file I'm going to copy the same code 4890 05:20:40,320 --> 05:20:43,840 or I need these 4891 05:21:07,558 --> 05:21:12,760 values now we have to update or refactor 4892 05:21:12,760 --> 05:21:15,240 the manual 4893 05:21:15,240 --> 05:21:19,440 values with configuration config 4894 05:21:19,440 --> 05:21:22,878 service so I I'm going to 4895 05:21:22,878 --> 05:21:27,320 create a new type orm async config 4896 05:21:27,320 --> 05:21:29,600 object and then we're going to use 4897 05:21:29,600 --> 05:21:33,440 inside our type orm 4898 05:21:33,440 --> 05:21:37,360 module let's refactor it you have to 4899 05:21:37,360 --> 05:21:39,280 create it this is the same process you 4900 05:21:39,280 --> 05:21:42,400 have to create this module or register 4901 05:21:42,400 --> 05:21:45,320 this module as a 4902 05:21:46,600 --> 05:21:48,840 dynamic I'm going to use the same 4903 05:21:48,840 --> 05:21:51,520 configuration when defining the type or 4904 05:21:51,520 --> 05:21:52,480 a 4905 05:21:52,480 --> 05:21:55,958 module I'm going to do it 4906 05:21:55,958 --> 05:22:00,320 here you can import type orm config 4907 05:22:00,320 --> 05:22:03,040 options we also need to get config 4908 05:22:03,040 --> 05:22:06,240 module and config 4909 05:22:07,520 --> 05:22:10,280 service 4910 05:22:10,280 --> 05:22:13,400 type or module 4911 05:22:17,320 --> 05:22:21,080 options I got the DB host DB Port 4912 05:22:21,080 --> 05:22:24,958 username DB name password entities and 4913 05:22:24,958 --> 05:22:26,878 synchronize set to 4914 05:22:26,878 --> 05:22:29,680 false here we are going to get it from 4915 05:22:29,680 --> 05:22:32,360 the process.env 4916 05:22:32,360 --> 05:22:35,718 I'm going to copy 4917 05:22:35,718 --> 05:22:39,600 that code and I'm going to paste or you 4918 05:22:39,600 --> 05:22:41,000 can 4919 05:22:41,000 --> 05:22:45,440 say replace this text 4920 05:22:49,798 --> 05:22:53,000 host inside the app module we have to 4921 05:22:53,000 --> 05:22:54,040 register 4922 05:22:54,040 --> 05:22:57,480 it type orm module this 4923 05:22:57,480 --> 05:23:00,200 time type 4924 05:23:00,200 --> 05:23:03,160 orm ASN 4925 05:23:03,160 --> 05:23:07,120 config get rid of data 4926 05:23:07,440 --> 05:23:10,440 source 4927 05:23:24,760 --> 05:23:27,730 wrong driver I think something is 4928 05:23:27,730 --> 05:23:29,200 [Music] 4929 05:23:29,200 --> 05:23:32,200 missing 4930 05:23:37,440 --> 05:23:40,440 postgress 4931 05:23:45,958 --> 05:23:50,520 we have to use the for root async in the 4932 05:23:50,520 --> 05:23:54,680 app module I think that is why it is 4933 05:23:54,680 --> 05:23:57,400 showing me the 4934 05:24:07,360 --> 05:24:10,360 error 4935 05:24:15,120 --> 05:24:17,520 you can see that application is running 4936 05:24:17,520 --> 05:24:20,798 fine now it means we have successfully 4937 05:24:20,798 --> 05:24:22,280 configured the 4938 05:24:22,280 --> 05:24:24,920 database we have successfully configured 4939 05:24:24,920 --> 05:24:25,798 the 4940 05:24:25,798 --> 05:24:29,958 settings it's working fine 4941 05:24:32,280 --> 05:24:35,718 now in the previous video we have 4942 05:24:35,718 --> 05:24:39,638 created the environment variables 4943 05:24:39,638 --> 05:24:42,760 we have set up the DB configuration DB 4944 05:24:42,760 --> 05:24:47,558 Port username password and database 4945 05:24:47,558 --> 05:24:50,320 name we if you want to add the 4946 05:24:50,320 --> 05:24:53,480 validation if you want to validate these 4947 05:24:53,480 --> 05:24:56,280 environment variables you can also do 4948 05:24:56,280 --> 05:25:00,718 that with the help of class validator 4949 05:25:05,680 --> 05:25:08,798 package to implement the validation you 4950 05:25:08,798 --> 05:25:13,280 have to create the env. env. validation. 4951 05:25:13,280 --> 05:25:15,600 TS 4952 05:25:15,600 --> 05:25:21,680 file we did not add node EnV inside the 4953 05:25:21,680 --> 05:25:24,798 development or production. 4954 05:25:24,798 --> 05:25:28,320 EnV I have set the Isam which 4955 05:25:28,320 --> 05:25:30,798 is 4956 05:25:30,798 --> 05:25:33,878 enum here I have defined the validation 4957 05:25:33,878 --> 05:25:38,760 for Port DB host username password DB 4958 05:25:38,760 --> 05:25:40,760 name and the 4959 05:25:40,760 --> 05:25:44,480 secret we're going to use class based 4960 05:25:44,480 --> 05:25:47,680 validator class validator package that's 4961 05:25:47,680 --> 05:25:50,958 why I created the 4962 05:25:52,440 --> 05:25:55,520 class and we will have the validate 4963 05:25:55,520 --> 05:25:59,760 method it is going to accept the config 4964 05:25:59,760 --> 05:26:03,360 object with record key value 4965 05:26:03,360 --> 05:26:06,638 pair first of all we're going to use 4966 05:26:06,638 --> 05:26:11,000 plane to instance from class Transformer 4967 05:26:11,000 --> 05:26:14,040 package plain instance converts a plain 4968 05:26:14,040 --> 05:26:16,280 literal object to class based 4969 05:26:16,280 --> 05:26:19,120 Constructor object you can learn more 4970 05:26:19,120 --> 05:26:21,680 about class Transformer by looking at 4971 05:26:21,680 --> 05:26:25,360 the doc so class Transformer allows you 4972 05:26:25,360 --> 05:26:28,000 to transform plain object to some 4973 05:26:28,000 --> 05:26:31,280 instance of a class you can look at the 4974 05:26:31,280 --> 05:26:33,400 documentation of class Transformer if 4975 05:26:33,400 --> 05:26:36,798 you want to learn more about 4976 05:26:37,240 --> 05:26:39,440 it 4977 05:26:39,440 --> 05:26:44,840 and you have to enable this property to 4978 05:26:44,878 --> 05:26:47,798 true and we have to provide the 4979 05:26:47,798 --> 05:26:49,400 environment variable which we have 4980 05:26:49,400 --> 05:26:52,480 created the class this one environment 4981 05:26:52,480 --> 05:26:55,040 variables and we have the config SE 4982 05:26:55,040 --> 05:26:56,760 config 4983 05:26:56,760 --> 05:26:59,958 object you I'll teach you I'll show you 4984 05:26:59,958 --> 05:27:02,440 what is inside the validated 4985 05:27:02,440 --> 05:27:05,400 config now it should be the instance of 4986 05:27:05,400 --> 05:27:07,160 a 4987 05:27:07,160 --> 05:27:08,760 class 4988 05:27:08,760 --> 05:27:11,120 if error comes you can send the error 4989 05:27:11,120 --> 05:27:12,200 back 4990 05:27:12,200 --> 05:27:14,320 otherwise you have to return the 4991 05:27:14,320 --> 05:27:17,160 validated config and then you have to 4992 05:27:17,160 --> 05:27:21,320 provide this validate method inside the 4993 05:27:21,320 --> 05:27:24,080 config 4994 05:27:24,240 --> 05:27:27,360 module we don't have node EnV you can 4995 05:27:27,360 --> 05:27:30,280 create inside the environment variable 4996 05:27:30,280 --> 05:27:33,638 let me do that 4997 05:27:37,120 --> 05:27:40,120 first 4998 05:27:43,000 --> 05:27:46,680 env. environment I'm going to copy 4999 05:27:46,680 --> 05:27:52,320 that in the production I'm going to copy 5000 05:27:53,760 --> 05:27:57,200 here now you have to create a new 5001 05:27:57,200 --> 05:28:01,160 file inside the root 5002 05:28:04,760 --> 05:28:10,120 directory env. validation. TS file 5003 05:28:10,638 --> 05:28:14,160 file we will have the enum for 5004 05:28:14,160 --> 05:28:15,558 development 5005 05:28:15,558 --> 05:28:17,798 environment it could be development 5006 05:28:17,798 --> 05:28:20,080 production test and 5007 05:28:20,080 --> 05:28:23,480 provision now you have to create a 5008 05:28:23,480 --> 05:28:29,120 class to validate the environment 5009 05:28:32,120 --> 05:28:36,040 variables we have Port dbhost username 5010 05:28:36,040 --> 05:28:38,958 password and the secret now we have to 5011 05:28:38,958 --> 05:28:43,320 implement the logic for validate 5012 05:28:48,760 --> 05:28:51,798 function it should return the validated 5013 05:28:51,798 --> 05:28:54,920 object validated 5014 05:29:03,480 --> 05:29:06,520 config so I got the instance of the 5015 05:29:06,520 --> 05:29:09,280 class we provided the environment 5016 05:29:09,280 --> 05:29:12,160 variables here let's say if I would like 5017 05:29:12,160 --> 05:29:13,798 to 5018 05:29:13,798 --> 05:29:19,958 log the config I can say please log the 5019 05:29:20,360 --> 05:29:24,840 config and here I would like to 5020 05:29:26,240 --> 05:29:28,120 say 5021 05:29:28,120 --> 05:29:33,240 please log validated 5022 05:29:34,558 --> 05:29:37,320 config now we have to perform the 5023 05:29:37,320 --> 05:29:40,040 validation 5024 05:29:40,200 --> 05:29:42,558 there is a validate sync method you can 5025 05:29:42,558 --> 05:29:44,718 use 5026 05:29:44,718 --> 05:29:48,958 it let me import 5027 05:29:53,520 --> 05:29:57,840 them I'm going to import them 5028 05:30:06,958 --> 05:30:09,958 here 5029 05:30:21,440 --> 05:30:23,200 if error comes we need to throw the 5030 05:30:23,200 --> 05:30:25,480 error otherwise we need to return the 5031 05:30:25,480 --> 05:30:29,840 validated object validated config 5032 05:30:29,840 --> 05:30:33,760 object so we created the validate 5033 05:30:33,760 --> 05:30:37,120 function now it's time to register it 5034 05:30:37,120 --> 05:30:39,958 inside the app 5035 05:30:39,958 --> 05:30:44,000 module I'm going to register it in the 5036 05:30:44,000 --> 05:30:47,240 config module here there is a 5037 05:30:47,240 --> 05:30:52,558 validate we have to import the 5038 05:31:06,920 --> 05:31:10,840 validate you have to import it manually 5039 05:31:10,840 --> 05:31:13,320 it's not giving me the 5040 05:31:13,320 --> 05:31:16,240 suggestions can say 5041 05:31:16,240 --> 05:31:19,240 validate 5042 05:31:36,840 --> 05:31:39,840 from 5043 05:31:43,160 --> 05:31:45,798 it should not be 5044 05:31:45,798 --> 05:31:47,590 EnV it should 5045 05:31:47,590 --> 05:31:49,480 [Music] 5046 05:31:49,480 --> 05:31:53,480 be env. validation that's why I'm not 5047 05:31:53,480 --> 05:31:55,600 getting the 5048 05:31:55,600 --> 05:31:58,000 suggestion because this is a typescript 5049 05:31:58,000 --> 05:32:00,680 file it's not an environment 5050 05:32:00,680 --> 05:32:05,798 file now I should get the suggestion for 5051 05:32:06,840 --> 05:32:08,520 validate 5052 05:32:08,520 --> 05:32:12,600 let me import it from env. validation do 5053 05:32:12,600 --> 05:32:17,360 not import it from the class validator 5054 05:32:17,360 --> 05:32:21,760 package I would like to test this 5055 05:32:30,558 --> 05:32:34,600 application you can see that we have I 5056 05:32:34,600 --> 05:32:38,040 have the config object inside the the 5057 05:32:38,040 --> 05:32:41,600 config object I have the property Port 5058 05:32:41,600 --> 05:32:46,480 secret node EnV DB host we Define these 5059 05:32:46,480 --> 05:32:49,878 properties inside the environment file. 5060 05:32:49,878 --> 05:32:53,680 env. development I got the DB name all 5061 05:32:53,680 --> 05:32:57,520 these couple of things now you have 5062 05:32:57,520 --> 05:33:00,600 environment variable which is the 5063 05:33:00,600 --> 05:33:05,600 instance of a Class A Class based 5064 05:33:05,600 --> 05:33:09,120 object in environment variables I got 5065 05:33:09,120 --> 05:33:11,440 all this port all this 5066 05:33:11,440 --> 05:33:16,000 stuff I'm going to comment this 5067 05:33:16,520 --> 05:33:20,000 out what if I set the wrong value in the 5068 05:33:20,000 --> 05:33:24,638 development file let's let me test 5069 05:33:34,040 --> 05:33:38,440 it if I set the DB Port should be string 5070 05:33:38,440 --> 05:33:40,878 let's say 5071 05:33:40,878 --> 05:33:45,000 that it should give me the 5072 05:34:01,000 --> 05:34:04,120 error I have to restart the application 5073 05:34:04,120 --> 05:34:07,840 because I have made changes in theb do 5074 05:34:07,840 --> 05:34:10,400 development 5075 05:34:25,240 --> 05:34:30,400 file I did not set the validation for DB 5076 05:34:30,400 --> 05:34:34,240 Port that's why I did not get the 5077 05:34:34,240 --> 05:34:39,638 error let's say I'm going to do it here 5078 05:34:43,680 --> 05:34:44,760 DB 5079 05:34:44,760 --> 05:34:49,200 Port I can say the type should be 5080 05:34:49,958 --> 05:34:54,000 number I have saved the 5081 05:34:59,040 --> 05:35:03,000 application I have to restart 5082 05:35:06,638 --> 05:35:09,638 it 5083 05:35:23,280 --> 05:35:27,240 you can see that I got the 5084 05:35:27,480 --> 05:35:30,638 error property DB Port has filed the 5085 05:35:30,638 --> 05:35:34,718 following constraints is 5086 05:35:35,160 --> 05:35:39,638 number let's replace the DB 5087 05:35:40,440 --> 05:35:43,240 Port we have successfully implemented 5088 05:35:43,240 --> 05:35:48,200 the validations for environment 5089 05:35:50,240 --> 05:35:52,718 variables in this lesson I'm going to 5090 05:35:52,718 --> 05:35:56,600 teach you how to increase the speed of 5091 05:35:56,600 --> 05:35:59,400 module reloading what do I mean by 5092 05:35:59,400 --> 05:36:03,000 module hot reloading if I change 5093 05:36:03,000 --> 05:36:06,760 anything here in any file it will take 5094 05:36:06,760 --> 05:36:11,120 too much time time to reload the 5095 05:36:13,920 --> 05:36:16,080 application if I'm going to log 5096 05:36:16,080 --> 05:36:18,600 something 5097 05:36:19,360 --> 05:36:23,320 here now my application is running first 5098 05:36:23,320 --> 05:36:26,320 time it will take some time let me show 5099 05:36:26,320 --> 05:36:29,240 you the problem and what problem we're 5100 05:36:29,240 --> 05:36:31,798 going to solve and how we going to solve 5101 05:36:31,798 --> 05:36:34,160 that 5102 05:36:36,520 --> 05:36:39,520 problem 5103 05:37:06,520 --> 05:37:09,520 e 5104 05:37:36,440 --> 05:37:39,440 for 5105 05:37:39,878 --> 05:37:42,320 in this lesson I'm going to teach you 5106 05:37:42,320 --> 05:37:45,280 how to increase the speed of nestjs 5107 05:37:45,280 --> 05:37:46,718 project 5108 05:37:46,718 --> 05:37:50,920 reloading if I make changes in any file 5109 05:37:50,920 --> 05:37:54,080 let's say I'm going to delete this code 5110 05:37:54,080 --> 05:37:57,680 it will take some time to reload the 5111 05:37:57,680 --> 05:38:00,280 application I would like to increase 5112 05:38:00,280 --> 05:38:03,558 this speed with the help of web 5113 05:38:03,558 --> 05:38:06,360 pack you can see that it's taking too 5114 05:38:06,360 --> 05:38:08,600 much time to reload 5115 05:38:08,600 --> 05:38:11,958 application nestjs provides a way to 5116 05:38:11,958 --> 05:38:15,718 increase the speed of your hot 5117 05:38:15,718 --> 05:38:19,120 reloading you can see that it's still 5118 05:38:19,120 --> 05:38:22,040 calculating the 5119 05:38:23,520 --> 05:38:26,798 changes it's still calculating waiting 5120 05:38:26,798 --> 05:38:29,680 for the file 5121 05:38:36,440 --> 05:38:42,040 changes I think it has took 30 seconds 5122 05:38:42,040 --> 05:38:45,760 to load the application let's increase 5123 05:38:45,760 --> 05:38:48,958 the speed with the help of 5124 05:38:48,958 --> 05:38:51,958 weback first of all you have to create a 5125 05:38:51,958 --> 05:38:57,240 new file web H hot modu reloading doc 5126 05:38:57,240 --> 05:39:00,200 config you have to create this file 5127 05:39:00,200 --> 05:39:02,718 inside the root 5128 05:39:02,718 --> 05:39:05,638 directory and I'm going to copy this 5129 05:39:05,638 --> 05:39:08,360 code because I'm not not going to teach 5130 05:39:08,360 --> 05:39:12,440 you weac in this 5131 05:39:14,200 --> 05:39:17,760 course and step number two we have to 5132 05:39:17,760 --> 05:39:21,520 make some changes in bootstrap function 5133 05:39:21,520 --> 05:39:23,680 I can open the bootstrap function in the 5134 05:39:23,680 --> 05:39:29,000 main. file we have to add the code here 5135 05:39:29,000 --> 05:39:35,400 if module. H then you going to do this 5136 05:39:36,320 --> 05:39:39,320 process 5137 05:39:43,680 --> 05:39:48,440 it is asking me which module I going to 5138 05:39:48,440 --> 05:39:54,080 talk about let me check it from my 5139 05:40:00,558 --> 05:40:04,798 project yes I need to declare module as 5140 05:40:04,798 --> 05:40:06,320 a 5141 05:40:06,320 --> 05:40:09,320 variable 5142 05:40:10,200 --> 05:40:13,638 I can I would like to declare it here I 5143 05:40:13,638 --> 05:40:16,680 can also add that 5144 05:40:16,680 --> 05:40:20,440 part into my 5145 05:40:31,200 --> 05:40:34,240 documentation now our third step is to 5146 05:40:34,240 --> 05:40:38,520 add a script to use web pack instead of 5147 05:40:38,520 --> 05:40:41,120 default configuration to run the project 5148 05:40:41,120 --> 05:40:42,878 in development 5149 05:40:42,878 --> 05:40:48,920 mode we have to refactor the start dep 5150 05:40:49,240 --> 05:40:52,718 script I would like to change it and 5151 05:40:52,718 --> 05:40:55,558 let's do 5152 05:40:55,558 --> 05:40:58,200 it this time we're going to run the 5153 05:40:58,200 --> 05:41:01,320 project with web 5154 05:41:06,240 --> 05:41:09,240 pack 5155 05:41:36,240 --> 05:41:38,680 e 5156 05:41:38,680 --> 05:41:43,080 cannot find run script weback 5157 05:41:43,080 --> 05:41:47,600 plug-in do we need to install 5158 05:41:52,958 --> 05:41:57,520 it oh yeah we need to install 5159 05:42:06,120 --> 05:42:09,120 it 5160 05:42:36,120 --> 05:42:39,120 e 5161 05:42:47,520 --> 05:42:50,760 make sure you installed it as a Dev 5162 05:42:50,760 --> 05:42:56,240 dependency so I need to uninstall 5163 05:42:58,360 --> 05:43:03,600 it now we need to install it as a dep 5164 05:43:06,120 --> 05:43:09,120 dependency 5165 05:43:13,920 --> 05:43:16,798 let's run the 5166 05:43:36,040 --> 05:43:39,040 project 5167 05:43:47,878 --> 05:43:52,520 weback is building your 5168 05:44:06,040 --> 05:44:09,040 sources 5169 05:44:15,958 --> 05:44:18,718 project is 5170 05:44:19,600 --> 05:44:25,280 completed let's try to send the API 5171 05:44:25,320 --> 05:44:29,000 request but it's still 5172 05:44:36,040 --> 05:44:37,680 running 5173 05:44:37,680 --> 05:44:40,240 first time it will take some time to 5174 05:44:40,240 --> 05:44:43,200 initiate the 5175 05:44:56,558 --> 05:45:00,680 project now project has completed it's 5176 05:45:00,680 --> 05:45:04,160 time to send the API request I'm going 5177 05:45:04,160 --> 05:45:08,798 to send API request to root URL 5178 05:45:08,798 --> 05:45:11,000 let's say I would like to make some 5179 05:45:11,000 --> 05:45:14,280 changes I can 5180 05:45:14,440 --> 05:45:18,478 say hello world I saved the 5181 05:45:18,478 --> 05:45:21,478 application now you can see that it took 5182 05:45:21,478 --> 05:45:25,718 2 to 3 seconds to reload the 5183 05:45:26,280 --> 05:45:29,120 application updated modules you can see 5184 05:45:29,120 --> 05:45:34,840 that it also loged the message updated 5185 05:45:34,920 --> 05:45:39,558 modules so I'm going to move this 5186 05:45:39,558 --> 05:45:43,440 line application has started again I 5187 05:45:43,440 --> 05:45:46,360 found it's very helpful when you are 5188 05:45:46,360 --> 05:45:50,360 building a project in development 5189 05:45:51,200 --> 05:45:53,840 environment we're going to use Swagger 5190 05:45:53,840 --> 05:45:57,478 package to document the 5191 05:45:57,478 --> 05:46:00,000 apis if you don't know about Swagger 5192 05:46:00,000 --> 05:46:04,400 it's an OP Source software Frameworks 5193 05:46:04,400 --> 05:46:07,718 that enables developer to document and 5194 05:46:07,718 --> 05:46:10,320 design the restful web 5195 05:46:10,320 --> 05:46:13,478 services it provides a set of tools and 5196 05:46:13,478 --> 05:46:16,478 specification for defining the structure 5197 05:46:16,478 --> 05:46:19,798 end points and data models of an 5198 05:46:19,798 --> 05:46:22,958 API we will have Swagger specification 5199 05:46:22,958 --> 05:46:26,600 Swagger editor and Swagger 5200 05:46:26,958 --> 05:46:30,080 UI if you haven't looked at the Swagger 5201 05:46:30,080 --> 05:46:32,958 let me show you the 5202 05:46:35,840 --> 05:46:38,840 demo 5203 05:46:41,840 --> 05:46:45,760 this is the example of swagger 5204 05:46:45,760 --> 05:46:49,600 documentation you will have all the end 5205 05:46:49,600 --> 05:46:53,558 points what these end points are doing 5206 05:46:53,558 --> 05:46:56,360 you can look 5207 05:46:56,680 --> 05:46:59,558 at as you can see that we have profile 5208 05:46:59,558 --> 05:47:04,000 route we have all these routes available 5209 05:47:04,000 --> 05:47:06,920 you can also create a section for each 5210 05:47:06,920 --> 05:47:10,400 each route I'll teach you how to do that 5211 05:47:10,400 --> 05:47:13,080 so I just want to show you this is the 5212 05:47:13,080 --> 05:47:17,320 overall view of swagger 5213 05:47:21,360 --> 05:47:25,558 documentation so we have to implement it 5214 05:47:25,558 --> 05:47:28,320 first of all you have to install a third 5215 05:47:28,320 --> 05:47:31,478 party package which is nestjs 5216 05:47:31,478 --> 05:47:35,840 Swagger uh Swagger Nest has created this 5217 05:47:35,840 --> 05:47:38,840 package 5218 05:47:46,600 --> 05:47:48,478 let's install this 5219 05:47:48,478 --> 05:47:51,120 package or I can add into my 5220 05:47:51,120 --> 05:47:54,040 package.json 5221 05:47:57,760 --> 05:48:02,600 file let's install 5222 05:48:05,718 --> 05:48:08,718 it 5223 05:48:11,558 --> 05:48:14,760 Swagger package has installed now we 5224 05:48:14,760 --> 05:48:17,360 need to configure this Swagger module in 5225 05:48:17,360 --> 05:48:21,520 bootstrap function you have to do it 5226 05:48:21,520 --> 05:48:25,400 here you can use document Builder from a 5227 05:48:25,400 --> 05:48:26,638 Swagger 5228 05:48:26,638 --> 05:48:29,320 module you can have to set the title 5229 05:48:29,320 --> 05:48:31,558 title should be Spotify GL the name of 5230 05:48:31,558 --> 05:48:32,520 your 5231 05:48:32,520 --> 05:48:35,240 application and you can write the 5232 05:48:35,240 --> 05:48:37,240 description for your 5233 05:48:37,240 --> 05:48:39,718 application you can set the version and 5234 05:48:39,718 --> 05:48:41,878 finally you got to call the build 5235 05:48:41,878 --> 05:48:44,280 function and then we will create the 5236 05:48:44,280 --> 05:48:47,080 document with the help of swagger module 5237 05:48:47,080 --> 05:48:49,080 and you have to provide your application 5238 05:48:49,080 --> 05:48:52,120 instance and this config module this 5239 05:48:52,120 --> 05:48:54,478 config 5240 05:48:54,878 --> 05:48:57,958 setting and here we're going to mount 5241 05:48:57,958 --> 05:49:02,080 the Swagger document at/ API route and 5242 05:49:02,080 --> 05:49:03,718 you have to provide the application 5243 05:49:03,718 --> 05:49:07,920 instance and your document 5244 05:49:07,920 --> 05:49:10,120 I'm going to copy the same code and I'm 5245 05:49:10,120 --> 05:49:12,360 going to place it here inside the 5246 05:49:12,360 --> 05:49:14,000 bootstrap 5247 05:49:14,000 --> 05:49:18,080 function you got to do it 5248 05:49:26,680 --> 05:49:31,680 here let's import Swagger 5249 05:49:35,638 --> 05:49:38,638 module 5250 05:49:38,680 --> 05:49:41,558 I'm not getting the hints for Swagger 5251 05:49:41,558 --> 05:49:43,798 module and 5252 05:49:43,798 --> 05:49:47,760 document let's import 5253 05:49:55,718 --> 05:49:58,840 manually cannot find a module nestjs 5254 05:49:58,840 --> 05:50:01,638 worker 5255 05:50:05,638 --> 05:50:08,638 why 5256 05:50:09,080 --> 05:50:11,760 I think I have to install it 5257 05:50:11,760 --> 05:50:16,878 manually and install next just 5258 05:50:22,558 --> 05:50:26,760 swager now the error is going 5259 05:50:26,760 --> 05:50:31,520 now we have a Swagger 5260 05:50:35,638 --> 05:50:38,638 dependency 5261 05:50:42,320 --> 05:50:46,160 here we have the nest F 5262 05:50:53,280 --> 05:50:56,360 package you can write the name of your 5263 05:50:56,360 --> 05:50:57,760 title 5264 05:50:57,760 --> 05:51:00,080 application and you have to write the 5265 05:51:00,080 --> 05:51:02,760 description version and finally we got a 5266 05:51:02,760 --> 05:51:05,920 call the build and we are creating the 5267 05:51:05,920 --> 05:51:09,280 document and we have set up the Swagger 5268 05:51:09,280 --> 05:51:13,440 module it's time to run the 5269 05:51:19,680 --> 05:51:24,718 application oh there is an 5270 05:51:25,520 --> 05:51:30,160 issue it was an extra 5271 05:51:35,520 --> 05:51:38,000 parenthesis 5272 05:51:38,000 --> 05:51:41,080 application is running now let's try to 5273 05:51:41,080 --> 05:51:42,878 test 5274 05:51:42,878 --> 05:51:46,280 it I can go to the Local Host colon 3000 5275 05:51:46,280 --> 05:51:49,000 SL 5276 05:51:52,760 --> 05:51:55,878 API now we have the title of the 5277 05:51:55,878 --> 05:51:58,160 document and the 5278 05:51:58,160 --> 05:52:02,160 description we have all these routes but 5279 05:52:02,160 --> 05:52:04,520 we did not add the 5280 05:52:04,520 --> 05:52:07,360 tags I'll show you tags 5281 05:52:07,360 --> 05:52:09,920 in the next 5282 05:52:09,920 --> 05:52:12,680 video let's try to send the request to 5283 05:52:12,680 --> 05:52:14,440 the root 5284 05:52:14,440 --> 05:52:18,240 URL so I got this response hello I am 5285 05:52:18,240 --> 05:52:20,840 learning nests 5286 05:52:20,840 --> 05:52:24,360 fundamentals if I execute this endpoint 5287 05:52:24,360 --> 05:52:25,798 I got the 5288 05:52:25,798 --> 05:52:30,040 port the 200 status 5289 05:52:31,080 --> 05:52:33,798 code in this lesson I'm going to teach 5290 05:52:33,798 --> 05:52:39,558 you how to document this signup 5291 05:52:41,478 --> 05:52:45,280 route if you look at this signup 5292 05:52:45,280 --> 05:52:48,840 route I don't have any schema yet it's 5293 05:52:48,840 --> 05:52:51,680 not displaying the schema what type of 5294 05:52:51,680 --> 05:52:54,240 parameters do we need do we need first 5295 05:52:54,240 --> 05:52:57,600 name do we need last 5296 05:52:58,558 --> 05:53:02,680 name I also want to show the description 5297 05:53:02,680 --> 05:53:05,440 or the purpose of this signup 5298 05:53:05,440 --> 05:53:08,440 route 5299 05:53:11,840 --> 05:53:14,680 first of all you have to add the API tag 5300 05:53:14,680 --> 05:53:18,958 to the O controller let's convert all 5301 05:53:18,958 --> 05:53:22,520 these routes into o 5302 05:53:22,520 --> 05:53:25,440 section we're going to place the login 5303 05:53:25,440 --> 05:53:28,840 route into OD section enable to fa and 5304 05:53:28,840 --> 05:53:32,958 validate to fa let's do it first I'm 5305 05:53:32,958 --> 05:53:35,440 going to open the O 5306 05:53:35,440 --> 05:53:38,160 controller 5307 05:53:38,160 --> 05:53:39,718 here I can 5308 05:53:39,718 --> 05:53:43,240 say API 5309 05:53:44,878 --> 05:53:48,718 tags I can use the 5310 05:53:51,160 --> 05:53:55,600 Au my application is running 5311 05:53:59,798 --> 05:54:03,200 now you can see all the routes are 5312 05:54:03,200 --> 05:54:08,360 placed inside the O section 5313 05:54:10,840 --> 05:54:12,798 one more thing I would like to add in 5314 05:54:12,798 --> 05:54:15,360 the O 5315 05:54:16,478 --> 05:54:19,558 controller we have to decorate the sign 5316 05:54:19,558 --> 05:54:22,600 up route the next step we have to add 5317 05:54:22,600 --> 05:54:25,200 the API operation and the 5318 05:54:25,200 --> 05:54:29,798 response I'm going to use the API 5319 05:54:30,320 --> 05:54:33,040 operation we need to get it from this 5320 05:54:33,040 --> 05:54:35,320 Swagger 5321 05:54:35,320 --> 05:54:37,440 module 5322 05:54:37,440 --> 05:54:39,760 it will return the user in the response 5323 05:54:39,760 --> 05:54:42,360 with 2011 status code this is called 5324 05:54:42,360 --> 05:54:46,040 please register a new 5325 05:54:48,920 --> 05:54:52,958 user make sure it's running 5326 05:55:01,600 --> 05:55:05,000 now let's 5327 05:55:05,320 --> 05:55:08,320 refresh 5328 05:55:08,478 --> 05:55:10,680 I cannot see the sign up rout I think it 5329 05:55:10,680 --> 05:55:13,120 is taking too much 5330 05:55:13,120 --> 05:55:16,798 time oh I found the 5331 05:55:16,798 --> 05:55:20,478 error API 5332 05:55:24,200 --> 05:55:27,160 response we need to get the API response 5333 05:55:27,160 --> 05:55:30,000 yes we have the API 5334 05:55:30,000 --> 05:55:32,830 response let's restart the 5335 05:55:32,830 --> 05:55:35,240 [Music] 5336 05:55:35,240 --> 05:55:38,240 application 5337 05:55:40,440 --> 05:55:42,360 now you can see that we have the 5338 05:55:42,360 --> 05:55:44,160 register 5339 05:55:44,160 --> 05:55:48,200 user we have specified it 5340 05:55:48,200 --> 05:55:51,760 here somebody which is registered new 5341 05:55:51,760 --> 05:55:55,040 user and you can see the 5342 05:55:55,040 --> 05:55:58,320 response it should it will return a user 5343 05:55:58,320 --> 05:56:00,840 in the 5344 05:56:03,920 --> 05:56:07,958 response let's try to test it 5345 05:56:07,958 --> 05:56:12,520 if I if I provide first 5346 05:56:13,478 --> 05:56:16,110 name NH last 5347 05:56:16,110 --> 05:56:18,718 [Music] 5348 05:56:18,718 --> 05:56:21,718 name 5349 05:56:22,080 --> 05:56:26,040 shsf and we have to provide 5350 05:56:26,040 --> 05:56:30,000 email SN 5351 05:56:30,040 --> 05:56:33,958 gmail.com let's provide the 5352 05:56:35,240 --> 05:56:42,160 password 1 2 3 4 5 let's try to test it 5353 05:56:43,280 --> 05:56:49,160 out internal server 5354 05:56:49,520 --> 05:56:54,000 error because let me look at the error 5355 05:56:54,000 --> 05:56:57,400 entity metadata note found no metadata 5356 05:56:57,400 --> 05:56:59,520 for a user was 5357 05:56:59,520 --> 05:57:03,760 found I found there is an issue with 5358 05:57:03,760 --> 05:57:08,240 type orm and a web pack if you use vbac 5359 05:57:08,240 --> 05:57:11,878 hot reloading like we have used weac 5360 05:57:11,878 --> 05:57:15,520 package to reload the application fast 5361 05:57:15,520 --> 05:57:20,080 type orm does not work with this 5362 05:57:20,080 --> 05:57:22,840 configuration like this one we have to 5363 05:57:22,840 --> 05:57:26,080 replace it and you have to register all 5364 05:57:26,080 --> 05:57:30,000 the entities manually but it can work 5365 05:57:30,000 --> 05:57:32,600 with migrations there is no problem with 5366 05:57:32,600 --> 05:57:35,840 migrations but you have to replace it 5367 05:57:35,840 --> 05:57:38,120 here 5368 05:57:39,680 --> 05:57:43,558 we have to import couple of 5369 05:57:48,040 --> 05:57:52,400 entities application is broke 5370 05:57:52,400 --> 05:57:56,760 now I think I have to restart 5371 05:58:04,558 --> 05:58:08,160 it application is running fine let's 5372 05:58:08,160 --> 05:58:12,040 test it out I'm going to create a new 5373 05:58:12,040 --> 05:58:16,400 user a user has created successfully you 5374 05:58:16,400 --> 05:58:22,120 can see that with 2011 status 5375 05:58:30,878 --> 05:58:34,440 code when you look at the sign up 5376 05:58:34,440 --> 05:58:37,798 route if you check schema it is not 5377 05:58:37,798 --> 05:58:39,240 displaying me in the schema what do I 5378 05:58:39,240 --> 05:58:41,600 mean by 5379 05:58:41,798 --> 05:58:46,638 schema here I cannot see the login dto I 5380 05:58:46,638 --> 05:58:49,558 don't have create song dto I don't have 5381 05:58:49,558 --> 05:58:53,000 create user 5382 05:58:58,600 --> 05:59:01,920 dto it should display me the schema let 5383 05:59:01,920 --> 05:59:04,520 me run the 5384 05:59:05,040 --> 05:59:08,040 application 5385 05:59:10,760 --> 05:59:14,240 my application is running 5386 05:59:14,240 --> 05:59:17,080 now if you look at the schema in the 5387 05:59:17,080 --> 05:59:19,718 signup route you will see the empty 5388 05:59:19,718 --> 05:59:22,920 create user 5389 05:59:23,718 --> 05:59:27,760 dto you can fix it with the help of API 5390 05:59:27,760 --> 05:59:29,798 property you have to tell Swagger hey 5391 05:59:29,798 --> 05:59:32,040 this is the API property I want to I 5392 05:59:32,040 --> 05:59:35,040 want to see in this Swagger 5393 05:59:35,040 --> 05:59:39,040 document let's add API property in the 5394 05:59:39,040 --> 05:59:41,440 user 5395 05:59:41,718 --> 05:59:44,520 entity we have to do it here please 5396 05:59:44,520 --> 05:59:47,040 provide the first name of the user first 5397 05:59:47,040 --> 05:59:50,680 name of the user let me import API 5398 05:59:50,680 --> 05:59:53,478 property you have to do the same thing 5399 05:59:53,478 --> 05:59:58,200 for last name you can also add example 5400 05:59:58,200 --> 06:00:03,240 like what could be the data type look 5401 06:00:04,958 --> 06:00:07,840 like 5402 06:00:07,840 --> 06:00:12,160 I'm also going to add API property for 5403 06:00:12,160 --> 06:00:16,600 email let's do it for the 5404 06:00:27,840 --> 06:00:32,400 password my application is running 5405 06:00:33,120 --> 06:00:36,400 now but I cannot see this schema value I 5406 06:00:36,400 --> 06:00:41,878 cannot see properties here for my 5407 06:00:42,440 --> 06:00:46,400 schema you have to register nestjs 5408 06:00:46,400 --> 06:00:51,360 Swagger plugin you can do it in the nest 5409 06:00:51,360 --> 06:00:54,320 CLI I'm going to copy the 5410 06:00:54,320 --> 06:00:57,080 plugins open 5411 06:00:57,080 --> 06:01:01,638 nli and I'm going to do it 5412 06:01:03,280 --> 06:01:06,120 here let's restart the application 5413 06:01:06,120 --> 06:01:10,240 because we have modified 5414 06:01:15,958 --> 06:01:21,000 nli let's refresh the 5415 06:01:28,080 --> 06:01:30,638 application I cannot 5416 06:01:30,638 --> 06:01:34,320 see my 5417 06:01:34,840 --> 06:01:37,840 schema 5418 06:01:45,400 --> 06:01:48,200 I have to register the plug-in inside 5419 06:01:48,200 --> 06:01:49,920 the compiler 5420 06:01:49,920 --> 06:01:52,760 options that was the 5421 06:01:52,760 --> 06:01:56,600 mistake restart the 5422 06:02:04,040 --> 06:02:08,638 application now you can see the schema 5423 06:02:08,638 --> 06:02:15,080 value first name last name email and the 5424 06:02:15,200 --> 06:02:17,718 password it will also show you the 5425 06:02:17,718 --> 06:02:21,920 schema value for login 5426 06:02:34,840 --> 06:02:37,840 dto 5427 06:02:38,440 --> 06:02:42,080 here we have the user example value 5428 06:02:42,080 --> 06:02:48,040 email ID API key 5429 06:03:00,040 --> 06:03:02,680 playlist in this lesson I'm going to 5430 06:03:02,680 --> 06:03:06,280 teach you how to test Authentication in 5431 06:03:06,280 --> 06:03:08,878 Swagger 5432 06:03:09,520 --> 06:03:13,440 document we have to enable barer o it's 5433 06:03:13,440 --> 06:03:14,400 a 5434 06:03:14,400 --> 06:03:19,878 scheme you can do it in main.ts file you 5435 06:03:19,878 --> 06:03:22,600 have to add this function Nest Swagger 5436 06:03:22,600 --> 06:03:25,240 module provides this function add 5437 06:03:25,240 --> 06:03:27,440 barrier 5438 06:03:27,440 --> 06:03:31,400 o this is the type HTTP scheme is 5439 06:03:31,400 --> 06:03:34,080 barrier format should be 5440 06:03:34,080 --> 06:03:37,360 JWT name is 5441 06:03:37,360 --> 06:03:39,958 JWT you can add you can add the 5442 06:03:39,958 --> 06:03:43,400 description jwd token and I would like 5443 06:03:43,400 --> 06:03:45,478 to provide in the 5444 06:03:45,478 --> 06:03:48,840 header this is the name when you need to 5445 06:03:48,840 --> 06:03:53,400 use or apply on the controller 5446 06:03:53,760 --> 06:03:56,680 function one more thing we also need to 5447 06:03:56,680 --> 06:04:01,000 update the secret key in the JWT 5448 06:04:01,000 --> 06:04:04,520 strategy I don't want to use o constant. 5449 06:04:04,520 --> 06:04:07,200 secret I I have to get it from the 5450 06:04:07,200 --> 06:04:09,840 process.env 5451 06:04:09,840 --> 06:04:14,958 secret this get profile is our protected 5452 06:04:15,478 --> 06:04:17,718 route if you want to apply 5453 06:04:17,718 --> 06:04:21,160 authentication or app if you want to 5454 06:04:21,160 --> 06:04:24,200 protect or apply authentication of 5455 06:04:24,200 --> 06:04:28,680 swagger document you have to use the API 5456 06:04:28,680 --> 06:04:33,520 barrier a and this is the name we use 5457 06:04:33,520 --> 06:04:37,200 jw/ here this one 5458 06:04:37,200 --> 06:04:40,680 like create songs endpoint is also a 5459 06:04:40,680 --> 06:04:43,920 protected route you can also apply 5460 06:04:43,920 --> 06:04:48,958 authentication on that in the songs 5461 06:04:48,958 --> 06:04:52,000 controller these are all the protected 5462 06:04:52,000 --> 06:04:54,718 routes only 5463 06:04:54,718 --> 06:05:00,120 artist can access update endpoint delete 5464 06:05:00,120 --> 06:05:04,240 endpoint and create endpoint so you can 5465 06:05:04,240 --> 06:05:06,240 apply Authentication 5466 06:05:06,240 --> 06:05:09,240 on these routes as 5467 06:05:09,240 --> 06:05:13,080 well but I just want to show you the 5468 06:05:13,080 --> 06:05:17,160 demo in the app controller we have this 5469 06:05:17,160 --> 06:05:20,760 protected route get profile 5470 06:05:20,760 --> 06:05:24,920 endpoint let me show you in 5471 06:05:24,920 --> 06:05:30,280 our Swagger document SL 5472 06:05:32,958 --> 06:05:36,958 API here we have login 5473 06:05:36,958 --> 06:05:41,400 route we don't have any authentication 5474 06:05:41,400 --> 06:05:45,920 section let's implement it first of all 5475 06:05:45,920 --> 06:05:50,840 let me write documentation for login 5476 06:05:50,840 --> 06:05:55,040 route inside the O 5477 06:06:00,240 --> 06:06:04,120 controller it looks 5478 06:06:04,280 --> 06:06:09,280 good now we have to enable the 5479 06:06:09,280 --> 06:06:12,120 authentication I'm going to copy the 5480 06:06:12,120 --> 06:06:16,200 code and paste it 5481 06:06:23,798 --> 06:06:28,600 here now we have to apply JWT 5482 06:06:28,600 --> 06:06:30,798 authentication we also need to update 5483 06:06:30,798 --> 06:06:34,520 the secret key in the JWT 5484 06:06:34,520 --> 06:06:36,160 strategy 5485 06:06:36,160 --> 06:06:40,798 I don't want to use o secret from the O 5486 06:06:40,798 --> 06:06:43,718 constants let's get it from the 5487 06:06:43,718 --> 06:06:46,440 environment 5488 06:06:47,320 --> 06:06:51,200 variable and this is we have to 5489 06:06:51,200 --> 06:06:54,478 use I'm telling to Swagger hey Swagger 5490 06:06:54,478 --> 06:06:57,558 this is the protected 5491 06:07:03,320 --> 06:07:07,120 route application is broken now restart 5492 06:07:07,120 --> 06:07:10,360 the application I found an issue when 5493 06:07:10,360 --> 06:07:13,000 you are working with we hod module 5494 06:07:13,000 --> 06:07:16,240 reloading do not save the application if 5495 06:07:16,240 --> 06:07:20,520 your application is in error 5496 06:07:20,520 --> 06:07:24,080 format if there is an error in the file 5497 06:07:24,080 --> 06:07:26,360 do not save the application otherwise it 5498 06:07:26,360 --> 06:07:30,718 will break the V hod module reloading 5499 06:07:30,718 --> 06:07:33,558 now it it will take some time 5500 06:07:33,558 --> 06:07:37,440 to restart the app 5501 06:07:43,680 --> 06:07:46,798 application application is running 5502 06:07:46,798 --> 06:07:50,080 now I have to refresh my 5503 06:07:50,080 --> 06:07:54,478 page now you can see the 5504 06:07:54,878 --> 06:07:58,638 authorize if I try to access the profile 5505 06:07:58,638 --> 06:08:01,440 route from 5506 06:08:01,440 --> 06:08:05,718 here it should give me the error unauth 5507 06:08:05,718 --> 06:08:09,840 red so you can you have to log in to get 5508 06:08:09,840 --> 06:08:11,360 the access 5509 06:08:11,360 --> 06:08:15,760 token I'm going to send the login 5510 06:08:20,638 --> 06:08:25,520 request password does not match what was 5511 06:08:25,520 --> 06:08:30,600 the password 1 2 3 4 5 5512 06:08:32,240 --> 06:08:37,440 6 I have got the access token 5513 06:08:37,638 --> 06:08:41,200 now I can use this 5514 06:08:41,200 --> 06:08:43,920 token and I'm going to provide it here 5515 06:08:43,920 --> 06:08:48,400 this is the name JWT o you can see that 5516 06:08:48,400 --> 06:08:50,240 authorize 5517 06:08:50,240 --> 06:08:57,240 Now by enabling this JWT o 5518 06:08:57,600 --> 06:09:02,000 here this Swagger will use this token 5519 06:09:02,000 --> 06:09:04,360 just like we saved 5520 06:09:04,360 --> 06:09:07,360 here 5521 06:09:07,760 --> 06:09:10,760 let's access the protected 5522 06:09:10,760 --> 06:09:14,520 route you can see that I got the user ID 5523 06:09:14,520 --> 06:09:18,320 and the email it has already 5524 06:09:18,320 --> 06:09:24,040 attached authorization barrier and the 5525 06:09:26,400 --> 06:09:30,240 token if you have all protected routes 5526 06:09:30,240 --> 06:09:34,200 in the controller you can also add API 5527 06:09:34,200 --> 06:09:36,080 barrier or 5528 06:09:36,080 --> 06:09:40,638 here for all controller routes but I 5529 06:09:40,638 --> 06:09:43,400 have only single route is protected 5530 06:09:43,400 --> 06:09:46,798 protected this is the public right route 5531 06:09:46,798 --> 06:09:50,760 that is why I did not use the API or o 5532 06:09:50,760 --> 06:09:54,680 barrier or inside root inside the 5533 06:09:54,680 --> 06:09:56,558 controller for a 5534 06:09:56,558 --> 06:10:01,920 controller so I applied separately API 5535 06:10:01,920 --> 06:10:07,320 or API barrier OD 5536 06:10:24,240 --> 06:10:28,680 you can also Define the tags for songs 5537 06:10:28,680 --> 06:10:32,840 controller I want you to do 5538 06:10:33,600 --> 06:10:37,080 it you can apply High authentication on 5539 06:10:37,080 --> 06:10:40,718 songs controller protected 5540 06:10:43,958 --> 06:10:48,320 route like create is a protected route 5541 06:10:48,320 --> 06:10:53,440 you have to add API or o barrier 5542 06:10:53,440 --> 06:10:56,280 decorator 5543 06:11:02,440 --> 06:11:06,080 here it did not create the aay tax 5544 06:11:06,080 --> 06:11:09,040 because my application has broken now by 5545 06:11:09,040 --> 06:11:13,718 using this code restart the 5546 06:11:19,600 --> 06:11:22,040 application my application is running 5547 06:11:22,040 --> 06:11:25,280 now I can see all the songs related 5548 06:11:25,280 --> 06:11:29,080 route you can also do it for the 5549 06:11:29,080 --> 06:11:32,680 playlist API 5550 06:11:34,240 --> 06:11:36,680 TX 5551 06:11:36,680 --> 06:11:39,920 you can see the playlist 5552 06:11:39,920 --> 06:11:42,400 section in this lesson I'm going to 5553 06:11:42,400 --> 06:11:46,200 teach you how to install mango DB using 5554 06:11:46,200 --> 06:11:48,680 docket 5555 06:11:49,000 --> 06:11:51,440 compose if you don't have installed 5556 06:11:51,440 --> 06:11:54,520 mongod DB on your machine you can 5557 06:11:54,520 --> 06:11:57,478 install it manually or you can install 5558 06:11:57,478 --> 06:12:00,360 with Docker compose I have already 5559 06:12:00,360 --> 06:12:02,798 installed Docker on my machine my Docker 5560 06:12:02,798 --> 06:12:03,798 is 5561 06:12:03,798 --> 06:12:06,878 running you can see that here is my 5562 06:12:06,878 --> 06:12:09,798 Docker is running 5563 06:12:15,360 --> 06:12:19,160 now but we don't have any project right 5564 06:12:19,160 --> 06:12:22,160 now to use with 5565 06:12:22,160 --> 06:12:25,600 mongodb I don't want to install mongodb 5566 06:12:25,600 --> 06:12:27,920 in my previous project we're going to 5567 06:12:27,920 --> 06:12:30,440 create a new project to play around with 5568 06:12:30,440 --> 06:12:31,840 mongod DP 5569 06:12:31,840 --> 06:12:34,680 database I want you to create a new 5570 06:12:34,680 --> 06:12:37,120 project 5571 06:12:37,958 --> 06:12:41,320 I can say Nest 5572 06:12:41,320 --> 06:12:43,958 new and we need to provide the name of 5573 06:12:43,958 --> 06:12:46,320 your project I can 5574 06:12:46,320 --> 06:12:50,320 say n 5575 06:12:50,320 --> 06:12:52,600 production nestjs 5576 06:12:52,600 --> 06:12:56,040 project with mongod DB and I want to 5577 06:12:56,040 --> 06:12:59,400 ship it on production level you have to 5578 06:12:59,400 --> 06:13:01,798 choose your package manager I'm going to 5579 06:13:01,798 --> 06:13:02,840 use 5580 06:13:02,840 --> 06:13:05,798 npm it will take some time to install 5581 06:13:05,798 --> 06:13:08,000 the 5582 06:13:12,280 --> 06:13:15,000 dependencies my project has created 5583 06:13:15,000 --> 06:13:19,478 successfully all the dependencies have 5584 06:13:19,718 --> 06:13:23,798 installed I'm going to go to my 5585 06:13:28,360 --> 06:13:30,718 project I'm going to open the project 5586 06:13:30,718 --> 06:13:33,638 into my vs 5587 06:13:34,040 --> 06:13:37,040 code 5588 06:13:40,160 --> 06:13:42,680 let me run the project by using npm Run 5589 06:13:42,680 --> 06:13:45,240 start 5590 06:13:45,920 --> 06:13:50,878 Dev let's commit in 5591 06:13:50,878 --> 06:13:54,478 it and I would like to create a new 5592 06:13:54,478 --> 06:13:59,160 Branch I'll store this code into this 5593 06:14:04,040 --> 06:14:06,320 branch 5594 06:14:06,320 --> 06:14:09,560 [Music] 5595 06:14:10,600 --> 06:14:13,478 to connect with mongodb we will install 5596 06:14:13,478 --> 06:14:15,798 this dependency or this package in the 5597 06:14:15,798 --> 06:14:19,558 next video or you can install it 5598 06:14:19,558 --> 06:14:23,478 here we need NCS mango package and we 5599 06:14:23,478 --> 06:14:27,120 need mango separate 5600 06:14:28,240 --> 06:14:30,798 package this is the magic we need to 5601 06:14:30,798 --> 06:14:33,160 implement we're going to use docket 5602 06:14:33,160 --> 06:14:37,478 compose to start mang DB 5603 06:14:38,878 --> 06:14:42,680 driver I'm going to create a new file in 5604 06:14:42,680 --> 06:14:45,440 the root directory I can say Docker 5605 06:14:45,440 --> 06:14:48,440 compose 5606 06:14:48,798 --> 06:14:53,320 EML you have to Define your 5607 06:14:53,320 --> 06:14:57,360 version I can say one you have to define 5608 06:14:57,360 --> 06:14:59,600 the 5609 06:15:00,080 --> 06:15:02,320 services here we need to define the 5610 06:15:02,320 --> 06:15:06,280 service mongod DB or mango 5611 06:15:06,280 --> 06:15:09,240 mongodb the name of the 5612 06:15:09,240 --> 06:15:12,718 service you have to provide the 5613 06:15:12,718 --> 06:15:15,878 environment I can 5614 06:15:15,878 --> 06:15:20,478 say mongod DP 5615 06:15:21,520 --> 06:15:23,920 database and we need to provide the name 5616 06:15:23,920 --> 06:15:27,080 of the database which is 5617 06:15:27,080 --> 06:15:32,040 test we also need to provide the image 5618 06:15:32,040 --> 06:15:35,478 we need to get the local uh latest image 5619 06:15:35,478 --> 06:15:38,280 from the docker 5620 06:15:38,558 --> 06:15:41,680 repository Docker Hub or you can say 5621 06:15:41,680 --> 06:15:43,840 let's define the port I'm going to copy 5622 06:15:43,840 --> 06:15:46,798 the same code I don't want to make 5623 06:15:46,798 --> 06:15:51,840 mistake that's it or you can change your 5624 06:15:51,840 --> 06:15:55,840 database I can say 5625 06:15:55,840 --> 06:15:58,840 Spotify 5626 06:15:58,958 --> 06:16:03,440 clone now you have to start MB 5627 06:16:03,440 --> 06:16:06,160 driver you have to execute this command 5628 06:16:06,160 --> 06:16:07,958 do compose 5629 06:16:07,958 --> 06:16:10,520 up my project I'm going to open my 5630 06:16:10,520 --> 06:16:13,400 project into R directory and let's 5631 06:16:13,400 --> 06:16:16,120 execute this 5632 06:16:21,120 --> 06:16:24,280 command I 5633 06:16:25,040 --> 06:16:27,680 think already allocated I think my 5634 06:16:27,680 --> 06:16:30,000 already project is running 5635 06:16:30,000 --> 06:16:34,558 on 2701 docket compos let me stop that 5636 06:16:34,558 --> 06:16:36,840 project 5637 06:16:36,920 --> 06:16:40,878 project here we need to stop 5638 06:16:40,878 --> 06:16:43,010 it let's try it 5639 06:16:43,010 --> 06:16:45,920 [Music] 5640 06:16:45,920 --> 06:16:48,440 again now it is going to start the 5641 06:16:48,440 --> 06:16:50,840 mongod 5642 06:16:51,040 --> 06:16:55,638 DB I'm also going to use external tool 5643 06:16:55,638 --> 06:16:58,040 it's a graphical user 5644 06:16:58,040 --> 06:17:01,760 interface mango DP Compass you can 5645 06:17:01,760 --> 06:17:06,200 install it you can play around with GUI 5646 06:17:06,200 --> 06:17:09,440 with mongodb database this is the GUI 5647 06:17:09,440 --> 06:17:11,478 mongodb mongodb 5648 06:17:11,478 --> 06:17:14,170 Compass you have to have a new 5649 06:17:14,170 --> 06:17:15,478 [Music] 5650 06:17:15,478 --> 06:17:19,600 connection you can have a new 5651 06:17:21,680 --> 06:17:25,400 connection it is going to ask me the 5652 06:17:25,400 --> 06:17:27,958 host and the port I can say Local Host 5653 06:17:27,958 --> 06:17:29,558 and this is the port 5654 06:17:29,558 --> 06:17:32,638 2701 because my mango DB is running on 5655 06:17:32,638 --> 06:17:38,160 this port 27017 so I'm going to connect 5656 06:17:38,520 --> 06:17:43,840 it and you can create a new database 5657 06:17:43,840 --> 06:17:47,040 Spotify clone and you can have the 5658 06:17:47,040 --> 06:17:49,798 collection which is 5659 06:17:50,600 --> 06:17:52,840 songs you can see 5660 06:17:52,840 --> 06:17:55,558 that I have interacted with mongodb 5661 06:17:55,558 --> 06:17:59,400 database and I created a new 5662 06:18:03,760 --> 06:18:06,080 database 5663 06:18:06,080 --> 06:18:08,200 so our mongod DP driver is running 5664 06:18:08,200 --> 06:18:10,440 successfully in the next video I'll 5665 06:18:10,440 --> 06:18:12,440 teach you how to connect nestjs 5666 06:18:12,440 --> 06:18:17,558 application with mangos or mongod 5667 06:18:18,000 --> 06:18:20,958 DP in the previous video we have 5668 06:18:20,958 --> 06:18:24,040 installed these two packages 5669 06:18:24,040 --> 06:18:28,080 mangos and nestjs 5670 06:18:28,840 --> 06:18:31,160 mangos if you would like to connect 5671 06:18:31,160 --> 06:18:34,320 nestjs application with mango or mango 5672 06:18:34,320 --> 06:18:35,440 DB 5673 06:18:35,440 --> 06:18:39,200 you have to create a new module in the 5674 06:18:39,200 --> 06:18:42,040 app module you have to create it by 5675 06:18:42,040 --> 06:18:45,200 calling the for root method this for 5676 06:18:45,200 --> 06:18:48,120 root method is similar to mango. connect 5677 06:18:48,120 --> 06:18:51,520 from the mango package you can read the 5678 06:18:51,520 --> 06:18:54,798 connect package from the official 5679 06:18:54,798 --> 06:18:56,840 documentation what I want you to make 5680 06:18:56,840 --> 06:18:59,478 sure your mongodb is 5681 06:18:59,478 --> 06:19:03,360 running my mango mongod DB driver mongod 5682 06:19:03,360 --> 06:19:07,080 DB is running with the help of Docker I 5683 06:19:07,080 --> 06:19:10,160 use Docker compose 5684 06:19:10,160 --> 06:19:15,600 up here it is running Docker compose 5685 06:19:17,400 --> 06:19:21,200 up now we have to call this method 5686 06:19:21,200 --> 06:19:24,920 mongos module. forward in the app 5687 06:19:24,920 --> 06:19:28,040 module make sure you have this database 5688 06:19:28,040 --> 06:19:30,680 Spotify clone if I run the application 5689 06:19:30,680 --> 06:19:33,600 let's see what will 5690 06:19:33,638 --> 06:19:36,638 happen 5691 06:19:40,000 --> 06:19:44,160 npm runs start 5692 06:19:52,120 --> 06:19:56,360 def mango module because we did not 5693 06:19:56,360 --> 06:19:59,600 import it let's import it with the help 5694 06:19:59,600 --> 06:20:03,638 of VSS code I'm going to save the 5695 06:20:03,638 --> 06:20:06,638 application 5696 06:20:07,240 --> 06:20:10,040 you can see that Mango's package has 5697 06:20:10,040 --> 06:20:13,478 installed it has also initialize the 5698 06:20:13,478 --> 06:20:15,718 mango score module 5699 06:20:15,718 --> 06:20:17,798 dependencies it means we have 5700 06:20:17,798 --> 06:20:21,280 successfully connected to mongod 5701 06:20:21,280 --> 06:20:25,360 DP if I will write the invalid Port 5702 06:20:25,360 --> 06:20:28,440 let's see what will 5703 06:20:33,638 --> 06:20:36,638 happen 5704 06:20:38,840 --> 06:20:41,680 I'm getting the error mangos module 5705 06:20:41,680 --> 06:20:43,958 unable to connect the 5706 06:20:43,958 --> 06:20:47,878 database so it means we have configured 5707 06:20:47,878 --> 06:20:50,760 the mango DB with 5708 06:20:50,760 --> 06:20:54,600 nestjs but if you look at mango 5709 06:20:54,600 --> 06:20:57,840 Compass I think I don't have Spotify 5710 06:20:57,840 --> 06:20:58,760 clone 5711 06:20:58,760 --> 06:21:01,840 database it can work without creating 5712 06:21:01,840 --> 06:21:06,440 the database if I create the database 5713 06:21:06,440 --> 06:21:07,958 it will 5714 06:21:07,958 --> 06:21:12,040 work I can create the name of the 5715 06:21:12,040 --> 06:21:15,680 collection now we have Spotify 5716 06:21:15,680 --> 06:21:19,360 clone it looks 5717 06:21:19,638 --> 06:21:22,200 good now we're going to create schema 5718 06:21:22,200 --> 06:21:25,000 using mongos 5719 06:21:25,000 --> 06:21:29,160 model in mongos everything starts with 5720 06:21:29,160 --> 06:21:33,040 schema each schema maps to mongodb 5721 06:21:33,040 --> 06:21:35,840 collection and different finds the shape 5722 06:21:35,840 --> 06:21:37,760 of the document within the 5723 06:21:37,760 --> 06:21:40,718 collection schemas are used to define 5724 06:21:40,718 --> 06:21:43,958 the models and models are responsible 5725 06:21:43,958 --> 06:21:46,440 for creating and reading documents from 5726 06:21:46,440 --> 06:21:49,200 the mongodb 5727 06:21:49,200 --> 06:21:51,280 database so we're going to create the 5728 06:21:51,280 --> 06:21:52,958 first 5729 06:21:52,958 --> 06:21:56,040 schema you have to create a new folder 5730 06:21:56,040 --> 06:21:58,200 songs here you have to create the 5731 06:21:58,200 --> 06:22:01,000 schemas folder and you have to have a 5732 06:22:01,000 --> 06:22:04,600 schema which is songs. TS file but we 5733 06:22:04,600 --> 06:22:07,718 will use this song document when we will 5734 06:22:07,718 --> 06:22:10,680 inject the model into song 5735 06:22:10,680 --> 06:22:14,440 service you can use the decorator schema 5736 06:22:14,440 --> 06:22:18,680 nestjs mango module provides schema 5737 06:22:18,680 --> 06:22:21,680 decorator this prop represents the 5738 06:22:21,680 --> 06:22:23,798 property in the 5739 06:22:23,798 --> 06:22:27,440 collection we will have songs collection 5740 06:22:27,440 --> 06:22:29,558 and inside the song collection we will 5741 06:22:29,558 --> 06:22:34,040 have these Fields Title release date 5742 06:22:34,040 --> 06:22:35,000 duration 5743 06:22:35,000 --> 06:22:37,400 and the 5744 06:22:39,280 --> 06:22:42,478 lyrics Yep this schema decorator marks a 5745 06:22:42,478 --> 06:22:45,878 class as a schema definition it Maps Our 5746 06:22:45,878 --> 06:22:48,718 Song class to mongod DP collection of 5747 06:22:48,718 --> 06:22:52,400 the same name but it will add S at the 5748 06:22:52,400 --> 06:22:54,878 end of the final mango collection name 5749 06:22:54,878 --> 06:22:55,760 will be 5750 06:22:55,760 --> 06:22:58,600 songs and prop decorator defines a 5751 06:22:58,600 --> 06:23:01,558 property in the document and finally we 5752 06:23:01,558 --> 06:23:06,000 will have schema Factory 5753 06:23:06,000 --> 06:23:09,000 it will return the raw definition of 5754 06:23:09,000 --> 06:23:12,878 schema I logged on song schema and I got 5755 06:23:12,878 --> 06:23:15,280 this result schema object we have these 5756 06:23:15,280 --> 06:23:17,680 properties title release date and 5757 06:23:17,680 --> 06:23:22,440 duration and couple of metadata for each 5758 06:23:22,440 --> 06:23:24,680 field this is how you will create a 5759 06:23:24,680 --> 06:23:28,600 schema I want you to implement 5760 06:23:33,440 --> 06:23:36,440 it 5761 06:23:37,120 --> 06:23:39,440 so I'm going to implement 5762 06:23:39,440 --> 06:23:43,840 here by creating a new 5763 06:23:44,080 --> 06:23:47,798 folder inside these 5764 06:23:49,920 --> 06:23:53,680 songs we will have 5765 06:23:54,240 --> 06:23:59,440 schemas here I'm going to create a song 5766 06:23:59,600 --> 06:24:03,360 schema or song. TS 5767 06:24:03,360 --> 06:24:05,798 file 5768 06:24:05,798 --> 06:24:09,040 I just pasted my 5769 06:24:16,840 --> 06:24:20,760 code we have defined four Fields I got 5770 06:24:20,760 --> 06:24:23,160 all these properties from our previous 5771 06:24:23,160 --> 06:24:25,920 project which we worked with tym and 5772 06:24:25,920 --> 06:24:30,120 post I got title release date duration 5773 06:24:30,120 --> 06:24:35,040 and lyrics I set this data type for dur 5774 06:24:35,040 --> 06:24:38,400 string I couldn't find a Time Field Time 5775 06:24:38,400 --> 06:24:42,440 data type in mangos so you can validate 5776 06:24:42,440 --> 06:24:46,160 manually duration by using third party 5777 06:24:46,160 --> 06:24:48,520 date 5778 06:24:51,200 --> 06:24:55,718 plug-in so we created the schema 5779 06:24:56,280 --> 06:24:58,600 successfully this lesson I'm going to 5780 06:24:58,600 --> 06:25:02,360 teach you how to save record in mongodb 5781 06:25:02,360 --> 06:25:04,680 database we're going to save record card 5782 06:25:04,680 --> 06:25:06,200 in mongod DP 5783 06:25:06,200 --> 06:25:08,280 collection right now we don't have a 5784 06:25:08,280 --> 06:25:11,000 songs module we don't have songs 5785 06:25:11,000 --> 06:25:13,840 controller we don't have song surveys 5786 06:25:13,840 --> 06:25:16,840 we're going to create all these stuff 5787 06:25:16,840 --> 06:25:18,958 and then we're going to create post end 5788 06:25:18,958 --> 06:25:21,878 point inside the songs controller you 5789 06:25:21,878 --> 06:25:23,840 have learned how to create the post 5790 06:25:23,840 --> 06:25:26,798 endpoint how to create network request 5791 06:25:26,798 --> 06:25:31,680 object or data transfer object 5792 06:25:31,680 --> 06:25:34,600 dto and then I'm going to use song song 5793 06:25:34,600 --> 06:25:37,280 service song service has create method 5794 06:25:37,280 --> 06:25:40,400 to save the record in mongodb collection 5795 06:25:40,400 --> 06:25:43,600 and finally we will create create song 5796 06:25:43,600 --> 06:25:49,320 dto object inside the create song d. 5797 06:25:50,478 --> 06:25:54,200 file and here I would like to inject the 5798 06:25:54,200 --> 06:25:58,040 model remember schema will act as a 5799 06:25:58,040 --> 06:26:01,080 model you can use schema to generate a 5800 06:26:01,080 --> 06:26:03,920 model so I'm telling please give me the 5801 06:26:03,920 --> 06:26:04,840 model 5802 06:26:04,840 --> 06:26:07,920 the name of the model which is songs and 5803 06:26:07,920 --> 06:26:10,680 I would like to use here I would like to 5804 06:26:10,680 --> 06:26:14,478 inject it as a dependency I remember I 5805 06:26:14,478 --> 06:26:17,718 have created the type in the song schema 5806 06:26:17,718 --> 06:26:20,200 file I got the song 5807 06:26:20,200 --> 06:26:23,360 document and this song model provides 5808 06:26:23,360 --> 06:26:26,080 crud operation create read update and 5809 06:26:26,080 --> 06:26:28,958 delete it is similar to type orm 5810 06:26:28,958 --> 06:26:31,718 repository it will be treat treated as a 5811 06:26:31,718 --> 06:26:33,240 type RM 5812 06:26:33,240 --> 06:26:36,000 Repository and finally we have a safe 5813 06:26:36,000 --> 06:26:38,400 method in the song model and I am going 5814 06:26:38,400 --> 06:26:40,878 to call the method here it is going to 5815 06:26:40,878 --> 06:26:43,240 return the 5816 06:26:43,718 --> 06:26:47,320 promise and finally we have created the 5817 06:26:47,320 --> 06:26:48,280 songs 5818 06:26:48,280 --> 06:26:51,840 module we have to inject the song model 5819 06:26:51,840 --> 06:26:54,120 just like we did with type orm 5820 06:26:54,120 --> 06:26:56,878 repository if you will not do that part 5821 06:26:56,878 --> 06:26:59,638 you will not be able to inject song 5822 06:26:59,638 --> 06:27:02,520 module here that's why you have to 5823 06:27:02,520 --> 06:27:06,360 register it as a here I'm telling to njs 5824 06:27:06,360 --> 06:27:08,760 I would like to use song model in the 5825 06:27:08,760 --> 06:27:11,558 service or in my 5826 06:27:11,558 --> 06:27:13,600 provider and then you can send the 5827 06:27:13,600 --> 06:27:14,920 request to this 5828 06:27:14,920 --> 06:27:19,080 URL to to create a new song in the 5829 06:27:19,080 --> 06:27:21,638 mongod TB collection I want you to 5830 06:27:21,638 --> 06:27:24,080 implement 5831 06:27:24,080 --> 06:27:27,558 it let me implement it for 5832 06:27:27,558 --> 06:27:30,920 you let's 5833 06:27:31,000 --> 06:27:36,120 generate let's generate controller 5834 06:27:36,638 --> 06:27:38,798 songs let 5835 06:27:38,798 --> 06:27:41,478 generate so 5836 06:27:41,478 --> 06:27:44,478 service 5837 06:27:49,160 --> 06:27:53,080 songs it has created the songs 5838 06:27:53,080 --> 06:27:56,840 module it has also updated the entry in 5839 06:27:56,840 --> 06:27:58,200 the app 5840 06:27:58,200 --> 06:28:01,760 module you can also see the songs 5841 06:28:01,760 --> 06:28:05,280 controller it has also updated Creed the 5842 06:28:05,280 --> 06:28:08,958 songs controller inside the app module 5843 06:28:08,958 --> 06:28:11,718 you can see 5844 06:28:11,840 --> 06:28:16,440 that now we have songs 5845 06:28:16,558 --> 06:28:19,240 module inside the songs module we have 5846 06:28:19,240 --> 06:28:23,240 songs controller and the song 5847 06:28:25,320 --> 06:28:30,200 service now it's time to create a new 5848 06:28:30,200 --> 06:28:33,680 endpoint I'm going to copy the code 5849 06:28:33,680 --> 06:28:35,638 inside the songs 5850 06:28:35,638 --> 06:28:38,798 controller we just injected the song 5851 06:28:38,798 --> 06:28:41,600 service right now this create method 5852 06:28:41,600 --> 06:28:46,000 does not exist in the song 5853 06:28:46,000 --> 06:28:50,760 service we also don't have create song 5854 06:28:50,760 --> 06:28:54,440 dto Let's create let's create create 5855 06:28:54,440 --> 06:28:55,520 song 5856 06:28:55,520 --> 06:28:58,320 dto I'm going to create a new folder 5857 06:28:58,320 --> 06:29:02,638 dtos and I'm going to create a new file 5858 06:29:02,638 --> 06:29:06,440 file create song hd.s 5859 06:29:06,440 --> 06:29:09,680 file that's 5860 06:29:12,920 --> 06:29:16,360 it I already taught you how to create a 5861 06:29:16,360 --> 06:29:19,120 method in the 5862 06:29:19,760 --> 06:29:23,400 service let's refactor the song service 5863 06:29:23,400 --> 06:29:24,958 add a new 5864 06:29:24,958 --> 06:29:28,080 method we have injected the song model 5865 06:29:28,080 --> 06:29:29,920 make sure you provide the name of the 5866 06:29:29,920 --> 06:29:34,320 song model here we are injecting the 5867 06:29:34,320 --> 06:29:38,680 song model and I called called create 5868 06:29:38,680 --> 06:29:42,558 method from song 5869 06:29:57,840 --> 06:30:01,920 model and finally we have to tell njs I 5870 06:30:01,920 --> 06:30:04,638 want to use this song model in the songs 5871 06:30:04,638 --> 06:30:08,958 module so please make it 5872 06:30:30,280 --> 06:30:32,958 injectable and now we have to run the 5873 06:30:32,958 --> 06:30:35,920 application 5874 06:30:46,840 --> 06:30:49,718 I have to import 5875 06:30:49,718 --> 06:30:54,040 song dependency 5876 06:31:02,920 --> 06:31:05,920 song 5877 06:31:08,798 --> 06:31:10,600 it looks 5878 06:31:10,600 --> 06:31:14,680 good we have to test 5879 06:31:16,320 --> 06:31:19,718 it I'm going to create a new file to 5880 06:31:19,718 --> 06:31:25,878 make API request I can say api. 5881 06:31:26,240 --> 06:31:30,840 HTTP let's send the API 5882 06:31:32,840 --> 06:31:35,840 request 5883 06:31:42,080 --> 06:31:45,718 you can see that a song has created we 5884 06:31:45,718 --> 06:31:48,958 can verify from 5885 06:31:48,958 --> 06:31:51,840 Compass here is the record inside the 5886 06:31:51,840 --> 06:31:54,280 songs 5887 06:31:55,920 --> 06:32:00,558 collection let's add another 5888 06:32:02,840 --> 06:32:05,840 song 5889 06:32:06,638 --> 06:32:09,958 so we have another 5890 06:32:15,040 --> 06:32:18,478 song it should display the second song 5891 06:32:18,478 --> 06:32:21,718 but I cannot find 5892 06:32:24,280 --> 06:32:28,280 out no I can see the second 5893 06:32:28,280 --> 06:32:33,040 song we have two records in mongodb 5894 06:32:33,040 --> 06:32:36,600 collection this is the 5895 06:32:40,360 --> 06:32:45,520 document if I don't set the title what 5896 06:32:45,520 --> 06:32:49,718 will happen you will see internal server 5897 06:32:49,718 --> 06:32:52,798 error because we have set the 5898 06:32:52,798 --> 06:32:55,920 validations inside the song 5899 06:32:55,920 --> 06:32:59,080 schema these properties are required 5900 06:32:59,080 --> 06:33:01,680 title is required release date is 5901 06:33:01,680 --> 06:33:05,760 required and duration is required 5902 06:33:09,398 --> 06:33:11,798 in this lesson you're going to learn how 5903 06:33:11,798 --> 06:33:16,280 to implement find and delete method we 5904 06:33:16,280 --> 06:33:19,840 will create two endpoints to handle 5905 06:33:19,840 --> 06:33:21,718 find and 5906 06:33:21,718 --> 06:33:24,718 delete 5907 06:33:26,600 --> 06:33:30,080 request let's start implement it I have 5908 06:33:30,080 --> 06:33:33,840 already taught you how to implement crud 5909 06:33:33,840 --> 06:33:38,080 or rest based API end 5910 06:33:41,000 --> 06:33:45,558 points let's create a method find to 5911 06:33:45,558 --> 06:33:49,840 fetch all the records from mango DP 5912 06:33:49,840 --> 06:33:52,080 collection it is going to give me all 5913 06:33:52,080 --> 06:33:54,360 the 5914 06:33:55,840 --> 06:33:58,478 documents now we have to create a route 5915 06:33:58,478 --> 06:34:01,240 to handle the 5916 06:34:02,718 --> 06:34:05,718 request 5917 06:34:06,798 --> 06:34:09,398 our application is running now let's 5918 06:34:09,398 --> 06:34:12,200 send the API 5919 06:34:12,200 --> 06:34:15,638 request to fetch all the 5920 06:34:15,638 --> 06:34:20,440 songs we have to send the get API 5921 06:34:27,638 --> 06:34:31,440 request we don't have any record yet 5922 06:34:31,440 --> 06:34:33,920 let's create a new song I have I've 5923 06:34:33,920 --> 06:34:37,040 created a new song I'm going to create 5924 06:34:37,040 --> 06:34:39,638 another 5925 06:34:40,200 --> 06:34:44,440 song now we have two songs let's fetch 5926 06:34:44,440 --> 06:34:47,200 all these 5927 06:34:53,638 --> 06:34:56,760 songs we have successfully implemented 5928 06:34:56,760 --> 06:34:58,600 the find all 5929 06:34:58,600 --> 06:35:01,240 method now it's time to implement the 5930 06:35:01,240 --> 06:35:02,638 find by 5931 06:35:02,638 --> 06:35:05,638 ID 5932 06:35:12,440 --> 06:35:15,120 I'm going to create a new method find by 5933 06:35:15,120 --> 06:35:18,878 ID in the song service and there is a 5934 06:35:18,878 --> 06:35:22,718 method find by ID in the song model you 5935 06:35:22,718 --> 06:35:25,478 have to provide the ID we're going to 5936 06:35:25,478 --> 06:35:29,320 get the ID from the request 5937 06:35:29,760 --> 06:35:32,638 parameter let's create a new 5938 06:35:32,638 --> 06:35:35,638 route 5939 06:35:42,638 --> 06:35:44,920 now we are going to send the request to 5940 06:35:44,920 --> 06:35:49,280 fetch song Only based on 5941 06:35:50,000 --> 06:35:53,680 ID find one 5942 06:36:01,920 --> 06:36:05,040 song we have to get the ID I'm going to 5943 06:36:05,040 --> 06:36:08,160 send the request to fetch all the 5944 06:36:08,160 --> 06:36:12,120 songs now I'm going to copy the ID and 5945 06:36:12,120 --> 06:36:15,718 we need to provide the ID 5946 06:36:15,718 --> 06:36:19,240 here so I got the single record on the 5947 06:36:19,240 --> 06:36:22,200 based on 5948 06:36:23,878 --> 06:36:28,040 ID you can also implement the delete 5949 06:36:28,040 --> 06:36:30,680 endpoint I'm going to create a delete 5950 06:36:30,680 --> 06:36:34,200 function inside the song service 5951 06:36:34,200 --> 06:36:38,040 song model provides delete one method 5952 06:36:38,040 --> 06:36:41,160 and you have to provide the ID mango 5953 06:36:41,160 --> 06:36:44,440 work works with underscore ID if you 5954 06:36:44,440 --> 06:36:47,718 have worked with mongodb the ID 5955 06:36:47,718 --> 06:36:54,558 stored ID stores in this underscore ID 5956 06:36:54,558 --> 06:36:58,080 format if you if you are using or have 5957 06:36:58,080 --> 06:37:02,360 used postgress or MySQL in my SQL or 5958 06:37:02,360 --> 06:37:05,440 postgress you say the primary key or 5959 06:37:05,440 --> 06:37:11,360 primary ID with this ID without 5960 06:37:11,840 --> 06:37:14,000 underscore now we have to create a 5961 06:37:14,000 --> 06:37:18,040 delete endpoint in the songs 5962 06:37:18,638 --> 06:37:21,280 controller we have to get the delete 5963 06:37:21,280 --> 06:37:24,920 method that is 5964 06:37:32,440 --> 06:37:34,360 it 5965 06:37:34,360 --> 06:37:37,760 let's create a new 5966 06:37:37,760 --> 06:37:40,478 song because we're going to delete that 5967 06:37:40,478 --> 06:37:44,200 song I'm going to get the ID and now we 5968 06:37:44,200 --> 06:37:46,760 have to delete the song to send the 5969 06:37:46,760 --> 06:37:48,478 delete API 5970 06:37:48,478 --> 06:37:51,920 request delete 5971 06:37:57,160 --> 06:38:01,160 song you can see deleted count is one 5972 06:38:01,160 --> 06:38:04,638 acknowledged true it means we have 5973 06:38:04,638 --> 06:38:07,680 successfully deleted the song you can 5974 06:38:07,680 --> 06:38:10,000 double check it by sending the API 5975 06:38:10,000 --> 06:38:13,360 request to fetch all the songs and you 5976 06:38:13,360 --> 06:38:18,478 can see new song does not exist 5977 06:38:18,478 --> 06:38:22,280 here you can also implement the update 5978 06:38:22,280 --> 06:38:24,760 functionality I already taught you when 5979 06:38:24,760 --> 06:38:27,040 you were learning type or mcard 5980 06:38:27,040 --> 06:38:29,638 operations similarly you can implement 5981 06:38:29,638 --> 06:38:32,760 the update operation you can have you 5982 06:38:32,760 --> 06:38:34,160 first of all you have you have to Define 5983 06:38:34,160 --> 06:38:37,478 update function Here song model provide 5984 06:38:37,478 --> 06:38:40,600 the update method you can use that 5985 06:38:40,600 --> 06:38:42,920 method and then you got to create a 5986 06:38:42,920 --> 06:38:46,680 controller to handle the update 5987 06:38:47,638 --> 06:38:50,080 request in this lesson I'm going to 5988 06:38:50,080 --> 06:38:52,798 teach you the concept of 5989 06:38:52,798 --> 06:38:56,000 populate if you want to make a relation 5990 06:38:56,000 --> 06:38:59,160 or a reference between two document 5991 06:38:59,160 --> 06:39:02,360 between two collections remember in 5992 06:39:02,360 --> 06:39:04,760 mongodb 5993 06:39:04,760 --> 06:39:07,718 collection collection will have 5994 06:39:07,718 --> 06:39:11,240 documents in my SQL or post you will 5995 06:39:11,240 --> 06:39:13,398 have tables and 5996 06:39:13,398 --> 06:39:15,878 columns so if you want to make the 5997 06:39:15,878 --> 06:39:17,878 reference or relation between two 5998 06:39:17,878 --> 06:39:20,240 collection you can do with the help of 5999 06:39:20,240 --> 06:39:22,920 poate you have to implement the concept 6000 06:39:22,920 --> 06:39:24,360 of 6001 06:39:24,360 --> 06:39:27,920 poate I have this use case or scenario 6002 06:39:27,920 --> 06:39:31,680 each song must have must belong to one 6003 06:39:31,680 --> 06:39:36,718 album and each each album can have many 6004 06:39:36,718 --> 06:39:39,520 songs I want you to implement it if you 6005 06:39:39,520 --> 06:39:42,280 want to do that you can Google Mongo's 6006 06:39:42,280 --> 06:39:45,280 relation or check the official 6007 06:39:45,280 --> 06:39:47,478 [Music] 6008 06:39:47,478 --> 06:39:50,558 documentation this is our 6009 06:39:50,558 --> 06:39:54,798 album schema we did not create the album 6010 06:39:54,798 --> 06:39:57,000 schema yet I'm going to teach you how to 6011 06:39:57,000 --> 06:39:59,360 do that or you going to create or you 6012 06:39:59,360 --> 06:40:02,398 can create the album schema just like we 6013 06:40:02,398 --> 06:40:05,320 did the song schema scha it is going to 6014 06:40:05,320 --> 06:40:09,520 take the array of the songs because each 6015 06:40:09,520 --> 06:40:12,718 album can have many songs that's why I 6016 06:40:12,718 --> 06:40:15,760 created the array of songs and you can 6017 06:40:15,760 --> 06:40:17,958 have multiple 6018 06:40:17,958 --> 06:40:21,760 types it's an array I got the types from 6019 06:40:21,760 --> 06:40:25,440 mango package here you will see type 6020 06:40:25,440 --> 06:40:29,920 from the mangos and I'm telling that the 6021 06:40:29,920 --> 06:40:31,878 reference is 6022 06:40:31,878 --> 06:40:35,200 songs because when you create the song 6023 06:40:35,200 --> 06:40:38,040 when you created the song schema you 6024 06:40:38,040 --> 06:40:42,320 have defined a class for class song by 6025 06:40:42,320 --> 06:40:45,240 default the collection will be songs you 6026 06:40:45,240 --> 06:40:48,920 can also double check it from mongod DB 6027 06:40:48,920 --> 06:40:52,638 Compass let me show 6028 06:40:52,840 --> 06:40:55,718 you I'm telling you I would like to make 6029 06:40:55,718 --> 06:40:59,540 a reference with songs 6030 06:40:59,540 --> 06:41:00,680 [Music] 6031 06:41:00,680 --> 06:41:05,240 collection let me do that 6032 06:41:06,600 --> 06:41:09,280 inside the Spotify clone we have the 6033 06:41:09,280 --> 06:41:12,160 collection name which is songs I have 6034 06:41:12,160 --> 06:41:14,520 added the reference 6035 06:41:14,520 --> 06:41:18,080 here and finally you also have to add a 6036 06:41:18,080 --> 06:41:22,040 relation for this for the album in the 6037 06:41:22,040 --> 06:41:25,600 song schema I'm creating a property and 6038 06:41:25,600 --> 06:41:29,478 type should be Mango's object ID or 6039 06:41:29,478 --> 06:41:33,718 mongodb object ID here I'm I'm getting 6040 06:41:33,718 --> 06:41:36,920 the or defining the reference album. 6041 06:41:36,920 --> 06:41:39,718 name it should be 6042 06:41:39,718 --> 06:41:43,360 albums and we have the album and finally 6043 06:41:43,360 --> 06:41:46,680 you got to create album module just like 6044 06:41:46,680 --> 06:41:49,520 we did the song module and I'm saying I 6045 06:41:49,520 --> 06:41:52,558 want to use the album model in the album 6046 06:41:52,558 --> 06:41:56,040 service or we we will be able to inject 6047 06:41:56,040 --> 06:42:01,080 the album model to perform crud 6048 06:42:01,080 --> 06:42:04,040 operations and here I have injected the 6049 06:42:04,040 --> 06:42:06,878 album model in the album service just 6050 06:42:06,878 --> 06:42:11,680 like we did songs model inside the song 6051 06:42:11,680 --> 06:42:14,478 service and I created a new method we 6052 06:42:14,478 --> 06:42:18,718 will have a create album dto and here we 6053 06:42:18,718 --> 06:42:21,120 will have find 6054 06:42:21,120 --> 06:42:23,840 albums this is how you will implement 6055 06:42:23,840 --> 06:42:26,600 the populate let's say you want to 6056 06:42:26,600 --> 06:42:30,000 display all the songs on the front end 6057 06:42:30,000 --> 06:42:34,120 against each album then you need this 6058 06:42:34,120 --> 06:42:36,840 query you got to I'm telling to mongod 6059 06:42:36,840 --> 06:42:40,440 DB please give me all the albums please 6060 06:42:40,440 --> 06:42:43,398 give me all the albums for each Album 6061 06:42:43,398 --> 06:42:45,958 please also give me all the songs this 6062 06:42:45,958 --> 06:42:47,920 is how you going to do 6063 06:42:47,920 --> 06:42:51,040 it and create when you are trying to 6064 06:42:51,040 --> 06:42:53,320 create a new album you got to provide 6065 06:42:53,320 --> 06:42:57,398 the title and the songs it should be the 6066 06:42:57,398 --> 06:43:02,040 IDS and we have create method to create 6067 06:43:02,040 --> 06:43:04,360 a new album 6068 06:43:04,360 --> 06:43:08,080 and uh find all albums and you also have 6069 06:43:08,080 --> 06:43:11,000 to refactor create Song D you can 6070 06:43:11,000 --> 06:43:13,200 implement it I want you to do it I 6071 06:43:13,200 --> 06:43:15,240 already taught you all of these steps to 6072 06:43:15,240 --> 06:43:16,040 do 6073 06:43:16,040 --> 06:43:20,600 that first of all we have to 6074 06:43:20,600 --> 06:43:24,558 create album nextest 6075 06:43:24,558 --> 06:43:28,520 generate nextest generate module 6076 06:43:28,520 --> 06:43:31,200 albums or I can say 6077 06:43:31,200 --> 06:43:34,520 albums and next gener at 6078 06:43:34,520 --> 06:43:37,520 controller 6079 06:43:37,520 --> 06:43:44,240 album and Nest generate service 6080 06:43:57,798 --> 06:44:01,200 albums Nest generate 6081 06:44:01,200 --> 06:44:04,520 module albums 6082 06:44:04,520 --> 06:44:07,520 Nest generate 6083 06:44:07,840 --> 06:44:10,120 controller 6084 06:44:10,120 --> 06:44:14,718 albums Nest generate 6085 06:44:14,718 --> 06:44:17,718 service 6086 06:44:32,040 --> 06:44:35,040 albums 6087 06:44:37,680 --> 06:44:40,520 I don't need spec files I'm going to 6088 06:44:40,520 --> 06:44:43,000 delete 6089 06:44:44,840 --> 06:44:48,760 them now you have albums module albums 6090 06:44:48,760 --> 06:44:53,080 controller and album service it has also 6091 06:44:53,080 --> 06:44:56,040 added the entry in the app 6092 06:44:56,040 --> 06:45:01,040 module let's create a new folder 6093 06:45:01,160 --> 06:45:03,840 schemas I have to create create a new 6094 06:45:03,840 --> 06:45:08,200 schema I can name it to album 6095 06:45:08,440 --> 06:45:13,000 schema. we will also need a 6096 06:45:13,680 --> 06:45:16,398 dtos now we're going to I'm going to 6097 06:45:16,398 --> 06:45:19,160 copy album 6098 06:45:19,160 --> 06:45:22,080 schema and I'm going to paste it here 6099 06:45:22,080 --> 06:45:24,360 that's 6100 06:45:31,920 --> 06:45:33,878 it 6101 06:45:33,878 --> 06:45:35,120 and I can 6102 06:45:35,120 --> 06:45:40,958 use I also have to refactor the song 6103 06:45:54,638 --> 06:45:58,920 schema we have to add another property 6104 06:45:58,920 --> 06:46:02,920 one thing I forgot to add it should be a 6105 06:46:02,920 --> 06:46:07,200 mongodb property that's I forgot to 6106 06:46:07,200 --> 06:46:12,680 add add prop decorator for the lyrics 6107 06:46:14,200 --> 06:46:17,240 property we need to get the types from 6108 06:46:17,240 --> 06:46:18,718 object 6109 06:46:18,718 --> 06:46:20,878 ID from 6110 06:46:20,878 --> 06:46:24,920 mango we also have to import the 6111 06:46:24,920 --> 06:46:28,600 album from album 6112 06:46:29,200 --> 06:46:34,840 schema everything is good right now 6113 06:46:36,718 --> 06:46:38,520 let's 6114 06:46:38,520 --> 06:46:41,398 create we have to 6115 06:46:41,398 --> 06:46:43,878 update album 6116 06:46:43,878 --> 06:46:47,320 module actually we 6117 06:46:47,320 --> 06:46:52,020 need we need this line I'm going to copy 6118 06:46:52,020 --> 06:46:55,279 [Music] 6119 06:47:01,840 --> 06:47:04,840 everything 6120 06:47:31,840 --> 06:47:34,840 e 6121 06:48:00,280 --> 06:48:04,040 we don't have dto let's create 6122 06:48:04,040 --> 06:48:08,320 that create Alum 6123 06:48:17,440 --> 06:48:20,080 dto now we have to 6124 06:48:20,080 --> 06:48:22,680 define a 6125 06:48:22,680 --> 06:48:26,398 controller we already created the 6126 06:48:26,398 --> 06:48:28,680 controller I can 6127 06:48:28,680 --> 06:48:31,718 say should be albums 6128 06:48:31,718 --> 06:48:35,638 controller album 6129 06:48:44,440 --> 06:48:47,920 service and we also need to refactor the 6130 06:48:47,920 --> 06:48:50,638 create song 6131 06:49:01,718 --> 06:49:04,718 dto 6132 06:49:10,718 --> 06:49:14,558 I have found the error in the album 6133 06:49:14,558 --> 06:49:18,120 service get rid of the song Let's import 6134 06:49:18,120 --> 06:49:19,558 it 6135 06:49:19,558 --> 06:49:22,140 here if I save the 6136 06:49:22,140 --> 06:49:24,600 [Music] 6137 06:49:24,600 --> 06:49:27,478 application we also have to 6138 06:49:27,478 --> 06:49:31,638 refactor in the song 6139 06:49:31,638 --> 06:49:34,638 schema 6140 06:49:47,398 --> 06:49:49,120 everything is good 6141 06:49:49,120 --> 06:49:53,398 now what we what I want to do I would 6142 06:49:53,398 --> 06:49:55,798 like to create the album 6143 06:49:55,798 --> 06:49:58,360 first create 6144 06:49:58,360 --> 06:50:01,638 album we have to send the post 6145 06:50:01,638 --> 06:50:06,120 request by providing the 6146 06:50:07,160 --> 06:50:09,398 data it is going 6147 06:50:09,398 --> 06:50:12,958 to get give it is going to asking me the 6148 06:50:12,958 --> 06:50:14,520 name of the 6149 06:50:14,520 --> 06:50:18,520 album title title of the 6150 06:50:18,520 --> 06:50:21,360 album let's say 6151 06:50:21,360 --> 06:50:24,200 dance and I would like to provide the 6152 06:50:24,200 --> 06:50:25,878 array of the 6153 06:50:25,878 --> 06:50:29,280 songs I want to save let's 6154 06:50:29,280 --> 06:50:31,638 say these two 6155 06:50:31,638 --> 06:50:34,638 collections 6156 06:50:34,958 --> 06:50:36,760 these two 6157 06:50:36,760 --> 06:50:39,878 collections inside the dance inside the 6158 06:50:39,878 --> 06:50:40,910 dance 6159 06:50:40,910 --> 06:50:44,099 [Music] 6160 06:50:47,280 --> 06:50:52,878 album I'm also going to save this 6161 06:50:54,718 --> 06:50:58,638 document or save the song inside this 6162 06:50:58,638 --> 06:51:01,558 album let's create a new 6163 06:51:01,558 --> 06:51:03,120 album 6164 06:51:03,120 --> 06:51:05,840 I got the 6165 06:51:06,120 --> 06:51:10,600 eror song validation 6166 06:51:10,760 --> 06:51:15,520 failed duration duration is required 6167 06:51:15,520 --> 06:51:20,280 OMG I forgot to add the albums 6168 06:51:20,280 --> 06:51:23,600 here now you can see that I got the 6169 06:51:23,600 --> 06:51:26,958 dance album with these two 6170 06:51:26,958 --> 06:51:31,080 songs now we have to fetch all the songs 6171 06:51:31,080 --> 06:51:33,398 I'm going to test the popular method 6172 06:51:33,398 --> 06:51:35,200 popular 6173 06:51:35,200 --> 06:51:37,400 functionality find all 6174 06:51:37,400 --> 06:51:38,638 [Music] 6175 06:51:38,638 --> 06:51:41,920 albums with 6176 06:51:43,558 --> 06:51:48,280 songs this time we have to send the get 6177 06:51:49,878 --> 06:51:54,080 request let's fetch all the 6178 06:51:54,320 --> 06:51:57,600 albums you can see that I got the albums 6179 06:51:57,600 --> 06:52:01,120 array this is the first album and we 6180 06:52:01,120 --> 06:52:05,240 have all the songs against each album 6181 06:52:05,240 --> 06:52:08,360 this is how you can make a relation or 6182 06:52:08,360 --> 06:52:12,798 reference with document with 6183 06:52:13,360 --> 06:52:17,680 collections or with 6184 06:52:23,520 --> 06:52:26,360 models in this lesson we're going to 6185 06:52:26,360 --> 06:52:29,240 prepare our application for 6186 06:52:29,240 --> 06:52:31,798 deployment I'm going to teach you how to 6187 06:52:31,798 --> 06:52:35,360 deploy nestjs application at 6188 06:52:35,360 --> 06:52:38,280 Railway you will also deploy your 6189 06:52:38,280 --> 06:52:39,558 postgress 6190 06:52:39,558 --> 06:52:42,760 database at Railway we're going to 6191 06:52:42,760 --> 06:52:45,878 deploy our application this one the 6192 06:52:45,878 --> 06:52:49,398 Spotify clone which we were 6193 06:52:50,478 --> 06:52:54,240 building our application is using postp 6194 06:52:54,240 --> 06:52:57,000 database with type orm I'm going to 6195 06:52:57,000 --> 06:52:59,638 teach you how to deploy the complete 6196 06:52:59,638 --> 06:53:01,440 application at 6197 06:53:01,440 --> 06:53:03,040 sailway 6198 06:53:03,040 --> 06:53:06,798 first of all you got to create a new 6199 06:53:06,798 --> 06:53:09,798 project make sure you deploy your 6200 06:53:09,798 --> 06:53:12,878 project at GitHub 6201 06:53:12,878 --> 06:53:17,638 repository I did not deploy the project 6202 06:53:20,240 --> 06:53:23,680 yet first of all we have to do a do a 6203 06:53:23,680 --> 06:53:25,760 little bit 6204 06:53:25,760 --> 06:53:28,040 configuration we have to set our 6205 06:53:28,040 --> 06:53:31,200 environment variable when I will run the 6206 06:53:31,200 --> 06:53:34,478 application in production it should run 6207 06:53:34,478 --> 06:53:38,120 this production. EnV file if I'll be 6208 06:53:38,120 --> 06:53:40,280 working at development environment I 6209 06:53:40,280 --> 06:53:42,840 want to work with local post database I 6210 06:53:42,840 --> 06:53:45,840 don't want to change in production 6211 06:53:45,840 --> 06:53:50,398 level so we got to configure a couple of 6212 06:54:01,360 --> 06:54:05,320 settings we already created the env. 6213 06:54:05,320 --> 06:54:08,080 development. env. production but it is 6214 06:54:08,080 --> 06:54:11,160 not configur configurable when my 6215 06:54:11,160 --> 06:54:13,320 project is running in production mode it 6216 06:54:13,320 --> 06:54:17,718 should pick this production 6217 06:54:20,958 --> 06:54:24,360 file I've already written down the 6218 06:54:24,360 --> 06:54:28,680 validation in EnV validation for node 6219 06:54:28,680 --> 06:54:31,798 environment but I did not set in the 6220 06:54:31,798 --> 06:54:34,000 configuration 6221 06:54:34,000 --> 06:54:35,520 let me do 6222 06:54:35,520 --> 06:54:39,120 it I can say node 6223 06:54:39,120 --> 06:54:42,320 EnV and I can get the value 6224 06:54:42,320 --> 06:54:45,920 from process 6225 06:54:49,040 --> 06:54:54,840 dot process. env. node 6226 06:54:57,638 --> 06:55:01,638 EnV now this variable is available for 6227 06:55:01,638 --> 06:55:04,320 the config 6228 06:55:05,920 --> 06:55:08,680 service I've already provided you the 6229 06:55:08,680 --> 06:55:11,798 nests deployment raway started kit BR 6230 06:55:11,798 --> 06:55:14,840 started kit project you have to open it 6231 06:55:14,840 --> 06:55:19,760 and run npm install to install all the 6232 06:55:26,000 --> 06:55:28,520 dependencies now we have to configure 6233 06:55:28,520 --> 06:55:34,240 the EnV file path in app module 6234 06:55:34,440 --> 06:55:37,280 get rid of this 6235 06:55:37,280 --> 06:55:40,958 stuff I don't want to do 6236 06:55:40,958 --> 06:55:43,958 it 6237 06:55:52,878 --> 06:55:56,760 configure get it off this 6238 06:55:59,520 --> 06:56:04,240 middleware get rid of this data source 6239 06:56:17,920 --> 06:56:22,040 here we have to update our 6240 06:56:22,040 --> 06:56:25,840 path what I can 6241 06:56:26,440 --> 06:56:30,240 say please get the value 6242 06:56:30,240 --> 06:56:34,240 from process do 6243 06:56:35,840 --> 06:56:38,840 do 6244 06:56:40,000 --> 06:56:43,878 EnV do node 6245 06:56:46,638 --> 06:56:50,040 EnV or you can say get the path from the 6246 06:56:50,040 --> 06:56:52,840 current working directory and you can 6247 06:56:52,840 --> 06:56:55,360 provide the name of your 6248 06:56:55,360 --> 06:56:59,000 file current working 6249 06:56:59,000 --> 06:57:01,160 directory and 6250 06:57:01,160 --> 06:57:04,160 Slash 6251 06:57:05,080 --> 06:57:09,478 dot here we need to do EnV dot 6252 06:57:09,478 --> 06:57:12,798 development or 6253 06:57:23,600 --> 06:57:28,440 production we have to remove this 6254 06:57:31,120 --> 06:57:34,120 one 6255 06:57:39,080 --> 06:57:41,520 please get the working 6256 06:57:41,520 --> 06:57:45,958 directory and find the env. production 6257 06:57:45,958 --> 06:57:50,360 file or env. development 6258 06:57:50,360 --> 06:57:53,878 file we have to set the script or we 6259 06:57:53,878 --> 06:57:55,680 have to set the development environment 6260 06:57:55,680 --> 06:57:59,558 in our package.json file for the dev I 6261 06:57:59,558 --> 06:58:06,000 can say I want to mention node EnV to 6262 06:58:06,600 --> 06:58:10,520 development we have to do 6263 06:58:15,040 --> 06:58:19,638 it here we have to set the production 6264 06:58:19,638 --> 06:58:24,440 value when you run the application with 6265 06:58:27,718 --> 06:58:30,760 production how can I test 6266 06:58:30,760 --> 06:58:36,040 it in the main.ts file maybe I can use 6267 06:58:36,040 --> 06:58:42,040 that or what can I do yeah I have the 6268 06:58:42,040 --> 06:58:44,600 access to config 6269 06:58:44,600 --> 06:58:48,558 service I can say console 6270 06:58:48,558 --> 06:58:50,798 DOT 6271 06:58:50,798 --> 06:58:52,360 log 6272 06:58:52,360 --> 06:58:54,160 config 6273 06:58:54,160 --> 06:58:57,160 service 6274 06:58:57,520 --> 06:59:00,120 doget I can provide the name which is 6275 06:59:00,120 --> 06:59:01,040 not 6276 06:59:01,040 --> 06:59:02,680 EnV 6277 06:59:02,680 --> 06:59:04,760 let me part it to 6278 06:59:04,760 --> 06:59:08,680 string let's see what will 6279 06:59:19,680 --> 06:59:22,760 happen you can see that it's working 6280 06:59:22,760 --> 06:59:26,240 fine now my application is running in 6281 06:59:26,240 --> 06:59:29,120 the development 6282 06:59:29,120 --> 06:59:31,718 environment if I run the application in 6283 06:59:31,718 --> 06:59:33,840 produ prodction 6284 06:59:33,840 --> 06:59:36,600 environment when you see the production. 6285 06:59:36,600 --> 06:59:39,040 environment I don't set the I didn't set 6286 06:59:39,040 --> 06:59:41,280 the database configuration so it will 6287 06:59:41,280 --> 06:59:43,958 not work my application will be crashed 6288 06:59:43,958 --> 06:59:46,680 let me show 6289 06:59:58,280 --> 07:00:01,440 you you can see that my application has 6290 07:00:01,440 --> 07:00:03,440 crashed 6291 07:00:03,440 --> 07:00:08,520 an instance of environment variable has 6292 07:00:30,920 --> 07:00:33,798 failed 6293 07:00:33,798 --> 07:00:37,200 yeah it is showing me the validation 6294 07:00:37,200 --> 07:00:41,638 error dbport does not exist DB host does 6295 07:00:41,638 --> 07:00:46,280 not exist if I copy this 6296 07:00:46,280 --> 07:00:49,320 configuration what will 6297 07:00:49,320 --> 07:00:53,798 happen now it should run the 6298 07:00:59,200 --> 07:01:01,398 application you can see that my 6299 07:01:01,398 --> 07:01:05,040 application is working fine 6300 07:01:07,958 --> 07:01:10,958 now but we will replace these 6301 07:01:10,958 --> 07:01:16,398 configurations with Railway post 6302 07:01:24,680 --> 07:01:27,638 configuration I have everything in the 6303 07:01:27,638 --> 07:01:29,398 master 6304 07:01:29,398 --> 07:01:33,160 Branch let's create the GitHub 6305 07:01:33,160 --> 07:01:35,840 repository because Railway will be 6306 07:01:35,840 --> 07:01:39,638 asking for GitHub 6307 07:01:50,040 --> 07:01:53,080 repository let's create a new repository 6308 07:01:53,080 --> 07:01:55,680 I can give any 6309 07:01:55,680 --> 07:02:00,320 name nextjs deploy to 6310 07:02:00,840 --> 07:02:03,840 railway 6311 07:02:18,680 --> 07:02:21,478 if you want you can create private 6312 07:02:21,478 --> 07:02:25,320 repository I'm going to make it 6313 07:02:29,600 --> 07:02:32,558 public we have to add 6314 07:02:32,558 --> 07:02:36,398 origin it is asking me to create push as 6315 07:02:36,398 --> 07:02:38,160 a main 6316 07:02:38,160 --> 07:02:41,680 branch let's do 6317 07:02:42,520 --> 07:02:46,240 that so I got the main branch everything 6318 07:02:46,240 --> 07:02:49,040 inside the main 6319 07:02:53,478 --> 07:02:58,000 branch let's try to check it 6320 07:02:59,320 --> 07:03:04,638 out so I have everything in the main 6321 07:03:05,040 --> 07:03:09,320 branch now we 6322 07:03:09,878 --> 07:03:13,160 have we have EnV doev development we 6323 07:03:13,160 --> 07:03:15,440 have env. 6324 07:03:15,440 --> 07:03:19,558 production you should not push your env. 6325 07:03:19,558 --> 07:03:23,080 development or env. production to the 6326 07:03:23,080 --> 07:03:25,558 GitHub 6327 07:03:29,520 --> 07:03:32,200 repository because we wanted to set it 6328 07:03:32,200 --> 07:03:35,040 to the kit 6329 07:03:35,040 --> 07:03:38,840 ignore do not add 6330 07:03:38,840 --> 07:03:41,840 Dov 6331 07:03:42,558 --> 07:03:46,920 files if I add. 6332 07:03:51,680 --> 07:03:56,000 EnV update get 6333 07:04:00,718 --> 07:04:03,718 ignore 6334 07:04:04,920 --> 07:04:08,120 we have to push the 6335 07:04:15,958 --> 07:04:22,958 code I try to add do EnV in the G ignore 6336 07:04:22,958 --> 07:04:25,558 edit I try to 6337 07:04:25,558 --> 07:04:29,320 add EnV in the 6338 07:04:29,320 --> 07:04:32,798 ignore and it was not not 6339 07:04:32,798 --> 07:04:36,280 working I also removed the cache for 6340 07:04:36,280 --> 07:04:41,798 env. development and env. production 6341 07:04:45,160 --> 07:04:46,798 file 6342 07:04:46,798 --> 07:04:49,798 removed 6343 07:04:50,080 --> 07:04:54,160 envs now we have to push 6344 07:04:55,200 --> 07:04:58,200 it 6345 07:04:59,200 --> 07:05:03,638 oops envs 6346 07:05:06,520 --> 07:05:09,558 let's push the 6347 07:05:30,638 --> 07:05:33,638 code 6348 07:05:37,080 --> 07:05:40,638 let me delete this file 6349 07:05:47,440 --> 07:05:51,430 manually I'm also going to delete env. 6350 07:05:51,430 --> 07:05:54,499 [Music] 6351 07:06:00,558 --> 07:06:03,558 production 6352 07:06:11,240 --> 07:06:14,320 now we got to pull the 6353 07:06:18,280 --> 07:06:22,478 code everything is up to date right now 6354 07:06:22,478 --> 07:06:24,000 if I try to 6355 07:06:24,000 --> 07:06:26,520 push you should not 6356 07:06:26,520 --> 07:06:30,920 see env. production and EnV do 6357 07:06:30,920 --> 07:06:33,520 development 6358 07:06:38,398 --> 07:06:41,958 file so we have pushed the source code 6359 07:06:41,958 --> 07:06:44,200 to our GitHub repository you going to 6360 07:06:44,200 --> 07:06:46,958 say deply from the GitHub 6361 07:06:46,958 --> 07:06:51,320 repository here we have to configure 6362 07:06:51,320 --> 07:06:57,120 our repository I can say deploy njs app 6363 07:06:57,120 --> 07:07:00,240 to I can say now it is going to deploy 6364 07:07:00,240 --> 07:07:03,520 the code 6365 07:07:09,160 --> 07:07:10,200 it is 6366 07:07:10,200 --> 07:07:14,120 building and running env. production no 6367 07:07:14,120 --> 07:07:17,638 it is trying to building the 6368 07:07:30,440 --> 07:07:33,000 project 6369 07:07:33,000 --> 07:07:37,558 now you can see that our project has 6370 07:07:37,558 --> 07:07:41,120 deployed if you want you can see the log 6371 07:07:41,120 --> 07:07:44,160 what is going on 6372 07:07:46,080 --> 07:07:49,160 here I got this 6373 07:07:49,160 --> 07:07:52,360 error instance of environment variables 6374 07:07:52,360 --> 07:07:55,120 has failed in the valid 6375 07:07:55,120 --> 07:07:58,040 validation that's an issue we have to 6376 07:07:58,040 --> 07:08:03,440 configure our environment variables 6377 07:08:06,440 --> 07:08:10,718 our application has crashed now we have 6378 07:08:10,718 --> 07:08:13,718 to set the environment 6379 07:08:13,718 --> 07:08:17,798 variables first of all I have 6380 07:08:17,798 --> 07:08:21,478 to create a new database it's very easy 6381 07:08:21,478 --> 07:08:25,240 to create a database in real 6382 07:08:25,240 --> 07:08:30,440 way just right click and find the 6383 07:08:30,440 --> 07:08:35,040 database I'm going to have 6384 07:08:41,120 --> 07:08:45,320 post now you can have the 6385 07:08:47,840 --> 07:08:51,040 post when you see the connect here you 6386 07:08:51,040 --> 07:08:55,160 can find your database 6387 07:08:55,160 --> 07:08:59,440 option I can say this is 6388 07:08:59,440 --> 07:09:03,520 post and you can up create your EnV 6389 07:09:03,520 --> 07:09:06,160 production file 6390 07:09:06,160 --> 07:09:07,660 EnV 6391 07:09:07,660 --> 07:09:10,979 [Music] 6392 07:09:11,840 --> 07:09:16,320 production oops I deleted that 6393 07:09:23,040 --> 07:09:26,000 files we need to get back our 6394 07:09:26,000 --> 07:09:29,558 development. EnV 6395 07:09:30,320 --> 07:09:35,240 file do env. 6396 07:09:36,920 --> 07:09:40,360 development and I also need to get 6397 07:09:40,360 --> 07:09:43,478 production EnV 6398 07:09:47,320 --> 07:09:51,320 file now I'm going to copy my 6399 07:09:51,320 --> 07:09:54,798 database in the 6400 07:09:55,200 --> 07:09:59,200 production let's do it 6401 07:10:00,320 --> 07:10:03,320 here 6402 07:10:08,490 --> 07:10:11,559 [Music] 6403 07:10:11,798 --> 07:10:13,270 database this 6404 07:10:13,270 --> 07:10:14,478 [Music] 6405 07:10:14,478 --> 07:10:18,760 one and we need PG 6406 07:10:19,398 --> 07:10:22,600 Host this 6407 07:10:27,520 --> 07:10:31,320 one and we need password I want you to 6408 07:10:31,320 --> 07:10:35,798 use your own password do not copy my 6409 07:10:37,040 --> 07:10:40,040 password 6410 07:10:45,280 --> 07:10:48,958 username it should be post it's working 6411 07:10:48,958 --> 07:10:53,320 fine you also need a 6412 07:10:54,760 --> 07:10:59,120 port everything is good to go 6413 07:11:00,240 --> 07:11:03,240 now 6414 07:11:05,680 --> 07:11:10,320 now I want you to copy these EnV file 6415 07:11:10,320 --> 07:11:12,718 the your production EnV 6416 07:11:12,718 --> 07:11:16,718 file and open the N nestjs deploy to 6417 07:11:16,718 --> 07:11:17,920 raway 6418 07:11:17,920 --> 07:11:21,280 project and in the in in the variables 6419 07:11:21,280 --> 07:11:24,120 you have to set your environment 6420 07:11:24,120 --> 07:11:27,360 variables that's it I'm going to update 6421 07:11:27,360 --> 07:11:30,920 my variables now it is going to redeploy 6422 07:11:30,920 --> 07:11:33,240 the app 6423 07:11:40,760 --> 07:11:43,200 application now redeployment is in 6424 07:11:43,200 --> 07:11:46,240 progress It's Time to it's it will take 6425 07:11:46,240 --> 07:11:48,558 some 6426 07:11:53,798 --> 07:11:57,638 time my application has deplo 6427 07:11:57,638 --> 07:12:01,680 deployed now it is going to start let's 6428 07:12:01,680 --> 07:12:03,638 look at the deployment 6429 07:12:03,638 --> 07:12:07,120 logs amazing I I was logging the 6430 07:12:07,120 --> 07:12:09,798 production in the app module or I think 6431 07:12:09,798 --> 07:12:12,798 I was doing in the main.ts file you 6432 07:12:12,798 --> 07:12:14,680 should not do 6433 07:12:14,680 --> 07:12:17,398 that it's working fine it means we 6434 07:12:17,398 --> 07:12:20,878 successfully deployed our 6435 07:12:21,398 --> 07:12:24,920 application so how can you test it maybe 6436 07:12:24,920 --> 07:12:26,760 you had thinking about it how can I test 6437 07:12:26,760 --> 07:12:29,638 this application right now we don't have 6438 07:12:29,638 --> 07:12:32,080 any domain let's generate domain to test 6439 07:12:32,080 --> 07:12:34,280 the 6440 07:12:34,280 --> 07:12:37,040 application uh if you want to refactor 6441 07:12:37,040 --> 07:12:41,000 you can change that I can say Spotify 6442 07:12:41,000 --> 07:12:44,000 clone 6443 07:12:47,478 --> 07:12:49,920 production now we have this domain name 6444 07:12:49,920 --> 07:12:51,680 I'm going to open 6445 07:12:51,680 --> 07:12:54,398 it it is going to give me the nothing 6446 07:12:54,398 --> 07:12:56,840 here don't worry when you try to access 6447 07:12:56,840 --> 07:12:58,440 the API 6448 07:12:58,440 --> 07:13:01,398 routes it will take some time because we 6449 07:13:01,398 --> 07:13:04,440 have set the domain 6450 07:13:06,840 --> 07:13:10,920 name it is going to take some 6451 07:13:19,080 --> 07:13:23,440 time now it's working I think fine 6452 07:13:23,440 --> 07:13:29,718 now after 30 seconds my project has 6453 07:13:30,040 --> 07:13:33,040 Reloaded 6454 07:13:35,320 --> 07:13:36,840 now we're going to figure it out I'm 6455 07:13:36,840 --> 07:13:39,840 going to send the API request to test 6456 07:13:39,840 --> 07:13:44,240 the application it is working fine now 6457 07:13:44,240 --> 07:13:47,200 but what will happen if I try to create 6458 07:13:47,200 --> 07:13:50,440 a new user let's say I want to perform 6459 07:13:50,440 --> 07:13:53,240 database operation I'm sure it is going 6460 07:13:53,240 --> 07:13:56,000 to give me the 6461 07:13:57,320 --> 07:14:00,040 error let's create a new 6462 07:14:00,040 --> 07:14:03,040 user 6463 07:14:06,558 --> 07:14:08,920 Jane 6464 07:14:08,920 --> 07:14:12,798 Don Jan 6465 07:14:13,000 --> 07:14:15,160 gmail.com 1 6466 07:14:15,160 --> 07:14:18,680 2 3 4 6467 07:14:18,680 --> 07:14:22,760 56 I'm going to send the 6468 07:14:23,398 --> 07:14:26,840 request I got internal server 6469 07:14:26,840 --> 07:14:31,398 error you can check the log what what is 6470 07:14:31,398 --> 07:14:33,878 going 6471 07:14:38,600 --> 07:14:42,958 on users does not 6472 07:14:42,958 --> 07:14:48,798 exist because my database is empty now 6473 07:14:48,798 --> 07:14:51,398 we did not implement the migration 6474 07:14:51,398 --> 07:14:54,280 remember you have seen in the data 6475 07:14:54,280 --> 07:14:57,200 source we have set this synchronized to 6476 07:14:57,200 --> 07:15:00,718 false but do not use synchronize to true 6477 07:15:00,718 --> 07:15:02,080 in 6478 07:15:02,080 --> 07:15:04,760 production what we have to do we have to 6479 07:15:04,760 --> 07:15:08,200 run our migrations to perform changes we 6480 07:15:08,200 --> 07:15:10,718 have to add relations because I done 6481 07:15:10,718 --> 07:15:14,160 that I've done 6482 07:15:15,958 --> 07:15:19,000 it I think I don't want phone migration 6483 07:15:19,000 --> 07:15:21,878 you can delete 6484 07:15:23,160 --> 07:15:24,878 that 6485 07:15:24,878 --> 07:15:29,080 now I have to execute these queries 6486 07:15:29,080 --> 07:15:32,280 these queries will create table 6487 07:15:32,280 --> 07:15:37,040 in our postrest database here 6488 07:15:37,040 --> 07:15:39,520 here what we have to do we got to run 6489 07:15:39,520 --> 07:15:41,680 the 6490 07:15:46,680 --> 07:15:51,360 migrations I got this issue we have to 6491 07:15:51,360 --> 07:15:54,638 migrate the database in the production 6492 07:15:54,638 --> 07:15:57,840 so we have to use a 6493 07:15:57,840 --> 07:16:01,000 migration make sure you are using the 6494 07:16:01,000 --> 07:16:04,680 password process. env. password not a DB 6495 07:16:04,680 --> 07:16:08,280 password because we have defined the 6496 07:16:08,280 --> 07:16:13,240 password in our environment 6497 07:16:19,520 --> 07:16:23,000 file we have to run the migration I'm 6498 07:16:23,000 --> 07:16:23,958 going 6499 07:16:23,958 --> 07:16:28,280 to run the script npm run migration I 6500 07:16:28,280 --> 07:16:30,398 would like to create all my tables 6501 07:16:30,398 --> 07:16:35,760 that's why I just provided the name in 6502 07:16:40,320 --> 07:16:44,200 it so I got the 6503 07:16:44,200 --> 07:16:47,040 undefined 6504 07:16:47,040 --> 07:16:50,120 process. I'm doing the log for node 6505 07:16:50,120 --> 07:16:52,798 environment I'm also doing the log for 6506 07:16:52,798 --> 07:16:57,360 DB DB host I'm also doing the log for DB 6507 07:16:57,360 --> 07:16:59,958 password you can see that I got the 6508 07:16:59,958 --> 07:17:03,600 development value it is working fine but 6509 07:17:03,600 --> 07:17:06,240 when you look at the env. development 6510 07:17:06,240 --> 07:17:08,120 I'm using the database for the Local 6511 07:17:08,120 --> 07:17:11,478 Host like post gra local instance here 6512 07:17:11,478 --> 07:17:14,398 I'm using the railway postra 6513 07:17:14,398 --> 07:17:18,798 instance so I have to tell to nestjs 6514 07:17:18,798 --> 07:17:20,120 build 6515 07:17:20,120 --> 07:17:23,478 command under the they are using 6516 07:17:23,478 --> 07:17:26,440 generate command is using njs build 6517 07:17:26,440 --> 07:17:30,240 command this one type orm but I want to 6518 07:17:30,240 --> 07:17:33,120 say please run the produ run the project 6519 07:17:33,120 --> 07:17:36,160 in the production let if you want to run 6520 07:17:36,160 --> 07:17:39,760 the de in migration in production you 6521 07:17:39,760 --> 07:17:44,360 have to Pro tell njs project you can 6522 07:17:44,360 --> 07:17:47,920 create a separate script for build for 6523 07:17:47,920 --> 07:17:50,478 production or separate SCP for separate 6524 07:17:50,478 --> 07:17:54,798 script for build in the development 6525 07:17:54,798 --> 07:17:58,040 environment but you can do quickly by 6526 07:17:58,040 --> 07:17:59,760 using say that 6527 07:17:59,760 --> 07:18:03,718 export node EnV here I want to set the 6528 07:18:03,718 --> 07:18:06,798 node environment variable to production 6529 07:18:06,798 --> 07:18:09,638 now you can see development environment 6530 07:18:09,638 --> 07:18:11,958 should be in the 6531 07:18:11,958 --> 07:18:15,920 production I'm going to run the 6532 07:18:16,160 --> 07:18:18,760 migration I've also logged the node 6533 07:18:18,760 --> 07:18:20,440 environment it should give me the 6534 07:18:20,440 --> 07:18:22,920 production by default 6535 07:18:22,920 --> 07:18:26,240 it if it if it logs 6536 07:18:26,240 --> 07:18:28,638 production it means it is going to run 6537 07:18:28,638 --> 07:18:32,080 the production EnV 6538 07:18:32,080 --> 07:18:34,280 this 6539 07:18:35,280 --> 07:18:38,760 one you can see node environment is 6540 07:18:38,760 --> 07:18:41,680 production but I got DB host is empty 6541 07:18:41,680 --> 07:18:45,120 undefined and password I figured it out 6542 07:18:45,120 --> 07:18:47,958 the issue I have to 6543 07:18:47,958 --> 07:18:50,878 install do EnV 6544 07:18:50,878 --> 07:18:54,440 package and here we have to use the EnV 6545 07:18:54,440 --> 07:18:57,520 package it is not getting variables from 6546 07:18:57,520 --> 07:18:59,798 EnV because we were using the config 6547 07:18:59,798 --> 07:19:02,718 module for config 6548 07:19:05,000 --> 07:19:09,440 configuration after installing config 6549 07:19:09,440 --> 07:19:12,280 module do EnV 6550 07:19:12,280 --> 07:19:15,440 package you have to import it 6551 07:19:15,440 --> 07:19:19,440 here do en/ config now you should be 6552 07:19:19,440 --> 07:19:23,000 able to access these variables 6553 07:19:23,000 --> 07:19:26,878 so I'm going to run the 6554 07:19:29,638 --> 07:19:32,638 migration 6555 07:19:34,080 --> 07:19:37,680 before running the build you have to set 6556 07:19:37,680 --> 07:19:40,080 the node envirment do you want to run it 6557 07:19:40,080 --> 07:19:41,798 for production or do you want to run it 6558 07:19:41,798 --> 07:19:44,440 for development but if you want you can 6559 07:19:44,440 --> 07:19:47,000 create a separate script SE separate 6560 07:19:47,000 --> 07:19:51,520 build script for production and 6561 07:19:56,000 --> 07:19:58,520 development I still got the 6562 07:19:58,520 --> 07:20:02,638 eror production 6563 07:20:03,040 --> 07:20:07,000 it's not getting the 6564 07:20:09,600 --> 07:20:14,000 value I think it is not finding the env. 6565 07:20:14,000 --> 07:20:16,920 development 6566 07:20:19,958 --> 07:20:25,240 file so there was an issue it is not 6567 07:20:25,240 --> 07:20:29,080 loading the EnV from the env. 6568 07:20:29,080 --> 07:20:31,160 development. env. 6569 07:20:31,160 --> 07:20:35,600 production if we will have simple. EnV 6570 07:20:35,600 --> 07:20:38,760 then EnV package can get variables from 6571 07:20:38,760 --> 07:20:41,718 the file but right now it is not getting 6572 07:20:41,718 --> 07:20:44,160 it so you have to provide the custom 6573 07:20:44,160 --> 07:20:46,840 path I just did a little bit 6574 07:20:46,840 --> 07:20:51,160 configuration we have to import the 6575 07:20:58,600 --> 07:21:02,558 path we have import the 6576 07:21:09,760 --> 07:21:13,200 part so we have to 6577 07:21:13,200 --> 07:21:16,000 use this 6578 07:21:16,000 --> 07:21:19,000 syntax 6579 07:21:20,558 --> 07:21:23,798 require this 6580 07:21:24,718 --> 07:21:29,520 one all I have to do is provide the EnV 6581 07:21:29,520 --> 07:21:32,520 path 6582 07:21:33,920 --> 07:21:38,240 now it should work it should get the 6583 07:21:38,240 --> 07:21:42,760 values so I'm going to create the 6584 07:21:45,200 --> 07:21:49,200 migration cannot read 6585 07:21:53,040 --> 07:21:55,040 property 6586 07:21:55,040 --> 07:21:59,520 PA is equals to 6587 07:21:59,520 --> 07:22:02,520 path 6588 07:22:03,120 --> 07:22:06,600 but I got still 6589 07:22:09,718 --> 07:22:14,280 undefined let's log EnV 6590 07:22:17,120 --> 07:22:22,120 path oh it is getting the value 6591 07:22:22,120 --> 07:22:23,760 from 6592 07:22:23,760 --> 07:22:28,200 DB but my file is located 6593 07:22:28,200 --> 07:22:34,280 at root folder so it is not here inside 6594 07:22:34,280 --> 07:22:36,558 the 6595 07:22:40,360 --> 07:22:44,478 DB if I try this 6596 07:22:47,200 --> 07:22:50,798 one I tried to work with this env. 6597 07:22:50,798 --> 07:22:53,120 production with custom path but it is 6598 07:22:53,120 --> 07:22:54,240 not 6599 07:22:54,240 --> 07:22:58,280 working all I did if I 6600 07:22:58,280 --> 07:23:01,478 change EnV it is it will work let's let 6601 07:23:01,478 --> 07:23:02,440 me show 6602 07:23:02,440 --> 07:23:04,680 you get rid of 6603 07:23:04,680 --> 07:23:08,040 [Music] 6604 07:23:08,398 --> 07:23:15,080 everything now if I copy that and 6605 07:23:16,558 --> 07:23:19,958 create EnV file in the root directory it 6606 07:23:19,958 --> 07:23:21,318 will 6607 07:23:21,318 --> 07:23:24,718 work I found there is an issue with type 6608 07:23:24,718 --> 07:23:28,080 orm now I'm going to paste my 6609 07:23:28,080 --> 07:23:31,040 code for production 6610 07:23:31,040 --> 07:23:32,718 you you have 6611 07:23:32,718 --> 07:23:36,958 to adjust nestjs build when you try to 6612 07:23:36,958 --> 07:23:39,718 run the migration you have to update the 6613 07:23:39,718 --> 07:23:43,398 changes here I found this is the way you 6614 07:23:43,398 --> 07:23:46,120 can implement it but there was an issue 6615 07:23:46,120 --> 07:23:49,718 with custom file path so now it will 6616 07:23:49,718 --> 07:23:53,160 work I'm going to run the 6617 07:23:55,760 --> 07:23:59,920 migration now I can see production and 6618 07:23:59,920 --> 07:24:03,798 the my container name which is my DB 6619 07:24:03,798 --> 07:24:09,520 host and my password now migrations has 6620 07:24:09,520 --> 07:24:11,398 been generated 6621 07:24:11,398 --> 07:24:13,680 successfully it's time to run the 6622 07:24:13,680 --> 07:24:17,360 migration we got to run this 6623 07:24:19,718 --> 07:24:22,638 command npm 6624 07:24:22,638 --> 07:24:25,638 run 6625 07:24:26,040 --> 07:24:29,040 migration 6626 07:24:29,318 --> 07:24:33,200 run should be 6627 07:24:37,000 --> 07:24:40,040 run you can see that it is running all 6628 07:24:40,040 --> 07:24:43,638 the queries creating tables in the 6629 07:24:43,638 --> 07:24:46,718 railway postris 6630 07:24:50,200 --> 07:24:52,600 database now you can understand you can 6631 07:24:52,600 --> 07:24:56,798 use this EnV for type orm 6632 07:24:56,798 --> 07:25:02,120 build use it for type or RM 6633 07:25:02,120 --> 07:25:04,920 migrations because in the app module we 6634 07:25:04,920 --> 07:25:07,600 are using the configuration of config 6635 07:25:07,600 --> 07:25:11,760 module we are not loading this EnV file 6636 07:25:11,760 --> 07:25:13,760 we are loading envir environment 6637 07:25:13,760 --> 07:25:17,360 development env. production file but you 6638 07:25:17,360 --> 07:25:21,040 can use this EnV for 6639 07:25:21,440 --> 07:25:25,638 migrations now we need to double check 6640 07:25:29,240 --> 07:25:32,240 it 6641 07:25:35,798 --> 07:25:39,240 we have to deply our 6642 07:25:42,240 --> 07:25:45,878 code now it will take some time to 6643 07:25:45,878 --> 07:25:49,080 redeploy the 6644 07:25:51,958 --> 07:25:54,840 application the application is running 6645 07:25:54,840 --> 07:26:00,798 now I got this secret key the database 6646 07:26:00,798 --> 07:26:03,398 host and the 6647 07:26:03,398 --> 07:26:07,440 password now we're going to run 6648 07:26:09,120 --> 07:26:13,680 it/ API open this Swagger 6649 07:26:16,040 --> 07:26:19,760 tool you can also double check it from 6650 07:26:19,760 --> 07:26:21,478 postris 6651 07:26:21,478 --> 07:26:25,840 database I have only migration table it 6652 07:26:25,840 --> 07:26:29,718 should give me all the tables but 6653 07:26:29,718 --> 07:26:32,240 something went wrong 6654 07:26:32,240 --> 07:26:35,280 here inside the 6655 07:26:35,280 --> 07:26:41,160 migration the record is empty table is 6656 07:26:41,798 --> 07:26:46,718 empty unique key primary key what is 6657 07:26:46,718 --> 07:26:49,160 going 6658 07:26:57,840 --> 07:27:00,798 on oh I have two migr 6659 07:27:00,798 --> 07:27:04,760 ations get rid of this 6660 07:27:09,958 --> 07:27:14,360 file get rid of all these 6661 07:27:15,318 --> 07:27:18,040 migrations let's generate the migrations 6662 07:27:18,040 --> 07:27:19,760 from the 6663 07:27:19,760 --> 07:27:23,920 scratch I'm going to name it to in 6664 07:27:27,600 --> 07:27:32,718 it now migration has R it 6665 07:27:33,120 --> 07:27:37,318 successfully I'm going to comment this 6666 07:27:40,120 --> 07:27:44,478 code now you have to run the 6667 07:27:50,398 --> 07:27:53,440 migration everything is good to go all 6668 07:27:53,440 --> 07:27:56,638 the ques has have 6669 07:27:56,638 --> 07:27:59,680 executed now you should see the data in 6670 07:27:59,680 --> 07:28:02,680 the data datase you can see I got artist 6671 07:28:02,680 --> 07:28:05,760 playlist songs and users 6672 07:28:05,760 --> 07:28:08,638 table we have these 6673 07:28:08,638 --> 07:28:11,718 columns if you want you can run the 6674 07:28:11,718 --> 07:28:15,878 seed from App module I think we we were 6675 07:28:15,878 --> 07:28:19,718 doing seeding in main.ts 6676 07:28:19,958 --> 07:28:23,718 file you can enable this 6677 07:28:23,718 --> 07:28:26,760 code application is running 6678 07:28:26,760 --> 07:28:32,040 now let's create a new user 6679 07:28:35,160 --> 07:28:38,878 amazing we have created the record 6680 07:28:38,878 --> 07:28:41,440 successfully you can also double check 6681 07:28:41,440 --> 07:28:43,398 it from the post 6682 07:28:43,398 --> 07:28:47,240 database you will have the user 6683 07:28:47,240 --> 07:28:51,200 here but I created this user 6684 07:28:51,200 --> 07:28:55,318 from the this one from seeding because I 6685 07:28:55,318 --> 07:28:58,840 think my seeds are running 6686 07:28:58,840 --> 07:29:02,680 constantly I think I enable that seed in 6687 07:29:02,680 --> 07:29:05,680 the app module we got to disable 6688 07:29:05,680 --> 07:29:11,558 it otherwise it will generate more 6689 07:29:11,558 --> 07:29:16,478 users I'm going to remove them now you 6690 07:29:16,478 --> 07:29:21,958 have to see constantly a new 6691 07:29:23,160 --> 07:29:27,318 data I can say disabled 6692 07:29:29,040 --> 07:29:33,760 seats now it is going to redeploy the 6693 07:29:33,760 --> 07:29:37,520 application so this is how you can 6694 07:29:37,520 --> 07:29:42,398 deploy nestjs application to 6695 07:29:46,000 --> 07:29:48,600 railway in this module I'm going to 6696 07:29:48,600 --> 07:29:51,280 teach you how to do unit testing 6697 07:29:51,280 --> 07:29:53,680 integration testing and end to end 6698 07:29:53,680 --> 07:29:55,798 testing for nestjs 6699 07:29:55,798 --> 07:29:58,280 application before getting started on 6700 07:29:58,280 --> 07:30:00,120 testing I want to teach you some of the 6701 07:30:00,120 --> 07:30:02,760 basics of just because we're going to 6702 07:30:02,760 --> 07:30:04,920 write a lot of mock functions we're 6703 07:30:04,920 --> 07:30:06,878 going to write we're going to write a 6704 07:30:06,878 --> 07:30:09,200 lot of spying spy 6705 07:30:09,200 --> 07:30:12,360 functions you will learn how to create 6706 07:30:12,360 --> 07:30:15,478 Mo implementation of service of a 6707 07:30:15,478 --> 07:30:17,878 controller of a 6708 07:30:17,878 --> 07:30:20,160 repository if you know the basics of 6709 07:30:20,160 --> 07:30:22,280 justest if you know mo functions if you 6710 07:30:22,280 --> 07:30:24,440 know spy function then you can skip 6711 07:30:24,440 --> 07:30:25,520 these three 6712 07:30:25,520 --> 07:30:27,718 lessons if you want to refresh the 6713 07:30:27,718 --> 07:30:32,840 knowledge of just you can continue this 6714 07:30:35,318 --> 07:30:38,878 video just as you if you don't know 6715 07:30:38,878 --> 07:30:42,040 about just just is a popular JavaScript 6716 07:30:42,040 --> 07:30:45,080 framework it is developed by 6717 07:30:45,080 --> 07:30:48,638 Facebook you can use for testing 6718 07:30:48,638 --> 07:30:50,120 JavaScript 6719 07:30:50,120 --> 07:30:53,398 application you can use just with react 6720 07:30:53,398 --> 07:30:56,638 view angular and 6721 07:30:57,718 --> 07:31:00,478 nodejs these are all the function alties 6722 07:31:00,478 --> 07:31:03,160 of the Gest asynchronous testing code 6723 07:31:03,160 --> 07:31:05,478 coverage snapshot testing moing and 6724 07:31:05,478 --> 07:31:07,120 spying we're going to use a lot of 6725 07:31:07,120 --> 07:31:10,760 moking and spine with nestjs that's why 6726 07:31:10,760 --> 07:31:13,120 I want to teach you these two concepts 6727 07:31:13,120 --> 07:31:14,160 in more 6728 07:31:14,160 --> 07:31:17,080 details you will learn how to do matches 6729 07:31:17,080 --> 07:31:19,920 and test suits and test running and 6730 07:31:19,920 --> 07:31:24,318 assertion uh before creating the just 6731 07:31:24,318 --> 07:31:26,878 project I want you to open an empty 6732 07:31:26,878 --> 07:31:28,840 folder we're going to start it from the 6733 07:31:28,840 --> 07:31:30,878 scratch and you got to create 6734 07:31:30,878 --> 07:31:32,398 package.json 6735 07:31:32,398 --> 07:31:37,040 file I can say npm in it d-s please 6736 07:31:37,040 --> 07:31:39,638 create the package.json file for 6737 07:31:39,638 --> 07:31:42,920 me I got the package.json file now I'm 6738 07:31:42,920 --> 07:31:44,760 going to copy 6739 07:31:44,760 --> 07:31:50,240 that and let me replace 6740 07:31:50,240 --> 07:31:53,958 that here what I want to 6741 07:31:54,638 --> 07:31:57,440 say we we need to install the typing of 6742 07:31:57,440 --> 07:31:59,318 the justest we need to install the 6743 07:31:59,318 --> 07:32:01,958 justest app a Dev dependency and I 6744 07:32:01,958 --> 07:32:04,600 created the script to test the justest 6745 07:32:04,600 --> 07:32:06,520 if you want to run the justest in watch 6746 07:32:06,520 --> 07:32:08,558 mode I'll teach you how to do that you 6747 07:32:08,558 --> 07:32:10,840 got to run you got to run this 6748 07:32:10,840 --> 07:32:14,638 script so uh please install these two 6749 07:32:14,638 --> 07:32:18,878 dependencies I'm going to install 6750 07:32:23,240 --> 07:32:25,520 them it's going to install the 6751 07:32:25,520 --> 07:32:28,600 dependencies let's create a new file I 6752 07:32:28,600 --> 07:32:31,638 I'm going to name it to some .js file 6753 07:32:31,638 --> 07:32:34,160 we're going to test this function we're 6754 07:32:34,160 --> 07:32:38,760 going to write test cases to test this 6755 07:32:38,760 --> 07:32:41,958 function in the third step we have to 6756 07:32:41,958 --> 07:32:44,200 create some. 6757 07:32:44,200 --> 07:32:48,080 test.js file I'm not using typescript 6758 07:32:48,080 --> 07:32:50,478 here I just want to show you the basics 6759 07:32:50,478 --> 07:32:54,120 that's why I want to keep it more 6760 07:32:54,120 --> 07:32:57,280 simple we have to import the sum 6761 07:32:57,280 --> 07:33:01,760 function from the sum. JS file 6762 07:33:10,440 --> 07:33:15,080 I have to write the test case at 1 + 2 = 6763 07:33:15,080 --> 07:33:18,798 3 now we're going to write the test case 6764 07:33:18,798 --> 07:33:21,718 you can use the 6765 07:33:24,040 --> 07:33:28,718 test it should give me the 6766 07:33:28,718 --> 07:33:33,718 result 1 + 2 = 6767 07:33:33,878 --> 07:33:37,160 3 so we're going to call this 6768 07:33:37,160 --> 07:33:39,440 function it's going to take the call 6769 07:33:39,440 --> 07:33:41,718 back 6770 07:33:43,040 --> 07:33:45,520 function I can 6771 07:33:45,520 --> 07:33:47,440 say 6772 07:33:47,440 --> 07:33:50,120 expect it's not giving me suggestion 6773 07:33:50,120 --> 07:33:51,958 because it is taking too much time to 6774 07:33:51,958 --> 07:33:54,680 install the 6775 07:33:55,318 --> 07:33:57,520 dependencies because I think my internet 6776 07:33:57,520 --> 07:34:00,798 connection is slow right now 6777 07:34:00,798 --> 07:34:03,200 so we need a 6778 07:34:03,200 --> 07:34:06,400 [Music] 6779 07:34:16,520 --> 07:34:22,440 function sum. 2 B 3 we have to call that 6780 07:34:22,440 --> 07:34:24,760 function it is going to take two 6781 07:34:24,760 --> 07:34:27,520 arguments if you look at the sum. JS 6782 07:34:27,520 --> 07:34:31,520 file you will see two arguments A and B 6783 07:34:31,520 --> 07:34:33,878 the first argument I'm going to provide 6784 07:34:33,878 --> 07:34:36,398 one and the second argument I'm going to 6785 07:34:36,398 --> 07:34:39,520 provide two let's try to test it 6786 07:34:39,520 --> 07:34:43,398 out it's still 6787 07:34:45,360 --> 07:34:49,600 working npmi minus 6788 07:34:50,558 --> 07:34:54,600 TJ my both packages have installed just 6789 07:34:54,600 --> 07:34:56,718 in the typing of the 6790 07:34:56,718 --> 07:35:00,040 just now you can write the test case 6791 07:35:00,040 --> 07:35:03,440 I've written the test case you can run 6792 07:35:03,440 --> 07:35:06,280 it npm run test I would like to run it 6793 07:35:06,280 --> 07:35:08,958 in the watch 6794 07:35:23,440 --> 07:35:27,600 mode dash dash watch is not 6795 07:35:27,600 --> 07:35:32,878 sported so you have to write the watch 6796 07:35:45,040 --> 07:35:47,600 all now you can see that I have to 6797 07:35:47,600 --> 07:35:52,160 update the script for the watch 6798 07:35:55,318 --> 07:35:58,520 all now we have to select the pattern I 6799 07:35:58,520 --> 07:36:01,440 would like to run this s test.ts file 6800 07:36:01,440 --> 07:36:03,398 right now we have only single 6801 07:36:03,398 --> 07:36:07,798 file it's going to execute this file the 6802 07:36:07,798 --> 07:36:11,360 test has passed now if I write the 6803 07:36:11,360 --> 07:36:14,798 invalid value I save the 6804 07:36:14,798 --> 07:36:18,440 changes it has started automatically and 6805 07:36:18,440 --> 07:36:20,120 I got the 6806 07:36:20,120 --> 07:36:24,240 error expected four but received 6807 07:36:24,240 --> 07:36:27,398 three you can play you can play around 6808 07:36:27,398 --> 07:36:31,638 with multiple matches function let's 6809 07:36:31,638 --> 07:36:35,040 say 1 + 6810 07:36:35,040 --> 07:36:37,520 4 6811 07:36:37,520 --> 07:36:40,920 or to have been 6812 07:36:40,920 --> 07:36:45,718 called to have property to have 6813 07:36:45,718 --> 07:36:51,360 returned to have returned with let's say 6814 07:36:52,558 --> 07:36:57,318 five you can play around with multiple 6815 07:36:57,440 --> 07:37:03,000 watches received have as type 6816 07:37:04,360 --> 07:37:07,240 number so this is not a right matcher 6817 07:37:07,240 --> 07:37:12,080 function so I can say it should be 6818 07:37:17,920 --> 07:37:20,318 five this is how you will write your 6819 07:37:20,318 --> 07:37:23,080 first test 6820 07:37:23,080 --> 07:37:25,478 case now I'm going to teach you the 6821 07:37:25,478 --> 07:37:27,600 concept of 6822 07:37:27,600 --> 07:37:29,920 mocking we're going to play around with 6823 07:37:29,920 --> 07:37:34,240 mock functions Auto mocking inest refers 6824 07:37:34,240 --> 07:37:37,080 to the automatic creation of mock 6825 07:37:37,080 --> 07:37:39,878 implementation for imported modules or 6826 07:37:39,878 --> 07:37:43,000 dependencies during 6827 07:37:48,398 --> 07:37:53,478 testing let's create the mock 6828 07:37:54,478 --> 07:37:57,280 functions you can use the mock function 6829 07:37:57,280 --> 07:38:00,200 to track the function calls 6830 07:38:00,200 --> 07:38:02,398 if you want to remove the dependencies 6831 07:38:02,398 --> 07:38:05,958 we're going to do in njs testing we're 6832 07:38:05,958 --> 07:38:08,080 going to remove maybe the implementation 6833 07:38:08,080 --> 07:38:11,920 of service implementation of the service 6834 07:38:11,920 --> 07:38:14,718 then we will have more implementation we 6835 07:38:14,718 --> 07:38:17,240 can simplify the testing and control 6836 07:38:17,240 --> 07:38:20,160 behavior when you do the unit test your 6837 07:38:20,160 --> 07:38:24,798 code let's say if your code is dependent 6838 07:38:24,798 --> 07:38:27,558 on five dependencies let's say if your 6839 07:38:27,558 --> 07:38:32,200 class is dependent on on Class 6840 07:38:32,200 --> 07:38:34,840 B Class 6841 07:38:34,840 --> 07:38:36,760 B Class 6842 07:38:36,760 --> 07:38:41,000 C or class D let's 6843 07:38:41,000 --> 07:38:44,318 say and you want to do the unit testing 6844 07:38:44,318 --> 07:38:46,878 let's say this is the class A now Class 6845 07:38:46,878 --> 07:38:49,920 A is dependent on let's suppose Class B 6846 07:38:49,920 --> 07:38:52,600 Class C and class 6847 07:38:52,600 --> 07:38:56,878 D now Class A let's suppose we has two 6848 07:38:56,878 --> 07:38:59,798 function or one function if you if I 6849 07:38:59,798 --> 07:39:02,120 want to test the one function from the 6850 07:39:02,120 --> 07:39:05,000 class A when you do the unit testing you 6851 07:39:05,000 --> 07:39:08,280 test the individual Parts you test the 6852 07:39:08,280 --> 07:39:11,398 individual functions to test the 6853 07:39:11,398 --> 07:39:14,760 individual function maybe the individual 6854 07:39:14,760 --> 07:39:17,558 function of Class A is dependent on the 6855 07:39:17,558 --> 07:39:20,958 function of Class B function of Class C 6856 07:39:20,958 --> 07:39:24,240 and the function of Class D then we have 6857 07:39:24,240 --> 07:39:26,878 to write the mock implementation for the 6858 07:39:26,878 --> 07:39:30,760 class B Class C and the class D 6859 07:39:30,760 --> 07:39:34,478 I'll teach you in the later 6860 07:39:34,478 --> 07:39:37,558 videos now we're going to create mock 6861 07:39:37,558 --> 07:39:40,760 function this is how you will describe 6862 07:39:40,760 --> 07:39:44,160 or you can group all your test cases 6863 07:39:44,160 --> 07:39:45,558 into a 6864 07:39:45,558 --> 07:39:48,318 group describe I want to write all the 6865 07:39:48,318 --> 07:39:51,280 mo function examples in this 6866 07:39:51,280 --> 07:39:54,360 section it is similar to the test you 6867 07:39:54,360 --> 07:39:57,040 can Define the it or the test it is 6868 07:39:57,040 --> 07:39:59,920 referring as a test so it should create 6869 07:39:59,920 --> 07:40:03,160 the basic mock function let's create a 6870 07:40:03,160 --> 07:40:07,680 new file Mo function. spc. TS file I 6871 07:40:07,680 --> 07:40:10,520 have created the test.js file but you 6872 07:40:10,520 --> 07:40:12,080 can also create the 6873 07:40:12,080 --> 07:40:14,680 spec. JS 6874 07:40:14,680 --> 07:40:18,798 file the concept is same 6875 07:40:21,120 --> 07:40:25,080 now now we have to write the mo 6876 07:40:25,080 --> 07:40:28,240 function here I can 6877 07:40:28,240 --> 07:40:30,360 say 6878 07:40:30,360 --> 07:40:32,718 I will provide the 6879 07:40:32,718 --> 07:40:37,000 mo write Mo function 6880 07:40:37,000 --> 07:40:42,120 examples here this this this section 6881 07:40:42,120 --> 07:40:46,638 will have the mo function 6882 07:40:46,680 --> 07:40:49,958 examples let's write our first test case 6883 07:40:49,958 --> 07:40:52,280 it should create a basic Mo 6884 07:40:52,280 --> 07:40:55,280 function should create a 6885 07:40:55,280 --> 07:40:58,240 basic mock 6886 07:40:58,240 --> 07:41:01,240 function 6887 07:41:03,240 --> 07:41:06,360 you can create a mock function by using 6888 07:41:06,360 --> 07:41:09,920 the just. function I can say this is the 6889 07:41:09,920 --> 07:41:11,240 mock 6890 07:41:11,240 --> 07:41:14,840 function just provide is a 6891 07:41:14,840 --> 07:41:19,040 FN now we have the empty Mo function if 6892 07:41:19,040 --> 07:41:22,040 you log the mo function you will see 6893 07:41:22,040 --> 07:41:24,920 this is the empty function let's try to 6894 07:41:24,920 --> 07:41:29,040 do it run the script again to run the 6895 07:41:29,040 --> 07:41:31,318 Tex test in the watch 6896 07:41:31,318 --> 07:41:34,280 mode this time I'm going to add the 6897 07:41:34,280 --> 07:41:39,478 pattern for the more Dash function. spc. 6898 07:41:41,520 --> 07:41:44,680 file it is going to ask me the pattern I 6899 07:41:44,680 --> 07:41:49,680 can say mock now it's going to run this 6900 07:41:49,680 --> 07:41:54,440 file if you see the log you 6901 07:41:54,440 --> 07:42:01,760 have a function a mo function if I call 6902 07:42:02,040 --> 07:42:05,878 that you will see there is nothing here 6903 07:42:05,878 --> 07:42:08,840 we have undefined 6904 07:42:08,840 --> 07:42:12,000 function if you pass the argument it is 6905 07:42:12,000 --> 07:42:14,638 going to still it is going to still give 6906 07:42:14,638 --> 07:42:15,958 me the 6907 07:42:15,958 --> 07:42:20,558 undefined this is the empty mooch 6908 07:42:21,760 --> 07:42:24,798 function if you want to return the value 6909 07:42:24,798 --> 07:42:28,120 from the mock function you can also do 6910 07:42:28,120 --> 07:42:29,878 that 6911 07:42:29,878 --> 07:42:31,718 return 6912 07:42:31,718 --> 07:42:35,440 value Mo return value now let's say I I 6913 07:42:35,440 --> 07:42:37,080 would like to return the 6914 07:42:37,080 --> 07:42:41,040 four now if I call that function it 6915 07:42:41,040 --> 07:42:44,398 should give me the value of four I can 6916 07:42:44,398 --> 07:42:48,680 write the match functions on it mock I 6917 07:42:48,680 --> 07:42:50,360 call the 6918 07:42:50,360 --> 07:42:55,558 function to be the value should be 6919 07:42:57,040 --> 07:43:00,440 four you can see our test has passed now 6920 07:43:00,440 --> 07:43:03,558 if I add the invalid value which is 6921 07:43:03,558 --> 07:43:06,920 three it's going to give me the error 6922 07:43:06,920 --> 07:43:10,840 expected three but received 6923 07:43:14,600 --> 07:43:19,120 four if you want to remember I told you 6924 07:43:19,120 --> 07:43:23,080 you can also keep track the calls of the 6925 07:43:23,080 --> 07:43:26,878 function with the help of Mo how many 6926 07:43:26,878 --> 07:43:30,398 times this function has cost 6927 07:43:30,398 --> 07:43:34,520 called mo. calls. 6928 07:43:34,520 --> 07:43:38,398 length to be we have called 6929 07:43:38,398 --> 07:43:41,520 only one time so it should give me the 6930 07:43:41,520 --> 07:43:43,200 length is 6931 07:43:43,200 --> 07:43:47,220 one if I call the function two 6932 07:43:47,220 --> 07:43:48,638 [Music] 6933 07:43:48,638 --> 07:43:51,638 times Mo 6934 07:43:51,638 --> 07:43:53,638 function 6935 07:43:53,638 --> 07:43:55,958 dot to 6936 07:43:55,958 --> 07:43:58,040 have been 6937 07:43:58,040 --> 07:44:01,040 called 6938 07:44:02,958 --> 07:44:03,910 been 6939 07:44:03,910 --> 07:44:07,580 [Music] 6940 07:44:22,398 --> 07:44:26,040 called let's say I want to call that 6941 07:44:26,040 --> 07:44:30,000 function and I'm expecting the four 6942 07:44:30,000 --> 07:44:32,798 it should give me the error you can see 6943 07:44:32,798 --> 07:44:36,798 that the save to expected is one here we 6944 07:44:36,798 --> 07:44:37,878 have the 6945 07:44:37,878 --> 07:44:41,240 error it should give me the two because 6946 07:44:41,240 --> 07:44:44,000 we have called this function two times 6947 07:44:44,000 --> 07:44:46,360 if you want to keep track of the calls 6948 07:44:46,360 --> 07:44:48,478 of the function how many function have 6949 07:44:48,478 --> 07:44:50,318 how many times you have called the 6950 07:44:50,318 --> 07:44:55,760 function you can also do measure on 6951 07:44:55,760 --> 07:45:00,600 that here I can say expect 6952 07:45:00,600 --> 07:45:04,760 mock function to have been 6953 07:45:09,200 --> 07:45:11,718 called it is checking that did you call 6954 07:45:11,718 --> 07:45:15,520 the function yes we already called two 6955 07:45:15,520 --> 07:45:19,558 times this is how you you can return the 6956 07:45:19,558 --> 07:45:21,440 value from the mock function you can 6957 07:45:21,440 --> 07:45:23,080 create you have learned how to create 6958 07:45:23,080 --> 07:45:26,080 the basic mock 6959 07:45:27,920 --> 07:45:30,920 function 6960 07:45:31,718 --> 07:45:34,318 now you're going to learn how to create 6961 07:45:34,318 --> 07:45:37,160 a basic Mo function with 6962 07:45:37,160 --> 07:45:40,240 arguments we did not create a function 6963 07:45:40,240 --> 07:45:41,478 with 6964 07:45:41,478 --> 07:45:44,240 argument we just created a basic mock 6965 07:45:44,240 --> 07:45:47,120 function then we have returned the 6966 07:45:47,120 --> 07:45:50,120 value here I can 6967 07:45:50,120 --> 07:45:54,958 say it should create a mock 6968 07:45:54,958 --> 07:45:57,920 function with an 6969 07:45:57,920 --> 07:46:00,920 argument 6970 07:46:12,478 --> 07:46:15,878 let's create a function con 6971 07:46:15,878 --> 07:46:21,080 mock song function you can say song 6972 07:46:23,680 --> 07:46:27,000 mock just. 6973 07:46:27,000 --> 07:46:31,080 function it can have create song dto 6974 07:46:31,080 --> 07:46:33,200 let's say 6975 07:46:33,200 --> 07:46:34,760 that 6976 07:46:34,760 --> 07:46:38,360 mock create 6977 07:46:44,000 --> 07:46:47,638 song and this 6978 07:46:47,638 --> 07:46:51,160 function will have this 6979 07:46:51,160 --> 07:46:55,878 argument but I also want to write the 6980 07:46:55,878 --> 07:47:00,840 implementation you can do that as well 6981 07:47:01,280 --> 07:47:04,318 well right now we did not return the 6982 07:47:04,318 --> 07:47:07,280 value if you want to return the value 6983 07:47:07,280 --> 07:47:08,958 you can 6984 07:47:08,958 --> 07:47:13,520 do this stuff it is going to return a 6985 07:47:13,520 --> 07:47:15,840 song 6986 07:47:15,840 --> 07:47:19,920 title animals and 6987 07:47:19,920 --> 07:47:24,200 ID or you can oh 6988 07:47:25,000 --> 07:47:28,638 animals and ID which is one let's say 6989 07:47:28,638 --> 07:47:29,558 that 6990 07:47:29,558 --> 07:47:34,558 that if I am expecting on 6991 07:47:34,558 --> 07:47:38,798 that it should give me the same 6992 07:47:38,798 --> 07:47:41,798 object 6993 07:47:41,920 --> 07:47:45,600 animals ID which is 6994 07:47:45,600 --> 07:47:49,200 two or ID which is one if you want to 6995 07:47:49,200 --> 07:47:51,718 test the object you can use this two 6996 07:47:51,718 --> 07:47:54,200 equal 6997 07:47:56,478 --> 07:47:58,558 expression make sure you have called 6998 07:47:58,558 --> 07:48:01,200 that function with 6999 07:48:01,200 --> 07:48:04,760 argument create D is not 7000 07:48:04,760 --> 07:48:09,398 defined title should be 7001 07:48:21,080 --> 07:48:25,718 animals create D is not 7002 07:48:26,718 --> 07:48:30,920 defined I think it will not not work we 7003 07:48:30,920 --> 07:48:35,120 have to write the implementation here I 7004 07:48:35,120 --> 07:48:36,958 just want to show you the another 7005 07:48:36,958 --> 07:48:39,398 alternative path it's a call back 7006 07:48:39,398 --> 07:48:42,000 function it's going to it's going to 7007 07:48:42,000 --> 07:48:44,160 return the create song 7008 07:48:44,160 --> 07:48:47,360 dto now you 7009 07:48:47,360 --> 07:48:50,160 can look 7010 07:48:50,160 --> 07:48:53,398 at this 7011 07:48:57,718 --> 07:48:59,318 one 7012 07:48:59,318 --> 07:49:02,200 now my test has passed now if I will 7013 07:49:02,200 --> 07:49:05,520 write the ID it should give me the false 7014 07:49:05,520 --> 07:49:08,000 it should give me the false value or it 7015 07:49:08,000 --> 07:49:10,920 has failed because I am just returning 7016 07:49:10,920 --> 07:49:15,360 the ID but you can pass this 7017 07:49:15,360 --> 07:49:20,160 test if I create if I return the 7018 07:49:20,160 --> 07:49:24,280 object use the spread operator and ID 7019 07:49:24,280 --> 07:49:26,280 which is 7020 07:49:26,280 --> 07:49:30,638 one let's try to check it out 7021 07:49:35,080 --> 07:49:40,718 out now my function has my test has 7022 07:49:40,718 --> 07:49:41,840 passed 7023 07:49:41,840 --> 07:49:48,398 now if I provide the another value 7024 07:49:54,240 --> 07:49:58,840 lower in our application this function 7025 07:49:58,840 --> 07:50:01,600 like create song function does not exist 7026 07:50:01,600 --> 07:50:04,080 here we don't have a create song 7027 07:50:04,080 --> 07:50:06,240 function we don't have njs project we 7028 07:50:06,240 --> 07:50:08,120 don't have repository we don't have 7029 07:50:08,120 --> 07:50:10,878 service controller that's why this is 7030 07:50:10,878 --> 07:50:14,760 how you can create the mock 7031 07:50:19,760 --> 07:50:22,398 implementation you can also create a 7032 07:50:22,398 --> 07:50:25,600 mock function with mock 7033 07:50:25,600 --> 07:50:28,478 implementation we used callback function 7034 07:50:28,478 --> 07:50:31,080 here here but if you want to 7035 07:50:31,080 --> 07:50:34,478 use this Mo implementation you can do 7036 07:50:34,478 --> 07:50:35,920 that as 7037 07:50:35,920 --> 07:50:40,920 well it should create a mock 7038 07:50:40,920 --> 07:50:44,520 function with an 7039 07:50:44,600 --> 07:50:48,000 argument with Mo 7040 07:50:48,000 --> 07:50:51,240 implementation this 7041 07:50:56,360 --> 07:50:59,920 one I don't have preer 7042 07:50:59,920 --> 07:51:03,200 file R 7043 07:51:03,200 --> 07:51:06,360 see single C to 7044 07:51:06,360 --> 07:51:10,958 true because I use this prettier 7045 07:51:11,318 --> 07:51:15,840 formatter now my code has formatted 7046 07:51:16,920 --> 07:51:20,040 now we're going to use the same 7047 07:51:20,040 --> 07:51:24,360 approach const mock create 7048 07:51:24,360 --> 07:51:27,520 song just. 7049 07:51:27,520 --> 07:51:31,080 function I have defined the 7050 07:51:31,080 --> 07:51:36,120 function mock create song do 7051 07:51:36,120 --> 07:51:39,120 mock 7052 07:51:41,040 --> 07:51:45,240 implementation create song 7053 07:51:46,440 --> 07:51:49,440 dto it's going to return the object 7054 07:51:49,440 --> 07:51:54,760 create song dto and the ID which is 7055 07:51:54,760 --> 07:52:00,440 one now I'm going to ex run the mat to 7056 07:52:00,440 --> 07:52:02,878 test the 7057 07:52:03,958 --> 07:52:07,200 code my code has passed now my test has 7058 07:52:07,200 --> 07:52:11,920 passed now you can approach you can 7059 07:52:11,920 --> 07:52:15,200 implement the same stuff by using the 7060 07:52:15,200 --> 07:52:18,520 call back function here or if you prefer 7061 07:52:18,520 --> 07:52:20,878 the mug implementation you can do in 7062 07:52:20,878 --> 07:52:23,200 this 7063 07:52:27,440 --> 07:52:30,440 way now I'm going to teach you how to 7064 07:52:30,440 --> 07:52:32,840 create a mo function and it function 7065 07:52:32,840 --> 07:52:36,360 this function should return the 7066 07:52:38,240 --> 07:52:41,318 promise all you need to do is call the 7067 07:52:41,318 --> 07:52:45,958 mo result value we use the mo return 7068 07:52:45,958 --> 07:52:48,318 value you can do that with the help of 7069 07:52:48,318 --> 07:52:51,520 Mo resolved 7070 07:52:51,558 --> 07:52:54,520 value let's say I would like to create a 7071 07:52:54,520 --> 07:52:56,798 new 7072 07:52:57,440 --> 07:53:00,440 function 7073 07:53:01,638 --> 07:53:04,398 let's create a new test case it 7074 07:53:04,398 --> 07:53:10,000 should create a mo function with 7075 07:53:11,760 --> 07:53:14,600 promise ultimately it it is ultimately 7076 07:53:14,600 --> 07:53:17,318 it is going to return the 7077 07:53:17,318 --> 07:53:20,638 promise fetch song data or you can say 7078 07:53:20,638 --> 07:53:23,280 that fetch all the songs from the DB or 7079 07:53:23,280 --> 07:53:26,520 you can say the network 7080 07:53:27,440 --> 07:53:30,798 request now we got a 7081 07:53:30,798 --> 07:53:35,398 return Mo return or Mo resolved 7082 07:53:35,398 --> 07:53:39,200 value let's say fetch songs I got the 7083 07:53:39,200 --> 07:53:41,878 array ID 7084 07:53:41,878 --> 07:53:45,798 one title 7085 07:53:50,840 --> 07:53:55,270 Lover now I will like to call that fetch 7086 07:53:55,270 --> 07:53:56,520 [Music] 7087 07:53:56,520 --> 07:53:58,920 songs Doc do 7088 07:53:58,920 --> 07:54:01,680 results dot to 7089 07:54:01,680 --> 07:54:06,638 equal if you want to apply assertion on 7090 07:54:06,638 --> 07:54:09,558 promises you can call the results 7091 07:54:09,558 --> 07:54:13,680 property and then I can apply this 7092 07:54:13,680 --> 07:54:18,200 Expressions to equal this 7093 07:54:18,840 --> 07:54:23,398 one make sure you could to call that 7094 07:54:24,478 --> 07:54:28,920 function now my test has passed if I 7095 07:54:28,920 --> 07:54:32,600 write fetch I'm going to call the fetch 7096 07:54:32,600 --> 07:54:35,958 functions do 7097 07:54:37,200 --> 07:54:41,680 resolves dot to have 7098 07:54:42,398 --> 07:54:46,958 length it should have 7099 07:54:50,760 --> 07:54:53,878 one my test has passed if I write the 7100 07:54:53,878 --> 07:54:57,398 two it should give me the error because 7101 07:54:57,398 --> 07:55:01,318 we have only one property in the songs 7102 07:55:01,318 --> 07:55:04,120 array I got the 7103 07:55:04,120 --> 07:55:08,760 error I'm going to call it with 7104 07:55:08,840 --> 07:55:13,360 one this is how you will do the 7105 07:55:14,558 --> 07:55:18,160 mocking you have learned the feature 7106 07:55:18,160 --> 07:55:20,478 Auto moing which is mock 7107 07:55:20,478 --> 07:55:24,160 functions but we did not talk about the 7108 07:55:24,160 --> 07:55:27,240 spank and you can control the 7109 07:55:27,240 --> 07:55:28,840 behavior 7110 07:55:28,840 --> 07:55:32,200 and you can assert on their usage during 7111 07:55:32,200 --> 07:55:34,920 testing you can create the function 7112 07:55:34,920 --> 07:55:37,878 spying on spy you can spy on a function 7113 07:55:37,878 --> 07:55:40,360 by using the just. spy 7114 07:55:40,360 --> 07:55:45,200 on it allows you to create a spy or a 7115 07:55:45,200 --> 07:55:48,280 mock function for an existing object 7116 07:55:48,280 --> 07:55:51,160 method if you want to spy on existing 7117 07:55:51,160 --> 07:55:53,160 object you can do that if you want to 7118 07:55:53,160 --> 07:55:57,200 spy on existing class you can do 7119 07:55:57,240 --> 07:56:01,840 that now we're going to spy on an 7120 07:56:01,840 --> 07:56:05,398 object we have an object spy on a method 7121 07:56:05,398 --> 07:56:07,200 on the 7122 07:56:07,200 --> 07:56:10,638 object we going to Let's imagine we will 7123 07:56:10,638 --> 07:56:13,398 have the song repository and we have the 7124 07:56:13,398 --> 07:56:16,440 create method what I want to do I want 7125 07:56:16,440 --> 07:56:19,080 to spy on the create method I don't want 7126 07:56:19,080 --> 07:56:22,398 to use this implementation I want to use 7127 07:56:22,398 --> 07:56:25,680 my fake implementation in under the 7128 07:56:25,680 --> 07:56:29,718 hood just will tell you you I called 7129 07:56:29,718 --> 07:56:35,280 this function but I have used your 7130 07:56:35,280 --> 07:56:38,600 implementation this implementation this 7131 07:56:38,600 --> 07:56:41,760 one instead of instead of using this 7132 07:56:41,760 --> 07:56:44,478 original 7133 07:56:49,000 --> 07:56:52,080 implementation let's spy 7134 07:56:52,080 --> 07:56:55,478 on I want to I want you to create a new 7135 07:56:55,478 --> 07:57:00,878 file since by on demo dopc 7136 07:57:07,958 --> 07:57:11,520 dots let's create a song 7137 07:57:11,520 --> 07:57:14,520 repository and we have a create function 7138 07:57:14,520 --> 07:57:18,878 let's imagine we have a find 7139 07:57:20,958 --> 07:57:24,398 function let's imagine we have the find 7140 07:57:24,398 --> 07:57:26,240 one 7141 07:57:26,240 --> 07:57:30,760 function I want you to add the add this 7142 07:57:30,760 --> 07:57:34,318 Pi on this find find and find one but 7143 07:57:34,318 --> 07:57:36,760 first let me do that find one will 7144 07:57:36,760 --> 07:57:39,120 accept the ID let's 7145 07:57:39,120 --> 07:57:42,120 imagine the 7146 07:57:42,120 --> 07:57:45,090 ID now let's create a 7147 07:57:45,090 --> 07:57:48,189 [Music] 7148 07:57:57,120 --> 07:58:00,440 function so I created the test case it 7149 07:58:00,440 --> 07:58:03,280 should spy on the 7150 07:58:03,280 --> 07:58:05,718 existing method 7151 07:58:05,718 --> 07:58:09,360 function object 7152 07:58:13,878 --> 07:58:17,600 method so you can create a function on 7153 07:58:17,600 --> 07:58:20,200 this py 7154 07:58:27,040 --> 07:58:30,040 on 7155 07:58:31,798 --> 07:58:35,718 so this is how you will spy on the 7156 07:58:35,718 --> 07:58:40,398 function it should also give me 7157 07:58:42,360 --> 07:58:46,840 the toll tip 7158 07:58:48,318 --> 07:58:51,398 create if I log this piy function let me 7159 07:58:51,398 --> 07:58:54,558 show you what is inside 7160 07:58:54,558 --> 07:59:00,040 that I have to run this file 7161 07:59:09,240 --> 07:59:12,240 P I want to spy 7162 07:59:12,240 --> 07:59:15,080 on it's going to run this 7163 07:59:15,080 --> 07:59:21,478 file my test has passed but this is a mo 7164 07:59:22,520 --> 07:59:26,398 function if I call 7165 07:59:26,638 --> 07:59:29,600 that it should give me nothing because 7166 07:59:29,600 --> 07:59:31,020 it's an 7167 07:59:31,020 --> 07:59:33,318 [Music] 7168 07:59:33,318 --> 07:59:37,240 undefined what what if I call the 7169 07:59:37,240 --> 07:59:42,160 function SP and I'm going to call 7170 07:59:42,160 --> 07:59:47,160 it not an ID let's provide the 7171 07:59:54,398 --> 07:59:57,718 title or let's say if I call the 7172 07:59:57,718 --> 07:59:58,320 function 7173 07:59:58,320 --> 07:59:59,478 [Music] 7174 07:59:59,478 --> 08:00:02,558 function of this song 7175 08:00:02,558 --> 08:00:06,520 repository you will see the 7176 08:00:09,478 --> 08:00:14,040 magic I have called this function now my 7177 08:00:14,040 --> 08:00:19,478 function this function is pying on this 7178 08:00:21,160 --> 08:00:24,638 function what will 7179 08:00:25,360 --> 08:00:29,920 happen if I log 7180 08:00:29,958 --> 08:00:32,798 spy do 7181 08:00:32,798 --> 08:00:34,798 mock do 7182 08:00:34,798 --> 08:00:40,318 calls it should say I have been 7183 08:00:40,318 --> 08:00:45,680 called with this argument 7184 08:00:45,680 --> 08:00:49,080 array it's an array mo. length which 7185 08:00:49,080 --> 08:00:50,140 should be 7186 08:00:50,140 --> 08:00:51,600 [Music] 7187 08:00:51,600 --> 08:00:54,920 one it is saying I've been called only 7188 08:00:54,920 --> 08:00:58,680 one time by using this one if I call 7189 08:00:58,680 --> 08:01:02,680 song repository. create let's 7190 08:01:02,680 --> 08:01:04,878 say 7191 08:01:04,878 --> 08:01:07,558 title new 7192 08:01:07,558 --> 08:01:10,638 song now the length should be 7193 08:01:10,638 --> 08:01:13,680 two you can see 7194 08:01:13,680 --> 08:01:17,160 that if you want you can spy on the 7195 08:01:17,160 --> 08:01:21,760 function spy do to have been 7196 08:01:25,840 --> 08:01:29,040 called now my function has been 7197 08:01:29,040 --> 08:01:32,878 called expect 7198 08:01:35,000 --> 08:01:41,240 spy you can also say that to have been 7199 08:01:42,360 --> 08:01:46,160 called called 7200 08:01:53,718 --> 08:01:56,000 with called 7201 08:01:56,000 --> 08:02:00,840 with title should be 7202 08:02:02,920 --> 08:02:07,200 lower it is fine now if I say lovers it 7203 08:02:07,200 --> 08:02:08,840 should give me the 7204 08:02:08,840 --> 08:02:13,000 error you can see I got the 7205 08:02:18,798 --> 08:02:23,160 error you can also track the how many 7206 08:02:23,160 --> 08:02:26,840 times this function has called to have 7207 08:02:26,840 --> 08:02:30,520 been being call times it's going to give 7208 08:02:30,520 --> 08:02:32,360 me the 7209 08:02:32,360 --> 08:02:36,200 one I'm spying on this function song 7210 08:02:36,200 --> 08:02:38,680 repository. create similarly you can spy 7211 08:02:38,680 --> 08:02:42,200 on the find and the find 7212 08:02:43,558 --> 08:02:47,478 one you can also spy on a class I'm 7213 08:02:47,478 --> 08:02:50,318 going to create a new 7214 08:02:50,318 --> 08:02:53,080 class we 7215 08:02:53,080 --> 08:02:55,440 have let's say I'm going to place the 7216 08:02:55,440 --> 08:02:58,638 Clause here we have safe method in the 7217 08:02:58,638 --> 08:03:03,398 artist repository I want to spy on this 7218 08:03:06,760 --> 08:03:09,360 function but you can also write the 7219 08:03:09,360 --> 08:03:12,318 implementation just like we did it we 7220 08:03:12,318 --> 08:03:15,160 can have our own custom 7221 08:03:15,160 --> 08:03:17,558 implementation I call this 7222 08:03:17,558 --> 08:03:21,040 method now you can see 7223 08:03:21,040 --> 08:03:24,840 that to have been called times to have 7224 08:03:24,840 --> 08:03:28,558 been called with at the end you can also 7225 08:03:28,558 --> 08:03:30,600 restore the 7226 08:03:30,600 --> 08:03:35,000 mo but I would like to do this one after 7227 08:03:35,000 --> 08:03:38,240 each please restore all the mocks this 7228 08:03:38,240 --> 08:03:41,318 is a hook after 7229 08:03:41,318 --> 08:03:44,680 each it is going to run after all the 7230 08:03:44,680 --> 08:03:48,600 test in this section in describe 7231 08:03:48,600 --> 08:03:51,878 section now we're going to copy 7232 08:03:51,878 --> 08:03:56,718 that test case and paste it 7233 08:03:56,718 --> 08:03:58,280 here 7234 08:03:58,280 --> 08:04:01,680 we have created a new instance from the 7235 08:04:01,680 --> 08:04:03,200 artist 7236 08:04:03,200 --> 08:04:06,160 repository and I call just. spy on I 7237 08:04:06,160 --> 08:04:08,520 want to spy on the save function but I 7238 08:04:08,520 --> 08:04:11,558 also want to add my 7239 08:04:11,558 --> 08:04:13,520 implementation it is going to return the 7240 08:04:13,520 --> 08:04:16,280 create artist dto or it is going to 7241 08:04:16,280 --> 08:04:18,958 return you can say 7242 08:04:18,958 --> 08:04:23,280 that I would like to return the 7243 08:04:23,638 --> 08:04:27,478 object create artist dto and I want to 7244 08:04:27,478 --> 08:04:29,440 add the 7245 08:04:29,440 --> 08:04:34,398 ID now you can see that spy it is going 7246 08:04:34,398 --> 08:04:38,398 to call this function artist. save what 7247 08:04:38,398 --> 08:04:42,718 is here I called spy name and it has 7248 08:04:42,718 --> 08:04:45,920 returned Martin Garrick and with ID 7249 08:04:45,920 --> 08:04:49,638 which is one and how many times to have 7250 08:04:49,638 --> 08:04:52,280 been called yes expector to have been 7251 08:04:52,280 --> 08:04:56,638 called with name we provided the Martin 7252 08:04:56,638 --> 08:04:59,200 garx 7253 08:04:59,200 --> 08:05:02,680 spy and I don't need to do that because 7254 08:05:02,680 --> 08:05:05,240 we are doing it 7255 08:05:05,240 --> 08:05:07,000 here 7256 08:05:07,000 --> 08:05:10,000 expect 7257 08:05:12,280 --> 08:05:16,240 spym do calls. length how many times 7258 08:05:16,240 --> 08:05:17,638 have you been 7259 08:05:17,638 --> 08:05:22,478 called I think I did it one time 7260 08:05:23,760 --> 08:05:27,930 rt. everything is to have been 7261 08:05:27,930 --> 08:05:29,160 [Music] 7262 08:05:29,160 --> 08:05:33,440 hold with two 7263 08:05:34,360 --> 08:05:38,920 times also called artist. save and I 7264 08:05:38,920 --> 08:05:41,878 also called it 7265 08:05:45,718 --> 08:05:49,520 manually now it looks 7266 08:05:50,398 --> 08:05:52,958 good in this lesson I'm going to teach 7267 08:05:52,958 --> 08:05:55,958 you the concept of unit testing in 7268 08:05:55,958 --> 08:05:59,040 nestjs when you do unit testing you test 7269 08:05:59,040 --> 08:06:01,200 the individual component of your 7270 08:06:01,200 --> 08:06:03,558 application individual component means 7271 08:06:03,558 --> 08:06:06,680 individual function of a controller of a 7272 08:06:06,680 --> 08:06:09,280 repository of a 7273 08:06:09,280 --> 08:06:15,000 service we have song song service I have 7274 08:06:15,000 --> 08:06:17,440 attached this started kit project you 7275 08:06:17,440 --> 08:06:21,318 have to open it and run npm install you 7276 08:06:21,318 --> 08:06:24,398 got to install all the 7277 08:06:24,398 --> 08:06:27,878 dependencies we have uh we have this 7278 08:06:27,878 --> 08:06:28,840 song 7279 08:06:28,840 --> 08:06:31,878 controller when I'll be talking about 7280 08:06:31,878 --> 08:06:35,478 unit testing I mean I have to test 7281 08:06:35,478 --> 08:06:40,080 individually this function get song get 7282 08:06:40,080 --> 08:06:44,440 songs create song and update song and 7283 08:06:44,440 --> 08:06:46,200 delete 7284 08:06:46,200 --> 08:06:50,558 song but you notice the song controller 7285 08:06:50,558 --> 08:06:53,160 is dependent on song 7286 08:06:53,160 --> 08:06:57,120 service here we need more implementation 7287 08:06:57,120 --> 08:06:58,958 of song 7288 08:06:58,958 --> 08:07:00,520 service 7289 08:07:00,520 --> 08:07:03,440 because I want to test individually this 7290 08:07:03,440 --> 08:07:06,840 song controller but when we will do end 7291 08:07:06,840 --> 08:07:10,160 to end or integration testing when you 7292 08:07:10,160 --> 08:07:12,760 will test the end to end you will 7293 08:07:12,760 --> 08:07:15,958 integrate all of your services all of 7294 08:07:15,958 --> 08:07:19,440 your classes all of your 7295 08:07:22,798 --> 08:07:26,160 dependencies you can verify your unit of 7296 08:07:26,160 --> 08:07:31,878 code such as fun s methods or 7297 08:07:31,878 --> 08:07:34,520 classes with the help of unit test you 7298 08:07:34,520 --> 08:07:38,120 can find the bugs earlier you can 7299 08:07:38,120 --> 08:07:40,958 improve the code quality you can do 7300 08:07:40,958 --> 08:07:44,478 faster debugging you can refactor all 7301 08:07:44,478 --> 08:07:48,680 refactor code very 7302 08:07:50,798 --> 08:07:54,680 easily now when you run the source code 7303 08:07:54,680 --> 08:07:57,878 when you run the test for song control 7304 08:07:57,878 --> 08:08:02,520 dopc file nestjs already created this 7305 08:08:02,520 --> 08:08:06,878 spec file for us if you run that it is 7306 08:08:06,878 --> 08:08:11,558 going to say hey when I look at the song 7307 08:08:11,558 --> 08:08:15,840 controller I saw dependency There song 7308 08:08:15,840 --> 08:08:19,840 service but when you are creating the 7309 08:08:19,840 --> 08:08:22,120 testing module here we are creating the 7310 08:08:22,120 --> 08:08:25,878 testing module on the Fly n sjs will 7311 08:08:25,878 --> 08:08:27,958 compile it when it will comp compile it 7312 08:08:27,958 --> 08:08:30,120 will instantiate all the dependencies 7313 08:08:30,120 --> 08:08:33,718 for the testing module here it is saying 7314 08:08:33,718 --> 08:08:36,558 I'm depending I'm dependent on song 7315 08:08:36,558 --> 08:08:38,680 service but you are not providing any 7316 08:08:38,680 --> 08:08:41,398 song service here you not providing any 7317 08:08:41,398 --> 08:08:43,398 provider let's 7318 08:08:43,398 --> 08:08:48,478 say you are not any provider song 7319 08:08:49,440 --> 08:08:52,398 service so it will give me the 7320 08:08:52,398 --> 08:08:55,120 error because we are doing unit testing 7321 08:08:55,120 --> 08:08:57,120 I don't want to include song service 7322 08:08:57,120 --> 08:08:59,680 here I want to test individually these 7323 08:08:59,680 --> 08:09:02,200 components but I'll teach you how to 7324 08:09:02,200 --> 08:09:04,638 fake how to create the fake 7325 08:09:04,638 --> 08:09:06,718 implementation of song service here we 7326 08:09:06,718 --> 08:09:09,798 are getting the controller from this 7327 08:09:09,798 --> 08:09:13,040 module and it should be defined this is 7328 08:09:13,040 --> 08:09:18,520 the first test NP you can run the test 7329 08:09:18,520 --> 08:09:21,920 they have created the script for testing 7330 08:09:21,920 --> 08:09:25,200 I'm going to use this one npm run 7331 08:09:25,200 --> 08:09:29,318 test watch 7332 08:09:30,718 --> 08:09:34,638 let me provide the pattern for song 7333 08:09:34,638 --> 08:09:36,440 controller 7334 08:09:36,440 --> 08:09:42,558 dopc now it is going to watch this spec 7335 08:09:44,878 --> 08:09:48,798 file you can see that test has failed 7336 08:09:48,798 --> 08:09:51,798 now 7337 08:09:52,398 --> 08:09:55,520 here have you included song service in 7338 08:09:55,520 --> 08:09:56,318 the 7339 08:09:56,318 --> 08:09:57,840 provider 7340 08:09:57,840 --> 08:10:00,478 no we no we did 7341 08:10:00,478 --> 08:10:06,558 not if I will add the song 7342 08:10:09,878 --> 08:10:13,440 service but still getting the error now 7343 08:10:13,440 --> 08:10:16,160 the song service is dependent on the 7344 08:10:16,160 --> 08:10:18,080 song 7345 08:10:18,080 --> 08:10:22,160 repository but we will include all these 7346 08:10:22,160 --> 08:10:25,520 dependencies in integration testing but 7347 08:10:25,520 --> 08:10:28,080 I want to test the individual components 7348 08:10:28,080 --> 08:10:30,558 so get rid of the song 7349 08:10:30,558 --> 08:10:33,440 service I got the error so we need a 7350 08:10:33,440 --> 08:10:35,760 fake implementation we got to create the 7351 08:10:35,760 --> 08:10:39,520 mo service for the 7352 08:10:40,160 --> 08:10:44,398 song Here is how you can 7353 08:10:44,398 --> 08:10:47,280 register a mock service with the help of 7354 08:10:47,280 --> 08:10:49,760 Mo 7355 08:10:51,920 --> 08:10:54,080 implementation you have to register the 7356 08:10:54,080 --> 08:10:56,478 song service and you going to provide 7357 08:10:56,478 --> 08:10:58,398 the song service 7358 08:10:58,398 --> 08:11:01,440 provider here you have to use the use 7359 08:11:01,440 --> 08:11:04,638 value property to create the fake 7360 08:11:04,638 --> 08:11:07,240 implementation object or the 7361 08:11:07,240 --> 08:11:10,318 service you will have the get songs 7362 08:11:10,318 --> 08:11:14,558 method inside the song service here I 7363 08:11:14,558 --> 08:11:17,360 created the mo function and it is going 7364 08:11:17,360 --> 08:11:20,718 to return the promise with this 7365 08:11:20,718 --> 08:11:23,638 record let's try to check it 7366 08:11:23,638 --> 08:11:28,760 out when I will open the song service 7367 08:11:28,760 --> 08:11:32,120 we H we will have these methods get song 7368 08:11:32,120 --> 08:11:35,760 is returning a promise get song is also 7369 08:11:35,760 --> 08:11:37,240 you have to write the fake 7370 08:11:37,240 --> 08:11:39,760 implementation or Mo implementation of 7371 08:11:39,760 --> 08:11:42,280 the song 7372 08:11:45,718 --> 08:11:47,840 service we 7373 08:11:47,840 --> 08:11:50,520 need 7374 08:11:50,520 --> 08:11:53,958 providers song 7375 08:11:56,160 --> 08:11:58,318 service 7376 08:11:58,318 --> 08:12:00,760 song 7377 08:12:01,398 --> 08:12:05,520 service here we have to write the fake 7378 08:12:05,520 --> 08:12:08,840 implementation get 7379 08:12:09,040 --> 08:12:13,160 songs you can create a mock 7380 08:12:13,160 --> 08:12:18,240 function all you need to do is return a 7381 08:12:18,240 --> 08:12:21,200 promise with a value let's 7382 08:12:21,200 --> 08:12:23,190 say ID is 7383 08:12:23,190 --> 08:12:24,638 [Music] 7384 08:12:24,638 --> 08:12:29,120 one title 7385 08:12:30,600 --> 08:12:33,478 title let me check the song 7386 08:12:33,478 --> 08:12:36,878 model or song entity we have one 7387 08:12:36,878 --> 08:12:38,478 property which 7388 08:12:38,478 --> 08:12:41,080 is 7389 08:12:41,080 --> 08:12:44,680 title dancing 7390 08:12:45,280 --> 08:12:49,280 feed now I 7391 08:12:49,280 --> 08:12:55,318 have the mock implementation of get 7392 08:12:55,318 --> 08:12:58,760 songs now you can write the test 7393 08:12:58,760 --> 08:13:02,040 case make sure you get the 7394 08:13:02,040 --> 08:13:06,798 service if you need I think I don't need 7395 08:13:09,558 --> 08:13:13,080 that you can see that now my test has 7396 08:13:13,080 --> 08:13:17,040 past now I'm going to write another test 7397 08:13:17,040 --> 08:13:19,680 case it 7398 08:13:19,680 --> 08:13:24,840 should it should fetch all the 7399 08:13:26,040 --> 08:13:29,040 songs 7400 08:13:31,760 --> 08:13:36,520 or you can wrap your code inside the 7401 08:13:36,520 --> 08:13:40,638 describe fetch all the 7402 08:13:40,958 --> 08:13:44,160 songs here I have to write this one or 7403 08:13:44,160 --> 08:13:46,600 you can say if you need to write 7404 08:13:46,600 --> 08:13:48,878 multiple test 7405 08:13:48,878 --> 08:13:52,398 cases for get songs functionality you 7406 08:13:52,398 --> 08:13:55,760 have to do it in this 7407 08:13:56,040 --> 08:13:59,040 block 7408 08:14:00,000 --> 08:14:02,798 now we have to call the 7409 08:14:02,798 --> 08:14:05,798 function 7410 08:14:05,920 --> 08:14:10,040 songs v 7411 08:14:10,040 --> 08:14:12,958 controller. get 7412 08:14:12,958 --> 08:14:17,878 songs it's going to call this method get 7413 08:14:17,878 --> 08:14:22,240 songs you have to use the async 7414 08:14:26,040 --> 08:14:28,040 keyword 7415 08:14:28,040 --> 08:14:31,120 now we have to write the 7416 08:14:31,120 --> 08:14:33,878 matcher it is going to give me the array 7417 08:14:33,878 --> 08:14:37,398 of the songs to 7418 08:14:38,120 --> 08:14:42,760 equal now we're going to copy this 7419 08:14:48,318 --> 08:14:52,958 one a test has passed now get 7420 08:14:52,958 --> 08:14:55,958 song should fetch all the 7421 08:14:55,958 --> 08:14:58,958 songs 7422 08:15:04,238 --> 08:15:06,718 now I'm going to 7423 08:15:06,718 --> 08:15:11,120 copy all of the code for mock 7424 08:15:11,120 --> 08:15:15,680 implementation for the methods of 7425 08:15:17,558 --> 08:15:20,958 controllers it should add the create 7426 08:15:20,958 --> 08:15:25,398 song dto it should have the update song 7427 08:15:25,398 --> 08:15:29,840 dto what we have done so far I created 7428 08:15:29,840 --> 08:15:30,958 the mo 7429 08:15:30,958 --> 08:15:34,200 function with Mo implementation remember 7430 08:15:34,200 --> 08:15:39,478 it is get it is getting the idea is an 7431 08:15:39,478 --> 08:15:43,638 argument now it's going to return the 7432 08:15:43,638 --> 08:15:46,440 promise here for the create song it is 7433 08:15:46,440 --> 08:15:48,280 accepting the create song 7434 08:15:48,280 --> 08:15:51,878 dto I create the ID which is a unique 7435 08:15:51,878 --> 08:15:55,200 key and I have separate the separate the 7436 08:15:55,200 --> 08:15:58,760 create Song D it's returning a promise 7437 08:15:58,760 --> 08:16:02,000 similarly we have done for the update 7438 08:16:02,000 --> 08:16:05,680 song we created the mock function with 7439 08:16:05,680 --> 08:16:08,638 an argument update song dto returning 7440 08:16:08,638 --> 08:16:10,040 the 7441 08:16:10,040 --> 08:16:14,200 promise in song service for the update 7442 08:16:14,200 --> 08:16:16,440 it is returning the update result so 7443 08:16:16,440 --> 08:16:19,360 make sure you have this affected one and 7444 08:16:19,360 --> 08:16:21,200 delete song what is an 7445 08:16:21,200 --> 08:16:24,040 error oh I have to restart 7446 08:16:24,040 --> 08:16:25,840 the 7447 08:16:25,840 --> 08:16:27,360 project 7448 08:16:27,360 --> 08:16:29,440 now we have a fake implementation of 7449 08:16:29,440 --> 08:16:30,958 delete 7450 08:16:30,958 --> 08:16:35,820 song now let's test this 7451 08:16:35,820 --> 08:16:36,878 [Music] 7452 08:16:36,878 --> 08:16:40,280 function get song by ID I'm going to 7453 08:16:40,280 --> 08:16:42,200 copy 7454 08:16:42,200 --> 08:16:45,200 that get song by 7455 08:16:45,200 --> 08:16:51,558 ID you can provide an ID which should be 7456 08:16:51,600 --> 08:16:55,000 anything get song Yes ID could be 7457 08:16:55,000 --> 08:16:57,398 anything or I I can provide 7458 08:16:57,398 --> 08:16:59,600 a unique 7459 08:16:59,600 --> 08:17:03,280 key a uu 7460 08:17:10,440 --> 08:17:14,558 ID you can see the test has pass now for 7461 08:17:14,558 --> 08:17:15,878 get song 7462 08:17:15,878 --> 08:17:19,920 ID now I'm going to copy the create song 7463 08:17:19,920 --> 08:17:22,718 Let's test 7464 08:17:23,280 --> 08:17:28,080 it we have created the new new song by 7465 08:17:28,080 --> 08:17:30,000 using Create song 7466 08:17:30,000 --> 08:17:33,478 dto we have to provide this create song 7467 08:17:33,478 --> 08:17:36,878 dto and song. title should be run away 7468 08:17:36,878 --> 08:17:40,638 because we are giving this argument here 7469 08:17:40,638 --> 08:17:42,958 create function is giving me the create 7470 08:17:42,958 --> 08:17:45,878 song dto with 7471 08:17:46,080 --> 08:17:50,160 ID or you can also write 7472 08:17:50,160 --> 08:17:53,600 another matcher to 7473 08:17:53,600 --> 08:17:57,398 equal ID should be a uu ID 7474 08:17:57,398 --> 08:18:00,238 ID and 7475 08:18:00,238 --> 08:18:03,040 title should 7476 08:18:03,040 --> 08:18:04,798 be 7477 08:18:04,798 --> 08:18:09,120 Runway run 7478 08:18:18,200 --> 08:18:23,718 away you can see that my test has passed 7479 08:18:23,718 --> 08:18:26,878 now similarly we have to do the same 7480 08:18:26,878 --> 08:18:29,920 thing for update 7481 08:18:31,120 --> 08:18:34,478 song we are creating the update song 7482 08:18:34,478 --> 08:18:38,120 dtoo by using the update song dto object 7483 08:18:38,120 --> 08:18:41,360 I would like to update the 7484 08:18:42,120 --> 08:18:44,798 title it should be defined and affected 7485 08:18:44,798 --> 08:18:47,200 should be 7486 08:18:53,638 --> 08:18:55,718 one update 7487 08:18:55,718 --> 08:18:58,718 song 7488 08:18:58,958 --> 08:19:02,638 you can see update song has passed 7489 08:19:02,638 --> 08:19:05,680 now now we have to 7490 08:19:05,680 --> 08:19:10,120 perform testing for delete 7491 08:19:12,000 --> 08:19:16,080 song I just provided the ID you can 7492 08:19:16,080 --> 08:19:18,878 provide any 7493 08:19:23,360 --> 08:19:27,680 ID you can see that delete song has also 7494 08:19:27,680 --> 08:19:30,958 passed now this is how you will test 7495 08:19:30,958 --> 08:19:35,360 your controller unit testing for 7496 08:19:36,080 --> 08:19:38,478 controller in this lesson we're going to 7497 08:19:38,478 --> 08:19:41,120 test our service we're going to write 7498 08:19:41,120 --> 08:19:43,638 unit test for 7499 08:19:43,638 --> 08:19:46,280 service when you run this service you 7500 08:19:46,280 --> 08:19:49,520 will get the same error because in this 7501 08:19:49,520 --> 08:19:52,798 song service we have a one dependency 7502 08:19:52,798 --> 08:19:58,520 which is repository the song Repository 7503 08:19:58,520 --> 08:20:02,638 we have these so repository will 7504 08:20:02,638 --> 08:20:06,280 have this we have to write the mo 7505 08:20:06,280 --> 08:20:10,280 implementation for find method find one 7506 08:20:10,280 --> 08:20:15,200 or fail save method and create method 7507 08:20:15,200 --> 08:20:18,878 update and the delete because our 7508 08:20:18,878 --> 08:20:21,718 service is using these methods from the 7509 08:20:21,718 --> 08:20:25,200 song repository so if you write the if 7510 08:20:25,200 --> 08:20:29,840 you run the test for song service. speec 7511 08:20:29,840 --> 08:20:31,840 you will get the same issue this is the 7512 08:20:31,840 --> 08:20:34,920 default implementation from nestjs 7513 08:20:34,920 --> 08:20:38,000 Project they are using the provider 7514 08:20:38,000 --> 08:20:40,040 right now we don't need any controller 7515 08:20:40,040 --> 08:20:43,080 here we are doing unit testing we are 7516 08:20:43,080 --> 08:20:47,440 doing individual testing for each 7517 08:20:53,040 --> 08:20:57,760 function let's run the song 7518 08:20:57,760 --> 08:21:00,520 service it is going to find the song 7519 08:21:00,520 --> 08:21:04,398 service. spc. TS 7520 08:21:07,200 --> 08:21:11,000 file the test has failed 7521 08:21:11,000 --> 08:21:14,280 now it is dependent on the song 7522 08:21:14,280 --> 08:21:16,840 repository but I did not provide here I 7523 08:21:16,840 --> 08:21:19,798 did not register it so we have to create 7524 08:21:19,798 --> 08:21:25,520 the fake or Mo implementation for song 7525 08:21:25,520 --> 08:21:28,520 Repository 7526 08:21:28,680 --> 08:21:31,440 what I'm going to do I'm going to copy 7527 08:21:31,440 --> 08:21:34,040 this code we 7528 08:21:34,040 --> 08:21:37,040 need 7529 08:21:39,718 --> 08:21:42,440 these get 7530 08:21:42,440 --> 08:21:46,000 repository and get 7531 08:21:51,520 --> 08:21:55,000 song I have created a two objects song 7532 08:21:55,000 --> 08:22:00,760 array song one song song and the song 7533 08:22:02,638 --> 08:22:06,318 array this is how you will create the mo 7534 08:22:06,318 --> 08:22:08,798 implementation for the repository we 7535 08:22:08,798 --> 08:22:12,120 have to get the repository 7536 08:22:12,120 --> 08:22:16,440 token here I can do 7537 08:22:23,280 --> 08:22:25,878 that one thing make sure you provide the 7538 08:22:25,878 --> 08:22:27,840 song service 7539 08:22:27,840 --> 08:22:30,878 I forgot to do that in the controller 7540 08:22:30,878 --> 08:22:33,558 Let's test 7541 08:22:40,280 --> 08:22:44,440 it so everything is working fine now now 7542 08:22:44,440 --> 08:22:47,360 we're going to test the 7543 08:22:53,360 --> 08:22:56,878 service we have to import it get 7544 08:22:56,878 --> 08:22:59,600 repository 7545 08:22:59,638 --> 08:23:03,638 token I'm telling to nestjs this is my 7546 08:23:03,638 --> 08:23:09,000 fake or Mo implementation for the 7547 08:23:10,160 --> 08:23:13,638 repository song 7548 08:23:16,080 --> 08:23:19,440 service for the find it is going to 7549 08:23:19,440 --> 08:23:22,440 return the array we use promise. resolve 7550 08:23:22,440 --> 08:23:23,318 Mo 7551 08:23:23,318 --> 08:23:26,238 implementation for the just I'm getting 7552 08:23:26,238 --> 08:23:29,040 the options as a find one options you 7553 08:23:29,040 --> 08:23:32,958 can import it from the type orm we also 7554 08:23:32,958 --> 08:23:34,440 need to 7555 08:23:34,440 --> 08:23:37,318 import create song 7556 08:23:37,318 --> 08:23:41,840 dto it is returning a promise with one 7557 08:23:41,840 --> 08:23:45,120 song it is going to return the promise 7558 08:23:45,120 --> 08:23:47,558 with a song newly created song and save 7559 08:23:47,558 --> 08:23:50,120 method is just a simple Mo function it's 7560 08:23:50,120 --> 08:23:51,638 not doing 7561 08:23:51,638 --> 08:23:55,920 anything in update we need to get update 7562 08:23:55,920 --> 08:23:57,160 song 7563 08:23:57,160 --> 08:24:01,000 dto we have return the mo 7564 08:24:01,000 --> 08:24:03,200 implementation and it is returning a 7565 08:24:03,200 --> 08:24:05,440 promise with one 7566 08:24:05,440 --> 08:24:08,520 song for the delete it is returning a 7567 08:24:08,520 --> 08:24:09,920 promise with 7568 08:24:09,920 --> 08:24:12,878 affected but maybe for the update you 7569 08:24:12,878 --> 08:24:17,600 can give this response affected 7570 08:24:17,600 --> 08:24:20,558 one now we have to test 7571 08:24:20,558 --> 08:24:25,318 it if you need a repo you can get the 7572 08:24:25,318 --> 08:24:29,440 repo yes we may need a repo I can get 7573 08:24:29,440 --> 08:24:34,840 the repository by using get repository 7574 08:24:34,840 --> 08:24:37,600 token let's 7575 08:24:37,600 --> 08:24:40,680 test the method from the song 7576 08:24:40,680 --> 08:24:43,958 service it should give me this song by 7577 08:24:43,958 --> 08:24:46,520 ID we're going to test this method this 7578 08:24:46,520 --> 08:24:48,080 [Music] 7579 08:24:48,080 --> 08:24:50,398 one this 7580 08:24:50,398 --> 08:24:54,600 one get song by ID this 7581 08:24:54,600 --> 08:25:01,360 one it is using find one or fail and is 7582 08:25:01,360 --> 08:25:04,520 using fetching the song on the based on 7583 08:25:04,520 --> 08:25:07,798 ID so what I have I have done I created 7584 08:25:07,798 --> 08:25:09,238 a new 7585 08:25:09,238 --> 08:25:13,160 song I I got the song on the based on ID 7586 08:25:13,160 --> 08:25:15,798 it should be Yu 7587 08:25:15,798 --> 08:25:19,680 ID Yu 7588 08:25:19,680 --> 08:25:24,760 ID so I'm going to spy on this find one 7589 08:25:24,760 --> 08:25:27,200 or fail 7590 08:25:27,200 --> 08:25:29,558 what I have 7591 08:25:29,558 --> 08:25:32,958 done I called this method get song in 7592 08:25:32,958 --> 08:25:34,318 under the 7593 08:25:34,318 --> 08:25:38,440 hood this Pi this method this Pi just Pi 7594 08:25:38,440 --> 08:25:41,760 will be spy on find one or fail it is 7595 08:25:41,760 --> 08:25:44,398 saying hey you have called find one or 7596 08:25:44,398 --> 08:25:47,520 fail from the original repository 7597 08:25:47,520 --> 08:25:51,920 method and it has been called with this 7598 08:25:53,638 --> 08:25:56,080 one this is how you can write the unit 7599 08:25:56,080 --> 08:25:58,238 test 7600 08:25:58,718 --> 08:26:01,840 now we're going to write the test 7601 08:26:01,840 --> 08:26:07,040 case to create song I created this 7602 08:26:07,040 --> 08:26:12,040 song and I'm going to expect it with one 7603 08:26:13,120 --> 08:26:15,638 song called one 7604 08:26:15,638 --> 08:26:20,200 time and you can say called with title 7605 08:26:20,200 --> 08:26:24,360 lover let's copy the 7606 08:26:25,238 --> 08:26:29,398 update let's copy the 7607 08:26:43,200 --> 08:26:47,238 delete everything is passed 7608 08:26:49,200 --> 08:26:53,840 now for update I think inside the song 7609 08:26:53,840 --> 08:26:58,080 service what we have done update 7610 08:26:58,080 --> 08:27:02,160 result so we have to fix 7611 08:27:02,160 --> 08:27:06,000 it more repository it should give me the 7612 08:27:06,000 --> 08:27:09,200 affected and the 7613 08:27:13,360 --> 08:27:16,360 one 7614 08:27:17,318 --> 08:27:19,558 result 7615 08:27:19,558 --> 08:27:23,558 result do affected equals 7616 08:27:23,558 --> 08:27:25,120 to 7617 08:27:25,120 --> 08:27:28,120 affected 7618 08:27:34,878 --> 08:27:38,120 one test has passed now if you want to 7619 08:27:38,120 --> 08:27:41,958 spy on delete method how many times I 7620 08:27:41,958 --> 08:27:44,080 have call delete method if you want to 7621 08:27:44,080 --> 08:27:48,160 track the calls you can spy on that by 7622 08:27:48,160 --> 08:27:51,200 using the Spy repository on delete 7623 08:27:51,200 --> 08:27:54,200 method I've have been called this one 7624 08:27:54,200 --> 08:27:57,280 time and it is affected one time I got 7625 08:27:57,280 --> 08:28:00,360 the it means affected means one record 7626 08:28:00,360 --> 08:28:01,840 has deleted 7627 08:28:01,840 --> 08:28:05,600 successfully and it has been called with 7628 08:28:05,600 --> 08:28:13,398 uid that's all for unit testing with 7629 08:28:25,040 --> 08:28:28,040 service 7630 08:28:29,878 --> 08:28:32,398 now you're going to learn the concept of 7631 08:28:32,398 --> 08:28:34,920 end to end testing we're going to do end 7632 08:28:34,920 --> 08:28:38,318 to end testing for our nextjs project 7633 08:28:38,318 --> 08:28:40,520 end to end testing is a software testing 7634 08:28:40,520 --> 08:28:43,398 methodology that focus on testing the 7635 08:28:43,398 --> 08:28:46,520 entire application flow from start to 7636 08:28:46,520 --> 08:28:48,920 finish now we're going to integrate all 7637 08:28:48,920 --> 08:28:52,080 the song controller repository song 7638 08:28:52,080 --> 08:28:54,360 service 7639 08:28:54,360 --> 08:28:57,520 everything you can do 7640 08:28:57,520 --> 08:29:01,160 API testing in end to endend 7641 08:29:01,160 --> 08:29:04,160 testing validation of the user flow 7642 08:29:04,160 --> 08:29:05,600 improved user 7643 08:29:05,600 --> 08:29:07,920 experience now we're going to start 7644 08:29:07,920 --> 08:29:10,120 doing the end to end testing first step 7645 08:29:10,120 --> 08:29:12,958 we have to add the script in our 7646 08:29:12,958 --> 08:29:14,280 package.json 7647 08:29:14,280 --> 08:29:16,718 file they have written the script for 7648 08:29:16,718 --> 08:29:19,080 end to endend testing but they did not 7649 08:29:19,080 --> 08:29:20,920 add add the 7650 08:29:20,920 --> 08:29:24,040 watch that's why I just addit the watch 7651 08:29:24,040 --> 08:29:27,360 or detect open handle 7652 08:29:27,360 --> 08:29:30,318 I I have written this line or keyword 7653 08:29:30,318 --> 08:29:33,000 for to handle the 7654 08:29:33,000 --> 08:29:37,040 errors if you will not at this line This 7655 08:29:37,040 --> 08:29:38,878 one this 7656 08:29:38,878 --> 08:29:41,440 word it will give you some type of 7657 08:29:41,440 --> 08:29:44,000 warning 7658 08:29:44,120 --> 08:29:46,520 messages the first step we have to 7659 08:29:46,520 --> 08:29:50,920 register it the type or a 7660 08:29:52,040 --> 08:29:56,398 module in the song and to end test here 7661 08:29:56,398 --> 08:29:59,760 we have the end to end 7662 08:29:59,798 --> 08:30:03,040 test this 7663 08:30:05,160 --> 08:30:07,798 one this is the end to endend test for 7664 08:30:07,798 --> 08:30:10,558 our generic application let's run 7665 08:30:10,558 --> 08:30:13,680 it npm run 7666 08:30:13,680 --> 08:30:17,160 test find the 7667 08:30:17,160 --> 08:30:21,958 script test E2 7668 08:30:21,958 --> 08:30:24,920 e and the watch 7669 08:30:24,920 --> 08:30:27,920 mode 7670 08:30:33,600 --> 08:30:36,360 let's provide the pattern 7671 08:30:36,360 --> 08:30:40,440 app it is going to run this spec file 7672 08:30:40,440 --> 08:30:44,318 this is end to endend testing for app 7673 08:30:47,718 --> 08:30:51,238 module going to create nests application 7674 08:30:51,238 --> 08:30:54,160 and initialize it here it is going to 7675 08:30:54,160 --> 08:30:57,920 test our root 7676 08:30:58,638 --> 08:31:01,238 URL under the hood they are using the 7677 08:31:01,238 --> 08:31:03,238 Super 7678 08:31:03,238 --> 08:31:07,200 Test request from the Super Test making 7679 08:31:07,200 --> 08:31:09,878 the API request by using calling the 7680 08:31:09,878 --> 08:31:13,440 server this is the end point and it is 7681 08:31:13,440 --> 08:31:17,040 expecting the 200 status 7682 08:31:17,040 --> 08:31:20,600 code test has 7683 08:31:21,920 --> 08:31:27,840 failed got expected 200 but got 4 7684 08:31:39,920 --> 08:31:45,958 44 but I would like to add the test 7685 08:31:45,958 --> 08:31:49,760 case into to end testing for our song 7686 08:31:49,760 --> 08:31:53,840 controller for song 7687 08:31:54,840 --> 08:31:57,840 module 7688 08:32:05,718 --> 08:32:09,040 let's create a new 7689 08:32:09,440 --> 08:32:13,000 file here inside the test I'm going to 7690 08:32:13,000 --> 08:32:14,600 name it to 7691 08:32:14,600 --> 08:32:17,878 songs I can call it 7692 08:32:17,878 --> 08:32:22,160 songs do E2 7693 08:32:22,160 --> 08:32:27,840 do- spec. DS file 7694 08:32:31,878 --> 08:32:34,680 it is just an initial code you can copy 7695 08:32:34,680 --> 08:32:38,360 that from the app do e2e 7696 08:32:38,360 --> 08:32:42,080 testing I got this 7697 08:32:43,718 --> 08:32:48,000 one describe it should be 7698 08:32:48,840 --> 08:32:52,638 songs import couple of 7699 08:32:52,638 --> 08:32:56,840 dependencies type or module 7700 08:32:56,840 --> 08:33:01,040 make sure you have created the database 7701 08:33:01,040 --> 08:33:04,478 for testing I created that with the test 7702 08:33:04,478 --> 08:33:08,360 F and syn synchronize set to true you 7703 08:33:08,360 --> 08:33:12,040 have to register entity manually it will 7704 08:33:12,040 --> 08:33:14,200 not work with regular 7705 08:33:14,200 --> 08:33:18,160 expression at the end of each or at the 7706 08:33:18,160 --> 08:33:21,280 end of all test when all the test will 7707 08:33:21,280 --> 08:33:23,478 be finished it is going to drop the 7708 08:33:23,478 --> 08:33:27,718 schema we will have the song Mar 7709 08:33:29,120 --> 08:33:33,280 is not getting the path I think 7710 08:33:36,920 --> 08:33:41,638 so we have to do in This 7711 08:33:45,200 --> 08:33:50,040 Way song Find song 7712 08:33:51,440 --> 08:33:54,718 entity same thing 7713 08:33:54,718 --> 08:33:57,718 here 7714 08:33:58,798 --> 08:33:59,638 there 7715 08:33:59,638 --> 08:34:03,718 is I got the testing 7716 08:34:04,840 --> 08:34:06,638 modu that's 7717 08:34:06,638 --> 08:34:10,600 it now we have to run 7718 08:34:16,478 --> 08:34:20,360 it here you have to provide 7719 08:34:20,360 --> 08:34:22,040 your 7720 08:34:22,040 --> 08:34:23,840 username 7721 08:34:23,840 --> 08:34:27,040 password and the Local Host Post Port in 7722 08:34:27,040 --> 08:34:29,920 the name of your 7723 08:34:32,958 --> 08:34:36,718 TP it is going to run this 7724 08:34:36,718 --> 08:34:40,878 file I got the error you your test Suite 7725 08:34:40,878 --> 08:34:43,120 must contain at least one test all right 7726 08:34:43,120 --> 08:34:46,200 now we're going to test the 7727 08:34:49,200 --> 08:34:52,080 code first 7728 08:34:52,080 --> 08:34:56,798 step after each test we have to clear 7729 08:34:56,798 --> 08:34:58,558 the repository because we're going to 7730 08:34:58,558 --> 08:35:01,478 interact with 7731 08:35:03,520 --> 08:35:06,360 database in the 7732 08:35:06,360 --> 08:35:09,760 describe this will be run before all and 7733 08:35:09,760 --> 08:35:15,040 we will have after each we have to clear 7734 08:35:15,040 --> 08:35:18,840 everything from the song table or song 7735 08:35:18,840 --> 08:35:21,440 repository this clear method will delete 7736 08:35:21,440 --> 08:35:23,958 the records from the table but it will 7737 08:35:23,958 --> 08:35:26,478 keep the schema schema 7738 08:35:26,478 --> 08:35:28,840 this function or this hook will run 7739 08:35:28,840 --> 08:35:31,558 after every 7740 08:35:32,958 --> 08:35:37,638 test make sure you import the super 7741 08:35:43,680 --> 08:35:46,200 test I'm going to write the test case 7742 08:35:46,200 --> 08:35:47,360 for 7743 08:35:47,360 --> 08:35:52,520 delete or not not a delete for 7744 08:35:54,558 --> 08:35:56,280 create 7745 08:35:56,280 --> 08:36:00,238 I just created a new method create song 7746 08:36:00,238 --> 08:36:03,840 we have the song song. title song. 7747 08:36:03,840 --> 08:36:07,160 repository and it is going to save the 7748 08:36:07,160 --> 08:36:10,718 song in the real database right now we 7749 08:36:10,718 --> 08:36:13,000 don't have more implementation for the 7750 08:36:13,000 --> 08:36:16,360 song repository or the song service it 7751 08:36:16,360 --> 08:36:19,040 is going to save the record in our real 7752 08:36:19,040 --> 08:36:21,760 database we are using the test 7753 08:36:21,760 --> 08:36:24,558 def if you don't have test def you can 7754 08:36:24,558 --> 08:36:27,238 create it 7755 08:36:27,238 --> 08:36:30,280 I have set the title get the repository 7756 08:36:30,280 --> 08:36:33,238 instance from the app.get and called the 7757 08:36:33,238 --> 08:36:35,680 safe 7758 08:36:40,600 --> 08:36:43,840 method maybe you need to 7759 08:36:43,840 --> 08:36:47,478 add a weight 7760 08:36:52,000 --> 08:36:54,520 expression because we are returning a 7761 08:36:54,520 --> 08:36:56,160 song 7762 08:36:56,160 --> 08:36:58,478 with 7763 08:36:59,200 --> 08:37:02,558 promise you can do in this 7764 08:37:02,558 --> 08:37:05,238 way it will automatically get this 7765 08:37:05,238 --> 08:37:06,920 promise with 7766 08:37:06,920 --> 08:37:11,680 song let's write the test 7767 08:37:11,680 --> 08:37:15,280 case before fetching these songs make 7768 08:37:15,280 --> 08:37:18,760 sure you have the record in the database 7769 08:37:18,760 --> 08:37:22,878 that is why that is why I created a new 7770 08:37:22,878 --> 08:37:26,000 song with create song first you got to 7771 08:37:26,000 --> 08:37:29,000 save the song in the in the postest 7772 08:37:29,000 --> 08:37:31,440 database with title 7773 08:37:31,440 --> 08:37:35,398 animals and then you have to send the 7774 08:37:35,398 --> 08:37:38,760 API request to this URL 7775 08:37:38,760 --> 08:37:42,318 doget and here I'm checking the status 7776 08:37:42,318 --> 08:37:46,238 code should be 200 and the request body 7777 08:37:46,238 --> 08:37:48,318 or the results body should have length 7778 08:37:48,318 --> 08:37:51,280 one which should be have one record it 7779 08:37:51,280 --> 08:37:53,520 is an array it is going to give me the 7780 08:37:53,520 --> 08:37:58,000 array in results. party and I'm I have 7781 08:37:58,000 --> 08:37:59,840 created a one song 7782 08:37:59,840 --> 08:38:03,000 so it should be there there should be 7783 08:38:03,000 --> 08:38:06,120 one song in our database so that's why I 7784 08:38:06,120 --> 08:38:08,160 converted into an 7785 08:38:08,160 --> 08:38:12,478 array after then when this test will be 7786 08:38:12,478 --> 08:38:15,080 completed it is going to call this 7787 08:38:15,080 --> 08:38:17,680 method clear the 7788 08:38:17,680 --> 08:38:20,200 repository you can see our test has 7789 08:38:20,200 --> 08:38:26,398 passed now if I write let's say 20 1 or 7790 08:38:26,398 --> 08:38:28,680 maybe 400 it is going to give me the 7791 08:38:28,680 --> 08:38:33,360 error hey I'm expecting 7792 08:38:46,280 --> 08:38:50,318 200 you can see I got the 7793 08:38:50,840 --> 08:38:54,360 error so get song endpoints has tested 7794 08:38:54,360 --> 08:38:56,558 successfully 7795 08:38:56,558 --> 08:39:00,840 now we're going to get test get song by 7796 08:39:00,840 --> 08:39:04,238 ID first we have to create a new song 7797 08:39:04,238 --> 08:39:06,318 then I have to I have to send the API 7798 08:39:06,318 --> 08:39:09,160 request make sure you have to send the 7799 08:39:09,160 --> 08:39:12,200 ID I created this song because after 7800 08:39:12,200 --> 08:39:14,280 each test it is going to clear the 7801 08:39:14,280 --> 08:39:17,360 repository it is going to delete all the 7802 08:39:17,360 --> 08:39:20,040 records from the songs 7803 08:39:20,040 --> 08:39:23,398 table create a 7804 08:39:23,398 --> 08:39:25,920 song then it's going to you have to 7805 08:39:25,920 --> 08:39:27,878 provide the ID first I have created the 7806 08:39:27,878 --> 08:39:31,280 song then we have to test it make sure 7807 08:39:31,280 --> 08:39:34,840 the body should equal to this 7808 08:39:35,558 --> 08:39:38,718 song I'm going to save the 7809 08:39:38,718 --> 08:39:42,238 application it should 7810 08:39:44,680 --> 08:39:47,680 pass two test has passed 7811 08:39:47,680 --> 08:39:53,840 now now we're going to test update songs 7812 08:39:54,318 --> 08:39:59,120 endpoint make sure you have to import 7813 08:39:59,200 --> 08:40:02,920 it it should be 7814 08:40:04,520 --> 08:40:07,318 it create a new 7815 08:40:07,318 --> 08:40:10,318 song and you have update song dto I 7816 08:40:10,318 --> 08:40:13,440 would like to update the animals to 7817 08:40:13,440 --> 08:40:16,760 wonderful sending the API 7818 08:40:16,760 --> 08:40:21,840 request this is how you will send the 7819 08:40:22,040 --> 08:40:25,478 data to if you want to send the Json op 7820 08:40:25,478 --> 08:40:29,200 object into the request body you can 7821 08:40:29,200 --> 08:40:31,440 call the send method I want to send it 7822 08:40:31,440 --> 08:40:33,120 as a update song 7823 08:40:33,120 --> 08:40:35,958 dto this one I want to update the title 7824 08:40:35,958 --> 08:40:39,160 which is wonderful status code should be 7825 08:40:39,160 --> 08:40:42,478 200 and affected should be one it means 7826 08:40:42,478 --> 08:40:46,520 our record has updated 7827 08:40:47,558 --> 08:40:50,318 successfully and 7828 08:40:50,318 --> 08:40:54,238 let's do this for 7829 08:40:54,238 --> 08:40:57,238 post 7830 08:40:58,120 --> 08:41:01,440 we have this is our create song dto we 7831 08:41:01,440 --> 08:41:04,040 did not 7832 08:41:04,360 --> 08:41:07,360 create we did not create the song right 7833 08:41:07,360 --> 08:41:10,440 now I'm going to use the create song dto 7834 08:41:10,440 --> 08:41:13,160 provide it as a request 7835 08:41:13,160 --> 08:41:16,160 body the status code should be 2011 and 7836 08:41:16,160 --> 08:41:19,200 the title should be 7837 08:41:20,638 --> 08:41:24,760 animals for test has passed now we have 7838 08:41:24,760 --> 08:41:27,878 to test the delete song 7839 08:41:27,878 --> 08:41:32,360 endpoint let's create the song first and 7840 08:41:32,360 --> 08:41:34,478 then we have to provide the ID which 7841 08:41:34,478 --> 08:41:36,040 record you want to 7842 08:41:36,040 --> 08:41:38,958 delete status should be 7843 08:41:38,958 --> 08:41:42,120 200 and the affected body should be 7844 08:41:42,120 --> 08:41:44,878 affected should be one it means record 7845 08:41:44,878 --> 08:41:47,680 has deleted 7846 08:41:49,478 --> 08:41:51,558 successfully this is how you will write 7847 08:41:51,558 --> 08:41:53,718 the end to endend or integration testing 7848 08:41:53,718 --> 08:41:57,878 with database or the nestjs 7849 08:41:57,878 --> 08:42:01,360 application nestjs version 10 has 7850 08:42:01,360 --> 08:42:02,520 released 7851 08:42:02,520 --> 08:42:06,600 now I'm going to show you how to create 7852 08:42:06,600 --> 08:42:10,760 new projects with nestjs version 10 7853 08:42:10,760 --> 08:42:14,120 version 10 is very fast they have 7854 08:42:14,120 --> 08:42:18,718 integrated the rest rust compiler they 7855 08:42:18,718 --> 08:42:20,878 have integrate the rest to compile the 7856 08:42:20,878 --> 08:42:23,238 source code 7857 08:42:23,238 --> 08:42:26,718 sjs if you Google the version 10 you 7858 08:42:26,718 --> 08:42:29,718 will find the blog post from the owner 7859 08:42:29,718 --> 08:42:32,080 of the 7860 08:42:32,200 --> 08:42:34,798 nestjs they have 7861 08:42:34,798 --> 08:42:39,120 released version 10 in the version 7862 08:42:39,120 --> 08:42:43,000 10 you you can compile the code in a 7863 08:42:43,000 --> 08:42:47,878 faster way like it will also improve the 7864 08:42:47,878 --> 08:42:48,760 hot 7865 08:42:48,760 --> 08:42:50,478 reloading you 7866 08:42:50,478 --> 08:42:54,718 can you can do development in the more 7867 08:42:54,718 --> 08:42:57,120 fast 7868 08:43:00,878 --> 08:43:03,718 way to start a project you have to add 7869 08:43:03,718 --> 08:43:05,000 this 7870 08:43:05,000 --> 08:43:07,930 command we we're going to use this 7871 08:43:07,930 --> 08:43:09,040 [Music] 7872 08:43:09,040 --> 08:43:11,558 options this one we will add these 7873 08:43:11,558 --> 08:43:13,680 options into Nest 7874 08:43:13,680 --> 08:43:17,398 CLI they have also introduced a new 7875 08:43:17,398 --> 08:43:21,398 feature this time you can Mo the entire 7876 08:43:21,398 --> 08:43:24,040 module instead of provider and 7877 08:43:24,040 --> 08:43:26,120 controller 7878 08:43:26,120 --> 08:43:29,638 these are all the new features in the 7879 08:43:29,638 --> 08:43:33,478 nestjs version 10 but the main feature 7880 08:43:33,478 --> 08:43:38,478 is improving the compile code speed of 7881 08:43:38,478 --> 08:43:39,920 the 7882 08:43:39,920 --> 08:43:44,318 compiler but let's create a new project 7883 08:43:44,318 --> 08:43:46,680 first of all you have 7884 08:43:46,680 --> 08:43:48,360 to 7885 08:43:48,360 --> 08:43:53,238 remove or uninstall nest 7886 08:43:53,238 --> 08:43:56,440 jli because you will have have the older 7887 08:43:56,440 --> 08:43:59,238 version of n 7888 08:43:59,238 --> 08:44:03,080 CLI I'm using Mac so we need to add the 7889 08:44:03,080 --> 08:44:07,200 sudo keyword instead of install first we 7890 08:44:07,200 --> 08:44:10,280 need to 7891 08:44:10,280 --> 08:44:12,398 uninstall the 7892 08:44:12,398 --> 08:44:17,600 nli I have already installed 7893 08:44:18,440 --> 08:44:21,398 nli can I check the version of this 7894 08:44:21,398 --> 08:44:23,958 package I don't know let's try to look 7895 08:44:23,958 --> 08:44:27,040 at that 7896 08:44:27,840 --> 08:44:29,920 I have 7897 08:44:29,920 --> 08:44:33,718 8.1.0 I have I have the latest version 7898 08:44:33,718 --> 08:44:37,680 but it will generate the nestjs 10 7899 08:44:37,680 --> 08:44:40,680 project Nest 7900 08:44:40,680 --> 08:44:43,718 new and we have to provide the name of 7901 08:44:43,718 --> 08:44:47,558 our project I can 7902 08:44:48,920 --> 08:44:51,878 say web 7903 08:44:51,878 --> 08:44:53,958 socket 7904 08:44:53,958 --> 08:44:56,958 implementation 7905 08:45:04,798 --> 08:45:08,840 nextest let's do it again nextest new 7906 08:45:08,840 --> 08:45:12,638 the name of the project 7907 08:45:13,120 --> 08:45:16,120 websocket 7908 08:45:19,680 --> 08:45:23,798 implementation I would like to use the 7909 08:45:23,920 --> 08:45:25,600 npm 7910 08:45:25,600 --> 08:45:30,160 it will take some time to install these 7911 08:45:49,760 --> 08:45:52,638 dependencies my project has 7912 08:45:52,638 --> 08:45:58,080 created I can look at by going to this 7913 08:46:00,840 --> 08:46:03,478 directory I would like to open this 7914 08:46:03,478 --> 08:46:07,558 project into my vs code 7915 08:46:13,120 --> 08:46:16,558 editor if you check the package. jsn 7916 08:46:16,558 --> 08:46:19,520 file you can see we have the nest 7917 08:46:19,520 --> 08:46:22,000 version 7918 08:46:22,160 --> 08:46:25,680 10 now what I have to 7919 08:46:25,680 --> 08:46:27,000 what I have to 7920 08:46:27,000 --> 08:46:32,440 do we have to compile the code 7921 08:46:34,840 --> 08:46:38,238 faster let's first we have to run the 7922 08:46:38,238 --> 08:46:42,160 project without this command This 7923 08:46:42,160 --> 08:46:47,398 One S swc Speedy web compiler first we 7924 08:46:47,398 --> 08:46:49,000 have to run it 7925 08:46:49,000 --> 08:46:52,798 without Speedy web 7926 08:46:53,840 --> 08:46:56,840 compiler 7927 08:46:58,798 --> 08:47:03,600 you can look at the time for running the 7928 08:47:11,718 --> 08:47:15,238 project when you will have multiple 7929 08:47:15,238 --> 08:47:17,600 modules you will have multiple 7930 08:47:17,600 --> 08:47:20,000 controllers and you added the testing 7931 08:47:20,000 --> 08:47:22,238 functionality your application speed 7932 08:47:22,238 --> 08:47:24,398 time will be slower your development 7933 08:47:24,398 --> 08:47:26,200 time will will be 7934 08:47:26,200 --> 08:47:29,520 slow so let's make it 7935 08:47:29,520 --> 08:47:32,000 faster with the help 7936 08:47:32,000 --> 08:47:35,798 of these two options I want you to add 7937 08:47:35,798 --> 08:47:38,040 these 7938 08:47:38,040 --> 08:47:42,398 options into package into 7939 08:47:43,600 --> 08:47:46,200 nli n c. 7940 08:47:46,200 --> 08:47:49,318 Json I'm going to add these two options 7941 08:47:49,318 --> 08:47:51,798 to my 7942 08:47:53,760 --> 08:47:56,318 compiler these two 7943 08:47:56,318 --> 08:47:58,958 options Builder I would like to use the 7944 08:47:58,958 --> 08:48:02,398 S swc and type 7945 08:48:06,360 --> 08:48:10,200 check let's run the 7946 08:48:22,520 --> 08:48:26,080 project you can see that I got the issue 7947 08:48:26,080 --> 08:48:31,040 fail to load fail to load s swc CLI and 7948 08:48:31,040 --> 08:48:36,920 S swc core we got to install these two 7949 08:48:36,920 --> 08:48:39,440 packages let me execute this 7950 08:48:39,440 --> 08:48:40,878 [Music] 7951 08:48:40,878 --> 08:48:44,558 command install it as a def dependency 7952 08:48:44,558 --> 08:48:47,440 because I want to improve the project 7953 08:48:47,440 --> 08:48:49,120 running 7954 08:48:49,120 --> 08:48:51,760 time increase the 7955 08:48:51,760 --> 08:48:53,718 development 7956 08:48:53,718 --> 08:48:56,718 time 7957 08:49:01,600 --> 08:49:04,360 so packages have 7958 08:49:04,360 --> 08:49:07,600 installed now we need to run the 7959 08:49:07,600 --> 08:49:12,000 project now it can take the S 7960 08:49:13,360 --> 08:49:18,840 swc it can use this Builder and type 7961 08:49:21,798 --> 08:49:26,638 check our project is running 7962 08:49:34,840 --> 08:49:40,558 the project running time has increased 7963 08:49:40,558 --> 08:49:44,878 now if I save something in our 7964 08:49:44,878 --> 08:49:46,760 application let's 7965 08:49:46,760 --> 08:49:50,238 say in the app 7966 08:49:50,238 --> 08:49:52,150 controller in the app 7967 08:49:52,150 --> 08:49:53,638 [Music] 7968 08:49:53,638 --> 08:49:55,718 service 7969 08:49:55,718 --> 08:49:59,878 hello world from nestjs version 10 I 7970 08:49:59,878 --> 08:50:02,798 want to save the 7971 08:50:03,238 --> 08:50:07,000 application successfully 7972 08:50:10,238 --> 08:50:14,878 compiled this is how this is how you can 7973 08:50:14,878 --> 08:50:19,160 improve this implement or in improve the 7974 08:50:19,160 --> 08:50:23,478 development time the project running 7975 08:50:23,478 --> 08:50:27,360 time in development 7976 08:50:33,160 --> 08:50:35,440 environment in this module we're going 7977 08:50:35,440 --> 08:50:38,080 to work with web 7978 08:50:38,080 --> 08:50:41,040 sockets when you want to implement real 7979 08:50:41,040 --> 08:50:44,600 to real time application like chat web 7980 08:50:44,600 --> 08:50:47,200 application real time web 7981 08:50:47,200 --> 08:50:50,840 analytics live streaming online gaming 7982 08:50:50,840 --> 08:50:53,398 then you need web 7983 08:50:53,398 --> 08:50:57,318 socket which we have worked with HTTP 7984 08:50:57,318 --> 08:50:59,318 server in 7985 08:50:59,318 --> 08:51:02,638 HTTP where client sends the request in 7986 08:51:02,638 --> 08:51:04,600 the server 7987 08:51:04,600 --> 08:51:08,798 response but if you if you're using web 7988 08:51:08,798 --> 08:51:11,478 sockets web sockets establish a 7989 08:51:11,478 --> 08:51:13,760 persistent connection between client and 7990 08:51:13,760 --> 08:51:14,878 the 7991 08:51:14,878 --> 08:51:17,920 server and you can have ongoing 7992 08:51:17,920 --> 08:51:20,120 communication the best example is 7993 08:51:20,120 --> 08:51:22,878 Facebook Messenger you send a message to 7994 08:51:22,878 --> 08:51:25,200 your friend and your friend rep replies 7995 08:51:25,200 --> 08:51:26,920 and you reply to your friend and your 7996 08:51:26,920 --> 08:51:29,280 friend reply this is how the bir 7997 08:51:29,280 --> 08:51:31,840 directional communication Works in under 7998 08:51:31,840 --> 08:51:33,840 the hood they have implemented the 7999 08:51:33,840 --> 08:51:36,440 websocket protocol they are using the 8000 08:51:36,440 --> 08:51:38,600 websocket 8001 08:51:38,600 --> 08:51:40,318 technology if you need to make 8002 08:51:40,318 --> 08:51:43,200 persistant connection then you need 8003 08:51:43,200 --> 08:51:45,840 websocket you want to implement a 8004 08:51:45,840 --> 08:51:49,680 realtime feature like live updates live 8005 08:51:49,680 --> 08:51:53,520 score live streaming then you need 8006 08:51:53,520 --> 08:51:55,360 websocket 8007 08:51:55,360 --> 08:51:57,798 here is chat application can be built 8008 08:51:57,798 --> 08:52:00,520 with web socket collaborative editing 8009 08:52:00,520 --> 08:52:04,080 tools real time analytics online gaming 8010 08:52:04,080 --> 08:52:08,478 and the live streaming nestjs also 8011 08:52:08,478 --> 08:52:11,558 provides websocket module you can use 8012 08:52:11,558 --> 08:52:14,840 that but it also provide two approaches 8013 08:52:14,840 --> 08:52:16,280 you want to do you want to work with 8014 08:52:16,280 --> 08:52:18,160 socket iio or you want to use the web 8015 08:52:18,160 --> 08:52:20,600 socket you can get the details from 8016 08:52:20,600 --> 08:52:22,000 their official 8017 08:52:22,000 --> 08:52:24,080 documentation but in this video I'm 8018 08:52:24,080 --> 08:52:26,878 going to implement the socket 8019 08:52:26,878 --> 08:52:31,638 IO you got to install these two 8020 08:52:32,840 --> 08:52:35,238 packages I'm going to open my 8021 08:52:35,238 --> 08:52:40,398 package.json file let me add my 8022 08:52:40,440 --> 08:52:43,440 dependencies I think I already installed 8023 08:52:43,440 --> 08:52:45,558 these two dependencies I don't need to 8024 08:52:45,558 --> 08:52:48,878 do it again you can do it manually by 8025 08:52:48,878 --> 08:52:52,040 running npm install and first install 8026 08:52:52,040 --> 08:52:52,798 this 8027 08:52:52,798 --> 08:52:54,878 dependency and then you have have to 8028 08:52:54,878 --> 08:52:57,080 install these 8029 08:52:57,080 --> 08:53:00,040 dependencies our next step is to create 8030 08:53:00,040 --> 08:53:03,080 the events module we will have the 8031 08:53:03,080 --> 08:53:06,478 Gateway a Gateway will be responsible 8032 08:53:06,478 --> 08:53:10,120 for handling the events you will have 8033 08:53:10,120 --> 08:53:12,558 the events module a Gateway is nothing 8034 08:53:12,558 --> 08:53:15,398 more it's a class with provider you can 8035 08:53:15,398 --> 08:53:17,920 inject it as a 8036 08:53:17,920 --> 08:53:20,920 provider here we we need this Gateway 8037 08:53:20,920 --> 08:53:22,958 we're going to create the event Gateway 8038 08:53:22,958 --> 08:53:25,238 it's nothing more it's a class 8039 08:53:25,238 --> 08:53:28,360 here you have to decorate with websocket 8040 08:53:28,360 --> 08:53:32,718 Gateway I'm allowing any origin you can 8041 08:53:32,718 --> 08:53:35,360 use any specific origin if you 8042 08:53:35,360 --> 08:53:38,040 want here I'm trying to implement the 8043 08:53:38,040 --> 08:53:41,000 own module in 8044 08:53:41,200 --> 08:53:45,040 it when any client connected to my 8045 08:53:45,040 --> 08:53:47,520 application this event will be 8046 08:53:47,520 --> 08:53:49,680 triggered this is how you will register 8047 08:53:49,680 --> 08:53:53,360 the event by using the own own 8048 08:53:53,360 --> 08:53:57,238 function I can also get the socket ID I 8049 08:53:57,238 --> 08:54:01,600 can also log that have has it connected 8050 08:54:01,600 --> 08:54:03,920 or 8051 08:54:04,318 --> 08:54:06,878 not this is how you will create or 8052 08:54:06,878 --> 08:54:10,558 register a new event subscribe message 8053 08:54:10,558 --> 08:54:13,200 and we will have a function new message 8054 08:54:13,200 --> 08:54:15,238 you will have the message body you will 8055 08:54:15,238 --> 08:54:17,600 get the 8056 08:54:17,718 --> 08:54:22,680 data if someone sends message or filed 8057 08:54:22,680 --> 08:54:25,200 an event from the client side 8058 08:54:25,200 --> 08:54:28,958 application like react angular view or 8059 08:54:28,958 --> 08:54:30,680 any kind of front- end 8060 08:54:30,680 --> 08:54:34,398 application this function will be called 8061 08:54:34,398 --> 08:54:37,600 but make sure they have called this 8062 08:54:37,600 --> 08:54:41,360 event with message key this key and it 8063 08:54:41,360 --> 08:54:44,478 is going to lock the data you can read 8064 08:54:44,478 --> 08:54:48,760 this text here for every 8065 08:54:48,760 --> 08:54:53,318 detail let's create events 8066 08:54:53,318 --> 08:54:56,318 module 8067 08:54:57,520 --> 08:55:00,558 we have created the events module now 8068 08:55:00,558 --> 08:55:03,360 let's create the 8069 08:55:04,520 --> 08:55:09,000 Gateway you can see we have the events 8070 08:55:14,878 --> 08:55:17,600 module Gateway will be registered as a 8071 08:55:17,600 --> 08:55:22,080 provider you can see in the events 8072 08:55:22,080 --> 08:55:26,398 module we have websocket Gateway by 8073 08:55:26,398 --> 08:55:31,440 using this decorator and we have one 8074 08:55:32,760 --> 08:55:38,638 event if you want you can add the 8075 08:55:42,798 --> 08:55:47,280 CES and we also need to establish a 8076 08:55:47,280 --> 08:55:51,680 connection I need to check make sure you 8077 08:55:51,680 --> 08:55:55,680 imported server from socket IO 8078 08:55:55,680 --> 08:55:59,238 Library you have to import it manually 8079 08:55:59,238 --> 08:56:01,080 otherwise it will not 8080 08:56:01,080 --> 08:56:05,398 work websocket G websocket 8081 08:56:05,398 --> 08:56:08,600 server now we have this handle 8082 08:56:08,600 --> 08:56:10,840 message subscribe 8083 08:56:10,840 --> 08:56:13,920 message when you need to register an 8084 08:56:13,920 --> 08:56:16,360 event in backend application or the 8085 08:56:16,360 --> 08:56:18,558 nestjs application you will use the 8086 08:56:18,558 --> 08:56:21,558 Subscribe message 8087 08:56:23,238 --> 08:56:26,238 decorator 8088 08:56:27,920 --> 08:56:31,558 and what I would like to do I want to 8089 08:56:31,558 --> 08:56:34,280 get the data or you can have client but 8090 08:56:34,280 --> 08:56:37,760 I like the data I don't have any payload 8091 08:56:37,760 --> 08:56:41,200 option but I'll use the message body 8092 08:56:41,200 --> 08:56:44,200 decorator instead of use this one 8093 08:56:44,200 --> 08:56:46,398 message 8094 08:56:46,398 --> 08:56:51,440 body data should be type 8095 08:56:51,440 --> 08:56:54,558 any I'm not going to return any thing 8096 08:56:54,558 --> 08:56:57,200 right now all I want to log the 8097 08:56:57,200 --> 08:57:01,120 message I can say message received from 8098 08:57:01,120 --> 08:57:03,318 the 8099 08:57:03,478 --> 08:57:06,760 client and this is my 8100 08:57:06,760 --> 08:57:11,718 message or I can log the 8101 08:57:13,600 --> 08:57:16,718 data so far we have registered the event 8102 08:57:16,718 --> 08:57:19,718 when any web application like any 8103 08:57:19,718 --> 08:57:22,000 front-end application has connected to 8104 08:57:22,000 --> 08:57:24,638 our websocket server 8105 08:57:24,638 --> 08:57:27,318 first they need to connect on the our 8106 08:57:27,318 --> 08:57:30,318 host here we have provided but right now 8107 08:57:30,318 --> 08:57:31,958 we're going to run the project on Local 8108 08:57:31,958 --> 08:57:35,558 Host so any front end application should 8109 08:57:35,558 --> 08:57:39,360 connect with Local Host colon 3000 and 8110 08:57:39,360 --> 08:57:41,798 then he can establish a connection 8111 08:57:41,798 --> 08:57:44,920 between with with this application when 8112 08:57:44,920 --> 08:57:47,040 when any front end application establish 8113 08:57:47,040 --> 08:57:49,318 the connection it's going to call the 8114 08:57:49,318 --> 08:57:51,478 own module in it but right now I don't 8115 08:57:51,478 --> 08:57:53,440 have any own module in it right now it 8116 08:57:53,440 --> 08:57:54,840 will log 8117 08:57:54,840 --> 08:57:57,120 this 8118 08:57:58,638 --> 08:58:00,520 message 8119 08:58:00,520 --> 08:58:05,160 implements on module in it that's it 8120 08:58:05,160 --> 08:58:08,920 after module loaded or this file is this 8121 08:58:08,920 --> 08:58:11,040 class is loaded successfully then we can 8122 08:58:11,040 --> 08:58:12,080 log the 8123 08:58:12,080 --> 08:58:14,840 message when connection established this 8124 08:58:14,840 --> 08:58:16,718 event will be triggered and it is going 8125 08:58:16,718 --> 08:58:19,360 to call with socket I ID and the 8126 08:58:19,360 --> 08:58:21,920 connected its Boolean 8127 08:58:21,920 --> 08:58:26,120 value now we have set of the websocket 8128 08:58:26,120 --> 08:58:29,958 server and we created our first event 8129 08:58:29,958 --> 08:58:32,478 which is message you can have multiple 8130 08:58:32,478 --> 08:58:35,280 events here you can also perform the DB 8131 08:58:35,280 --> 08:58:37,040 operation like if someone sends a 8132 08:58:37,040 --> 08:58:41,398 message you can save the message into 8133 08:58:48,478 --> 08:58:51,280 database in the next video in the next 8134 08:58:51,280 --> 08:58:54,478 lesson we're going to consume this event 8135 08:58:54,478 --> 08:58:56,840 from our frontend application we will 8136 08:58:56,840 --> 08:58:58,878 have a simple basic front end 8137 08:58:58,878 --> 08:59:01,760 application with index.html and there 8138 08:59:01,760 --> 08:59:05,680 would be JavaScript in the index.html 8139 08:59:05,680 --> 08:59:08,478 file now it's time to consume the 8140 08:59:08,478 --> 08:59:12,200 application consume the event or send 8141 08:59:12,200 --> 08:59:15,478 message from the frontend application 8142 08:59:15,478 --> 08:59:18,840 when you run your server let's say npm 8143 08:59:18,840 --> 08:59:21,238 Run start 8144 08:59:21,238 --> 08:59:24,520 def now my server is it's ready to 8145 08:59:24,520 --> 08:59:28,120 listen this message event it's waiting 8146 08:59:28,120 --> 08:59:32,000 for someone to send a message by using 8147 08:59:32,000 --> 08:59:33,238 this event 8148 08:59:33,238 --> 08:59:37,080 key so all you need to do you got to 8149 08:59:37,080 --> 08:59:39,520 create a 8150 08:59:39,520 --> 08:59:41,080 basic 8151 08:59:41,080 --> 08:59:43,920 index.html file you can copy that code 8152 08:59:43,920 --> 08:59:46,558 or you can create manually I can say 8153 08:59:46,558 --> 08:59:49,718 client here I will have 8154 08:59:49,718 --> 08:59:52,958 index.html file one more thing you will 8155 08:59:52,958 --> 08:59:56,920 see events Gateway subscribed to the 8156 08:59:56,920 --> 09:00:00,360 message event to this 8157 09:00:00,360 --> 09:00:02,798 message so it means we successfully 8158 09:00:02,798 --> 09:00:05,200 created the event I'm going to copy the 8159 09:00:05,200 --> 09:00:09,600 code I'm using the socket IO Library 8160 09:00:09,600 --> 09:00:12,520 where we are creating the socket IO 8161 09:00:12,520 --> 09:00:14,840 instance by providing our application 8162 09:00:14,840 --> 09:00:17,798 path in our case we have nest's 8163 09:00:17,798 --> 09:00:21,280 application is running on Port 8164 09:00:21,280 --> 09:00:25,920 3000 we have registered a event 8165 09:00:26,878 --> 09:00:30,760 it's trying to connect with our 8166 09:00:30,760 --> 09:00:33,600 application with nestjs application when 8167 09:00:33,600 --> 09:00:35,478 it when it will be connected it is going 8168 09:00:35,478 --> 09:00:39,040 to log this message and emit this 8169 09:00:39,040 --> 09:00:41,318 message to the server here I'm going to 8170 09:00:41,318 --> 09:00:42,398 open the 8171 09:00:42,398 --> 09:00:45,160 server this one events 8172 09:00:45,160 --> 09:00:48,040 Gateway we have registered the message 8173 09:00:48,040 --> 09:00:50,958 event this is how you can emit the event 8174 09:00:50,958 --> 09:00:55,718 I'm telling to socket IO please call 8175 09:00:55,718 --> 09:00:58,798 this function that's it and we also 8176 09:00:58,798 --> 09:01:01,160 registered couple of events if someone 8177 09:01:01,160 --> 09:01:06,120 if if server sends data back to the 8178 09:01:06,120 --> 09:01:08,760 client we have to register it we have to 8179 09:01:08,760 --> 09:01:11,280 catch the data here that's why I use the 8180 09:01:11,280 --> 09:01:12,398 on 8181 09:01:12,398 --> 09:01:15,120 listener if exception event trigger it 8182 09:01:15,120 --> 09:01:18,080 is going to call this method if if our 8183 09:01:18,080 --> 09:01:20,200 backend application disconnected or 8184 09:01:20,200 --> 09:01:23,120 stopped it is going to call this event 8185 09:01:23,120 --> 09:01:25,798 it is going to trigger this event now we 8186 09:01:25,798 --> 09:01:27,680 right now we're not returning anything 8187 09:01:27,680 --> 09:01:31,360 here let's try to test 8188 09:01:31,600 --> 09:01:35,920 it uh I'm going to use open Live server 8189 09:01:35,920 --> 09:01:38,718 go live when you open the index.html you 8190 09:01:38,718 --> 09:01:39,520 will 8191 09:01:39,520 --> 09:01:43,360 see this it is going to open my project 8192 09:01:43,360 --> 09:01:44,840 into live server please check the 8193 09:01:44,840 --> 09:01:48,760 console for message 8194 09:01:50,280 --> 09:01:54,718 reply inside the console you will 8195 09:01:54,718 --> 09:01:57,558 see you have connected 8196 09:01:57,558 --> 09:02:01,558 successfully when you open your backend 8197 09:02:01,558 --> 09:02:03,798 application this message is received 8198 09:02:03,798 --> 09:02:05,478 from the 8199 09:02:05,478 --> 09:02:10,040 client so client has sent this message 8200 09:02:10,040 --> 09:02:11,558 or you can 8201 09:02:11,558 --> 09:02:15,040 say I am the 8202 09:02:15,040 --> 09:02:17,520 client I'm going to reload the 8203 09:02:17,520 --> 09:02:21,200 application it has automatically reload 8204 09:02:21,200 --> 09:02:24,798 reloaded you can see I'm the client 8205 09:02:24,798 --> 09:02:27,200 so it has also connected one more thing 8206 09:02:27,200 --> 09:02:30,760 you will also see this socket ID and it 8207 09:02:30,760 --> 09:02:33,000 has it means connected successfully 8208 09:02:33,000 --> 09:02:35,360 first it is going to log this message 8209 09:02:35,360 --> 09:02:39,120 console. ID Lo connected we have emitted 8210 09:02:39,120 --> 09:02:41,840 this message event it's going to call 8211 09:02:41,840 --> 09:02:42,920 this 8212 09:02:42,920 --> 09:02:46,440 function now we we are not returning 8213 09:02:46,440 --> 09:02:48,958 anything that's why this event is not 8214 09:02:48,958 --> 09:02:51,760 triggered let's try to return from 8215 09:02:51,760 --> 09:02:54,558 return something I'm going to return 8216 09:02:54,558 --> 09:02:57,558 some value let's 8217 09:02:58,558 --> 09:03:01,440 say some value from server let's see 8218 09:03:01,440 --> 09:03:03,920 what will 8219 09:03:04,200 --> 09:03:07,760 happen our application 8220 09:03:07,760 --> 09:03:10,160 reloaded let's try to test it out 8221 09:03:10,160 --> 09:03:13,680 message received from the 8222 09:03:17,558 --> 09:03:21,718 client but I did not log what message 8223 09:03:21,718 --> 09:03:25,718 have you received 8224 09:03:35,000 --> 09:03:38,160 you can see message received from the 8225 09:03:38,160 --> 09:03:41,280 client on the client side we sent this 8226 09:03:41,280 --> 09:03:45,440 message I'm the client it has 8227 09:03:45,440 --> 09:03:48,238 received but it is not triggering this 8228 09:03:48,238 --> 09:03:51,440 event I cannot see anything here let's 8229 09:03:51,440 --> 09:03:55,718 say if I if I'll try to log 8230 09:03:55,718 --> 09:03:58,718 something 8231 09:03:59,120 --> 09:04:02,760 hello I cannot see this Hello message in 8232 09:04:02,760 --> 09:04:04,398 my 8233 09:04:04,398 --> 09:04:06,798 console it's not 8234 09:04:06,798 --> 09:04:09,840 triggering so that is why you need 8235 09:04:09,840 --> 09:04:13,520 observable websocket also provides the 8236 09:04:13,520 --> 09:04:15,840 observable you can 8237 09:04:15,840 --> 09:04:20,958 say I would like to return this 8238 09:04:22,638 --> 09:04:25,638 observable 8239 09:04:25,840 --> 09:04:30,238 websocket response and I can have any 8240 09:04:30,238 --> 09:04:32,398 type here we have 8241 09:04:32,398 --> 09:04:35,958 to convert the object to observable 8242 09:04:35,958 --> 09:04:37,840 that's why I'm going to use the off 8243 09:04:37,840 --> 09:04:41,120 operator from 8244 09:04:42,318 --> 09:04:44,760 rxjs I can say 8245 09:04:44,760 --> 09:04:47,238 event you got to provide the name of the 8246 09:04:47,238 --> 09:04:50,000 event which is message and the 8247 09:04:50,000 --> 09:04:52,638 data here you can return anything from 8248 09:04:52,638 --> 09:04:54,798 the server 8249 09:04:54,798 --> 09:04:56,558 message 8250 09:04:56,558 --> 09:04:58,760 returned from 8251 09:04:58,760 --> 09:05:01,558 server 8252 09:05:01,558 --> 09:05:04,958 hello you should see this message here 8253 09:05:04,958 --> 09:05:09,238 because we are listening on the client 8254 09:05:09,238 --> 09:05:14,680 side you can see event message received 8255 09:05:14,680 --> 09:05:18,440 from the server if I will refresh our 8256 09:05:18,440 --> 09:05:20,000 application you will 8257 09:05:20,000 --> 09:05:22,638 see this 8258 09:05:22,638 --> 09:05:27,638 one message returned from the server 8259 09:05:28,120 --> 09:05:31,440 hello if I stopped my application it is 8260 09:05:31,440 --> 09:05:34,958 going to F this or trigger this 8261 09:05:34,958 --> 09:05:38,958 disconnect event you should see the 8262 09:05:38,958 --> 09:05:42,478 disconnected this is how you will create 8263 09:05:42,478 --> 09:05:45,200 wi directional or realtime chat 8264 09:05:45,200 --> 09:05:48,440 application with web 8265 09:05:49,718 --> 09:05:52,360 socket in this lesson I'm going to teach 8266 09:05:52,360 --> 09:05:56,040 you how to set up graphql server we're 8267 09:05:56,040 --> 09:05:59,600 going to use Apollo server nestjs Apollo 8268 09:05:59,600 --> 09:06:03,520 package nestjs graphql package graphql 8269 09:06:03,520 --> 09:06:04,798 and TS 8270 09:06:04,798 --> 09:06:07,958 Mor you can read the documentation of 8271 09:06:07,958 --> 09:06:10,398 these 8272 09:06:11,917 --> 09:06:15,122 packages I'm assuming you must know the 8273 09:06:15,122 --> 09:06:18,917 basics of graph ql I'm not going to 8274 09:06:18,917 --> 09:06:21,237 teach you the basics of graphql what is 8275 09:06:21,237 --> 09:06:25,519 graphql and why do we need graph Q 8276 09:06:25,758 --> 09:06:28,480 I have attached a started kit project 8277 09:06:28,480 --> 09:06:32,360 you have to open it and run npm 8278 09:06:32,360 --> 09:06:35,000 install I have already I have already 8279 09:06:35,000 --> 09:06:38,000 installed all these dependencies I don't 8280 09:06:38,000 --> 09:06:40,360 need to install them 8281 09:06:40,360 --> 09:06:44,718 again all I need to do is to install 8282 09:06:44,718 --> 09:06:47,917 graphql packages I'm going to copy all 8283 09:06:47,917 --> 09:06:50,557 of these 8284 09:06:52,519 --> 09:06:55,000 packages 8285 09:06:55,000 --> 09:06:58,598 the packages have installed 8286 09:06:58,598 --> 09:07:02,758 successfully and now the next step we 8287 09:07:02,758 --> 09:07:08,238 have to register the graphql 8288 09:07:10,040 --> 09:07:13,438 module I'm going to open my app 8289 09:07:13,438 --> 09:07:16,480 module and we I would like to import 8290 09:07:16,480 --> 09:07:19,122 graph module 8291 09:07:19,122 --> 09:07:21,398 here we need to import couple of 8292 09:07:21,398 --> 09:07:24,718 dependencies Apollo driver config 8293 09:07:24,718 --> 09:07:27,078 I'm telling graphql hey I want to use 8294 09:07:27,078 --> 09:07:28,598 Apollo 8295 09:07:28,598 --> 09:07:32,519 driver and I will have do graphql 8296 09:07:32,519 --> 09:07:35,839 file where I will place all my schema 8297 09:07:35,839 --> 09:07:39,717 definitions in the graphql 8298 09:07:41,237 --> 09:07:44,877 file this file will have the typescript 8299 09:07:44,877 --> 09:07:46,718 typings of 8300 09:07:46,718 --> 09:07:50,078 graphql and output as a class don't 8301 09:07:50,078 --> 09:07:51,398 worry I'll Teach You How can you 8302 09:07:51,398 --> 09:07:55,438 generate the typings 8303 09:07:57,438 --> 09:08:00,078 we are using for RO to register the 8304 09:08:00,078 --> 09:08:01,718 graphql 8305 09:08:01,718 --> 09:08:05,320 module the Apollo driver config specify 8306 09:08:05,320 --> 09:08:08,000 the type of the configuration options 8307 09:08:08,000 --> 09:08:09,960 being 8308 09:08:09,960 --> 09:08:13,237 provided so driver AP poo driver this 8309 09:08:13,237 --> 09:08:15,557 property sets the driver used by the 8310 09:08:15,557 --> 09:08:18,360 graphql module in this case we are 8311 09:08:18,360 --> 09:08:20,480 telling graph Nest chairs hey I want to 8312 09:08:20,480 --> 09:08:22,438 use a polo 8313 09:08:22,438 --> 09:08:24,718 driver 8314 09:08:24,718 --> 09:08:27,839 we will place all of our graphql schema 8315 09:08:27,839 --> 09:08:32,040 in graphql file the extension should be 8316 09:08:32,040 --> 09:08:34,360 do 8317 09:08:36,680 --> 09:08:40,078 graphql uh if you want to generate 8318 09:08:40,078 --> 09:08:42,278 typescript definition from your graphql 8319 09:08:42,278 --> 09:08:44,360 schema you have to specify these 8320 09:08:44,360 --> 09:08:46,718 definitions I'll teach you how to do 8321 09:08:46,718 --> 09:08:51,122 that and we are just combining the 8322 09:08:51,122 --> 09:08:55,438 path we will have so graph. TS file 8323 09:08:55,438 --> 09:08:57,960 inside the source 8324 09:08:57,960 --> 09:09:00,519 directory this property specified that 8325 09:09:00,519 --> 09:09:02,237 the generated type script definition 8326 09:09:02,237 --> 09:09:05,278 should be outputed as a class this means 8327 09:09:05,278 --> 09:09:06,877 that the generated code will be 8328 09:09:06,877 --> 09:09:09,598 representing represent the types as 8329 09:09:09,598 --> 09:09:12,238 typescript 8330 09:09:12,360 --> 09:09:16,917 classes now our next step is to create a 8331 09:09:16,917 --> 09:09:19,479 schema 8332 09:09:22,320 --> 09:09:24,877 file 8333 09:09:24,877 --> 09:09:26,758 inside the song folder I'm going to 8334 09:09:26,758 --> 09:09:29,278 create a new file song. 8335 09:09:29,278 --> 09:09:31,877 graphql here I will place all of my 8336 09:09:31,877 --> 09:09:36,122 schema file we're going to have a 8337 09:09:36,122 --> 09:09:39,960 query let's define a song 8338 09:09:39,960 --> 09:09:44,480 type we will have ID and the type of ID 8339 09:09:44,480 --> 09:09:47,480 is required this means it is a required 8340 09:09:47,480 --> 09:09:49,199 property it should not be 8341 09:09:49,199 --> 09:09:53,758 null and I'm going to add a string 8342 09:09:53,758 --> 09:09:56,398 variable or string type field which is 8343 09:09:56,398 --> 09:10:00,160 title the type of the title is string 8344 09:10:00,160 --> 09:10:03,320 and I'm also going to add a new query 8345 09:10:03,320 --> 09:10:06,278 type here I can say I want to fetch all 8346 09:10:06,278 --> 09:10:07,839 the 8347 09:10:07,839 --> 09:10:10,519 songs and you can say the type should be 8348 09:10:10,519 --> 09:10:14,519 song and it is going to return all the 8349 09:10:14,519 --> 09:10:17,917 songs our next step is to generate the 8350 09:10:17,917 --> 09:10:22,237 typings which is typescript definition 8351 09:10:22,237 --> 09:10:24,519 file 8352 09:10:24,519 --> 09:10:28,160 we need a graphql definition 8353 09:10:28,160 --> 09:10:29,839 Factory 8354 09:10:29,839 --> 09:10:33,438 class here we are trying to in we are 8355 09:10:33,438 --> 09:10:36,480 instantiating or creating a new object 8356 09:10:36,480 --> 09:10:40,278 from graph definition Factory there is a 8357 09:10:40,278 --> 09:10:43,278 generate method to generate the typing 8358 09:10:43,278 --> 09:10:46,718 you have to specify the type which is 8359 09:10:46,718 --> 09:10:50,237 I'm using the regular expression here I 8360 09:10:50,237 --> 09:10:53,398 will have all of my schema file in the 8361 09:10:53,398 --> 09:10:57,598 cql that's why I use the type definition 8362 09:10:57,598 --> 09:11:00,877 path and I would like to I would like to 8363 09:11:00,877 --> 09:11:03,160 create all the type typescript 8364 09:11:03,160 --> 09:11:07,398 definition in the graphql dots file it 8365 09:11:07,398 --> 09:11:10,877 will save it as a class let's create a 8366 09:11:10,877 --> 09:11:11,800 new 8367 09:11:11,800 --> 09:11:15,237 file generate typings dots file inside 8368 09:11:15,237 --> 09:11:17,679 the root 8369 09:11:19,078 --> 09:11:22,237 directory typings 8370 09:11:22,237 --> 09:11:25,237 dots 8371 09:11:27,122 --> 09:11:30,438 now all you need to do is to execute 8372 09:11:30,438 --> 09:11:31,680 this 8373 09:11:31,680 --> 09:11:38,122 command NPS npx DS node generate 8374 09:11:40,877 --> 09:11:43,237 typings it is going to generate the 8375 09:11:43,237 --> 09:11:48,359 graphql dots file with this 8376 09:11:48,438 --> 09:11:51,758 definition you can see that a graphql 8377 09:11:51,758 --> 09:11:53,718 dots file has 8378 09:11:53,718 --> 09:11:57,438 created we have a class which is song 8379 09:11:57,438 --> 09:12:00,519 and we have a abstract type which is I 8380 09:12:00,519 --> 09:12:03,160 query and we have this 8381 09:12:03,160 --> 09:12:07,199 type so we will use this type definition 8382 09:12:07,199 --> 09:12:10,438 when we will creating disolver functions 8383 09:12:10,438 --> 09:12:13,360 when we will resolve queries against 8384 09:12:13,360 --> 09:12:15,040 your schema 8385 09:12:15,040 --> 09:12:18,598 files now I would like to run the 8386 09:12:18,598 --> 09:12:21,718 application is everything fine now let's 8387 09:12:21,718 --> 09:12:25,122 test it 8388 09:12:27,199 --> 09:12:29,237 my application is running 8389 09:12:29,237 --> 09:12:32,278 now let's send the request to Local Host 8390 09:12:32,278 --> 09:12:34,519 colon 8391 09:12:34,519 --> 09:12:38,122 3000 and you will have playground at 8392 09:12:38,122 --> 09:12:40,078 this graph quel 8393 09:12:40,078 --> 09:12:42,598 Endo when you go to the Local Host colon 8394 09:12:42,598 --> 09:12:45,839 3000 you will see the playground this is 8395 09:12:45,839 --> 09:12:48,800 how you will perform 8396 09:12:48,800 --> 09:12:52,278 graphql apis you will test graphql apis 8397 09:12:52,278 --> 09:12:54,480 by using this 8398 09:12:54,480 --> 09:12:57,800 playground let's 8399 09:12:59,160 --> 09:13:02,398 say I'm going to send the API request 8400 09:13:02,398 --> 09:13:04,519 graphical API request I want to fetch 8401 09:13:04,519 --> 09:13:07,877 all the songs I want to see the ID and 8402 09:13:07,877 --> 09:13:10,519 the title but it is going to give me the 8403 09:13:10,519 --> 09:13:12,438 error hey you don't you are not 8404 09:13:12,438 --> 09:13:14,557 resolving this 8405 09:13:14,557 --> 09:13:17,320 query because it's returning 8406 09:13:17,320 --> 09:13:21,398 n it's getting the validation eror 8407 09:13:21,398 --> 09:13:24,480 because we don't have any resolver to 8408 09:13:24,480 --> 09:13:28,480 resolve this query this 8409 09:13:29,122 --> 09:13:32,800 one in the next video I'll teach you how 8410 09:13:32,800 --> 09:13:37,000 to create mutations and how to create 8411 09:13:37,000 --> 09:13:39,078 queries we're going to we're going to 8412 09:13:39,078 --> 09:13:42,839 fill our song 8413 09:13:42,839 --> 09:13:47,277 schema let's complete our 8414 09:13:50,877 --> 09:13:53,718 schema let's create a new 8415 09:13:53,718 --> 09:13:57,640 query to fetch a single 8416 09:13:57,640 --> 09:14:01,320 song when you try to fetch a single song 8417 09:14:01,320 --> 09:14:04,680 you must need to provide the ID so ID 8418 09:14:04,680 --> 09:14:06,278 should not be 8419 09:14:06,278 --> 09:14:09,237 null and it should return this 8420 09:14:09,237 --> 09:14:11,917 song in the 8421 09:14:11,917 --> 09:14:16,479 response let's write a new 8422 09:14:16,877 --> 09:14:20,598 query it should give me the 8423 09:14:20,598 --> 09:14:23,438 song and I would like to fetch the the 8424 09:14:23,438 --> 09:14:26,557 song on the based on 8425 09:14:26,557 --> 09:14:29,438 id id should not be 8426 09:14:29,438 --> 09:14:33,040 null now we're going to write uh venue 8427 09:14:33,040 --> 09:14:35,360 query in the playground you got to 8428 09:14:35,360 --> 09:14:37,398 follow the structure song and you have 8429 09:14:37,398 --> 09:14:40,519 to provide the ID in this case I'm 8430 09:14:40,519 --> 09:14:44,237 providing the 1 2 3 as an 8431 09:14:44,237 --> 09:14:47,438 ID now we're going to Define mutations 8432 09:14:47,438 --> 09:14:49,519 we learned how to fetch data with the 8433 09:14:49,519 --> 09:14:53,160 help of query but I want to write the 8434 09:14:53,160 --> 09:14:58,960 data so to to mutate the data you need a 8435 09:14:58,960 --> 09:15:03,598 mutation so let's define an mut 8436 09:15:03,598 --> 09:15:05,438 mutation 8437 09:15:05,438 --> 09:15:08,278 mutation I'm going to have create 8438 09:15:08,278 --> 09:15:11,360 song it will accept the create song 8439 09:15:11,360 --> 09:15:12,718 input 8440 09:15:12,718 --> 09:15:16,718 or create song 8441 09:15:16,718 --> 09:15:19,557 input and I don't have any type so 8442 09:15:19,557 --> 09:15:21,917 that's why I'm going to define the 8443 09:15:21,917 --> 09:15:25,839 input create song 8444 09:15:30,078 --> 09:15:33,278 input I will have the title field and 8445 09:15:33,278 --> 09:15:35,360 the type should be string it should not 8446 09:15:35,360 --> 09:15:39,438 be null so I can say the type of create 8447 09:15:39,438 --> 09:15:40,398 song 8448 09:15:40,398 --> 09:15:44,320 input it should be create song input and 8449 09:15:44,320 --> 09:15:47,160 it should not be null and it should give 8450 09:15:47,160 --> 09:15:50,680 me the song in the 8451 09:15:51,917 --> 09:15:54,917 response 8452 09:15:57,640 --> 09:16:00,278 now I want to write the mutation for 8453 09:16:00,278 --> 09:16:04,398 delete song or you can say update 8454 09:16:04,398 --> 09:16:06,839 song I would like to update the song 8455 09:16:06,839 --> 09:16:09,199 Only the based on ID the first argument 8456 09:16:09,199 --> 09:16:13,199 it should be ID and the update song 8457 09:16:13,199 --> 09:16:16,758 input we will have update song 8458 09:16:16,758 --> 09:16:21,278 input it should give me the update 8459 09:16:21,839 --> 09:16:25,000 result this is not an empty 8460 09:16:25,000 --> 09:16:29,160 field that's why I use this exclamation 8461 09:16:29,160 --> 09:16:31,598 mark and 8462 09:16:31,598 --> 09:16:33,960 input 8463 09:16:33,960 --> 09:16:36,758 update song 8464 09:16:36,758 --> 09:16:42,680 input title should be string but I did 8465 09:16:42,680 --> 09:16:45,758 not mention it to required property if 8466 09:16:45,758 --> 09:16:48,237 you will have multiple song multiple 8467 09:16:48,237 --> 09:16:51,839 fields in song let's say a user will 8468 09:16:51,839 --> 09:16:55,877 have let's say a song can have 8469 09:16:55,877 --> 09:16:59,639 date or you can 8470 09:17:00,640 --> 09:17:07,840 say date release date or publish date or 8471 09:17:08,160 --> 09:17:12,438 artist something like that but I cannot 8472 09:17:12,438 --> 09:17:16,278 set date required in the artist required 8473 09:17:16,278 --> 09:17:19,199 field because it depends on the user a 8474 09:17:19,199 --> 09:17:22,199 user can update the title or whatever he 8475 09:17:22,199 --> 09:17:23,598 wants to upate 8476 09:17:23,598 --> 09:17:26,360 update now I don't have any type for 8477 09:17:26,360 --> 09:17:27,360 update 8478 09:17:27,360 --> 09:17:30,917 result let's define a new type update 8479 09:17:30,917 --> 09:17:33,438 result it will have 8480 09:17:33,438 --> 09:17:36,960 affected and type should be 8481 09:17:36,960 --> 09:17:42,000 number do we have int yes 8482 09:17:42,078 --> 09:17:45,122 int similarly we will have a type for 8483 09:17:45,122 --> 09:17:47,598 delete 8484 09:17:48,160 --> 09:17:51,360 result delete 8485 09:17:51,360 --> 09:17:54,877 result and typ should be int now we're 8486 09:17:54,877 --> 09:17:58,757 going to Define mutation for delete 8487 09:17:59,519 --> 09:18:03,199 song I can delete on the based on 8488 09:18:03,199 --> 09:18:06,398 ID here I can say delete 8489 09:18:06,398 --> 09:18:09,960 song I don't need to put anything and it 8490 09:18:09,960 --> 09:18:13,360 should give me the delete result that's 8491 09:18:13,360 --> 09:18:16,877 it so we did not resolve these queries 8492 09:18:16,877 --> 09:18:19,557 in mutations in the next video we're 8493 09:18:19,557 --> 09:18:21,598 going to write resolver functions to 8494 09:18:21,598 --> 09:18:25,360 resolve these query and mutations Let's 8495 09:18:25,360 --> 09:18:28,160 test it out by generating the 8496 09:18:28,160 --> 09:18:31,800 type one more thing I want to show you 8497 09:18:31,800 --> 09:18:34,640 can create a script to run the typings I 8498 09:18:34,640 --> 09:18:37,237 can say in the package.json 8499 09:18:37,237 --> 09:18:41,718 file let's define a new 8500 09:18:41,718 --> 09:18:44,877 script we here you need to define the 8501 09:18:44,877 --> 09:18:49,000 script generate slash colon typings TS 8502 09:18:49,000 --> 09:18:52,398 node generate typings this njs project 8503 09:18:52,398 --> 09:18:55,078 all already installed TS node as a def 8504 09:18:55,078 --> 09:18:57,438 dependency you don't need to install it 8505 09:18:57,438 --> 09:18:59,758 again npm 8506 09:18:59,758 --> 09:19:04,640 run I can say generate 8507 09:19:09,758 --> 09:19:13,360 typings it will generate the typings or 8508 09:19:13,360 --> 09:19:18,400 types in the graphql dots 8509 09:19:21,640 --> 09:19:24,640 file 8510 09:19:28,240 --> 09:19:31,040 I found an 8511 09:19:31,120 --> 09:19:35,436 error unknown type create song input did 8512 09:19:35,436 --> 09:19:37,800 you mean create song input or update 8513 09:19:37,800 --> 09:19:39,880 song 8514 09:19:39,880 --> 09:19:44,520 input oops I have to fix 8515 09:19:47,200 --> 09:19:50,560 that everything is fine now let me see 8516 09:19:50,560 --> 09:19:53,360 my graphql dots file we have type for 8517 09:19:53,360 --> 09:19:57,120 create song input type for update song 8518 09:19:57,120 --> 09:20:01,320 input we have types for mutations and 8519 09:20:01,320 --> 09:20:04,756 update result and delete 8520 09:20:13,040 --> 09:20:16,160 result when you see your 8521 09:20:16,160 --> 09:20:18,320 playground you have to look at the 8522 09:20:18,320 --> 09:20:24,640 queries in mutations we have queries and 8523 09:20:24,840 --> 09:20:28,560 mutation when you try to 8524 09:20:28,560 --> 09:20:32,880 fetch let's say I want a query for 8525 09:20:32,880 --> 09:20:35,520 songs ID let's say I'm going to provide 8526 09:20:35,520 --> 09:20:36,640 invalid 8527 09:20:36,640 --> 09:20:39,916 ID let's try to fetch the title and the 8528 09:20:39,916 --> 09:20:41,960 ID I'm going to send the request it 8529 09:20:41,960 --> 09:20:45,320 should give me the error hey you are not 8530 09:20:45,320 --> 09:20:49,040 resolving that's why we need resolver I 8531 09:20:49,040 --> 09:20:54,320 can use n CLI to generate a new resolver 8532 09:20:54,320 --> 09:20:56,400 I can say 8533 09:20:56,400 --> 09:20:59,800 song it's going to create a new resolver 8534 09:20:59,800 --> 09:21:02,000 resolver is nothing more it's a provider 8535 09:21:02,000 --> 09:21:06,756 like a service you can inject it as a 8536 09:21:08,800 --> 09:21:11,240 dependency here you can see 8537 09:21:11,240 --> 09:21:15,400 that inside the providers we have a song 8538 09:21:15,400 --> 09:21:18,360 resolver and it is it also created the 8539 09:21:18,360 --> 09:21:21,080 song resolver dopc file and song 8540 09:21:21,080 --> 09:21:23,916 resolver we have resolver 8541 09:21:23,916 --> 09:21:28,480 Tator now I'm going to resolve these 8542 09:21:28,480 --> 09:21:30,720 queries I'm going to open song do 8543 09:21:30,720 --> 09:21:34,360 graphql let's resolve this 8544 09:21:34,360 --> 09:21:37,720 query I also need song 8545 09:21:37,720 --> 09:21:40,640 service so Constructor 8546 09:21:40,640 --> 09:21:46,000 private song service song 8547 09:21:48,080 --> 09:21:51,520 service let's import 8548 09:21:51,520 --> 09:21:54,520 it 8549 09:22:00,560 --> 09:22:05,000 I have this I need a query 8550 09:22:05,120 --> 09:22:07,480 decorator here I would like to resolve 8551 09:22:07,480 --> 09:22:10,000 this query 8552 09:22:10,000 --> 09:22:12,010 songs I can say get 8553 09:22:12,010 --> 09:22:14,120 [Music] 8554 09:22:14,120 --> 09:22:16,916 songs return this 8555 09:22:16,916 --> 09:22:23,040 do song service dot get songs 8556 09:22:23,040 --> 09:22:26,160 and you can also mention your return 8557 09:22:26,160 --> 09:22:30,276 type I can get it from the song entity 8558 09:22:30,276 --> 09:22:34,840 or song graphql type this 8559 09:22:37,960 --> 09:22:42,240 one make sure you are using the Asing 8560 09:22:42,240 --> 09:22:44,916 here what is saying unable to resolve 8561 09:22:44,916 --> 09:22:47,200 the signature of method decorator when 8562 09:22:47,200 --> 09:22:49,960 called as an 8563 09:22:51,436 --> 09:22:54,436 expression 8564 09:22:57,960 --> 09:23:00,800 oh we need to get the query from nestjs 8565 09:23:00,800 --> 09:23:03,200 Craft 8566 09:23:04,040 --> 09:23:07,960 ql now it looks 8567 09:23:08,160 --> 09:23:12,360 good let's try to test it 8568 09:23:18,680 --> 09:23:23,320 out let's try to resolve this query 8569 09:23:23,320 --> 09:23:26,640 I would like to fetch all the 8570 09:23:28,680 --> 09:23:30,800 songs you can see 8571 09:23:30,800 --> 09:23:35,080 that we have a song I already created a 8572 09:23:35,080 --> 09:23:37,800 song If you don't want to see the title 8573 09:23:37,800 --> 09:23:40,200 you can say I only want to see the 8574 09:23:40,200 --> 09:23:46,840 ID but I have single song in my 8575 09:23:48,400 --> 09:23:51,560 database now let's resolve the query to 8576 09:23:51,560 --> 09:23:55,400 fetch a sing single song I Can Say 8577 09:23:55,400 --> 09:23:57,720 song 8578 09:23:57,720 --> 09:24:01,040 async Get 8579 09:24:01,240 --> 09:24:06,040 song it should give me the song with 8580 09:24:08,680 --> 09:24:12,596 promise this do song service 8581 09:24:12,596 --> 09:24:15,680 dot get 8582 09:24:15,680 --> 09:24:18,560 song you need to provide the ID but I 8583 09:24:18,560 --> 09:24:21,320 can get the ID 8584 09:24:21,320 --> 09:24:24,160 here 8585 09:24:24,160 --> 09:24:27,360 I can use the arcs from the N graphql I 8586 09:24:27,360 --> 09:24:28,640 can say the 8587 09:24:28,640 --> 09:24:32,120 id id is 8588 09:24:32,960 --> 09:24:37,200 string let's provide the ID 8589 09:24:41,080 --> 09:24:44,520 here I have saved my 8590 09:24:44,520 --> 09:24:47,000 application let's try to resolve this 8591 09:24:47,000 --> 09:24:50,596 query I want to copy this 8592 09:24:50,596 --> 09:24:54,320 ID and let's fetch a 8593 09:24:54,320 --> 09:24:58,596 song ID should be this one and I'm 8594 09:24:58,596 --> 09:25:01,840 expecting the title in the response fail 8595 09:25:01,840 --> 09:25:03,880 to fetch please check your connection I 8596 09:25:03,880 --> 09:25:06,916 think my application did not start it 8597 09:25:06,916 --> 09:25:10,200 now I have to send the request I got my 8598 09:25:10,200 --> 09:25:13,916 record which is song on the based on ID 8599 09:25:13,916 --> 09:25:17,320 if I provide the invalid ID let's 8600 09:25:17,320 --> 09:25:21,040 say you will see the error invalid input 8601 09:25:21,040 --> 09:25:24,240 Syntax for the type 8602 09:25:24,916 --> 09:25:28,080 the ID does not exist as same 8603 09:25:28,080 --> 09:25:30,400 here now I'm going to provide the 8604 09:25:30,400 --> 09:25:34,400 correct ID I got the 8605 09:25:34,400 --> 09:25:37,640 record you have learned how to resolve 8606 09:25:37,640 --> 09:25:40,160 queries in the next video I'll teach you 8607 09:25:40,160 --> 09:25:42,680 how to resolve 8608 09:25:42,680 --> 09:25:45,080 mutations now you're going to learn how 8609 09:25:45,080 --> 09:25:48,040 to resolve mutations in the previous 8610 09:25:48,040 --> 09:25:51,800 video we resolved our queries let's 8611 09:25:51,800 --> 09:25:53,040 resolve 8612 09:25:53,040 --> 09:25:57,040 these mutations create song update song 8613 09:25:57,040 --> 09:25:59,640 and delete 8614 09:26:01,596 --> 09:26:06,720 song now you need mutation instead of 8615 09:26:06,720 --> 09:26:12,320 query we also need create song dto as An 8616 09:26:14,240 --> 09:26:18,200 arcs or you can say create song 8617 09:26:18,200 --> 09:26:20,960 input you can also do that you can also 8618 09:26:20,960 --> 09:26:24,560 get this typing from from graphql 8619 09:26:24,560 --> 09:26:28,916 file and I just called this 8620 09:26:32,200 --> 09:26:36,436 function it's not finding source. cql we 8621 09:26:36,436 --> 09:26:39,520 have to do it 8622 09:26:43,916 --> 09:26:47,880 manually now it looks 8623 09:26:51,160 --> 09:26:54,160 good 8624 09:26:54,560 --> 09:26:59,880 it is taking some time to in restart the 8625 09:27:07,160 --> 09:27:10,160 server let's create a new 8626 09:27:10,160 --> 09:27:13,400 song I'm going to write mutation create 8627 09:27:13,400 --> 09:27:16,000 song create song input we have to 8628 09:27:16,000 --> 09:27:18,480 provide 8629 09:27:19,040 --> 09:27:21,480 title the name of the song which is 8630 09:27:21,480 --> 09:27:23,000 follow me 8631 09:27:23,000 --> 09:27:26,360 I want to see ID and title in the 8632 09:27:26,360 --> 09:27:29,200 response you can see a new song has 8633 09:27:29,200 --> 09:27:32,320 created I can fetch all the songs and 8634 09:27:32,320 --> 09:27:34,756 you can see follow 8635 09:27:34,756 --> 09:27:38,240 me it means we successfully created this 8636 09:27:38,240 --> 09:27:39,960 we have successfully resolved this 8637 09:27:39,960 --> 09:27:41,240 create song 8638 09:27:41,240 --> 09:27:44,436 mutation now it's time to resolve update 8639 09:27:44,436 --> 09:27:45,276 song 8640 09:27:45,276 --> 09:27:47,610 mutation I'm going to copy this 8641 09:27:47,610 --> 09:27:49,120 [Music] 8642 09:27:49,120 --> 09:27:52,680 one update song we need to Res solve we 8643 09:27:52,680 --> 09:27:54,640 need to get update song 8644 09:27:54,640 --> 09:27:57,080 input update song 8645 09:27:57,080 --> 09:27:59,916 dto or you can use update song input 8646 09:27:59,916 --> 09:28:05,040 from graphql file I also need to get 8647 09:28:05,040 --> 09:28:08,680 update result from 8648 09:28:09,040 --> 09:28:12,436 graphql it is 8649 09:28:18,276 --> 09:28:21,040 saying I'm getting the 8650 09:28:21,040 --> 09:28:23,000 error 8651 09:28:23,000 --> 09:28:25,520 update 8652 09:28:26,276 --> 09:28:30,680 song If I get this 8653 09:28:30,680 --> 09:28:35,520 from from typ orm let's see what will 8654 09:28:36,840 --> 09:28:39,276 happen now it looks 8655 09:28:39,276 --> 09:28:42,000 good because this start update 8656 09:28:42,000 --> 09:28:45,560 song it's returning the update result 8657 09:28:45,560 --> 09:28:46,960 from type 8658 09:28:46,960 --> 09:28:50,840 orm when you look at the update song you 8659 09:28:50,840 --> 09:28:53,720 will see the Promise with update result 8660 09:28:53,720 --> 09:28:57,756 I imported update result from 8661 09:28:59,320 --> 09:29:03,640 typm let's try to update the 8662 09:29:03,640 --> 09:29:06,756 song I'm going to write 8663 09:29:06,756 --> 09:29:10,520 mutation update song we need the 8664 09:29:10,520 --> 09:29:13,916 ID I'm going to copy this 8665 09:29:13,916 --> 09:29:17,916 ID let's place it 8666 09:29:20,640 --> 09:29:23,800 here and we we also need to provide 8667 09:29:23,800 --> 09:29:26,160 update song 8668 09:29:26,160 --> 09:29:29,080 input I would like to update the title 8669 09:29:29,080 --> 09:29:31,680 update 8670 09:29:33,436 --> 09:29:37,320 song Let's fetch all the songs and you 8671 09:29:37,320 --> 09:29:39,596 can see the updated 8672 09:29:39,596 --> 09:29:43,880 song similarly we need delete mutation 8673 09:29:43,880 --> 09:29:47,320 let's resolve it I'm going to copy it 8674 09:29:47,320 --> 09:29:50,400 here delete song we have to get it from 8675 09:29:50,400 --> 09:29:53,960 the type orm 8676 09:29:57,000 --> 09:30:00,120 let's try to delete this song 8677 09:30:00,120 --> 09:30:02,160 mutation delete 8678 09:30:02,160 --> 09:30:05,480 song we need to provide the ID I'm going 8679 09:30:05,480 --> 09:30:08,520 to copy this 8680 09:30:09,680 --> 09:30:13,680 ID let's see 8681 09:30:14,720 --> 09:30:16,800 affected delete 8682 09:30:16,800 --> 09:30:20,436 song successfully executed it should 8683 09:30:20,436 --> 09:30:22,560 delete the song 8684 09:30:22,560 --> 09:30:24,840 let's try to fetch the song on the based 8685 09:30:24,840 --> 09:30:26,040 on this 8686 09:30:26,040 --> 09:30:29,480 ID you will see there is no song here 8687 09:30:29,480 --> 09:30:32,800 you can see there is no 8688 09:30:34,400 --> 09:30:37,276 song in this lesson I'm going to teach 8689 09:30:37,276 --> 09:30:40,680 you how to handle errors in 8690 09:30:40,680 --> 09:30:43,400 graphql let's say I want to fetch all 8691 09:30:43,400 --> 09:30:46,240 the songs but instead of returning all 8692 09:30:46,240 --> 09:30:50,120 the songs I have to return the 8693 09:30:50,120 --> 09:30:53,596 error you can do simp simply by using 8694 09:30:53,596 --> 09:30:56,200 throwing the error or you can say let me 8695 09:30:56,200 --> 09:30:59,436 do this in this way I'm going to comment 8696 09:30:59,436 --> 09:31:02,720 this line through new 8697 09:31:02,720 --> 09:31:07,520 error I can say unable to fetch 8698 09:31:07,640 --> 09:31:11,680 songs let's try to send the graphical 8699 09:31:11,680 --> 09:31:15,276 request it's still 8700 09:31:16,520 --> 09:31:19,800 working my application did not reload 8701 09:31:19,800 --> 09:31:22,680 yet let's wait 8702 09:31:22,680 --> 09:31:25,480 now it has 8703 09:31:26,240 --> 09:31:28,916 reloaded you can see unable to fetch the 8704 09:31:28,916 --> 09:31:34,200 songs I got the error internal server 8705 09:31:34,200 --> 09:31:36,560 error uh if you check the Apollo 8706 09:31:36,560 --> 09:31:39,680 documentation we are using Apollo driver 8707 09:31:39,680 --> 09:31:41,880 Apollo driver is saying you can use the 8708 09:31:41,880 --> 09:31:45,040 graphql error class and you can provide 8709 09:31:45,040 --> 09:31:47,000 your own 8710 09:31:47,000 --> 09:31:50,596 message Apollo Apollo Apollo 8711 09:31:50,596 --> 09:31:52,560 documentation is saying 8712 09:31:52,560 --> 09:31:55,400 they also provide buil-in error codes 8713 09:31:55,400 --> 09:31:58,200 like Parts field graphql validation 8714 09:31:58,200 --> 09:32:00,160 field Pat user 8715 09:32:00,160 --> 09:32:02,680 input bad request we're going to use 8716 09:32:02,680 --> 09:32:05,800 this error bad 8717 09:32:06,640 --> 09:32:09,240 request all you need to do is to throw 8718 09:32:09,240 --> 09:32:11,276 the error and you going to provide your 8719 09:32:11,276 --> 09:32:16,916 message in the code so let's use the 8720 09:32:16,916 --> 09:32:18,400 throw the 8721 09:32:18,400 --> 09:32:23,320 error new graphql error 8722 09:32:23,320 --> 09:32:27,436 I got this graphql error class from 8723 09:32:27,436 --> 09:32:31,200 graphql package here you can see that 8724 09:32:31,200 --> 09:32:35,320 now first I have to say unable to fetch 8725 09:32:35,320 --> 09:32:37,480 the 8726 09:32:37,680 --> 09:32:40,360 songs you have to provide the options 8727 09:32:40,360 --> 09:32:42,560 you can provide the 8728 09:32:42,560 --> 09:32:46,360 code you can say server error or what 8729 09:32:46,360 --> 09:32:48,436 what type of message you want to use it 8730 09:32:48,436 --> 09:32:50,756 depends on your use 8731 09:32:50,756 --> 09:32:53,000 case 8732 09:32:53,000 --> 09:32:56,800 I can say internal server 8733 09:33:00,240 --> 09:33:03,596 error let's try to test it out I saved 8734 09:33:03,596 --> 09:33:05,880 the 8735 09:33:07,640 --> 09:33:10,520 application it will take some time to in 8736 09:33:10,520 --> 09:33:11,910 reload the 8737 09:33:11,910 --> 09:33:14,480 [Music] 8738 09:33:14,480 --> 09:33:17,480 application application has reloaded 8739 09:33:17,480 --> 09:33:20,360 let's send the request and I got the 8740 09:33:20,360 --> 09:33:23,320 error unable to f F the songs and I got 8741 09:33:23,320 --> 09:33:25,960 internal server error this time you can 8742 09:33:25,960 --> 09:33:29,596 see graphql error unable to fetch the 8743 09:33:29,596 --> 09:33:33,080 songs you can also see the message I got 8744 09:33:33,080 --> 09:33:36,240 the error at song resolver doget songs 8745 09:33:36,240 --> 09:33:38,520 you can also check the error 8746 09:33:38,520 --> 09:33:42,240 stack get song song song resolver doget 8747 09:33:42,240 --> 09:33:45,480 song we have the error at this function 8748 09:33:45,480 --> 09:33:49,000 get songs this is how you can send the 8749 09:33:49,000 --> 09:33:50,720 error in 8750 09:33:50,720 --> 09:33:52,520 graphql 8751 09:33:52,520 --> 09:33:54,520 if you want to use your own custom 8752 09:33:54,520 --> 09:33:56,680 message you can also do that you can 8753 09:33:56,680 --> 09:33:58,800 provide your custom message your custom 8754 09:33:58,800 --> 09:34:01,436 code and your custom 8755 09:34:01,436 --> 09:34:04,240 extension you can also set the HTTP 8756 09:34:04,240 --> 09:34:07,800 status if you're working with HTTP API 8757 09:34:07,800 --> 09:34:10,200 you can also set your HTTP status code 8758 09:34:10,200 --> 09:34:13,116 and some type of 8759 09:34:14,480 --> 09:34:17,756 headers in this module you're going to 8760 09:34:17,756 --> 09:34:21,276 learn how to apply authentication in 8761 09:34:21,276 --> 09:34:22,400 graphic 8762 09:34:22,400 --> 09:34:25,520 SQL we have implemented authentication 8763 09:34:25,520 --> 09:34:26,800 with rest 8764 09:34:26,800 --> 09:34:30,880 API now I would like to implement 8765 09:34:30,880 --> 09:34:34,080 authenticate graph cuel 8766 09:34:34,080 --> 09:34:37,320 apis in the previous module you learned 8767 09:34:37,320 --> 09:34:41,756 how to create graphel apis with 8768 09:34:41,840 --> 09:34:45,596 nestjs I'm going to pick the same 8769 09:34:45,596 --> 09:34:48,840 example project just like we did with 8770 09:34:48,840 --> 09:34:52,240 authentication in rest API 8771 09:34:52,240 --> 09:34:55,480 I have provided the starter kit code you 8772 09:34:55,480 --> 09:35:00,276 have to open it and run npm 8773 09:35:00,276 --> 09:35:04,756 install when you install all these 8774 09:35:04,756 --> 09:35:08,040 dependencies you can run the project and 8775 09:35:08,040 --> 09:35:10,360 you will get this 8776 09:35:10,360 --> 09:35:13,880 error let me run the 8777 09:35:13,880 --> 09:35:18,436 project because there is no graphql file 8778 09:35:18,436 --> 09:35:23,480 there is no graphql extension file 8779 09:35:23,480 --> 09:35:25,756 when you open the app 8780 09:35:25,756 --> 09:35:29,520 module you will see the graphql 8781 09:35:29,520 --> 09:35:33,080 setup we have a graphql module I'm 8782 09:35:33,080 --> 09:35:36,436 creating a graphql module by using the 8783 09:35:36,436 --> 09:35:39,276 Apollo driver just like we did with in 8784 09:35:39,276 --> 09:35:42,116 the previous module I'm using the 8785 09:35:42,116 --> 09:35:44,520 context I'm just returning a simple 8786 09:35:44,520 --> 09:35:47,360 request mapping the request into request 8787 09:35:47,360 --> 09:35:49,596 it's nothing more I'll teach you more 8788 09:35:49,596 --> 09:35:53,560 about when we will protect 8789 09:35:54,880 --> 09:35:57,520 resolvers so I've already taught you all 8790 09:35:57,520 --> 09:36:00,040 of these stuff like we created the OD 8791 09:36:00,040 --> 09:36:03,840 service we have a login 8792 09:36:03,840 --> 09:36:08,276 method here we have a JWT guard I taught 8793 09:36:08,276 --> 09:36:10,360 you how can you do that what type of 8794 09:36:10,360 --> 09:36:13,360 implementation we have done we have 8795 09:36:13,360 --> 09:36:16,240 passport strategy with JWT it's 8796 09:36:16,240 --> 09:36:17,756 validating the 8797 09:36:17,756 --> 09:36:20,640 user and we have dto login dto 8798 09:36:20,640 --> 09:36:22,040 everything 8799 09:36:22,040 --> 09:36:24,840 everything I have taught to 8800 09:36:24,840 --> 09:36:29,400 you if you don't know about all of these 8801 09:36:29,400 --> 09:36:31,720 functionalities you can go back to the 8802 09:36:31,720 --> 09:36:34,160 module number six on 8803 09:36:34,160 --> 09:36:36,520 authentication you can watch the first 8804 09:36:36,520 --> 09:36:39,680 four lessons on authentication or first 8805 09:36:39,680 --> 09:36:44,240 three lessons on authenticating nestjs 8806 09:36:44,240 --> 09:36:47,200 apis you will get the 8807 09:36:47,200 --> 09:36:50,756 error no definition found no type 8808 09:36:50,756 --> 09:36:53,880 definition word found because we don't 8809 09:36:53,880 --> 09:36:56,916 have any graphql file I just did the 8810 09:36:56,916 --> 09:36:59,480 graphql setup first of all we're going 8811 09:36:59,480 --> 09:37:01,800 to set up our we're going to define the 8812 09:37:01,800 --> 09:37:05,436 schema we don't have we don't have any 8813 09:37:05,436 --> 09:37:07,916 schema what I want to do I want to do 8814 09:37:07,916 --> 09:37:13,916 schema in O folder let's create o. 8815 09:37:13,960 --> 09:37:16,040 graphql what we are going to do we're 8816 09:37:16,040 --> 09:37:19,720 going to create the user type and we're 8817 09:37:19,720 --> 09:37:22,040 going to Define query 8818 09:37:22,040 --> 09:37:25,276 and we will have mutation for sign up 8819 09:37:25,276 --> 09:37:30,320 mutation and we will have login 8820 09:37:32,916 --> 09:37:35,720 mutation first of all let's define a 8821 09:37:35,720 --> 09:37:38,160 user 8822 09:37:39,040 --> 09:37:42,200 type the first argument is ID it should 8823 09:37:42,200 --> 09:37:46,916 be required not null first name the type 8824 09:37:46,916 --> 09:37:47,916 should be 8825 09:37:47,916 --> 09:37:50,560 string I can get this value from the 8826 09:37:50,560 --> 09:37:52,040 user entity 8827 09:37:52,040 --> 09:37:54,200 you can open the user entity on the 8828 09:37:54,200 --> 09:37:57,640 right hand side right 8829 09:37:57,960 --> 09:38:02,520 side first name is required last name is 8830 09:38:02,520 --> 09:38:05,080 also 8831 09:38:05,240 --> 09:38:08,800 required and email is also 8832 09:38:08,800 --> 09:38:13,000 required and password is also required 8833 09:38:13,000 --> 09:38:17,400 so we need these properties these five 8834 09:38:17,400 --> 09:38:19,800 properties I'm not going to play around 8835 09:38:19,800 --> 09:38:23,040 with playlist something like 8836 09:38:23,040 --> 09:38:27,040 that let's make it more simple we 8837 09:38:27,040 --> 09:38:30,160 defined a user 8838 09:38:30,160 --> 09:38:35,756 type now I'm going to define a 8839 09:38:36,756 --> 09:38:39,240 mutation sign 8840 09:38:39,240 --> 09:38:43,240 up it is going to take the signup input 8841 09:38:43,240 --> 09:38:46,160 and the type should be signup input we 8842 09:38:46,160 --> 09:38:48,160 did not create it 8843 09:38:48,160 --> 09:38:50,756 yet it should be required and it is 8844 09:38:50,756 --> 09:38:53,040 going to turn a user back in the 8845 09:38:53,040 --> 09:38:55,596 response or sign up 8846 09:38:55,596 --> 09:38:58,680 response we not we did not create this 8847 09:38:58,680 --> 09:39:02,400 we did not create this signup response 8848 09:39:02,400 --> 09:39:06,520 type now let's define a new type with 8849 09:39:06,520 --> 09:39:08,960 signup 8850 09:39:08,960 --> 09:39:13,200 input when you when you sign up we are 8851 09:39:13,200 --> 09:39:15,756 telling you need to provide hey client 8852 09:39:15,756 --> 09:39:18,480 you need to provide the first name you 8853 09:39:18,480 --> 09:39:21,840 need to provide the last name you need 8854 09:39:21,840 --> 09:39:24,160 to provide the 8855 09:39:24,160 --> 09:39:27,960 email email is also required password is 8856 09:39:27,960 --> 09:39:29,200 also 8857 09:39:29,200 --> 09:39:34,116 required now let's define the sign up 8858 09:39:34,320 --> 09:39:37,916 response sign up 8859 09:39:40,040 --> 09:39:43,080 response what you want to send in this 8860 09:39:43,080 --> 09:39:46,680 signup response you can send a simple 8861 09:39:46,680 --> 09:39:49,360 email it depends on you I'm going to 8862 09:39:49,360 --> 09:39:53,320 send a simple email 8863 09:39:53,596 --> 09:39:56,360 now we're going to Define 8864 09:39:56,360 --> 09:40:00,000 query we have a mutation let's 8865 09:40:00,000 --> 09:40:03,560 define login 8866 09:40:03,880 --> 09:40:06,560 query I can say 8867 09:40:06,560 --> 09:40:11,800 login it is going to take login 8868 09:40:12,520 --> 09:40:16,400 input inside the login 8869 09:40:16,400 --> 09:40:19,680 input and it is going to return a login 8870 09:40:19,680 --> 09:40:23,560 response we did not not have these type 8871 09:40:23,560 --> 09:40:25,916 yet it's are 8872 09:40:25,916 --> 09:40:29,000 required let's define an 8873 09:40:29,000 --> 09:40:32,880 input I'm going to Define input here 8874 09:40:32,880 --> 09:40:35,880 login 8875 09:40:37,800 --> 09:40:40,400 input we need 8876 09:40:40,400 --> 09:40:43,360 email type should be string we need 8877 09:40:43,360 --> 09:40:46,840 password type should be 8878 09:40:46,840 --> 09:40:50,960 string and we also need to Define login 8879 09:40:50,960 --> 09:40:53,240 resp 8880 09:40:53,320 --> 09:40:56,560 response I want to send the access token 8881 09:40:56,560 --> 09:40:58,880 you can double check it from your OD 8882 09:40:58,880 --> 09:41:02,880 service or user service we have a login 8883 09:41:02,880 --> 09:41:04,916 in the login we are just returning the 8884 09:41:04,916 --> 09:41:07,560 access token here you can see that 8885 09:41:07,560 --> 09:41:10,400 access token the type is string we're 8886 09:41:10,400 --> 09:41:13,160 going to follow the same convention the 8887 09:41:13,160 --> 09:41:17,320 type is string it should be 8888 09:41:17,320 --> 09:41:21,040 required we created the login mutation 8889 09:41:21,040 --> 09:41:22,880 that that's 8890 09:41:22,880 --> 09:41:26,360 it let's try to test it out by running 8891 09:41:26,360 --> 09:41:28,916 the mutation or running the typing 8892 09:41:28,916 --> 09:41:32,360 command npm run 8893 09:41:32,360 --> 09:41:33,710 generate 8894 09:41:33,710 --> 09:41:36,680 [Music] 8895 09:41:36,680 --> 09:41:39,000 typings it is going to generate the 8896 09:41:39,000 --> 09:41:42,480 typescript definition 8897 09:41:44,840 --> 09:41:47,960 file typescript definition file has 8898 09:41:47,960 --> 09:41:52,116 created you can see graphql do s 8899 09:41:52,116 --> 09:41:56,320 file we have all of these 8900 09:41:56,916 --> 09:42:00,756 typings we have created or defined our 8901 09:42:00,756 --> 09:42:04,840 schema in the next video I'll teach you 8902 09:42:04,840 --> 09:42:08,840 how to resolve queries and mutation 8903 09:42:08,840 --> 09:42:13,520 we're going to work on sign up mutation 8904 09:42:14,040 --> 09:42:17,116 first in the previous video we have 8905 09:42:17,116 --> 09:42:18,960 created this sign up 8906 09:42:18,960 --> 09:42:21,596 mutation now we need to resolve this 8907 09:42:21,596 --> 09:42:22,756 signup 8908 09:42:22,756 --> 09:42:25,680 mutation we don't have resolver inside 8909 09:42:25,680 --> 09:42:31,916 the O module let's create a new 8910 09:42:40,720 --> 09:42:42,960 resolver we have to provide the name 8911 09:42:42,960 --> 09:42:45,360 which is 8912 09:42:46,040 --> 09:42:51,160 O Au resolver has created you can check 8913 09:42:51,160 --> 09:42:54,240 inside the Au 8914 09:42:54,640 --> 09:42:58,360 module we have Au resolver we have spec 8915 09:42:58,360 --> 09:43:02,116 file and we have Au 8916 09:43:04,116 --> 09:43:07,870 resolver we have to resolve our signup 8917 09:43:07,870 --> 09:43:09,040 [Music] 8918 09:43:09,040 --> 09:43:12,040 mutation 8919 09:43:12,040 --> 09:43:15,560 mutation which is sign up I'm going to 8920 09:43:15,560 --> 09:43:18,960 open the autra ql 8921 09:43:18,960 --> 09:43:23,360 here I want you to do it should be sign 8922 09:43:23,360 --> 09:43:24,240 up 8923 09:43:24,240 --> 09:43:27,360 user I'm going to define a new function 8924 09:43:27,360 --> 09:43:30,520 and it is going to return a sign up 8925 09:43:30,520 --> 09:43:32,520 response you can get the 8926 09:43:32,520 --> 09:43:37,160 typing from graphql dots 8927 09:43:37,520 --> 09:43:42,040 file and one more thing we need to 8928 09:43:42,040 --> 09:43:44,360 inject 8929 09:43:44,360 --> 09:43:47,720 service user service and OD service we 8930 09:43:47,720 --> 09:43:49,520 need both of 8931 09:43:49,520 --> 09:43:55,520 them user service and we need OD 8932 09:44:01,080 --> 09:44:03,916 service we have a create 8933 09:44:03,916 --> 09:44:08,436 method let's get the argument I can use 8934 09:44:08,436 --> 09:44:09,560 the 8935 09:44:09,560 --> 09:44:14,040 arcs we need to get sign up 8936 09:44:14,560 --> 09:44:19,360 input and I can say sign up 8937 09:44:19,360 --> 09:44:22,680 input you can also also get the type 8938 09:44:22,680 --> 09:44:23,916 sign up 8939 09:44:23,916 --> 09:44:29,880 input and I'm going to provide sign up 8940 09:44:30,000 --> 09:44:33,596 input everything is fine now let's try 8941 09:44:33,596 --> 09:44:35,720 to test this signup 8942 09:44:35,720 --> 09:44:39,800 mutation I'm going to run my 8943 09:44:44,640 --> 09:44:47,596 application my application has started 8944 09:44:47,596 --> 09:44:49,916 and I got the 8945 09:44:49,916 --> 09:44:52,916 error 8946 09:44:53,800 --> 09:44:58,276 sign up defined in resolver but not in 8947 09:44:58,276 --> 09:44:59,880 the 8948 09:44:59,880 --> 09:45:03,560 schema oops there is a type typo Here 8949 09:45:03,560 --> 09:45:05,320 sign 8950 09:45:05,320 --> 09:45:10,400 up we have to generate the typing 8951 09:45:13,080 --> 09:45:16,960 again npm run generate 8952 09:45:16,960 --> 09:45:19,916 [Music] 8953 09:45:19,916 --> 09:45:22,916 typings 8954 09:45:25,276 --> 09:45:28,560 everything is up to 8955 09:45:32,596 --> 09:45:37,276 date it is taking too much time because 8956 09:45:37,276 --> 09:45:42,880 I'm using no nestjs Version 9 because I 8957 09:45:42,880 --> 09:45:45,880 built this project in module number six 8958 09:45:45,880 --> 09:45:49,596 by using the nestjs version 9 that is 8959 09:45:49,596 --> 09:45:52,916 why it's very slow 8960 09:45:59,840 --> 09:46:05,480 I have to oh let's restart it 8961 09:46:14,320 --> 09:46:17,480 again my application has 8962 09:46:17,480 --> 09:46:20,040 started I'm going to send request to the 8963 09:46:20,040 --> 09:46:24,800 Local Host callon 3000 and/ 8964 09:46:28,000 --> 09:46:31,240 graphql if you check the docs we have a 8965 09:46:31,240 --> 09:46:33,720 sign up 8966 09:46:36,000 --> 09:46:40,240 mutation let's create a new 8967 09:46:40,240 --> 09:46:43,800 user first name make sure you have 8968 09:46:43,800 --> 09:46:47,240 database in your 8969 09:46:49,840 --> 09:46:54,436 postgress let's provide the last 8970 09:46:55,596 --> 09:46:59,520 name let's provide the 8971 09:47:03,400 --> 09:47:07,360 email let's provide the 8972 09:47:11,880 --> 09:47:15,840 password I got the 8973 09:47:18,960 --> 09:47:22,640 error because I did did not select any 8974 09:47:22,640 --> 09:47:27,800 field here I would like to select the 8975 09:47:28,080 --> 09:47:33,436 email because I can only access the 8976 09:47:33,960 --> 09:47:37,436 email I think I already created a 8977 09:47:37,436 --> 09:47:39,720 user with 8978 09:47:39,720 --> 09:47:44,320 this email let's use new 8979 09:47:44,320 --> 09:47:47,560 one there is an 8980 09:47:47,560 --> 09:47:49,756 issue reading 8981 09:47:49,756 --> 09:47:52,756 password 8982 09:47:55,240 --> 09:47:58,800 let's try to check the 8983 09:48:01,436 --> 09:48:05,200 error password 8984 09:48:05,400 --> 09:48:08,400 is 8985 09:48:10,436 --> 09:48:13,436 invalid 8986 09:48:13,596 --> 09:48:18,200 why let's try to log sign up input what 8987 09:48:18,200 --> 09:48:19,720 it is 8988 09:48:19,720 --> 09:48:22,720 getting 8989 09:48:27,520 --> 09:48:31,360 or do we have sign up function 8990 09:48:31,360 --> 09:48:35,320 here inside the OD service do no we got 8991 09:48:35,320 --> 09:48:36,680 to use 8992 09:48:36,680 --> 09:48:41,000 the user service. 8993 09:48:41,160 --> 09:48:45,080 create let me check in the Au controller 8994 09:48:45,080 --> 09:48:48,160 what we are calling what we are 8995 09:48:48,160 --> 09:48:53,560 using user service. create that is 8996 09:48:58,160 --> 09:49:02,200 it I'm getting the 8997 09:49:04,520 --> 09:49:08,240 undefined maybe there is an issue 8998 09:49:08,240 --> 09:49:13,240 in autra Q let's check it 8999 09:49:13,436 --> 09:49:18,080 out oh syn sying up 9000 09:49:18,080 --> 09:49:22,640 input my God sign up 9001 09:49:22,960 --> 09:49:26,800 input sync 9002 09:49:28,880 --> 09:49:33,520 input sign up 9003 09:49:37,240 --> 09:49:41,560 input let's run the project let's update 9004 09:49:41,560 --> 09:49:46,116 the fix it sign up input we also need to 9005 09:49:46,116 --> 09:49:49,640 update it here sign up 9006 09:49:49,640 --> 09:49:51,596 input 9007 09:49:51,596 --> 09:49:54,116 run the 9008 09:50:00,200 --> 09:50:04,520 project Let's test it out by sending the 9009 09:50:04,520 --> 09:50:08,840 request to create a new 9010 09:50:12,320 --> 09:50:15,560 user sign up input we also need to 9011 09:50:15,560 --> 09:50:18,116 update 9012 09:50:19,640 --> 09:50:21,400 that 9013 09:50:21,400 --> 09:50:24,596 we have created a new 9014 09:50:24,596 --> 09:50:28,916 user a user has created 9015 09:50:28,916 --> 09:50:33,480 successfully you can see that we got the 9016 09:50:34,160 --> 09:50:38,720 object let's log in I want you to 9017 09:50:38,720 --> 09:50:43,116 implement it I'm going to copy 9018 09:50:43,116 --> 09:50:47,040 that we need to get the 9019 09:50:47,436 --> 09:50:53,560 query from nestjs graphql 9020 09:50:53,560 --> 09:50:57,480 package we also need to get login input 9021 09:50:57,480 --> 09:51:00,596 and the login 9022 09:51:05,000 --> 09:51:09,800 response we have a typo here 9023 09:51:10,916 --> 09:51:15,520 aouc I need to run the typing 9024 09:51:19,520 --> 09:51:21,840 again 9025 09:51:21,840 --> 09:51:24,916 let's run the 9026 09:51:28,436 --> 09:51:33,720 project let's try to send the login 9027 09:51:42,276 --> 09:51:46,240 request I'm going to copy use email and 9028 09:51:46,240 --> 09:51:48,560 the 9029 09:51:49,160 --> 09:51:52,640 password I need to get the access 9030 09:51:52,640 --> 09:51:56,596 token we have successfully logged 9031 09:51:56,596 --> 09:52:01,640 in because I got the access 9032 09:52:03,520 --> 09:52:07,680 token in the next video I'll teach you 9033 09:52:07,680 --> 09:52:09,960 how to protect 9034 09:52:09,960 --> 09:52:14,960 resolver we're going to create a new 9035 09:52:14,960 --> 09:52:18,080 protected field which is 9036 09:52:18,080 --> 09:52:21,840 profile only authenticate user can 9037 09:52:21,840 --> 09:52:24,436 access this 9038 09:52:24,436 --> 09:52:29,360 profile we have created all 9039 09:52:29,360 --> 09:52:34,276 card this one JWT o card we have to use 9040 09:52:34,276 --> 09:52:37,360 this o card into our resolver I'll teach 9041 09:52:37,360 --> 09:52:39,596 you how to do that but I want you to 9042 09:52:39,596 --> 09:52:43,080 think about how can we implement or how 9043 09:52:43,080 --> 09:52:47,240 can we apply JWT off 9044 09:52:47,680 --> 09:52:51,040 card we have completed the sign up 9045 09:52:51,040 --> 09:52:53,160 and the login 9046 09:52:53,160 --> 09:52:55,680 functionality let's say I want to 9047 09:52:55,680 --> 09:52:57,116 protect 9048 09:52:57,116 --> 09:53:00,756 resolvers when we were building the rest 9049 09:53:00,756 --> 09:53:04,680 apis if you look at the app 9050 09:53:04,680 --> 09:53:07,800 controller I can say this is the 9051 09:53:07,800 --> 09:53:11,840 protected route only authenticated user 9052 09:53:11,840 --> 09:53:13,560 can access the 9053 09:53:13,560 --> 09:53:17,000 profile and here we also getting the 9054 09:53:17,000 --> 09:53:19,436 user from the request 9055 09:53:19,436 --> 09:53:22,720 object but you are thinking about how 9056 09:53:22,720 --> 09:53:25,720 can we protect the resolver let's say I 9057 09:53:25,720 --> 09:53:28,080 want to write a query 9058 09:53:28,080 --> 09:53:31,520 profile and type should be profile we 9059 09:53:31,520 --> 09:53:35,360 don't have any profile type let's create 9060 09:53:35,360 --> 09:53:37,560 the profile 9061 09:53:37,560 --> 09:53:41,116 type the type should be string the field 9062 09:53:41,116 --> 09:53:42,276 should be 9063 09:53:42,276 --> 09:53:46,720 string and here we have the user 9064 09:53:48,040 --> 09:53:52,640 ID let's generate the 9065 09:53:54,400 --> 09:53:58,720 typing when we resolve this 9066 09:53:58,720 --> 09:54:04,116 query only authenticated user can access 9067 09:54:04,116 --> 09:54:05,480 this 9068 09:54:05,480 --> 09:54:08,880 profile we should not allow anyone to 9069 09:54:08,880 --> 09:54:13,400 see other person or other people 9070 09:54:14,040 --> 09:54:16,436 profile how can you do 9071 09:54:16,436 --> 09:54:20,400 it when we were building the rest apis 9072 09:54:20,400 --> 09:54:22,640 we use the JWT 9073 09:54:22,640 --> 09:54:26,596 guard we have created this guard and we 9074 09:54:26,596 --> 09:54:32,040 apply when we applied this JWT o card on 9075 09:54:32,040 --> 09:54:35,320 our protected route just like we did in 9076 09:54:35,320 --> 09:54:36,520 the app 9077 09:54:36,520 --> 09:54:39,240 controller this is my protected route 9078 09:54:39,240 --> 09:54:42,680 the profile route and I have applied the 9079 09:54:42,680 --> 09:54:47,680 O card by using the use card JWT oard 9080 09:54:47,680 --> 09:54:49,800 maybe you thinking how can we do into 9081 09:54:49,800 --> 09:54:50,916 graph 9082 09:54:50,916 --> 09:54:55,480 well I'm telling you I'm you you also 9083 09:54:55,480 --> 09:55:00,756 need God you also need JWT o guard I 9084 09:55:00,756 --> 09:55:03,596 want you to implement it you don't need 9085 09:55:03,596 --> 09:55:06,916 to change anything in the JWT strategy 9086 09:55:06,916 --> 09:55:08,880 because we were using the passport 9087 09:55:08,880 --> 09:55:12,520 strategy if you are not sure if you 9088 09:55:12,520 --> 09:55:15,960 don't know about how did I implement the 9089 09:55:15,960 --> 09:55:17,200 passport 9090 09:55:17,200 --> 09:55:19,520 strategy you can look up the 9091 09:55:19,520 --> 09:55:22,400 Authentication section I think it's 9092 09:55:22,400 --> 09:55:25,160 section number six or module number six 9093 09:55:25,160 --> 09:55:26,560 or 9094 09:55:26,560 --> 09:55:30,360 seven and then you can get the 9095 09:55:30,360 --> 09:55:36,840 lesson for passport or jwd 9096 09:55:36,840 --> 09:55:40,880 authentication it has generated the 9097 09:55:40,880 --> 09:55:45,436 typings in the graphql dots we have the 9098 09:55:45,436 --> 09:55:46,680 profile 9099 09:55:46,680 --> 09:55:50,720 class now when we resolve this query I 9100 09:55:50,720 --> 09:55:53,560 have to say hey this is protected 9101 09:55:53,560 --> 09:55:56,160 route let's let me show you I'm going to 9102 09:55:56,160 --> 09:55:58,640 run the 9103 09:56:00,520 --> 09:56:04,276 project to access the protected 9104 09:56:04,276 --> 09:56:09,400 route a user or client should or client 9105 09:56:09,400 --> 09:56:13,596 must need to provide authentication 9106 09:56:13,596 --> 09:56:16,720 token the user can get authentication 9107 09:56:16,720 --> 09:56:19,840 token by executing or sending the login 9108 09:56:19,840 --> 09:56:21,360 request 9109 09:56:21,360 --> 09:56:24,000 and this login request gives the access 9110 09:56:24,000 --> 09:56:27,276 token in the 9111 09:56:27,320 --> 09:56:31,800 response let's wait for the 9112 09:56:34,640 --> 09:56:38,596 application my application is running 9113 09:56:38,596 --> 09:56:41,640 now let's send the request to the Local 9114 09:56:41,640 --> 09:56:45,360 Host col 3000 9115 09:56:49,116 --> 09:56:53,240 /ql here you can provide the 9116 09:56:53,240 --> 09:56:57,040 authorization HTTP 9117 09:56:58,200 --> 09:57:01,116 header I'm going to send the login 9118 09:57:01,116 --> 09:57:05,116 request I got the access 9119 09:57:05,116 --> 09:57:08,080 token here you can access the profile 9120 09:57:08,080 --> 09:57:12,400 route you have to provide the token 9121 09:57:12,756 --> 09:57:16,520 here we did not write the resolver for 9122 09:57:16,520 --> 09:57:18,720 the profile 9123 09:57:18,720 --> 09:57:23,240 query let's resolve 9124 09:57:26,756 --> 09:57:30,360 it inside the Au 9125 09:57:30,360 --> 09:57:35,160 resolver let's resolve Au query or 9126 09:57:35,160 --> 09:57:37,720 profile 9127 09:57:43,320 --> 09:57:46,756 query get 9128 09:57:47,680 --> 09:57:51,560 profile it is going to give 9129 09:57:51,560 --> 09:57:53,880 the 9130 09:57:54,560 --> 09:57:56,160 promise 9131 09:57:56,160 --> 09:57:59,160 with 9132 09:58:03,040 --> 09:58:06,360 profile and here is the 9133 09:58:06,360 --> 09:58:08,720 question 9134 09:58:08,720 --> 09:58:13,320 how can you access the profile or how 9135 09:58:13,320 --> 09:58:16,000 can you access the current logged in 9136 09:58:16,000 --> 09:58:19,436 user like we need to send current logged 9137 09:58:19,436 --> 09:58:21,040 in user 9138 09:58:21,040 --> 09:58:24,160 actually we need to send the user ID the 9139 09:58:24,160 --> 09:58:26,520 current logged in user 9140 09:58:26,520 --> 09:58:30,916 ID and his 9141 09:58:33,596 --> 09:58:37,480 email we are returning the 9142 09:58:37,480 --> 09:58:42,520 promise that is why I caught this 9143 09:58:43,880 --> 09:58:48,436 issue let's return new 9144 09:58:49,040 --> 09:58:52,520 promise and 9145 09:58:53,400 --> 09:58:56,880 resolve return 9146 09:58:56,880 --> 09:59:00,276 resolve this 9147 09:59:04,040 --> 09:59:08,360 object I just created the fake 9148 09:59:08,360 --> 09:59:12,916 promise to pass this 9149 09:59:15,880 --> 09:59:20,116 test oh I have written the profile in 9150 09:59:20,116 --> 09:59:21,320 the 9151 09:59:21,320 --> 09:59:26,640 mutation let's change it to 9152 09:59:28,720 --> 09:59:31,240 query restart the 9153 09:59:31,240 --> 09:59:34,440 [Music] 9154 09:59:34,640 --> 09:59:38,320 application now we're going to send the 9155 09:59:38,320 --> 09:59:41,916 request it's still 9156 09:59:44,840 --> 09:59:47,480 running now we're going to access the 9157 09:59:47,480 --> 09:59:51,040 profile I caught the profile 9158 09:59:51,040 --> 09:59:55,480 but only authenticated user can access 9159 09:59:55,480 --> 09:59:56,480 this 9160 09:59:56,480 --> 10:00:01,040 profile so we need to apply the O guard 9161 10:00:01,040 --> 10:00:05,000 how can you do it let's create a new 9162 10:00:05,000 --> 10:00:08,756 guard I'm going to copy that guard and 9163 10:00:08,756 --> 10:00:12,640 create a new guard and I'm going to say 9164 10:00:12,640 --> 10:00:15,640 gql 9165 10:00:18,916 --> 10:00:21,916 oards 9166 10:00:23,520 --> 10:00:30,320 here instead of JWT I can say graph o 9167 10:00:30,320 --> 10:00:37,520 card and now we need to do a little bit 9168 10:00:43,480 --> 10:00:48,116 logic we need to use the can activate 9169 10:00:48,116 --> 10:00:50,916 function because in graph ql we need to 9170 10:00:50,916 --> 10:00:54,320 get the execution 9171 10:00:56,116 --> 10:00:59,880 context and we need handle request it is 9172 10:00:59,880 --> 10:01:01,840 going to 9173 10:01:01,840 --> 10:01:04,360 return a 9174 10:01:04,360 --> 10:01:09,400 user and you can import authentication 9175 10:01:10,320 --> 10:01:13,640 error when you look when you look at app 9176 10:01:13,640 --> 10:01:17,480 module you will see this 9177 10:01:17,480 --> 10:01:20,880 request I'm just mapping 9178 10:01:20,880 --> 10:01:24,960 the request from req request here I got 9179 10:01:24,960 --> 10:01:26,960 the request from the context here you 9180 10:01:26,960 --> 10:01:29,400 can see 9181 10:01:29,400 --> 10:01:32,916 that and I call the super Constructor by 9182 10:01:32,916 --> 10:01:36,720 calling this execution context 9183 10:01:36,880 --> 10:01:39,916 host because in graphel you need to get 9184 10:01:39,916 --> 10:01:42,200 the request object from the 9185 10:01:42,200 --> 10:01:44,916 context when you have if you have worked 9186 10:01:44,916 --> 10:01:48,596 with graphql before each resolver will 9187 10:01:48,596 --> 10:01:51,720 have four argument 9188 10:01:51,720 --> 10:01:53,756 here you will have for 9189 10:01:53,756 --> 10:01:55,436 argument 9190 10:01:55,436 --> 10:01:57,520 parent I think 9191 10:01:57,520 --> 10:02:02,596 root context value and info you can get 9192 10:02:02,596 --> 10:02:05,160 the documentation from 9193 10:02:05,160 --> 10:02:08,960 Apollo server 9194 10:02:17,720 --> 10:02:21,840 resolver here you can see 9195 10:02:23,436 --> 10:02:29,000 each resolver has four arguments parent 9196 10:02:29,000 --> 10:02:33,720 ARS context value and 9197 10:02:33,720 --> 10:02:37,840 info ARS you can see 9198 10:02:38,640 --> 10:02:41,880 ARs and I'm going to log 9199 10:02:41,880 --> 10:02:44,880 everything 9200 10:02:46,080 --> 10:02:50,960 parent console. log ARS 9201 10:02:50,960 --> 10:02:54,840 console. log context 9202 10:02:55,436 --> 10:02:59,640 value and we also need to log the info 9203 10:02:59,640 --> 10:03:04,360 let's see what is inside this this stuff 9204 10:03:04,360 --> 10:03:07,000 but make sure you apply the 9205 10:03:07,000 --> 10:03:13,080 guard use guards I can say graphql or 9206 10:03:17,720 --> 10:03:22,640 guard now this route this resolver is 9207 10:03:22,640 --> 10:03:26,680 protected it is secure 9208 10:03:30,720 --> 10:03:33,200 now don't worry I'll teach you 9209 10:03:33,200 --> 10:03:36,960 everything what is going on 9210 10:03:44,436 --> 10:03:48,480 here let's send the 9211 10:03:48,720 --> 10:03:51,320 request 9212 10:03:51,320 --> 10:03:56,160 here you can see the log 9213 10:04:01,116 --> 10:04:04,160 console there are a lot of stuff 9214 10:04:04,160 --> 10:04:08,400 here parent is undefined there is no 9215 10:04:08,400 --> 10:04:12,040 parent for this query ARS is an object 9216 10:04:12,040 --> 10:04:14,680 but is but there is nothing inside the 9217 10:04:14,680 --> 10:04:18,360 ARs there's a context value this is 9218 10:04:18,360 --> 10:04:21,200 there is the request 9219 10:04:21,200 --> 10:04:24,320 object and inside the request object you 9220 10:04:24,320 --> 10:04:27,160 can find your 9221 10:04:37,040 --> 10:04:40,520 user here is the user I found the 9222 10:04:40,520 --> 10:04:42,916 user this is 9223 10:04:42,916 --> 10:04:44,916 the this is the 9224 10:04:44,916 --> 10:04:48,240 user this is the this object is current 9225 10:04:48,240 --> 10:04:51,360 logged in user 9226 10:04:51,360 --> 10:04:55,040 if you look at the jwd passport 9227 10:04:55,040 --> 10:04:58,400 strategy here we are 9228 10:04:58,400 --> 10:05:02,240 saving user ID and the 9229 10:05:02,240 --> 10:05:05,040 email that's why I got the user ID and 9230 10:05:05,040 --> 10:05:06,720 email what I can 9231 10:05:06,720 --> 10:05:10,000 say I can only 9232 10:05:10,000 --> 10:05:12,400 return context 9233 10:05:12,400 --> 10:05:17,160 value do request. 9234 10:05:18,640 --> 10:05:20,960 user 9235 10:05:20,960 --> 10:05:24,960 I don't need to use the promise 9236 10:05:27,360 --> 10:05:31,916 here it's just returning a simple 9237 10:05:40,200 --> 10:05:42,840 profile I'm going to send the request 9238 10:05:42,840 --> 10:05:47,200 this time I got the original user ID and 9239 10:05:47,200 --> 10:05:52,960 the email let's sign up up a new 9240 10:05:52,960 --> 10:05:57,276 user I'm going to create a new 9241 10:05:58,360 --> 10:06:01,360 user 9242 10:06:05,200 --> 10:06:09,200 Linda let's save the 9243 10:06:10,840 --> 10:06:16,880 email now I got the user we have 9244 10:06:16,880 --> 10:06:22,436 successfully sign up let's log 9245 10:06:22,560 --> 10:06:25,480 in I got the access 9246 10:06:25,480 --> 10:06:28,720 token now I can use this access token 9247 10:06:28,720 --> 10:06:31,000 here I want to access the profile of 9248 10:06:31,000 --> 10:06:35,840 this route or user wants to see this his 9249 10:06:35,840 --> 10:06:39,916 profile I got this 9250 10:06:41,960 --> 10:06:45,720 user now let's debug the application I'm 9251 10:06:45,720 --> 10:06:47,880 going to put couple of break points here 9252 10:06:47,880 --> 10:06:52,000 what is going on here let's look 9253 10:07:03,116 --> 10:07:06,880 at I have already set up the VS code 9254 10:07:06,880 --> 10:07:11,080 folder inside you have launch. Json file 9255 10:07:11,080 --> 10:07:12,720 if you don't know how to debug the 9256 10:07:12,720 --> 10:07:14,436 application let me teach you I've 9257 10:07:14,436 --> 10:07:18,160 already taught you there is a script in 9258 10:07:18,160 --> 10:07:22,520 the package. s file for the debug the 9259 10:07:22,520 --> 10:07:25,560 application you can 9260 10:07:25,840 --> 10:07:29,560 say npm 9261 10:07:29,720 --> 10:07:34,840 start please start the project in debug 9262 10:07:42,116 --> 10:07:45,400 mode my application is running in the 9263 10:07:45,400 --> 10:07:47,916 debug 9264 10:07:48,436 --> 10:07:51,436 mode 9265 10:07:51,720 --> 10:07:54,520 now I can start the debugging by 9266 10:07:54,520 --> 10:07:58,160 pressing on this attach 9267 10:08:01,436 --> 10:08:06,116 button I've put couple of break points 9268 10:08:14,160 --> 10:08:16,960 here now it has 9269 10:08:16,960 --> 10:08:21,436 started I can debug my applic 9270 10:08:22,200 --> 10:08:26,436 a I can also put breako 9271 10:08:26,436 --> 10:08:29,800 here or 9272 10:08:31,200 --> 10:08:34,800 here now we're going to send the 9273 10:08:34,800 --> 10:08:39,680 request it has stopped at this o 9274 10:08:39,680 --> 10:08:42,436 card and first of all it is going to run 9275 10:08:42,436 --> 10:08:46,276 the can activate function we are using 9276 10:08:46,276 --> 10:08:48,320 getting the graphql 9277 10:08:48,320 --> 10:08:51,200 context by calling the 9278 10:08:51,200 --> 10:08:55,160 create and here I got the context 9279 10:08:55,160 --> 10:08:58,916 object and inside the context you can 9280 10:08:58,916 --> 10:09:02,200 see we have the Handler function we have 9281 10:09:02,200 --> 10:09:03,640 the 9282 10:09:03,640 --> 10:09:05,720 ARs ARS 9283 10:09:05,720 --> 10:09:09,596 property the first one is object and we 9284 10:09:09,596 --> 10:09:12,840 have a request we have this request and 9285 10:09:12,840 --> 10:09:15,160 we have field name which is 9286 10:09:15,160 --> 10:09:18,116 graphql field which is 9287 10:09:18,116 --> 10:09:21,360 profile you you can see the field 9288 10:09:21,360 --> 10:09:27,360 noes this is how graph run under the 9289 10:09:27,360 --> 10:09:33,520 H this is my string or the query graph 9290 10:09:33,520 --> 10:09:36,040 request and here is the name something 9291 10:09:36,040 --> 10:09:39,000 like that profile which is 9292 10:09:39,000 --> 10:09:45,200 profile let's step over the code you 9293 10:09:45,200 --> 10:09:47,116 will see the 9294 10:09:47,116 --> 10:09:50,800 context inside the context we have OD 9295 10:09:50,800 --> 10:09:52,480 context type 9296 10:09:52,480 --> 10:09:57,040 graphql now I'm extracting the request 9297 10:09:57,040 --> 10:10:01,320 object by calling the get 9298 10:10:01,480 --> 10:10:05,436 context so I got the 9299 10:10:05,436 --> 10:10:09,200 request inside the 9300 10:10:10,720 --> 10:10:14,436 request you can see 9301 10:10:14,436 --> 10:10:17,520 that this is the request we have this 9302 10:10:17,520 --> 10:10:19,756 endpoint SLG graph 9303 10:10:19,756 --> 10:10:22,960 ql this is the body we have the 9304 10:10:22,960 --> 10:10:26,880 query and we have the 9305 10:10:29,160 --> 10:10:34,080 variables let's jump to the next break 9306 10:10:34,080 --> 10:10:37,400 point there is no 9307 10:10:37,400 --> 10:10:41,640 error and there is a user you can see 9308 10:10:41,640 --> 10:10:45,916 that and it is going to return the 9309 10:10:45,916 --> 10:10:50,200 user now you can access the user 9310 10:10:50,200 --> 10:10:52,520 inside the 9311 10:10:52,520 --> 10:10:56,400 context parent is null which is 9312 10:10:56,400 --> 10:10:59,276 undefined we don't have anything in the 9313 10:10:59,276 --> 10:11:02,320 ARs now you have the context value you 9314 10:11:02,320 --> 10:11:07,320 can also do debugging here context value 9315 10:11:07,320 --> 10:11:10,200 dot request. 9316 10:11:10,200 --> 10:11:14,400 user you can see I got the 9317 10:11:16,520 --> 10:11:19,960 user and we have the info 9318 10:11:19,960 --> 10:11:22,480 info is related to information related 9319 10:11:22,480 --> 10:11:25,720 to query if you need to access the path 9320 10:11:25,720 --> 10:11:28,240 parent type field name something like 9321 10:11:28,240 --> 10:11:29,320 that 9322 10:11:29,320 --> 10:11:32,916 schema then you can use the info 9323 10:11:32,916 --> 10:11:35,000 validation error we have directives 9324 10:11:35,000 --> 10:11:36,800 include skip 9325 10:11:36,800 --> 10:11:39,960 deicated query type graphical object 9326 10:11:39,960 --> 10:11:44,000 type we have the fields login 9327 10:11:44,000 --> 10:11:46,680 profile all this information related to 9328 10:11:46,680 --> 10:11:49,480 graphql schema and the query inside the 9329 10:11:49,480 --> 10:11:51,116 info 9330 10:11:51,116 --> 10:11:54,640 object now we are going to just return 9331 10:11:54,640 --> 10:11:57,840 the current logged in 9332 10:11:57,840 --> 10:12:02,116 user that's how the authentication Works 9333 10:12:02,116 --> 10:12:05,800 under the hood in 9334 10:12:06,560 --> 10:12:09,116 graphql if you need to implement 9335 10:12:09,116 --> 10:12:12,320 realtime application you have to build a 9336 10:12:12,320 --> 10:12:15,040 realtime functionality like chat 9337 10:12:15,040 --> 10:12:18,116 application or notification type 9338 10:12:18,116 --> 10:12:20,520 application 9339 10:12:20,520 --> 10:12:24,276 then you use graphql subscription if you 9340 10:12:24,276 --> 10:12:26,800 have worked with API rest 9341 10:12:26,800 --> 10:12:31,116 apis we used websocket and socket iio 9342 10:12:31,116 --> 10:12:32,720 for realtime 9343 10:12:32,720 --> 10:12:35,640 application but we are building graph 9344 10:12:35,640 --> 10:12:38,916 apis if you need realtime functionality 9345 10:12:38,916 --> 10:12:41,960 then you need graphql 9346 10:12:41,960 --> 10:12:45,000 subscription I'm going to build or 9347 10:12:45,000 --> 10:12:48,040 Implement graphql subscriptions with 9348 10:12:48,040 --> 10:12:51,436 nestjs Pro project first of all you have 9349 10:12:51,436 --> 10:12:54,596 to install dependency graphql 9350 10:12:54,596 --> 10:12:57,240 subscriptions and then we will install 9351 10:12:57,240 --> 10:12:59,436 the subscription 9352 10:12:59,436 --> 10:13:03,116 Handler just like query and mutation you 9353 10:13:03,116 --> 10:13:06,160 need to define a new type 9354 10:13:06,160 --> 10:13:08,560 subscription I have created this song 9355 10:13:08,560 --> 10:13:13,080 created field it means when a new song 9356 10:13:13,080 --> 10:13:16,276 is published to Spotify 9357 10:13:16,276 --> 10:13:18,720 application this Spotify application 9358 10:13:18,720 --> 10:13:22,400 will send the notification to all users 9359 10:13:22,400 --> 10:13:25,720 we want to implement this 9360 10:13:26,320 --> 10:13:29,720 feature when you define the type 9361 10:13:29,720 --> 10:13:32,360 subscription we defined it 9362 10:13:32,360 --> 10:13:35,800 here you have to write the resolver to 9363 10:13:35,800 --> 10:13:39,596 resolve this field we need a pops up 9364 10:13:39,596 --> 10:13:42,720 module from graphql subscriptions the 9365 10:13:42,720 --> 10:13:45,560 package we have installed 9366 10:13:45,560 --> 10:13:49,800 earlier here it is going to listen 9367 10:13:49,800 --> 10:13:53,720 for song created event this 9368 10:13:53,720 --> 10:13:59,240 resolver is listen for song created 9369 10:13:59,240 --> 10:14:02,240 event here we are resolving this field 9370 10:14:02,240 --> 10:14:04,596 song 9371 10:14:04,596 --> 10:14:07,276 created when you run the application you 9372 10:14:07,276 --> 10:14:09,880 should go to the Local Host colon 3000 9373 10:14:09,880 --> 10:14:11,080 /g 9374 10:14:11,080 --> 10:14:13,596 graphql I'm going to run the 9375 10:14:13,596 --> 10:14:16,960 subscription song created we have ID 9376 10:14:16,960 --> 10:14:19,520 title I have started the subscription 9377 10:14:19,520 --> 10:14:20,960 you will see the subscription is 9378 10:14:20,960 --> 10:14:24,640 listening it's in the listening mode our 9379 10:14:24,640 --> 10:14:28,596 next step is to publish this 9380 10:14:28,596 --> 10:14:31,756 event when we will publish the event you 9381 10:14:31,756 --> 10:14:34,840 have to send the payload which is a new 9382 10:14:34,840 --> 10:14:39,800 song and I have created a new song Here 9383 10:14:39,800 --> 10:14:42,240 we are publishing an 9384 10:14:42,240 --> 10:14:45,240 event 9385 10:14:45,320 --> 10:14:47,960 here it is going 9386 10:14:47,960 --> 10:14:51,200 to now the subscription is already 9387 10:14:51,200 --> 10:14:54,960 running here you can see and now it can 9388 10:14:54,960 --> 10:14:59,480 receive it has received a new 9389 10:15:01,680 --> 10:15:04,960 song if you want implement this 9390 10:15:04,960 --> 10:15:07,756 subscription you can do 9391 10:15:07,756 --> 10:15:10,800 it otherwise you can follow me I'm going 9392 10:15:10,800 --> 10:15:13,960 to implement it you have to install 9393 10:15:13,960 --> 10:15:17,240 package graphql 9394 10:15:17,916 --> 10:15:21,240 subscription and we have to install the 9395 10:15:21,240 --> 10:15:24,916 Handler in the app 9396 10:15:30,916 --> 10:15:34,436 module install subscription Handler 9397 10:15:34,436 --> 10:15:37,276 let's define a new type subscription in 9398 10:15:37,276 --> 10:15:39,200 the song 9399 10:15:39,200 --> 10:15:43,080 graphql I'm going to Define It Here song 9400 10:15:43,080 --> 10:15:45,916 created it is going to return a new 9401 10:15:45,916 --> 10:15:49,916 song now we need to resolve it 9402 10:15:49,916 --> 10:15:53,800 resolve the song created field inside 9403 10:15:53,800 --> 10:15:55,880 the song 9404 10:15:55,880 --> 10:15:59,596 resolver we need to import psup at the 9405 10:15:59,596 --> 10:16:01,276 top of our 9406 10:16:01,276 --> 10:16:05,640 resolver you can import it from graphql 9407 10:16:05,640 --> 10:16:08,640 subscriptions 9408 10:16:10,916 --> 10:16:14,000 package now we're going to resolve this 9409 10:16:14,000 --> 10:16:16,240 subscription song 9410 10:16:16,240 --> 10:16:19,756 created I can do it here at the 9411 10:16:19,756 --> 10:16:22,320 bottom we need to get 9412 10:16:22,320 --> 10:16:27,596 subscription from nestjs graphql 9413 10:16:35,880 --> 10:16:39,040 package let's try to import manually 9414 10:16:39,040 --> 10:16:42,040 from cql 9415 10:16:42,040 --> 10:16:46,040 subscriptions we need pups 9416 10:16:47,840 --> 10:16:49,480 up 9417 10:16:49,480 --> 10:16:52,400 let me run the application and then I 9418 10:16:52,400 --> 10:16:54,916 will be able to start the subscription 9419 10:16:54,916 --> 10:16:58,720 in the listening mode the pups sub Asing 9420 10:16:58,720 --> 10:17:02,000 iterator we are telling to pups sub 9421 10:17:02,000 --> 10:17:06,240 module or the ppup module is saying I'm 9422 10:17:06,240 --> 10:17:08,916 listening for the song created event 9423 10:17:08,916 --> 10:17:12,116 with this label this is an event I'm 9424 10:17:12,116 --> 10:17:14,160 listening for an event with this label 9425 10:17:14,160 --> 10:17:16,560 song 9426 10:17:17,480 --> 10:17:20,880 created my application is running 9427 10:17:20,880 --> 10:17:24,596 now let's run the 9428 10:17:24,596 --> 10:17:27,040 subscription when you check the docs you 9429 10:17:27,040 --> 10:17:31,160 will see subscription with song 9430 10:17:31,160 --> 10:17:33,080 created I'm going to start the 9431 10:17:33,080 --> 10:17:36,560 subscription song created when a new 9432 10:17:36,560 --> 10:17:38,840 song will be published it is going to 9433 10:17:38,840 --> 10:17:42,116 give me this record the song object now 9434 10:17:42,116 --> 10:17:45,436 it is in the listening 9435 10:17:47,756 --> 10:17:50,756 mode 9436 10:17:51,080 --> 10:17:52,560 when we will 9437 10:17:52,560 --> 10:17:54,680 create a new 9438 10:17:54,680 --> 10:17:58,720 song it means when artist in real world 9439 10:17:58,720 --> 10:18:02,436 Spotify application has published a new 9440 10:18:02,436 --> 10:18:05,320 song Spotify application will send the 9441 10:18:05,320 --> 10:18:09,756 notification of this song to all 9442 10:18:14,360 --> 10:18:17,720 users new song I can say new 9443 10:18:17,720 --> 10:18:19,640 song 9444 10:18:19,640 --> 10:18:23,200 I need to get ID and the 9445 10:18:25,960 --> 10:18:30,160 title a new song has created but I 9446 10:18:30,160 --> 10:18:33,400 cannot see anything here because we did 9447 10:18:33,400 --> 10:18:35,756 not publish the event when we will 9448 10:18:35,756 --> 10:18:40,200 create when we will be creating a new 9449 10:18:40,400 --> 10:18:44,596 song Here we have to 9450 10:18:45,360 --> 10:18:50,360 publish this event this one song creat 9451 10:18:50,360 --> 10:18:53,400 event so you can do it as well I'm going 9452 10:18:53,400 --> 10:18:56,400 to copy my 9453 10:18:56,916 --> 10:19:00,916 code and let's do it 9454 10:19:01,960 --> 10:19:04,276 here this is how you 9455 10:19:04,276 --> 10:19:08,560 can sent message to the event by using 9456 10:19:08,560 --> 10:19:11,916 the song created label and we have 9457 10:19:11,916 --> 10:19:13,756 provided the payload make sure you use 9458 10:19:13,756 --> 10:19:15,840 the song 9459 10:19:15,840 --> 10:19:18,480 created Property name otherwise it will 9460 10:19:18,480 --> 10:19:21,160 not not 9461 10:19:23,276 --> 10:19:27,680 work let's create a new 9462 10:19:30,240 --> 10:19:34,200 song I have created a new song and it is 9463 10:19:34,200 --> 10:19:37,160 saying I cannot could not connect to 9464 10:19:37,160 --> 10:19:39,160 websocket 9465 10:19:39,160 --> 10:19:43,320 endpoint please check if the endpoint 9466 10:19:43,320 --> 10:19:46,640 URL is 9467 10:19:46,640 --> 10:19:49,200 correct now it's in the listening mode 9468 10:19:49,200 --> 10:19:53,640 mod created a new song now I have got a 9469 10:19:53,640 --> 10:19:55,916 new 9470 10:19:57,436 --> 10:20:01,436 song because we are publishing a new 9471 10:20:01,436 --> 10:20:05,800 song and it has received it 9472 10:20:08,320 --> 10:20:12,116 here let's say I can say new the name of 9473 10:20:12,116 --> 10:20:13,800 the 9474 10:20:13,800 --> 10:20:16,916 song you can choose any 9475 10:20:16,916 --> 10:20:21,480 name I have created a new song or artist 9476 10:20:21,480 --> 10:20:25,360 has published a new song at Spotify web 9477 10:20:25,360 --> 10:20:27,756 application now the Spotify application 9478 10:20:27,756 --> 10:20:30,640 has has sent the notification to all 9479 10:20:30,640 --> 10:20:32,916 users who are subscribing to song 9480 10:20:32,916 --> 10:20:34,596 created 9481 10:20:34,596 --> 10:20:39,480 event that's it the concept of 9482 10:20:40,720 --> 10:20:44,000 subscription in this module you're going 9483 10:20:44,000 --> 10:20:47,560 to learn how to test graph 9484 10:20:47,560 --> 10:20:51,276 apis we have already tested our rest 9485 10:20:51,276 --> 10:20:54,000 apis we're going to use the same logic 9486 10:20:54,000 --> 10:20:57,000 to test graphql 9487 10:20:57,000 --> 10:21:00,240 apis we're going to do Mocking the same 9488 10:21:00,240 --> 10:21:05,360 way just we did in the previous 9489 10:21:06,560 --> 10:21:10,840 module when you see song resolver dopc 9490 10:21:10,840 --> 10:21:14,360 file you will see this default 9491 10:21:14,360 --> 10:21:17,756 code when you run the test by using this 9492 10:21:17,756 --> 10:21:21,916 command npm run test you will get this 9493 10:21:21,916 --> 10:21:25,880 error hey you are using a song resolver 9494 10:21:25,880 --> 10:21:29,080 but your song resolver is dependent on 9495 10:21:29,080 --> 10:21:30,520 song 9496 10:21:30,520 --> 10:21:33,680 service you have to import it but we are 9497 10:21:33,680 --> 10:21:37,080 doing unit testing so when you are doing 9498 10:21:37,080 --> 10:21:40,040 unit testing you have to test individual 9499 10:21:40,040 --> 10:21:43,840 parts of your code you have to test 9500 10:21:43,840 --> 10:21:45,360 individual 9501 10:21:45,360 --> 10:21:48,520 functions if this function is depend 9502 10:21:48,520 --> 10:21:50,916 dependent on some other functions or 9503 10:21:50,916 --> 10:21:53,040 some other dependency or some other 9504 10:21:53,040 --> 10:21:56,320 classes or some other services you will 9505 10:21:56,320 --> 10:22:00,400 create a Moog implementation of the 9506 10:22:00,400 --> 10:22:02,720 dependency I've already taught you the 9507 10:22:02,720 --> 10:22:05,080 Moog implementation when we will when we 9508 10:22:05,080 --> 10:22:08,640 will be testing song 9509 10:22:09,520 --> 10:22:12,800 controller this is how you will create 9510 10:22:12,800 --> 10:22:16,200 the mo implementation of song 9511 10:22:16,200 --> 10:22:19,640 service you we will have have these 9512 10:22:19,640 --> 10:22:22,436 methods inside the song 9513 10:22:22,436 --> 10:22:25,640 service in get song create song we are 9514 10:22:25,640 --> 10:22:27,240 just creating the mo 9515 10:22:27,240 --> 10:22:31,960 implementation I just copied this code 9516 10:22:31,960 --> 10:22:34,756 from the song Ser song controller song 9517 10:22:34,756 --> 10:22:37,680 controller spec file and I have updated 9518 10:22:37,680 --> 10:22:40,960 the create song dto to create song input 9519 10:22:40,960 --> 10:22:43,916 and update song dto to update song 9520 10:22:43,916 --> 10:22:47,436 input and then you will test your 9521 10:22:47,436 --> 10:22:49,000 functions 9522 10:22:49,000 --> 10:22:52,200 if you want to test your get fet song 9523 10:22:52,200 --> 10:22:54,480 Get song function inside the song 9524 10:22:54,480 --> 10:22:56,756 resolver you can do 9525 10:22:56,756 --> 10:22:59,680 that I'm applying the await expression 9526 10:22:59,680 --> 10:23:02,116 on get song and I got the song here I'm 9527 10:23:02,116 --> 10:23:03,560 doing 9528 10:23:03,560 --> 10:23:06,040 expectation checking asserting or 9529 10:23:06,040 --> 10:23:09,640 expecting value it will pass this is how 9530 10:23:09,640 --> 10:23:11,480 you will test create 9531 10:23:11,480 --> 10:23:14,560 song create song will be passed update 9532 10:23:14,560 --> 10:23:17,080 song and the delete 9533 10:23:17,080 --> 10:23:21,240 song Let's execute it or you want to 9534 10:23:21,240 --> 10:23:26,240 execute by yourself I want you to do 9535 10:23:31,880 --> 10:23:36,160 it let's select the song 9536 10:23:37,200 --> 10:23:39,640 resolver it's going to run the song 9537 10:23:39,640 --> 10:23:43,000 resolver dopc 9538 10:23:47,360 --> 10:23:50,360 file 9539 10:23:52,680 --> 10:23:55,520 you can see the 9540 10:23:55,640 --> 10:23:59,160 error hey your resolver is dependent on 9541 10:23:59,160 --> 10:24:02,400 the song service but you did not import 9542 10:24:02,400 --> 10:24:06,640 the song service let's fix it by mocking 9543 10:24:06,640 --> 10:24:08,840 the 9544 10:24:14,116 --> 10:24:17,320 service I'm going to copy the same 9545 10:24:17,320 --> 10:24:19,000 code 9546 10:24:19,000 --> 10:24:22,680 and paste paste it 9547 10:24:35,880 --> 10:24:39,276 here we don't have song service let's 9548 10:24:39,276 --> 10:24:41,840 import 9549 10:24:47,320 --> 10:24:49,200 it 9550 10:24:49,200 --> 10:24:51,400 it's not giving me the suggestion I have 9551 10:24:51,400 --> 10:24:54,160 to import 9552 10:25:07,000 --> 10:25:10,880 manually I got the song service we also 9553 10:25:10,880 --> 10:25:13,560 need to get the create song input and 9554 10:25:13,560 --> 10:25:16,480 update song 9555 10:25:17,240 --> 10:25:20,240 input 9556 10:25:24,040 --> 10:25:27,560 I think it will not get this path Source 9557 10:25:27,560 --> 10:25:29,800 do/ graphql 9558 10:25:29,800 --> 10:25:34,960 let's add the complete 9559 10:25:41,840 --> 10:25:47,240 path you can see our test has passed 9560 10:25:47,240 --> 10:25:49,520 now 9561 10:25:49,520 --> 10:25:52,080 now we're going to write the test unit 9562 10:25:52,080 --> 10:25:55,720 test for a song's 9563 10:25:57,840 --> 10:26:01,000 query it should fetch a song first of 9564 10:26:01,000 --> 10:26:03,596 all I need to apply a weight expression 9565 10:26:03,596 --> 10:26:07,640 on get songs it is going to give 9566 10:26:07,640 --> 10:26:12,200 me this song because we are using our Mo 9567 10:26:12,200 --> 10:26:15,400 implementation inside the get songs it 9568 10:26:15,400 --> 10:26:19,400 is going to return this object or this 9569 10:26:19,400 --> 10:26:22,840 array with a promise here we are just 9570 10:26:22,840 --> 10:26:25,480 resolving 9571 10:26:35,240 --> 10:26:41,200 it unable to fetch songs I got the 9572 10:26:41,200 --> 10:26:44,276 error let me check my song resolver what 9573 10:26:44,276 --> 10:26:47,160 is inside the song 9574 10:26:47,160 --> 10:26:50,596 resolver I think I'm throwing the error 9575 10:26:50,596 --> 10:26:53,040 that's why I got this error let's 9576 10:26:53,040 --> 10:26:55,596 comment this code and I would like to 9577 10:26:55,596 --> 10:26:57,320 return this song 9578 10:26:57,320 --> 10:27:01,160 service with get songs array it is going 9579 10:27:01,160 --> 10:27:04,000 to give me the array of the songs we are 9580 10:27:04,000 --> 10:27:05,116 just 9581 10:27:05,116 --> 10:27:09,560 using this get song method 9582 10:27:09,560 --> 10:27:13,756 but we are using this 9583 10:27:15,116 --> 10:27:18,880 implementation this one it will call the 9584 10:27:18,880 --> 10:27:23,040 get songs but it is it will use our own 9585 10:27:23,040 --> 10:27:26,240 implementation this 9586 10:27:39,200 --> 10:27:42,320 one this is how you can fix the bugs 9587 10:27:42,320 --> 10:27:45,560 with the help of unit 9588 10:27:47,116 --> 10:27:50,116 testing 9589 10:27:51,200 --> 10:27:53,080 our test has 9590 10:27:53,080 --> 10:27:59,000 passed now we need to test a single song 9591 10:28:01,240 --> 10:28:06,240 query we have created this 9592 10:28:06,240 --> 10:28:10,720 song actually we need to yeah we need to 9593 10:28:10,720 --> 10:28:13,960 test this one we need to create test 9594 10:28:13,960 --> 10:28:15,756 create new song 9595 10:28:15,756 --> 10:28:18,960 resolver we're just creating a new song 9596 10:28:18,960 --> 10:28:22,080 and we are expecting 9597 10:28:25,320 --> 10:28:28,960 it because create song will give me the 9598 10:28:28,960 --> 10:28:31,560 ID and whatever we 9599 10:28:31,560 --> 10:28:33,560 passed 9600 10:28:33,560 --> 10:28:36,520 here the object it is going to return 9601 10:28:36,520 --> 10:28:39,960 the same object 9602 10:28:47,040 --> 10:28:50,040 here 9603 10:28:52,200 --> 10:28:55,436 the test has passed now we need to test 9604 10:28:55,436 --> 10:28:58,436 update and 9605 10:29:01,596 --> 10:29:05,276 delete it is giving me the error update 9606 10:29:05,276 --> 10:29:09,240 song a unique 9607 10:29:17,040 --> 10:29:22,720 ID we are not expecting the ID in the 9608 10:29:22,720 --> 10:29:23,916 update 9609 10:29:23,916 --> 10:29:28,436 song there is an issue I 9610 10:29:37,080 --> 10:29:41,520 think it is going to give me the 9611 10:29:46,960 --> 10:29:49,960 ID 9612 10:29:52,960 --> 10:29:55,810 let's look at the update song 9613 10:29:55,810 --> 10:29:57,360 [Music] 9614 10:29:57,360 --> 10:30:00,240 resolver song 9615 10:30:00,240 --> 10:30:04,596 resolver let's look at the update 9616 10:30:14,596 --> 10:30:16,916 song update 9617 10:30:16,916 --> 10:30:19,916 song 9618 10:30:23,000 --> 10:30:26,160 I think I have to change the order first 9619 10:30:26,160 --> 10:30:29,040 I need to get the ID I need to place the 9620 10:30:29,040 --> 10:30:31,680 ID and then I need to get the update 9621 10:30:31,680 --> 10:30:37,880 song input you have to rearrange the 9622 10:30:40,200 --> 10:30:44,116 order now it should 9623 10:30:46,916 --> 10:30:49,916 work 9624 10:31:00,720 --> 10:31:04,800 let me look at the song 9625 10:31:06,800 --> 10:31:10,240 service ID it's giving you are not using 9626 10:31:10,240 --> 10:31:12,680 the update song 9627 10:31:12,680 --> 10:31:14,756 tto title 9628 10:31:14,756 --> 10:31:18,916 but let's restart the application 9629 10:31:18,916 --> 10:31:22,080 the error is gone 9630 10:31:33,000 --> 10:31:37,400 now everything is fine our update test 9631 10:31:37,400 --> 10:31:41,360 has passed and our delet delete the song 9632 10:31:41,360 --> 10:31:44,880 test has also 9633 10:31:44,880 --> 10:31:48,000 passed it is returning the affected 9634 10:31:48,000 --> 10:31:52,116 that's why I use the affected 9635 10:31:52,800 --> 10:31:56,080 here in the next video I'll teach you 9636 10:31:56,080 --> 10:31:58,680 how to do end to end 9637 10:31:58,680 --> 10:32:02,480 testing graphql 9638 10:32:02,480 --> 10:32:05,880 apis this lesson I'm going to teach you 9639 10:32:05,880 --> 10:32:08,436 how to do end to endend testing with 9640 10:32:08,436 --> 10:32:12,520 graphql apis in the previous lecture we 9641 10:32:12,520 --> 10:32:15,720 tested unit tested we tested the 9642 10:32:15,720 --> 10:32:18,400 individual function of resolver now 9643 10:32:18,400 --> 10:32:20,400 we're going to do integration or end to 9644 10:32:20,400 --> 10:32:22,596 endend testing we're going to test 9645 10:32:22,596 --> 10:32:25,400 exactly the API by sending the HTTP 9646 10:32:25,400 --> 10:32:29,720 request or queries from the HTTP 9647 10:32:29,720 --> 10:32:32,276 module this is how you will test the 9648 10:32:32,276 --> 10:32:34,800 songs 9649 10:32:35,080 --> 10:32:37,320 query and it is going to give me the 9650 10:32:37,320 --> 10:32:39,116 result I'm using the 9651 10:32:39,116 --> 10:32:43,520 request request package to send the HTTP 9652 10:32:43,520 --> 10:32:46,436 request if you are working with graphql 9653 10:32:46,436 --> 10:32:50,116 there is only single endpoint which is/ 9654 10:32:50,116 --> 10:32:53,436 graphql it will be same for each query 9655 10:32:53,436 --> 10:32:54,276 and 9656 10:32:54,276 --> 10:32:56,960 mutation now I'm trying to fetch all the 9657 10:32:56,960 --> 10:32:59,680 songs and I got all the songs this is 9658 10:32:59,680 --> 10:33:03,116 how you will test the end to end testing 9659 10:33:03,116 --> 10:33:06,360 I already taught you how to set up the 9660 10:33:06,360 --> 10:33:08,240 end to end testing 9661 10:33:08,240 --> 10:33:11,756 file when we were building when we were 9662 10:33:11,756 --> 10:33:14,436 testing the rest API in the previous 9663 10:33:14,436 --> 10:33:15,596 previous 9664 10:33:15,596 --> 10:33:18,480 module so 9665 10:33:18,480 --> 10:33:20,240 before each we're going to create 9666 10:33:20,240 --> 10:33:22,560 testing module this is a 9667 10:33:22,560 --> 10:33:24,840 same before 9668 10:33:24,840 --> 10:33:28,080 all we're going to 9669 10:33:28,080 --> 10:33:31,680 create new database I have the test 9670 10:33:31,680 --> 10:33:34,960 database it will drop after completed 9671 10:33:34,960 --> 10:33:37,160 all the 9672 10:33:37,160 --> 10:33:40,040 test and after each we're going to 9673 10:33:40,040 --> 10:33:43,040 delete everything after each test we're 9674 10:33:43,040 --> 10:33:44,880 going to delete everything from the 9675 10:33:44,880 --> 10:33:46,916 songs table that's why I'm using the 9676 10:33:46,916 --> 10:33:50,360 songs Repository do clear method and 9677 10:33:50,360 --> 10:33:52,640 after all we're going to stop the 9678 10:33:52,640 --> 10:33:55,080 application we're going to use this 9679 10:33:55,080 --> 10:33:59,240 method to test it's a helper function to 9680 10:33:59,240 --> 10:34:02,040 perform our testing nothing more it's 9681 10:34:02,040 --> 10:34:05,360 just creating a new 9682 10:34:05,596 --> 10:34:08,960 song and this is how you can test a root 9683 10:34:08,960 --> 10:34:11,040 end point it means our testing is 9684 10:34:11,040 --> 10:34:15,640 working fine let's implement 9685 10:34:15,640 --> 10:34:19,116 it there is no script for end to end 9686 10:34:19,116 --> 10:34:22,680 testing for the we have an end to end 9687 10:34:22,680 --> 10:34:25,560 test but I'm looking for a watch mode 9688 10:34:25,560 --> 10:34:27,840 testing that's why I'm going to add a 9689 10:34:27,840 --> 10:34:31,360 new script it will run end to endend 9690 10:34:31,360 --> 10:34:33,400 testing in the watch 9691 10:34:33,400 --> 10:34:39,000 mode let me run the end to end 9692 10:34:39,000 --> 10:34:42,320 test in the watch 9693 10:34:42,320 --> 10:34:46,400 mode it should be e to 9694 10:34:46,400 --> 10:34:49,880 e e to 9695 10:34:49,880 --> 10:34:53,916 e right now we have only a single file 9696 10:34:53,916 --> 10:34:56,960 which is AB 9697 10:34:58,436 --> 10:35:02,320 do E2 e-pack 9698 10:35:02,320 --> 10:35:06,040 dojon if you run this file let's say 9699 10:35:06,040 --> 10:35:10,720 app it is going to run this 9700 10:35:12,160 --> 10:35:15,436 file I'm going to copy this source 9701 10:35:15,436 --> 10:35:18,960 code and let's create a new file inside 9702 10:35:18,960 --> 10:35:20,720 the test 9703 10:35:20,720 --> 10:35:24,116 folder we will use this test folder for 9704 10:35:24,116 --> 10:35:25,560 in to end 9705 10:35:25,560 --> 10:35:29,436 testing let's define a new file 9706 10:35:29,436 --> 10:35:34,720 song SL do E2 9707 10:35:37,320 --> 10:35:42,000 e-. I have copied the source 9708 10:35:46,560 --> 10:35:49,400 code 9709 10:35:49,400 --> 10:35:52,520 this is the default 9710 10:36:00,200 --> 10:36:04,560 code we need to add a type orm setting 9711 10:36:04,560 --> 10:36:07,240 before all the 9712 10:36:07,240 --> 10:36:12,116 test we need to create a new 9713 10:36:14,000 --> 10:36:19,520 database let's run it here 9714 10:36:20,840 --> 10:36:24,240 we need to import couple of 9715 10:36:24,240 --> 10:36:28,840 modules we need a song module and 9716 10:36:28,840 --> 10:36:33,596 song entity let's get 9717 10:36:45,720 --> 10:36:49,520 it type Ork 9718 10:36:49,756 --> 10:36:55,160 module we also need to import type or 9719 10:36:56,000 --> 10:36:58,640 module actually I'm going to copy 9720 10:36:58,640 --> 10:37:02,596 everything and paste it 9721 10:37:04,756 --> 10:37:08,720 here now we have before 9722 10:37:08,720 --> 10:37:12,596 all we also 9723 10:37:12,840 --> 10:37:16,360 need let's 9724 10:37:16,436 --> 10:37:20,756 define it should be song 9725 10:37:20,756 --> 10:37:24,640 resolver end to end 9726 10:37:27,960 --> 10:37:33,040 test we need after all and after 9727 10:37:33,756 --> 10:37:38,400 each after each test you need to clear 9728 10:37:38,400 --> 10:37:43,160 the songs table and after all the test 9729 10:37:43,160 --> 10:37:46,360 you have to close the 9730 10:37:46,436 --> 10:37:49,436 application 9731 10:37:50,080 --> 10:37:52,160 I'm also going to use this helper 9732 10:37:52,160 --> 10:37:55,360 function I already taught you these 9733 10:37:55,360 --> 10:37:57,916 Basics when we were building when we 9734 10:37:57,916 --> 10:38:01,200 were testing the rest 9735 10:38:01,916 --> 10:38:07,080 apis that's it our setup is complete 9736 10:38:07,080 --> 10:38:11,040 now let's run the 9737 10:38:11,040 --> 10:38:17,000 test this time I would like to run songs 9738 10:38:17,000 --> 10:38:19,400 file 9739 10:38:26,436 --> 10:38:32,436 let's try to run the song file song. E2 9740 10:38:40,400 --> 10:38:44,240 e-pack our test has passed now and you 9741 10:38:44,240 --> 10:38:47,436 can see this test is running fine now 9742 10:38:47,436 --> 10:38:50,400 now so we have successfully done the 9743 10:38:50,400 --> 10:38:56,040 setup now it's time to test the 9744 10:39:00,480 --> 10:39:05,116 query we need to create the song first 9745 10:39:05,116 --> 10:39:08,360 because there is no record in our 9746 10:39:08,360 --> 10:39:10,756 database table which is songs there is 9747 10:39:10,756 --> 10:39:14,560 an empty here there is no 9748 10:39:14,560 --> 10:39:18,640 record so I'm just trying to write the 9749 10:39:18,640 --> 10:39:23,080 query to fetch all the 9750 10:39:23,400 --> 10:39:27,480 songs I created the query data object 9751 10:39:27,480 --> 10:39:29,480 I'm providing the 9752 10:39:29,480 --> 10:39:33,480 query and I'm sending the query in the 9753 10:39:33,480 --> 10:39:35,680 HTTP 9754 10:39:35,680 --> 10:39:38,960 request when you test the API graphql 9755 10:39:38,960 --> 10:39:42,436 API you will get the data or the 9756 10:39:42,436 --> 10:39:45,800 response in data object and we have the 9757 10:39:45,800 --> 10:39:49,680 songs I'm just expecting the songs 9758 10:39:49,680 --> 10:39:53,436 array uh uh let me run the application 9759 10:39:53,436 --> 10:39:57,436 in development so you will understand it 9760 10:39:57,436 --> 10:39:59,840 more 9761 10:40:07,200 --> 10:40:11,520 deeply application is running 9762 10:40:15,000 --> 10:40:18,080 now let's try to write the query to 9763 10:40:18,080 --> 10:40:20,436 fetch all the 9764 10:40:20,436 --> 10:40:23,680 songs if you write the query or you can 9765 10:40:23,680 --> 10:40:26,240 do without without query I'm going to 9766 10:40:26,240 --> 10:40:27,880 write query 9767 10:40:27,880 --> 10:40:32,756 query and I would like to fetch all the 9768 10:40:32,840 --> 10:40:35,960 songs ID and the 9769 10:40:35,960 --> 10:40:40,756 title so you got the result in the data 9770 10:40:40,756 --> 10:40:43,960 object and we have the songs 9771 10:40:43,960 --> 10:40:46,560 array one more thing if you write the 9772 10:40:46,560 --> 10:40:49,800 name of the query you can also do that 9773 10:40:49,800 --> 10:40:52,800 if you have experience in graphql then 9774 10:40:52,800 --> 10:40:56,320 you will know what I am doing you I'm 9775 10:40:56,320 --> 10:40:59,160 just I just gave the name of the query 9776 10:40:59,160 --> 10:41:01,360 get songs you can also do that it will 9777 10:41:01,360 --> 10:41:04,756 work fine there is no 9778 10:41:07,960 --> 10:41:10,916 issue now we're going to test a single 9779 10:41:10,916 --> 10:41:13,360 song 9780 10:41:16,240 --> 10:41:19,240 query 9781 10:41:20,916 --> 10:41:24,800 this time first step I created this 9782 10:41:24,800 --> 10:41:28,160 song and I created the query 9783 10:41:28,160 --> 10:41:32,560 object now we need the variable this is 9784 10:41:32,560 --> 10:41:35,320 how you can Define the variable in 9785 10:41:35,320 --> 10:41:38,800 graphql so I'm providing the variable ID 9786 10:41:38,800 --> 10:41:42,596 here you can exactly 9787 10:41:42,596 --> 10:41:46,360 query in your graphql playground let's 9788 10:41:46,360 --> 10:41:48,640 say 9789 10:41:48,640 --> 10:41:51,000 it this is a new 9790 10:41:51,000 --> 10:41:54,800 tab I can give you the name of the query 9791 10:41:54,800 --> 10:41:57,160 get 9792 10:41:57,800 --> 10:42:02,916 song let me copy that get 9793 10:42:02,916 --> 10:42:06,840 song now I def I created the get song 9794 10:42:06,840 --> 10:42:09,840 query and I'm expecting the ID as a 9795 10:42:09,840 --> 10:42:12,596 variable here I'm providing the variable 9796 10:42:12,596 --> 10:42:16,160 ID this is how you will set the query 9797 10:42:16,160 --> 10:42:20,240 variable this is a another way to fetch 9798 10:42:20,240 --> 10:42:24,840 the get song query I already taught you 9799 10:42:24,840 --> 10:42:28,240 this method it will work fine you got to 9800 10:42:28,240 --> 10:42:29,840 provide the 9801 10:42:29,840 --> 10:42:33,880 ID and here you can expect the name it 9802 10:42:33,880 --> 10:42:37,436 will work fine but this is another 9803 10:42:37,436 --> 10:42:40,276 alternative way of doing this 9804 10:42:40,276 --> 10:42:43,276 thing if you have an ID you can provide 9805 10:42:43,276 --> 10:42:45,560 the ID 9806 10:42:45,560 --> 10:42:49,116 here ID 9807 10:42:51,596 --> 10:42:54,080 we don't have any record that's why I 9808 10:42:54,080 --> 10:42:55,840 got the 9809 10:42:55,840 --> 10:42:59,480 error now we going to test 9810 10:42:59,480 --> 10:43:03,960 it you can see our test has passed now 9811 10:43:03,960 --> 10:43:07,116 because first we are creating the song 9812 10:43:07,116 --> 10:43:09,080 then I'm providing the 9813 10:43:09,080 --> 10:43:13,720 ID variable from the new song object you 9814 10:43:13,720 --> 10:43:16,756 don't need to change anything here you 9815 10:43:16,756 --> 10:43:20,800 can also do expectation 9816 10:43:20,800 --> 10:43:24,240 for assertion check for the status code 9817 10:43:24,240 --> 10:43:27,720 here instead of calling this 9818 10:43:28,000 --> 10:43:30,916 one that's why I I'm expecting the 200 9819 10:43:30,916 --> 10:43:34,276 status code and it is going to give give 9820 10:43:34,276 --> 10:43:36,720 me the response data and the song and 9821 10:43:36,720 --> 10:43:39,800 I'm checking on the based on new song 9822 10:43:39,800 --> 10:43:42,640 both are 9823 10:43:44,000 --> 10:43:47,436 same now our next step is to test the 9824 10:43:47,436 --> 10:43:50,276 create song 9825 10:43:54,680 --> 10:43:58,436 mutation it is the same way you define 9826 10:43:58,436 --> 10:44:01,800 the mutation I give the name create song 9827 10:44:01,800 --> 10:44:05,000 mutation instead of ID I'm expecting the 9828 10:44:05,000 --> 10:44:08,756 create song input we have created the 9829 10:44:08,756 --> 10:44:11,436 variable and I'm getting the create song 9830 10:44:11,436 --> 10:44:14,960 input we have title and here you can 9831 10:44:14,960 --> 10:44:18,916 provide the values for your 9832 10:44:18,916 --> 10:44:21,840 variables if you want you can do the 9833 10:44:21,840 --> 10:44:23,520 exact same 9834 10:44:23,520 --> 10:44:27,520 thing from your graphql playground let's 9835 10:44:27,520 --> 10:44:31,960 say I can say I'm going to create a new 9836 10:44:32,080 --> 10:44:37,000 song I just past it pasted the 9837 10:44:37,000 --> 10:44:41,360 mutation if you run there is no query 9838 10:44:41,360 --> 10:44:44,916 variable for create song 9839 10:44:44,916 --> 10:44:48,756 input let's define it you have to 9840 10:44:48,756 --> 10:44:51,360 provide the title I can 9841 10:44:51,360 --> 10:44:53,116 say 9842 10:44:53,116 --> 10:44:54,640 lover 9843 10:44:54,640 --> 10:44:59,200 song you can see the lover 9844 10:45:00,596 --> 10:45:04,960 song now I can provide the ID here let's 9845 10:45:04,960 --> 10:45:08,080 test it 9846 10:45:08,080 --> 10:45:12,596 here I provided the ID so I got the song 9847 10:45:12,596 --> 10:45:15,720 object there is a data object and we 9848 10:45:15,720 --> 10:45:18,916 have a create song 9849 10:45:18,916 --> 10:45:22,916 object that's why I used create song 9850 10:45:22,916 --> 10:45:26,080 Here create sing do title should be 9851 10:45:26,080 --> 10:45:29,840 animals our test has passed now we need 9852 10:45:29,840 --> 10:45:32,840 to run update mutation similarly just 9853 10:45:32,840 --> 10:45:36,080 like for just like with create 9854 10:45:36,080 --> 10:45:40,276 mutation it is expecting to arguments ID 9855 10:45:40,276 --> 10:45:43,200 and the update song input and we are 9856 10:45:43,200 --> 10:45:45,720 providing the ID in the variables and 9857 10:45:45,720 --> 10:45:48,000 update song input here 9858 10:45:48,000 --> 10:45:51,276 here it is going to return the affected 9859 10:45:51,276 --> 10:45:52,960 with 9860 10:45:52,960 --> 10:45:56,480 integer now we're going to copy this 9861 10:45:56,480 --> 10:45:58,800 delete 9862 10:45:58,800 --> 10:46:02,080 mutation delete is expecting only the ID 9863 10:46:02,080 --> 10:46:04,400 and it is going to return the 9864 10:46:04,400 --> 10:46:06,800 affected I defined the delete song 9865 10:46:06,800 --> 10:46:09,000 mutation and here I'm expecting the 9866 10:46:09,000 --> 10:46:11,960 value which is 9867 10:46:15,840 --> 10:46:18,840 one 9868 10:46:22,400 --> 10:46:27,116 our test have has P tests have 9869 10:46:27,116 --> 10:46:29,276 passed 9870 10:46:29,276 --> 10:46:35,250 query and we also need to test delete 9871 10:46:35,250 --> 10:46:38,699 [Music] 9872 10:46:43,276 --> 10:46:48,840 mutation delete mutation test has passed 9873 10:46:50,436 --> 10:46:53,320 F this is how you will do end to end 9874 10:46:53,320 --> 10:46:56,080 testing in 9875 10:46:56,240 --> 10:46:59,000 graphql in this lesson I'm going to 9876 10:46:59,000 --> 10:47:01,116 teach you the concept of 9877 10:47:01,116 --> 10:47:04,560 caching Apollo server provides caching 9878 10:47:04,560 --> 10:47:07,320 on the server site you can implement 9879 10:47:07,320 --> 10:47:11,276 it caching is a process of or method of 9880 10:47:11,276 --> 10:47:15,080 storing copies of file or data in a 9881 10:47:15,080 --> 10:47:17,756 temporary storage location 9882 10:47:17,756 --> 10:47:21,520 or cash to be accessed quickly like you 9883 10:47:21,520 --> 10:47:23,916 want to store the data temporarily in 9884 10:47:23,916 --> 10:47:27,000 some type of in memory built-in memory 9885 10:47:27,000 --> 10:47:30,000 you can store the data Maybe for 30 9886 10:47:30,000 --> 10:47:33,480 seconds or 60 seconds or a 1 9887 10:47:33,480 --> 10:47:36,880 minute it's called caching what is 9888 10:47:36,880 --> 10:47:38,720 server side caching you want to store 9889 10:47:38,720 --> 10:47:41,840 let's say you want to fetching the query 9890 10:47:41,840 --> 10:47:43,880 results you have a 9891 10:47:43,880 --> 10:47:47,400 query and user is fetching the 9892 10:47:47,400 --> 10:47:51,080 query constantly after every 10 seconds 9893 10:47:51,080 --> 10:47:54,436 after every 20 seconds or after every 10 9894 10:47:54,436 --> 10:47:58,240 seconds in that case you need caching 9895 10:47:58,240 --> 10:47:59,960 you don't want to send the request 9896 10:47:59,960 --> 10:48:03,520 backend server to DB server again and 9897 10:48:03,520 --> 10:48:07,080 again so it will increase the massive 9898 10:48:07,080 --> 10:48:08,800 traffic 9899 10:48:08,800 --> 10:48:11,520 load you are thinking why do why need 9900 10:48:11,520 --> 10:48:14,116 why do we need 9901 10:48:14,116 --> 10:48:17,916 caching if user is fetching the query 9902 10:48:17,916 --> 10:48:20,720 after 3 seconds again and again you did 9903 10:48:20,720 --> 10:48:23,360 not implement the caching your 9904 10:48:23,360 --> 10:48:25,480 application will send the request to the 9905 10:48:25,480 --> 10:48:28,080 backend server DB server and it will and 9906 10:48:28,080 --> 10:48:31,640 it will fetch the data from that server 9907 10:48:31,640 --> 10:48:34,040 again and 9908 10:48:34,040 --> 10:48:38,040 again it will decrease the performance 9909 10:48:38,040 --> 10:48:41,200 so the help of caching you can improve 9910 10:48:41,200 --> 10:48:43,640 the performance of your 9911 10:48:43,640 --> 10:48:46,720 application you can avoid unnecessary 9912 10:48:46,720 --> 10:48:50,116 request to server because the data will 9913 10:48:50,116 --> 10:48:52,960 be fetched from the cache 9914 10:48:52,960 --> 10:48:55,720 immediately you can reduce the bandwidth 9915 10:48:55,720 --> 10:48:58,756 which will be benefit to user you can 9916 10:48:58,756 --> 10:49:02,320 reduce the massive DB 9917 10:49:02,320 --> 10:49:04,800 loads with the help of caching you can 9918 10:49:04,800 --> 10:49:07,160 handle massive Network traffic 9919 10:49:07,160 --> 10:49:10,240 request yeah you can decrease the 9920 10:49:10,240 --> 10:49:12,720 service 9921 10:49:13,040 --> 10:49:16,000 calls so that was the basic fundamental 9922 10:49:16,000 --> 10:49:18,160 concept of caching now we're going to 9923 10:49:18,160 --> 10:49:21,080 implement the caching in in nests 9924 10:49:21,080 --> 10:49:23,916 project nests already using Apollo 9925 10:49:23,916 --> 10:49:26,640 service AP poo server Apollo 9926 10:49:26,640 --> 10:49:29,276 package so we're going to install the 9927 10:49:29,276 --> 10:49:32,880 package uh install this caching module 9928 10:49:32,880 --> 10:49:37,116 Apollo server plug-in response 9929 10:49:39,800 --> 10:49:42,756 cache this plug-in helps optimize the 9930 10:49:42,756 --> 10:49:44,840 performance of graphql queries by 9931 10:49:44,840 --> 10:49:47,200 caching the responses 9932 10:49:47,200 --> 10:49:50,520 this this plugin will help us to Cache 9933 10:49:50,520 --> 10:49:53,360 the response and serving the data from 9934 10:49:53,360 --> 10:49:56,520 the cache instead of executing the same 9935 10:49:56,520 --> 10:49:57,916 query 9936 10:49:57,916 --> 10:50:00,640 repeatedly our next step will be 9937 10:50:00,640 --> 10:50:02,360 register the caching 9938 10:50:02,360 --> 10:50:06,560 plugins when you install this plugin you 9939 10:50:06,560 --> 10:50:09,400 can register it as a plugins 9940 10:50:09,400 --> 10:50:12,840 array I have set the default maximum age 9941 10:50:12,840 --> 10:50:15,596 which is 5 Seconds I'm 9942 10:50:15,596 --> 10:50:17,520 saying hey 9943 10:50:17,520 --> 10:50:21,800 Apollo you can keep the data in 9944 10:50:21,800 --> 10:50:24,160 cash for 5 9945 10:50:24,160 --> 10:50:28,000 seconds after 5 Seconds you have to 9946 10:50:28,000 --> 10:50:30,240 refresh the 9947 10:50:30,240 --> 10:50:34,916 cache you have to refresh the 9948 10:50:38,240 --> 10:50:41,160 data and then you need to create a 9949 10:50:41,160 --> 10:50:44,560 directive cash controls SC private or 9950 10:50:44,560 --> 10:50:47,480 public you want to fetch the data 9951 10:50:47,480 --> 10:50:49,560 publicly or you want to fetch save the 9952 10:50:49,560 --> 10:50:53,320 data privately private represents a cash 9953 10:50:53,320 --> 10:50:55,320 control scope where the response should 9954 10:50:55,320 --> 10:50:58,560 be cached privately if you want to fetch 9955 10:50:58,560 --> 10:51:00,560 the authenticated user something like 9956 10:51:00,560 --> 10:51:02,880 that then then you need to set the 9957 10:51:02,880 --> 10:51:05,720 private represent a cache control scope 9958 10:51:05,720 --> 10:51:08,560 where the response can be cached 9959 10:51:08,560 --> 10:51:12,320 publicly this directive will have max 9960 10:51:12,320 --> 10:51:17,080 age scope and inherit max age and you 9961 10:51:17,080 --> 10:51:21,116 can apply cache control directive on 9962 10:51:21,116 --> 10:51:22,480 field 9963 10:51:22,480 --> 10:51:25,436 definition you can apply on a fing field 9964 10:51:25,436 --> 10:51:28,436 like title field in a song 9965 10:51:28,436 --> 10:51:32,400 type you can apply on object interface 9966 10:51:32,400 --> 10:51:35,960 and Union the max age is an optional 9967 10:51:35,960 --> 10:51:39,080 argument of type in that represents the 9968 10:51:39,080 --> 10:51:42,200 maximum age of the cach control in 9969 10:51:42,200 --> 10:51:44,800 seconds how long the 9970 10:51:44,800 --> 10:51:49,916 response can be considered a fresh and 9971 10:51:49,916 --> 10:51:53,240 cachable you can read this documentation 9972 10:51:53,240 --> 10:51:57,680 of maxh I've written down for 9973 10:51:59,800 --> 10:52:02,720 you private and public scope this will 9974 10:52:02,720 --> 10:52:05,916 be used for parent let's suppose your 9975 10:52:05,916 --> 10:52:09,360 query is dependent on parent 9976 10:52:09,360 --> 10:52:13,560 query then you need to use this 9977 10:52:14,720 --> 10:52:17,916 one you can also set the cach control 9978 10:52:17,916 --> 10:52:19,160 set cach 9979 10:52:19,160 --> 10:52:23,200 hint in the resolver by using this 9980 10:52:23,200 --> 10:52:26,160 line our next step will be apply the 9981 10:52:26,160 --> 10:52:29,596 cache control directive I applying it 9982 10:52:29,596 --> 10:52:34,360 for titled field inside the song 9983 10:52:34,360 --> 10:52:38,000 type cache control maximum age for 30 9984 10:52:38,000 --> 10:52:41,596 seconds I'm saying to Apollo hey 9985 10:52:41,596 --> 10:52:45,596 Apollo you can keep the title field 9986 10:52:45,596 --> 10:52:47,000 record 9987 10:52:47,000 --> 10:52:49,596 for 30 seconds in a 9988 10:52:49,596 --> 10:52:53,596 cache if user is fetching this title 9989 10:52:53,596 --> 10:52:58,240 field constantly after 20 seconds or 9990 10:52:58,240 --> 10:52:59,800 after 10 9991 10:52:59,800 --> 10:53:03,800 seconds then it will fetch from the 9992 10:53:03,800 --> 10:53:07,680 cash if user needs to fetch record after 9993 10:53:07,680 --> 10:53:11,000 a minute or after a 2 minute it will 9994 10:53:11,000 --> 10:53:15,200 fetch the data from the DB server but it 9995 10:53:15,200 --> 10:53:19,640 depends on you on your case on your use 9996 10:53:19,640 --> 10:53:22,840 case but 30 60 and 40 are the 9997 10:53:22,840 --> 10:53:25,160 recommended values on the based on 9998 10:53:25,160 --> 10:53:26,400 Apollo 9999 10:53:26,400 --> 10:53:28,880 documentation Apollo server uses 10000 10:53:28,880 --> 10:53:31,680 inmemory cache by default we are we are 10001 10:53:31,680 --> 10:53:34,400 going to use inmemory cache but you can 10002 10:53:34,400 --> 10:53:36,480 also configure different backend like 10003 10:53:36,480 --> 10:53:38,720 redis or M 10004 10:53:38,720 --> 10:53:42,400 cach you can also apply cash control 10005 10:53:42,400 --> 10:53:45,840 directive on a complete type let's say 10006 10:53:45,840 --> 10:53:48,040 if you have a scenario where you need to 10007 10:53:48,040 --> 10:53:52,000 fetch all of the fields from a specific 10008 10:53:52,000 --> 10:53:55,680 type then you can 10009 10:53:55,680 --> 10:53:59,200 add here cache control directive maximum 10010 10:53:59,200 --> 10:54:00,960 age to 40 10011 10:54:00,960 --> 10:54:03,880 seconds I just give you the basics 10012 10:54:03,880 --> 10:54:06,200 Basics when you implement in real world 10013 10:54:06,200 --> 10:54:09,480 scenario in real world large application 10014 10:54:09,480 --> 10:54:12,360 you will know your use case better now 10015 10:54:12,360 --> 10:54:14,360 it's time to implement it or you can 10016 10:54:14,360 --> 10:54:17,596 implement it by your own 10017 10:54:17,596 --> 10:54:21,800 let's install the plug-in 10018 10:54:32,240 --> 10:54:36,400 first plug-in have installed let's 10019 10:54:36,400 --> 10:54:39,840 import it in the app 10020 10:54:42,800 --> 10:54:45,360 module now we're going to register the 10021 10:54:45,360 --> 10:54:48,080 plug-in into plug array when you are 10022 10:54:48,080 --> 10:54:50,436 creating graphql module you can register 10023 10:54:50,436 --> 10:54:52,756 it 10024 10:54:55,276 --> 10:54:58,680 here so our third step is to creating a 10025 10:54:58,680 --> 10:55:01,680 cache control 10026 10:55:04,000 --> 10:55:07,880 directive I can do it 10027 10:55:08,160 --> 10:55:13,360 here it will have max age scope inherit 10028 10:55:13,360 --> 10:55:16,560 inherit max age private if you want to 10029 10:55:16,560 --> 10:55:19,200 dig into more deeper you can look at 10030 10:55:19,200 --> 10:55:21,916 from the Apollo server 10031 10:55:21,916 --> 10:55:24,360 documentation when you Google Apollo 10032 10:55:24,360 --> 10:55:27,116 server 10033 10:55:27,116 --> 10:55:30,640 caching this is the document you got to 10034 10:55:30,640 --> 10:55:33,680 read if you want to dig into deeper what 10035 10:55:33,680 --> 10:55:37,800 is Max age what is cach control scope 10036 10:55:37,800 --> 10:55:39,960 you can read this 10037 10:55:39,960 --> 10:55:43,400 document this is the implementation of 10038 10:55:43,400 --> 10:55:46,840 caching in AP poo server 10039 10:55:46,840 --> 10:55:48,560 if you want to do ready setup you can 10040 10:55:48,560 --> 10:55:51,160 also do 10041 10:55:55,080 --> 10:55:59,160 that we have created the cache control 10042 10:55:59,160 --> 10:56:01,160 directive 10043 10:56:01,160 --> 10:56:04,520 now I want to apply the cache control 10044 10:56:04,520 --> 10:56:08,480 directive on our field right now I have 10045 10:56:08,480 --> 10:56:12,480 only one single Field title I'm saying 10046 10:56:12,480 --> 10:56:14,560 I'm assuming that my 10047 10:56:14,560 --> 10:56:17,800 title field is going to be 10048 10:56:17,800 --> 10:56:19,320 feted 10049 10:56:19,320 --> 10:56:23,436 after 10 seconds I have a query in that 10050 10:56:23,436 --> 10:56:27,640 use case like client is asking title 10051 10:56:27,640 --> 10:56:30,680 field again and 10052 10:56:34,680 --> 10:56:39,040 again let's try to test it 10053 10:56:45,160 --> 10:56:48,160 first 10054 10:56:51,320 --> 10:56:55,116 my application server has 10055 10:57:00,276 --> 10:57:03,320 started now I can say I want to access 10056 10:57:03,320 --> 10:57:04,596 the 10057 10:57:04,596 --> 10:57:09,400 query of the songs I want to fetch the 10058 10:57:09,400 --> 10:57:13,436 title you can see it's working fine 10059 10:57:13,436 --> 10:57:17,520 now let's fetch the title again 10060 10:57:17,520 --> 10:57:21,160 now it is accessing the title field from 10061 10:57:21,160 --> 10:57:23,480 the inmemory 10062 10:57:23,480 --> 10:57:27,800 cache one last thing you can also apply 10063 10:57:27,800 --> 10:57:32,040 cache control directive on a type here I 10064 10:57:32,040 --> 10:57:34,480 want to say I have only single type 10065 10:57:34,480 --> 10:57:36,436 that's why I want to use it for only 10066 10:57:36,436 --> 10:57:39,240 demo purpose in real world application 10067 10:57:39,240 --> 10:57:42,276 you will have a different use case 10068 10:57:42,276 --> 10:57:43,570 restart the 10069 10:57:43,570 --> 10:57:45,116 [Music] 10070 10:57:45,116 --> 10:57:48,116 application 10071 10:57:52,890 --> 10:57:56,620 [Music] 10072 10:57:57,756 --> 10:58:00,116 my application is running now I'm going 10073 10:58:00,116 --> 10:58:02,400 to access this song FEI 10074 10:58:02,400 --> 10:58:06,436 songs it's still working 10075 10:58:08,320 --> 10:58:13,360 fine if I try to access the 10076 10:58:13,360 --> 10:58:18,040 song we need to provide the ID 10077 10:58:18,200 --> 10:58:21,240 let's fetch the 10078 10:58:26,160 --> 10:58:30,720 ID I'm going to copy the ID and let me 10079 10:58:30,720 --> 10:58:33,480 paste it 10080 10:58:33,916 --> 10:58:37,596 here now I would like to access the ID 10081 10:58:37,596 --> 10:58:41,240 and the title let's access the title 10082 10:58:41,240 --> 10:58:44,276 only or the 10083 10:58:44,276 --> 10:58:48,680 ID now the data is is going to be 10084 10:58:48,680 --> 10:58:52,200 cached I can access it 10085 10:58:52,200 --> 10:58:55,880 easily it's working 10086 10:58:56,320 --> 10:58:59,040 fine in this lesson I'm going to teach 10087 10:58:59,040 --> 10:59:02,520 you the concept of data loader why do we 10088 10:59:02,520 --> 10:59:05,080 need data 10089 10:59:05,200 --> 10:59:08,240 loer we have this schema I've already 10090 10:59:08,240 --> 10:59:14,040 found example project regarding data 10091 10:59:14,436 --> 10:59:18,916 loader we have a user and we have a post 10092 10:59:18,916 --> 10:59:20,400 there is a 10093 10:59:20,400 --> 10:59:24,960 relationship between user and the 10094 10:59:24,960 --> 10:59:28,480 post when you send the graphql request 10095 10:59:28,480 --> 10:59:32,116 to fetch all the post and you also want 10096 10:59:32,116 --> 10:59:35,116 to the owner of this post who has 10097 10:59:35,116 --> 10:59:36,640 created this 10098 10:59:36,640 --> 10:59:39,680 post under the 10099 10:59:39,680 --> 10:59:44,880 hood graphql your application will send 10100 10:59:44,880 --> 10:59:48,756 a a different different a new data 10101 10:59:48,756 --> 10:59:52,240 request to the database a new request to 10102 10:59:52,240 --> 10:59:57,160 the database to fetch a user for each 10103 10:59:57,160 --> 11:00:00,960 post this will decrease the performance 10104 11:00:00,960 --> 11:00:03,276 of your application because you're going 10105 11:00:03,276 --> 11:00:07,560 to do lot of database 10106 11:00:07,560 --> 11:00:10,680 request here your output will look like 10107 11:00:10,680 --> 11:00:14,916 that if I will show you 10108 11:00:14,916 --> 11:00:18,360 here let me show you with the help of 10109 11:00:18,360 --> 11:00:21,520 example I'm going to send the 10110 11:00:21,520 --> 11:00:25,360 request it is going to take a 3 seconds 10111 11:00:25,360 --> 11:00:28,596 because I have set this delay for the 3 10112 11:00:28,596 --> 11:00:32,276 second we have how many post we have we 10113 11:00:32,276 --> 11:00:36,116 have three post you can look at the 10114 11:00:36,116 --> 11:00:40,520 schema or our database I don't I'm not 10115 11:00:40,520 --> 11:00:43,840 using any database 10116 11:00:44,916 --> 11:00:47,916 here 10117 11:00:49,000 --> 11:00:51,520 so here is the 10118 11:00:51,520 --> 11:00:54,080 data we have a three 10119 11:00:54,080 --> 11:00:59,596 post inside the post we have a user 10120 11:01:02,116 --> 11:01:05,520 ID when you look at the user service we 10121 11:01:05,520 --> 11:01:08,880 have all of these 10122 11:01:08,880 --> 11:01:12,360 users every time let's say I log the 10123 11:01:12,360 --> 11:01:16,400 message fetching data from DB 10124 11:01:16,400 --> 11:01:18,276 let's example you can say that getting 10125 11:01:18,276 --> 11:01:23,480 user ID 1 fetching the user ID one this 10126 11:01:23,480 --> 11:01:25,360 is the you can imagine this is the 10127 11:01:25,360 --> 11:01:26,800 database 10128 11:01:26,800 --> 11:01:30,240 request every time it is going to fetch 10129 11:01:30,240 --> 11:01:35,560 the post for each user against post it 10130 11:01:35,560 --> 11:01:38,400 has to fetch the record from the 10131 11:01:38,400 --> 11:01:41,840 database you can imagine if you want to 10132 11:01:41,840 --> 11:01:46,680 load 50 post and you have to send the 10133 11:01:46,680 --> 11:01:52,160 API or backend DB request for each user 10134 11:01:52,160 --> 11:01:55,880 separately so what data loader will do 10135 11:01:55,880 --> 11:02:00,200 data loader will batch all of the users 10136 11:02:00,200 --> 11:02:03,436 it's going to wait and say I can send 10137 11:02:03,436 --> 11:02:05,720 you 10138 11:02:07,960 --> 11:02:12,680 request here what data loader will 10139 11:02:13,436 --> 11:02:16,400 do it will say 10140 11:02:16,400 --> 11:02:17,240 I 10141 11:02:17,240 --> 11:02:22,160 will get all of the IDS of the 10142 11:02:22,160 --> 11:02:25,756 users like one 2 10143 11:02:25,756 --> 11:02:31,360 3 and then I will 10144 11:02:31,360 --> 11:02:34,880 fetch all of the 10145 11:02:34,880 --> 11:02:40,400 users for all post and then if you need 10146 11:02:40,400 --> 11:02:43,240 any post if you need any 10147 11:02:43,240 --> 11:02:46,276 user against each 10148 11:02:46,276 --> 11:02:51,916 post you will fetch from the cash or 10149 11:02:51,916 --> 11:02:54,680 from the data loader cash data loader 10150 11:02:54,680 --> 11:02:57,436 maintains cash 10151 11:02:57,436 --> 11:03:00,916 also did you get the 10152 11:03:00,916 --> 11:03:03,880 point let's say I'm going to add I have 10153 11:03:03,880 --> 11:03:07,916 four users inside the post 10154 11:03:13,916 --> 11:03:18,640 service I'm going to add post 10155 11:03:21,240 --> 11:03:25,436 for title post 10156 11:03:25,680 --> 11:03:27,680 for 10157 11:03:27,680 --> 11:03:34,680 something and user ID which is for this 10158 11:03:37,320 --> 11:03:40,400 one application is running now I'm going 10159 11:03:40,400 --> 11:03:44,720 to send the request to fetch all the 10160 11:03:44,720 --> 11:03:47,720 post 10161 11:03:48,680 --> 11:03:53,720 now I got the for post and it has sent 10162 11:03:53,720 --> 11:03:57,276 the DB request to fetch a single user 10163 11:03:57,276 --> 11:03:59,720 what we can do we can say I want to 10164 11:03:59,720 --> 11:04:02,720 fetch if we are using data loader please 10165 11:04:02,720 --> 11:04:06,436 fetch all the users it's batching all 10166 11:04:06,436 --> 11:04:09,240 the users from the DB and maintains a 10167 11:04:09,240 --> 11:04:12,880 cache and next time our application will 10168 11:04:12,880 --> 11:04:17,400 fetch the user from this cache 10169 11:04:17,400 --> 11:04:19,200 I have already implemented the data 10170 11:04:19,200 --> 11:04:21,596 loader in this project actually I found 10171 11:04:21,596 --> 11:04:23,596 the source code on the internet to 10172 11:04:23,596 --> 11:04:25,756 explain the data loader it's a simple 10173 11:04:25,756 --> 11:04:27,596 source code so I thought I should teach 10174 11:04:27,596 --> 11:04:31,480 it with the help of this source 10175 11:04:31,596 --> 11:04:35,320 code inside the post 10176 11:04:35,320 --> 11:04:37,916 resolver here they are resolving the 10177 11:04:37,916 --> 11:04:41,080 created by field created by and it is 10178 11:04:41,080 --> 11:04:43,160 going to return the 10179 11:04:43,160 --> 11:04:47,400 user here instead of loading the user 10180 11:04:47,400 --> 11:04:49,880 from the database let's user service 10181 11:04:49,880 --> 11:04:53,400 will send the DB request so I'm going to 10182 11:04:53,400 --> 11:04:57,160 comment and now you will see the magic 10183 11:04:57,160 --> 11:05:00,800 instead of loading the user from the 10184 11:05:00,800 --> 11:05:04,960 DB I would like to fetch the user from 10185 11:05:04,960 --> 11:05:07,360 data 10186 11:05:10,200 --> 11:05:14,240 loader now we're going to send the 10187 11:05:14,640 --> 11:05:17,640 request 10188 11:05:21,080 --> 11:05:24,360 you can see that getting the users with 10189 11:05:24,360 --> 11:05:27,520 ID 1 2 and 10190 11:05:27,520 --> 11:05:32,080 4 it has bashed all the users and 10191 11:05:32,080 --> 11:05:37,480 created the user map to send the ID's 10192 11:05:37,480 --> 11:05:41,720 record to data loader and here is the 10193 11:05:41,720 --> 11:05:44,080 result so let me 10194 11:05:44,080 --> 11:05:47,720 explain what this source code is doing 10195 11:05:47,720 --> 11:05:50,840 if you check the app 10196 11:05:50,840 --> 11:05:54,880 module here we are providing the loader 10197 11:05:54,880 --> 11:05:57,596 I created the create user loader and 10198 11:05:57,596 --> 11:05:59,480 they are injecting the user 10199 11:05:59,480 --> 11:06:03,040 service inside the user service inside 10200 11:06:03,040 --> 11:06:05,320 the create user loader I'm passing the 10201 11:06:05,320 --> 11:06:07,720 user 10202 11:06:09,360 --> 11:06:12,480 service to work with data loader you 10203 11:06:12,480 --> 11:06:17,520 only need a single package which is data 10204 11:06:18,880 --> 11:06:21,640 loader this one you have to install only 10205 11:06:21,640 --> 11:06:24,116 the data 10206 11:06:26,800 --> 11:06:30,560 loader here you will have a user 10207 11:06:30,560 --> 11:06:33,800 loader what this loader will do it is 10208 11:06:33,800 --> 11:06:37,040 it's nothing more it's just a function 10209 11:06:37,040 --> 11:06:40,080 takes a user 10210 11:06:40,080 --> 11:06:43,436 service so I created the instance of a 10211 11:06:43,436 --> 11:06:47,116 data loader the ID will ID would be 10212 11:06:47,116 --> 11:06:50,276 number and another type which is the 10213 11:06:50,276 --> 11:06:52,680 object should be the 10214 11:06:52,680 --> 11:06:55,960 user this when you create the data 10215 11:06:55,960 --> 11:06:58,360 loader you have to provide it is going 10216 11:06:58,360 --> 11:07:02,240 to give me the IDS all of the 10217 11:07:02,400 --> 11:07:06,880 IDS and here I can fetch all of the 10218 11:07:06,880 --> 11:07:11,320 users against IDs here I did the users 10219 11:07:11,320 --> 11:07:16,800 map you you saw the users map here 1 2 3 10220 11:07:16,800 --> 11:07:18,880 because data loader Works in this way 10221 11:07:18,880 --> 11:07:21,000 you have to provide the results in this 10222 11:07:21,000 --> 11:07:26,800 way like I got 1 2 4 then I did the map 10223 11:07:26,800 --> 11:07:32,596 to fetch all the records ID 1 2 and 10224 11:07:34,360 --> 11:07:37,240 4 we created the user loader I have 10225 11:07:37,240 --> 11:07:40,756 attached inside the context 10226 11:07:40,756 --> 11:07:43,840 object remember graph resolver can have 10227 11:07:43,840 --> 11:07:49,400 four arguments in for context and 10228 11:07:50,160 --> 11:07:54,320 parent we have user loader here to fetch 10229 11:07:54,320 --> 11:07:57,756 all the users against a 10230 11:07:57,756 --> 11:08:01,080 post here this one inside the post 10231 11:08:01,080 --> 11:08:04,000 resolver I'm resolving the created by 10232 11:08:04,000 --> 11:08:06,840 field I got the user loader from the 10233 11:08:06,840 --> 11:08:09,596 context here you need to load the 10234 11:08:09,596 --> 11:08:13,916 user from the user 10235 11:08:13,916 --> 11:08:18,116 loader I got the parent I parent record 10236 11:08:18,116 --> 11:08:20,080 from the post which is the 10237 11:08:20,080 --> 11:08:24,080 post so I got the user 10238 11:08:24,080 --> 11:08:26,680 ID which we saved 10239 11:08:26,680 --> 11:08:30,560 here we have one more thing we have used 10240 11:08:30,560 --> 11:08:33,276 a schema based approach to build graphql 10241 11:08:33,276 --> 11:08:35,960 apis but I also wanted to show you 10242 11:08:35,960 --> 11:08:38,240 another way to build graphql API which 10243 11:08:38,240 --> 11:08:41,240 is code first approach this code first 10244 11:08:41,240 --> 11:08:43,200 approach will generate the schema. 10245 11:08:43,200 --> 11:08:46,276 graphql file automatically it depends on 10246 11:08:46,276 --> 11:08:48,480 you but I like the most schema based 10247 11:08:48,480 --> 11:08:50,596 approach but you can use both of them it 10248 11:08:50,596 --> 11:08:53,436 depends on your use case this is how you 10249 11:08:53,436 --> 11:08:57,116 will Define the graph qu object 10250 11:08:57,116 --> 11:09:00,436 type but we have worked with this schema 10251 11:09:00,436 --> 11:09:01,596 based 10252 11:09:01,596 --> 11:09:04,200 approach let's say you want to create 10253 11:09:04,200 --> 11:09:06,840 this graphel type which is posted with 10254 11:09:06,840 --> 11:09:08,840 the help of code based 10255 11:09:08,840 --> 11:09:11,596 approach you will say this is the graph 10256 11:09:11,596 --> 11:09:14,200 object type you can get it from nestjs 10257 11:09:14,200 --> 11:09:16,916 graphql package and this is the field 10258 11:09:16,916 --> 11:09:19,880 you can say this one this is the field 10259 11:09:19,880 --> 11:09:22,480 and you can 10260 11:09:23,436 --> 11:09:27,040 say title field and we have the body 10261 11:09:27,040 --> 11:09:30,276 field and we have the user ID I'm not 10262 11:09:30,276 --> 11:09:32,916 I'm not saving into the graphql datab 10263 11:09:32,916 --> 11:09:36,880 graphql graphql type that's why I keep 10264 11:09:36,880 --> 11:09:39,880 the user ID for entity based to build a 10265 11:09:39,880 --> 11:09:42,840 relationship with them so created by 10266 11:09:42,840 --> 11:09:46,276 this is the relationship between a user 10267 11:09:46,276 --> 11:09:48,680 here you can see that created by the 10268 11:09:48,680 --> 11:09:51,840 return type should not be null that's 10269 11:09:51,840 --> 11:09:55,240 why I they have used the user this is 10270 11:09:55,240 --> 11:09:58,756 this represents the return type what 10271 11:09:58,756 --> 11:10:02,480 your field will return so it's the type 10272 11:10:02,480 --> 11:10:05,880 of this created by it should return a 10273 11:10:05,880 --> 11:10:07,756 user user 10274 11:10:07,756 --> 11:10:13,160 type similarly you can look at the user 10275 11:10:14,240 --> 11:10:16,040 entity 10276 11:10:16,040 --> 11:10:18,360 object type this is how you will Define 10277 11:10:18,360 --> 11:10:22,880 here this is graphql type and my user id 10278 11:10:22,880 --> 11:10:25,360 the type should be number that should be 10279 11:10:25,360 --> 11:10:28,800 int that's why it's a nonn knowable 10280 11:10:28,800 --> 11:10:33,880 field and it should return or the type 10281 11:10:33,880 --> 11:10:37,040 of this ID should be int and we have a 10282 11:10:37,040 --> 11:10:39,596 field which is graphical type so this 10283 11:10:39,596 --> 11:10:43,880 code will be converted into this schema 10284 11:10:43,880 --> 11:10:47,040 type and how can you resolve the 10285 11:10:47,040 --> 11:10:51,360 resolver when you look at the post 10286 11:10:53,000 --> 11:10:55,880 resolver this is the resolver and I say 10287 11:10:55,880 --> 11:10:57,916 this is the post resolver that's why I 10288 11:10:57,916 --> 11:11:00,400 provided the post 10289 11:11:00,400 --> 11:11:03,916 entity now we have the post resolver the 10290 11:11:03,916 --> 11:11:05,400 query 10291 11:11:05,400 --> 11:11:10,756 tag so here you want to say what 10292 11:11:10,756 --> 11:11:13,596 resolver this will do in we are in the 10293 11:11:13,596 --> 11:11:17,240 post we are here 10294 11:11:17,960 --> 11:11:23,436 so this query will return all the post 10295 11:11:23,436 --> 11:11:26,276 and the name of the query should be post 10296 11:11:26,276 --> 11:11:29,916 I'm resolving this query so I'm fetching 10297 11:11:29,916 --> 11:11:32,200 the post similarly if you want to 10298 11:11:32,200 --> 11:11:36,560 resolve the created by here this one you 10299 11:11:36,560 --> 11:11:38,840 have to provide the resolve 10300 11:11:38,840 --> 11:11:41,240 field is all 10301 11:11:41,240 --> 11:11:44,240 field there is a field inside the post 10302 11:11:44,240 --> 11:11:46,400 Ty which is created by and it should 10303 11:11:46,400 --> 11:11:47,800 return a 10304 11:11:47,800 --> 11:11:51,596 user so this is the return type this is 10305 11:11:51,596 --> 11:11:53,720 how you will mention the return type it 10306 11:11:53,720 --> 11:11:56,560 should give the user and I got the 10307 11:11:56,560 --> 11:11:58,240 parent and the 10308 11:11:58,240 --> 11:12:02,080 context this is a uh nextjs has 10309 11:12:02,080 --> 11:12:05,640 documentation on 10310 11:12:06,640 --> 11:12:10,520 graphql you can learn more about a code 10311 11:12:10,520 --> 11:12:13,200 based approach if you like the code 10312 11:12:13,200 --> 11:12:15,200 based approach you can learn more about 10313 11:12:15,200 --> 11:12:16,140 from the 10314 11:12:16,140 --> 11:12:17,360 [Music] 10315 11:12:17,360 --> 11:12:19,400 documentation this is the code first 10316 11:12:19,400 --> 11:12:21,480 approach the object types you can read 10317 11:12:21,480 --> 11:12:23,436 the documentation if you are not getting 10318 11:12:23,436 --> 11:12:26,080 what I'm talking about you can just read 10319 11:12:26,080 --> 11:12:29,480 this text it will help you to understand 10320 11:12:29,480 --> 11:12:33,800 the code very easily so this is how a 10321 11:12:33,800 --> 11:12:37,480 field and type should be post array I 10322 11:12:37,480 --> 11:12:39,800 want you to check this documentation one 10323 11:12:39,800 --> 11:12:43,160 more package I want to share you have 10324 11:12:43,160 --> 11:12:47,116 graph nestjs graphql 10325 11:12:52,400 --> 11:12:55,200 tools if you want to use the code based 10326 11:12:55,200 --> 11:12:56,720 approach 10327 11:12:56,720 --> 11:13:01,880 and let's say you are using post or type 10328 11:13:01,880 --> 11:13:05,116 orm this is the package if you want to 10329 11:13:05,116 --> 11:13:06,520 work with 10330 11:13:06,520 --> 11:13:10,960 database and and you want to use loader 10331 11:13:10,960 --> 11:13:13,436 they have already built a data loader 10332 11:13:13,436 --> 11:13:16,480 here if you like this package you can 10333 11:13:16,480 --> 11:13:19,360 implement it it's simple you need to 10334 11:13:19,360 --> 11:13:22,520 provide resolver user object type user 10335 11:13:22,520 --> 11:13:26,080 resolver here it will have graphql 10336 11:13:26,080 --> 11:13:28,200 loader you only need to just provide The 10337 11:13:28,200 --> 11:13:31,200 Decorator graphql loader and it is going 10338 11:13:31,200 --> 11:13:34,756 to implement the loading stuff 10339 11:13:34,756 --> 11:13:37,480 here if you like it you can implement it 10340 11:13:37,480 --> 11:13:39,240 there is a this is how you will 10341 11:13:39,240 --> 11:13:43,160 Implement one to many many to one and 10342 11:13:43,160 --> 11:13:46,080 this is the usage 10343 11:13:46,080 --> 11:13:47,680 I just want to show 10344 11:13:47,680 --> 11:13:51,480 you you can do if you want to use 10345 11:13:51,480 --> 11:13:54,080 database and data loader you can 10346 11:13:54,080 --> 11:13:57,200 Implement by using this package this 10347 11:13:57,200 --> 11:13:59,360 package also have more features on 10348 11:13:59,360 --> 11:14:04,160 pagination sorting and field extraction 10349 11:14:06,320 --> 11:14:09,200 Etc if you want to fetch data from 10350 11:14:09,200 --> 11:14:12,960 external rest API you can do that we're 10351 11:14:12,960 --> 11:14:15,276 using Apollo server AP poo server 10352 11:14:15,276 --> 11:14:18,436 provides a package to fetch data from 10353 11:14:18,436 --> 11:14:21,436 external rest API a 10354 11:14:21,436 --> 11:14:25,640 poloo data source rest I have attached a 10355 11:14:25,640 --> 11:14:27,200 started 10356 11:14:27,200 --> 11:14:31,720 project I have already in I have already 10357 11:14:31,720 --> 11:14:35,720 have this package in package.json file 10358 11:14:35,720 --> 11:14:38,640 you have to install it Apollo data 10359 11:14:38,640 --> 11:14:40,276 source 10360 11:14:40,276 --> 11:14:43,160 rest you have to do npm install to 10361 11:14:43,160 --> 11:14:45,840 install all the dependencies 10362 11:14:45,840 --> 11:14:49,840 this is a class you have to extend 10363 11:14:49,840 --> 11:14:52,960 it we're going to do we're going to 10364 11:14:52,960 --> 11:14:54,080 fetch 10365 11:14:54,080 --> 11:14:57,960 todos from this Json placeholder API 10366 11:14:57,960 --> 11:15:01,960 it's a fake API with real data let's say 10367 11:15:01,960 --> 11:15:04,880 I want to send to-dos 10368 11:15:04,880 --> 11:15:07,400 request it is going to give me all of 10369 11:15:07,400 --> 11:15:13,916 the to-dos I want to fetch this rest 10370 11:15:13,916 --> 11:15:15,520 API 10371 11:15:15,520 --> 11:15:19,160 now this is how you will implement 10372 11:15:19,916 --> 11:15:22,800 it you will have this schema you will 10373 11:15:22,800 --> 11:15:27,240 create the schema to do ID user ID title 10374 11:15:27,240 --> 11:15:28,800 completed with 10375 11:15:28,800 --> 11:15:31,960 Boolean I'm using the same property I 10376 11:15:31,960 --> 11:15:34,400 defined a graphql type and then we have 10377 11:15:34,400 --> 11:15:36,960 a query query to fetch all the 10378 11:15:36,960 --> 11:15:39,680 to-dos I have a to-do module inside 10379 11:15:39,680 --> 11:15:43,080 to-do service to-do resolver now to-do 10380 11:15:43,080 --> 11:15:46,560 service is is going to f fetch the data 10381 11:15:46,560 --> 11:15:51,160 from external rest API instead of data 10382 11:15:51,160 --> 11:15:54,560 database inside the to- do service here 10383 11:15:54,560 --> 11:15:57,840 you have to extend the rest data source 10384 11:15:57,840 --> 11:16:00,720 it will it will ask the call the super 10385 11:16:00,720 --> 11:16:03,640 Constructor this is the base URL I'm 10386 11:16:03,640 --> 11:16:06,320 going to call this.get to fetch all the 10387 11:16:06,320 --> 11:16:10,480 to-dos you can use this method I I can 10388 11:16:10,480 --> 11:16:13,276 get the data source object from the 10389 11:16:13,276 --> 11:16:16,560 context here you got call data sources. 10390 11:16:16,560 --> 11:16:19,480 too api. getet too I'm talking about 10391 11:16:19,480 --> 11:16:24,000 this too API like this one so another 10392 11:16:24,000 --> 11:16:27,520 important thing this is how you will add 10393 11:16:27,520 --> 11:16:30,680 a data sources inside the context it's 10394 11:16:30,680 --> 11:16:33,240 an asynchronous so I provided the data 10395 11:16:33,240 --> 11:16:36,880 sources if you want to implement you can 10396 11:16:36,880 --> 11:16:39,080 go ahead and do it and let me do that 10397 11:16:39,080 --> 11:16:41,360 for 10398 11:16:42,080 --> 11:16:45,480 you I have I have to do docraft in my 10399 11:16:45,480 --> 11:16:47,276 started kit 10400 11:16:47,276 --> 11:16:51,880 project now let's extend data 10401 11:16:51,880 --> 11:16:55,960 source rest rest data 10402 11:16:55,960 --> 11:16:59,200 source let's have the 10403 11:16:59,200 --> 11:17:02,800 Constructor I'm going to call the super 10404 11:17:02,800 --> 11:17:06,956 Constructor now let's set the base 10405 11:17:06,956 --> 11:17:11,880 URL I can get the base URL from 10406 11:17:11,916 --> 11:17:14,800 here I don't need to provide todos this 10407 11:17:14,800 --> 11:17:18,116 should be the base URL now let's define 10408 11:17:18,116 --> 11:17:21,320 a function to get 10409 11:17:22,756 --> 11:17:25,320 to-dos and it is going to return the 10410 11:17:25,320 --> 11:17:28,480 array of 10411 11:17:30,400 --> 11:17:33,956 to-dos I can get the type from the 10412 11:17:33,956 --> 11:17:37,276 source. graphql file the typescript file 10413 11:17:37,276 --> 11:17:40,480 I've generated the typing by executing 10414 11:17:40,480 --> 11:17:41,756 this 10415 11:17:41,756 --> 11:17:46,720 command npm run generat typing 10416 11:17:47,916 --> 11:17:52,480 so let's call this 10417 11:17:53,880 --> 11:17:58,436 dot here disor get and provide the path 10418 11:17:58,436 --> 11:18:00,400 to 10419 11:18:00,400 --> 11:18:03,916 do is everything right I can check the 10420 11:18:03,916 --> 11:18:06,720 path which is to 10421 11:18:07,596 --> 11:18:10,480 do so we created the data 10422 11:18:10,480 --> 11:18:14,320 source and now we have to add this data 10423 11:18:14,320 --> 11:18:16,400 source 10424 11:18:16,400 --> 11:18:20,840 into to do resolver we got to call this 10425 11:18:20,840 --> 11:18:24,480 actually we need to add in the 10426 11:18:27,360 --> 11:18:29,560 context I'm going to create the data 10427 11:18:29,560 --> 11:18:30,916 sources 10428 11:18:30,916 --> 11:18:33,880 function it's going to return a to-do 10429 11:18:33,880 --> 11:18:38,040 API let's create import to-do 10430 11:18:38,040 --> 11:18:40,800 service it's going to return the object 10431 11:18:40,800 --> 11:18:42,800 and the function is going to return the 10432 11:18:42,800 --> 11:18:47,720 object now I can add in the 10433 11:18:49,160 --> 11:18:50,916 context 10434 11:18:50,916 --> 11:18:54,880 here it looks good so you can use the 10435 11:18:54,880 --> 11:18:59,756 context object here inside the to-do 10436 11:19:00,000 --> 11:19:05,360 resolver to do resolver let me get the 10437 11:19:11,436 --> 11:19:14,436 context data sources and I'm going to 10438 11:19:14,436 --> 11:19:16,720 call all this 10439 11:19:16,720 --> 11:19:19,000 method data source is a function there 10440 11:19:19,000 --> 11:19:22,720 is a to-do API object and we have get 10441 11:19:22,720 --> 11:19:25,160 too 10442 11:19:27,840 --> 11:19:31,956 function let's start the 10443 11:19:38,040 --> 11:19:40,916 project project is already Runing on 10444 11:19:40,916 --> 11:19:44,000 3,000 let me stop the project let's run 10445 11:19:44,000 --> 11:19:46,640 it again 10446 11:19:52,520 --> 11:19:54,680 my project is running I'm going to send 10447 11:19:54,680 --> 11:19:56,916 the 10448 11:19:58,720 --> 11:20:03,200 request I got all the 200 toos 10449 11:20:03,200 --> 11:20:05,800 Unfortunately they don't have pagination 10450 11:20:05,800 --> 11:20:08,436 otherwise I can implement the pagination 10451 11:20:08,436 --> 11:20:11,480 but you got the concept how to implement 10452 11:20:11,480 --> 11:20:14,400 the rest data source if you want to do 10453 11:20:14,400 --> 11:20:17,000 more Advanced Thing by using the rest 10454 11:20:17,000 --> 11:20:20,596 data source package there is also a 10455 11:20:20,596 --> 11:20:23,200 caching functionality you can override 10456 11:20:23,200 --> 11:20:25,320 the method I want you to check the 10457 11:20:25,320 --> 11:20:27,720 documentation if you want to implement 10458 11:20:27,720 --> 11:20:31,480 the rest data source there is also a 10459 11:20:31,480 --> 11:20:34,320 data loader you can also use the data 10460 11:20:34,320 --> 11:20:37,160 loader with rest data 10461 11:20:37,160 --> 11:20:41,080 source I want you to check this 10462 11:20:41,080 --> 11:20:44,640 documentation it also provide to fetch 10463 11:20:44,640 --> 11:20:45,916 duplicate 10464 11:20:45,916 --> 11:20:48,880 records you can read the documentation 10465 11:20:48,880 --> 11:20:52,116 to understand how to use the 10466 11:20:52,116 --> 11:20:55,720 cache stuff like 10467 11:20:56,276 --> 11:20:59,200 that in this module we're going to use 10468 11:20:59,200 --> 11:21:02,320 Prisma orm and we're going to build rest 10469 11:21:02,320 --> 11:21:07,640 apis Prisma is an orm in nodejs or 10470 11:21:07,640 --> 11:21:09,436 JavaScript when you look at the 10471 11:21:09,436 --> 11:21:12,276 documentation it's they are saying it's 10472 11:21:12,276 --> 11:21:15,520 a Next Generation orm for nodejs and 10473 11:21:15,520 --> 11:21:17,000 typescript 10474 11:21:17,000 --> 11:21:19,720 project it's similar to type orm but it 10475 11:21:19,720 --> 11:21:23,200 is faster it's an efficient they are 10476 11:21:23,200 --> 11:21:25,756 maintaining this project 10477 11:21:25,756 --> 11:21:28,400 regularly so you can look at the use 10478 11:21:28,400 --> 11:21:31,596 cases and customer stories when when do 10479 11:21:31,596 --> 11:21:32,880 you need 10480 11:21:32,880 --> 11:21:36,080 Prisma you can read the customer stories 10481 11:21:36,080 --> 11:21:38,756 and use 10482 11:21:39,880 --> 11:21:42,480 cases so in this video I'm going to 10483 11:21:42,480 --> 11:21:45,360 teach you how to set a Prisma in next 10484 11:21:45,360 --> 11:21:48,520 nestjs project you can also get the 10485 11:21:48,520 --> 11:21:51,880 Prisma documentation from nestjs package 10486 11:21:51,880 --> 11:21:55,160 from nestjs package inside the njs 10487 11:21:55,160 --> 11:21:57,916 documentation you will find a Prisma 10488 11:21:57,916 --> 11:22:01,640 folder you will see the Prisma section 10489 11:22:01,640 --> 11:22:05,000 here is a Prisma orm 10490 11:22:05,520 --> 11:22:07,720 documentation the first step we need to 10491 11:22:07,720 --> 11:22:10,360 install the 10492 11:22:11,680 --> 11:22:14,276 Prisma I have provided the started kit 10493 11:22:14,276 --> 11:22:18,320 code for you you can go to the starter 10494 11:22:18,320 --> 11:22:21,800 branch and run nbm install to install 10495 11:22:21,800 --> 11:22:24,200 all the dependencies this is just a 10496 11:22:24,200 --> 11:22:26,000 basic Nest CH 10497 11:22:26,000 --> 11:22:29,200 project I don't have anything here just 10498 11:22:29,200 --> 11:22:33,756 Basics basic setting for nest CH 10499 11:22:35,200 --> 11:22:37,680 project it is taking some time to 10500 11:22:37,680 --> 11:22:40,160 install the dependencies meanwhile I'm 10501 11:22:40,160 --> 11:22:42,436 going to create a new database for 10502 11:22:42,436 --> 11:22:45,480 Prisma because we're going to use postp 10503 11:22:45,480 --> 11:22:46,360 with 10504 11:22:46,360 --> 11:22:50,520 Prisma I'm going to create a new 10505 11:22:51,640 --> 11:22:54,400 database 10506 11:22:54,400 --> 11:22:57,320 Prisma API something like that you can 10507 11:22:57,320 --> 11:22:59,756 choose any 10508 11:22:59,756 --> 11:23:04,000 name all the packages have 10509 11:23:05,560 --> 11:23:08,720 installed now we need to install a 10510 11:23:08,720 --> 11:23:11,880 Prisma as a dep 10511 11:23:11,916 --> 11:23:13,436 dependency 10512 11:23:13,436 --> 11:23:18,240 Prisma left dependency has 10513 11:23:18,276 --> 11:23:21,320 installed now we need to initialize the 10514 11:23:21,320 --> 11:23:25,400 Prisma project I can use npx init Prisma 10515 11:23:25,400 --> 11:23:26,800 in 10516 11:23:26,800 --> 11:23:30,756 it it will set up the Prisma in our 10517 11:23:30,756 --> 11:23:35,160 project you will see schema. Prisma file 10518 11:23:35,160 --> 11:23:37,640 inside the Prisma folder here I'm 10519 11:23:37,640 --> 11:23:39,756 telling I'm going to use Prisma client 10520 11:23:39,756 --> 11:23:42,400 Library this is my post database 10521 11:23:42,400 --> 11:23:44,880 provider this is my database 10522 11:23:44,880 --> 11:23:49,800 URL they have also added a created EnV 10523 11:23:49,800 --> 11:23:53,720 file here we have postp database URL 10524 11:23:53,720 --> 11:23:57,840 make sure you have ADD theb in the git 10525 11:23:57,840 --> 11:24:01,200 ignore I think I've already added it yes 10526 11:24:01,200 --> 11:24:05,040 no I cannot see that let's add EnV I 10527 11:24:05,040 --> 11:24:09,720 don't want to put push my EnV to 10528 11:24:10,480 --> 11:24:13,436 GitHub they have already installed post 10529 11:24:13,436 --> 11:24:16,680 so I'm going to work with 10530 11:24:18,400 --> 11:24:21,640 post this is how you will set a Prisma 10531 11:24:21,640 --> 11:24:24,956 in your project in your nestjs 10532 11:24:24,956 --> 11:24:28,116 project in the previous video we have 10533 11:24:28,116 --> 11:24:31,276 set up our Prisma 10534 11:24:31,276 --> 11:24:33,436 application now we're going to create 10535 11:24:33,436 --> 11:24:36,520 our first 10536 11:24:37,720 --> 11:24:40,880 model I'm going to create the song 10537 11:24:40,880 --> 11:24:46,320 model the type should be int 10538 11:24:49,800 --> 11:24:52,360 and by 10539 11:24:52,360 --> 11:24:55,160 default this should be 10540 11:24:55,160 --> 11:24:57,916 int by default I'm going to set it to 10541 11:24:57,916 --> 11:24:59,000 Auto 10542 11:24:59,000 --> 11:25:02,880 increment and this is the 10543 11:25:02,880 --> 11:25:06,640 ID let's add another property this time 10544 11:25:06,640 --> 11:25:08,030 the type should be 10545 11:25:08,030 --> 11:25:11,116 [Music] 10546 11:25:11,116 --> 11:25:14,956 string but we did not set 10547 11:25:14,956 --> 11:25:18,360 database URL for the 10548 11:25:18,360 --> 11:25:23,040 post I'm going to create a new 10549 11:25:24,000 --> 11:25:28,276 database let's create a new 10550 11:25:28,916 --> 11:25:30,640 DB 10551 11:25:30,640 --> 11:25:35,720 Nest Prisma app or Nest 10552 11:25:36,640 --> 11:25:42,040 prisma-p my username or owner is 10553 11:25:42,840 --> 11:25:46,000 postgress now we need to replace the 10554 11:25:46,000 --> 11:25:48,320 owner here 10555 11:25:48,320 --> 11:25:50,916 postgress and your 10556 11:25:50,916 --> 11:25:54,480 password and the name of your database 10557 11:25:54,480 --> 11:25:58,240 and schema should be 10558 11:25:58,916 --> 11:26:02,956 public I'm going to run the 10559 11:26:04,840 --> 11:26:08,520 application my application is running 10560 11:26:08,520 --> 11:26:12,560 fine let's try to check the tables 10561 11:26:12,560 --> 11:26:16,720 inside the schema I'm going to 10562 11:26:16,720 --> 11:26:20,240 refresh and inside the tables I cannot 10563 11:26:20,240 --> 11:26:25,116 see any table because we did not migrate 10564 11:26:25,116 --> 11:26:25,880 the 10565 11:26:25,880 --> 11:26:29,800 database let's create our first 10566 11:26:29,800 --> 11:26:33,520 migration migrate 10567 11:26:33,520 --> 11:26:37,480 F Prisma migr F and the name of your 10568 11:26:37,480 --> 11:26:40,560 migration I can say in it this is my 10569 11:26:40,560 --> 11:26:43,040 first 10570 11:26:43,160 --> 11:26:46,160 migration 10571 11:26:48,520 --> 11:26:50,240 I think I did not provide the right 10572 11:26:50,240 --> 11:26:52,480 command Prisma 10573 11:26:52,480 --> 11:26:54,720 migr 10574 11:26:54,720 --> 11:27:00,000 DB or Dev yeah npx PR migrate 10575 11:27:00,000 --> 11:27:04,276 Dev Das Das name of your 10576 11:27:04,276 --> 11:27:07,116 migration this is my first migration 10577 11:27:07,116 --> 11:27:09,756 that's why I chose the init name but you 10578 11:27:09,756 --> 11:27:13,116 can choose anything 10579 11:27:13,116 --> 11:27:15,756 here 10580 11:27:15,756 --> 11:27:19,640 Local Host provided DB credentials are 10581 11:27:19,640 --> 11:27:21,880 not 10582 11:27:21,880 --> 11:27:24,480 valid post 10583 11:27:24,480 --> 11:27:28,596 SC there was an issue with double 10584 11:27:28,596 --> 11:27:32,840 s now a new migrations folder has 10585 11:27:32,840 --> 11:27:35,116 created inside the 10586 11:27:35,116 --> 11:27:39,040 folder we have in it migration 10587 11:27:39,040 --> 11:27:43,200 file we have song table this is the 10588 11:27:43,200 --> 11:27:46,116 migration log 10589 11:27:46,880 --> 11:27:50,480 let's try to check in our database I'm 10590 11:27:50,480 --> 11:27:52,400 going to refresh my 10591 11:27:52,400 --> 11:27:56,880 tables and you can see the song table in 10592 11:27:56,880 --> 11:27:58,400 our 10593 11:27:58,400 --> 11:28:02,480 database but we also have two columns ID 10594 11:28:02,480 --> 11:28:04,880 and 10595 11:28:08,916 --> 11:28:11,680 title in the previous video we have 10596 11:28:11,680 --> 11:28:15,596 created our first model and we ran 10597 11:28:15,596 --> 11:28:19,000 migrations but Prisma also provides a 10598 11:28:19,000 --> 11:28:22,480 Prisma client to perform cred operation 10599 11:28:22,480 --> 11:28:25,116 this Prisma client will generate the 10600 11:28:25,116 --> 11:28:26,200 correct 10601 11:28:26,200 --> 11:28:30,800 apis correct functions for song model 10602 11:28:30,800 --> 11:28:34,320 you will have prism client. creat song 10603 11:28:34,320 --> 11:28:37,276 prism client. find song all of these 10604 11:28:37,276 --> 11:28:40,720 methods in the prisman client Library 10605 11:28:40,720 --> 11:28:43,116 here we are using the prisman client but 10606 11:28:43,116 --> 11:28:45,680 we have to install this package Prisma 10607 11:28:45,680 --> 11:28:50,800 client and P install at Prisma 10608 11:28:50,800 --> 11:28:53,720 client this package will provide scrud 10609 11:28:53,720 --> 11:28:56,916 operations for all of your 10610 11:28:56,916 --> 11:28:59,800 models this Prisma client will interact 10611 11:28:59,800 --> 11:29:01,916 directly to the 10612 11:29:01,916 --> 11:29:04,800 database so we created the Prisma Cent 10613 11:29:04,800 --> 11:29:08,436 installed the Prisma client now we need 10614 11:29:08,436 --> 11:29:10,840 a service inside the source directory 10615 11:29:10,840 --> 11:29:14,840 and I call it Prisma dos service. 10616 11:29:14,840 --> 11:29:19,436 s it's injectable 10617 11:29:20,320 --> 11:29:22,116 service 10618 11:29:22,116 --> 11:29:25,596 exort class Prisma 10619 11:29:25,596 --> 11:29:28,560 service and I'm going to extend the 10620 11:29:28,560 --> 11:29:31,116 Prisma 10621 11:29:33,400 --> 11:29:37,956 client Prisma client 10622 11:29:42,200 --> 11:29:45,240 here we also need to 10623 11:29:45,240 --> 11:29:48,756 implement on module in it because we 10624 11:29:48,756 --> 11:29:51,956 have to set up the Prisma client when 10625 11:29:51,956 --> 11:29:54,840 our application is 10626 11:29:54,840 --> 11:29:58,200 running I can call it on module in 10627 11:29:58,200 --> 11:30:02,480 it on module in it this 10628 11:30:02,480 --> 11:30:04,360 do 10629 11:30:04,360 --> 11:30:10,116 connect this connect method will connect 10630 11:30:10,116 --> 11:30:13,360 our uh connect this Prisma client to our 10631 11:30:13,360 --> 11:30:15,916 database 10632 11:30:16,040 --> 11:30:18,880 I have I have not imported injectable 10633 11:30:18,880 --> 11:30:22,720 decorator let me get it that's all you 10634 11:30:22,720 --> 11:30:27,000 need to connect Prisma client to 10635 11:30:27,000 --> 11:30:28,720 our 10636 11:30:28,720 --> 11:30:33,000 database so we will not use Prisma 10637 11:30:33,000 --> 11:30:36,116 client we will use Prisma service to 10638 11:30:36,116 --> 11:30:39,596 interact with 10639 11:30:42,916 --> 11:30:45,520 database we're going to talk about how 10640 11:30:45,520 --> 11:30:48,720 to create cred 10641 11:30:53,400 --> 11:30:58,436 operations we have created a song model 10642 11:30:58,436 --> 11:31:02,000 here I want to create the correct end 10643 11:31:02,000 --> 11:31:05,276 points one more thing I did not talk 10644 11:31:05,276 --> 11:31:07,596 about or I did not teach there is a 10645 11:31:07,596 --> 11:31:10,240 resource 10646 11:31:12,840 --> 11:31:17,596 generator this CLI command will generate 10647 11:31:17,596 --> 11:31:21,840 the correct API endpoints controller 10648 11:31:21,840 --> 11:31:27,560 module service dto object something like 10649 11:31:27,560 --> 11:31:31,240 that if you need to scaold your 10650 11:31:31,240 --> 11:31:36,360 application you can use this command 10651 11:31:42,840 --> 11:31:45,840 resource 10652 11:31:46,880 --> 11:31:50,840 it can also works with work with graphql 10653 11:31:50,840 --> 11:31:53,596 code first approach schema first 10654 11:31:53,596 --> 11:31:56,436 approach or you can create a micro 10655 11:31:56,436 --> 11:32:00,276 service or you can create the web socket 10656 11:32:00,276 --> 11:32:03,756 but I chose the rest API because I would 10657 11:32:03,756 --> 11:32:07,080 like to build the rest API yes I also 10658 11:32:07,080 --> 11:32:10,680 want to generate correct entry end 10659 11:32:10,680 --> 11:32:14,276 points we have the songs model we have d 10660 11:32:14,276 --> 11:32:18,956 object entities so on 10661 11:32:19,160 --> 11:32:21,200 controller 10662 11:32:21,200 --> 11:32:23,800 service we have all of these method 10663 11:32:23,800 --> 11:32:25,276 inside the 10664 11:32:25,276 --> 11:32:28,400 service we have created a Prisma service 10665 11:32:28,400 --> 11:32:30,956 now I'm going to use a Prisma service 10666 11:32:30,956 --> 11:32:34,640 here to interact with database can call 10667 11:32:34,640 --> 11:32:35,880 it 10668 11:32:35,880 --> 11:32:38,240 Prisma Prisma 10669 11:32:38,240 --> 11:32:41,880 service it is going to give me the 10670 11:32:41,880 --> 11:32:46,080 error that's why I use this 10671 11:32:50,240 --> 11:32:54,320 path first of all let's create the 10672 11:32:54,320 --> 11:32:56,640 create 10673 11:32:56,640 --> 11:33:00,436 method return this do 10674 11:33:00,436 --> 11:33:02,916 Prisma 10675 11:33:02,916 --> 11:33:07,956 Dot Song you can see we have a song 10676 11:33:07,956 --> 11:33:11,116 object there is a create method and you 10677 11:33:11,116 --> 11:33:14,200 have to provide the data here you have 10678 11:33:14,200 --> 11:33:19,436 to provide create song 10679 11:33:22,116 --> 11:33:25,320 dto one more thing I want to talk about 10680 11:33:25,320 --> 11:33:29,436 you can use the types from Prisma client 10681 11:33:29,436 --> 11:33:31,800 Prisma client already generated couple 10682 11:33:31,800 --> 11:33:34,916 of typescript typing let's 10683 11:33:34,916 --> 11:33:38,276 say first I need to import 10684 11:33:38,276 --> 11:33:42,160 Prisma from Prisma 10685 11:33:42,720 --> 11:33:45,720 client 10686 11:33:48,400 --> 11:33:54,200 you will find book create input the 10687 11:33:54,200 --> 11:33:59,436 type in this type you will have title 10688 11:33:59,956 --> 11:34:03,480 field we also need to update inside the 10689 11:34:03,480 --> 11:34:06,040 song 10690 11:34:08,000 --> 11:34:12,436 controller we got to get Prisma client 10691 11:34:12,436 --> 11:34:16,200 and you will have book 10692 11:34:16,200 --> 11:34:18,756 DOT 10693 11:34:18,756 --> 11:34:21,756 book create 10694 11:34:21,756 --> 11:34:25,320 input my vs code is not giving me the 10695 11:34:25,320 --> 11:34:28,080 hint or 10696 11:34:40,880 --> 11:34:43,080 suggestion now we're going to run the 10697 11:34:43,080 --> 11:34:44,800 application it is going to give me a 10698 11:34:44,800 --> 11:34:47,520 couple of Errors 10699 11:34:47,520 --> 11:34:50,680 maybe oh it's not a book it should be a 10700 11:34:50,680 --> 11:34:54,720 song because I'm accessing the song 10701 11:34:54,720 --> 11:34:57,276 model I'm using the 10702 11:34:57,276 --> 11:34:59,240 book 10703 11:34:59,240 --> 11:35:03,320 song song. 10704 11:35:05,000 --> 11:35:10,200 input not a book song create 10705 11:35:12,640 --> 11:35:15,640 input 10706 11:35:27,640 --> 11:35:31,560 I did not import Prisma service inside 10707 11:35:31,560 --> 11:35:34,160 the song 10708 11:35:34,640 --> 11:35:37,436 module because we have to import it 10709 11:35:37,436 --> 11:35:42,520 that's why you have to register it as a 10710 11:35:42,520 --> 11:35:44,320 provider 10711 11:35:44,320 --> 11:35:49,116 Prisma service so I can get the Prisma 10712 11:36:01,720 --> 11:36:05,640 service my application is running fine 10713 11:36:05,640 --> 11:36:08,276 now now we're going to test it with the 10714 11:36:08,276 --> 11:36:11,956 help of fresh client let's create a new 10715 11:36:11,956 --> 11:36:14,880 file I can say HTTP 10716 11:36:14,880 --> 11:36:17,880 client. 10717 11:36:18,040 --> 11:36:21,560 HTTP create 10718 11:36:23,116 --> 11:36:25,480 song post 10719 11:36:25,480 --> 11:36:29,080 HTTP HTTP Local Host colum 10720 11:36:29,080 --> 11:36:31,596 3000 SL 10721 11:36:31,596 --> 11:36:35,240 songs what is the name of the API I can 10722 11:36:35,240 --> 11:36:36,916 check it from the 10723 11:36:36,916 --> 11:36:39,680 controller we have the 10724 11:36:39,680 --> 11:36:41,956 songs content 10725 11:36:41,956 --> 11:36:45,116 type application 10726 11:36:45,116 --> 11:36:49,116 Json let's provide the 10727 11:36:49,240 --> 11:36:52,720 title follow 10728 11:36:55,080 --> 11:37:00,640 me you can see a new song has created 10729 11:37:00,640 --> 11:37:03,200 successfully now we're going to write 10730 11:37:03,200 --> 11:37:07,200 the function to fetch all the song This 10731 11:37:07,200 --> 11:37:09,560 start 10732 11:37:12,436 --> 11:37:15,436 Prisma 10733 11:37:16,160 --> 11:37:18,756 I'm going to reload my window it is not 10734 11:37:18,756 --> 11:37:21,320 giving me 10735 11:37:24,756 --> 11:37:28,520 suggestions Prisma 10736 11:37:28,880 --> 11:37:31,880 Dot 10737 11:37:31,956 --> 11:37:35,680 Song dot find many there is a method 10738 11:37:35,680 --> 11:37:38,040 find 10739 11:37:42,436 --> 11:37:45,436 many 10740 11:37:45,880 --> 11:37:49,916 let's try to get all the 10741 11:38:04,360 --> 11:38:08,560 songs you can see I have a single 10742 11:38:08,560 --> 11:38:11,840 song now we have to implement the find 10743 11:38:11,840 --> 11:38:14,520 by ID this do 10744 11:38:14,520 --> 11:38:19,040 Prisma do song now it is giving me 10745 11:38:19,040 --> 11:38:21,840 suggestion find 10746 11:38:21,840 --> 11:38:26,436 unique and we have to provide the 10747 11:38:33,360 --> 11:38:36,320 ID where clause and you have to provide 10748 11:38:36,320 --> 11:38:38,520 the 10749 11:38:42,360 --> 11:38:44,000 ID 10750 11:38:44,000 --> 11:38:46,520 let's try to fetch the song on the based 10751 11:38:46,520 --> 11:38:47,756 on 10752 11:38:47,756 --> 11:38:52,596 ID fetch song by 10753 11:38:53,320 --> 11:38:56,800 ID so I got the song by ID if I provide 10754 11:38:56,800 --> 11:38:59,596 the invalid 10755 11:38:59,596 --> 11:39:03,800 ID I should get the empty 10756 11:39:07,436 --> 11:39:10,840 record I'm getting issues here I think I 10757 11:39:10,840 --> 11:39:13,720 need to generate the typing you can call 10758 11:39:13,720 --> 11:39:17,720 this method npx Prisma 10759 11:39:17,720 --> 11:39:20,116 generate it is going to generate the 10760 11:39:20,116 --> 11:39:25,000 typing inside the node node modules and 10761 11:39:25,000 --> 11:39:27,200 Prisma inside the Prisma you will get 10762 11:39:27,200 --> 11:39:29,916 the typing I got this from 10763 11:39:29,916 --> 11:39:32,840 documentation this typing will be stored 10764 11:39:32,840 --> 11:39:35,360 inside the node modules inside the 10765 11:39:35,360 --> 11:39:38,680 Prisma and the client 10766 11:39:40,956 --> 11:39:45,276 directory generated Prisma client 10767 11:39:45,276 --> 11:39:48,200 reload the 10768 11:39:53,276 --> 11:39:57,840 window now the error should be gone 10769 11:39:59,160 --> 11:40:02,560 now I have to update it it's a song 10770 11:40:02,560 --> 11:40:05,360 create 10771 11:40:05,720 --> 11:40:08,680 input we also need to update in the 10772 11:40:08,680 --> 11:40:12,240 controller we have the song 10773 11:40:12,240 --> 11:40:13,880 controller 10774 11:40:13,880 --> 11:40:17,756 here it should be 10775 11:40:18,956 --> 11:40:21,320 Prisma Prisma 10776 11:40:21,320 --> 11:40:24,956 SL song create 10777 11:40:24,956 --> 11:40:30,640 input I have to get this ke Prisma 10778 11:40:34,080 --> 11:40:37,436 client now the error should be gone now 10779 11:40:37,436 --> 11:40:40,080 it looks 10780 11:40:42,240 --> 11:40:43,756 good 10781 11:40:43,756 --> 11:40:47,956 so I can create a new 10782 11:40:49,916 --> 11:40:53,756 song and I can fetch all the songs that 10783 11:40:53,756 --> 11:40:56,160 looks 10784 11:40:57,840 --> 11:41:00,916 good in this lesson I'm going to teach 10785 11:41:00,916 --> 11:41:05,276 you how to create update endpoint 10786 11:41:05,276 --> 11:41:07,480 actually we have already created the 10787 11:41:07,480 --> 11:41:09,200 update 10788 11:41:09,200 --> 11:41:13,276 endpoint you will see the patch endpoint 10789 11:41:13,276 --> 11:41:16,840 Point inside the song 10790 11:41:21,276 --> 11:41:24,956 controller there is also a method inside 10791 11:41:24,956 --> 11:41:29,360 the song service there's an update 10792 11:41:29,360 --> 11:41:34,320 method all I need to do is to use a 10793 11:41:34,320 --> 11:41:38,916 Prisma service inside the song 10794 11:41:41,916 --> 11:41:46,000 service we have have to use the update 10795 11:41:46,000 --> 11:41:48,436 method from the 10796 11:41:48,436 --> 11:41:52,116 Prisma and there is an update method 10797 11:41:52,116 --> 11:41:53,956 inside the 10798 11:41:53,956 --> 11:41:56,956 song 10799 11:41:56,956 --> 11:41:59,916 update here you got to 10800 11:41:59,916 --> 11:42:04,720 provide we clause and the 10801 11:42:10,720 --> 11:42:15,116 data where and 10802 11:42:20,160 --> 11:42:23,116 data we have to fix a little bit 10803 11:42:23,116 --> 11:42:24,480 changing 10804 11:42:24,480 --> 11:42:28,276 here I'm getting I'm going to get we 10805 11:42:28,276 --> 11:42:33,000 clause and I can get the type for for Vi 10806 11:42:33,000 --> 11:42:38,560 song We unique input similarly I would 10807 11:42:38,560 --> 11:42:44,040 like to change the type here Prisma do 10808 11:42:44,040 --> 11:42:47,880 song update 10809 11:42:51,436 --> 11:42:54,880 input you can remove the V because both 10810 11:42:54,880 --> 11:42:58,000 the key and the value are same so I can 10811 11:42:58,000 --> 11:43:00,320 use a single 10812 11:43:00,320 --> 11:43:05,320 V we also need to update the type in 10813 11:43:05,320 --> 11:43:10,916 controller file let me update the type 10814 11:43:12,040 --> 11:43:13,800 here 10815 11:43:13,800 --> 11:43:16,200 here I would like to send the object 10816 11:43:16,200 --> 11:43:20,520 instead of single parameter which is 10817 11:43:20,520 --> 11:43:25,480 ID here I can change the type Prisma do 10818 11:43:25,480 --> 11:43:29,080 song update 10819 11:43:35,916 --> 11:43:39,520 input we also need to update the find 10820 11:43:39,520 --> 11:43:42,040 one a little 10821 11:43:42,040 --> 11:43:45,040 bit 10822 11:43:45,956 --> 11:43:50,560 I would like to get the songw 10823 11:43:50,560 --> 11:43:55,840 unique input and let me get the 10824 11:43:55,840 --> 11:43:57,840 songw 10825 11:43:57,840 --> 11:44:00,840 unique 10826 11:44:01,200 --> 11:44:03,596 input and I'm going to 10827 11:44:03,596 --> 11:44:08,756 provide song wear unique 10828 11:44:12,040 --> 11:44:15,040 input 10829 11:44:21,276 --> 11:44:22,956 we also need to 10830 11:44:22,956 --> 11:44:27,436 update controller for the find 10831 11:44:27,436 --> 11:44:33,200 one let me send the ID in the 10832 11:44:34,040 --> 11:44:37,800 object because I'm receiving the object 10833 11:44:37,800 --> 11:44:39,360 here 10834 11:44:39,360 --> 11:44:42,360 here if you want to look at all of these 10835 11:44:42,360 --> 11:44:45,400 types where these types have 10836 11:44:45,400 --> 11:44:48,916 stored when you go to the node modules 10837 11:44:48,916 --> 11:44:51,680 you will find there is a package 10838 11:44:51,680 --> 11:44:54,756 Prisma do Prisma 10839 11:44:54,756 --> 11:44:58,080 client you will have all of your types 10840 11:44:58,080 --> 11:45:01,276 in index. D.S 10841 11:45:01,276 --> 11:45:05,640 file so I can get song whereare input 10842 11:45:05,640 --> 11:45:08,840 you can see we have a song We 10843 11:45:08,840 --> 11:45:11,756 input can say 10844 11:45:11,756 --> 11:45:15,116 type here here we have a song We input 10845 11:45:15,116 --> 11:45:19,080 you can have ID and the 10846 11:45:19,080 --> 11:45:22,400 title here we have the song where unique 10847 11:45:22,400 --> 11:45:25,956 input you have you can have ID and or 10848 11:45:25,956 --> 11:45:28,720 note in the 10849 11:45:39,000 --> 11:45:45,276 title now we have to test it run run the 10850 11:45:55,400 --> 11:45:58,680 application cannot find 10851 11:45:58,680 --> 11:46:01,436 primsa let me fix 10852 11:46:01,436 --> 11:46:06,080 that there is an issue 10853 11:46:11,840 --> 11:46:14,840 Prisma 10854 11:46:15,400 --> 11:46:19,000 now my code looks 10855 11:46:20,200 --> 11:46:22,720 good I'm going to send the update 10856 11:46:22,720 --> 11:46:28,080 request from my rest client from my HTTP 10857 11:46:30,880 --> 11:46:34,680 client the song by ID we have to send 10858 11:46:34,680 --> 11:46:38,436 the patch request instead of 10859 11:46:41,596 --> 11:46:46,320 put and going to update this song with 10860 11:46:46,320 --> 11:46:50,400 id2 copy the content 10861 11:46:54,756 --> 11:46:58,400 type dancing 10862 11:47:01,360 --> 11:47:06,040 feet let's see a song has updated 10863 11:47:06,040 --> 11:47:08,880 successfully if I fetch the song on the 10864 11:47:08,880 --> 11:47:11,916 based on ID it looks good we have 10865 11:47:11,916 --> 11:47:13,756 dancing feed 10866 11:47:13,756 --> 11:47:18,080 let's create another 10867 11:47:18,160 --> 11:47:21,160 song I have created another song I would 10868 11:47:21,160 --> 11:47:24,240 like to update the 10869 11:47:27,200 --> 11:47:30,320 title my title has updated so you can 10870 11:47:30,320 --> 11:47:33,240 fetch all of the 10871 11:47:33,240 --> 11:47:36,956 songs it looks 10872 11:47:38,116 --> 11:47:41,480 good we have implemented the update 10873 11:47:41,480 --> 11:47:43,956 operation now now we have to implement 10874 11:47:43,956 --> 11:47:45,756 delete 10875 11:47:45,756 --> 11:47:48,956 operation I'm going to use the same 10876 11:47:48,956 --> 11:47:51,956 strategy to delete let me get the ver 10877 11:47:51,956 --> 11:47:53,840 clause in the 10878 11:47:53,840 --> 11:47:58,560 input there is a method in the 10879 11:47:58,560 --> 11:48:03,400 Prisma Prisma do song. delete it is 10880 11:48:03,400 --> 11:48:07,080 going to accept the V 10881 11:48:09,200 --> 11:48:13,200 Clause now I have to get it in the 10882 11:48:13,200 --> 11:48:16,320 controller we need to pass 10883 11:48:16,320 --> 11:48:19,400 that inside the song 10884 11:48:19,400 --> 11:48:22,640 controller here I have to provide the ID 10885 11:48:22,640 --> 11:48:24,480 inside the 10886 11:48:24,480 --> 11:48:27,480 object 10887 11:48:41,720 --> 11:48:44,596 ID 10888 11:48:44,596 --> 11:48:46,840 let's try to test it with the help of 10889 11:48:46,840 --> 11:48:49,800 HTTP 10890 11:48:54,640 --> 11:48:58,360 client I can say delete song by 10891 11:48:58,360 --> 11:49:01,276 ID I'm going to provide the ID instead 10892 11:49:01,276 --> 11:49:04,360 of patch we need to use the delete 10893 11:49:04,360 --> 11:49:07,000 operation you can see this song has 10894 11:49:07,000 --> 11:49:09,640 deleted I can verify by fetching all of 10895 11:49:09,640 --> 11:49:10,480 the 10896 11:49:10,480 --> 11:49:13,080 songs you can see we have only only two 10897 11:49:13,080 --> 11:49:15,956 songs right 10898 11:49:22,840 --> 11:49:25,160 now now we're going to build 10899 11:49:25,160 --> 11:49:27,756 relationship between 10900 11:49:27,756 --> 11:49:31,400 models Prisma also provides one to one 10901 11:49:31,400 --> 11:49:34,956 relation one to many relation and many 10902 11:49:34,956 --> 11:49:37,116 to one 10903 11:49:37,116 --> 11:49:40,640 relation and also have many to many 10904 11:49:40,640 --> 11:49:44,880 relation I'm going to add a new model 10905 11:49:44,880 --> 11:49:49,560 artist let's add Fields inside the 10906 11:49:49,560 --> 11:49:53,116 artist I will have the ID and the name 10907 11:49:53,116 --> 11:49:55,480 of the 10908 11:50:03,916 --> 11:50:08,080 artist now each 10909 11:50:08,080 --> 11:50:11,520 song an 10910 11:50:11,640 --> 11:50:13,800 artist 10911 11:50:13,800 --> 11:50:16,880 I'm going to add one to M relationship 10912 11:50:16,880 --> 11:50:20,040 between artist and 10913 11:50:23,800 --> 11:50:28,520 songs each artist can publish many 10914 11:50:28,520 --> 11:50:32,596 songs I saved the application you can 10915 11:50:32,596 --> 11:50:36,276 see it has automatically added a 10916 11:50:36,276 --> 11:50:40,160 relation let's rename a small a to a 10917 11:50:40,160 --> 11:50:44,560 capital A to small a 10918 11:50:46,276 --> 11:50:49,040 here you can see there is a reference 10919 11:50:49,040 --> 11:50:50,720 here 10920 11:50:50,720 --> 11:50:55,080 artist and fields which is artist ID and 10921 11:50:55,080 --> 11:50:58,276 the reference with the 10922 11:50:58,276 --> 11:51:02,880 ID now we have to run the migrations npm 10923 11:51:02,880 --> 11:51:05,116 run 10924 11:51:05,116 --> 11:51:09,640 migrations oh I have to run 10925 11:51:09,640 --> 11:51:13,116 npx Prisma migrate 10926 11:51:13,116 --> 11:51:15,400 Dev the name of the 10927 11:51:15,400 --> 11:51:19,756 migration which is ADD 10928 11:51:33,680 --> 11:51:36,840 artist it has also generated the Prisma 10929 11:51:36,840 --> 11:51:41,480 client queries or Prisma client 10930 11:51:41,520 --> 11:51:44,956 types when you see the migrations you 10931 11:51:44,956 --> 11:51:46,000 will 10932 11:51:46,000 --> 11:51:50,320 have the create table cury it is going 10933 11:51:50,320 --> 11:51:53,276 to create the artist table it has also 10934 11:51:53,276 --> 11:51:56,000 added a relationship between song 10935 11:51:56,000 --> 11:51:58,400 relationship between songs 10936 11:51:58,400 --> 11:52:02,160 model let me try to check the types you 10937 11:52:02,160 --> 11:52:07,320 can check it from Prisma client index. D 10938 11:52:07,320 --> 11:52:10,880 dots do I have the artist type yes let 10939 11:52:10,880 --> 11:52:16,436 me find artist create input yep it has 10940 11:52:16,436 --> 11:52:20,640 generated the types for 10941 11:52:21,240 --> 11:52:25,756 me now I'm going to generate a new 10942 11:52:25,756 --> 11:52:29,276 resource for 10943 11:52:34,000 --> 11:52:37,880 artist an artist resource has created I 10944 11:52:37,880 --> 11:52:41,436 don't need entities I'm going to delete 10945 11:52:41,436 --> 11:52:43,240 them 10946 11:52:43,240 --> 11:52:47,080 here I also don't need entities in this 10947 11:52:47,080 --> 11:52:50,560 song because we are defining our models 10948 11:52:50,560 --> 11:52:53,680 in the schema. Prisma 10949 11:52:53,680 --> 11:52:57,080 file let's create the 10950 11:52:57,080 --> 11:53:01,720 artist we have to open the r service and 10951 11:53:01,720 --> 11:53:03,276 here we need to 10952 11:53:03,276 --> 11:53:07,640 inject the artist the Prisma 10953 11:53:07,640 --> 11:53:11,276 service I can get it from the song 10954 11:53:11,276 --> 11:53:14,240 service which we have to import these 10955 11:53:14,240 --> 11:53:17,756 two dependency Prisma service and the 10956 11:53:17,756 --> 11:53:19,400 Prisma 10957 11:53:19,400 --> 11:53:23,360 client I'm going to copy the 10958 11:53:24,040 --> 11:53:28,360 Constructor here we have to 10959 11:53:28,916 --> 11:53:34,080 find the Prisma create 10960 11:53:35,080 --> 11:53:39,000 input command F to to 10961 11:53:39,000 --> 11:53:41,360 find artist 10962 11:53:41,360 --> 11:53:44,360 create 10963 11:53:44,520 --> 11:53:47,756 input yes I would like to use that type 10964 11:53:47,756 --> 11:53:50,320 I can get it from the Prisma 10965 11:53:50,320 --> 11:53:52,480 client 10966 11:53:52,480 --> 11:53:58,756 Artist Artist create input that's it now 10967 11:53:58,756 --> 11:54:00,040 I can 10968 11:54:00,040 --> 11:54:03,040 use Prisma do 10969 11:54:03,040 --> 11:54:05,040 artist do 10970 11:54:05,040 --> 11:54:07,880 create here we have to provide the data 10971 11:54:07,880 --> 11:54:11,320 object and inside the data I can 10972 11:54:11,320 --> 11:54:13,640 provide create 10973 11:54:13,640 --> 11:54:16,640 artist 10974 11:54:18,520 --> 11:54:22,400 dto one more thing you need to add 10975 11:54:22,400 --> 11:54:25,800 Prisma service in the artist 10976 11:54:25,800 --> 11:54:31,160 module we have to register it Prisma 10977 11:54:36,680 --> 11:54:39,720 service let me import Prisma 10978 11:54:39,720 --> 11:54:45,240 service and here I need to add Prisma 10979 11:54:46,040 --> 11:54:50,480 service we also need to adjust a type in 10980 11:54:50,480 --> 11:54:53,720 the controller for artist controller we 10981 11:54:53,720 --> 11:54:56,640 have to update our 10982 11:54:57,000 --> 11:55:01,400 type I have to use the same 10983 11:55:01,956 --> 11:55:06,916 type Prisma dot artist create we have to 10984 11:55:06,916 --> 11:55:09,880 import a 10985 11:55:09,880 --> 11:55:13,040 Prisma otherwise it will give you the 10986 11:55:13,040 --> 11:55:15,400 error I'm not going to use the create 10987 11:55:15,400 --> 11:55:16,756 artist 10988 11:55:16,756 --> 11:55:20,320 dto so the artist create input can take 10989 11:55:20,320 --> 11:55:22,880 the name of the artist if you want to 10990 11:55:22,880 --> 11:55:24,956 inject the multiple songs you can also 10991 11:55:24,956 --> 11:55:27,040 do that you can look up the 10992 11:55:27,040 --> 11:55:29,200 documentation of Prisma how can you 10993 11:55:29,200 --> 11:55:31,436 insert multiple 10994 11:55:31,436 --> 11:55:34,400 records if you want to do in this way 10995 11:55:34,400 --> 11:55:37,160 you can also do that artist unchecked 10996 11:55:37,160 --> 11:55:39,560 create input but I'm going to use this 10997 11:55:39,560 --> 11:55:45,520 one simple one npm R start 10998 11:55:54,240 --> 11:55:57,800 Dev my application is running fine now 10999 11:55:57,800 --> 11:56:01,800 let me open the HTT HTTP client let's 11000 11:56:01,800 --> 11:56:03,436 send the 11001 11:56:03,436 --> 11:56:07,800 request to create new 11002 11:56:10,756 --> 11:56:14,240 artist create 11003 11:56:15,520 --> 11:56:17,680 Artist 11004 11:56:17,680 --> 11:56:21,080 Artist here I can provide the name of 11005 11:56:21,080 --> 11:56:22,160 the 11006 11:56:22,160 --> 11:56:26,840 artist can say Martin 11007 11:56:28,720 --> 11:56:31,880 Garrick a new record has created 11008 11:56:31,880 --> 11:56:35,680 successfully the artist record has saved 11009 11:56:35,680 --> 11:56:39,520 let's add a new 11010 11:56:39,756 --> 11:56:44,160 artist V 11011 11:56:46,520 --> 11:56:49,956 another artist has 11012 11:56:50,640 --> 11:56:53,276 created we have made one too many 11013 11:56:53,276 --> 11:56:56,680 relationship between artist and songs 11014 11:56:56,680 --> 11:56:59,756 when you add a new song you can add a 11015 11:56:59,756 --> 11:57:02,880 relationship for the artist here when 11016 11:57:02,880 --> 11:57:06,200 you check the schema. Prisma file you 11017 11:57:06,200 --> 11:57:08,596 will have the artist ID in the song 11018 11:57:08,596 --> 11:57:11,596 model that's why we need this is how you 11019 11:57:11,596 --> 11:57:13,720 can build a relationship between song 11020 11:57:13,720 --> 11:57:16,800 and the artist so what I can do inside 11021 11:57:16,800 --> 11:57:19,360 the song 11022 11:57:21,720 --> 11:57:24,916 service we have to 11023 11:57:24,916 --> 11:57:30,520 expect the song actually author ID I can 11024 11:57:30,520 --> 11:57:31,560 find 11025 11:57:31,560 --> 11:57:34,680 that song 11026 11:57:34,680 --> 11:57:37,680 create 11027 11:57:38,200 --> 11:57:43,360 input so I have this song create input 11028 11:57:43,360 --> 11:57:47,916 type I also have song unchecked create 11029 11:57:47,916 --> 11:57:50,360 input did you notice the difference 11030 11:57:50,360 --> 11:57:54,080 between these two types here we have the 11031 11:57:54,080 --> 11:57:57,720 artist ID I can add the artist ID if you 11032 11:57:57,720 --> 11:58:00,680 want to insert a complete artist while 11033 11:58:00,680 --> 11:58:04,320 creating a new song you can also do that 11034 11:58:04,320 --> 11:58:06,360 you can look up the 11035 11:58:06,360 --> 11:58:09,276 documentation and here you 11036 11:58:09,276 --> 11:58:13,520 can you can do in this way 11037 11:58:13,520 --> 11:58:15,400 you can provide the array if you want to 11038 11:58:15,400 --> 11:58:17,240 do that if you want to insert a single 11039 11:58:17,240 --> 11:58:20,240 record you can also do that but I just 11040 11:58:20,240 --> 11:58:22,756 want to show you the way to 11041 11:58:22,756 --> 11:58:25,756 add 11042 11:58:27,276 --> 11:58:31,000 record so I'm going to use this 11043 11:58:31,000 --> 11:58:34,160 type instead of 11044 11:58:34,160 --> 11:58:37,680 simple song create 11045 11:58:37,680 --> 11:58:41,240 input now you also need to provide the 11046 11:58:41,240 --> 11:58:45,840 author ID in the in the request 11047 11:58:45,840 --> 11:58:50,200 parameter here I'm creating a new song 11048 11:58:50,200 --> 11:58:54,880 here let's provide the author 11049 11:58:54,916 --> 11:58:58,560 ID which is one I'm going to create a 11050 11:58:58,560 --> 11:59:02,040 new song let's say 11051 11:59:02,040 --> 11:59:05,800 Animals by Martin 11052 11:59:07,080 --> 11:59:12,400 gck I got an issue not an author ID it's 11053 11:59:12,400 --> 11:59:14,480 an artist 11054 11:59:14,480 --> 11:59:18,276 ID why I'm saying the author ID I got 11055 11:59:18,276 --> 11:59:23,116 the record with this author ID if you 11056 11:59:23,116 --> 11:59:24,320 want to 11057 11:59:24,320 --> 11:59:27,720 query the artist in 11058 11:59:27,720 --> 11:59:30,276 the response let's say I'm going to I'm 11059 11:59:30,276 --> 11:59:33,956 going to fetch all these songs but here 11060 11:59:33,956 --> 11:59:36,680 we have artist ID null here we have the 11061 11:59:36,680 --> 11:59:39,680 artist ID one if you want to populate 11062 11:59:39,680 --> 11:59:42,480 the record of the artist you you can do 11063 11:59:42,480 --> 11:59:46,160 that in the find menu and here you can 11064 11:59:46,160 --> 11:59:48,000 say 11065 11:59:48,000 --> 11:59:53,320 include artist to true that's 11066 12:00:00,720 --> 12:00:04,560 it let's fetch all the 11067 12:00:04,560 --> 12:00:09,000 songs here the artist ID is null artist 11068 12:00:09,000 --> 12:00:12,840 is null here you can see 11069 12:00:12,840 --> 12:00:16,276 the artist ID one and we have a complete 11070 12:00:16,276 --> 12:00:18,840 artist 11071 12:00:21,800 --> 12:00:25,956 record that's how you do 11072 12:00:35,840 --> 12:00:39,400 population we have used this include 11073 12:00:39,400 --> 12:00:42,596 syntax you can also use the connect 11074 12:00:42,596 --> 12:00:44,276 object if you want to make the 11075 12:00:44,276 --> 12:00:48,560 connection or update the reference ID 11076 12:00:48,560 --> 12:00:51,320 you can use the 11077 12:00:54,640 --> 12:00:57,596 connect now we're going to implement one 11078 12:00:57,596 --> 12:01:01,080 to one relationship between two models 11079 12:01:01,080 --> 12:01:05,000 we're going to have a user model here we 11080 12:01:05,000 --> 12:01:08,200 have a one to one relationship between 11081 12:01:08,200 --> 12:01:12,276 user and profile each user will have one 11082 12:01:12,276 --> 12:01:15,756 unique profile in the profile we have 11083 12:01:15,756 --> 12:01:18,276 photo we have the phone number of the 11084 12:01:18,276 --> 12:01:21,436 client of the user this is how you will 11085 12:01:21,436 --> 12:01:25,040 make a one toone relationship user and a 11086 12:01:25,040 --> 12:01:29,480 relation and this makes each profile 11087 12:01:29,480 --> 12:01:34,436 will have a unique ID of a user table 11088 12:01:34,436 --> 12:01:37,520 then we will generate Prisma 11089 12:01:37,520 --> 12:01:39,880 migrations then we will generate a new 11090 12:01:39,880 --> 12:01:42,160 user with the help of nest generat 11091 12:01:42,160 --> 12:01:44,240 resource a user we're going to use cred 11092 12:01:44,240 --> 12:01:47,080 AP endpoints and finally we're going to 11093 12:01:47,080 --> 12:01:50,720 save a user on the based on profile like 11094 12:01:50,720 --> 12:01:54,040 when we are when we are saving a user I 11095 12:01:54,040 --> 12:01:57,240 also want to save the profile this is 11096 12:01:57,240 --> 12:02:00,000 how you can do that profile and a create 11097 12:02:00,000 --> 12:02:05,040 object and the attributes of profile 11098 12:02:05,040 --> 12:02:08,080 table it will automatically made a 11099 12:02:08,080 --> 12:02:11,240 relationship between user and a profile 11100 12:02:11,240 --> 12:02:13,640 and finally I want to fetch all the user 11101 12:02:13,640 --> 12:02:15,160 with the 11102 12:02:15,160 --> 12:02:17,640 profile if you want to implement it you 11103 12:02:17,640 --> 12:02:21,000 can do it but let me show you how can I 11104 12:02:21,000 --> 12:02:25,560 implement it I'm going to copy the 11105 12:02:29,040 --> 12:02:33,276 model here here here inside the 11106 12:02:33,276 --> 12:02:36,360 schema let's add two new 11107 12:02:36,360 --> 12:02:39,080 models I've added two new models let's 11108 12:02:39,080 --> 12:02:40,756 run the 11109 12:02:40,756 --> 12:02:43,040 migration 11110 12:02:43,040 --> 12:02:48,000 npx Prisma migrate Dev the name of the 11111 12:02:48,000 --> 12:02:50,480 migration one 11112 12:02:50,480 --> 12:02:51,980 to1 11113 12:02:51,980 --> 12:02:55,029 [Music] 11114 12:02:58,080 --> 12:03:00,640 relation it has run the 11115 12:03:00,640 --> 12:03:03,400 migrations and now it has also generated 11116 12:03:03,400 --> 12:03:06,160 the types inside the Prisma client I can 11117 12:03:06,160 --> 12:03:09,400 double check that from the Prisma here 11118 12:03:09,400 --> 12:03:13,240 you have all of your user types user and 11119 12:03:13,240 --> 12:03:17,240 profile when you search the 11120 12:03:19,436 --> 12:03:24,720 profile here we have profile 11121 12:03:25,116 --> 12:03:30,756 stuff our next step is to generate a 11122 12:03:35,596 --> 12:03:38,596 resource Nest generate 11123 12:03:38,596 --> 12:03:40,720 resource 11124 12:03:40,720 --> 12:03:43,720 users 11125 12:03:45,956 --> 12:03:48,400 select rest API end 11126 12:03:48,400 --> 12:03:51,436 points getri 11127 12:03:51,436 --> 12:03:55,000 of the 11128 12:03:57,200 --> 12:04:00,800 entities and I need to inject Prisma 11129 12:04:00,800 --> 12:04:03,756 service inside the 11130 12:04:03,756 --> 12:04:07,000 module inside the users module I have to 11131 12:04:07,000 --> 12:04:09,800 inject the Prisma service 11132 12:04:09,800 --> 12:04:13,640 here Prisma 11133 12:04:17,480 --> 12:04:22,040 service inside the user service we need 11134 12:04:22,040 --> 12:04:26,160 to inject Prisma service 11135 12:04:26,160 --> 12:04:31,080 dependency I can get that from the song 11136 12:04:31,276 --> 12:04:35,436 service we need the Prisma 11137 12:04:40,640 --> 12:04:42,360 service 11138 12:04:42,360 --> 12:04:46,956 and I'm going to copy this one as a 11139 12:04:49,276 --> 12:04:51,840 Constructor so we have to call the 11140 12:04:51,840 --> 12:04:55,000 create method I'm going to copy the 11141 12:04:55,000 --> 12:04:56,956 exact create 11142 12:04:56,956 --> 12:04:59,880 method and provide it 11143 12:04:59,880 --> 12:05:03,800 here that's it this time I'm going to 11144 12:05:03,800 --> 12:05:07,680 use the create user 11145 12:05:07,680 --> 12:05:11,000 dto I have not defined the properties in 11146 12:05:11,000 --> 12:05:13,480 the create user user dto if you want to 11147 12:05:13,480 --> 12:05:16,560 add the validation you can use the class 11148 12:05:16,560 --> 12:05:19,000 validator I taught you earlier in the 11149 12:05:19,000 --> 12:05:20,116 previous 11150 12:05:20,116 --> 12:05:23,756 modules we will have photo the URL of 11151 12:05:23,756 --> 12:05:26,880 the photo should be 11152 12:05:27,520 --> 12:05:30,520 string and phone number the type should 11153 12:05:30,520 --> 12:05:32,680 be 11154 12:05:37,200 --> 12:05:39,756 string now we're going to I'm going to 11155 12:05:39,756 --> 12:05:43,080 copy find all method 11156 12:05:43,080 --> 12:05:46,320 and I'm going to use this profile I want 11157 12:05:46,320 --> 12:05:50,240 to fetch the profile with each user now 11158 12:05:50,240 --> 12:05:55,200 let's try to test it run the 11159 12:06:00,400 --> 12:06:03,436 application this create object is used 11160 12:06:03,436 --> 12:06:08,080 to create an nested object a relation 11161 12:06:08,080 --> 12:06:10,520 relation 11162 12:06:10,520 --> 12:06:13,520 object 11163 12:06:18,560 --> 12:06:21,916 let's create a new 11164 12:06:22,040 --> 12:06:25,040 user 11165 12:06:25,756 --> 12:06:30,956 users provide the name of the user 11166 12:06:30,956 --> 12:06:34,320 jendo and we have to provide the photo 11167 12:06:34,320 --> 12:06:36,956 URL some 11168 12:06:36,956 --> 12:06:39,560 api. com/ 11169 12:06:39,560 --> 12:06:42,240 phos 1. J 11170 12:06:42,240 --> 12:06:46,756 APG and you need to provide the phone 11171 12:06:49,360 --> 12:06:53,680 number let me create a new 11172 12:06:53,680 --> 12:06:57,320 user a user has created successfully 11173 12:06:57,320 --> 12:07:01,000 let's try to find all the 11174 12:07:04,320 --> 12:07:07,800 users VCH 11175 12:07:10,436 --> 12:07:13,436 users 11176 12:07:14,800 --> 12:07:18,680 here you can see that I can see the 11177 12:07:18,680 --> 12:07:22,116 user and we have the profile object 11178 12:07:22,116 --> 12:07:24,800 inside the user you will also see a 11179 12:07:24,800 --> 12:07:28,800 relationship with user ID one which is 11180 12:07:28,800 --> 12:07:32,560 one that's how you implement one to one 11181 12:07:32,560 --> 12:07:35,520 relation in this lesson you're going to 11182 12:07:35,520 --> 12:07:38,480 learn how to implement many to many 11183 12:07:38,480 --> 12:07:40,436 relationship between 11184 12:07:40,436 --> 12:07:43,840 models I have this use case one blog 11185 12:07:43,840 --> 12:07:47,756 post can belong to many categories and 11186 12:07:47,756 --> 12:07:50,320 many categories can belong to a single 11187 12:07:50,320 --> 12:07:55,160 block post so there is a many to many 11188 12:07:55,840 --> 12:07:58,640 relationship when you implement many to 11189 12:07:58,640 --> 12:08:02,720 many relationship you need a third model 11190 12:08:02,720 --> 12:08:06,596 in the third model you store the foreign 11191 12:08:06,596 --> 12:08:09,436 key of your first model and the foreign 11192 12:08:09,436 --> 12:08:12,596 key or prime for foreign key as the 11193 12:08:12,596 --> 12:08:17,596 primary key the foreign constraint here 11194 12:08:17,596 --> 12:08:22,916 it's a it's a foreign key of category 11195 12:08:22,916 --> 12:08:26,520 table and in Prisma you define the 11196 12:08:26,520 --> 12:08:29,916 primary key by combination of post ID 11197 12:08:29,916 --> 12:08:33,000 and the category 11198 12:08:33,200 --> 12:08:36,800 ID this is how you will make uh many to 11199 12:08:36,800 --> 12:08:39,680 many relationship now the categories 11200 12:08:39,680 --> 12:08:42,160 instead of categories array you will use 11201 12:08:42,160 --> 12:08:46,200 the categories on post array the same 11202 12:08:46,200 --> 12:08:49,916 stuff here instead of using post array 11203 12:08:49,916 --> 12:08:53,880 here you will use the categories on post 11204 12:08:53,880 --> 12:08:55,956 array and then 11205 12:08:55,956 --> 12:09:00,400 you we will generate 11206 12:09:00,840 --> 12:09:03,200 migrations we will generate migrations 11207 12:09:03,200 --> 12:09:05,840 and it will also generate the Prisma 11208 12:09:05,840 --> 12:09:07,040 client 11209 12:09:07,040 --> 12:09:09,640 types and then we will generate the 11210 12:09:09,640 --> 12:09:13,956 resource post we will create the post by 11211 12:09:13,956 --> 12:09:18,840 using this input type post create 11212 12:09:19,160 --> 12:09:22,360 input and I also have to update the type 11213 12:09:22,360 --> 12:09:24,720 in the 11214 12:09:24,720 --> 12:09:28,080 controller and this is how you will send 11215 12:09:28,080 --> 12:09:31,720 the request to create a new 11216 12:09:31,720 --> 12:09:35,956 post here the title of the post one to 11217 12:09:35,956 --> 12:09:36,880 many 11218 12:09:36,880 --> 12:09:40,640 relation this this is the Prisma post 11219 12:09:40,640 --> 12:09:43,116 and it it can have two 11220 12:09:43,116 --> 12:09:45,800 categories here you can say I I'm going 11221 12:09:45,800 --> 12:09:50,560 to create a new category this post can 11222 12:09:50,560 --> 12:09:54,520 also belong to Prisma category this post 11223 12:09:54,520 --> 12:09:58,360 can also belong to nestjs category that 11224 12:09:58,360 --> 12:10:01,360 is why I created two categories you can 11225 12:10:01,360 --> 12:10:04,720 see that category number one and this is 11226 12:10:04,720 --> 12:10:08,360 category number two here I here we have 11227 12:10:08,360 --> 12:10:10,240 the array of the 11228 12:10:10,240 --> 12:10:13,276 categories similarly you can do the same 11229 12:10:13,276 --> 12:10:15,360 thing if you don't want to create a new 11230 12:10:15,360 --> 12:10:18,840 post you can connect with existing 11231 12:10:18,840 --> 12:10:21,400 post I'm going to teach you transaction 11232 12:10:21,400 --> 12:10:23,276 let's say I'm going to write blog post 11233 12:10:23,276 --> 12:10:25,200 on transactions in 11234 12:10:25,200 --> 12:10:30,160 Prisma and here this post also belong to 11235 12:10:30,160 --> 12:10:33,756 nestjs category and Prisma category 11236 12:10:33,756 --> 12:10:34,756 that's 11237 12:10:34,756 --> 12:10:38,720 why I'm I'm connecting to category 11238 12:10:38,720 --> 12:10:42,360 number one and the category number two 11239 12:10:42,360 --> 12:10:45,800 and finally you can also do a relation 11240 12:10:45,800 --> 12:10:49,436 queries with the help of this type post 11241 12:10:49,436 --> 12:10:52,680 where uni input also have to update in 11242 12:10:52,680 --> 12:10:53,640 the 11243 12:10:53,640 --> 12:10:57,640 controller this is how you can send the 11244 12:10:57,640 --> 12:11:00,520 request categories this is a we Clause 11245 12:11:00,520 --> 12:11:03,640 category and the name you can check all 11246 12:11:03,640 --> 12:11:08,116 of different type of queries in the 11247 12:11:08,116 --> 12:11:12,400 documentation let's try to implement it 11248 12:11:12,400 --> 12:11:17,360 first if you want you can Implement by 11249 12:11:20,320 --> 12:11:23,040 yourself many to 11250 12:11:23,040 --> 12:11:26,040 many 11251 12:11:35,596 --> 12:11:38,916 relation I'm going to create a post 11252 12:11:38,916 --> 12:11:43,520 model and my next model model is 11253 12:11:43,520 --> 12:11:47,000 category it will have name and the ID 11254 12:11:47,000 --> 12:11:50,756 and my third model is categories on post 11255 12:11:50,756 --> 12:11:54,320 I'm going to save now you can see 11256 12:11:54,320 --> 12:11:57,596 that these two properties are inside the 11257 12:11:57,596 --> 12:12:01,320 categories on post I would like to store 11258 12:12:01,320 --> 12:12:03,436 the 11259 12:12:03,436 --> 12:12:09,800 date and I want to store who has created 11260 12:12:10,116 --> 12:12:12,596 this 11261 12:12:12,596 --> 12:12:16,680 let's run the 11262 12:12:28,240 --> 12:12:31,840 migration it has also generated a Prisma 11263 12:12:31,840 --> 12:12:33,116 client 11264 12:12:33,116 --> 12:12:35,720 types you can check it in the node 11265 12:12:35,720 --> 12:12:39,160 modules at Prisma / 11266 12:12:39,160 --> 12:12:42,360 client our next step step is to generate 11267 12:12:42,360 --> 12:12:45,116 the post resource we're going to perform 11268 12:12:45,116 --> 12:12:47,880 some cred 11269 12:12:56,840 --> 12:13:00,240 operations please select the rest 11270 12:13:00,240 --> 12:13:05,320 API yes I want create entry end 11271 12:13:05,320 --> 12:13:10,040 points I have the post get rid of the 11272 12:13:10,040 --> 12:13:13,040 entities 11273 12:13:14,560 --> 12:13:17,320 one more thing I would like to register 11274 12:13:17,320 --> 12:13:18,720 a Prisma 11275 12:13:18,720 --> 12:13:22,040 service inside the post module otherwise 11276 12:13:22,040 --> 12:13:24,840 you will get the 11277 12:13:30,680 --> 12:13:33,520 error now we're going to write the code 11278 12:13:33,520 --> 12:13:36,720 in to create a new 11279 12:13:36,720 --> 12:13:41,320 post we also need to inject Prisma 11280 12:13:41,320 --> 12:13:43,276 Service as a 11281 12:13:43,276 --> 12:13:47,560 dependency I'm going to copy these two 11282 12:13:47,560 --> 12:13:52,880 lines and I'm going to paste here Post 11283 12:13:52,880 --> 12:13:56,320 Service let me copy The Constructor we 11284 12:13:56,320 --> 12:13:59,320 need to inject the 11285 12:14:01,880 --> 12:14:04,916 dependency I'm going to copy my 11286 12:14:04,916 --> 12:14:07,916 create 11287 12:14:10,040 --> 12:14:13,040 function 11288 12:14:15,400 --> 12:14:18,880 something is 11289 12:14:32,320 --> 12:14:36,560 missing I have done something 11290 12:14:38,520 --> 12:14:43,520 wrong so let's you use the 11291 12:14:48,520 --> 12:14:51,756 Constructor pris 11292 12:14:51,756 --> 12:14:57,880 private Prisma and Prisma 11293 12:15:07,880 --> 12:15:12,916 service let's use this type 11294 12:15:15,520 --> 12:15:18,320 actually I don't need 11295 12:15:18,320 --> 12:15:23,956 that let me copy the fold 11296 12:15:24,560 --> 12:15:28,160 create what is 11297 12:15:30,240 --> 12:15:33,680 this now it looks 11298 12:15:33,680 --> 12:15:37,880 good so we also need to use that post 11299 12:15:37,880 --> 12:15:42,916 create post create input in the post 11300 12:15:52,040 --> 12:15:56,116 controller let me import 11301 12:16:00,360 --> 12:16:06,320 Prisma now it's time to test this 11302 12:16:09,840 --> 12:16:12,840 application 11303 12:16:15,080 --> 12:16:17,480 the application is running fine let's 11304 12:16:17,480 --> 12:16:20,276 send the API 11305 12:16:20,276 --> 12:16:25,480 request I'm going to copy my API 11306 12:16:29,520 --> 12:16:32,520 request here I'm going to create a new 11307 12:16:32,520 --> 12:16:37,680 post and I'm going to also create a two 11308 12:16:37,680 --> 12:16:40,200 categories so you can use the create 11309 12:16:40,200 --> 12:16:41,400 array 11310 12:16:41,400 --> 12:16:43,520 inside the create array I have the 11311 12:16:43,520 --> 12:16:47,200 category this is the post on category 11312 12:16:47,200 --> 12:16:51,480 object so that's why I use that 11313 12:16:51,480 --> 12:16:55,436 properties let's send the 11314 12:16:55,680 --> 12:17:00,160 request it should be 3,000 11315 12:17:00,160 --> 12:17:05,040 or 3,000 it should 11316 12:17:05,400 --> 12:17:08,916 3,000 now you can see that one to many 11317 12:17:08,916 --> 12:17:13,436 relation post has created 11318 12:17:16,520 --> 12:17:20,480 successfully now it's time to do a 11319 12:17:20,480 --> 12:17:24,080 register another 11320 12:17:25,596 --> 12:17:28,640 post this time I don't want to create a 11321 12:17:28,640 --> 12:17:30,956 new 11322 12:17:31,080 --> 12:17:34,596 category create a post 11323 12:17:34,596 --> 12:17:38,640 with existing 11324 12:17:39,040 --> 12:17:42,160 categories I would like to create a new 11325 12:17:42,160 --> 12:17:44,800 post and I want to use 11326 12:17:44,800 --> 12:17:47,956 the category number one and category 11327 12:17:47,956 --> 12:17:52,160 number two let's create a new post has 11328 12:17:52,160 --> 12:17:55,276 created it has also made a 11329 12:17:55,276 --> 12:17:59,160 connection with this category you can 11330 12:17:59,160 --> 12:18:02,840 double check from the PG 11331 12:18:07,480 --> 12:18:11,520 admin meanwhile I'm going to write the 11332 12:18:11,520 --> 12:18:15,000 code to do relational 11333 12:18:15,000 --> 12:18:17,276 queries let's 11334 12:18:17,276 --> 12:18:21,720 update the find find 11335 12:18:24,956 --> 12:18:28,596 all that looks good we also have to 11336 12:18:28,596 --> 12:18:30,160 update the 11337 12:18:30,160 --> 12:18:34,800 get inside the post 11338 12:18:39,720 --> 12:18:42,160 controller 11339 12:18:42,160 --> 12:18:45,916 now we have to fetch all the post with 11340 12:18:45,916 --> 12:18:48,160 nestjs 11341 12:18:48,160 --> 12:18:51,840 category so you can do that you can 11342 12:18:51,840 --> 12:18:54,520 learn more about how to do queries in 11343 12:18:54,520 --> 12:18:56,436 Prisma 11344 12:18:56,436 --> 12:19:00,360 documentation I'm going to send the 11345 12:19:00,800 --> 12:19:05,840 request the post the should be 11346 12:19:09,276 --> 12:19:11,640 3,000 now you can 11347 12:19:11,640 --> 12:19:16,596 see I got these two post with the help 11348 12:19:16,596 --> 12:19:20,800 with the I justed query with 11349 12:19:33,480 --> 12:19:37,040 category what is my name of the database 11350 12:19:37,040 --> 12:19:39,400 let me get it from 11351 12:19:39,400 --> 12:19:41,160 EnV PR 11352 12:19:41,160 --> 12:19:43,800 Nest Prisma 11353 12:19:43,800 --> 12:19:47,560 DB here I have Nest Prisma DB let me 11354 12:19:47,560 --> 12:19:50,000 find the 11355 12:19:50,000 --> 12:19:54,360 tables and we have post category and 11356 12:19:54,360 --> 12:19:59,596 categories on post let's look at the 11357 12:19:59,596 --> 12:20:04,080 post we should have two 11358 12:20:04,276 --> 12:20:07,720 post you can 11359 12:20:08,080 --> 12:20:11,596 see I have one too many and 11360 12:20:11,596 --> 12:20:16,720 transaction I must have two 11361 12:20:18,756 --> 12:20:21,840 categories you can see that let me find 11362 12:20:21,840 --> 12:20:24,756 categories on 11363 12:20:27,680 --> 12:20:33,800 post here you can see I have four 11364 12:20:36,436 --> 12:20:40,116 records there is another way to use 11365 12:20:40,116 --> 12:20:43,000 transac ction feature if you have to 11366 12:20:43,000 --> 12:20:46,160 perform the batch or bulk operations 11367 12:20:46,160 --> 12:20:49,200 like delete manyu create manyu and 11368 12:20:49,200 --> 12:20:54,200 update manyu you can use all of these in 11369 12:20:54,200 --> 12:20:57,640 a one query you want to create three 11370 12:20:57,640 --> 12:21:00,756 records you want to delete two records 11371 12:21:00,756 --> 12:21:03,040 or maybe you want to update a single 11372 12:21:03,040 --> 12:21:05,640 record or maybe you want to update five 11373 12:21:05,640 --> 12:21:09,640 records so in that case let's say if any 11374 12:21:09,640 --> 12:21:11,800 query fail 11375 12:21:11,800 --> 12:21:16,200 then all of the operation will be 11376 12:21:16,200 --> 12:21:19,240 discarded I'm going to run multiple 11377 12:21:19,240 --> 12:21:23,480 queries at same time if any query failed 11378 12:21:23,480 --> 12:21:27,320 you will not get result 11379 12:21:29,956 --> 12:21:33,560 back you can use this query or 11380 12:21:33,560 --> 12:21:36,880 transaction API when you want to create 11381 12:21:36,880 --> 12:21:38,400 batch 11382 12:21:38,400 --> 12:21:41,436 operation here is an example 11383 12:21:41,436 --> 12:21:44,320 I want to find all of the post I want to 11384 12:21:44,320 --> 12:21:46,840 find all of the artist I want to find 11385 12:21:46,840 --> 12:21:48,880 all songs I want to find all 11386 12:21:48,880 --> 12:21:52,360 applications in a single query if this 11387 12:21:52,360 --> 12:21:55,916 query fails it is going to give me the 11388 12:21:55,916 --> 12:21:57,880 this operation this complete operation 11389 12:21:57,880 --> 12:22:00,400 will be discarded I will not get any 11390 12:22:00,400 --> 12:22:02,800 result that is why you use this 11391 12:22:02,800 --> 12:22:04,000 transaction 11392 12:22:04,000 --> 12:22:07,520 API to implement the demo or to 11393 12:22:07,520 --> 12:22:09,436 implement the transaction 11394 12:22:09,436 --> 12:22:13,596 API let's do it in app 11395 12:22:13,596 --> 12:22:17,596 controller we have to inject Prisma 11396 12:22:17,596 --> 12:22:20,596 service I just want to show you the demo 11397 12:22:20,596 --> 12:22:24,880 how can you use the transaction 11398 12:22:24,880 --> 12:22:27,880 API this my 11399 12:22:27,880 --> 12:22:31,400 service we have to inject it 11400 12:22:31,400 --> 12:22:35,596 here I can say 11401 12:22:38,360 --> 12:22:43,320 transaction let me register Prisma 11402 12:22:45,276 --> 12:22:50,360 service now I have injected it 11403 12:22:53,756 --> 12:22:56,756 here we have to import 11404 12:22:56,756 --> 12:23:01,840 it sounds good I'm going to create a new 11405 12:23:01,840 --> 12:23:05,560 method with name 11406 12:23:05,720 --> 12:23:09,116 sequential and get 11407 12:23:09,116 --> 12:23:12,160 sequential result results this could be 11408 12:23:12,160 --> 12:23:13,800 the name of the 11409 12:23:13,800 --> 12:23:19,916 query and here I'm going to copy my 11410 12:23:20,436 --> 12:23:23,480 code so let's try to test it I'm going 11411 12:23:23,480 --> 12:23:26,116 to run the 11412 12:23:30,160 --> 12:23:33,276 application let me send the request for 11413 12:23:33,276 --> 12:23:37,756 sequential query in my HTTP 11414 12:23:37,756 --> 12:23:39,360 client 11415 12:23:39,360 --> 12:23:41,400 sequential 11416 12:23:41,400 --> 12:23:43,680 query 11417 12:23:43,680 --> 12:23:47,956 HTTP Local Host colum 11418 12:23:47,956 --> 12:23:51,436 3000 and 11419 12:23:53,956 --> 12:23:57,756 sequential you can see I got the result 11420 12:23:57,756 --> 12:24:03,520 of all of my post 11421 12:24:03,640 --> 12:24:07,000 applications this these should be post 11422 12:24:07,000 --> 12:24:09,200 the 11423 12:24:09,320 --> 12:24:11,040 songs 11424 12:24:11,040 --> 12:24:14,720 and here you will have application these 11425 12:24:14,720 --> 12:24:17,756 are all the applications with customer 11426 12:24:17,756 --> 12:24:20,756 ID this is how it will 11427 12:24:20,756 --> 12:24:24,596 work if you want to add include or to 11428 12:24:24,596 --> 12:24:29,480 fetch nested record you can also do 11429 12:24:31,956 --> 12:24:35,436 that now I'm going to talk about the 11430 12:24:35,436 --> 12:24:37,800 concept of nested 11431 12:24:37,800 --> 12:24:41,880 queries this topic already is in the 11432 12:24:41,880 --> 12:24:43,520 transaction 11433 12:24:43,520 --> 12:24:46,360 API I'm talking about transaction when 11434 12:24:46,360 --> 12:24:51,400 you have this type of use case let's 11435 12:24:51,400 --> 12:24:56,360 say first I have to create the customer 11436 12:24:56,360 --> 12:24:59,596 email customer or the user and then 11437 12:24:59,596 --> 12:25:02,840 there is an address let me look at the 11438 12:25:02,840 --> 12:25:06,840 model structure we have a customer model 11439 12:25:06,840 --> 12:25:09,116 and we have an address model there is a 11440 12:25:09,116 --> 12:25:11,640 one toone relationship between customer 11441 12:25:11,640 --> 12:25:14,596 and the address and I have created the 11442 12:25:14,596 --> 12:25:18,000 enum application type and I have the 11443 12:25:18,000 --> 12:25:20,400 application model and the type should be 11444 12:25:20,400 --> 12:25:23,956 application type and default to now and 11445 12:25:23,956 --> 12:25:26,720 I choose a loan loan 11446 12:25:26,720 --> 12:25:29,596 here and I have made one to many 11447 12:25:29,596 --> 12:25:32,916 relationship between application and the 11448 12:25:32,916 --> 12:25:36,720 customer we have the applications array 11449 12:25:36,720 --> 12:25:42,480 here so let's say I want to create a new 11450 12:25:42,480 --> 12:25:46,596 customer address and here I'm creating a 11451 12:25:46,596 --> 12:25:50,320 new address and here is the Fe here are 11452 12:25:50,320 --> 12:25:52,400 the fields of 11453 12:25:52,400 --> 12:25:55,596 customers and I'm going to create 11454 12:25:55,596 --> 12:25:57,840 applications basically we going to we 11455 12:25:57,840 --> 12:26:00,116 are talking about a banking type 11456 12:26:00,116 --> 12:26:02,800 application let's say a customer comes 11457 12:26:02,800 --> 12:26:06,560 to apply for the loan customer can apply 11458 12:26:06,560 --> 12:26:10,360 for multiple loan applications loan car 11459 12:26:10,360 --> 12:26:12,800 financing or business financing a 11460 12:26:12,800 --> 12:26:15,640 customer can apply apply all of these or 11461 12:26:15,640 --> 12:26:20,200 can can apply to one or second type 11462 12:26:20,200 --> 12:26:23,276 application so tenure of your 11463 12:26:23,276 --> 12:26:25,756 application would be 5 years or 2 years 11464 12:26:25,756 --> 12:26:27,800 how many amount you want how much how 11465 12:26:27,800 --> 12:26:30,320 much loan you 11466 12:26:30,320 --> 12:26:33,800 want so this type of application when 11467 12:26:33,800 --> 12:26:37,560 banking banking in banking application 11468 12:26:37,560 --> 12:26:41,560 when an application comes they create a 11469 12:26:41,560 --> 12:26:44,520 new customer record they create the 11470 12:26:44,520 --> 12:26:47,480 address and they create the application 11471 12:26:47,480 --> 12:26:50,720 based on current user or current 11472 12:26:50,720 --> 12:26:52,276 customer 11473 12:26:52,276 --> 12:26:56,320 demand let's say a new customer comes 11474 12:26:56,320 --> 12:27:00,200 with business financing application John 11475 12:27:00,200 --> 12:27:03,320 wants to get the business financing from 11476 12:27:03,320 --> 12:27:06,720 a bank so he's decided he's going to 11477 12:27:06,720 --> 12:27:09,880 apply for business financing application 11478 12:27:09,880 --> 12:27:12,560 what under the hood is saying what under 11479 12:27:12,560 --> 12:27:14,800 the hood banking application will create 11480 12:27:14,800 --> 12:27:18,116 a new customer they will create a new 11481 12:27:18,116 --> 12:27:23,240 address and they will also create a new 11482 12:27:23,240 --> 12:27:25,640 application but they have a 11483 12:27:25,640 --> 12:27:28,680 feature a customer can apply for two 11484 12:27:28,680 --> 12:27:31,400 loans two applications or three 11485 12:27:31,400 --> 12:27:33,840 applications he can also apply on 11486 12:27:33,840 --> 12:27:36,200 business financing he can also apply on 11487 12:27:36,200 --> 12:27:37,160 car 11488 12:27:37,160 --> 12:27:39,400 financing so that's why there is a 11489 12:27:39,400 --> 12:27:41,436 create array 11490 12:27:41,436 --> 12:27:44,360 here what what are the benefits of using 11491 12:27:44,360 --> 12:27:47,840 transaction under the 11492 12:27:47,916 --> 12:27:51,080 hood Prisma is using transaction when 11493 12:27:51,080 --> 12:27:53,360 you run the nested queries these are all 11494 12:27:53,360 --> 12:27:55,320 the this is the nested 11495 12:27:55,320 --> 12:27:58,360 query we are creating address and we are 11496 12:27:58,360 --> 12:28:00,680 creating applications this is the nested 11497 12:28:00,680 --> 12:28:03,956 query let's say if an error comes here 11498 12:28:03,956 --> 12:28:06,640 if I will add the invalid value 11499 12:28:06,640 --> 12:28:10,116 here it will not create the complete app 11500 12:28:10,116 --> 12:28:11,956 application it will not create the 11501 12:28:11,956 --> 12:28:14,840 customer it will not create the 11502 12:28:14,840 --> 12:28:17,560 address so that is the benefit of 11503 12:28:17,560 --> 12:28:19,520 transaction I'll show you with the help 11504 12:28:19,520 --> 12:28:22,436 of demo this is how you can 11505 12:28:22,436 --> 12:28:27,200 implement the nested queries in Prisma 11506 12:28:27,200 --> 12:28:30,000 I've already done that for you I'm not 11507 12:28:30,000 --> 12:28:32,596 going to implement it here first of I 11508 12:28:32,596 --> 12:28:35,640 can walk walk through the code I created 11509 12:28:35,640 --> 12:28:38,596 the customer model we have the ID we 11510 12:28:38,596 --> 12:28:41,200 have the name field we have the email we 11511 12:28:41,200 --> 12:28:43,916 have the address and we have the 11512 12:28:43,916 --> 12:28:46,596 applications 11513 12:28:46,880 --> 12:28:50,800 array so address should be here it 11514 12:28:50,800 --> 12:28:54,240 should be at the top here so I have made 11515 12:28:54,240 --> 12:28:57,680 a on to one relationship between address 11516 12:28:57,680 --> 12:28:59,360 and the customer here is a unique 11517 12:28:59,360 --> 12:29:02,080 denotes to denotes to one to one 11518 12:29:02,080 --> 12:29:05,160 relationship we have Zip City Country 11519 12:29:05,160 --> 12:29:07,080 inside the address and there is a on 11520 12:29:07,080 --> 12:29:08,916 toone relationship between customer and 11521 12:29:08,916 --> 12:29:12,436 the address but I also have one too many 11522 12:29:12,436 --> 12:29:15,360 relationship between application and 11523 12:29:15,360 --> 12:29:18,276 customer so I told you a John can apply 11524 12:29:18,276 --> 12:29:21,640 for car car financing he can apply for 11525 12:29:21,640 --> 12:29:22,800 business 11526 12:29:22,800 --> 12:29:25,160 financing that's why I use the 11527 12:29:25,160 --> 12:29:27,276 application 11528 12:29:27,276 --> 12:29:30,756 array so the tenure amount application 11529 12:29:30,756 --> 12:29:33,080 type I have set the default to 11530 12:29:33,080 --> 12:29:36,956 loan so that was our model structure 11531 12:29:36,956 --> 12:29:41,800 next step I have executed nested query 11532 12:29:41,800 --> 12:29:45,160 Migration by executing this command 11533 12:29:45,160 --> 12:29:47,720 nested 11534 12:29:48,320 --> 12:29:50,320 queries 11535 12:29:50,320 --> 12:29:53,040 here it is going to create the customer 11536 12:29:53,040 --> 12:29:55,240 table it is going to create the address 11537 12:29:55,240 --> 12:29:57,360 table and create the constraint with 11538 12:29:57,360 --> 12:30:00,080 customer and the address it is also 11539 12:30:00,080 --> 12:30:05,320 going to create uh application table and 11540 12:30:05,320 --> 12:30:08,240 create constraint between application 11541 12:30:08,240 --> 12:30:11,080 and the customer 11542 12:30:11,080 --> 12:30:15,520 and then I have set the default value to 11543 12:30:15,680 --> 12:30:18,276 laan and then I have created the 11544 12:30:18,276 --> 12:30:20,400 application resource by running Nest 11545 12:30:20,400 --> 12:30:22,840 generate resource and you will see the 11546 12:30:22,840 --> 12:30:24,956 application inside the application 11547 12:30:24,956 --> 12:30:28,116 module I have injected the Prisma 11548 12:30:28,116 --> 12:30:31,436 service and then here I'm injecting the 11549 12:30:31,436 --> 12:30:34,640 Prisma Service as a dependency and I 11550 12:30:34,640 --> 12:30:35,596 just 11551 12:30:35,596 --> 12:30:39,400 called create application dto by using 11552 12:30:39,400 --> 12:30:42,320 the customer customer create input you 11553 12:30:42,320 --> 12:30:43,880 can double check it from the customer 11554 12:30:43,880 --> 12:30:46,200 create input you can get name email 11555 12:30:46,200 --> 12:30:47,916 address and 11556 12:30:47,916 --> 12:30:51,360 application all of this stuff 11557 12:30:51,360 --> 12:30:53,520 here name 11558 12:30:53,520 --> 12:30:57,276 email address I provided the address and 11559 12:30:57,276 --> 12:31:00,320 I'm creating it here I also can provide 11560 12:31:00,320 --> 12:31:04,000 the applications array you can check the 11561 12:31:04,000 --> 12:31:10,680 type by copying it and type and paste 11562 12:31:10,680 --> 12:31:13,916 so it should give me 11563 12:31:13,916 --> 12:31:17,720 the this one 11564 12:31:19,276 --> 12:31:22,080 type so I got this application create 11565 12:31:22,080 --> 12:31:24,436 nested with custom customer input so I 11566 12:31:24,436 --> 12:31:28,000 use the create did you see I use the 11567 12:31:28,000 --> 12:31:31,436 create can have the array so create can 11568 12:31:31,436 --> 12:31:34,480 have the array 11569 12:31:38,840 --> 12:31:42,520 here so you can provide the application 11570 12:31:42,520 --> 12:31:44,080 object 11571 12:31:44,080 --> 12:31:49,240 here if any fails the transaction will 11572 12:31:49,240 --> 12:31:53,840 roll back or discarded the 11573 12:31:55,880 --> 12:31:59,640 operation let's try to test 11574 12:31:59,640 --> 12:32:03,400 it you have to send the AP request 11575 12:32:03,400 --> 12:32:05,880 inside the HTTP 11576 12:32:05,880 --> 12:32:08,840 client here I'm going to send the API 11577 12:32:08,840 --> 12:32:12,200 request John J 11578 12:32:12,200 --> 12:32:17,040 2 jendo City New York ZIP code this one 11579 12:32:17,040 --> 12:32:21,160 amount he wants say 11580 12:32:24,596 --> 12:32:27,596 10,000 inue over 5 years and he wants 11581 12:32:27,596 --> 12:32:29,756 business financing I'm going to create a 11582 12:32:29,756 --> 12:32:30,640 new 11583 12:32:30,640 --> 12:32:33,360 application you you can see a new 11584 12:32:33,360 --> 12:32:36,840 application has created if I provide the 11585 12:32:36,840 --> 12:32:39,276 invalid value I have set the integer 11586 12:32:39,276 --> 12:32:42,320 here I'm providing the float or float 11587 12:32:42,320 --> 12:32:46,360 value I got the issue now it will not 11588 12:32:46,360 --> 12:32:49,116 create an application you can double 11589 12:32:49,116 --> 12:32:51,756 check it from PG 11590 12:32:51,756 --> 12:32:55,800 admin that's the benefit of 11591 12:32:56,640 --> 12:32:59,080 transaction in this lesson you're going 11592 12:32:59,080 --> 12:33:01,200 to learn the interactive 11593 12:33:01,200 --> 12:33:03,080 transactions what do I mean by 11594 12:33:03,080 --> 12:33:04,360 interactive 11595 12:33:04,360 --> 12:33:08,360 transaction when one query is dependent 11596 12:33:08,360 --> 12:33:11,800 on another query one query result is 11597 12:33:11,800 --> 12:33:15,276 dependent on another query result let's 11598 12:33:15,276 --> 12:33:17,916 take a use case you're going to build 11599 12:33:17,916 --> 12:33:20,840 account transfer feature in banking 11600 12:33:20,840 --> 12:33:23,200 application we're going to build this 11601 12:33:23,200 --> 12:33:26,880 use case a John has an account in a bank 11602 12:33:26,880 --> 12:33:29,560 and he want to he want to transfer the 11603 12:33:29,560 --> 12:33:33,640 $100 to Sam account so what would be the 11604 12:33:33,640 --> 12:33:36,720 process you have you have to implement 11605 12:33:36,720 --> 12:33:39,840 as a developer you have to update the J 11606 12:33:39,840 --> 12:33:43,000 account or deduct the $10000 from a Jone 11607 12:33:43,000 --> 12:33:45,480 account and then you have to check the 11608 12:33:45,480 --> 12:33:48,400 balance of JN account if balance is less 11609 12:33:48,400 --> 12:33:50,240 than zero you have to roll back the 11610 12:33:50,240 --> 12:33:53,080 transaction the operation should not be 11611 12:33:53,080 --> 12:33:54,880 continue 11612 12:33:54,880 --> 12:34:00,240 so this query result is dependent on 11613 12:34:00,240 --> 12:34:04,276 this query result you can see if any of 11614 12:34:04,276 --> 12:34:07,040 the step fails operation will be 11615 12:34:07,040 --> 12:34:09,800 discarded if there is an error occurs 11616 12:34:09,800 --> 12:34:13,840 here it will roll back the 11617 12:34:15,880 --> 12:34:19,080 transaction so first of all we need 11618 12:34:19,080 --> 12:34:22,400 account model we will have these 11619 12:34:22,400 --> 12:34:24,200 properties 11620 12:34:24,200 --> 12:34:28,276 ID balance and the title then we're 11621 12:34:28,276 --> 12:34:31,880 going to generate migration and resource 11622 12:34:31,880 --> 12:34:32,800 for 11623 12:34:32,800 --> 12:34:36,160 accounts we will inject Prisma service 11624 12:34:36,160 --> 12:34:38,800 here I will use Prisma account create 11625 12:34:38,800 --> 12:34:40,436 input 11626 12:34:40,436 --> 12:34:43,040 and I'm also going to update in the 11627 12:34:43,040 --> 12:34:45,840 controller we we're going to create 11628 12:34:45,840 --> 12:34:49,160 first account John then we will create 11629 12:34:49,160 --> 12:34:51,200 second account which is 11630 12:34:51,200 --> 12:34:54,720 Sam and we will implement the logic to 11631 12:34:54,720 --> 12:34:57,680 transfer amount to One account to 11632 12:34:57,680 --> 12:34:59,480 another 11633 12:34:59,480 --> 12:35:02,200 account I'm going to create a route 11634 12:35:02,200 --> 12:35:03,520 Handler for 11635 12:35:03,520 --> 12:35:07,160 transfer we will need transfer account 11636 12:35:07,160 --> 12:35:10,116 dto because I cannot use from from 11637 12:35:10,116 --> 12:35:11,880 Prisma 11638 12:35:11,880 --> 12:35:16,480 types there is no specific type for 11639 12:35:16,480 --> 12:35:19,116 transfer account I have to use the 11640 12:35:19,116 --> 12:35:23,200 custom data transfer object here when I 11641 12:35:23,200 --> 12:35:26,756 will transfer I will send the transfer 11642 12:35:26,756 --> 12:35:30,160 account request you need to provide 11643 12:35:30,160 --> 12:35:33,840 sender ID receiver ID and the amount you 11644 12:35:33,840 --> 12:35:35,360 want a 11645 12:35:35,360 --> 12:35:37,800 transfer so we will create a simple 11646 12:35:37,800 --> 12:35:40,680 basic transfer from transer method 11647 12:35:40,680 --> 12:35:44,240 inside the account service here is the 11648 12:35:44,240 --> 12:35:47,560 logic from the controller I will get the 11649 12:35:47,560 --> 12:35:50,116 sender and I have renamed to 11650 12:35:50,116 --> 12:35:53,116 from here I will have receiver I have 11651 12:35:53,116 --> 12:35:56,596 renamed to two and I will get the amount 11652 12:35:56,596 --> 12:35:58,756 and first we will 11653 12:35:58,756 --> 12:36:02,680 deduct the balance from the John account 11654 12:36:02,680 --> 12:36:05,840 by getting by getting the account on the 11655 12:36:05,840 --> 12:36:09,756 Bas ID here decrement the amount from 11656 12:36:09,756 --> 12:36:13,756 the John account then we try to verify 11657 12:36:13,756 --> 12:36:16,360 the balance of the John if balance is 11658 12:36:16,360 --> 12:36:18,956 less than zero we have to send the error 11659 12:36:18,956 --> 12:36:21,596 back and we have to roll back the 11660 12:36:21,596 --> 12:36:23,956 transaction here we here we got to roll 11661 12:36:23,956 --> 12:36:26,360 back the transaction but this 11662 12:36:26,360 --> 12:36:29,916 transaction object will do for us and 11663 12:36:29,916 --> 12:36:33,240 this transaction function it will take 11664 12:36:33,240 --> 12:36:35,840 all it will take it's a it's a call it's 11665 12:36:35,840 --> 12:36:38,680 a asynchronous operation it it's going 11666 12:36:38,680 --> 12:36:41,520 to take TX 11667 12:36:41,640 --> 12:36:44,200 so first this is the asynchronous 11668 12:36:44,200 --> 12:36:47,480 operation if error comes please scroll 11669 12:36:47,480 --> 12:36:50,080 back the transaction do not deduct any 11670 12:36:50,080 --> 12:36:53,040 amount from the John account and here is 11671 12:36:53,040 --> 12:36:54,840 our second 11672 12:36:54,840 --> 12:36:57,840 operation if everything is fine we got 11673 12:36:57,840 --> 12:37:00,916 to add balance to the same account and 11674 12:37:00,916 --> 12:37:04,240 finally we have to return the balance or 11675 12:37:04,240 --> 12:37:06,720 return the 11676 12:37:07,200 --> 12:37:09,916 recipient that's our trans transer 11677 12:37:09,916 --> 12:37:12,436 process this is how you can test you 11678 12:37:12,436 --> 12:37:14,880 have to provide the sender ID receiver 11679 12:37:14,880 --> 12:37:17,956 ID and amount you want to transfer here 11680 12:37:17,956 --> 12:37:20,116 I'm saying I want to transfer $50 from 11681 12:37:20,116 --> 12:37:23,040 join account to Sam account and here I 11682 12:37:23,040 --> 12:37:26,320 want to transfer $40 from J account to 11683 12:37:26,320 --> 12:37:29,640 Sam account we created both account with 11684 12:37:29,640 --> 12:37:34,116 $100 now if I try to send $20 now John 11685 12:37:34,116 --> 12:37:37,360 will have $10 in account in his account 11686 12:37:37,360 --> 12:37:41,320 if I try to send or deduct the $20 from 11687 12:37:41,320 --> 12:37:42,240 Jo 11688 12:37:42,240 --> 12:37:44,560 account you will receive this error 11689 12:37:44,560 --> 12:37:47,596 balance is less than zero and our 11690 12:37:47,596 --> 12:37:50,956 transaction will automatically roll back 11691 12:37:50,956 --> 12:37:53,320 this operation that's how the 11692 12:37:53,320 --> 12:37:55,800 transaction is helpful when your one 11693 12:37:55,800 --> 12:37:59,080 query result is dependent on another if 11694 12:37:59,080 --> 12:38:00,916 you want to implement it you can do it 11695 12:38:00,916 --> 12:38:04,160 on your own but let me do that for 11696 12:38:04,160 --> 12:38:07,160 you first of all we got to create a new 11697 12:38:07,160 --> 12:38:11,240 model inside the schema file 11698 12:38:11,240 --> 12:38:13,840 I have already done that 11699 12:38:13,840 --> 12:38:18,200 model oh I have to implement 11700 12:38:18,200 --> 12:38:23,360 here interactive transaction op schema. 11701 12:38:23,360 --> 12:38:26,160 Prisma and we have to 11702 12:38:26,160 --> 12:38:28,276 add a new 11703 12:38:28,276 --> 12:38:33,000 model finally we got to run the 11704 12:38:38,360 --> 12:38:41,160 migration 11705 12:38:41,160 --> 12:38:44,520 migrate not a 11706 12:38:44,520 --> 12:38:50,160 migrate also have to update here M 11707 12:38:52,116 --> 12:38:57,680 crate our next step is to generate the 11708 12:39:08,320 --> 12:39:11,320 accounts 11709 12:39:22,640 --> 12:39:25,596 select the rest 11710 12:39:26,320 --> 12:39:28,596 API let me 11711 12:39:28,596 --> 12:39:31,596 delete 11712 12:39:33,756 --> 12:39:36,436 entities and we have to register a 11713 12:39:36,436 --> 12:39:40,840 Prisma service inside the account module 11714 12:39:40,840 --> 12:39:45,400 I can register as a provider Prisma 11715 12:39:46,200 --> 12:39:51,436 service and we have to import Prisma 11716 12:39:51,436 --> 12:39:54,680 service Prisma service 11717 12:39:54,680 --> 12:39:58,436 from Prisma 11718 12:39:59,360 --> 12:40:04,956 service let's inject Prisma service 11719 12:40:04,956 --> 12:40:07,200 inside the account 11720 12:40:07,200 --> 12:40:11,640 service I'm going to do dependent see 11721 12:40:11,840 --> 12:40:15,360 injection I have to copy this 11722 12:40:15,360 --> 12:40:18,200 line and I'm going to paste it here I 11723 12:40:18,200 --> 12:40:21,880 also need to get Prisma client so I 11724 12:40:21,880 --> 12:40:25,116 imported the Prisma 11725 12:40:26,756 --> 12:40:31,880 client let's rename or add a logic to 11726 12:40:31,880 --> 12:40:34,800 create a new 11727 12:40:35,840 --> 12:40:38,640 account we got to do the same stuff 11728 12:40:38,640 --> 12:40:41,916 inside the account 11729 12:40:47,436 --> 12:40:49,540 controller import 11730 12:40:49,540 --> 12:40:52,690 [Music] 11731 12:40:57,680 --> 12:41:00,956 Prisma let's run the application and 11732 12:41:00,956 --> 12:41:06,000 create two accounts in our database for 11733 12:41:08,240 --> 12:41:11,240 testing 11734 12:41:38,160 --> 12:41:41,160 e 11735 12:41:50,200 --> 12:41:53,360 application is running now I'm going to 11736 12:41:53,360 --> 12:41:56,040 create a new account let me send the 11737 12:41:56,040 --> 12:41:59,640 request for account 11738 12:42:02,436 --> 12:42:07,840 creation board should be 3,000 not 11739 12:42:08,116 --> 12:42:11,116 3001 11740 12:42:14,320 --> 12:42:17,240 so I have created the account John now 11741 12:42:17,240 --> 12:42:19,840 we have to create another account with 11742 12:42:19,840 --> 12:42:23,000 Sam name account has 11743 12:42:23,000 --> 12:42:26,240 created let's implement the 11744 12:42:26,240 --> 12:42:29,480 transfer account logic transfer amount 11745 12:42:29,480 --> 12:42:32,596 not an account transfer amount 11746 12:42:32,596 --> 12:42:36,160 logic open the account controller we 11747 12:42:36,160 --> 12:42:39,956 have to register a new route for 11748 12:42:39,956 --> 12:42:41,596 transfer 11749 12:42:41,596 --> 12:42:47,680 amount I'm going to create a post method 11750 12:42:47,680 --> 12:42:51,360 we don't have transfer dto let's create 11751 12:42:51,360 --> 12:42:56,800 that transfer dto in our dto 11752 12:42:56,800 --> 12:42:59,800 accounts 11753 12:43:03,116 --> 12:43:06,116 dto 11754 12:43:06,360 --> 12:43:08,040 transfer 11755 12:43:08,040 --> 12:43:11,680 account - 11756 12:43:14,040 --> 12:43:18,040 D.S so here we have transfer account dto 11757 12:43:18,040 --> 12:43:21,436 we also don't have a method 11758 12:43:21,436 --> 12:43:25,400 yet inside the service account service 11759 12:43:25,400 --> 12:43:26,680 so I can 11760 12:43:26,680 --> 12:43:29,040 open account 11761 12:43:29,040 --> 12:43:33,116 controller let me import transfer dto we 11762 12:43:33,116 --> 12:43:37,680 don't have method I can use the vs code 11763 12:43:37,680 --> 12:43:40,116 please declare the 11764 12:43:40,116 --> 12:43:44,436 method inside the account 11765 12:43:44,436 --> 12:43:48,160 service now you will have a method 11766 12:43:48,160 --> 12:43:51,400 inside the account 11767 12:43:53,640 --> 12:43:58,080 service this is the transfer 11768 12:44:00,200 --> 12:44:03,240 method so here we're going to implement 11769 12:44:03,240 --> 12:44:07,560 our logic to transfer the account from 11770 12:44:07,560 --> 12:44:10,480 John to Sam I'm going to copy the 11771 12:44:10,480 --> 12:44:13,720 complete op complete function I'm going 11772 12:44:13,720 --> 12:44:16,080 to paste it 11773 12:44:16,080 --> 12:44:19,756 here so everything is good to go now we 11774 12:44:19,756 --> 12:44:21,800 have to test the 11775 12:44:21,800 --> 12:44:28,480 process let's send the transfer amount 11776 12:44:28,640 --> 12:44:32,200 request it should be 11777 12:44:37,956 --> 12:44:40,956 3,000 11778 12:44:42,640 --> 12:44:47,160 let's deduct the $50 from John account 11779 12:44:47,160 --> 12:44:50,480 to Sam account I'm going to deduct $50 11780 12:44:50,480 --> 12:44:52,880 from John now you can see the balance of 11781 12:44:52,880 --> 12:44:53,756 the 11782 12:44:53,756 --> 12:44:57,520 Sam if I deduct the $40 11783 12:44:57,520 --> 12:45:00,520 again let's use it 11784 12:45:00,520 --> 12:45:05,436 here so now the Sam has 190 and the John 11785 12:45:05,436 --> 12:45:06,756 has 11786 12:45:06,756 --> 12:45:10,436 $10 if I try to 11787 12:45:10,436 --> 12:45:13,160 uh deduct $20 from John right now the 11788 12:45:13,160 --> 12:45:15,360 balance of John is 10 it should give me 11789 12:45:15,360 --> 12:45:16,916 the 11790 12:45:16,916 --> 12:45:19,756 error you can see I got the error one 11791 12:45:19,756 --> 12:45:22,880 does not have one the ID sender sender 11792 12:45:22,880 --> 12:45:27,276 ID with one does not have enough to send 11793 12:45:27,276 --> 12:45:30,720 $20 so I got the 11794 12:45:30,720 --> 12:45:35,596 eror so I got the error here my 11795 12:45:35,596 --> 12:45:38,756 transaction function automatically roll 11796 12:45:38,756 --> 12:45:41,276 back back this 11797 12:45:41,276 --> 12:45:46,596 operation that's why transaction is very 11798 12:45:46,596 --> 12:45:51,276 helpful if you are doing 1020 operations 11799 12:45:51,276 --> 12:45:52,916 or 1020 11800 12:45:52,916 --> 12:45:55,640 queries your one query result is 11801 12:45:55,640 --> 12:45:57,680 dependent on another another query 11802 12:45:57,680 --> 12:46:00,080 result is dependent on another let's 11803 12:46:00,080 --> 12:46:02,200 imagine after 10th query there is an 11804 12:46:02,200 --> 12:46:05,000 error then the transaction will do the 11805 12:46:05,000 --> 12:46:07,840 roll back for 11806 12:46:07,840 --> 12:46:10,520 you 11807 12:46:10,520 --> 12:46:12,800 in this lesson I'm going to teach you 11808 12:46:12,800 --> 12:46:16,240 how to upload the file in 11809 12:46:16,240 --> 12:46:21,000 njs nestjs has already created a molter 11810 12:46:21,000 --> 12:46:23,520 module if you have used molter in 11811 12:46:23,520 --> 12:46:26,800 Express we're going to use the same 11812 12:46:26,800 --> 12:46:29,800 logic they have created the molter 11813 12:46:29,800 --> 12:46:33,840 module they also have nests platform 11814 12:46:33,840 --> 12:46:35,520 Express to 11815 12:46:35,520 --> 12:46:39,720 implement file upload feature so this 11816 12:46:39,720 --> 12:46:42,276 package provides the typescript typing 11817 12:46:42,276 --> 12:46:45,720 to work with multer package which is a 11818 12:46:45,720 --> 12:46:50,880 middleware for handling multiart form 11819 12:46:50,880 --> 12:46:54,116 data and then we will create our route 11820 12:46:54,116 --> 12:46:55,436 Handler for 11821 12:46:55,436 --> 12:46:58,320 update they have we're going to use the 11822 12:46:58,320 --> 12:47:02,040 Interceptor that's why I use the use use 11823 12:47:02,040 --> 12:47:03,360 Interceptor 11824 12:47:03,360 --> 12:47:06,560 decator here we have file 11825 12:47:06,560 --> 12:47:09,360 Interceptor they have already created 11826 12:47:09,360 --> 12:47:12,160 this file Interceptor and you got to 11827 12:47:12,160 --> 12:47:15,756 provide the name of your file and here 11828 12:47:15,756 --> 12:47:18,320 we will have the upload file decorator 11829 12:47:18,320 --> 12:47:21,400 the type should be express. multer do 11830 12:47:21,400 --> 12:47:25,200 file that's it then it will log the file 11831 12:47:25,200 --> 12:47:27,640 when you send the API request from Front 11832 12:47:27,640 --> 12:47:30,480 End application or Postman to upload a 11833 12:47:30,480 --> 12:47:33,320 file it will receive the 11834 12:47:33,320 --> 12:47:36,800 file so this decorator indicates that 11835 12:47:36,800 --> 12:47:39,116 the method should use the f file 11836 12:47:39,116 --> 12:47:40,560 Interceptor 11837 12:47:40,560 --> 12:47:44,160 Interceptor to handle the file uploads 11838 12:47:44,160 --> 12:47:46,040 the string file inside the file 11839 12:47:46,040 --> 12:47:48,560 Interceptor decorator refers to the 11840 12:47:48,560 --> 12:47:51,916 field name in the request 11841 12:47:55,000 --> 12:47:57,800 body now we're going to implement this 11842 12:47:57,800 --> 12:48:00,320 file upload 11843 12:48:00,320 --> 12:48:03,160 method first of all we have to install 11844 12:48:03,160 --> 12:48:05,956 the molter 11845 12:48:07,720 --> 12:48:11,520 type I've already attached the started 11846 12:48:11,520 --> 12:48:14,640 kit project or if you don't have an sjs 11847 12:48:14,640 --> 12:48:16,480 project you can create a brand new 11848 12:48:16,480 --> 12:48:19,680 project and start from them start from 11849 12:48:19,680 --> 12:48:22,160 there we have 11850 12:48:22,160 --> 12:48:25,596 installed uh package now I'm going to 11851 12:48:25,596 --> 12:48:28,000 copy the same 11852 12:48:28,000 --> 12:48:31,916 code and I'm going to do it here inside 11853 12:48:31,916 --> 12:48:34,520 the app 11854 12:48:37,720 --> 12:48:40,720 controller 11855 12:48:43,680 --> 12:48:46,800 we also need to import file Interceptor 11856 12:48:46,800 --> 12:48:49,436 from nestjs platform 11857 12:48:49,436 --> 12:48:53,360 Express whenever you need to upload the 11858 12:48:53,360 --> 12:48:56,800 file you have to use the use Interceptor 11859 12:48:56,800 --> 12:48:58,680 and they have created the file 11860 12:48:58,680 --> 12:49:02,320 Interceptor the name of the file here 11861 12:49:02,320 --> 12:49:06,880 you will provide the molter options when 11862 12:49:06,880 --> 12:49:09,640 you worked with molon if you already 11863 12:49:09,640 --> 12:49:13,480 work with molter opt molter package 11864 12:49:13,480 --> 12:49:17,000 these are the options are same these 11865 12:49:17,000 --> 12:49:18,640 options are same when you work with 11866 12:49:18,640 --> 12:49:19,880 molter 11867 12:49:19,880 --> 12:49:23,596 package so I'm not going to do it 11868 12:49:23,596 --> 12:49:26,916 here and we will have upload file now we 11869 12:49:26,916 --> 12:49:30,080 need to test it I'm going to start the 11870 12:49:30,080 --> 12:49:32,956 project we need a postman to send the 11871 12:49:32,956 --> 12:49:35,116 API 11872 12:49:35,116 --> 12:49:37,640 request in terms of 11873 12:49:37,640 --> 12:49:40,640 file 11874 12:49:45,680 --> 12:49:47,800 so here I'm going to send 11875 12:49:47,800 --> 12:49:51,436 request to the upload and we have to 11876 12:49:51,436 --> 12:49:52,680 provide the 11877 12:49:52,680 --> 12:49:57,360 file select the body and you got to 11878 12:49:57,360 --> 12:50:01,436 provide file you can select the file 11879 12:50:01,436 --> 12:50:06,240 from here a body and a file and then you 11880 12:50:06,240 --> 12:50:10,640 have to select the file 11881 12:50:12,640 --> 12:50:15,640 I'm going to use this PNG file you can 11882 12:50:15,640 --> 12:50:18,916 choose any 11883 12:50:19,320 --> 12:50:22,800 file B request I have to remove this one 11884 12:50:22,800 --> 12:50:25,520 now I'm going to send request you can 11885 12:50:25,520 --> 12:50:29,200 see a file has created successfully 11886 12:50:29,200 --> 12:50:31,480 actually uploaded this is the original 11887 12:50:31,480 --> 12:50:35,080 name this is the field file but I want 11888 12:50:35,080 --> 12:50:39,000 to save this file some somewhere in my 11889 12:50:39,000 --> 12:50:42,160 folder you can create a disc folder and 11890 12:50:42,160 --> 12:50:46,956 you can save save that save there to 11891 12:50:46,956 --> 12:50:49,276 implement this logic you going to 11892 12:50:49,276 --> 12:50:52,596 provide these two 11893 12:51:05,200 --> 12:51:07,756 options actually I'm going to copy the 11894 12:51:07,756 --> 12:51:10,000 complete 11895 12:51:10,000 --> 12:51:14,840 file intercept use 11896 12:51:15,640 --> 12:51:18,116 intercept I'm going to copy the complete 11897 12:51:18,116 --> 12:51:20,720 use Interceptor now it looks 11898 12:51:20,720 --> 12:51:24,916 good so we have to import this package 11899 12:51:24,916 --> 12:51:26,956 from disk 11900 12:51:26,956 --> 12:51:29,916 storage I think I can get that from 11901 12:51:29,916 --> 12:51:32,640 molter 11902 12:51:33,756 --> 12:51:36,756 package 11903 12:51:36,956 --> 12:51:40,240 molter disktop 11904 12:51:40,240 --> 12:51:44,240 storage now it looks 11905 12:51:46,240 --> 12:51:50,400 good so it will create the upload folder 11906 12:51:50,400 --> 12:51:55,916 for me and here I will have the files 11907 12:51:55,916 --> 12:51:58,800 folder now we're going to send the 11908 12:51:58,800 --> 12:52:02,320 request to save upload the 11909 12:52:02,320 --> 12:52:06,240 file and you can see there is a file 11910 12:52:06,240 --> 12:52:09,116 inside the upload folder and inside the 11911 12:52:09,116 --> 12:52:12,880 files we have create song mutation. PNG 11912 12:52:12,880 --> 12:52:18,080 file you can also add a 11913 12:52:19,080 --> 12:52:22,000 validation let's say you can 11914 12:52:22,000 --> 12:52:25,756 say I only want 11915 12:52:25,756 --> 12:52:29,560 to accept PNG 11916 12:52:29,560 --> 12:52:32,800 file how can you add a 11917 12:52:32,800 --> 12:52:36,596 validation I can say upload 11918 12:52:36,596 --> 12:52:41,350 PNG you can do the validation upload PNG 11919 12:52:41,350 --> 12:52:43,480 [Music] 11920 12:52:43,480 --> 12:52:46,400 file you can implement the validation 11921 12:52:46,400 --> 12:52:50,680 here let me copy the validation 11922 12:52:50,756 --> 12:52:54,480 logic they provided the pars file pip 11923 12:52:54,480 --> 12:52:57,916 Builder what this pipe Builder will do 11924 12:52:57,916 --> 12:53:01,640 this is a custom utility that creates a 11925 12:53:01,640 --> 12:53:06,040 pipe for validating uploaded files if 11926 12:53:06,040 --> 12:53:08,480 you want to add a maximum size you can 11927 12:53:08,480 --> 12:53:10,480 can accept you can also do that but I'm 11928 12:53:10,480 --> 12:53:12,916 not going to implement 11929 12:53:12,916 --> 12:53:16,756 that we need this 11930 12:53:16,756 --> 12:53:21,116 logic inside the upload 11931 12:53:25,560 --> 12:53:28,560 file 11932 12:53:31,800 --> 12:53:35,956 here I also added an error status code 11933 12:53:35,956 --> 12:53:38,680 if error comes it is going to log this 11934 12:53:38,680 --> 12:53:39,720 error 11935 12:53:39,720 --> 12:53:42,880 message unprocessable 11936 12:53:42,880 --> 12:53:46,680 entity now it can accept only the PNG 11937 12:53:46,680 --> 12:53:51,916 file type I've applied the pipe to add 11938 12:53:51,916 --> 12:53:55,956 PNG validation let's test it 11939 12:53:55,956 --> 12:53:59,680 out if I add upload 11940 12:53:59,680 --> 12:54:05,200 PNG if I add invalid file what will 11941 12:54:05,200 --> 12:54:10,320 happen I'm going to add jpack 11942 12:54:11,040 --> 12:54:13,520 I got the 11943 12:54:13,520 --> 12:54:17,520 error oh upload Das 11944 12:54:17,560 --> 12:54:21,276 PNG I got the error for double to 11945 12:54:21,276 --> 12:54:24,276 unprocessable 11946 12:54:31,000 --> 12:54:34,040 entity if I provide the valid file which 11947 12:54:34,040 --> 12:54:35,560 is 11948 12:54:35,560 --> 12:54:40,320 PNG what will happen 11949 12:54:47,360 --> 12:54:50,160 can get that from 11950 12:54:50,160 --> 12:54:53,520 here this is the PNG 11951 12:54:53,520 --> 12:54:56,320 file now the file has uploaded 11952 12:54:56,320 --> 12:54:59,200 successfully 11953 12:55:07,240 --> 12:55:10,240 here 11954 12:55:12,800 --> 12:55:15,276 one more thing we got to return the 11955 12:55:15,276 --> 12:55:18,320 message I forgot to return the 11956 12:55:18,320 --> 12:55:19,916 message 11957 12:55:19,916 --> 12:55:22,916 from here 11958 12:55:22,916 --> 12:55:26,200 to also need to upload the 11959 12:55:26,200 --> 12:55:30,240 message file uploaded 11960 12:55:35,756 --> 12:55:40,240 successfully let's test it out 11961 12:55:54,240 --> 12:55:58,080 out this is the PNG 11962 12:55:58,080 --> 12:56:02,276 file you should see the 11963 12:56:02,520 --> 12:56:06,840 message file uploaded 11964 12:56:06,840 --> 12:56:09,200 successfully and you you will see your 11965 12:56:09,200 --> 12:56:11,756 file 11966 12:56:12,956 --> 12:56:15,956 here in this lesson I'm going to teach 11967 12:56:15,956 --> 12:56:20,276 you the concept of custom 11968 12:56:20,276 --> 12:56:22,800 decorator if you want to add additional 11969 12:56:22,800 --> 12:56:25,720 functionality to a class to a method you 11970 12:56:25,720 --> 12:56:29,000 can use custom decorator just like we 11971 12:56:29,000 --> 12:56:32,040 did with app module app controller we 11972 12:56:32,040 --> 12:56:35,680 use decorator a lot when you look at the 11973 12:56:35,680 --> 12:56:40,240 app module here we have used a decorator 11974 12:56:40,240 --> 12:56:43,640 this module decorator in the controller 11975 12:56:43,640 --> 12:56:46,956 app. controller we have controller 11976 12:56:46,956 --> 12:56:49,640 decorator it mean this decorator is 11977 12:56:49,640 --> 12:56:52,596 adding an additional functionality to 11978 12:56:52,596 --> 12:56:54,000 the app 11979 12:56:54,000 --> 12:56:58,000 controller like they are adding 11980 12:56:58,000 --> 12:57:02,080 metadata this controller can have 11981 12:57:02,080 --> 12:57:06,916 functions attributes all of the 11982 12:57:07,116 --> 12:57:09,400 stuff 11983 12:57:09,400 --> 12:57:13,080 so you can improve your modularity and 11984 12:57:13,080 --> 12:57:15,756 reusability with the help of custom 11985 12:57:15,756 --> 12:57:20,276 decorator you can organize code very 11986 12:57:20,596 --> 12:57:23,840 easily you can extend Nest functionality 11987 12:57:23,840 --> 12:57:27,680 by adding custom 11988 12:57:27,720 --> 12:57:30,800 decorator here we're going to use custom 11989 12:57:30,800 --> 12:57:33,840 decorator I have a basic simple example 11990 12:57:33,840 --> 12:57:35,680 we're going to create our own custom 11991 12:57:35,680 --> 12:57:38,840 decorator the user decorator. 11992 12:57:38,840 --> 12:57:43,080 this you use pram decorator so I can 11993 12:57:43,080 --> 12:57:45,116 apply it on a 11994 12:57:45,116 --> 12:57:48,200 parameter and I'm using a function this 11995 12:57:48,200 --> 12:57:50,720 function is accepting to argument data 11996 12:57:50,720 --> 12:57:53,240 it should be unknown type and we are 11997 12:57:53,240 --> 12:57:55,880 getting the execution 11998 12:57:55,880 --> 12:57:58,956 context and I can get the request from 11999 12:57:58,956 --> 12:58:02,200 this execution context here I'm am 12000 12:58:02,200 --> 12:58:06,436 adding a user into the request. user 12001 12:58:06,436 --> 12:58:08,720 when you have implemented auth tication 12002 12:58:08,720 --> 12:58:11,360 you can create a custom decorator for a 12003 12:58:11,360 --> 12:58:14,680 user imagine you can get 12004 12:58:14,680 --> 12:58:18,400 this request. user from a Json web token 12005 12:58:18,400 --> 12:58:21,640 and Json web token Parts the token and 12006 12:58:21,640 --> 12:58:24,840 found the user you can imagine that but 12007 12:58:24,840 --> 12:58:29,400 I hardcoded it and return request. 12008 12:58:29,400 --> 12:58:32,240 user yeah this is The Decorator Factory 12009 12:58:32,240 --> 12:58:35,160 function it takes to parameter data and 12010 12:58:35,160 --> 12:58:37,040 the Conta execution 12011 12:58:37,040 --> 12:58:38,956 context 12012 12:58:38,956 --> 12:58:40,800 and now we're going to create a new user 12013 12:58:40,800 --> 12:58:44,040 entity here we have applied a user 12014 12:58:44,040 --> 12:58:46,640 decorator let's say this is this is 12015 12:58:46,640 --> 12:58:49,916 authenticated route only authenticated 12016 12:58:49,916 --> 12:58:53,116 user can access this route so I would 12017 12:58:53,116 --> 12:58:55,560 like to get the user so you can use a 12018 12:58:55,560 --> 12:58:56,560 user 12019 12:58:56,560 --> 12:58:59,360 decorator and the user entity this user 12020 12:58:59,360 --> 12:59:01,880 decorator is going to execute this 12021 12:59:01,880 --> 12:59:04,360 function and this function will be 12022 12:59:04,360 --> 12:59:07,480 executed and it is going to add a user 12023 12:59:07,480 --> 12:59:11,000 in the request EST object that's how you 12024 12:59:11,000 --> 12:59:15,160 can implement the custom decorator let's 12025 12:59:15,160 --> 12:59:22,560 create a new file here I can say a user 12026 12:59:22,560 --> 12:59:25,560 decorator 12027 12:59:26,756 --> 12:59:29,436 decorator. I'm going to copy my 12028 12:59:29,436 --> 12:59:32,040 code user 12029 12:59:32,040 --> 12:59:36,480 decorator you can use any name 12030 12:59:36,956 --> 12:59:40,520 here I also need to create another 12031 12:59:40,520 --> 12:59:45,596 entity this time I'm going to call user. 12032 12:59:45,800 --> 12:59:48,800 entity 12033 12:59:49,320 --> 12:59:52,240 TS now we're going to apply The 12034 12:59:52,240 --> 12:59:55,436 Decorator on a 12035 12:59:55,436 --> 12:59:58,916 user do I have a route I can create the 12036 12:59:58,916 --> 13:00:03,640 complete route to implement this user 12037 13:00:05,520 --> 13:00:11,080 decorator copy this and and paste it 12038 13:00:11,116 --> 13:00:16,596 here and we need these two 12039 13:00:22,436 --> 13:00:27,040 Imports now we have to test our 12040 13:00:27,240 --> 13:00:30,880 application so whenever you need to get 12041 13:00:30,880 --> 13:00:34,320 a user inside any function all you need 12042 13:00:34,320 --> 13:00:39,000 to do is to apply the decorator 12043 13:00:39,000 --> 13:00:41,756 here you can apply The Decorator upload 12044 13:00:41,756 --> 13:00:45,680 file you can apply decorator here it 12045 13:00:45,680 --> 13:00:49,160 depends on your use 12046 13:00:50,240 --> 13:00:54,720 case so my application is 12047 13:00:59,040 --> 13:01:02,756 running I'm going to create a new HTTP 12048 13:01:02,756 --> 13:01:06,520 client. HTTP file to send API 12049 13:01:06,520 --> 13:01:10,596 request can say that find 12050 13:01:10,596 --> 13:01:14,596 user only based on 12051 13:01:14,596 --> 13:01:18,956 ID we have to send the get 12052 13:01:19,560 --> 13:01:25,160 request 3,000 slash user and you got to 12053 13:01:25,160 --> 13:01:28,320 provide a user 12054 13:01:31,640 --> 13:01:37,000 ID now you can see I got a user so this 12055 13:01:37,000 --> 13:01:40,200 decorator a user decorator is calling 12056 13:01:40,200 --> 13:01:43,320 the user decorator function and this 12057 13:01:43,320 --> 13:01:46,276 function is getting a user or setting a 12058 13:01:46,276 --> 13:01:49,320 user to the request object and returning 12059 13:01:49,320 --> 13:01:51,520 the 12060 13:01:51,680 --> 13:01:55,000 request get decorator is nothing it's 12061 13:01:55,000 --> 13:01:57,796 just like a function here in our 12062 13:01:57,796 --> 13:02:02,320 case I'm running a function 12063 13:02:02,320 --> 13:02:04,916 here uh in this lesson I'm going to 12064 13:02:04,916 --> 13:02:07,116 teach you if you want to schedule any 12065 13:02:07,116 --> 13:02:10,040 method or any function you can do with 12066 13:02:10,040 --> 13:02:12,240 grown 12067 13:02:12,240 --> 13:02:15,436 task uh let's uh if you want to schedule 12068 13:02:15,436 --> 13:02:18,880 any task let's say you want to run you 12069 13:02:18,880 --> 13:02:20,000 want to 12070 13:02:20,000 --> 13:02:23,916 calculate the expiry date of a license 12071 13:02:23,916 --> 13:02:27,436 let's say your trial version is going to 12072 13:02:27,436 --> 13:02:31,956 be expired after 7 days You're Building 12073 13:02:31,956 --> 13:02:34,756 this type of SAS application you your 12074 13:02:34,756 --> 13:02:38,480 company provides 14 days free trial 12075 13:02:38,480 --> 13:02:41,240 and the trial version will be expired 12076 13:02:41,240 --> 13:02:42,720 after 14 12077 13:02:42,720 --> 13:02:46,276 days what your application will do under 12078 13:02:46,276 --> 13:02:50,276 the hood application will run a crown 12079 13:02:50,276 --> 13:02:54,276 task it will execute every day in try to 12080 13:02:54,276 --> 13:02:57,040 find out the expiry date for the current 12081 13:02:57,040 --> 13:03:01,720 user if user has used software for 14 12082 13:03:01,720 --> 13:03:05,796 days your Chown to will determine that 12083 13:03:05,796 --> 13:03:07,796 and it will send the notification to the 12084 13:03:07,796 --> 13:03:11,200 user into your application hey this user 12085 13:03:11,200 --> 13:03:14,520 has used 14 days free trial and you may 12086 13:03:14,520 --> 13:03:18,480 must have a logic or a code to handle 12087 13:03:18,480 --> 13:03:19,400 this 12088 13:03:19,400 --> 13:03:21,560 situation maybe you can send the credit 12089 13:03:21,560 --> 13:03:22,956 card information to 12090 13:03:22,956 --> 13:03:26,640 upgrade so nestjs provide a package to 12091 13:03:26,640 --> 13:03:29,596 implement this type of 12092 13:03:29,596 --> 13:03:31,520 functionality we're going to we're going 12093 13:03:31,520 --> 13:03:33,436 to we're going to install a couple of 12094 13:03:33,436 --> 13:03:36,400 packages first you 12095 13:03:36,400 --> 13:03:38,520 need next 12096 13:03:38,520 --> 13:03:41,520 JS 12097 13:03:41,880 --> 13:03:43,956 schedule 12098 13:03:43,956 --> 13:03:49,880 and you also need or I can install it 12099 13:03:53,596 --> 13:03:55,520 here 12100 13:03:55,520 --> 13:03:58,796 types of 12101 13:03:59,560 --> 13:04:03,520 grown actually I need a type as a Chone 12102 13:04:03,520 --> 13:04:05,640 SE as a Dev dependency let's first 12103 13:04:05,640 --> 13:04:09,640 install this package 12104 13:04:15,040 --> 13:04:16,040 now we 12105 13:04:16,040 --> 13:04:19,320 install the 12106 13:04:19,916 --> 13:04:22,720 types or I can use the minus 12107 13:04:22,720 --> 13:04:26,796 t and types 12108 13:04:32,560 --> 13:04:36,640 Chone NJ has provide a schedule 12109 13:04:36,640 --> 13:04:40,880 module from nestjs casual package I have 12110 13:04:40,880 --> 13:04:43,436 to import it if you want to use this 12111 13:04:43,436 --> 13:04:47,916 package let's import the module schedule 12112 13:04:47,916 --> 13:04:51,596 module I have to register it or create 12113 13:04:51,596 --> 13:04:53,796 this 12114 13:04:55,160 --> 13:04:58,640 module this root module will initialize 12115 13:04:58,640 --> 13:05:00,200 this casual 12116 13:05:00,200 --> 13:05:02,956 module and it will register any 12117 13:05:02,956 --> 13:05:06,200 declarative chrone jobs timeouts 12118 13:05:06,200 --> 13:05:09,116 interval that exist assist within your 12119 13:05:09,116 --> 13:05:11,796 application right now we did not Define 12120 13:05:11,796 --> 13:05:15,000 any Crone task if you have defined Crone 12121 13:05:15,000 --> 13:05:18,436 task this for root will initialize all 12122 13:05:18,436 --> 13:05:20,080 of your crown 12123 13:05:20,080 --> 13:05:23,000 task and registration occurs when the 12124 13:05:23,000 --> 13:05:25,000 application when the own application 12125 13:05:25,000 --> 13:05:29,400 bootstrap life cycle hook 12126 13:05:29,400 --> 13:05:34,080 occurs now we're going to declare some 12127 13:05:34,360 --> 13:05:37,200 cras this cronas will run the function 12128 13:05:37,200 --> 13:05:40,436 autom automatically we will specify some 12129 13:05:40,436 --> 13:05:43,596 specific dat time when this Crown task 12130 13:05:43,596 --> 13:05:46,720 will run it depends on your use case do 12131 13:05:46,720 --> 13:05:48,596 you want to run this Crown task every 12132 13:05:48,596 --> 13:05:51,956 week or every day or every 5 minutes or 12133 13:05:51,956 --> 13:05:53,916 every 10 minutes or every hour it 12134 13:05:53,916 --> 13:05:57,880 depends on your use use case now we're 12135 13:05:57,880 --> 13:06:01,520 going to create a new task 12136 13:06:02,480 --> 13:06:06,520 service let's create a new to 12137 13:06:06,520 --> 13:06:09,520 service 12138 13:06:13,560 --> 13:06:17,200 so let's define our first gr task in 12139 13:06:17,200 --> 13:06:19,640 task 12140 13:06:21,360 --> 13:06:26,796 service private read only logger it will 12141 13:06:26,796 --> 13:06:29,040 log some 12142 13:06:29,040 --> 13:06:31,480 message task 12143 13:06:31,480 --> 13:06:34,796 service do 12144 13:06:35,680 --> 13:06:38,436 name if you want to Define a crown task 12145 13:06:38,436 --> 13:06:40,956 you need a decorator in the previous 12146 13:06:40,956 --> 13:06:44,520 video I taught you what are the benefits 12147 13:06:44,520 --> 13:06:47,840 of decorator like I can extend the 12148 13:06:47,840 --> 13:06:49,956 functionality of a function by adding 12149 13:06:49,956 --> 13:06:52,080 the Chone 12150 13:06:52,080 --> 13:06:55,680 decorator here you will provide your 12151 13:06:55,680 --> 13:07:01,560 time path time date something like 12152 13:07:01,560 --> 13:07:05,756 that so start start 12153 13:07:05,756 --> 13:07:09,160 start you can learn learn more 12154 13:07:09,160 --> 13:07:14,756 about this strategy by looking at Crown 12155 13:07:15,160 --> 13:07:20,796 task schedu Nest or 12156 13:07:36,436 --> 13:07:39,436 npm 12157 13:07:49,160 --> 13:07:53,040 you can play around with these timing 12158 13:07:53,040 --> 13:07:56,880 settings from this package this package 12159 13:07:56,880 --> 13:07:58,880 you can learn more 12160 13:07:58,880 --> 13:08:01,956 about how can you set the current dat 12161 13:08:01,956 --> 13:08:04,596 stuff like 12162 13:08:05,000 --> 13:08:09,916 that here the first star 12163 13:08:09,916 --> 13:08:13,916 represent second the second star 12164 13:08:13,916 --> 13:08:17,436 represent minute the third star 12165 13:08:17,436 --> 13:08:20,520 represent R and the fourth star 12166 13:08:20,520 --> 13:08:24,080 represent the day of the month and the 12167 13:08:24,080 --> 13:08:26,640 fifth star represent the 12168 13:08:26,640 --> 13:08:29,840 month and the last star represent the 12169 13:08:29,840 --> 13:08:32,240 week 12170 13:08:32,360 --> 13:08:35,276 day did you get that let's say I want to 12171 13:08:35,276 --> 13:08:40,240 run the crown task after every 10 12172 13:08:40,240 --> 13:08:43,000 seconds so what I want to do I want to 12173 13:08:43,000 --> 13:08:49,596 log a simple message after every 10 12174 13:08:49,756 --> 13:08:55,040 seconds and say Myron 12175 13:08:55,680 --> 13:09:02,200 task I'm going to log a message this do 12176 13:09:02,240 --> 13:09:04,640 logger not a 12177 13:09:04,640 --> 13:09:08,796 throw this. logger do 12178 13:09:08,796 --> 13:09:12,040 debug called when the 12179 13:09:12,040 --> 13:09:16,640 current second is 12180 13:09:17,240 --> 13:09:20,276 10 let's try to test the 12181 13:09:20,276 --> 13:09:23,720 application I have registered the 12182 13:09:23,720 --> 13:09:27,480 schedule module here this for will will 12183 13:09:27,480 --> 13:09:31,040 initialize this method and we set up the 12184 13:09:31,040 --> 13:09:34,080 configuration to run after 10 12185 13:09:34,080 --> 13:09:36,320 second let's do 12186 13:09:36,320 --> 13:09:39,320 that 12187 13:09:50,520 --> 13:09:52,916 you can 12188 13:09:53,640 --> 13:10:02,596 see it has run this debug after every 10 12189 13:10:06,240 --> 13:10:09,240 seconds 12190 13:10:14,956 --> 13:10:18,840 what if I change the setting to 12191 13:10:36,240 --> 13:10:38,680 five 12192 13:10:38,680 --> 13:10:44,116 now after 5 Seconds it has 12193 13:11:06,240 --> 13:11:09,240 executed 12194 13:11:36,160 --> 13:11:39,160 e 12195 13:11:39,276 --> 13:11:43,200 actually it is running called when the 12196 13:11:43,200 --> 13:11:47,880 current second is actually it is wait it 12197 13:11:47,880 --> 13:11:52,956 will wait for 1 minute and 5 Seconds 12198 13:11:52,956 --> 13:11:55,756 then it will 12199 13:12:03,000 --> 13:12:09,116 run wait for 1 minute and 5 Seconds 12200 13:12:36,116 --> 13:12:38,240 is 12201 13:12:38,240 --> 13:12:42,400 now you can see at 12202 13:12:42,400 --> 13:12:46,756 519 and then it has ran after a minute 12203 13:12:46,756 --> 13:12:49,400 and on the fifth 12204 13:12:49,400 --> 13:12:54,040 second what if I run this cronas after 12205 13:12:54,040 --> 13:12:54,810 every 12206 13:12:54,810 --> 13:12:57,910 [Music] 12207 13:13:01,596 --> 13:13:04,276 second you can see it's running after 12208 13:13:04,276 --> 13:13:05,400 every 12209 13:13:05,400 --> 13:13:10,596 seconds so I can use 12210 13:13:10,916 --> 13:13:14,360 that called when the 12211 13:13:14,360 --> 13:13:19,160 crun C call that's 12212 13:13:30,840 --> 13:13:36,040 it so it will wait for a minute and a 15 12213 13:13:36,040 --> 13:13:39,040 seconds 12214 13:13:49,956 --> 13:13:52,640 if you want to do a little bit setting 12215 13:13:52,640 --> 13:13:57,400 let's say I want to run every hour at 12216 13:13:57,400 --> 13:14:01,080 the start of five uh at the start of the 12217 13:14:01,080 --> 13:14:02,840 fifth 12218 13:14:02,840 --> 13:14:05,480 minutes but it depends on you how you 12219 13:14:05,480 --> 13:14:09,040 want to run 12220 13:14:12,116 --> 13:14:13,160 this will 12221 13:14:13,160 --> 13:14:14,720 run 12222 13:14:14,720 --> 13:14:17,560 after 12223 13:14:17,560 --> 13:14:20,560 every 12224 13:14:30,840 --> 13:14:33,240 minute 12225 13:14:33,240 --> 13:14:35,956 yep after every minute it's 12226 13:14:35,956 --> 13:14:38,000 running 12227 13:14:38,000 --> 13:14:39,520 this is how you can play around with 12228 13:14:39,520 --> 13:14:41,956 crown 12229 13:14:43,200 --> 13:14:45,796 task in this lesson you're going to 12230 13:14:45,796 --> 13:14:49,276 learn cookies cookies are small pieces 12231 13:14:49,276 --> 13:14:52,916 of data dat a server sends to a user 12232 13:14:52,916 --> 13:14:57,040 browser while the user is browsing a 12233 13:14:57,040 --> 13:15:00,560 website these cookies are stored in user 12234 13:15:00,560 --> 13:15:03,916 device and are used to store information 12235 13:15:03,916 --> 13:15:08,040 about the user interaction with website 12236 13:15:08,040 --> 13:15:11,000 if you look up the Google Chrome you 12237 13:15:11,000 --> 13:15:14,560 will see the developer tools and you can 12238 13:15:14,560 --> 13:15:17,840 find the application 12239 13:15:19,080 --> 13:15:21,840 tab here you will find all of your 12240 13:15:21,840 --> 13:15:25,080 cookies if you have 12241 13:15:26,000 --> 13:15:30,040 stored we have only single cookie you 12242 13:15:30,040 --> 13:15:31,956 can Implement also cookie based 12243 13:15:31,956 --> 13:15:33,880 authentication just like we did in 12244 13:15:33,880 --> 13:15:37,640 authentication module we saved at 12245 13:15:37,640 --> 13:15:42,276 Json web token into a local 12246 13:15:42,956 --> 13:15:46,000 storage but if you were if you work with 12247 13:15:46,000 --> 13:15:49,160 a local storage or front-end application 12248 13:15:49,160 --> 13:15:52,276 you can store your Json web token in 12249 13:15:52,276 --> 13:15:55,200 this storage but you can also use a 12250 13:15:55,200 --> 13:15:57,796 cookie and you can save your token in 12251 13:15:57,796 --> 13:15:59,116 encrypted 12252 13:15:59,116 --> 13:16:02,560 format so these are all the use cases of 12253 13:16:02,560 --> 13:16:05,840 cookies like most of the time when 12254 13:16:05,840 --> 13:16:11,520 you when you access or fetch or look at 12255 13:16:11,520 --> 13:16:13,956 the same look at the website or when you 12256 13:16:13,956 --> 13:16:16,916 visit the same website again and again 12257 13:16:16,916 --> 13:16:20,720 the next time it tries to fetch the data 12258 13:16:20,720 --> 13:16:23,756 from your cookies it has saved all of 12259 13:16:23,756 --> 13:16:25,040 your 12260 13:16:25,040 --> 13:16:27,840 authentication Lo all of your password 12261 13:16:27,840 --> 13:16:29,880 in cookies when you go to the Google 12262 13:16:29,880 --> 13:16:31,080 password 12263 13:16:31,080 --> 13:16:34,916 manager where all of this data is stored 12264 13:16:34,916 --> 13:16:38,596 all of this data is stored in your cook 12265 13:16:38,596 --> 13:16:42,480 is remember when you look at the history 12266 13:16:42,480 --> 13:16:44,116 or let 12267 13:16:44,116 --> 13:16:46,400 me you look at the 12268 13:16:46,400 --> 13:16:50,000 history if you want to delete all of 12269 13:16:50,000 --> 13:16:54,320 your data it will also ask you hey do 12270 13:16:54,320 --> 13:16:56,360 you want to do you want to delete all of 12271 13:16:56,360 --> 13:16:59,680 your cookie stuff you have data inside 12272 13:16:59,680 --> 13:17:01,880 the 12273 13:17:05,116 --> 13:17:08,680 cookie so you can save any token inside 12274 13:17:08,680 --> 13:17:10,840 the 12275 13:17:10,840 --> 13:17:13,520 cookie let me talk about the use cases 12276 13:17:13,520 --> 13:17:14,320 of 12277 13:17:14,320 --> 13:17:18,680 cookies you can manage user sessions 12278 13:17:18,680 --> 13:17:20,916 let's say if you want to expire a user 12279 13:17:20,916 --> 13:17:25,796 session after 30 days or after 24 12280 13:17:25,796 --> 13:17:29,080 hours you can have that type of logic in 12281 13:17:29,080 --> 13:17:31,480 cookie and this cookie will be stored 12282 13:17:31,480 --> 13:17:33,520 here inside the application and the 12283 13:17:33,520 --> 13:17:34,916 cookies 12284 13:17:34,916 --> 13:17:38,160 tab if you have any type of shopping 12285 13:17:38,160 --> 13:17:40,596 cart application you can also do a 12286 13:17:40,596 --> 13:17:43,080 little bit saving in 12287 13:17:43,080 --> 13:17:47,360 the in the cookies let's say a user has 12288 13:17:47,360 --> 13:17:50,720 uh add added three to four items to the 12289 13:17:50,720 --> 13:17:53,796 checkout and he left the website and 12290 13:17:53,796 --> 13:17:56,840 then you can send user hey you left 12291 13:17:56,840 --> 13:18:00,160 something a user can see all of his 12292 13:18:00,160 --> 13:18:02,756 checkout 12293 13:18:05,720 --> 13:18:08,680 items you can do some type of tracking 12294 13:18:08,680 --> 13:18:11,040 and analytics you can do authentication 12295 13:18:11,040 --> 13:18:12,880 like cookies can help with 12296 13:18:12,880 --> 13:18:15,116 authentication and security by storing 12297 13:18:15,116 --> 13:18:17,880 tokens or other information that 12298 13:18:17,880 --> 13:18:21,160 verifies a user 12299 13:18:21,640 --> 13:18:24,160 identity you can use load balancing 12300 13:18:24,160 --> 13:18:26,756 State Management and cashing stuff with 12301 13:18:26,756 --> 13:18:29,520 cookies now I'm going to implement the 12302 13:18:29,520 --> 13:18:33,080 cookies in NS project you got to install 12303 13:18:33,080 --> 13:18:35,560 these two packages I have already 12304 13:18:35,560 --> 13:18:37,880 installed them 12305 13:18:37,880 --> 13:18:39,200 I've already installed in my 12306 13:18:39,200 --> 13:18:41,756 package.json file this is the cookie 12307 13:18:41,756 --> 13:18:44,680 parser you need and then you also need a 12308 13:18:44,680 --> 13:18:47,200 typing for cookie parser go ahead and 12309 13:18:47,200 --> 13:18:49,720 install 12310 13:18:51,276 --> 13:18:53,840 them all right our next step is to 12311 13:18:53,840 --> 13:18:57,000 register a cookie parts sir in main.ts 12312 13:18:57,000 --> 13:19:00,320 file this is our main entry file here I 12313 13:19:00,320 --> 13:19:03,160 have registered a cookie parser you can 12314 13:19:03,160 --> 13:19:04,480 import it as a 12315 13:19:04,480 --> 13:19:07,560 star and here if you want to add add 12316 13:19:07,560 --> 13:19:09,796 your secret you can do it here or if you 12317 13:19:09,796 --> 13:19:10,720 want to 12318 13:19:10,720 --> 13:19:14,200 add uh some logic or decode something 12319 13:19:14,200 --> 13:19:17,520 like that this is the option from a 12320 13:19:17,520 --> 13:19:22,276 cookie package you can go to cookie 12321 13:19:24,560 --> 13:19:27,880 parser so this package is similar to 12322 13:19:27,880 --> 13:19:30,720 this one this one cookie parser but you 12323 13:19:30,720 --> 13:19:32,596 can look at the documentation what type 12324 13:19:32,596 --> 13:19:35,640 of options you 12325 13:19:35,640 --> 13:19:38,640 have 12326 13:19:40,080 --> 13:19:44,200 now our next step is to set the 12327 13:19:44,200 --> 13:19:46,956 cookie I'm going to create a new route 12328 13:19:46,956 --> 13:19:51,436 to set the cookie and we need a response 12329 13:19:51,436 --> 13:19:54,080 from the nestjs comment and response 12330 13:19:54,080 --> 13:19:57,680 type from the Express package here I'm 12331 13:19:57,680 --> 13:19:59,756 allowing the framework you can set the 12332 13:19:59,756 --> 13:20:02,276 cookie I am I'm going to set the cookie 12333 13:20:02,276 --> 13:20:04,160 cooking token name encrypted cookie 12334 13:20:04,160 --> 13:20:06,360 string something here it's a key value 12335 13:20:06,360 --> 13:20:08,840 PA here and I'm sending a message cookie 12336 13:20:08,840 --> 13:20:09,796 saved 12337 13:20:09,796 --> 13:20:12,680 successfully here here I'm trying to log 12338 13:20:12,680 --> 13:20:15,400 all of your cookies and return cookies 12339 13:20:15,400 --> 13:20:18,596 in the get route now we need to test the 12340 13:20:18,596 --> 13:20:22,720 application when you look the app 12341 13:20:23,680 --> 13:20:25,796 controller here let's say I'm want to 12342 13:20:25,796 --> 13:20:27,240 set a new 12343 13:20:27,240 --> 13:20:30,200 cookie I've already saved that cookie in 12344 13:20:30,200 --> 13:20:32,000 my application when you look at the 12345 13:20:32,000 --> 13:20:34,320 Local Host colon application tab here we 12346 13:20:34,320 --> 13:20:36,956 have the cookie let's say I want to set 12347 13:20:36,956 --> 13:20:41,116 a new cookie it should be let's say a 12348 13:20:41,116 --> 13:20:42,436 user 12349 13:20:42,436 --> 13:20:46,240 token or authenticated token something 12350 13:20:46,240 --> 13:20:48,596 like that JWT 12351 13:20:48,596 --> 13:20:51,596 or 12352 13:20:51,880 --> 13:20:56,560 JWT encrypt something like 12353 13:20:57,436 --> 13:21:01,880 that yes jw. 12354 13:21:03,320 --> 13:21:08,520 iio so what I want to do I want to 12355 13:21:08,956 --> 13:21:11,956 so 12356 13:21:12,400 --> 13:21:16,480 let's can I do 12357 13:21:17,240 --> 13:21:22,480 that let's add this payload 12358 13:21:23,040 --> 13:21:27,080 here or you can say I want to add a user 12359 13:21:27,080 --> 13:21:28,560 ID let's 12360 13:21:28,560 --> 13:21:30,320 say 12361 13:21:30,320 --> 13:21:34,680 token I'm going to add a user 12362 13:21:35,520 --> 13:21:38,160 ID 12363 13:21:38,160 --> 13:21:41,880 let's say a mongod DB 12364 13:21:44,240 --> 13:21:48,400 ID or you can say user 12365 13:21:50,276 --> 13:21:53,400 ID so let's send the request from our 12366 13:21:53,400 --> 13:21:55,840 browser to first set the 12367 13:21:55,840 --> 13:21:59,116 cookie so I can set the request please 12368 13:21:59,116 --> 13:22:03,080 set the cookie cookie saved successfully 12369 13:22:03,080 --> 13:22:06,596 and you can see your user ID here this 12370 13:22:06,596 --> 13:22:10,040 one but it did not 12371 13:22:13,480 --> 13:22:16,680 encrypt but it did not save as 12372 13:22:16,680 --> 13:22:19,596 encrypted you have to save your 12373 13:22:19,596 --> 13:22:21,756 encrypted value here let's say I'm going 12374 13:22:21,756 --> 13:22:24,680 to copy this 12375 13:22:24,680 --> 13:22:27,240 token and you got to paste it 12376 13:22:27,240 --> 13:22:32,519 [Music] 12377 13:22:35,436 --> 13:22:40,200 here now I have an encrypted 12378 13:22:40,200 --> 13:22:43,560 value so cookie saved successfully what 12379 13:22:43,560 --> 13:22:46,360 if I try to get the 12380 13:22:46,360 --> 13:22:49,880 cookie get all of my 12381 13:22:49,880 --> 13:22:52,520 cookie it is going to give me the user 12382 13:22:52,520 --> 13:22:56,360 ID and cookie token 12383 13:22:56,360 --> 13:22:58,880 name that's how if you want to implement 12384 13:22:58,880 --> 13:23:02,756 cookie based authentication you can do 12385 13:23:03,400 --> 13:23:06,040 that if you want to implement High 12386 13:23:06,040 --> 13:23:09,880 intent intensive task then you need 12387 13:23:09,880 --> 13:23:13,040 cues High intensive task 12388 13:23:13,040 --> 13:23:17,436 mean sending emails generating reports 12389 13:23:17,436 --> 13:23:20,840 processing images or any task that can 12390 13:23:20,840 --> 13:23:25,520 be offloaded from the main application 12391 13:23:27,200 --> 13:23:30,116 flow with the help of cues you can 12392 13:23:30,116 --> 13:23:33,560 manage in process task in the background 12393 13:23:33,560 --> 13:23:35,840 without blocking the main application 12394 13:23:35,840 --> 13:23:38,360 execution 12395 13:23:41,276 --> 13:23:46,240 so you can use cues in task 12396 13:23:46,240 --> 13:23:49,436 generation when you you identify task 12397 13:23:49,436 --> 13:23:51,916 that can be processed 12398 13:23:51,916 --> 13:23:54,436 asynchronously let's consider sending an 12399 13:23:54,436 --> 13:23:57,596 email after a user register instead of 12400 13:23:57,596 --> 13:23:59,360 sending email directly with the 12401 13:23:59,360 --> 13:24:02,080 registration endpoint you can push the 12402 13:24:02,080 --> 13:24:05,320 task of a sending email to a 12403 13:24:05,320 --> 13:24:08,320 queue 12404 13:24:10,680 --> 13:24:12,720 so this task is executed in the 12405 13:24:12,720 --> 13:24:14,560 background separate from the main 12406 13:24:14,560 --> 13:24:17,956 application thread this this ensure that 12407 13:24:17,956 --> 13:24:20,680 the main application remains responsive 12408 13:24:20,680 --> 13:24:23,040 and does not get blogged by the time 12409 13:24:23,040 --> 13:24:25,116 consuming 12410 13:24:25,116 --> 13:24:28,080 tasks so if you have highly CPU 12411 13:24:28,080 --> 13:24:33,160 intensive task then you can use 12412 13:24:33,160 --> 13:24:37,240 qes let's dive into the implementation 12413 13:24:37,240 --> 13:24:41,840 of cues in nestjs nestjs provided the 12414 13:24:41,840 --> 13:24:44,520 bull package they have written their own 12415 13:24:44,520 --> 13:24:46,520 implementation by using the original 12416 13:24:46,520 --> 13:24:49,480 bull package you have to install the 12417 13:24:49,480 --> 13:24:50,596 package 12418 13:24:50,596 --> 13:24:55,400 first I'm going to install the nestjs 12419 13:24:57,000 --> 13:25:02,720 bll I also need to install npm B npm 12420 13:25:02,720 --> 13:25:05,240 install B the simple bull 12421 13:25:05,240 --> 13:25:10,480 package nextjs pull is using the redish 12422 13:25:10,480 --> 13:25:13,000 under the 12423 13:25:13,000 --> 13:25:19,160 hood so you need radish caching 12424 13:25:20,320 --> 13:25:23,720 Library make sure you have Docker in 12425 13:25:23,720 --> 13:25:25,596 your 12426 13:25:25,596 --> 13:25:29,840 system we're going to use Docker to pull 12427 13:25:29,840 --> 13:25:32,560 redish image I'm not going to install 12428 13:25:32,560 --> 13:25:34,640 redish separately so that's why I'm 12429 13:25:34,640 --> 13:25:38,240 going to use docker 12430 13:25:40,840 --> 13:25:44,436 now let's create a new module our 12431 13:25:44,436 --> 13:25:46,000 application what we want to do we're 12432 13:25:46,000 --> 13:25:49,240 going to create a fake implementation to 12433 13:25:49,240 --> 13:25:54,720 convert do wave audio file to MP3 we're 12434 13:25:54,720 --> 13:25:56,560 going to write fake implementation let's 12435 13:25:56,560 --> 13:25:58,640 say you have a use case where you want 12436 13:25:58,640 --> 13:26:03,360 to convert do wave file to MP3 or you 12437 13:26:03,360 --> 13:26:05,360 have an application where you need this 12438 13:26:05,360 --> 13:26:07,916 functionality you want to convert video 12439 13:26:07,916 --> 13:26:13,756 MP4 to MP3 in that case you need a 12440 13:26:15,796 --> 13:26:17,360 q 12441 13:26:17,360 --> 13:26:20,240 let's create a new audio module we're 12442 13:26:20,240 --> 13:26:22,360 going to write implementation here to 12443 13:26:22,360 --> 13:26:26,756 convert do wave file to 12444 13:26:26,756 --> 13:26:30,080 MP3 then we need a docket 12445 13:26:30,080 --> 13:26:34,560 compose we're going to use redish 12446 13:26:35,160 --> 13:26:37,360 image 12447 13:26:37,360 --> 13:26:39,916 now we're going to set up Ries in the 12448 13:26:39,916 --> 13:26:42,796 docket compose file let's create a new 12449 13:26:42,796 --> 13:26:46,720 file inside the root 12450 13:26:48,916 --> 13:26:53,040 directory I'm going to do it here docket 12451 13:26:53,040 --> 13:26:56,040 compos 12452 13:26:56,436 --> 13:27:01,000 yl let's rename it to 12453 13:27:02,080 --> 13:27:07,276 yl so we have to in start the docker 12454 13:27:07,276 --> 13:27:10,000 compose my Docker is running 12455 13:27:10,000 --> 13:27:13,360 now let's install Docker 12456 13:27:13,360 --> 13:27:15,880 compose 12457 13:27:15,880 --> 13:27:18,756 up it will pull the redish image from 12458 13:27:18,756 --> 13:27:22,200 the docker Hub but I have already pulled 12459 13:27:22,200 --> 13:27:24,400 Docker radish 12460 13:27:24,400 --> 13:27:27,400 image so my 12461 13:27:27,400 --> 13:27:31,360 radish server has 12462 13:27:31,360 --> 13:27:34,596 started I think I've already installed 12463 13:27:34,596 --> 13:27:38,560 another extension to work with redes if 12464 13:27:38,560 --> 13:27:41,756 you want you can install 12465 13:27:41,756 --> 13:27:45,276 that here you can see the redis I have 12466 13:27:45,276 --> 13:27:48,400 installed this one database manager for 12467 13:27:48,400 --> 13:27:53,000 my SQL Maria B post sqlite and redis 12468 13:27:53,000 --> 13:27:56,116 here you will see the database if I try 12469 13:27:56,116 --> 13:27:58,840 to create a connection for the 12470 13:27:58,840 --> 13:28:01,720 redes Local Host 12471 13:28:01,720 --> 13:28:06,160 6379 that's my port so I can connect now 12472 13:28:06,160 --> 13:28:07,916 now you will 12473 13:28:07,916 --> 13:28:10,880 see all of my 12474 13:28:10,880 --> 13:28:13,560 databases so this is my current or the 12475 13:28:13,560 --> 13:28:16,000 latest I'm going to delete the rest of 12476 13:28:16,000 --> 13:28:16,956 the 12477 13:28:16,956 --> 13:28:23,000 other now I have the empty let's remove 12478 13:28:23,040 --> 13:28:26,080 everything let's 12479 13:28:26,080 --> 13:28:30,360 connect so I have the audio 12480 13:28:30,596 --> 13:28:33,640 que so get rid of this 12481 13:28:33,640 --> 13:28:38,040 one now we have the empty data 12482 13:28:42,276 --> 13:28:44,680 SP now we need to 12483 13:28:44,680 --> 13:28:47,596 register a bull 12484 13:28:47,596 --> 13:28:51,680 module so in the app 12485 13:28:51,680 --> 13:28:55,956 module I'm going to register it 12486 13:28:56,360 --> 13:29:00,640 here your module 12487 13:29:01,080 --> 13:29:04,240 and I'm going to do it 12488 13:29:04,240 --> 13:29:09,480 here make sure you imported a bull 12489 13:29:11,520 --> 13:29:14,956 module bull 12490 13:29:22,480 --> 13:29:25,480 module 12491 13:29:29,276 --> 13:29:33,480 import from Nest 12492 13:29:34,956 --> 13:29:40,436 CHS SGS Bull and bull 12493 13:29:42,080 --> 13:29:44,796 module so I'm going to open a new tab to 12494 13:29:44,796 --> 13:29:47,320 run my 12495 13:29:47,320 --> 13:29:52,240 application next we need to register a 12496 13:29:52,240 --> 13:29:56,160 queue inside the audio module you will 12497 13:29:56,160 --> 13:29:59,840 find the audio module here so we got a 12498 13:29:59,840 --> 13:30:01,720 register it 12499 13:30:01,720 --> 13:30:05,240 here make sure you imported the bull 12500 13:30:05,240 --> 13:30:08,360 module and I'm going to copy 12501 13:30:08,360 --> 13:30:12,480 that and let me paste it 12502 13:30:12,480 --> 13:30:16,200 here so this register Q This method is 12503 13:30:16,200 --> 13:30:18,480 used to register a queue with your 12504 13:30:18,480 --> 13:30:21,240 within your application it takes an 12505 13:30:21,240 --> 13:30:23,436 optional object as an argument to 12506 13:30:23,436 --> 13:30:25,956 configure the que we have provided the 12507 13:30:25,956 --> 13:30:28,560 name of our Q audio 12508 13:30:28,560 --> 13:30:33,680 q and this is the name you will see the 12509 13:30:33,680 --> 13:30:37,040 name here 12510 13:30:37,040 --> 13:30:40,080 here it should be the audio Q this one 12511 13:30:40,080 --> 13:30:43,276 where is my redish 12512 13:30:43,276 --> 13:30:47,840 yep you will see the audio queue 12513 13:30:48,200 --> 13:30:51,320 here let's move forward now we're going 12514 13:30:51,320 --> 13:30:54,840 to create a convert end point I'm going 12515 13:30:54,840 --> 13:30:56,520 to copy 12516 13:30:56,520 --> 13:31:00,436 that into audio 12517 13:31:01,360 --> 13:31:04,680 controller and 12518 13:31:04,840 --> 13:31:08,596 I'm so we have audio Q let's imagine we 12519 13:31:08,596 --> 13:31:11,596 would like to convert v file into MP3 12520 13:31:11,596 --> 13:31:14,796 this is my I'm just writing the fake 12521 13:31:14,796 --> 13:31:17,040 implementation we are not writing the 12522 13:31:17,040 --> 13:31:20,200 exact logic to convert the file but you 12523 13:31:20,200 --> 13:31:22,560 can do that this is just a file name 12524 13:31:22,560 --> 13:31:27,436 this is an optional object so this will 12525 13:31:27,436 --> 13:31:30,956 add a new entry inside the 12526 13:31:30,956 --> 13:31:35,080 Q so we have injected the audio q i got 12527 13:31:35,080 --> 13:31:38,520 this Q from from The Bu package the type 12528 13:31:38,520 --> 13:31:42,480 now we need a processor to process the 12529 13:31:42,480 --> 13:31:47,680 queue let's define a new audio 12530 13:31:47,680 --> 13:31:51,916 processor audio. 12531 13:31:52,240 --> 13:31:54,720 processor. it would be a service or 12532 13:31:54,720 --> 13:31:57,200 provider so we got to register it here 12533 13:31:57,200 --> 13:32:00,160 inside the 12534 13:32:00,160 --> 13:32:01,796 providers 12535 13:32:01,796 --> 13:32:04,796 AIO 12536 13:32:04,840 --> 13:32:07,840 processor 12537 13:32:08,240 --> 13:32:09,640 so 12538 13:32:09,640 --> 13:32:12,916 here when you want to process this when 12539 13:32:12,916 --> 13:32:15,596 the application wants to process it is 12540 13:32:15,596 --> 13:32:19,840 going to call this handle convert this 12541 13:32:19,840 --> 13:32:24,840 job will take this argument as a job 12542 13:32:24,840 --> 13:32:27,560 here inside the audio 12543 13:32:27,560 --> 13:32:29,796 processor so I'm just trying to do the 12544 13:32:29,796 --> 13:32:32,160 log here let's run the 12545 13:32:32,160 --> 13:32:36,436 application to convert W file to m B3 we 12546 13:32:36,436 --> 13:32:39,680 have to send the convert request 12547 13:32:39,680 --> 13:32:42,916 so in the HTTP 12548 13:32:42,916 --> 13:32:47,040 client I'm going to send the post 12549 13:32:47,520 --> 13:32:54,840 request convert let's say dot v file to 12550 13:32:54,840 --> 13:32:57,840 MP3 12551 13:32:59,116 --> 13:33:04,520 post HTTP Local Host Callum 3000 SL 12552 13:33:04,520 --> 13:33:07,200 audio SL convert I don't need to provide 12553 13:33:07,200 --> 13:33:09,116 any argument but if you want to provide 12554 13:33:09,116 --> 13:33:13,796 argument or or request body you can do 12555 13:33:13,796 --> 13:33:18,880 that now you can see it has run 12556 13:33:18,880 --> 13:33:22,320 successfully I got this message starting 12557 13:33:22,320 --> 13:33:27,116 wave file inside the audio 12558 13:33:27,320 --> 13:33:31,520 processor here starting start converting 12559 13:33:31,520 --> 13:33:36,080 v file to MP3 and I got the object which 12560 13:33:36,080 --> 13:33:39,276 is sample. W file which which I have 12561 13:33:39,276 --> 13:33:42,756 passed here whatever you pass here it 12562 13:33:42,756 --> 13:33:45,276 will be received here inside the job. 12563 13:33:45,276 --> 13:33:47,720 data and finally it has completed this 12564 13:33:47,720 --> 13:33:50,360 one if you have any type of logic to 12565 13:33:50,360 --> 13:33:53,276 convert P file to MP3 you will Implement 12566 13:33:53,276 --> 13:33:54,880 here 12567 13:33:54,880 --> 13:33:58,116 here but what if I look up the 12568 13:33:58,116 --> 13:34:01,436 database do you have this 12569 13:34:01,436 --> 13:34:05,480 one convert yep you can see the bull and 12570 13:34:05,480 --> 13:34:08,880 we we have the audio Q this is our 12571 13:34:08,880 --> 13:34:12,000 message I can show 12572 13:34:12,000 --> 13:34:16,320 you here you can see the name is 12573 13:34:16,320 --> 13:34:21,000 convert which we named here the convert 12574 13:34:21,000 --> 13:34:27,320 this one q key key is here 12575 13:34:28,080 --> 13:34:29,640 convert 12576 13:34:29,640 --> 13:34:33,640 convert and the data is sample. W file 12577 13:34:33,640 --> 13:34:37,640 and all of these metad data 12578 13:34:37,680 --> 13:34:40,080 what if I provide additional data let's 12579 13:34:40,080 --> 13:34:42,880 say ID which is one so it is going to 12580 13:34:42,880 --> 13:34:46,200 save also 12581 13:34:50,680 --> 13:34:54,840 ID let's run the request you can see I 12582 13:34:54,840 --> 13:34:56,880 got the file and 12583 13:34:56,880 --> 13:35:01,520 ID and Let me refresh 12584 13:35:04,640 --> 13:35:07,640 it 12585 13:35:17,880 --> 13:35:20,840 I'm going to 12586 13:35:21,160 --> 13:35:23,796 connect uh you can 12587 13:35:23,796 --> 13:35:26,680 see file sample 12588 13:35:26,680 --> 13:35:30,840 do wave and you can see the ID the name 12589 13:35:30,840 --> 13:35:34,360 is convert and the audio CU which is we 12590 13:35:34,360 --> 13:35:38,480 have named here here inside the audio 12591 13:35:38,480 --> 13:35:42,436 module this is the audio 12592 13:35:44,520 --> 13:35:47,480 Q in this lesson I'm going to teach you 12593 13:35:47,480 --> 13:35:49,080 the event 12594 13:35:49,080 --> 13:35:52,080 emitter in nextjs event emitor is a 12595 13:35:52,080 --> 13:35:54,520 mechanism that allows different parts of 12596 13:35:54,520 --> 13:35:57,320 your application to communicate with 12597 13:35:57,320 --> 13:36:01,200 each other and even driven 12598 13:36:01,200 --> 13:36:04,320 approach so you can learn more about you 12599 13:36:04,320 --> 13:36:06,756 can do communication between module 12600 13:36:06,756 --> 13:36:10,436 Services components or classes under the 12601 13:36:10,436 --> 13:36:13,080 hood nestjs uses event emiter to 12602 13:36:13,080 --> 13:36:15,436 collaborate with these services and 12603 13:36:15,436 --> 13:36:16,756 modules and 12604 13:36:16,756 --> 13:36:20,040 components so an event emitter works 12605 13:36:20,040 --> 13:36:22,640 with Publisher and subscriber pattern if 12606 13:36:22,640 --> 13:36:24,796 you heard about this design pattern or 12607 13:36:24,796 --> 13:36:29,000 you have used this design 12608 13:36:30,520 --> 13:36:32,680 pattern now we're going to talk about 12609 13:36:32,680 --> 13:36:34,756 some practical use cases of event 12610 13:36:34,756 --> 13:36:37,756 emitter you can do module communication 12611 13:36:37,756 --> 13:36:40,240 you can send the notification 12612 13:36:40,240 --> 13:36:43,436 system we're going to use this use 12613 13:36:43,436 --> 13:36:46,160 case in the previous video we have 12614 13:36:46,160 --> 13:36:49,200 converted wave file to 12615 13:36:49,200 --> 13:36:52,880 MP3 now I want to convert I want to send 12616 13:36:52,880 --> 13:36:54,916 the notification if the file has 12617 13:36:54,916 --> 13:36:57,400 successfully converted we need to send 12618 13:36:57,400 --> 13:37:00,480 the notification to the user we can also 12619 13:37:00,480 --> 13:37:04,320 use from for Real Time updates if you 12620 13:37:04,320 --> 13:37:06,596 have any plugin syst system you can do 12621 13:37:06,596 --> 13:37:08,916 with the help of event emitter you can 12622 13:37:08,916 --> 13:37:11,080 achieve logging and monitoring you can 12623 13:37:11,080 --> 13:37:13,956 do error handling and Reporting this one 12624 13:37:13,956 --> 13:37:17,880 is also useful you can Implement in user 12625 13:37:17,880 --> 13:37:21,160 authentication and 12626 13:37:21,756 --> 13:37:24,240 authorization for example authorization 12627 13:37:24,240 --> 13:37:26,880 service could emit an event when user 12628 13:37:26,880 --> 13:37:29,880 logs in and other parts of application 12629 13:37:29,880 --> 13:37:33,116 can respond Accord accordingly let's say 12630 13:37:33,116 --> 13:37:37,596 user has logged after 10 days your you 12631 13:37:37,596 --> 13:37:40,240 can your system or your application can 12632 13:37:40,240 --> 13:37:41,640 have 12633 13:37:41,640 --> 13:37:44,000 notification you can also Implement in 12634 13:37:44,000 --> 13:37:47,116 the caching and management in our case 12635 13:37:47,116 --> 13:37:48,720 we're going to take example from our 12636 13:37:48,720 --> 13:37:50,596 previous 12637 13:37:50,596 --> 13:37:53,080 example let's say you want to send the 12638 13:37:53,080 --> 13:37:55,720 notification when w file converts 12639 13:37:55,720 --> 13:37:59,596 successfully into MP3 format you can use 12640 13:37:59,596 --> 13:38:02,916 event emitter to send the 12641 13:38:04,436 --> 13:38:06,000 notification 12642 13:38:06,000 --> 13:38:08,756 you got to install the package first 12643 13:38:08,756 --> 13:38:11,040 nestjs event 12644 13:38:11,040 --> 13:38:13,916 emitter and Next Step you got to 12645 13:38:13,916 --> 13:38:19,080 register the event emitter in the app 12646 13:38:20,560 --> 13:38:24,680 module let me change my 12647 13:38:26,116 --> 13:38:29,116 Branch 12648 13:38:30,116 --> 13:38:32,436 here 12649 13:38:32,436 --> 13:38:36,320 event emitter Pro 12650 13:38:36,320 --> 13:38:40,796 we got to create install event emitter 12651 13:38:40,796 --> 13:38:43,680 again because I was in a different 12652 13:38:43,680 --> 13:38:47,596 branch let's install 12653 13:38:52,720 --> 13:38:56,040 it we have event 12654 13:38:56,040 --> 13:39:01,560 emitter let's register it in the app 12655 13:39:02,680 --> 13:39:07,320 module I'm going to do it here 12656 13:39:08,720 --> 13:39:13,720 we have to import the event emiter 12657 13:39:14,916 --> 13:39:19,796 module I have to import it manually 12658 13:39:19,796 --> 13:39:23,596 event emitter 12659 13:39:23,596 --> 13:39:26,116 module from 12660 13:39:26,116 --> 13:39:28,360 nestjs event 12661 13:39:28,360 --> 13:39:30,640 emiter that looks good we have 12662 13:39:30,640 --> 13:39:33,840 registered the event emitter module this 12663 13:39:33,840 --> 13:39:36,916 will register all of your event listener 12664 13:39:36,916 --> 13:39:39,916 or or create all of your event listener 12665 13:39:39,916 --> 13:39:42,560 we will have audio converted 12666 13:39:42,560 --> 13:39:45,796 listener it's a service it's a 12667 13:39:45,796 --> 13:39:48,720 provider this we have just a method it 12668 13:39:48,720 --> 13:39:51,360 will accept the event and the audio 12669 13:39:51,360 --> 13:39:54,000 event we will create the type this is 12670 13:39:54,000 --> 13:39:56,160 the name of the listener or the name of 12671 13:39:56,160 --> 13:40:00,436 the event that's why I used on event 12672 13:40:00,436 --> 13:40:04,720 here when you emit the event this audio 12673 13:40:04,720 --> 13:40:08,520 converted event this function will be 12674 13:40:08,520 --> 13:40:11,276 called we're going to emit this event in 12675 13:40:11,276 --> 13:40:14,520 the handle convert in the previous video 12676 13:40:14,520 --> 13:40:18,200 we used this method to convert fave into 12677 13:40:18,200 --> 13:40:21,596 MP3 so when you emit this event this 12678 13:40:21,596 --> 13:40:23,240 method will be 12679 13:40:23,240 --> 13:40:26,596 called so you can have any type of logic 12680 13:40:26,596 --> 13:40:29,200 here maybe you will have an email 12681 13:40:29,200 --> 13:40:31,956 service and your email service will have 12682 13:40:31,956 --> 13:40:35,796 a method to send email let's create a 12683 13:40:35,796 --> 13:40:38,200 new file I'm going to copy all of the 12684 13:40:38,200 --> 13:40:40,840 code this is a provider we're going to 12685 13:40:40,840 --> 13:40:43,880 in we're going to create inside 12686 13:40:43,880 --> 13:40:46,880 the 12687 13:40:47,360 --> 13:40:48,916 audio 12688 13:40:48,916 --> 13:40:51,040 converted what's the name of the file 12689 13:40:51,040 --> 13:40:53,916 audio converted 12690 13:40:55,480 --> 13:41:00,116 listener I can say audio do 12691 13:41:01,796 --> 13:41:06,596 converted or you can say audio 12692 13:41:06,596 --> 13:41:09,596 converted 12693 13:41:13,756 --> 13:41:17,756 listener. so we have this 12694 13:41:18,240 --> 13:41:21,796 file we did not create this type let's 12695 13:41:21,796 --> 13:41:24,560 create this 12696 13:41:24,560 --> 13:41:27,916 type in audio 12697 13:41:27,916 --> 13:41:31,756 folder I can say 12698 13:41:31,756 --> 13:41:37,000 audio actually we can do it in an events 12699 13:41:37,000 --> 13:41:40,240 folder we're going to store all of the 12700 13:41:40,240 --> 13:41:42,680 types 12701 13:41:42,680 --> 13:41:45,796 audio converted 12702 13:41:45,796 --> 13:41:48,880 event. it's nothing it's just a type for 12703 13:41:48,880 --> 13:41:52,240 types typescript 12704 13:41:52,240 --> 13:41:55,720 class it looks good make sure you 12705 13:41:55,720 --> 13:41:58,640 register your provider in the audio 12706 13:41:58,640 --> 13:42:02,000 module so audio 12707 13:42:02,000 --> 13:42:04,116 converted 12708 13:42:04,116 --> 13:42:06,080 listener 12709 13:42:06,080 --> 13:42:08,680 now we need to import we need to emit 12710 13:42:08,680 --> 13:42:11,000 this 12711 13:42:11,680 --> 13:42:15,000 event in your handle convert we have 12712 13:42:15,000 --> 13:42:16,596 done it in the 12713 13:42:16,596 --> 13:42:19,596 processor while converted successfully 12714 13:42:19,596 --> 13:42:22,080 and you can send a 12715 13:42:22,080 --> 13:42:26,480 message make sure you 12716 13:42:26,480 --> 13:42:32,276 have import or inject event 12717 13:42:32,756 --> 13:42:37,116 emitter event emitter 12718 13:42:37,720 --> 13:42:40,116 event emitter 12719 13:42:40,116 --> 13:42:44,840 2 from njs event emitter 12720 13:42:45,680 --> 13:42:48,400 package 12721 13:42:48,400 --> 13:42:50,640 njs event 12722 13:42:50,640 --> 13:42:52,520 emiter 12723 13:42:52,520 --> 13:42:55,560 oops event emiter I don't know why they 12724 13:42:55,560 --> 13:42:57,840 use two so I'm going to use the same 12725 13:42:57,840 --> 13:43:01,640 stuff two event emitter 12726 13:43:01,680 --> 13:43:05,720 to and we have emit this 12727 13:43:05,720 --> 13:43:08,520 event this 12728 13:43:08,520 --> 13:43:12,360 one now we have to test the application 12729 13:43:12,360 --> 13:43:14,040 let's run the 12730 13:43:14,040 --> 13:43:18,796 application I think do I need a 12731 13:43:18,796 --> 13:43:23,160 Docker let's run the 12732 13:43:23,436 --> 13:43:26,840 application yes I need a 12733 13:43:26,840 --> 13:43:30,200 Docker let's install the uh let's start 12734 13:43:30,200 --> 13:43:34,040 the docker first because we were using 12735 13:43:34,040 --> 13:43:40,240 redes when we were building this Q so 12736 13:43:40,240 --> 13:43:43,080 this Q is dependent on bull module is 12737 13:43:43,080 --> 13:43:45,796 dependent on 12738 13:43:45,796 --> 13:43:50,436 rades let me start my 12739 13:43:54,480 --> 13:43:57,320 Docker now my Docker is running now 12740 13:43:57,320 --> 13:43:59,956 let's run the application to test this 12741 13:43:59,956 --> 13:44:02,680 event 12742 13:44:03,436 --> 13:44:06,276 emitor we have to send the API request 12743 13:44:06,276 --> 13:44:09,000 to convert fa file to 12744 13:44:09,000 --> 13:44:12,756 MP3 it should log the message actually 12745 13:44:12,756 --> 13:44:15,880 it should run this 12746 13:44:15,880 --> 13:44:19,240 event this 12747 13:44:33,956 --> 13:44:36,956 meth 12748 13:44:38,796 --> 13:44:41,720 let's send the 12749 13:44:45,756 --> 13:44:50,436 request I'm not able to send the request 12750 13:44:50,436 --> 13:44:53,080 why saying 12751 13:44:53,080 --> 13:44:57,240 waiting I think I did something wrong 12752 13:44:57,240 --> 13:45:01,240 here look at the audio 12753 13:45:01,240 --> 13:45:03,916 processor or audio 12754 13:45:03,916 --> 13:45:06,916 controller 12755 13:45:12,916 --> 13:45:17,116 wa I think it is not calling this method 12756 13:45:17,116 --> 13:45:20,080 or emitting this 12757 13:45:20,080 --> 13:45:24,520 method emitting this IM 12758 13:45:30,956 --> 13:45:36,320 event oh I think we did not stop part 12759 13:45:36,320 --> 13:45:38,756 that is 12760 13:45:43,916 --> 13:45:50,560 server let's do it I'm going to send the 12761 13:45:55,560 --> 13:45:59,520 request connect with 12762 13:46:00,596 --> 13:46:03,080 r 12763 13:46:03,080 --> 13:46:07,560 so it has X executed 12764 13:46:07,680 --> 13:46:10,560 successfully and I got the message 12765 13:46:10,560 --> 13:46:14,040 notification has sent to the user that 12766 13:46:14,040 --> 13:46:16,240 file is converted 12767 13:46:16,240 --> 13:46:19,520 successfully so I also loged the 12768 13:46:19,520 --> 13:46:22,000 event that looks good we have 12769 13:46:22,000 --> 13:46:23,880 successfully 12770 13:46:23,880 --> 13:46:27,160 achieved our desired use 12771 13:46:27,160 --> 13:46:31,720 case here it has emited the event I also 12772 13:46:31,720 --> 13:46:37,436 passed a data object from the Q job. 12773 13:46:37,436 --> 13:46:41,840 data which we were sending from here so 12774 13:46:41,840 --> 13:46:44,520 I received the same 12775 13:46:44,520 --> 13:46:48,640 object inside the event 12776 13:46:50,436 --> 13:46:55,040 listener here I received the same event 12777 13:46:55,040 --> 13:46:57,560 here and here I locked the event the 12778 13:46:57,560 --> 13:47:00,796 first one is the event and the message 12779 13:47:00,796 --> 13:47:02,956 notification has sent to user 12780 13:47:02,956 --> 13:47:05,360 successfully this is how you will 12781 13:47:05,360 --> 13:47:09,276 Implement event emitter in 12782 13:47:13,436 --> 13:47:16,360 nestjs in this lesson I'm going to teach 12783 13:47:16,360 --> 13:47:19,160 you the streaming in 12784 13:47:19,160 --> 13:47:22,720 sjs streaming is the process of sending 12785 13:47:22,720 --> 13:47:26,680 or receiving data in small chunks called 12786 13:47:26,680 --> 13:47:29,480 streams rather than sending or receiving 12787 13:47:29,480 --> 13:47:33,400 the entire data at once the concept is 12788 13:47:33,400 --> 13:47:36,880 based on stream API in 12789 13:47:36,880 --> 13:47:40,756 nodejs and is utilized for more 12790 13:47:40,756 --> 13:47:44,040 efficient data handling especially when 12791 13:47:44,040 --> 13:47:46,560 dealing with large amounts of data such 12792 13:47:46,560 --> 13:47:50,560 as files Network request or realtime 12793 13:47:50,560 --> 13:47:52,956 data 12794 13:47:53,080 --> 13:47:55,840 Transmissions you can use streaming for 12795 13:47:55,840 --> 13:47:59,400 file upload and file download you can do 12796 13:47:59,400 --> 13:48:02,276 realtime communication if you have to 12797 13:48:02,276 --> 13:48:05,160 build a media streaming type application 12798 13:48:05,160 --> 13:48:08,680 like Netflix then you need streaming 12799 13:48:08,680 --> 13:48:12,276 API API responses reading and writing 12800 13:48:12,276 --> 13:48:14,200 from 12801 13:48:14,200 --> 13:48:18,160 stream when processing data from sources 12802 13:48:18,160 --> 13:48:20,680 that generate data 12803 13:48:20,680 --> 13:48:24,080 incrementally such as reading log file 12804 13:48:24,080 --> 13:48:27,436 or parsing large XML or Json 12805 13:48:27,436 --> 13:48:29,840 documents you can do some data 12806 13:48:29,840 --> 13:48:32,916 aggregation server sent events database 12807 13:48:32,916 --> 13:48:35,840 operations proxy servers all of these 12808 13:48:35,840 --> 13:48:40,360 practical use cases of streaming in this 12809 13:48:40,360 --> 13:48:44,520 video I'm just showing you how to 12810 13:48:44,520 --> 13:48:47,596 download a file with the help of 12811 13:48:47,596 --> 13:48:51,436 streaming we need a nest controller a 12812 13:48:51,436 --> 13:48:54,880 file controller here I will 12813 13:48:54,880 --> 13:49:00,916 specify a a a method it will download 12814 13:49:00,916 --> 13:49:04,916 the file from this crun project as a 12815 13:49:04,916 --> 13:49:08,320 package.json file it is going to save in 12816 13:49:08,320 --> 13:49:11,880 my computer this is another customized 12817 13:49:11,880 --> 13:49:14,560 way to download the 12818 13:49:14,560 --> 13:49:17,916 file when you download the file with 12819 13:49:17,916 --> 13:49:20,796 this method it will download the exact 12820 13:49:20,796 --> 13:49:23,560 package.json file here it will not 12821 13:49:23,560 --> 13:49:26,796 download with extension only text file 12822 13:49:26,796 --> 13:49:28,480 let me show you the difference between 12823 13:49:28,480 --> 13:49:32,560 them let's create a new 12824 13:49:33,160 --> 13:49:36,680 controller open the file controller let 12825 13:49:36,680 --> 13:49:39,680 me import some 12826 13:49:40,720 --> 13:49:43,400 dependencies and we need to use this 12827 13:49:43,400 --> 13:49:46,480 method to download the file I'm going to 12828 13:49:46,480 --> 13:49:52,200 create a new method here let's run the 12829 13:50:01,480 --> 13:50:04,080 application let's try to send the 12830 13:50:04,080 --> 13:50:07,116 request to the 12831 13:50:07,840 --> 13:50:11,680 file file we have the endpoint file and 12832 13:50:11,680 --> 13:50:14,000 the stream 12833 13:50:14,000 --> 13:50:19,160 file file and stream 12834 13:50:19,160 --> 13:50:22,116 file now it is going to 12835 13:50:22,116 --> 13:50:26,520 download the package.json 12836 13:50:26,520 --> 13:50:29,400 file you can 12837 13:50:29,400 --> 13:50:34,116 see it is giving just only a stream file 12838 13:50:34,116 --> 13:50:37,400 it did not download with package.json 12839 13:50:37,400 --> 13:50:41,000 file if I open the file you will see it 12840 13:50:41,000 --> 13:50:43,436 is nothing more it's just a simple text 12841 13:50:43,436 --> 13:50:45,956 file in a Json 12842 13:50:45,956 --> 13:50:48,200 format but we going to we want to 12843 13:50:48,200 --> 13:50:51,840 download the exact Json file you can do 12844 13:50:51,840 --> 13:50:53,596 the same stuff when you down when you 12845 13:50:53,596 --> 13:50:55,080 want to download 12846 13:50:55,080 --> 13:50:59,560 images or video you have to specify here 12847 13:50:59,560 --> 13:51:01,436 the content type should be video or 12848 13:51:01,436 --> 13:51:04,200 audio it depends on your use case and 12849 13:51:04,200 --> 13:51:08,200 you also can provide the name of your 12850 13:51:08,200 --> 13:51:10,796 file just like we did the package.json 12851 13:51:10,796 --> 13:51:16,276 file you have to do it content dispo dis 12852 13:51:16,276 --> 13:51:18,956 disposition now we're going to copy the 12853 13:51:18,956 --> 13:51:23,400 same part method and place it 12854 13:51:23,400 --> 13:51:27,720 here it will return a streamable file I 12855 13:51:27,720 --> 13:51:30,840 have created the stream by you joining 12856 13:51:30,840 --> 13:51:33,080 the path which is this one I'm referring 12857 13:51:33,080 --> 13:51:35,160 to package.json file 12858 13:51:35,160 --> 13:51:39,276 file and finally I'm setting the 12859 13:51:39,276 --> 13:51:42,040 response by setting application Json my 12860 13:51:42,040 --> 13:51:44,560 file name would be this one act as the 12861 13:51:44,560 --> 13:51:46,880 attachment and finally it is going to 12862 13:51:46,880 --> 13:51:49,796 return the streamable file now if I go 12863 13:51:49,796 --> 13:51:51,840 to this 12864 13:51:51,840 --> 13:51:56,000 endpoint file and/ stream file 12865 13:51:56,000 --> 13:51:58,276 customize what will 12866 13:51:58,276 --> 13:52:02,276 happen now it is going to do stream file 12867 13:52:02,276 --> 13:52:06,520 customize. Json on not not it should be 12868 13:52:06,520 --> 13:52:10,400 Json now it is going to see you will see 12869 13:52:10,400 --> 13:52:12,720 the package.json file I already 12870 13:52:12,720 --> 13:52:15,200 downloaded the package.json file when we 12871 13:52:15,200 --> 13:52:18,240 were when I was practicing this I can 12872 13:52:18,240 --> 13:52:21,116 replace that you will see the 12873 13:52:21,116 --> 13:52:23,840 package.json 12874 13:52:25,200 --> 13:52:29,480 file I have opened the file in my 12875 13:52:29,480 --> 13:52:31,520 Mozilla 12876 13:52:31,520 --> 13:52:34,640 Firefox you can see your the package 12877 13:52:34,640 --> 13:52:36,756 Json file we have scripts we have all 12878 13:52:36,756 --> 13:52:39,116 the dependencies and we have the dev 12879 13:52:39,116 --> 13:52:40,880 dependencies this is how you can 12880 13:52:40,880 --> 13:52:43,276 download any file if you have images you 12881 13:52:43,276 --> 13:52:46,520 can specify your image format or you can 12882 13:52:46,520 --> 13:52:49,560 name the file name or your image file 12883 13:52:49,560 --> 13:52:55,436 name you can also set your image content 12884 13:52:55,436 --> 13:52:58,080 type in this lesson you're going to 12885 13:52:58,080 --> 13:53:00,520 learn this session in 12886 13:53:00,520 --> 13:53:03,796 sjs session refers to the concept of 12887 13:53:03,796 --> 13:53:07,200 maintaining stateful data between 12888 13:53:07,200 --> 13:53:10,360 consecutive request from the same client 12889 13:53:10,360 --> 13:53:13,360 a session allows you to send allows you 12890 13:53:13,360 --> 13:53:16,720 to store and retrieve user specific 12891 13:53:16,720 --> 13:53:19,840 information on the server server on the 12892 13:53:19,840 --> 13:53:23,240 server across multiple request typically 12893 13:53:23,240 --> 13:53:26,320 using cookies to identify the session if 12894 13:53:26,320 --> 13:53:28,400 you have used session based 12895 13:53:28,400 --> 13:53:30,480 authentication in your project the 12896 13:53:30,480 --> 13:53:32,160 concept is 12897 13:53:32,160 --> 13:53:36,560 same you can use session to maintain the 12898 13:53:36,560 --> 13:53:39,916 authentication state of a user when a 12899 13:53:39,916 --> 13:53:43,000 user log in logs in session can be 12900 13:53:43,000 --> 13:53:45,520 created with their authen authentication 12901 13:53:45,520 --> 13:53:48,596 data and subsequent request can be 12902 13:53:48,596 --> 13:53:51,400 authenticated based on the session you 12903 13:53:51,400 --> 13:53:54,276 can use session in authorization user 12904 13:53:54,276 --> 13:53:57,276 preferences shopping cards user tracking 12905 13:53:57,276 --> 13:53:58,840 caching 12906 13:53:58,840 --> 13:54:03,080 personalization and form data 12907 13:54:03,080 --> 13:54:05,956 persistence we going to use the express 12908 13:54:05,956 --> 13:54:09,480 session let's install these two 12909 13:54:09,480 --> 13:54:12,560 packages I have already installed in my 12910 13:54:12,560 --> 13:54:15,240 package.json 12911 13:54:15,240 --> 13:54:17,320 file 12912 13:54:17,320 --> 13:54:20,160 session Express session and the next 12913 13:54:20,160 --> 13:54:24,320 package you need type of Express session 12914 13:54:24,320 --> 13:54:27,640 then our next step is to register a 12915 13:54:27,640 --> 13:54:30,360 middleware when you go to main.ts you 12916 13:54:30,360 --> 13:54:32,040 got to register it 12917 13:54:32,040 --> 13:54:34,880 here AB do session make make sure you 12918 13:54:34,880 --> 13:54:37,916 import your session from the express 12919 13:54:37,916 --> 13:54:41,040 session you can use any secret key you 12920 13:54:41,040 --> 13:54:44,200 can also set resave you can read the 12921 13:54:44,200 --> 13:54:46,840 documentation of resave what this resave 12922 13:54:46,840 --> 13:54:49,000 is doing you can also read the 12923 13:54:49,000 --> 13:54:53,640 documentation of save 12924 13:54:54,640 --> 13:54:57,320 uninitialized you can find documentation 12925 13:54:57,320 --> 13:54:59,480 here from the express 12926 13:54:59,480 --> 13:55:03,240 session when you check a save or 12927 13:55:03,240 --> 13:55:05,756 resave here you will find the 12928 13:55:05,756 --> 13:55:08,596 documentation 12929 13:55:08,640 --> 13:55:10,956 uninitialized initialized you can also 12930 13:55:10,956 --> 13:55:14,276 find the documentation of 12931 13:55:14,480 --> 13:55:18,480 uninitialized next we have app 12932 13:55:18,480 --> 13:55:20,796 controller here I'm going to create a 12933 13:55:20,796 --> 13:55:25,480 simple route to login let's say uh a 12934 13:55:25,480 --> 13:55:28,560 user wants to log in when logged in 12935 13:55:28,560 --> 13:55:30,880 successfully we have verified the user 12936 13:55:30,880 --> 13:55:34,320 from the database then you can set the 12937 13:55:34,320 --> 13:55:37,160 user into the session I have set the 12938 13:55:37,160 --> 13:55:40,320 object ID and the username and when 12939 13:55:40,320 --> 13:55:44,276 later user wants to access his profile 12940 13:55:44,276 --> 13:55:47,240 he can do that because we have saved the 12941 13:55:47,240 --> 13:55:49,956 user in the session so here if there is 12942 13:55:49,956 --> 13:55:51,796 a user in the session it is going to 12943 13:55:51,796 --> 13:55:55,116 return user else not logged in you can 12944 13:55:55,116 --> 13:55:57,276 test it simple by sending the login 12945 13:55:57,276 --> 13:55:59,756 request it is saying a user has logged 12946 13:55:59,756 --> 13:56:03,436 in just like we did just like it did if 12947 13:56:03,436 --> 13:56:06,720 I said if I try to retrieve the profile 12948 13:56:06,720 --> 13:56:09,080 it is going to give me the profile 12949 13:56:09,080 --> 13:56:11,436 because we have set that user when we 12950 13:56:11,436 --> 13:56:15,436 were logged in this one now I got this 12951 13:56:15,436 --> 13:56:17,320 hello with the user and the name this is 12952 13:56:17,320 --> 13:56:20,320 how uh if you use Json web token we 12953 13:56:20,320 --> 13:56:22,956 basically stored a Json web token in the 12954 13:56:22,956 --> 13:56:25,200 storage but if you want to use session 12955 13:56:25,200 --> 13:56:27,720 based authentication on front end you 12956 13:56:27,720 --> 13:56:31,480 can do that 844217

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.