đŸ‘‹ Next.js Conf 2024: Come build, party, run, and connect with us! See all events

How to structure a schema in Sanity.io to allow for multiple documents

16 replies
Last updated: Jun 27, 2022
hi ,
Is it possible to be able to add more then 1 document ? how do I structure this in the schema?
Jun 24, 2022, 12:51 PM
You would need to have a field that’s an array of files/images.
Jun 24, 2022, 12:51 PM
{
  type: "array",
  of: [ { type: "image" } ]
}
Jun 24, 2022, 12:51 PM
my documents schema looks like this. where should I fit the array in ?
{
        title: 'Document',
        description: "Place document here",
        name: 'file',
        type: 'file',
        fields: [
          {
            name: 'description',
            type: 'string',
            title: 'Description'
          }
        ]
      },
Jun 24, 2022, 12:57 PM
try something like
{
  title: 'Documents',
  description: 'A list of documents',
  name: 'documents',
  type: "array",
  of: [
    {
      title: 'Document',
      description: "Place document here",
      name: 'file',
      type: 'file',
      fields: [
        {
          name: 'description',
          type: 'string',
          title: 'Description'
        }
      ]
    }
  ]
}
but personally, i would probably turn the single Document into its own type, then reference that perhaps... really depends on your need/use case
Jun 24, 2022, 1:18 PM
Yea I was thinking about that to just reference the docs and if needed create
Jun 24, 2022, 1:23 PM
it also gives you the advantage to re-use that document elsewhere if you needed, rather than it being tied to a single place
Jun 24, 2022, 1:27 PM
yes thats the better way to do it
Jun 24, 2022, 1:29 PM
how do I transform the reference so that I can add more then 1 ?
Jun 24, 2022, 1:30 PM
you would need to make it a
document type
like
type: 'document'
instead of
type: 'file
, then move the file type down into the fields
Jun 24, 2022, 1:32 PM
then you could make an array of reference to that new type in your original code
Jun 24, 2022, 1:32 PM
hmm could you show an code example of how to structure this?
Jun 24, 2022, 1:39 PM
sure, here's something similar i have for doing "categories" on a blog
first i create the
category
document type
export default {
  title: "Category",
  name: "category",
  type: "document",
  fields: [
    {
      title: "Title",
      name: "title",
      type: "string",
    },
    {
      title: "Slug",
      name: "slug",
      type: "slug",
      options: {
        source: 'title',
        maxLength: 200, // will be ignored if slugify is set
        slugify: input => input.toLowerCase().replace(/\s+/g, '-').slice(0, 200)
      },
      validation: Rule => Rule.required()
    },
    {
      title: "Description",
      name: "description",
      type: "text"
    }
  ]
}
then i add the reference array to my
article
type
export default {
  title: "Article",
  name: "article",
  type: "document",
  fields: [
    {
      title: "Title",
      name: "title",
      type: "string"
    },
    {
      title: "Categories",
      name: "categories",
      fieldset: 'details',
      type: "array",
      of: [
        {
          type: "reference",
          to: [{type: "category"}]
        }
      ],
      options: {
        sortable: false
      },
      preview: {
        select: {
          title: "title"
        }
      }
    }
  ],
}
i tried to strip out all the unrelated code in this example
Jun 24, 2022, 1:54 PM
I see you have seperated the schemas, how does the
fieldset: 'details',
acctualy work?
Jun 24, 2022, 2:42 PM
oh, i missed stripping that out ... its hard to explain without the rest of the fieldset code
Jun 24, 2022, 2:55 PM
i would remove that if you're trying to use my code as a 1:1 proxy
Jun 24, 2022, 2:55 PM
This was exactly what I was looking for thanks
user G
!
Jun 27, 2022, 6:41 AM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?