Check out a free preview of the full Rapid Development on AWS: React, Node.js & GraphQL course:
The "Adding a Database with Mobile Hub" Lesson is part of the full, Rapid Development on AWS: React, Node.js & GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

To resolve the issue of data being stored only in memory, Steve uses AWS Mobile Hub CLI to create a database to persist data.

Get Unlimited Access Now

Transcript from the "Adding a Database with Mobile Hub" Lesson

[00:00:00]
>> Steve Kinney: But I have this, but still I've logged in. It's still data in memory, right? I don't have a database yet. I'm gonna go back, and I'm gonna look at the features to figure out how I could spin up a database, awsmobile. And we can see, this is interesting.

[00:00:22] This is one called database, that seems like a very good choice for the work that I'm trying to accomplish right now. So what I'll do is I'll do awsmobile, and we will say database. Now I don't want the defaults particularly in this case. I wanna configure configure what my data base is, what it is called, what are the fields that it's keeping track of, so on and so forth.

[00:00:47]
>> Steve Kinney: So what we'll do is we'll say enable, and we'll say prompt. All right, open or restricted. Can anyone access this database or is it restricted through authentication? Well, in this case, we set up authentication for a reason. We wanna say like, hey, for this user, we wanna go in and be able to see whether or not it's working.

[00:01:08] Or we want data that is restricted to that user. That user should get their personal data, it's not just an open access database. There are different cases, like for instance, there's the case of like, the global Twitter stream. You don't need to be logged in to see a tweet, right?

[00:01:23] So there's different cases. But in this case, this is very personal data, so we'll go with restricted. We're gonna call it grudges. I'm gonna spell it right this time. And there we go. And so, now we can say what are the columns in the database and what types are they?

[00:01:41] So do we wanna add another column? Yeah, we do. So it added, you can see right now, it added a user ID column. This is gonna be how it gets the records for a particular user. They're gonna authenticate with cognito, that's gonna give them a user user ID.

[00:01:54] When we query the database, by default we're only gonna get the records that match that user ID. That is gonna be the primary key for every field that belongs to that user in the database. We'll be able to set a secondary key like another unique key, and I will just use like a generic ID for that one.

[00:02:09] But this way, we'll have an ID of the actual item and we'll also have the user, right? Those primary keys are a way that's quickly search DynamoDB which is a new single database. It's very easy way to like, they'll all be batched together in the database. We can pull them all in.

[00:02:23] So under the column, we'll call this one the ID. And you can see that like very easily, you can switch between a bunch of these. We'll just go with the really simple ones, which is string. We'll add another column, person. And these right now are correlated to the fields in the state of my application.

[00:02:39] And we'll say it's a string.
>> Steve Kinney: Say deed. And we'll go ahead with a string for this one, and then the other one was avenged. This one is a boolean. And then we want to add no more columns. We'll say that ID is our other, our secondary key, which is called a sword key, and you can read all about those, it's delightful reading.

[00:03:05] The actual dynamo documentation is surprisingly not bad. For reading about a database, it is surprisingly easy to read. Dynamo can do some really cool things, there's an event stream that every time it changes it like fires an event. So you can like, this changed. You can like, trigger other things in your AWS ecosystem based on it.

[00:03:25] It's really interesting and I like, highly recommend for something like, light Saturday reading in the afternoon to read a database user guide. Cuz, why not. All right, cool. So now we have a database, but you can't interact with this database yet, right? It's a database sitting in Amazon.

[00:03:45] We need an API to talk to this database. We need some way for the client's side code to talk to this database in the back end. I will go ahead and do a awsmobile push. Often, I will say this as a rule of thumb now. If everything should be right and all of a sudden it's not right, you probably didn't do an awsmobile push.

[00:04:05] No reason to feel bad about that, I say that as somebody who forgot to awsmobile push about a thousand times. So, I say that from the scar tissue of having not done it. Not from like, did you push? I guarantee we'll have to say it at one point, or somebody will actually probably have to say it to me at some point today.

[00:04:23] So, ten points for whoever catches me forgetting to push when we have an error in like a live coding flub. Points, they're like Super Mario. I don't really know what they mean, but it's cool. All right, so we have this back end. And we can verify that by going to awsmobile console again.

[00:04:46]
>> Steve Kinney: A great way to like, validate the things on your system or the things you did are actually happening, right? So awsmobile console, we can see our node SQL database, there's our grudges. It's currently syncing up and creating it. So this might be like, yeah, I could download models.

[00:05:01] Unfortunately, only, [LAUGH] only if this is your real house. So for us, luckily it's JavaScript, it's very easy for us to just create the objects that we need, not a big deal. You could also create the tables in here as well.