1 00:00:00,150 --> 00:00:00,570 All right. 2 00:00:00,570 --> 00:00:05,970 So we're finally at the end of the section where we're actually going to insert 500 users at once. 3 00:00:06,120 --> 00:00:11,940 And you'd think that it'd be easy because we already saw how to insert a single user like this, create 4 00:00:11,940 --> 00:00:13,230 a single object. 5 00:00:13,770 --> 00:00:17,550 We passed it in email and created that, and we just pass in that single one. 6 00:00:17,940 --> 00:00:23,550 But then we have to worry about, okay, well, if we're trying to do 500, technically what we could 7 00:00:23,550 --> 00:00:26,490 do is repeat this entire process 500 times. 8 00:00:26,490 --> 00:00:31,650 But there's a better way than that, because if you think about this, every time we're running a query 9 00:00:31,650 --> 00:00:38,190 here, that's 500 separate queries, inserting one at a time individually, which is not ideal. 10 00:00:38,220 --> 00:00:44,760 What would be much better is to generate all 500 users at once and then do a bulk insert. 11 00:00:44,970 --> 00:00:48,210 Insert them all together like like this. 12 00:00:48,210 --> 00:00:54,920 Here we inserted two, not 500, but this is two people being inserted where we're passing in a list. 13 00:00:54,930 --> 00:00:56,430 Now, this is my SQL though. 14 00:00:56,430 --> 00:01:00,780 Now the way we do that in Node using this library is a bit different. 15 00:01:01,110 --> 00:01:02,490 So I'm going to show you. 16 00:01:02,490 --> 00:01:07,770 I dug through the documentation and found out how to do a bulk insert rather than passing in a single 17 00:01:07,770 --> 00:01:08,850 object like this. 18 00:01:08,850 --> 00:01:13,200 But it's actually expecting is an array of arrays. 19 00:01:14,100 --> 00:01:15,990 So something like this here. 20 00:01:16,140 --> 00:01:18,570 So I'll show you a better example. 21 00:01:18,630 --> 00:01:19,980 We'll actually do it together. 22 00:01:19,980 --> 00:01:26,520 But rather than giving it an object, here is an array called data inside of it three separate arrays. 23 00:01:26,670 --> 00:01:28,440 The first thing is an email. 24 00:01:28,440 --> 00:01:30,660 The second thing is created at. 25 00:01:31,280 --> 00:01:38,420 Then the query that we're doing is now insert into users and we actually do have email comma created 26 00:01:38,420 --> 00:01:41,000 at our traditional syntax followed by values. 27 00:01:41,420 --> 00:01:46,340 And then this question mark will be replaced with whatever comes after it, which is data. 28 00:01:46,340 --> 00:01:48,230 So then we'll take this array here. 29 00:01:50,050 --> 00:01:51,220 And we'll stick it here. 30 00:01:51,640 --> 00:01:55,210 And basically it will insert each one of these has its own row. 31 00:01:55,210 --> 00:01:59,830 So we'll say this is email, this is created at insert that, here's email created at insert that. 32 00:01:59,830 --> 00:02:02,970 So this is the JavaScript way of bridging that gap. 33 00:02:02,980 --> 00: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 34 00:02:07,780 --> 00:02:12,220 with other libraries in other languages where they don't, you know, the syntax is different. 35 00:02:12,220 --> 00:02:15,430 They don't support the exact same way that you would work with MySQL. 36 00:02:15,430 --> 00:02:20,860 So it's kind of weird, but once you do it and you have it done, you can just refer back to the code 37 00:02:20,860 --> 00:02:22,270 you previously written. 38 00:02:22,570 --> 00:02:24,730 So let's test that it works first. 39 00:02:25,480 --> 00:02:34,300 So we had inserting data take to comment that out and move down to the bottom, inserting lots of data 40 00:02:36,100 --> 00:02:38,590 and just make this a little bit easier to see the division. 41 00:02:39,370 --> 00:02:39,880 Okay. 42 00:02:40,600 --> 00:02:46,510 So rather than just well, we can leave it as blockage email I guess in oh, email in May at Gmail. 43 00:02:46,510 --> 00:02:49,060 So we have this array of arrays, only three. 44 00:02:49,060 --> 00:02:52,300 But the difference between three and 500 is basically nothing. 45 00:02:52,300 --> 00:02:57,190 It's the difference between doing one and two that we're trying to get around right now. 46 00:02:57,220 --> 00:03:00,880 We saw how to do one, so this should be easy once we get three. 47 00:03:01,630 --> 00:03:06,610 So we save this here and let's just make sure that it works. 48 00:03:06,640 --> 00:03:12,910 I'll also add my connection end and we'll make sure everything else has come to that except the connection 49 00:03:12,910 --> 00:03:16,210 part and we'll run it from. 50 00:03:18,620 --> 00:03:22,050 Our app over here, it looks like three affected rows. 51 00:03:22,070 --> 00:03:23,300 That is good news. 52 00:03:23,300 --> 00:03:24,710 That means that they were inserted. 53 00:03:24,980 --> 00:03:27,080 If we go over here and do select star. 54 00:03:28,430 --> 00:03:31,150 We can see -- is in there and blah. 55 00:03:31,910 --> 00:03:33,290 And where are you? 56 00:03:33,290 --> 00:03:35,030 UG is at the bottom here too. 57 00:03:35,210 --> 00:03:36,110 So this worked. 58 00:03:36,110 --> 00:03:39,290 This successfully entered or inserted three. 59 00:03:39,320 --> 00:03:40,310 The syntax is weird. 60 00:03:40,310 --> 00:03:41,480 I totally agree. 61 00:03:41,480 --> 00:03:42,340 It's different. 62 00:03:42,350 --> 00:03:43,480 I totally get that. 63 00:03:43,490 --> 00:03:45,020 Especially if you're new to JavaScript. 64 00:03:45,020 --> 00:03:46,640 This must all be overwhelming. 65 00:03:46,850 --> 00:03:48,950 Don't worry, we're going to get through it. 66 00:03:48,950 --> 00:03:50,090 We're going to make the app. 67 00:03:50,120 --> 00:03:53,120 But what I want to show is that, again, it's different. 68 00:03:53,120 --> 00:03:57,980 You have to be prepared to kind of play within the rules of whatever language you're working in. 69 00:03:58,700 --> 00:03:59,810 Okay, so we've got this. 70 00:03:59,810 --> 00:04:07,520 But rather than these three, what we really want to do is generate an array with 500 entries and each 71 00:04:07,520 --> 00:04:10,820 one is going to have fake or email and a fake or date. 72 00:04:11,330 --> 00:04:14,540 So to start, I'm actually going to make data empty. 73 00:04:15,350 --> 00:04:16,310 Just like that. 74 00:04:17,550 --> 00:04:22,440 And all we would need to do to add in our first fake array would be data. 75 00:04:23,010 --> 00:04:26,850 Push and push will just add to the end of this array. 76 00:04:26,880 --> 00:04:29,170 It will add inside of it whatever we tell it to. 77 00:04:29,190 --> 00:04:30,750 So we want a new array. 78 00:04:31,740 --> 00:04:33,270 I'll make some space here. 79 00:04:35,980 --> 00:04:37,780 And all we want to do is push in two things. 80 00:04:37,780 --> 00:04:42,910 The first is faker, dot internet, dot email. 81 00:04:43,450 --> 00:04:48,400 And the second thing is faker, dot, dot, dot passed. 82 00:04:49,450 --> 00:04:58,720 So this will generate if we just do a single one oops and do a console.log data and don't do our query 83 00:04:58,720 --> 00:04:59,230 here. 84 00:05:00,300 --> 00:05:04,530 Comment that out and we run it wrong terminal. 85 00:05:05,160 --> 00:05:07,200 If we run it, you can see. 86 00:05:07,200 --> 00:05:08,260 All right, it did it. 87 00:05:08,280 --> 00:05:10,830 We've got our first result in there. 88 00:05:10,920 --> 00:05:17,850 So then logically, if we just repeated this and did another push and another push and I rerun it, 89 00:05:18,180 --> 00:05:19,760 now we end up with three. 90 00:05:19,770 --> 00:05:25,030 So we just need to do that 500 times and we're not going to copy and paste 500 times. 91 00:05:25,050 --> 00:05:28,590 We're going to use a loop and it's just going to be a simple for loop. 92 00:05:28,590 --> 00:05:35,280 So for var i is equal to zero while I is less than 500, add one to I. 93 00:05:35,310 --> 00:05:36,600 So we saw one of these earlier. 94 00:05:36,600 --> 00:05:39,870 If you're not familiar with JavaScript, these are the bread and butter. 95 00:05:40,200 --> 00:05:41,490 It's a way of repeating code. 96 00:05:41,490 --> 00:05:45,390 So this basically says repeat whatever's inside of here 500 times. 97 00:05:45,390 --> 00:05:46,890 So we just pass that in. 98 00:05:47,790 --> 00:05:51,140 And this will now run 500 times. 99 00:05:51,150 --> 00:05:54,510 Each time it's going to generate a new email and a new date. 100 00:05:54,540 --> 00:05:56,400 It's going to push them into the data array. 101 00:05:56,400 --> 00:06:00,720 So we end up with one giant data array so we can print that out. 102 00:06:00,990 --> 00:06:02,130 Let's see what happens. 103 00:06:03,890 --> 00:06:07,860 And you can see as I scroll through here, you'll have to trust that it's 500. 104 00:06:07,880 --> 00:06:10,400 We've got tons of results in here. 105 00:06:10,490 --> 00:06:11,630 Tons and tons of them. 106 00:06:12,760 --> 00:06:13,450 Awesome. 107 00:06:15,160 --> 00:06:19,290 So now all we need to do is uncomment this section. 108 00:06:19,300 --> 00:06:25,360 I'm going to comment out or console.log, but uncomment the part where we're actually taking that data, 109 00:06:25,900 --> 00:06:31,900 which is our giant array full of fake data, and we're inserting it and nothing needs to change. 110 00:06:31,900 --> 00:06:34,750 We have email first followed by created, right? 111 00:06:34,750 --> 00:06:37,300 If you look at our last one, email created it. 112 00:06:37,870 --> 00:06:39,280 Okay, moment of truth. 113 00:06:39,280 --> 00:06:41,140 Here, let's try it. 114 00:06:42,730 --> 00:06:43,750 Save the file. 115 00:06:46,500 --> 00:06:48,330 It looks like 500 effective rows. 116 00:06:48,330 --> 00:06:49,620 Look at how fast that was. 117 00:06:49,650 --> 00:06:52,050 Look at how short our code to do it was. 118 00:06:52,530 --> 00:06:54,960 If you ignore all the white space, it's just this part. 119 00:06:55,990 --> 00:06:56,200 Now. 120 00:06:56,200 --> 00:06:57,210 Let's head over here. 121 00:06:57,250 --> 00:06:58,330 Let's do well. 122 00:06:58,330 --> 00:06:59,890 Let's start by selecting Star. 123 00:07:00,710 --> 00:07:01,490 Awesome. 124 00:07:02,120 --> 00:07:03,290 Look at all of them there. 125 00:07:04,490 --> 00:07:11,030 And I'm actually going to drop everything because we have some bad users in there with time created 126 00:07:11,040 --> 00:07:12,580 at 2000. 127 00:07:12,590 --> 00:07:17,690 So we're going to just do delete from users. 128 00:07:18,940 --> 00:07:20,740 518 of them gone. 129 00:07:20,770 --> 00:07:22,390 Now we do a select star. 130 00:07:23,320 --> 00:07:24,070 Nothing. 131 00:07:24,400 --> 00:07:25,650 But don't worry. 132 00:07:25,660 --> 00:07:27,490 We're just going to rerun our code one more time. 133 00:07:28,350 --> 00:07:30,570 And so easy, so fast. 134 00:07:31,200 --> 00:07:33,210 We now have 500 users in there. 135 00:07:33,870 --> 00:07:34,550 Awesome. 136 00:07:34,950 --> 00:07:36,120 So we'll end it here. 137 00:07:36,480 --> 00:07:39,840 I recommend that you delete everything and just remake it with 500. 138 00:07:39,840 --> 00:07:40,620 Exactly. 139 00:07:40,650 --> 00:07:43,060 We have a couple of exercises using these. 140 00:07:43,080 --> 00:07:44,720 Of course, your data will vary, right? 141 00:07:44,760 --> 00:07:49,290 You're going to have different emails, different dates, but it won't matter for the exercises. 142 00:07:49,290 --> 00:07:55,050 And once we get past those exercises, we'll move on to working with this data in the context of a web 143 00:07:55,050 --> 00:07:55,830 application. 144 00:07:55,980 --> 00:07:58,140 So we saw a lot in this section. 145 00:07:58,170 --> 00:08:01,770 It's very kind of disparate, fast paced, I'm aware. 146 00:08:02,310 --> 00:08:05,010 If you don't know JavaScript, I can imagine it's quite intimidating. 147 00:08:05,010 --> 00:08:09,660 But hopefully the takeaway is not that you're a JavaScript genius, that you could recreate this on 148 00:08:09,660 --> 00:08:17,310 your own, but more that you see that we're able to use JavaScript to insert or select from a MySQL 149 00:08:17,310 --> 00:08:22,440 database, and that we can do things that we could do on our own, like insert a single user. 150 00:08:22,440 --> 00:08:27,210 But we can also do really powerful things that we couldn't do on our own or would take forever, like 151 00:08:27,210 --> 00:08:32,220 inserting 500 users that are randomly generated with just a couple of lines of code. 152 00:08:32,610 --> 00:08:37,620 So this is really our first thing that we've seen that takes advantage of the power you get from using 153 00:08:37,620 --> 00:08:39,030 a language like JavaScript. 154 00:08:39,570 --> 00:08:40,190 Awesome.