Migrating data from old to new field using Sanity's Patches API
5 replies
Last updated: Oct 6, 2021
T
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.
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
T
Hey
Patches API docs, Can you show me where I can take the content from Body in the referenced document, please?
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
T
Great. Thank you so much!!
Oct 6, 2021, 7:48 PM
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
