1 00:00:00,180 --> 00:00:00,570 Okay. 2 00:00:00,570 --> 00:00:03,550 So let's refresh what our master plan was here. 3 00:00:03,570 --> 00:00:09,630 The whole point of using Faker was eventually we want to be able to insert 500 of these where we have 4 00:00:09,630 --> 00:00:13,800 an email that's randomized and we also want to have a date. 5 00:00:13,800 --> 00:00:15,960 I created that date that is randomized. 6 00:00:15,960 --> 00:00:17,520 So let's focus on that part. 7 00:00:17,730 --> 00:00:21,750 We saw, just to refresh your memory, that we can do things like this. 8 00:00:22,410 --> 00:00:28,230 Let me I'm going to comment all of this out and to just run one line at the bottom which is console.log 9 00:00:28,650 --> 00:00:34,800 faker, dot, dot, dot passed and that just gives us a date. 10 00:00:34,800 --> 00:00:38,130 In the past, I think it's within a year and maybe two. 11 00:00:38,970 --> 00:00:41,250 And let's see what I'm screwing up here. 12 00:00:41,910 --> 00:00:49,110 We've got faker update passed console.log and we'll go over to the correct terminal here and we'll run 13 00:00:49,110 --> 00:00:50,790 Node JS. 14 00:00:51,960 --> 00:00:52,480 Okay. 15 00:00:52,800 --> 00:00:53,970 Now we're getting a date. 16 00:00:54,000 --> 00:00:56,160 Now, you may have noticed a problem here. 17 00:00:56,190 --> 00:01:00,420 This date syntax, which I'll put here just so we can take a look. 18 00:01:01,380 --> 00:01:03,030 It's not valid JavaScript as it is. 19 00:01:03,030 --> 00:01:07,080 It's complaining, but this is not going to jive well with our database. 20 00:01:07,260 --> 00:01:13,470 Remember that our database, my SQL is expecting something like this where we have the year. 21 00:01:13,710 --> 00:01:20,220 I'll just do it this year, year, year, dash month, month, dash de day, space minute or what do 22 00:01:20,220 --> 00:01:23,160 we have our our minute, minute, second, second. 23 00:01:23,800 --> 00:01:27,290 So sort of, you know, the time part looks okay. 24 00:01:27,310 --> 00:01:29,320 Then we get all this stuff that we don't want. 25 00:01:29,920 --> 00:01:33,990 The year, though, the month, the day of the week. 26 00:01:34,000 --> 00:01:35,620 We don't want the month spelled out. 27 00:01:35,620 --> 00:01:36,850 We just want the day. 28 00:01:36,850 --> 00:01:38,320 So this is a problem. 29 00:01:38,680 --> 00:01:41,920 So to prove that it's problematic, let's try something. 30 00:01:42,100 --> 00:01:45,460 Let's try taking, say, this date right here. 31 00:01:45,460 --> 00:01:47,560 Monday, April 24th, 2017. 32 00:01:47,560 --> 00:01:52,930 Copy it over and let's insert it manually into my SQL and see what happens. 33 00:01:53,170 --> 00:01:56,440 So I'll do an insert into users. 34 00:01:56,440 --> 00:02:04,030 We have email and created at values and we'll just pick a random email. 35 00:02:04,030 --> 00:02:13,900 Let's do dusty at gmail.com, comma, and then we'll pass in this giant JavaScript date or the JavaScript 36 00:02:13,900 --> 00:02:15,280 style date string. 37 00:02:17,110 --> 00:02:19,300 What do you think will happen when we insert this? 38 00:02:19,510 --> 00:02:23,410 So I'm just going to paste it in and hit enter. 39 00:02:23,950 --> 00:02:24,250 All right. 40 00:02:24,250 --> 00:02:26,110 One row affected, one warning. 41 00:02:26,230 --> 00:02:28,040 Well, let's see what happened. 42 00:02:28,060 --> 00:02:31,210 Let's do a select star from users. 43 00:02:31,840 --> 00:02:38,830 Actually lets you select start from users where email equals dusty at gmail.com. 44 00:02:40,510 --> 00:02:43,600 And look at that created that is all screwed up. 45 00:02:43,600 --> 00:02:45,490 It's set to 200000. 46 00:02:45,490 --> 00:02:48,070 It's a problem that's not going to work for us. 47 00:02:48,160 --> 00:02:49,540 It's wrong, right? 48 00:02:49,930 --> 00:02:54,730 So that's proving to you the whole point of this is to show you that the JavaScript way of doing data 49 00:02:54,730 --> 00:02:59,800 that we get from Faker is not compatible with my SQL. 50 00:03:00,820 --> 00:03:03,190 So let's just see what happens. 51 00:03:03,370 --> 00:03:12,250 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. 52 00:03:13,160 --> 00:03:15,230 A person with a faker date. 53 00:03:15,680 --> 00:03:17,780 What do we get an error message or what happens? 54 00:03:17,780 --> 00:03:19,690 So we'll do this. 55 00:03:19,700 --> 00:03:27,850 I'll make some space here, and rather than just email, we'll also do create a debt which is fake or 56 00:03:28,190 --> 00:03:29,660 date past. 57 00:03:29,660 --> 00:03:35,540 So the same thing we did down here which generated this date, but now we're doing it dynamically here 58 00:03:35,540 --> 00:03:39,050 with an email and it created that date both dynamic. 59 00:03:39,290 --> 00:03:45,800 We're going to insert person using the same syntax and let's see what happens. 60 00:03:46,490 --> 00:03:48,350 Okay, hold our breath here. 61 00:03:48,680 --> 00:03:50,690 Node App Tags. 62 00:03:53,000 --> 00:03:53,530 And what do you know? 63 00:03:53,540 --> 00:03:54,590 We don't get an error. 64 00:03:54,860 --> 00:03:56,030 So what's going on? 65 00:03:56,060 --> 00:03:57,020 Did it work? 66 00:03:57,260 --> 00:03:58,460 Let's come over here. 67 00:03:58,490 --> 00:04:05,990 Unfortunately, we're not able to do our select star in order by created it because our created at dates 68 00:04:05,990 --> 00:04:06,740 now vary. 69 00:04:06,740 --> 00:04:10,820 So it's no longer that the last one added in has the most recent date. 70 00:04:10,820 --> 00:04:16,000 So we'll just do a select star from users and what we'll look for. 71 00:04:16,010 --> 00:04:19,370 In my case, I happen to know the data that we were working with. 72 00:04:19,790 --> 00:04:23,210 This was the one that was just added Lucy of 38. 73 00:04:23,240 --> 00:04:24,620 Yours, of course, will be different. 74 00:04:24,620 --> 00:04:30,680 But notice that the date time they created it isn't 0000 like we had for Dusty. 75 00:04:30,680 --> 00:04:33,830 This is the one where we manually took this. 76 00:04:33,830 --> 00:04:38,840 The same exact thing coming from JavaScript, but we did it ourselves and it didn't work. 77 00:04:38,840 --> 00:04:45,140 But then when we used person instead of variable, then we use connection query and we passed it in. 78 00:04:45,410 --> 00:04:46,520 It somehow worked. 79 00:04:46,520 --> 00:04:49,640 And basically it comes down to the magic of this library. 80 00:04:49,640 --> 00:04:55,430 And I'm showing this to you to illustrate another point, which is there's a lot of work that goes into 81 00:04:55,460 --> 00:05:00,080 sewing the stitches between something like Node and MySQL. 82 00:05:00,080 --> 00:05:08,420 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. 83 00:05:08,780 --> 00:05:09,440 Here we go. 84 00:05:09,770 --> 00:05:15,260 If we look, there's something right here that says date objects are converted to year, year, year, 85 00:05:15,290 --> 00:05:17,540 year, dash month, month, dash day date. 86 00:05:17,540 --> 00:05:20,360 Basically the syntax we're looking for, they're converted to that. 87 00:05:20,360 --> 00:05:27,290 So as part of this library, it takes it upon itself to bridge my SQL and node. 88 00:05:27,290 --> 00:05:31,970 It's it says, all right, if this is a JavaScript date, which is what we're working with from Faker, 89 00:05:31,970 --> 00:05:38,600 well, I know that the user is trying to insert it into a mysql table that has no idea about JavaScript 90 00:05:38,600 --> 00:05:41,660 dates, so we're going to convert it before we send it. 91 00:05:41,660 --> 00:05:47,360 And then one other kind of interesting thing we can do is save this to a variable and we'll call it 92 00:05:47,360 --> 00:05:49,490 end result. 93 00:05:50,270 --> 00:06:00,590 And if I just do a console.log end result SQL, which again all new stuff, I know it can be overwhelming. 94 00:06:00,680 --> 00:06:04,820 What this will do is show us that compiled SQL that is being sent over. 95 00:06:04,820 --> 00:06:09,230 So this is kind of before what person looks like and this will be an after. 96 00:06:10,140 --> 00:06:11,030 Let's take a look. 97 00:06:11,750 --> 00:06:13,160 Well, we have to go to Node. 98 00:06:15,950 --> 00:06:16,420 Okay. 99 00:06:16,670 --> 00:06:20,540 So you can see before we had created it looks like this. 100 00:06:21,250 --> 00:06:22,290 Tuesday May. 101 00:06:22,300 --> 00:06:24,490 It's a wrong format, that's JavaScript format. 102 00:06:24,490 --> 00:06:33,760 But sometime before we actually sent this over, it was converted to create a that is equal to 20 1605 103 00:06:33,760 --> 00:06:37,450 Dash 24 and this is the correct MySQL syntax. 104 00:06:38,110 --> 00:06:43,300 So this is kind of cool to be able to see the end result SQL that's being sent over. 105 00:06:43,450 --> 00:06:50,500 This is valid SQL that we could just copy this over and yeah, it might look slightly different. 106 00:06:50,500 --> 00:06:53,350 These are escaped, which you do not have to do. 107 00:06:54,550 --> 00:06:59,050 But if you notice, this is SQL syntax and we could just paste that in. 108 00:06:59,920 --> 00:07:01,150 So I'm going to get rid of that. 109 00:07:01,150 --> 00:07:03,340 But I just wanted you to see that you could do that. 110 00:07:03,430 --> 00:07:08,560 And more importantly, the whole point here is to show you that there's stuff happening behind the scenes 111 00:07:08,560 --> 00:07:13,870 which can be great and useful, but it's also the stuff that you just don't know what's happening sometimes, 112 00:07:13,870 --> 00:07:18,760 and you run into it, you discover it, but it's a job of these libraries to connect the two. 113 00:07:18,760 --> 00:07:23,530 And there's a discrepancy in this case between how JavaScript handles dates and how MySQL handles or 114 00:07:23,530 --> 00:07:24,340 expects them. 115 00:07:24,490 --> 00:07:30,820 So behind the scenes, this library that we're not writing, we're not in charge of somebody is that 116 00:07:30,820 --> 00:07:32,200 library is patching them. 117 00:07:32,200 --> 00:07:33,580 It's fixing it for us. 118 00:07:33,730 --> 00:07:36,250 Okay, so we've inserted a single user. 119 00:07:36,250 --> 00:07:39,640 Now all we have left is to figure out how we insert 500. 120 00:07:39,640 --> 00:07:40,780 That's the next video.