Create a Database using Hasura Cloud and a Postgres Instance to Store Data

Jason Lengstorf
InstructorJason Lengstorf
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 4 years ago

We'll store client interaction using Hasura as our primary database. Hasura allows you to build a powerful, secure & flexible GraphQL API that can query data in your databases, HTTP services, serverless functions, as well as third party APIs.

We'll start by creating a Hasura Cloud account. Proceed to create a Postgres database on Heroku. If you don’t have an account on Heroku, you need to sign up on Heroku. You won’t need a credit card.

We'll add corgi data to the database using the Unsplash ID as our primary key, and count as an integer for our count functionality.

Lecturer: [0:00] Our next step is going to be to get this boot count to work again. Because we had it hardcoded before, but it wouldn't stick around. Now, we want that to actually be persistent.

[0:09] For this project, we're going to use Hasura. Hasura is really cool. It's very useful for quickly setting up a GraphQL API. It's powered by Postgres which means that we don't need to worry about scalability. Postgres is powering some of the biggest applications in the enterprise.

[0:26] It's been battle-tested to hell and back so you can trust this as something that will grow with your company. Hasura makes it really easy to get up and start it very quickly.

[0:37] We go to the website. We can start for free. That's going to take us to cloud.hasura.io and you're going to log in. I'm going to login using my GitHub account.

[0:46] To start, let's create a new project. Up here, we're going to create a new project at serverless Egghead.

[0:52] We're going to use the free tier. For the region, select US East, Ohio. That is the free tier. Then click this Continue to database setup.

[1:06] Now, I want to try with Heroku. This is the fastest way to setup. You will need a Heroku account, but this means that it's free for up to 10,000 records in your database, which is a really nice way to get up and running. You don't have to host anything or put in a credit card. You can just get rolling.

[1:24] I'm already logged in, which means that it's setup to my Heroku app. If you haven't logged in before, you'll have to authorize Hasura to use your Heroku account. Once this is done, we can create our project.

[1:36] Now, I've created my Hasura project. We've got this GraphQL API that we want. We're going to hold on to that. We're going to store it in our environment, because it might change. We might migrate that later or we might use it in a bunch of places. It's just easier, in my opinion, to keep these somewhere consolidated.

[1:53] We're going to create an environment variable called Hasura URL, and just paste that in. We're also, because this is open, when we launched this console, we can see here, this is our Hasura setup and we've got access to graphical which is our GraphQL Explorer, we've got access to our data, and there's nothing in here now.

[2:14] This is also open, anyone can hit this, and that is not necessarily what we want. We want this to be locked down, so that only apps that we authorize can write to it. To do that, we go in here, setup our environment variables. I'm going to add a new one. If I search for admin secret, we can see admin secret here, and you can make this any value you want.

[2:36] For the sake of learning, we're going to call this Egghead secret. I'm going to add that, and then I'm going to come out here, and we're going to set a new environment variable called Hasura admin secret. I reload this, authorizing, OK.

[2:56] Now you can see that, because we added that admin secret, some new stuff showed up in here, we've got an ID token. That's exactly what we want. That's what we're looking for. That helps us keep our data safe. Our first step is to define our database, we need to tell the database what it's going to store. We're going to click through to this Data tab.

[3:15] Then we're going to add a table, we can do that with either of these buttons. Click Create Table. This table is going to be called boops, it's the number of boop that we want to count. It's also going to be a two-field table, we want to keep this as simple as we can. The first thing is the ID, that ID is going to match the Unsplash ID. It's what comes back from our database, so let's just use that.

[3:37] That is going to be a text field, but we do want it to be our primary key, which means that it will be how the database is indexed. It also means that it's going to be required to be unique. We can only have one boop count per entry per Corgi. Then we're going to keep track of the count. That is going to be an integer. Once we've got that setup, we're going to add that table.

[4:04] Now, if we come back out to graphical, we can see that there are a few options out here for us to load our boops. If we go to load all of our boops ID and count, we get an empty array back. That is to be expected. If we come back over here and grab one of our corgis, and we know that it's got an ID here, then we can take that ID, and we can come back here, and let's add account.

[4:35] We will go here and insert a row. The ID is going to be here and we're going to add one boop. So now if we go back out here, we can rerun this, and we'll see that we've got a boop and a count. Perfect. That's what we're looking for. That's what we want to be able to track.

[4:53] With that, we've got a Hasura database stood up for free. Without any code written, we were able to just go to Hasura, create a new instance, and configure it right from this dashboard. Using this, we'll be able to power our entire app.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today