Building with Sanity and Nuxt with Ekene Eze
Sanity Schema
Welcome back. In this stage. We'll take a closer look at sanity schemas. In the last lesson, we set up this Sanity project and introduced you to send you to sanity studio. You can see that we already have products. Vendor and category schemas available in the project.
And that is because of the e-commerce schema we selected while setting up the application. So if you used a blank schemer. Don't worry, I'll go ahead and create a new schema to show you how it's done. To do that. Let's go back to VS Code
Open the schema directory. And create a new file.
And let's call it dispatch.
To create a new schema. Let's export. A default object.
And give it a name.
Give it a title.
Let's also give it a type.
And that would be document.
And last but not the least, let's now define the fields that will be available in this schema.
The first field we want is the name.
Let's give you the title.
and give you the type.
Both string.
Let's duplicate that. And make this address instead.
So this schema that we just created will represent delivery services for our Vendors.
What this means is that for every vendor in this project, we can assign them a dispatch service that will deliver their products to customers.
So tell sanity that we've made a new schema and we can do that in schema dot js by importing the dispatch schema that we just created here.
And then register it here.
And when we save, the studio will get the new update and display the newly created dispatch schema. Right here. If you click on it. You will notice that there are no existing documents in the schema. So let's go ahead and make some.
Address will be. X Y Z. Street.
Publish that.
And make a new one.
Let's call this one. FedEx.
And give it an address of.
Four Five, Six. ABC Street. And published start again.
No, we have to document in our dispatch schema. Two for the Confirmit, let's go over to the vision section of our studio and run a query to fetch the dispatch services.
And here we just need to change product to dispatch.
And click on the run query button.
The next thing we want to do Is creates a relationship between the dispatch schema and the vendor schema. So that each vendor will have a dispatch service assigned to them. To do that. Let's go back to vs code.
make a new field.
And call it. Once again. Dispatcher.
And give it a new title.
And it'll be dispatch service.
Let's give it a type of reference
And it will be referenced to type dispatch.
The reference type is a special type in sanity that helps you create relationships between schemas. Which is what this field is going to do for us, basically. It's going to add a field to the form, to select a dispatch company when creating a vendor.
So if we save this file, And go back to sanity.
If you want to create a new vendor. You get the dispatch service field. Which lets you select a particular dispatch service provider from the existing dispatch services we have in the dispatch schema, which at the moment is FedEx and DHL.
This will also apply to existing documents. So if you look at Nestle, for instance,
If you come all the way down. You see that it also has the option to select a dispatch service.
And this is a little about sanity Schemas and how they work with each other. In the next stage, we're going to look at the datastore API.
We'll also look at sanity. Datasets and how to work with them. See you there.