Created
August 16, 2022 22:48
-
-
Save lucasassisrosa/03b925df24bf43b469f7005bf6c5cbae to your computer and use it in GitHub Desktop.
CMS Content Batch Update using Contentful Management API - Update target entries "Reference" Field to "Short Text" Field
This file contains hidden or 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
const contentful = require('contentful-management'); | |
const REFERENCE_FIELD = 'topic_ref'; | |
const SHORT_TEXT_FIELD = 'topic'; | |
const client = contentful.createClient({ | |
accessToken: process.env.CONTENTFUL_MANAGEMENT_TOKEN, | |
}); | |
client | |
.getSpace(process.env.CONTENTFUL_SPACE) | |
.then((space) => space.getEnvironment(process.env.CONTENTFUL_ENVIRONMENT)) | |
.then((environment) => | |
environment.getEntries({ | |
content_type: process.env.CONTENTFUL_CONTENT_TYPE, | |
limit: 1000, | |
skip: 0, | |
// reference field | |
`fields.${REFERENCE_FIELD}.sys.contentType.sys.id`: process.env.CONTENTFUL_REFERENCE_FIELD_CONTENT_TYPE_ID), | |
`fields.${REFERENCE_FIELD}.sys.id`: process.env.CONTENTFUL_REFERENCE_FIELD_ENTRY_ID, | |
}) | |
) | |
.then((entries) => { | |
console.log(`updating ${entries.total}`); | |
entries.items.map(async (entry) => { | |
delete entry.fields[REFERENCE_FIELD]; | |
entry.fields[SHORT_TEXT_FIELD] = { 'en-US': 'Test' }; | |
entry | |
.update() | |
.then((entry) => entry.publish()) | |
.then((updatedEntry) => { | |
console.log(`done for ${updatedEntry.fields.slug['en-US']}`); | |
}); | |
}); | |
}) | |
.catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment