# Creating a schema https://www.sanity.io/learn/course/day-one-with-sanity-studio/creating-a-schema.md Configure a schema for Sanity Studio that defines your content model and builds out an editorial interface. Let’s start with some foundational knowledge of how your Sanity Studio and Content Lake are integrated and how to think about the “schema.” You can skip right to the code part and return to this later if you prefer to be hands-on first. ## What is a schema? The "schema" for a Sanity Studio workspace defines what document types and fields are visible to authors. In the schema configuration you define much of the editorial experience for these documents and fields, like field descriptions, validation, initial value, and so on. If you have used other CMSes, the “schema” will be similar to what is commonly referred to as “content model,” “fields and entities,” “custom types,” “advanced custom fields,” etc. It's important to note that the schema is confined to a Studio workspace, not to the Sanity Content Lake dataset, which is considered "schemaless." That means that you can store any JSON documents in Content Lake, as long as it has a value for the `_type` property. Primarily, configuring the schema is configuring the content types that an author can **create** and **edit** in the Studio. This is also where you shape how and what content you can query in applications. ## Planning schema types In a production project, you should first consult with your wider team of designers, content creators, and others to work with them to design a content model that best represents your business and your goals. 1. See [Implementing Sanity successfully](https://www.sanity.io/learn/course/implementing-sanity-successfully) for guidance on how to work with a team to set yourself up for success. 2. Visually create and collaborate on Sanity Studio schema types with our [schema.club](https://schema.club) app. In the following lessons, you'll be building the content model from the [Hello, Structured Content](https://www.sanity.io/learn/course/hello-structured-content) course. Configuring schema types to represent a live music production company. 1. See [Content Modeling](https://www.sanity.io/learn/course/hello-structured-content/content-modeling-lesson) for a lesson in identifying content types in an organization. ## Create a new document type Create and open a new file in your Studio’s `schemaTypes` folder called `eventType.ts`. Copy-paste the following code into it: 1. **Create** your first document type: `event`. ```typescript:app/studio/schemaTypes/eventType.ts import {defineField, defineType} from 'sanity' export const eventType = defineType({ name: 'event', title: 'Event', type: 'document', fields: [ defineField({ name: 'name', type: 'string', }), ], }) ``` 1. The `defineField` and `defineType` helper functions in the code above are not required, but they provide autocomplete suggestions and can catch errors in your configuration in code editors with TypeScript tooling. Now you can import this document type into the `schemaTypes` array in the `index.ts` file in the same folder. 1. **Update** the Studio schema to include the Event type ```typescript:apps/studio/schemaTypes/index.ts import {eventType} from './eventType' export const schemaTypes = [eventType] ``` When you save these two files, your Studio should automatically reload and show your first document type. You can and should create a new "event" document. ![New Sanity Studio event document showing "Cosmic Harmony Festival"](https://cdn.sanity.io/images/3do82whm/next/4552ef1b13390930528b5aa7d28b50aa20c34e28-2240x1480.png) When you add content in the **Name** field, all your changes are automatically synced to your project's dataset in the Content Lake. Now, let's add some more document types with fields in them. Same procedure as with the event type: add new files, copy-paste the code into them, and import and add them to the `schemaType` array in `index.ts`. 1. **Create** new document types for **Artist** and **Venue**. ```typescript:apps/studio/schemaTypes/artistType.ts import {defineField, defineType} from 'sanity' export const artistType = defineType({ name: 'artist', title: 'Artist', type: 'document', fields: [ defineField({ name: 'name', type: 'string', }), defineField({ name: 'description', type: 'text', }), defineField({ name: 'photo', type: 'image', }), ], }) ``` ```typescript:apps/studio/schemaTypes/venueType.ts import {defineField, defineType} from 'sanity' export const venueType = defineType({ name: 'venue', title: 'Venue', type: 'document', fields: [ defineField({ name: 'name', type: 'string', }), defineField({ name: 'city', type: 'string', }), defineField({ name: 'country', type: 'string', }), ], }) ``` Notice how all these document types use **singular** names and titles. This is because the singular form makes sense in most contexts where these values are used. Later in this course, you will learn how to customize document lists to use plural names. 1. **Update** the array of schema types with the new document types ```typescript:apps/studio/schemaTypes/index.ts import {eventType} from './eventType' import {artistType} from './artistType' import {venueType} from './venueType' export const schemaTypes = [artistType, eventType, venueType] ``` Before we go further, confirm in your Sanity Studio that you can create new Artist, Event and Venue type documents. ![Sanity Studio with "create" menu open showing Artist, Event and Venue document types](https://cdn.sanity.io/images/3do82whm/next/0dd301be7d91d6708bc7da9b39628147a3d59819-2240x1480.png) ## Adding familiar field types Sanity Studio has the field types you'd expect for storing content in a JSON format. For example `string`, `number`, `boolean`, `array`, `object`, and more. In a typical project, the document types you create and the fields you add within them should be informed by conversations you've had with designers and content creators. 1. Using the Sanity schema docs as a guide, complete the fields we need for our project. See: [Schemas and Forms](https://www.sanity.io/learn/studio/schemas-and-forms). Add the following fields to your `event` schema type. You will extend the configuration later to make their purpose clearer: 1. `slug`: a `slug` type field 2. `eventType`: a `string` type field 3. `date`: a `datetime` type field 4. `doorsOpen`: a `number` type field 5. `venue`: a `reference` type field to the `venue` document type 6. `headline`: a `reference` type field to the `artist` document type 7. `image`: an `image` type field 8. `details`: an `array` of `block` type fields 9. `tickets`: a `url` field Once complete, your `eventType` file should look like this: ```typescript:apps/studio/schemaTypes/eventType.ts import {defineField, defineType} from 'sanity' export const eventType = defineType({ name: 'event', title: 'Event', type: 'document', fields: [ defineField({ name: 'name', type: 'string', }), defineField({ name: 'slug', type: 'slug', }), defineField({ name: 'eventType', type: 'string', }), defineField({ name: 'date', type: 'datetime', }), defineField({ name: 'doorsOpen', type: 'number', }), defineField({ name: 'venue', type: 'reference', to: [{type: 'venue'}], }), defineField({ name: 'headline', type: 'reference', to: [{type: 'artist'}], }), defineField({ name: 'image', type: 'image', }), defineField({ name: 'details', type: 'array', of: [{type: 'block'}], }), defineField({ name: 'tickets', type: 'url', }), ], }) ``` You can now compose and publish documents with multiple fields of varying data types, including a "reference" field that can relate one document with another. You could deploy this to content creators in its current state. It’s a fully-functioning content management system! ### The `details` field as “block content” You might notice that the `details` field appears as a block content (or "rich text") editor in the Studio. Any `array` type field that includes a `block` type will automatically change the UI for the field to this editor. This is how Sanity Studio is designed for authoring and storing block content. Instead of saving block content and rich text in formats like Markdown or HTML as a string, Sanity Studio stores it in the open-source specification called [Portable Text](https://portabletext.org). This unlocks powerful querying and filtering capabilities in your projects and makes integrating across most platforms and frameworks easier. ## Import some content In the following lessons you'll query and render content from this dataset. You could painstakingly hand-craft individual documents in Sanity Studio. Or you can import this test dataset using Sanity CLI. 1. Download the seed data dataset and place `production.tar.gz` at the root of your Studio directory 2. Run the following to import the archive into your project's `production` dataset ```sh # inside /apps/studio pnpm dlx sanity@latest dataset import production.tar.gz production ``` A successful import will give you a bunch of artists, venues, and events in the past and future between 2010–2030. Reminder: All the documents have now been written to the Content Lake, and you are browsing them in your locally configured Sanity Studio. Your multiplayer, real-time dashboard for authoring content is presently stuck on your computer. Time to share it with the world, and your authors, in the next lesson.