1 00:00:00,520 --> 00:00:01,780 Welcome back. 2 00:00:01,780 --> 00:00:08,160 We left off in the last video with this console log of the KNEX query that we just did. 3 00:00:08,200 --> 00:00:16,000 Now this on the right here is what KNEX does. It builds our SQL statement for us. 4 00:00:16,300 --> 00:00:26,230 So what we just console logged, was the query statement that postgres or KNEX created for postgres. 5 00:00:26,340 --> 00:00:32,100 You can see over here that the builder is an object that has all the information we need to speak with 6 00:00:32,100 --> 00:00:34,670 the smart-brain and do this query. 7 00:00:34,770 --> 00:00:43,890 If I actually want to access something from the users, I will have to do our friend .then() because 8 00:00:44,010 --> 00:00:49,170 this returns a promise and we get our response. 9 00:00:49,170 --> 00:00:55,950 In our case it will be data, this data - again we don't have to do Json because we're not sending it through HTTP - 10 00:00:55,950 --> 00:01:03,040 we get back the data and if we just console.log this data let's see what we get. 11 00:01:03,140 --> 00:01:09,420 Save and go down and it's an empty array. 12 00:01:09,460 --> 00:01:10,510 That's a good sign. 13 00:01:10,510 --> 00:01:13,480 We don't have any users yet but we're getting a response. 14 00:01:14,320 --> 00:01:15,270 So let's do that. 15 00:01:15,490 --> 00:01:21,960 Let's create a statement to insert users when they register. 16 00:01:22,090 --> 00:01:28,390 I'm going to change this into the DB variable just so it doesn't get confusing. 17 00:01:28,630 --> 00:01:31,740 And we'll just keep that as database. 18 00:01:31,740 --> 00:01:37,710 All right let's go to the register endpoint and I'm going to minimize this database because we're going 19 00:01:37,710 --> 00:01:40,440 to stop using it and delete it afterwards. 20 00:01:42,680 --> 00:01:49,490 And if we go to register we see that now instead of doing the database .users.push in the variable 21 00:01:49,730 --> 00:01:59,490 we can just use KNEX we can remove this and do DB dot - Well we'll have to insert, 22 00:01:59,490 --> 00:01:59,920 right. 23 00:02:00,120 --> 00:02:06,780 Let's look at the KNEX docs and see and we see clear methods - insert. 24 00:02:06,790 --> 00:02:08,669 All right look at this great documentation. 25 00:02:08,669 --> 00:02:13,410 We can just find out exactly what we need. In order to insert something we have the insert method with 26 00:02:13,410 --> 00:02:17,580 the data and what we want, or what is going to get returned. 27 00:02:17,580 --> 00:02:28,610 We can't just copy and paste this, so we can just do DB into users we want to insert and it just reads so nicely. 28 00:02:28,610 --> 00:02:29,730 I love KNEX. 29 00:02:30,030 --> 00:02:38,570 We want to insert an object and this object is going to contain whatever we had in the users information. 30 00:02:38,610 --> 00:02:47,400 So we want to insert an e-mail, in which case there will be email. We have the name, which again has the name 31 00:02:47,430 --> 00:02:50,010 that we see from the body. 32 00:02:50,010 --> 00:02:57,600 We also have the password but remember in our table we don't have the password under the users so we'll 33 00:02:57,600 --> 00:02:58,740 leave that blank. 34 00:02:58,740 --> 00:03:06,960 And if we go back to our table example here and connect back to here we see the user's ID is automatically 35 00:03:06,960 --> 00:03:12,710 generated name, email, entries defaults to zero so we don't have to enter anything. 36 00:03:13,080 --> 00:03:17,780 And joined is the date that they've joined. 37 00:03:17,800 --> 00:03:32,320 So let's just do joined new date and once we insert let's just do a .then(console.log) to see what 38 00:03:32,320 --> 00:03:34,290 we get back. 39 00:03:34,330 --> 00:03:41,390 I'm going to save. We don't get any errors which is good and we can go back to our app. Actually, just 40 00:03:41,390 --> 00:03:48,350 because it's faster, instead of doing register through here, we're going to open up postman and 41 00:03:48,350 --> 00:03:52,870 postman already has all our previous commands here, which I love. 42 00:03:52,940 --> 00:04:03,610 So let's click on the register and point with the body of Ann Apples and let's click send. 43 00:04:03,630 --> 00:04:08,790 All right we get a response but remember we never change a response but if we go back to the server 44 00:04:10,220 --> 00:04:12,550 we get result insert. 45 00:04:12,650 --> 00:04:13,460 All right. 46 00:04:13,460 --> 00:04:14,660 It looks like it worked. 47 00:04:14,870 --> 00:04:25,260 If we go back to the database and do select star from users how cool is that. 48 00:04:25,490 --> 00:04:34,250 We have Ann who has been entered as our very first user, with default entries of 0. Amazing. 49 00:04:34,340 --> 00:04:35,980 It's like magic. 50 00:04:35,990 --> 00:04:42,430 How simple was it to just use KNEX and postgres and create our first database. 51 00:04:42,460 --> 00:04:43,170 All right. 52 00:04:43,250 --> 00:04:47,870 In the next video we're going to work on this response. 53 00:04:47,870 --> 00:04:48,740 I'll see you in that one. 54 00:04:49,270 --> 00:04:49,650 Buh-bye.