Updating default field value in Sanity.io using a script
10 replies
Last updated: Nov 2, 2021
T
Hi all, 👋
I got a question related to data migration or "massive" data updates. I have updated my
post document and added a boolean field called
But my question is, how do I create a script that sets this property to false in all my already created
posts? Thx in advance 🤗
I got a question related to data migration or "massive" data updates. I have updated my
post document and added a boolean field called
isHighlighted. I'd like that field to be
falseby default. Putting the property
initialValue: falseon that document property does work for new posts.
But my question is, how do I create a script that sets this property to false in all my already created
posts? Thx in advance 🤗
Nov 2, 2021, 4:46 PM
Hey Tomas! You'll have to create a patch for this. Do you prefer to use the JS client or the CLI for this?
Nov 2, 2021, 4:53 PM
T
Hi
user M
, thx for replying. Well, it's only 28 articlesNov 2, 2021, 4:58 PM
T
do you have any suggestion?
Nov 2, 2021, 4:58 PM
T
and also, I going over this documentation in sanity's site:
https://www.sanity.io/docs/migrating-data
https://www.sanity.io/docs/migrating-data
Nov 2, 2021, 4:59 PM
I usually write a JS script because it's what I'm more comfortable with. You need to do a few things: configure your client => fetch all of the documents that need to be changed => loop through the documents and commit the changes to each one. The following you could do something like the following:
You could then run the script using
import sanityClient from 'part:@sanity/base/client' const client = sanityClient.withConfig({apiVersion: '2021-03-25'}) const query = `*[_type == 'post' && !defined(isHighlighted)]` //get all of your posts that do not have isHighlighted set const mutateDocs = async (query) => { const docsToMutate = await client.fetch(query, {}) for (const doc of docsToMutate) { const mutation = { isHighlighted: true } console.log('uploading') client .patch(doc._id) // Document ID to patch .set(mutation) // Shallow merge .commit() // Perform the patch and return a promise .then((updatedDoc) => { console.log('Hurray, the doc is updated! New document:') console.log(updatedDoc._id) }) .catch((err) => { console.error('Oh no, the update failed: ', err.message) }) } } mutateDocs(query)
sanity exec --with-user-token.
Nov 2, 2021, 5:49 PM
I usually write a JS script because it's what I'm more comfortable with. You need to do a few things: configure your client => fetch all of the documents that need to be changed => loop through the documents and commit the changes to each one. The following you could do something like the following:
You could then run the script using
import sanityClient from 'part:@sanity/base/client' const client = sanityClient.withConfig({apiVersion: '2021-03-25'}) const query = `*[_type == 'post' && !defined(isHighlighted)]` //get all of your posts that do not have isHighlighted set const mutateDocs = async (query) => { const docsToMutate = await client.fetch(query, {}) for (const doc of docsToMutate) { const mutation = { isHighlighted: true } console.log('uploading') client .patch(doc._id) // Document ID to patch .set(mutation) // Shallow merge .commit() // Perform the patch and return a promise .then((updatedDoc) => { console.log('Hurray, the doc is updated! New document:') console.log(updatedDoc._id) }) .catch((err) => { console.error('Oh no, the update failed: ', err.message) }) } } mutateDocs(query)
sanity exec --with-user-token.
Nov 2, 2021, 5:49 PM
T
you know I had forgotten about the groq method
defined(_field_)available in the queryu
Nov 2, 2021, 6:48 PM
T
your script rocked as well
Nov 2, 2021, 6:48 PM
T
thank you for that
Nov 2, 2021, 6:48 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.