So I know that was a very brief introduction to two lines of code and of JavaScript.
But we're now moving on to more important things, things that are relevant to my SQL.
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 Node comes with thousands and thousands of libraries.
You might have heard them called or packages or modules code that other people have written that we
can include inside of our application.
Really simply all I 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 MySQL.
And all I have to say is install my SQL install faker and there are thousands of them out there, like
I said.
And that's one of the biggest strengths of Node is that it has a huge library of all these packages
so 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 NPM node package manager and to install faker, which
don't worry, I'll explain what faker is.
This is all we have to type into our terminal and PM install faker.
But before we go there, let me show you what Faker is.
So Faker is a package that there's a bunch of different versions of it.
There's I think the original one was for Ruby, I'm not positive, but there's Faker kind of implementations
for other languages, including JavaScript.
And what it does is it really helps us.
It streamlines 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 email address.
I did not generate those myself.
So we're going to use Faker to do that.
So Faker is 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 fake or let's 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, colors, 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.
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.
So this video, I probably could have 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.
We type NPM, install the name of the package and hit enter and it will take a second.
And that on its own installs a package.
You'll notice I have a new folder here called Node Modules inside.
There's all the faker code, all this stuff.
So all that is here now.
I just had to type a single line.
It's pretty awesome.
Now in my app, guys, I can't just start using it immediately.
I can't just say fake or dot email 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 var faker equals require faker.
So this right here, it's 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 Faker 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, internet, dot email and that will generate things like this
or this or this or a 23 Simone Walsh Aubry underscore dot tree numbers.
We've got Hotmail and Gmail and Yahoo!
So this would be a pain if we were doing this ourselves to come up with 500 individual emails.
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
or date and this is important past.
So 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 fake internet dot email and we need parentheses at the end.
Sort of like my SQL.
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 app JS.
Nothing happens.
Well, that'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 console.log like this.
There we go.
Now, if I run it again, I see.
Okay.
We get a new email.
Jared.
Leila.
Trevor.
Whatever.
So we're getting these fake emails.
We're going to use that along with.
I copy this and do faker dot.
Date dot passed.
Which is the other thing I showed you.
We're 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 with Faker.
But do you know dot hacker, dot abbreviation or finance, dot currency symbol or Bitcoin address,
let's do address and then city.
So we could do we'll get rid of these too for now console dot log add faker dot address, dot city.
And if we save and run that.
There we go.
Roberto ville.
And if we try it again, we get something different.
New Alexandrov fought.
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 a number and the street and a city.
Let's start with those three.
So Faker has ways of doing that.
We've got what do we have?
Street name and street address.
So let's start with that.
We've got faker address, Dot Street address.
Let's start there and run this again.
Okay, so there we go.
That's working.
Then we've got the city, right.
And then let's say we wanted to do a state as well.
If we're working in the US.
Perfect.
So here's a complete address right now.
Every time let's say I 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 address.
So on its own, if I just save this.
Nothing is going to happen when I hit.
Oops.
When I hit save, now I go down here to Node.
We don't get anything.
What I have to do is now call this function just like this.
Now.
It's saying, okay, is there something called generate 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 to do that.
And we get three different addresses.
Of course, we might want to compile these together so that they're not on three separate lines.
Maybe 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.
So that's the basics of faker.
The basics of packages will be installing other packages as well as the basics of node.