1 00:00:00,090 --> 00:00:04,320 So I know that was a very brief introduction to two lines of code and of JavaScript. 2 00:00:04,770 --> 00:00:08,960 But we're now moving on to more important things, things that are relevant to my SQL. 3 00:00:08,970 --> 00:00:12,570 And the first thing we'll do is talk about a package called Faker. 4 00:00:13,050 --> 00:00:15,600 So the first question you might have is what is a package? 5 00:00:15,600 --> 00:00:21,240 And basically Node comes with thousands and thousands of libraries. 6 00:00:21,240 --> 00:00:27,000 You might have heard them called or packages or modules code that other people have written that we 7 00:00:27,000 --> 00:00:28,920 can include inside of our application. 8 00:00:28,920 --> 00:00:35,580 Really simply all I have to do is say install faker, which is a package we're going to use, but we'll 9 00:00:35,580 --> 00:00:39,210 use another package later called Express or we'll use one called MySQL. 10 00:00:39,210 --> 00:00:44,790 And all I have to say is install my SQL install faker and there are thousands of them out there, like 11 00:00:44,790 --> 00:00:45,300 I said. 12 00:00:45,300 --> 00:00:50,940 And that's one of the biggest strengths of Node is that it has a huge library of all these packages 13 00:00:50,940 --> 00:00:53,010 so that you don't have to do everything yourself. 14 00:00:53,010 --> 00:00:56,880 You can go and get some help by including somebody else's package. 15 00:00:57,330 --> 00:01:05,640 So the way we do it is through something called NPM node package manager and to install faker, which 16 00:01:05,640 --> 00:01:07,490 don't worry, I'll explain what faker is. 17 00:01:07,500 --> 00:01:11,490 This is all we have to type into our terminal and PM install faker. 18 00:01:11,820 --> 00:01:14,970 But before we go there, let me show you what Faker is. 19 00:01:14,970 --> 00:01:19,410 So Faker is a package that there's a bunch of different versions of it. 20 00:01:19,410 --> 00:01:25,050 There's I think the original one was for Ruby, I'm not positive, but there's Faker kind of implementations 21 00:01:25,050 --> 00:01:27,240 for other languages, including JavaScript. 22 00:01:27,240 --> 00:01:30,210 And what it does is it really helps us. 23 00:01:30,210 --> 00:01:33,480 It streamlines the process of generating fake data. 24 00:01:33,960 --> 00:01:39,300 So we're going to use it because we want to start off with a bunch of fake data in our database. 25 00:01:39,300 --> 00:01:44,310 I don't know if you noticed, I had 518 519 users in my database. 26 00:01:44,310 --> 00:01:46,170 I did not enter those manually. 27 00:01:46,350 --> 00:01:48,120 And they are they're all unique. 28 00:01:48,120 --> 00:01:49,890 They have a different, unique email address. 29 00:01:49,890 --> 00:01:51,720 I did not generate those myself. 30 00:01:51,900 --> 00:01:54,000 So we're going to use Faker to do that. 31 00:01:54,150 --> 00:01:55,650 So Faker is very simple. 32 00:01:55,650 --> 00:02:03,600 I can say things like faker, dot address, dot county and it will give me a random county name and 33 00:02:03,600 --> 00:02:04,980 it has hundreds of them. 34 00:02:05,160 --> 00:02:12,570 Or I could say fake or let's say name dot job descriptor and it will give me a random job descriptor. 35 00:02:12,570 --> 00:02:14,220 So there's tons of things in here. 36 00:02:14,220 --> 00:02:22,980 Phone numbers, random numbers, file names, colors, avatars, emails, usernames, domains, passwords, 37 00:02:22,980 --> 00:02:28,140 like so many things that we could pick from credit card, fake credit card numbers, account numbers, 38 00:02:28,140 --> 00:02:29,550 you get it, hopefully. 39 00:02:29,550 --> 00:02:30,690 So back in cloud nine. 40 00:02:30,690 --> 00:02:34,890 Now, what I'm going to do is actually empty this file entirely. 41 00:02:35,040 --> 00:02:40,710 App JS You can make a new file if you'd like, and now I'm going to install Faker so we can use it. 42 00:02:41,280 --> 00:02:44,850 So this video, I probably could have done a better job explaining. 43 00:02:44,850 --> 00:02:51,480 This is about two things one faker, but on a higher level packages in general and how you use them. 44 00:02:51,480 --> 00:02:53,520 So this is the workflow for using a package. 45 00:02:53,520 --> 00:02:59,160 We type NPM, install the name of the package and hit enter and it will take a second. 46 00:03:00,120 --> 00:03:02,550 And that on its own installs a package. 47 00:03:02,550 --> 00:03:06,210 You'll notice I have a new folder here called Node Modules inside. 48 00:03:06,210 --> 00:03:09,420 There's all the faker code, all this stuff. 49 00:03:10,260 --> 00:03:11,430 So all that is here now. 50 00:03:11,430 --> 00:03:12,620 I just had to type a single line. 51 00:03:12,630 --> 00:03:13,590 It's pretty awesome. 52 00:03:14,640 --> 00:03:18,360 Now in my app, guys, I can't just start using it immediately. 53 00:03:18,360 --> 00:03:23,340 I can't just say fake or dot email or something. 54 00:03:23,520 --> 00:03:29,970 I first have to tell this file I want to use fake or I have to include it or import it and it's very 55 00:03:29,970 --> 00:03:30,570 easy. 56 00:03:31,110 --> 00:03:35,790 I'm just going to say var faker equals require faker. 57 00:03:37,420 --> 00:03:40,180 So this right here, it's a variable. 58 00:03:40,210 --> 00:03:43,690 I can call this anything I could say, my faker. 59 00:03:44,590 --> 00:03:46,510 I'm going to call it faker standard. 60 00:03:46,510 --> 00:03:49,070 And this is the part that I can't change. 61 00:03:49,090 --> 00:03:50,440 This has to be faker. 62 00:03:50,440 --> 00:03:56,650 So this is going to go find a package that I've installed in node modules with the name Faker and then 63 00:03:56,650 --> 00:03:58,530 save it into this variable. 64 00:03:58,540 --> 00:04:05,080 So now whenever I use the word faker referring to this variable, I actually am referring to the package. 65 00:04:05,290 --> 00:04:07,510 So then we can do things with it. 66 00:04:07,510 --> 00:04:14,920 So we could do something like this faker, internet, dot email and that will generate things like this 67 00:04:14,920 --> 00:04:23,170 or this or this or a 23 Simone Walsh Aubry underscore dot tree numbers. 68 00:04:23,170 --> 00:04:25,250 We've got Hotmail and Gmail and Yahoo! 69 00:04:25,270 --> 00:04:30,670 So this would be a pain if we were doing this ourselves to come up with 500 individual emails. 70 00:04:31,000 --> 00:04:35,140 And then the other thing I'll show you is we're going to use it for dates, so I'll be able to say fake 71 00:04:35,140 --> 00:04:38,410 or date and this is important past. 72 00:04:39,160 --> 00:04:41,020 So we don't want to have any future dates. 73 00:04:41,020 --> 00:04:45,730 Basically, we want to start with 500 users in our database who have signed up at some point in the 74 00:04:45,730 --> 00:04:46,360 past. 75 00:04:46,840 --> 00:04:48,700 So it will generate things like this. 76 00:04:49,150 --> 00:04:51,400 So we also don't want to have to do that ourselves. 77 00:04:51,820 --> 00:05:00,280 So let's start with fake internet dot email and we need parentheses at the end. 78 00:05:00,400 --> 00:05:01,840 Sort of like my SQL. 79 00:05:01,870 --> 00:05:08,440 It's a function we're telling it to execute if I just do it on its own and I run the file now, which 80 00:05:08,440 --> 00:05:09,640 is app JS. 81 00:05:11,490 --> 00:05:12,430 Nothing happens. 82 00:05:12,450 --> 00:05:13,230 Well, that's a lie. 83 00:05:13,260 --> 00:05:14,320 It does happen. 84 00:05:14,340 --> 00:05:17,340 This runs, but we don't see the result. 85 00:05:17,340 --> 00:05:22,950 And that's because we need to print it so we can do console.log like this. 86 00:05:23,580 --> 00:05:24,270 There we go. 87 00:05:24,750 --> 00:05:27,780 Now, if I run it again, I see. 88 00:05:27,790 --> 00:05:27,980 Okay. 89 00:05:28,050 --> 00:05:29,040 We get a new email. 90 00:05:29,310 --> 00:05:30,120 Jared. 91 00:05:30,780 --> 00:05:31,660 Leila. 92 00:05:31,680 --> 00:05:32,110 Leila. 93 00:05:32,130 --> 00:05:32,970 Trevor. 94 00:05:33,000 --> 00:05:33,740 Whatever. 95 00:05:33,750 --> 00:05:36,300 So we're getting these fake emails. 96 00:05:36,300 --> 00:05:39,540 We're going to use that along with. 97 00:05:40,890 --> 00:05:43,890 I copy this and do faker dot. 98 00:05:44,490 --> 00:05:47,060 Date dot passed. 99 00:05:47,070 --> 00:05:48,690 Which is the other thing I showed you. 100 00:05:48,720 --> 00:05:50,370 We're going to use those two. 101 00:05:51,590 --> 00:05:54,080 To generate a 500 user. 102 00:05:54,080 --> 00:05:55,700 So basically this is one user. 103 00:05:55,820 --> 00:05:57,590 So it's kind of fun to play around with Faker. 104 00:05:57,590 --> 00:06:07,100 But do you know dot hacker, dot abbreviation or finance, dot currency symbol or Bitcoin address, 105 00:06:07,100 --> 00:06:09,590 let's do address and then city. 106 00:06:10,100 --> 00:06:20,390 So we could do we'll get rid of these too for now console dot log add faker dot address, dot city. 107 00:06:21,500 --> 00:06:22,970 And if we save and run that. 108 00:06:23,970 --> 00:06:24,690 There we go. 109 00:06:24,720 --> 00:06:25,930 Roberto ville. 110 00:06:26,070 --> 00:06:28,310 And if we try it again, we get something different. 111 00:06:28,320 --> 00:06:30,540 New Alexandrov fought. 112 00:06:30,990 --> 00:06:33,390 So that's the basics of faker. 113 00:06:33,420 --> 00:06:34,380 I will show you. 114 00:06:34,380 --> 00:06:36,000 If so, if you already know JavaScript. 115 00:06:36,090 --> 00:06:36,930 Skip this section. 116 00:06:36,930 --> 00:06:44,370 But I just want to show a bit more sort of the extended version of crash course in Node, how we can 117 00:06:44,370 --> 00:06:47,570 string things together and make more complex logic. 118 00:06:47,580 --> 00:06:50,820 So let's say I wanted to generate a complete address. 119 00:06:50,910 --> 00:06:55,900 So I wanted a number and the street and a city. 120 00:06:55,920 --> 00:06:57,180 Let's start with those three. 121 00:06:57,600 --> 00:06:59,780 So Faker has ways of doing that. 122 00:06:59,790 --> 00:07:01,640 We've got what do we have? 123 00:07:01,650 --> 00:07:03,520 Street name and street address. 124 00:07:03,540 --> 00:07:04,860 So let's start with that. 125 00:07:06,900 --> 00:07:12,180 We've got faker address, Dot Street address. 126 00:07:14,190 --> 00:07:16,320 Let's start there and run this again. 127 00:07:17,110 --> 00:07:18,570 Okay, so there we go. 128 00:07:19,560 --> 00:07:20,430 That's working. 129 00:07:20,880 --> 00:07:22,500 Then we've got the city, right. 130 00:07:23,280 --> 00:07:26,070 And then let's say we wanted to do a state as well. 131 00:07:26,220 --> 00:07:27,660 If we're working in the US. 132 00:07:30,010 --> 00:07:30,580 Perfect. 133 00:07:30,580 --> 00:07:32,950 So here's a complete address right now. 134 00:07:32,950 --> 00:07:35,620 Every time let's say I want three different addresses. 135 00:07:36,160 --> 00:07:38,320 I would have to copy these lines like that. 136 00:07:40,650 --> 00:07:42,030 Which is not the end of the world. 137 00:07:42,030 --> 00:07:42,340 Right. 138 00:07:42,360 --> 00:07:46,340 We can copy lines, but there's a much better way of doing this. 139 00:07:46,350 --> 00:07:49,860 So what we could do instead is create what's known as a function. 140 00:07:50,310 --> 00:07:53,070 And I'll just call this generate address. 141 00:07:53,100 --> 00:07:55,590 Think of this as a way of reusing our code. 142 00:07:56,880 --> 00:08:02,700 And the syntax looks like this function a name, parentheses, and then brackets and whatever we put 143 00:08:02,700 --> 00:08:10,710 in those brackets will be run every time I refer back to generate address. 144 00:08:10,710 --> 00:08:13,140 So on its own, if I just save this. 145 00:08:13,260 --> 00:08:15,480 Nothing is going to happen when I hit. 146 00:08:15,960 --> 00:08:16,470 Oops. 147 00:08:17,490 --> 00:08:20,670 When I hit save, now I go down here to Node. 148 00:08:21,240 --> 00:08:22,440 We don't get anything. 149 00:08:23,190 --> 00:08:28,140 What I have to do is now call this function just like this. 150 00:08:30,480 --> 00:08:31,230 Now. 151 00:08:32,450 --> 00:08:34,820 It's saying, okay, is there something called generate address? 152 00:08:34,860 --> 00:08:36,049 Oh, yes, there is. 153 00:08:36,200 --> 00:08:36,650 All right. 154 00:08:36,650 --> 00:08:38,480 Well, let's run the three lines inside of it. 155 00:08:38,900 --> 00:08:45,560 So if I ran this three times now it's only three lines to do that. 156 00:08:47,470 --> 00:08:49,090 And we get three different addresses. 157 00:08:49,690 --> 00:08:53,980 Of course, we might want to compile these together so that they're not on three separate lines. 158 00:08:53,990 --> 00:08:55,600 Maybe you want commas between them. 159 00:08:55,600 --> 00:08:57,040 We could really improve it. 160 00:08:57,160 --> 00:09:01,810 But the point is that that's basically how you define a function, and we'll be working with functions 161 00:09:01,810 --> 00:09:02,470 later on. 162 00:09:02,470 --> 00:09:04,600 But for now, that's all I'm going to show. 163 00:09:04,930 --> 00:09:06,310 So that's the basics of faker. 164 00:09:06,340 --> 00:09:11,230 The basics of packages will be installing other packages as well as the basics of node.