Managing Contentful Migration Scripts with Multiple Environments

The Problem

//Script for creating the Article Content Typemodule.exports = function(migration) {
const article = migration.createContentType('article', {
name: 'Article',
description: 'Some description goes here...',
displayField: 'title'
})
article.createField('title', {
name: "Title",
type: "Symbol",
required: true,
localized: false,
disabled: false,
omitted: false,
deleted: false
})
article.createField('author', {
name: "Author",
type: "Symbol",
required: true,
localized: false,
disabled: false,
omitted: false,
deleted: false
})
article.createField('datePublished', {
name: "Date Published",
type: "Date",
required: true,
localized: false,
disabled: false,
omitted: false,
deleted: false
})
}
//Script for updating the Article Content Typemodule.exports = function(migration) { const article = migration.editContentType('article', {
name: 'Article',
description: 'Some description goes here...',
displayField: 'articleTitle'
})
// Notice, changing the field's name is simpler than changing the field's id. article.editField('title', {
name: "Article Title",
type: "Symbol",
required: true,
localized: false,
disabled: false,
omitted: false,
deleted: false
});
// Adding the new field article.createField('ratings', {
name: "Ratings",
type: "Integer",
required: true,
localized: false,
disabled: false,
omitted: false,
deleted: false
});
}
//Script for creating the Article Content Typeconst OperateOn = require("contentful-smart-migration");

module.exports = async function(migration, context) {
const article = {
id: "article",
opts: {
name: "Article",
description: "Some description goes here...",
displayField: "title",
},
};

const fields = [
{
id: "title",
name: "Title",
},
{
id: "author",
name: "Author",
},
{
id: "datePublished",
name: "Date Published",
type: "Date"
}
];

const contentType = new OperateOn(migration, context, article, fields);
await contentType.operationOnContent();
await contentType.operationOnField();
};
//Using the same script aboveconst OperateOn = require("contentful-smart-migration");

module.exports = async function(migration, context) {
const article = {
id: "article",
opts: {
name: "Article",
description: "Some description goes here...",
displayField: "title",
},
};

const fields = [
{
id: "articleTitle", // With CSM, you can easily update the id
name: "Article Title",
modify: {
old_id: "title"
}
},
{
id: "author",
name: "Author",
},
{
id: "datePublished",
name: "Date Published",
type: "Date",
},
{
id: "ratings",
name: "Ratings"
}
];

const contentType = new OperateOn(migration, context, article, fields);
await contentType.operationOnContent();
await contentType.operationOnField();
};
npm i contentful-smart-migration --save-dev
const OperateOn = require("contentful-smart-migration");
const article = {
id: "article",
opts: {
name: "Article",
description: "Some description goes here...", //optional
displayField: "title",
},
};
const fields = [
{
id: "title",
name: "Title",
},
{
id: "author",
name: "Author",
},
{
id: "datePublished",
name: "Date Published",
type: "Date"
}
];
const contentType = new OperateOn(migration, context, article, fields); 
await contentType.operationOnContent(); // Against the Content Type
await contentType.operationOnField(); // Against the Field

Husband, Father, Web Dev and many other things.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store