How to structure a schema in Sanity.io to allow for multiple documents
16 replies
Last updated: Jun 27, 2022
S
hi ,
Is it possible to be able to add more then 1 document ? how do I structure this in the schema?
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
K
You would need to have a field that’s an array of files/images.
Jun 24, 2022, 12:51 PM
K
{ type: "array", of: [ { type: "image" } ] }
Jun 24, 2022, 12:51 PM
S
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
B
try something like
but personally, i would probably turn the single Document into its own type, then reference that perhaps... really depends on your need/use case
{ 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' } ] } ] }
Jun 24, 2022, 1:18 PM
S
Yea I was thinking about that to just reference the docs and if needed create
Jun 24, 2022, 1:23 PM
B
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
S
yes thats the better way to do it
Jun 24, 2022, 1:29 PM
S
how do I transform the reference so that I can add more then 1 ?
Jun 24, 2022, 1:30 PM
B
you would need to make it a
document typelike
type: 'document'instead of
type: 'file, then move the file type down into the fields
Jun 24, 2022, 1:32 PM
B
then you could make an array of reference to that new type in your original code
Jun 24, 2022, 1:32 PM
S
hmm could you show an code example of how to structure this?
Jun 24, 2022, 1:39 PM
B
sure, here's something similar i have for doing "categories" on a blog
first i create the
then i add the reference array to my
i tried to strip out all the unrelated code in this example
first i create the
categorydocument 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" } ] }
articletype
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" } } } ], }
Jun 24, 2022, 1:54 PM
S
I see you have seperated the schemas, how does the
acctualy work?
fieldset: 'details',
Jun 24, 2022, 2:42 PM
B
oh, i missed stripping that out ... its hard to explain without the rest of the fieldset code
Jun 24, 2022, 2:55 PM
B
i would remove that if you're trying to use my code as a 1:1 proxy
Jun 24, 2022, 2:55 PM
S
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.