Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Updating default field value in Sanity.io using a script

10 replies
Last updated: Nov 2, 2021
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
isHighlighted
. I'd like that field to be
false
by default. Putting the property
initialValue: false
on 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
Hi
user M
, thx for replying. Well, it's only 28 articles
Nov 2, 2021, 4:58 PM
do you have any suggestion?
Nov 2, 2021, 4:58 PM
and also, I going over this documentation in sanity's site:
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:
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) 
You could then run the script using
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:
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) 
You could then run the script using
sanity exec --with-user-token
.
Nov 2, 2021, 5:49 PM
you know I had forgotten about the groq method
defined(_field_)
available in the queryu
Nov 2, 2021, 6:48 PM
your script rocked as well
Nov 2, 2021, 6:48 PM
thank you for that
Nov 2, 2021, 6:48 PM
Very happy to help!
Nov 2, 2021, 6:49 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?