Created
November 10, 2021 22:15
-
-
Save kmelve/f44e46ea3cfb04aad4eb90ad565fa720 to your computer and use it in GitHub Desktop.
Simple migration script that can be run with `sanity exec` in a Sanity Studio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// migration.js | |
/** | |
* Run: | |
* sanity exec --with-user-token migration.js | |
* | |
*/ | |
import sanityClient from 'part:@sanity/base/client' | |
const client = sanityClient.withConfig({ apiVersion: '2021-11-10' }) | |
// Patch 1000 at a time | |
const query = `*[_type == "stream"][0...999]{title, publishedDate, _id, _rev}` | |
async function migrate() { | |
const docs = await client.fetch(query) | |
const patches = docs.map((doc) => ({ | |
id: doc._id, | |
patch: { | |
set: { | |
test: true | |
}, | |
}, | |
ifRevisionID: doc._rev, // prevent race conditions | |
})) | |
const transactions = patches.reduce( | |
(tx, patch) => tx.patch(patch.id, patch.patch), | |
client.transaction() | |
) | |
const result = await transactions.commit() | |
console.log('Done!') | |
console.log(JSON.stringify(result, null, 2)) | |
} | |
migrate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment