Okay.
So let's refresh what our master plan was here.
The whole point of using Faker was eventually we want to be able to insert 500 of these where we have
an email that's randomized and we also want to have a date.
I created that date that is randomized.
So let's focus on that part.
We saw, just to refresh your memory, that we can do things like this.
Let me I'm going to comment all of this out and to just run one line at the bottom which is console.log
faker, dot, dot, dot passed and that just gives us a date.
In the past, I think it's within a year and maybe two.
And let's see what I'm screwing up here.
We've got faker update passed console.log and we'll go over to the correct terminal here and we'll run
Node JS.
Okay.
Now we're getting a date.
Now, you may have noticed a problem here.
This date syntax, which I'll put here just so we can take a look.
It's not valid JavaScript as it is.
It's complaining, but this is not going to jive well with our database.
Remember that our database, my SQL is expecting something like this where we have the year.
I'll just do it this year, year, year, dash month, month, dash de day, space minute or what do
we have our our minute, minute, second, second.
So sort of, you know, the time part looks okay.
Then we get all this stuff that we don't want.
The year, though, the month, the day of the week.
We don't want the month spelled out.
We just want the day.
So this is a problem.
So to prove that it's problematic, let's try something.
Let's try taking, say, this date right here.
Monday, April 24th, 2017.
Copy it over and let's insert it manually into my SQL and see what happens.
So I'll do an insert into users.
We have email and created at values and we'll just pick a random email.
Let's do dusty at gmail.com, comma, and then we'll pass in this giant JavaScript date or the JavaScript
style date string.
What do you think will happen when we insert this?
So I'm just going to paste it in and hit enter.
All right.
One row affected, one warning.
Well, let's see what happened.
Let's do a select star from users.
Actually lets you select start from users where email equals dusty at gmail.com.
And look at that created that is all screwed up.
It's set to 200000.
It's a problem that's not going to work for us.
It's wrong, right?
So that's proving to you the whole point of this is to show you that the JavaScript way of doing data
that we get from Faker is not compatible with my SQL.
So let's just see what happens.
I guess I can keep this here, but let's see what happens if we go to our app guys and we try and insert.
A person with a faker date.
What do we get an error message or what happens?
So we'll do this.
I'll make some space here, and rather than just email, we'll also do create a debt which is fake or
date past.
So the same thing we did down here which generated this date, but now we're doing it dynamically here
with an email and it created that date both dynamic.
We're going to insert person using the same syntax and let's see what happens.
Okay, hold our breath here.
Node App Tags.
And what do you know?
We don't get an error.
So what's going on?
Did it work?
Let's come over here.
Unfortunately, we're not able to do our select star in order by created it because our created at dates
now vary.
So it's no longer that the last one added in has the most recent date.
So we'll just do a select star from users and what we'll look for.
In my case, I happen to know the data that we were working with.
This was the one that was just added Lucy of 38.
Yours, of course, will be different.
But notice that the date time they created it isn't 0000 like we had for Dusty.
This is the one where we manually took this.
The same exact thing coming from JavaScript, but we did it ourselves and it didn't work.
But then when we used person instead of variable, then we use connection query and we passed it in.
It somehow worked.
And basically it comes down to the magic of this library.
And I'm showing this to you to illustrate another point, which is there's a lot of work that goes into
sewing the stitches between something like Node and MySQL.
So if we go to the docs here, if we do a search for I'll just do date time and I think it's down.
Here we go.
If we look, there's something right here that says date objects are converted to year, year, year,
year, dash month, month, dash day date.
Basically the syntax we're looking for, they're converted to that.
So as part of this library, it takes it upon itself to bridge my SQL and node.
It's it says, all right, if this is a JavaScript date, which is what we're working with from Faker,
well, I know that the user is trying to insert it into a mysql table that has no idea about JavaScript
dates, so we're going to convert it before we send it.
And then one other kind of interesting thing we can do is save this to a variable and we'll call it
end result.
And if I just do a console.log end result SQL, which again all new stuff, I know it can be overwhelming.
What this will do is show us that compiled SQL that is being sent over.
So this is kind of before what person looks like and this will be an after.
Let's take a look.
Well, we have to go to Node.
Okay.
So you can see before we had created it looks like this.
Tuesday May.
It's a wrong format, that's JavaScript format.
But sometime before we actually sent this over, it was converted to create a that is equal to 20 1605
Dash 24 and this is the correct MySQL syntax.
So this is kind of cool to be able to see the end result SQL that's being sent over.
This is valid SQL that we could just copy this over and yeah, it might look slightly different.
These are escaped, which you do not have to do.
But if you notice, this is SQL syntax and we could just paste that in.
So I'm going to get rid of that.
But I just wanted you to see that you could do that.
And more importantly, the whole point here is to show you that there's stuff happening behind the scenes
which can be great and useful, but it's also the stuff that you just don't know what's happening sometimes,
and you run into it, you discover it, but it's a job of these libraries to connect the two.
And there's a discrepancy in this case between how JavaScript handles dates and how MySQL handles or
expects them.
So behind the scenes, this library that we're not writing, we're not in charge of somebody is that
library is patching them.
It's fixing it for us.
Okay, so we've inserted a single user.
Now all we have left is to figure out how we insert 500.
That's the next video.