Saskia Bobinska
Senior Support Engineer @Sanity
Sometimes you need to change only one key-value-pair in your data, this is how you can do it in 2 lines
/** create a copy of the originalDocumentData object
* while removing the existing `_id` (which will be the published one).
*
* You can add more values you want to remove in one line of code.
*/
const draftDoc = (({_id, ...objectValues}) => objectValues)(originalDocumentData)
/** Extend the `newDoc` object with a draft `_id`
*/
draftDoc['_id'] = 'drafts.' + originalDocumentData?._id
/** Another way would be using this logic,
* but you would then need to add further lines to delete every other value.
*/
const newDocument = {...doc, ...{_id: newDocId, _type: NEW_TYPE}}
delete newDocument.incomingReferences
delete newDocument._rev
Lets assume you have a immutable value (for example _id
or _type
) you need to change for a couple of different types. Immutable values cannot be patched, so you would need to copy the data without the value you want to change and then redefine it, before createOrReplace
the new document.
Maybe you need to unpublish a bunch of documents with different data structures or need to change the _type
of a couple of types, which means you would have to change only one part (_id
or _type
) of the object, without touching the other values.
I found this easy way to do so, and thought someone might find it useful too.
FYI: To change immutable values you can of course use better routes (Migration of types | Unpublishing via Document Action), I just needed an example. And sometimes you need a bespoke solution outside of the 2 examples.
For more information on how to work with objects in Javascript/Typescript, see the Mozilla documentation on objects.
Senior Support Engineer @Sanity
Use a migration script to update all references from one document to another
Go to Update references via Content Migration scriptIf you install plugins or other packages with conflicting (peer) dependencies, you can use this useful workaround to unblock yourself.
Go to What to do when Studio crashes due to conflicts in dependency versions?When creating custom document actions, it can be necessary to check, wether all validation rules are full-filled.
Go to Use the validation status of a document in your custom document actionsOnly return a value when a condition is fulfilled
Go to Conditional values in GROQ queries