WEBVTT

00:00.090 --> 00:04.760
So I know that was a very brief introduction to two lines of node of Javascript.

00:04.890 --> 00:07.280
But we're now moving on to more important things.

00:07.280 --> 00:12.760
Things that are relevant to my out and the first thing we'll do is talk about a package called faker.

00:13.170 --> 00:15.570
So the first question you might have is what is a package.

00:15.690 --> 00:21.200
And basically Noad comes with thousands and thousands of libraries.

00:21.240 --> 00:26.820
You might have heard them called their packages or modules code that other people have written that

00:26.820 --> 00:33.300
we can include instead of for application really simply all we have to do is say install faker which

00:33.300 --> 00:38.070
is a package we're going to use but we'll use another package later called Express or we'll use one

00:38.070 --> 00:39.210
called My as well.

00:39.210 --> 00:44.600
And all I have to say is install my Eskew up install F.A. and there are thousands of them out there

00:44.610 --> 00:50.220
like I said and that's one of the biggest strengths of note is that it has a huge library of all these

00:50.220 --> 00:52.980
packages that you don't have to do everything yourself.

00:53.070 --> 00:57.080
You can go and get some help by including somebody else's package.

00:57.480 --> 01:02.750
So the way we do it is through something called and PM node package manager.

01:03.330 --> 01:07.440
And to install faker which don't worry I'll explain what failure is.

01:07.710 --> 01:11.600
This is all we have to type into our terminal NPM install failure.

01:11.970 --> 01:15.020
But before we go there let me show you what F.A. is.

01:15.090 --> 01:19.560
So faker is a package that there's a bunch of different versions of it.

01:19.710 --> 01:25.410
I think the original one was for Ruby not positive but there's faker kind of implementations for other

01:25.410 --> 01:32.340
languages including javascript and what it does is it really helps us streamline the process of generating

01:32.340 --> 01:33.680
fake data.

01:34.110 --> 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.

01:39.300 --> 01:44.310
I don't know if you noticed I had 518 519 users in my database.

01:44.400 --> 01:48.090
I did not enter those manually and they are they're all unique.

01:48.090 --> 01:49.930
They have a different unique e-mail address.

01:49.930 --> 01:51.740
I did not generate those myself.

01:52.020 --> 01:54.050
So we're going to use failure to do that.

01:54.300 --> 01:55.620
So fakers very simple.

01:55.620 --> 02:03.990
I can say things like faker dot address dot County and it will give me a random county name and it has

02:03.990 --> 02:04.920
hundreds of them.

02:05.340 --> 02:12.670
Or I could say faker did but say name dot job descriptor and it will give me a random job descriptor.

02:12.750 --> 02:14.160
So there's tons of things in here.

02:14.160 --> 02:23.460
Phone numbers random numbers file names collar's avatars emails usernames domains passwords like so

02:23.460 --> 02:28.710
many things that we could pick from credit card fake credit card numbers account numbers you get it

02:28.950 --> 02:29.550
hopefully.

02:29.580 --> 02:30.660
So back in cloud nine.

02:30.660 --> 02:35.310
Now what I'm going to do is actually empty this file entirely.

02:35.520 --> 02:37.840
Yes you can make a new file if you'd like.

02:37.890 --> 02:41.350
And now I'm going to install faker so we can use it.

02:41.400 --> 02:46.340
So this video I probably could've done a better job explaining this is about two things.

02:46.350 --> 02:47.570
One faker.

02:47.640 --> 02:51.500
But on a higher level packages in general and how you use them.

02:51.510 --> 02:58.050
So this is the workflow for using a package type NPM install the name of the package and hit enter you

02:58.090 --> 03:04.230
know take a second and that on it own installs a package you'll notice I have a new folder here called

03:04.230 --> 03:10.180
node modules inside there is all the F.A. code all this stuff.

03:10.340 --> 03:11.370
So all of that is here now.

03:11.390 --> 03:12.610
I just had to type a single line.

03:12.620 --> 03:13.960
It's pretty awesome.

03:14.750 --> 03:15.720
Now in my app.

03:15.790 --> 03:16.160
Yes.

03:16.190 --> 03:18.300
I can't just start using it immediately.

03:18.300 --> 03:23.390
I can't just say fake or dot e-mail or something.

03:23.660 --> 03:29.360
I first have to tell this file I want to use fake or I have to include it or import it.

03:29.480 --> 03:31.130
And it's very easy.

03:31.130 --> 03:36.980
I'm just going to say Fard faker equals require faker.

03:37.460 --> 03:44.110
So this right here is a variable I can call this anything I could say my faker.

03:44.660 --> 03:46.510
I'm going to call it faker standard.

03:46.700 --> 03:49.130
And this is the part that I can't change.

03:49.130 --> 03:50.560
This has to be faker.

03:50.570 --> 03:56.600
So this is going to go find a package that I've installed in node modules with the name F.A. and then

03:56.600 --> 03:58.570
save it into this variable.

03:58.580 --> 04:05.160
So now whenever I use the word faker referring to this variable I actually am referring to the package.

04:05.480 --> 04:12.590
So then we can do things with it so we could do something like this faker dumb Internet email and that

04:12.590 --> 04:19.120
will generate things like this or this or this or 23.

04:19.220 --> 04:25.270
Simone Walsh Aubree underscore dot tree numbers we've got hot male and female and Yahoo.

04:25.400 --> 04:30.680
So this would be a pain if we were doing this ourselves to come up with 500 individual emails.

04:31.040 --> 04:34.020
And the other thing I'll show you is we're going to use it for dates.

04:34.070 --> 04:36.120
So I'll be able to say fake or not date.

04:36.290 --> 04:39.120
And this is important past.

04:39.300 --> 04:41.110
We don't want to have any future dates.

04:41.150 --> 04:46.460
Basically we want to start with 500 users in our database who have signed up at some point in the past.

04:46.830 --> 04:49.230
So it will generate things like this.

04:49.280 --> 04:51.570
So we also don't want to have to do that ourselves.

04:51.980 --> 04:57.940
So let's start with faker Internet email.

04:58.690 --> 05:04.940
I mean in parentheses at the end sort of like might as well it's a function we're telling it to execute.

05:04.940 --> 05:12.420
If I just do it on its own and I run the file now which is actually yes nothing happens.

05:12.510 --> 05:13.300
Well it's a lie.

05:13.320 --> 05:14.410
It does happen.

05:14.550 --> 05:17.400
This runs but we don't see the result.

05:17.550 --> 05:23.610
And that's because we need to print it so we can do cancel that log like this.

05:23.610 --> 05:24.910
There we go.

05:24.910 --> 05:29.460
Now if I run it again I see OK we get a new e-mail.

05:29.460 --> 05:33.730
Jared Lee lovely Trevor whatever.

05:33.930 --> 05:36.330
So we're getting these fake e-mails.

05:36.420 --> 05:43.230
We're going to use that along with you know copy this and do faker.

05:43.700 --> 05:48.690
Dot dot dot past which is the other thing I showed you.

05:48.830 --> 05:54.040
Going to use those two to generate a 500 user.

05:54.040 --> 05:55.720
So basically this is one user.

05:55.990 --> 06:03.940
So it's kind of fun to play around a faker bit could do you know dot hacker dot abbreviation or finance

06:04.000 --> 06:07.080
dot currency symbol or bitcoin address.

06:07.180 --> 06:19.750
Let's do address and then city so we can do get rid of these two for now Council log faker address that

06:19.780 --> 06:21.500
city.

06:21.510 --> 06:24.840
If we save and run that there we go.

06:24.840 --> 06:28.990
Roberto ville and if we try it again you get something different new.

06:29.010 --> 06:30.810
Alexandra are for it.

06:31.140 --> 06:33.450
So that's the basics of faker.

06:33.510 --> 06:38.790
I will show you if so if you already know javascript skip this section but I just want to show a bit

06:38.790 --> 06:45.690
more sort of the extended version of crash course in node how we can string things together and make

06:45.690 --> 06:47.710
more complex logic.

06:47.730 --> 06:55.360
So let's say I wanted to generate a complete address so I wanted you know a number and the street and

06:55.360 --> 06:55.920
the city.

06:55.920 --> 06:59.800
Let's start with those three so faker has ways of doing that.

06:59.820 --> 07:03.630
You've got what do we have street name and street address.

07:03.720 --> 07:16.460
So let's start with that got faker at address dot street address start there and run this again.

07:16.900 --> 07:17.510
OK.

07:17.750 --> 07:20.580
So there we go that's working.

07:20.900 --> 07:23.130
Then we've got the city right.

07:23.450 --> 07:27.410
And then let's say we want to do a state as well for working in the U.S.

07:30.070 --> 07:30.670
perfect.

07:30.670 --> 07:32.890
So here is the complete address right now.

07:32.890 --> 07:41.010
Every time I say one want three different addresses I would have to copy these lines like that which

07:41.070 --> 07:42.390
is not the end of the world right.

07:42.420 --> 07:43.780
We can copy lines.

07:44.310 --> 07:46.490
But there's a much better way of doing this.

07:46.500 --> 07:53.010
So what we could do instead is create what's known as a function and I'll just call this generate address.

07:53.190 --> 08:00.570
Think of this as a way of reusing our code and the syntax looks like this function a name parentheses

08:00.620 --> 08:10.150
and then brackets and whatever we put in those brackets will be run every time I refer back to generate

08:10.150 --> 08:11.790
an address so on it's own.

08:11.910 --> 08:19.140
If I just save this nothing is going to happen when I hit a UPS when I hit save.

08:19.140 --> 08:20.850
Now I go down here to Node.

08:21.360 --> 08:28.400
We don't get anything but I have to do is now call this function just like this.

08:30.550 --> 08:34.850
Now it saying OK is there something called general address.

08:34.870 --> 08:36.230
Oh yes there is.

08:36.230 --> 08:38.650
All right well let's run the three lines inside of it.

08:39.050 --> 08:50.220
So if I ran this three times now it's only three lines that we get three different addresses of course

08:50.310 --> 08:54.080
we might want to you know compile these together so that they're not on three separate lines.

08:54.180 --> 08:55.710
You want commas between them.

08:55.710 --> 08:56.970
We could really improve it.

08:57.150 --> 09:01.770
But the point is that that's basically how you define a function and we'll be working with functions

09:01.770 --> 09:02.410
later on.

09:02.550 --> 09:04.480
But for now that's all I'm going to show.

09:04.680 --> 09:06.330
OK so that's the basics of faker.

09:06.360 --> 09:11.030
The basics of packages will be installing other packages as well as the basics of node.
