Jamstack Explorers

Building with Sanity and Nuxt with Ekene Eze

Sanity Datastore and Datasets

Welcome back. So let's talk about sanity datastore and datasets. The sanity datastore is a cloud storage where the data for your application is stored. You can access it using sanity, APIs, or other client libraries. However, all the data in your datastore is organized into datasets.

Datasets can be considered as variations of the data in your datastore that are represented in different states. This makes it possible for you to organize your data in multiple environments when working with sanity.

It also makes it possible for you to manage access to your datasets since it can be created in both private and public modes.

So, how do you request for these data you might ask.

Well, in order to access the data in your datasets. Sanity provides an open-source tool called groq.

It is a query language built by sanity that you can use in sanity studio or even within your own applications to access the data from sanity datasets.

At the moment, you can also use GraphQL queries to access sanity data directly into your applications.

Of course, there are also existing client libraries that are available to you.

Back to our sanity studio. We currently have only one dataset.

And that is production, which was created when we set up the application. We can run queries against this data set using groq like this.

And click on the run query button. And this should now give us all the dispatch services that we created.

If you're unfamiliar with this syntax, this is called Groq, which you can learn more about from groq dot dev.

Right here at the extreme left is a sample dataset that is just provided for you to play around with and see how groq works. In the middle column is the groq query, which will return every item in the dataset where completed is true. And user ID is two. If you click on the play button, you should now get all the results listed here.

So back to the sanity studio. Since we have only one dataset. Let's go ahead and create a new one. And to do that. Bring up the terminal and run the following command. sanity dataset create.

And the name of the dataset you want to create is? Let's call it staging.

And make it public.

Now that this data set is created. If you want to, you can come back and reload your studio.

And now, and if you click on here, you should see that we have both staging and production datasets. But at the moment. If you run any queries against the staging dataset, you'll see that there are no documents available in the staging dataset at the moment. So what we can do is instead of manually adding documents to this dataset,

Let's go ahead and copy all the data that we have in our production dataset and put them into the staging dataset.

And to do that. We'll copy the existing data in our production dataset into a zip file.

And then import that zip file into our staging dataset.

You'll find this particular process really useful in the future because it can be a bit tricky at first, but it's quite simple to follow and understand.

First, let's go ahead and bring up the terminal again. And export the existing content we have in our production dataset into a zip file.

And let's do that by running the command sanity dataset export.

The next thing we want to specify is the dataset that we want to export and its production.

And we want to export it into a file and let's call it the file data.

And make it a zip file.

And the next thing we want to do is import this data file that we just created into our staging dataset. And to do that, let's go ahead and run the command.

Specify the name of the file you want to import, which we called data.

And we want to import it into our staging dataset.

Great. So back to our sanity studio. If we run this query again against the staging dataset. We should now get the same exact results as we did before.

And you can go ahead and confirm that your staging dataset is actually created on the sanity dashboard, which you can find at manage. Dot sanity.io should click on your sanity mission.

And come all the way to data sets. You should now see that we have put production and staging datasets. If you wanted, you also have the option to delete either of the datasets.

So this is as far as we'll go with datastore and datasets

In the next lesson, we'll go ahead and set up a Nuxt application that we can use to consume the products that we've created in our sanity application. See you there.


your progress