Go back to 8base Academy
August 4, 2022

Importing Relational Data using the 8base-CLI Import Command

Sebastian Scholl
@SebScholl

Sebastian (00:00)
Hey everyone, this is Sebastian from 8base. Today, what we're going to be walking through is how you can import relational data when using the import command on the 8base CLI. Let's jump into it.

Sebastian (00:13)
Here in my IDE, essentially what I'm first going to walk you through is a simple schema that I was able to export from a workspace. This schema has two tables in it; restaurants and reviews, and a restaurant has many reviews; that's the relationship between the two tables. Now I could create this schema file by configuring a workspace with my functions directory. So running the command 8base configure, which would select the workspace I want to communicate with, and then running 8base export, file, and then give it a file name such as schema.JSON.

Sebastian (00:50)
Running that command will export the schema of the workspace that you're connected to, and then you'll be at this point. One of the ways that you look at this is, maybe you have some type of schema that you use across many projects, or you want to re-use across many projects, that would be a good reason to save that schema. Then at this point, we could add some sample data or seed data to this import so that when you do import it to a new workspace, you're going to get that data with the import.

Sebastian (01:19)
In order to bring in data with schema, we're going to go down to the bottom of this JSON file, we open up a new key called data, and assign it an object value. Within this object here, we can reference any table that will be imported or exists in the workspace by name. For example, here we know that we can do restaurants, and then open an array. Here we can establish the different objects that will be the create inputs for different records.

Sebastian (02:00)
For example, there's a name field on my restaurant. I could have a restaurant that has the name taco shop and another one that has the name, let's just call this sushi shop. There we go. Cool, and the same thing would be done for reviews. For example, we just go create a different key, let's call it reviews, and a review would have different fields. Let's just change this to rating. Excuse me, back where we are. We can just change this to ratings, and then give it two values, right? There we go.

Sebastian (02:46)
I have a sample data import that I'm going to pull in, so we don't have to go through the boring exercise of watching me write a bunch of dummy data. I'm going to go over here to my sample data.JSON file, and I'm going to copy this object, which is going to give me about 60 records to import. Then we'll walk through some of the unique parts of what you have to add and understand on how you relate data on this import.

Sebastian (03:14)
Here, I'm just going to paste that in and go right back to the top. Cool, just as we had seen before, we have a key called data, and then inside that object, we said restaurants was just referencing our table name, and here we're passing an array of different dummy data or seed data for our workspace.

Sebastian (03:34)
If we look at the first record, we can see a couple things. First off is that we have two fields which we care about, which is the name, and then the Lat Long coordinates; the location of the restaurant. But then we are assigning essentially a dynamic ID. Here we say ID, and we give it the dollar sign, and then we can give it any type of pseudo name that we want to but it has to be unique. This will not be the ID that actually persists the database. 8base will handle the generation of a global unique ID for you. However, this is the ID that we can reference within this DSL file or within this data file when relating those objects.

Sebastian (04:13)
As you can see, for restaurants, we've gone through and added a different ID for each of those restaurants. Now if I go all the way down to my reviews, what we can now see is that we are referencing the review or the restaurant by name on the review. We're establishing that relationship there, and then referencing the ID. Now make sure that even if the review ID is not being used, that you give it an ID, because it needs one.

Sebastian (04:42)
For example, we're not referencing review one ID anywhere, but we still need to add it to that object so that it can handle the management of that on import. However, essentially every review belongs to a restaurant; that's the relationship. In this case, we're going to say that the restaurant has a review has a restaurant, and the ID of that restaurant is restaurant one, restaurant two, restaurant three, so on so forth for each of those reviews.

Sebastian (05:11)
Now if this was a many relationship or many to many relationship, we could establish it like so, and just add the multiple declarations in here, know that that's available to you as well. Also, one important thing to know is if we were importing files such as images with this import, it's a very similar approach, however, you just use the URL key. For example, let's say that each restaurant had an image, and let's just call that image. Inside of here, what we'd be able to say is the URL, and we could give it a web link, right? So any type of path to the image or address to the image, and then it will handle the importing and uploading of that image on import. It's good to know.

Sebastian (06:05)
Now what I'm going to do, is I'm going to go ahead and run the import command to my workspace. So 8base import and then schema.JSON, that's the name of our file here. So I run that, and this will take probably about 20-30 seconds so I'll be right back when it's done.

Sebastian (06:22)
Cool, that task just completed and threw a little error here, but we'll see whether or not it actually messed up what we are trying to accomplish. Now I'm going to go over to my workspace that I connected it with, and I'm going to reload the page. We can see that it added those two tables to our workspace. And if we jump and look at the data viewer, we can see that all the data was successfully imported, as well as those relationships were imported as well.

Sebastian (06:49)
The restaurant having many reviews is showing those all here, so all those relationships were already set up, as well as the other way around to where all the reviews that were imported, we're getting the content, the ratings, and then the establishment of that relationship with the restaurant to which they belong.

Sebastian (07:06)
If you've been investigating different strategies on how you can import data into 8base, this is definitely one to consider. Three different ways that you can keep in mind. Are CSV import for simple data, you can use the GraphQL API if you're comfortable writing scripts, which will import your data; that's a more advanced use case, or the one we just investigated, which is essentially saving your data in a JSON file and then importing with the schema.

Sebastian (07:34)
Note too, that you don't need to necessarily import schema with the data. You can just import data if the data you are importing matches the schema in the workspace that you're importing it to.

Sebastian (07:47)
Happy developing and see you in future videos.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.