All right.
So we're finally at the end of the section where we're actually going to insert 500 users at once.
And you'd think that it'd be easy because we already saw how to insert a single user like this, create
a single object.
We passed it in email and created that, and we just pass in that single one.
But then we have to worry about, okay, well, if we're trying to do 500, technically what we could
do is repeat this entire process 500 times.
But there's a better way than that, because if you think about this, every time we're running a query
here, that's 500 separate queries, inserting one at a time individually, which is not ideal.
What would be much better is to generate all 500 users at once and then do a bulk insert.
Insert them all together like like this.
Here we inserted two, not 500, but this is two people being inserted where we're passing in a list.
Now, this is my SQL though.
Now the way we do that in Node using this library is a bit different.
So I'm going to show you.
I dug through the documentation and found out how to do a bulk insert rather than passing in a single
object like this.
But it's actually expecting is an array of arrays.
So something like this here.
So I'll show you a better example.
We'll actually do it together.
But rather than giving it an object, here is an array called data inside of it three separate arrays.
The first thing is an email.
The second thing is created at.
Then the query that we're doing is now insert into users and we actually do have email comma created
at our traditional syntax followed by values.
And then this question mark will be replaced with whatever comes after it, which is data.
So then we'll take this array here.
And we'll stick it here.
And basically it will insert each one of these has its own row.
So we'll say this is email, this is created at insert that, here's email created at insert that.
So this is the JavaScript way of bridging that gap.
This is how it's done with this library, and this is why it gets wonky and weird when you're working
with other libraries in other languages where they don't, you know, the syntax is different.
They don't support the exact same way that you would work with MySQL.
So it's kind of weird, but once you do it and you have it done, you can just refer back to the code
you previously written.
So let's test that it works first.
So we had inserting data take to comment that out and move down to the bottom, inserting lots of data
and just make this a little bit easier to see the division.
Okay.
So rather than just well, we can leave it as blockage email I guess in oh, email in May at Gmail.
So we have this array of arrays, only three.
But the difference between three and 500 is basically nothing.
It's the difference between doing one and two that we're trying to get around right now.
We saw how to do one, so this should be easy once we get three.
So we save this here and let's just make sure that it works.
I'll also add my connection end and we'll make sure everything else has come to that except the connection
part and we'll run it from.
Our app over here, it looks like three affected rows.
That is good news.
That means that they were inserted.
If we go over here and do select star.
We can see -- is in there and blah.
And where are you?
UG is at the bottom here too.
So this worked.
This successfully entered or inserted three.
The syntax is weird.
I totally agree.
It's different.
I totally get that.
Especially if you're new to JavaScript.
This must all be overwhelming.
Don't worry, we're going to get through it.
We're going to make the app.
But what I want to show is that, again, it's different.
You have to be prepared to kind of play within the rules of whatever language you're working in.
Okay, so we've got this.
But rather than these three, what we really want to do is generate an array with 500 entries and each
one is going to have fake or email and a fake or date.
So to start, I'm actually going to make data empty.
Just like that.
And all we would need to do to add in our first fake array would be data.
Push and push will just add to the end of this array.
It will add inside of it whatever we tell it to.
So we want a new array.
I'll make some space here.
And all we want to do is push in two things.
The first is faker, dot internet, dot email.
And the second thing is faker, dot, dot, dot passed.
So this will generate if we just do a single one oops and do a console.log data and don't do our query
here.
Comment that out and we run it wrong terminal.
If we run it, you can see.
All right, it did it.
We've got our first result in there.
So then logically, if we just repeated this and did another push and another push and I rerun it,
now we end up with three.
So we just need to do that 500 times and we're not going to copy and paste 500 times.
We're going to use a loop and it's just going to be a simple for loop.
So for var i is equal to zero while I is less than 500, add one to I.
So we saw one of these earlier.
If you're not familiar with JavaScript, these are the bread and butter.
It's a way of repeating code.
So this basically says repeat whatever's inside of here 500 times.
So we just pass that in.
And this will now run 500 times.
Each time it's going to generate a new email and a new date.
It's going to push them into the data array.
So we end up with one giant data array so we can print that out.
Let's see what happens.
And you can see as I scroll through here, you'll have to trust that it's 500.
We've got tons of results in here.
Tons and tons of them.
Awesome.
So now all we need to do is uncomment this section.
I'm going to comment out or console.log, but uncomment the part where we're actually taking that data,
which is our giant array full of fake data, and we're inserting it and nothing needs to change.
We have email first followed by created, right?
If you look at our last one, email created it.
Okay, moment of truth.
Here, let's try it.
Save the file.
It looks like 500 effective rows.
Look at how fast that was.
Look at how short our code to do it was.
If you ignore all the white space, it's just this part.
Now.
Let's head over here.
Let's do well.
Let's start by selecting Star.
Awesome.
Look at all of them there.
And I'm actually going to drop everything because we have some bad users in there with time created
at 2000.
So we're going to just do delete from users.
518 of them gone.
Now we do a select star.
Nothing.
But don't worry.
We're just going to rerun our code one more time.
And so easy, so fast.
We now have 500 users in there.
Awesome.
So we'll end it here.
I recommend that you delete everything and just remake it with 500.
Exactly.
We have a couple of exercises using these.
Of course, your data will vary, right?
You're going to have different emails, different dates, but it won't matter for the exercises.
And once we get past those exercises, we'll move on to working with this data in the context of a web
application.
So we saw a lot in this section.
It's very kind of disparate, fast paced, I'm aware.
If you don't know JavaScript, I can imagine it's quite intimidating.
But hopefully the takeaway is not that you're a JavaScript genius, that you could recreate this on
your own, but more that you see that we're able to use JavaScript to insert or select from a MySQL
database, and that we can do things that we could do on our own, like insert a single user.
But we can also do really powerful things that we couldn't do on our own or would take forever, like
inserting 500 users that are randomly generated with just a couple of lines of code.
So this is really our first thing that we've seen that takes advantage of the power you get from using
a language like JavaScript.
Awesome.