Go back to 8base Academy
October 1, 2019

Defining Data Tables and Relationships in the Data Builder

Sebastian Scholl

* This is an automated transcript. Please excuse inaccuracies.

Okay so in this episode we are going to be going over data modeling in the 8base, specifically that is how do you create tables, put fields on those tables and then define associations between the tables. I think the best way is to just jump right into an example, so let's get going.  


Okay so first off, I'm starting in a new 8base workspace that I created just for this video called, “Simple CMS”. Pretty much we're going to be creating two tables for the sake of this demonstration one called “posts” and the other one called “authors” that's what I called the workspace Simple CMS. I hope that makes sense to you and so before we jump in and start creating those tables I also wrote a simple little spec for what we're going to be doing. So, on the post let me make that bigger. Okay well anyway, so this is, give me the post table and on the post table we're going to make a title, a body a publishing date and as well as add an author relationship to it which is going to be a belongs to relationship. When it comes to the author table, we're going to add a pen name which give me texts posts which is going to be a has many relationships as well as Avatar which is going to be an image file alright.  


So, let's just jump back to that workspace and I'm going to click on create a new table. Since I don't have any tables this is the layout I'm going to see however, you can find all this in the data page of your management console for the workspace that you want it to be in. So, let me click on “new table” and I'm going to write in posts. So, let's start with posts.  


So, first off, I'm going to give the post a status all right and so the status is going to be types which you can think of switch almost is like a truthy falsie type thing or a select. For example, we have on/off yes/no so on and so forth, but I'm going to choose custom and then I'm going to add in a list of different options. Those being, draft in, review and published. Sorry if my head is covering some of the fields here. But essentially these are the different statuses that we're going to allow are opposed to have we're going to make it mandatory if I click on the mandatory validation as well as set a default which is going to be draft, so that every new post we create defaults to draft status- cool.  


Next, I'm going to add the title. I'm just going to try to go through these pretty quickly… 55 and then we're to make it mandatory and then we're also going to say, “No duplicate values” which means that titles are going to be unique so that we could query by them if we wanted to. Then I'm going to add body, text- since is that since the body text could be really long, we have like 20,000 characters and then let's add a publishing date. So, as you can see just all have to do is select the type. We're going to make that mandatory and then we're going to handle or our 8base is going to handle on the back end all the validation to make sure that the right types are only being saved to the database. So, then I think that's it for our posts table. The only thing we're going to want to do is create the authors association, but we're going to wait to do that until after we've created our authors table which we're going to do now.  


So, I'm going to click on new table up here give it a minute… cool authors. An author is going to have a pen name which is going to be text. Let's make that mandatory and no duplicate values. So, mandatory and unique. Then we're in a given author and avatar. So, this is great. So, if I just click “file” and I click on “image”, “create field”. So, now within the management console everything has been configured to be able to upload files to the table. However, it's also being created so that from your Graph QL API you can do file uploads. We'll go over how all that works in another video, but just so you know how do you put like a profile picture essentially to the author was that easy.  


So, now I'm going to click back to the post table right. So, if I wanted to, I could say that, “post has an author” and then select “type table”. But instead just for convenience I'm going to grab a Thurs over here drop it on top of the posts table definition and then it's going to populate all that information for me.  


So, here we're going to look at some of these options. So, first off is the table relationships. We can see with this graphic right here that the author has many posts and the posts belong to single author; that's exactly what we want. If we wanted to change that we could do create a many-many relationship, one-to-many in the other direction or even a one-to-one relationship. We'll go over more about relationships once again in another video, however for now know that defining those associations between the tables really is that straightforward. And then we're also going to say that the author field is a mandatory for a post. I'm just going to change that to author.  


Then on this relational field name, that is actually how is the relationship described on the other table. So, post is totally fine in our case just to make that clearer after I create this association, done. If I went over to authors and now, we can see that an author has many posts, that's exactly what we want. So, just to create a few records in our workspace or in our database right now. I'm going to click over to the data tab here which is right next to schema and let's create our first author. We're going to call this author or give the author a pen name of sub that's me and then give him an avatar, upload the avatar and second add the row. Boom! First author was created.  


So, now if we wanted to go in here and create our first post it's going to be a draft. We're going to say, “First post is the title. A really interesting blog host is the host itself. The publishing date is today and then set is going to be the author.” And now our post was created.  


So, it's great that you have the ability to jump right into the management console and start creating updating and deleting records, using the UI. However, it'll probably be very common that you want to perform these same types of actions using the API. And one thing that's so powerful about 8base as well as just so cool is, based on your schema definitions right here, we generate all the Graph QL resources that you need to perform crud actions through the API. What do I mean by that? I'm going to jump over the API Explorer which we will go into depth within probably the next few videos. However, right here if I were to say okay well, I want to make a query and, in that query, I want to query a hosts list. Since we created posts and you saw us do than that video for both list queries and normal queries, mutations and subscriptions which is web WebSocket real-time updates, those Graph QL resources have been created for you. So, for example if we wanted to query this list of posts, we could say okay for each item in the list well we want the ID, the title and the author which for the author we want the pen name- cool. So, I'm going to run that query and you can see that we only have one post that makes sense and it gave us already the title as well as the author and the data on the author. So, that was a relational query that just came right out of the box and was ready to go.  


Like I said we're going to go over so many more of these things in depth really soon. However, I hope that you found this video very valuable in getting started on how to view basic usage of the data builder as well as how to create some records within your workspace. If you want to stay updated on future videos please, subscribe to the channel as well as feel free to ask any questions in the comments or reach out directly. Hope you found this valuable and looking forward to seeing you in future videos. Take care.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.