Finding references to a deprecated component in Sanity schemas and documents.
9 replies
Last updated: Sep 20, 2022
D
I’m deprecating a component. Is there an easy way to find all documents which reference a specific schema as a block?
Sep 20, 2022, 6:30 PM
There is! You can find it using:
Though, I'm unclear from your question whether the component you're deprecating is a document you're referencing, some schema that isn't a document, or a schema inside of block content. That query will only work if it's a reference to a document.
*[references(<id-of-document>)].
Though, I'm unclear from your question whether the component you're deprecating is a document you're referencing, some schema that isn't a document, or a schema inside of block content. That query will only work if it's a reference to a document.
Sep 20, 2022, 6:33 PM
D
Sorry, for being unclear. The schema is for an object not a document.
Sep 20, 2022, 6:35 PM
D
e.g. find all documents that use the custom form block.
Sep 20, 2022, 6:36 PM
No worries, just wanted to make sure I'm giving you relevant information.
Does that object always have the same name when you use it in your schema? If so, something like
Does that object always have the same name when you use it in your schema? If so, something like
*[defined(<name-of-field>)]should get it for you. If that's not the case, I"m afraid there's not an easy way to find each place it's used.
Sep 20, 2022, 6:37 PM
D
thx, and where would you be executing that query?
Sep 20, 2022, 6:38 PM
D
The schema looks roughly like this:
{ title: 'body', type: 'array', of: [ { type: block, .... }, { type: 'custom-form'}, { ... ~100 components } ] }
Sep 20, 2022, 6:39 PM
I usually write a JS script that I can use to batch remove a field that performs that query. Here's an example:
It looks like you're using that custom-form inside of block content, so your query will need to be more complicated than just using the
import { studioClient } from './studioClient'; import cq from 'concurrent-queue'; // Create a queue to limit the rate at which you write changes to Sanity let queue = cq() .limit({ concurrency: 25 }) .process(task => { return new Promise(function (resolve, reject) { setTimeout(resolve.bind(undefined, task), 1000); }); }); const mutateDocs = async () => { //Fetch the documents you need to mutate const query = `*[defined(<name-of-field>)]`; const docs = await studioClient.fetch(query); // Loop through all of the docs returned from our query for (const doc of docs) { queue(doc).then(async () => { // Add a message to help us know the upload is happening console.log(`Mutating ${doc._id}`); // Tell the client to patch the current document studioClient .patch(doc._id) // Unset the field .unset(['<name-of-field>']) // Commit the changes .commit() .then(updatedDoc => console.log(`Hurray, the doc is updated! New document:`, updatedDoc) ) .catch(err => console.error('Oh no, the update failed: ', err.message)); }); } }; mutateDocs(); // execute this script by running // $ sanity exec ./lib/utils/mutateDocs.js --withUserToken
definedfunction. You'll probably need to do something like
*['custom-form' in body[]._type].
Sep 20, 2022, 6:48 PM
D
k, thanks for the pointers
Sep 20, 2022, 8:14 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.