Migrating data from old to new field using Sanity's Patches API

5 replies
Last updated: Oct 6, 2021
Hello team, thank you for the great product.I have a question about migrating data from old to new field. In
this document , we have a new field called Body (rich text) and old field called Content (reference). Inside the linked Content document , we have Body field, which is rich text field.Question: How can we programmatically copy data from Body (inside Content field) to the new Body field? I know we can do it manually but unfortunately we have 1000+ posts so it's really hard to do by hand.
Oct 6, 2021, 3:17 PM
Hey
user M
. Thanks for the response.On the
Patches API docs, Can you show me where I can take the content from Body in the referenced document, please?
Oct 6, 2021, 7:26 PM
Using the JS client it would look something like the script below. I haven't tested it, so there may be some typos I haven't caught! Note that you would need to set up some sort of queueing if you're going through hundreds of documents to avoid hitting the API rate limit .
//import and configure your sanity client
const sanityClient = require('@sanity/client')

const client = sanityClient({
  projectId: <your-project-id>',
  dataset: <your-dataset-name>,
  apiVersion: '2021-03-29', 
  token: <your-read-write-token>,
})


const mutateDocs = async () => {
  //fetch the docs that need to be mutated
  const docs = await client.fetch(`*[_type == <your-type-to-change>]`)
  for (const doc of docs) {
    const { _id, body, content } = doc
    //get the content from the referenced document
    const resolvedContent = await client.fetch(`*[_id == $contentRef] { body }`, { contentRef: content._ref })
    client.patch(_id)
          .set({
             body: resolvedContent
          })
         .commit()
         .then(updatedDoc => console.log(`Hurray, the document: ${id} was updated`))
         . catch(err => console.log('Update failed', err.message))
  }
}
Oct 6, 2021, 7:46 PM
Using the JS client it would look something like the script below. I haven't tested it, so there may be some typos I haven't caught! Note that you would need to set up some sort of queueing if you're going through hundreds of documents to avoid hitting the API rate limit .
//import and configure your sanity client
const sanityClient = require('@sanity/client')

const client = sanityClient({
  projectId: <your-project-id>',
  dataset: <your-dataset-name>,
  apiVersion: '2021-03-29', 
  token: <your-read-write-token>,
})


const mutateDocs = async () => {
  //fetch the docs that need to be mutated
  const docs = await client.fetch(`*[_type == <your-type-to-change>]`)
  for (const doc of docs) {
    const { _id, body, content } = doc
    //get the content from the referenced document
    const resolvedContent = await client.fetch(`*[_id == $contentRef] { body }`, { contentRef: content._ref })
    client.patch(_id)
          .set({
             body: resolvedContent
          })
         .commit()
         .then(updatedDoc => console.log(`Hurray, the document: ${id} was updated`))
         . catch(err => console.log('Update failed', err.message))
  }
}
Oct 6, 2021, 7:46 PM
Great. Thank you so much!!
Oct 6, 2021, 7:48 PM
Happy to help! Let us know if you're still hitting stumbling blocks!
Oct 6, 2021, 7:53 PM

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?