🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Limiting the amount of documents per schema in Sanity and switching to Structure Builder without losing data.

11 replies
Last updated: Jun 9, 2022
Hi 👋 first time with Sanity. It's so good! I was just wondering if it is possible to limit the amount of documents that any one schema can make … like here with only wanting one Home Page for example? Or a maximum of 2 say for another document?
Jun 8, 2022, 8:20 PM
Hi
user F
thank you! Can this only be done via the Structure Builder? My site is finished but it's a collection of documents with a Page Builder as the home page.
Jun 9, 2022, 5:35 AM
Yes, this can only be done via the structure builder. 🙂
Jun 9, 2022, 7:39 AM
Ah ok. Are there any plans to add this at the document level, or to Page Builder?
If I switched now to Structure Builder, would that delete all of the existing content?
Jun 9, 2022, 8:04 AM
I’m a little confused. You wouldn’t switch to the structure builder. Your studio is already using the structure builder. You’re just not customizing it, if you will. So if you were to start customizing it, you wouldn’t lose anything, but the way singleton documents work would mean you would need to redo your documents, yes. Because the idea is that you create the singleton manually in the structure builder configuration, and you disable the option to create them via the studio.
When in doubt, back up your dataset beforehand anyway.
🙂
Jun 9, 2022, 8:12 AM
Ah, I see thank you!
How do I backup my dataset?
Jun 9, 2022, 8:14 AM
Thank you!
Jun 9, 2022, 8:17 AM
I could redo my documents as there are only a few but would this also mean changing how I query the data? At the moment I get my data like this

export async function getStaticProps() {
  const data = await client.fetch(
    `
      *[_type in ["homePage"]]{
        _type == "homePage" => {
          pageBuilder[]->{...}
        }
      }
    `);

Jun 9, 2022, 8:19 AM
You can simplify your query a bit:
*[_type in "homePage"] {
  pageBuilder[]->
}
Your query would still work I think anyway.
🙂
Jun 9, 2022, 8:21 AM
Ah, sorry, I've actually got as I'm passing some data props to a footer and a modal…

export async function getStaticProps() {
  const data = await client.fetch(
    `
      *[_type in ["footer", "termsAndConditions", "homePage"]]{
        _type == "footer" => {
          ...
        },
        _type == "termsAndConditions" => {
          ...
        },
        _type == "homePage" => {
          pageBuilder[]->{...}
        }
      }
    `);

Jun 9, 2022, 8:25 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?