WEBVTT

00:00.120 --> 00:00.560
All right.

00:00.600 --> 00:05.970
So we're finally at the end of the section where we're actually going to insert 500 users at once.

00:06.330 --> 00:11.970
And you'd think that'd be easy because we already saw how to insert a single user like this create a

00:11.970 --> 00:18.960
single object passed in email and created that and we just passed in that single one but then we have

00:18.960 --> 00:19.680
to worry about.

00:19.730 --> 00:25.980
OK well for trying to do 500 technically what we could do is repeat this entire process five hundred

00:25.980 --> 00:26.510
times.

00:26.550 --> 00:31.620
But there's a better way than that because if you think about this every time we're running a query

00:31.620 --> 00:34.040
here that's 500 separate queries.

00:34.170 --> 00:40.920
Inserting one at a time individually which is not ideal what would be much better is to generate all

00:40.920 --> 00:48.230
500 users at once and then do a bulk insert you know insert them all together like you know like this.

00:48.300 --> 00:54.900
Here we inserted two not five hundred but this is two people being inserted we're passing in a list.

00:54.900 --> 01:00.870
Now this is MY as well though now the way we do that node using this library is a bit different.

01:01.260 --> 01:02.460
So I'm going to show you.

01:02.490 --> 01:07.710
I dug through the documentation and found out how to do a bulk insert rather than passing in a single

01:07.710 --> 01:08.840
object like this.

01:08.970 --> 01:16.070
But it's actually expecting an array of arrays so something like this here.

01:16.280 --> 01:19.900
So I'll show you a better example would actually do it together.

01:20.120 --> 01:22.080
But rather than giving it an object.

01:22.160 --> 01:26.670
Here is an array called data inside of it three separate erase.

01:26.810 --> 01:28.430
The first thing is an e-mail.

01:28.430 --> 01:31.150
The second thing is created at.

01:31.400 --> 01:38.390
Then the queery that we're doing is now insert into users and we actually do have email Commack created

01:38.390 --> 01:44.840
at our traditional syntax followed by values and then this question mark will be replaced with whatever

01:44.840 --> 01:46.350
comes after it which is data.

01:46.490 --> 01:48.260
So then it will take this array here.

01:50.080 --> 01:55.210
And will stick it here and basically it will insert each one of these has its own row.

01:55.300 --> 01:56.460
So I will say this is e-mail.

01:56.470 --> 01:59.960
This is created an insert that here's email created at insert that.

01:59.980 --> 02:03.040
So this is the javascript way of bridging that gap.

02:03.070 --> 02:07.780
This is how it's done with this library and this is why it gets wonky and weird when you're working

02:07.780 --> 02:12.150
with other libraries and other languages where they don't you know the syntax is different.

02:12.160 --> 02:15.450
They don't support the exact same way that you would work with my Esky well.

02:15.580 --> 02:20.950
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

02:20.950 --> 02:22.390
previously written.

02:22.690 --> 02:24.800
So let's test that it works first.

02:25.150 --> 02:25.440
OK.

02:25.450 --> 02:31.580
So we had inserting data take to comments that out and move down to the bottom.

02:31.630 --> 02:38.880
Inserting lots of data just make this a little easier to see the division.

02:39.370 --> 02:40.160
OK.

02:40.750 --> 02:44.300
So rather than just well we can leave it as Blyde email I guess.

02:44.400 --> 02:46.500
Ugh Hatchie mail and mad at gmail.

02:46.540 --> 02:51.610
So we have this array of her race only three but the difference between three and five hundred is basically

02:51.610 --> 02:52.360
nothing.

02:52.390 --> 02:57.170
It's the difference between doing one and two that we're trying to get around right now.

02:57.400 --> 03:05.140
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

03:05.140 --> 03:06.650
sure that it works.

03:06.670 --> 03:13.300
Also add my connection and involved make sure everything else has come out except the connection part

03:14.200 --> 03:19.730
and we'll run it from our app over here.

03:20.030 --> 03:22.110
OK it looks like three affected rows.

03:22.160 --> 03:23.280
That is good news.

03:23.280 --> 03:24.940
That means that they were inserted.

03:25.020 --> 03:31.420
If we go over here and do select star we can see meth is in there and blah.

03:32.000 --> 03:33.310
And where are you.

03:33.310 --> 03:35.280
Ugh is at the bottom here too.

03:35.360 --> 03:39.310
So this worked this successfully entered or inserted three.

03:39.350 --> 03:40.470
The syntax is weird.

03:40.580 --> 03:41.460
I totally agree.

03:41.480 --> 03:42.380
It's different.

03:42.380 --> 03:43.460
I totally get that.

03:43.490 --> 03:46.970
Especially if you're new to javascript this must all be overwhelming.

03:46.970 --> 03:50.070
Don't worry we're going to get through it we're going to make the app.

03:50.180 --> 03:53.110
But what I want to show is that again it's different.

03:53.120 --> 03:58.230
You have to be prepared to kind of play within the rules of whatever language you're working in.

03:58.680 --> 03:58.960
OK.

03:59.000 --> 03:59.800
So we've got this.

03:59.810 --> 04:07.480
But rather than these three what we really want to do is generate an array with 500 entries and each

04:07.480 --> 04:11.040
one is going to have fake or e-mail and a fake or date.

04:11.490 --> 04:19.240
So to start I'm actually going to make data empty just like that and all we would need to do is to add

04:19.240 --> 04:26.870
in our first fake array would be data up push and push would just add to the end of this array.

04:26.910 --> 04:29.180
It add inside of it whatever we tell it to.

04:29.230 --> 04:31.450
So we want a new array.

04:31.750 --> 04:33.400
I'll make some space here.

04:36.090 --> 04:37.740
And all we want to do is push in two things.

04:37.740 --> 04:42.980
The first is faker dot Internet email.

04:43.560 --> 04:48.780
And the second thing is faker that date dot past.

04:49.560 --> 04:58.320
So this will generate if we just do a single one ups and do a concert adult log data and don't do our

04:58.320 --> 04:59.190
query here.

05:00.360 --> 05:08.270
Comment that we run it wrong terminal we run it you can see right it did it.

05:08.310 --> 05:10.930
We've got our first result in there.

05:11.100 --> 05:18.450
So then logically if we just repeated this and did another push and another push and I rerun it now

05:18.450 --> 05:19.710
we end up with three.

05:19.950 --> 05:24.960
So we just need to do that five hundred times and we're not going to copy and paste 500 times.

05:25.140 --> 05:28.640
We're going to use a loop and it's just going to be a simple for loop.

05:28.650 --> 05:31.310
So for our eye it's equal to zero.

05:31.390 --> 05:35.390
While it's less than 500 add 1 to eyes.

05:35.410 --> 05:40.160
We saw one of these earlier if you're not familiar with javascript these are the bread and butter.

05:40.200 --> 05:41.480
It's a way of repeating code.

05:41.550 --> 05:45.340
So this basically says repeat whatever's inside of here 500 times.

05:45.570 --> 05:52.110
So we're just passed that in and this will now run 500 times each time.

05:52.110 --> 05:54.560
It's going to generate a new e-mail and a new date.

05:54.600 --> 06:00.180
It's going to push them into the data array so we end up with one giant data array so we can print that

06:00.180 --> 06:00.740
out.

06:01.020 --> 06:02.210
Let's see what happens.

06:03.950 --> 06:06.030
And you can see scroll through here.

06:06.170 --> 06:07.970
You'll have to trust that it's 500.

06:08.000 --> 06:11.560
We've got tons of results here tons and tons of them.

06:12.760 --> 06:13.350
Awesome.

06:14.230 --> 06:14.950
OK.

06:15.290 --> 06:22.280
So now all we need to do is uncomment this section when it comes to our constant log.

06:22.520 --> 06:28.130
But uncomment the part where we're actually taking that data which is our giant array full of fake data

06:28.850 --> 06:30.090
and we're inserting it.

06:31.010 --> 06:31.910
And nothing needs to change.

06:31.910 --> 06:34.140
We have e-mail first followed by created.

06:34.250 --> 06:34.660
Right.

06:34.790 --> 06:38.560
If you look at our last one e-mail created OK.

06:38.630 --> 06:39.860
Moment of Truth here.

06:40.340 --> 06:43.540
Let's try it save the file

06:46.340 --> 06:48.320
it looks like 500 effect grows.

06:48.320 --> 06:49.750
Look at how fast that was.

06:49.760 --> 06:52.370
Look at how short our code to do it was.

06:52.550 --> 06:56.040
If you ignore all the whitespace it's just this part.

06:56.050 --> 06:57.140
Now let's head over here.

06:57.220 --> 06:57.990
Let's do.

06:58.090 --> 07:00.700
Well let's start right selecting star.

07:00.700 --> 07:02.000
Awesome.

07:02.080 --> 07:08.320
Look at all of them there and I'm actually going to drop everything because we have some bad users in

07:08.320 --> 07:08.940
there.

07:09.170 --> 07:12.760
You know time created set to 000.

07:12.760 --> 07:20.950
So we're going to just do delete from users 518 of them gone.

07:20.950 --> 07:27.440
Now we do like Star nothing but don't worry I'm just going to rerun our code one more time.

07:28.440 --> 07:33.430
And so easy so fast we now have 500 users in there.

07:33.900 --> 07:34.440
Awesome.

07:34.590 --> 07:34.970
OK.

07:35.010 --> 07:36.530
So we'll end it here.

07:36.540 --> 07:39.830
I recommend that you delete everything and just remake it with five hundred.

07:39.830 --> 07:40.850
Exactly.

07:40.860 --> 07:43.130
We have a couple of exercises using these.

07:43.140 --> 07:44.530
Of course your data will vary.

07:44.550 --> 07:49.290
Right you're going have different e-mails different dates but it won't matter for the exercises.

07:49.320 --> 07:55.020
And once we get past those exercises we'll move on to working with this data in the context of a web

07:55.020 --> 07:56.130
application.

07:56.130 --> 07:58.040
So we saw a lot in this section.

07:58.200 --> 08:02.240
It's very disparate fast pace I'm aware.

08:02.370 --> 08:05.020
If you don't know javascript I can imagine it's quite intimidating.

08:05.100 --> 08:10.040
But hopefully the takeaway is not that your javascript genius that you could recreate this on your own

08:10.380 --> 08:17.950
but more that you see that we're able to use javascript to insert or select from a minuscule database.

08:18.060 --> 08:23.550
And that we can do things that we can do on our own like insert a single user but we can also do really

08:23.550 --> 08:29.160
powerful things that we couldn't do on our own or it would take forever like inserting 500 users that

08:29.160 --> 08:32.670
are randomly generated with just a couple lines of code.

08:32.700 --> 08:37.590
So this is really our first thing that we've seen that takes advantage of the power you get from using

08:37.590 --> 08:39.600
a language like javascript.

08:39.600 --> 08:39.970
Awesome.
