Skip to content

Instantly share code, notes, and snippets.

@abedzantout
Created September 19, 2019 16:02
Show Gist options
  • Save abedzantout/130fab45a2ce4d051c7964f7d47c6209 to your computer and use it in GitHub Desktop.
Save abedzantout/130fab45a2ce4d051c7964f7d47c6209 to your computer and use it in GitHub Desktop.
Automatically populate Contentful
const authorFields = [
{
id: 'name',
name: 'Name',
type: 'Symbol',
required: true
},
{
id: 'title',
name: 'Job Title',
type: 'Symbol',
required: true
},
{
id: 'company',
name: 'Company',
type: 'Symbol',
required: true
},
{
id: 'shortBio',
name: 'Short Bio',
type: 'Text',
required: true
},
{
id: 'email',
name: 'Email',
type: 'Symbol',
required: true
},
{
id: 'twitter',
name: 'Twitter',
type: 'Symbol',
required: false
}
];
const blogFields = [
{
id: 'title',
name: 'Title',
type: 'Symbol',
required: true
},
{
id: 'slug',
name: 'Slug',
type: 'Symbol',
required: true
},
{
id: 'heroImage',
name: 'Hero Image',
type: 'Link',
linkType: 'Asset',
required: true
},
{
id: 'description',
name: 'Description',
type: 'Text',
required: true
},
{
id: 'body',
name: 'Body',
type: 'Text',
required: true
},
{
id: 'author',
name: 'Author',
type: 'Link',
required: true,
linkType: 'Entry',
validations: [{ linkContentType: ['author'] }]
},
{
id: 'publishDate',
name: 'Publish Date',
type: 'Date',
required: true
},
{
id: 'tags',
name: 'Tags',
type: 'Array',
required: false,
items: {
type: 'Link',
linkType: 'Entry',
validations: [{ linkContentType: ['tag'] }]
}
}
];
const tagFields = [
{
id: 'name',
name: 'Name',
type: 'Symbol',
required: true
}
];
module.exports = function(migration, context) {
const tag = migration.createContentType('tag', {
name: 'Tag',
description: 'tag',
displayField: 'name'
});
tagFields.forEach(field => {
tag.createField(field.id, {
name: field.name,
type: field.type,
required: field.required
});
});
const author = migration.createContentType('author', {
name: 'Author',
description: 'Author of Blog post',
displayField: 'name'
});
authorFields.forEach(field => {
author.createField(field.id, {
name: field.name,
type: field.type,
required: field.required
});
});
const blog = migration.createContentType('blogPost', {
name: 'Blog Post',
description: 'Blog Post',
displayField: 'title'
});
blogFields.forEach(field => {
const options = {
name: field.name,
type: field.type,
required: field.required,
validations: field.validations ? [...field.validations] : []
};
if (field.linkType) {
options.linkType = field.linkType;
}
if (field.items) {
options.items = field.items;
}
blog.createField(field.id, options);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment