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'd be easy because we already saw how to insert a single user like this create a
single object passed in email and created that and we just passed in that single one but then we have
to worry about.
OK well for trying to do 500 technically what we could do is repeat this entire process five hundred
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 you know insert them all together like you know like this.
Here we inserted two not five hundred but this is two people being inserted we're passing in a list.
Now this is MY as well though now the way we do that 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 an array of arrays so something like this here.
So I'll show you a better example would actually do it together.
But rather than giving it an object.
Here is an array called data inside of it three separate erase.
The first thing is an e-mail.
The second thing is created at.
Then the queery that we're doing is now insert into users and we actually do have email Commack 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 it will take this array here.
And will stick it here and basically it will insert each one of these has its own row.
So I will say this is e-mail.
This is created an 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 and 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 my Esky well.
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.
OK.
So we had inserting data take to comments that out and move down to the bottom.
Inserting lots of data just make this a little easier to see the division.
OK.
So rather than just well we can leave it as Blyde email I guess.
Ugh Hatchie mail and mad at gmail.
So we have this array of her race only three but the difference between three and five hundred 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.
Also add my connection and involved make sure everything else has come out except the connection part
and we'll run it from our app over here.
OK 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 meth is in there and blah.
And where are you.
Ugh 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.
OK.
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 e-mail 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 is to add
in our first fake array would be data up push and push would just add to the end of this array.
It 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 email.
And the second thing is faker that date dot past.
So this will generate if we just do a single one ups and do a concert adult log data and don't do our
query here.
Comment that we run it wrong terminal we run it you can see 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 five hundred 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 our eye it's equal to zero.
While it's less than 500 add 1 to eyes.
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're just passed that in and this will now run 500 times each time.
It's going to generate a new e-mail 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 scroll through here.
You'll have to trust that it's 500.
We've got tons of results here tons and tons of them.
Awesome.
OK.
So now all we need to do is uncomment this section when it comes to our constant 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 e-mail first followed by created.
Right.
If you look at our last one e-mail created OK.
Moment of Truth here.
Let's try it save the file
it looks like 500 effect grows.
Look at how fast that was.
Look at how short our code to do it was.
If you ignore all the whitespace it's just this part.
Now let's head over here.
Let's do.
Well let's start right 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.
You know time created set to 000.
So we're going to just do delete from users 518 of them gone.
Now we do like Star nothing but don't worry I'm just going to rerun our code one more time.
And so easy so fast we now have 500 users in there.
Awesome.
OK.
So we'll end it here.
I recommend that you delete everything and just remake it with five hundred.
Exactly.
We have a couple of exercises using these.
Of course your data will vary.
Right you're going have different e-mails 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 disparate fast pace I'm aware.
If you don't know javascript I can imagine it's quite intimidating.
But hopefully the takeaway is not that your 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 minuscule database.
And that we can do things that we can 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 it would take forever like inserting 500 users that
are randomly generated with just a couple 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.