This course has been updated! We now recommend you take the AWS for Front-End Engineers (ft. S3, Cloudfront & Route 53) course.
Transcript from the "Exercise 3 Solution Part 2" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Kevin Whinnery: Now, we should be able to start using the model with this new property. So, the first thing I might try is to run my test, npm test, and it does actually successfully create a Todo item. But I might sort of modify that to also,
>> Kevin Whinnery: Add in a completed flag.
[00:00:29] So, in addition to creating a todo with the title text.
>> Kevin Whinnery: I think I actually want completed, there all right, right. I'm gonna create a todo with both the title and the completed and add another assertion there that says, todo.completed, [INAUDIBLE] false. And there's a bunch of ways to do that assertion but that one works okay.
[00:01:09] But this is one way for me to just quick verify that the table has been sort of updated and the model is kinda doing what I expect. These are fairly shallow tests again but it's the first step. Now, what I need to do is update my REST API endpoints to actually update these properties on the models as well.
[00:01:33] So for that, I'm gonna open up my todo routes. And for both the update and the create operation, I'm gonna have to accept another parameter for the completed flag essentially. So, for update, we'll do this first.
>> Kevin Whinnery: I'm gonna look for a request body. Excuse me I'm actually gonna do that after my 404 check.
[00:02:03]
>> Kevin Whinnery: So, I'm gonna set the completed property of my model to be equal to the result of a Boolean check. So, if a request.body.completed === 'true', that's gonna set completed to be true for all other values of that component that's gonna evaluate false. There's other ways to do this, this is a fairly quick and dirty implementation but it's one way to make that piece happen.
[00:02:41] And then, in my create, I'm gonna do something similar. So, in this case, I'm gonna set that completed property, again based on the result of that Boolean comparison, whether or not it's true. And then, it should be updated in my database accordingly. So, those are the two REST API endpoints, I need to update.
[00:03:13] Now, I'm gonna run my application and make sure that my REST API is doing what I feel it should do. So, I'll start up the dev server.
>> Kevin Whinnery: And now, you can verify this using cURL or whatever HTTP client you want. Like I said I'm kinda a fan of Postman so, I'm gonna do it that way.
[00:03:33] So, if I start off by sending a get to my input, I can see that I have all of my todos in the database but for most of them the completed attribute is set to null. So, let's create a new one with that attribute in place. So, in addition to the body, title excuse me, I'm gonna pass in a completed string and I'll initially set it to false.
[00:04:02] And I created that value in the database so, completed is set to false. I could create another one,
>> Kevin Whinnery: Set to true.
>> Kevin Whinnery: Send that in and then, again, if it's anything but the string true, it's going to default to false. So now, if I list those items in the database, the most recently created ones are there with their proper values.
[00:04:38] And I can test the update by using the put. Put is the operation that I'm using to update the models. In this case, the title is going to be, whatever I want that to be.
>> Kevin Whinnery: And completed is going to be true, I can send that. And once again, I can see ID11 has been updated but that had one already.
[00:05:11] So let's do maybe ID number two.
>> Kevin Whinnery: And hit Send, and then, that's been updated as well.
>> Kevin Whinnery: All right, so, once again, the steps were creating a migration, handling, adding and removing the column whether or not we're migrating the database up or down. After we did that, we updated our actual Todo model, to have the property that we added to the database.
[00:05:56] We updated our test and then, we updated our express controller logic to take advantage of that new property which wasn't there before.
>> Kevin Whinnery: Cool, any questions on any of that code? There were a number of steps there that you had to discover. So, not surprised that a good chunk of people.
[00:06:21] Yeah, there was definitely a lot to grok there if you haven't done it before.
>> Kevin Whinnery: Anybody in the chat maybe we'll give them a chance to
>> Kevin Whinnery: Okay, if you have any other questions on the SQLite side of things, I'd be happy to answer them. SQLite is a fairly full-featured ORM.
[00:06:51] It can do complex relationships and can use database specific features like JSON fields and Postgres. So, if you're interested in digging deeper, it's definitely gonna be your best bet for an ORM if you're not gonna go with a driver directly.