So I know that was a very brief introduction to two lines of node of Javascript.
But we're now moving on to more important things.
Things that are relevant to my out and the first thing we'll do is talk about a package called faker.
So the first question you might have is what is a package.
And basically Noad comes with thousands and thousands of libraries.
You might have heard them called their packages or modules code that other people have written that
we can include instead of for application really simply all we have to do is say install faker which
is a package we're going to use but we'll use another package later called Express or we'll use one
called My as well.
And all I have to say is install my Eskew up install F.A. and there are thousands of them out there
like I said and that's one of the biggest strengths of note is that it has a huge library of all these
packages that you don't have to do everything yourself.
You can go and get some help by including somebody else's package.
So the way we do it is through something called and PM node package manager.
And to install faker which don't worry I'll explain what failure is.
This is all we have to type into our terminal NPM install failure.
But before we go there let me show you what F.A. is.
So faker is a package that there's a bunch of different versions of it.
I think the original one was for Ruby not positive but there's faker kind of implementations for other
languages including javascript and what it does is it really helps us streamline the process of generating
fake data.
So we're going to use it because we want to start off with a bunch of fake data in our database.
I don't know if you noticed I had 518 519 users in my database.
I did not enter those manually and they are they're all unique.
They have a different unique e-mail address.
I did not generate those myself.
So we're going to use failure to do that.
So fakers very simple.
I can say things like faker dot address dot County and it will give me a random county name and it has
hundreds of them.
Or I could say faker did but say name dot job descriptor and it will give me a random job descriptor.
So there's tons of things in here.
Phone numbers random numbers file names collar's avatars emails usernames domains passwords like so
many things that we could pick from credit card fake credit card numbers account numbers you get it
hopefully.
So back in cloud nine.
Now what I'm going to do is actually empty this file entirely.
Yes you can make a new file if you'd like.
And now I'm going to install faker so we can use it.
So this video I probably could've done a better job explaining this is about two things.
One faker.
But on a higher level packages in general and how you use them.
So this is the workflow for using a package type NPM install the name of the package and hit enter you
know take a second and that on it own installs a package you'll notice I have a new folder here called
node modules inside there is all the F.A. code all this stuff.
So all of that is here now.
I just had to type a single line.
It's pretty awesome.
Now in my app.
Yes.
I can't just start using it immediately.
I can't just say fake or dot e-mail or something.
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 easy.
I'm just going to say Fard faker equals require faker.
So this right here is a variable I can call this anything I could say my faker.
I'm going to call it faker standard.
And this is the part that I can't change.
This has to be faker.
So this is going to go find a package that I've installed in node modules with the name F.A. and then
save it into this variable.
So now whenever I use the word faker referring to this variable I actually am referring to the package.
So then we can do things with it so we could do something like this faker dumb Internet email and that
will generate things like this or this or this or 23.
Simone Walsh Aubree underscore dot tree numbers we've got hot male and female and Yahoo.
So this would be a pain if we were doing this ourselves to come up with 500 individual emails.
And the other thing I'll show you is we're going to use it for dates.
So I'll be able to say fake or not date.
And this is important past.
We don't want to have any future dates.
Basically we want to start with 500 users in our database who have signed up at some point in the past.
So it will generate things like this.
So we also don't want to have to do that ourselves.
So let's start with faker Internet email.
I mean in parentheses at the end sort of like might as well 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 is actually yes nothing happens.
Well it's a lie.
It does happen.
This runs but we don't see the result.
And that's because we need to print it so we can do cancel that log like this.
There we go.
Now if I run it again I see OK we get a new e-mail.
Jared Lee lovely Trevor whatever.
So we're getting these fake e-mails.
We're going to use that along with you know copy this and do faker.
Dot dot dot past which is the other thing I showed you.
Going to use those two to generate a 500 user.
So basically this is one user.
So it's kind of fun to play around a faker bit could do you know dot hacker dot abbreviation or finance
dot currency symbol or bitcoin address.
Let's do address and then city so we can do get rid of these two for now Council log faker address that
city.
If we save and run that there we go.
Roberto ville and if we try it again you get something different new.
Alexandra are for it.
So that's the basics of faker.
I will show you if so if you already know javascript skip this section but I just want to show a bit
more sort of the extended version of crash course in node how we can string things together and make
more complex logic.
So let's say I wanted to generate a complete address so I wanted you know a number and the street and
the city.
Let's start with those three so faker has ways of doing that.
You've got what do we have street name and street address.
So let's start with that got faker at address dot street address start there and run this again.
OK.
So there we go that's working.
Then we've got the city right.
And then let's say we want to do a state as well for working in the U.S.
perfect.
So here is the complete address right now.
Every time I say one want three different addresses I would have to copy these lines like that which
is not the end of the world right.
We can copy lines.
But there's a much better way of doing this.
So what we could do instead is create what's known as a function and I'll just call this generate address.
Think of this as a way of reusing our code and the syntax looks like this function a name parentheses
and then brackets and whatever we put in those brackets will be run every time I refer back to generate
an address so on it's own.
If I just save this nothing is going to happen when I hit a UPS when I hit save.
Now I go down here to Node.
We don't get anything but I have to do is now call this function just like this.
Now it saying OK is there something called general address.
Oh yes there is.
All right well let's run the three lines inside of it.
So if I ran this three times now it's only three lines that we get three different addresses of course
we might want to you know compile these together so that they're not on three separate lines.
You want commas between them.
We could really improve it.
But the point is that that's basically how you define a function and we'll be working with functions
later on.
But for now that's all I'm going to show.
OK so that's the basics of faker.
The basics of packages will be installing other packages as well as the basics of node.