Skip to content

Instantly share code, notes, and snippets.

@coreyward
Created June 28, 2022 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coreyward/1e49700b961434c1c7c5f55275061c95 to your computer and use it in GitHub Desktop.
Save coreyward/1e49700b961434c1c7c5f55275061c95 to your computer and use it in GitHub Desktop.
Replace all references - Sanity
// import and configure the client
const oldRef = "abc123"
const newRef = "def456"
client
.fetch(
`*[references($oldRef)][0...250] {
_id,
_rev,
}`,
{ oldRef }
)
.then((docs) => {
const tx = docs.reduce(
(tx, doc) =>
tx.patch(doc._id, {
ifRevisionID: doc._rev,
set: {
[`..[_ref == "${oldRef}"]._ref`]: newRef,
},
}),
client.transaction()
);
tx.commit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment