How to rename a field in a model and all its content retroactively

Re-naming a field for content that’s already been published requires two main updates:

on the model level:

Update the field name in the model configuration editor will ensure all newly created content has the field.

on the content level:

Now we want to apply the new field name retroactively to all content that’s already been published, in two steps using your favorite scripting language:
1- Read all content on a model using our content API
2 - Use our write API to update the content entry, you’ll need to generate a private key to be used here,

So in node it’d be something like this

// read all result  might need to paginate if list > 100
const allContentOnAmodel = await fetch(`https//builder.io/api/v2/content/${modelName}`)
  .then(res => res.json());

allContentOnAmodel.results.map(entry =>  {
  entry[newFieldName] = entry[oldFieldName];
  delete entry[oldFieldName];
  await fetch(`https://builder.io/api/v2/write/${modelName}`, {
   method: 'POST',
   headers: {
     Authorization: `bearer ${privateKey}`,
    },
    body: JSON.strinigify(entry),
  })
})



1 Like