Contentful logo

Contentful Community

How to programmatically create content models

#1

Hi!

Is there a way to manage the content model in code alone? Migration scripts seem to be an ok alternative that i’m currently looking into, but somehow it seems to consume a bit more work to what i’m looking for.

The way i’m looking for to work with would be that you would define the content model in the code like:

“displayField”: “name”,
“name”: “Card Header”,
“description”: “Header Card”,
“fields”: [
{
“id”: “name”,
“name”: “Name”,
“type”: “Symbol”,
“localized”: false,
“required”: true,
“validations”: [
],
“disabled”: false,
“omitted”: false
},
{
“id”: “headerText”,
“name”: “Header Text”,
“type”: “Symbol”,
“localized”: true,
“required”: true,
“validations”: [
],
“disabled”: false,
“omitted”: false
},
{
“id”: “headerIcon”,
“name”: “Header Icon”,
“type”: “Link”,
“localized”: false,
“required”: true,
“validations”: [
],
“disabled”: false,
“omitted”: false,
“linkType”: “Asset”
}
]

And if you have some changes to the content model you would simply make the changes to what you have defined and commit the changes? Pushing the information to the environment would make the necessary changes?

In addition, It seems that since we don’t have the migration scripts in place at the moment - changes have been done both in master and dev environments using the contentful ui, which is not good of course. I’m planning on exporting the info from master and dev check the differences in the export and combining them and importing this combined json to both dev and master. Will this work or would there be a better solution to fix this? Master env changes have only done to the description field of the content model.

How does the export and import work? Does it simply update and create the content models in brute force or is there something else we would need to account for?

Thank you in advance!

0 Likes

#2

Hi @kobemeat,

The export/import tool does work in lines of a “brute force” in such a way that every item is programmatically exported and recreated in your source space.

You should be able to programmatically create and edit content models without any problems using the migration tool, but could be a bit more specific in what exactly you’d like to change in your content models? In the end of the day, there are a few changes that wouldn’t be possible to be made at the actual level of the API, such as modifying their IDs, or IDs of their fields.

0 Likes

#3

@gabriel,

So with the migration tool - in order to create a new content model you do something like this:

// New category content type.
const category = migration.createContentType(‘category’)
.name(‘Category’)
.displayField(‘name’);
category.createField(‘name’).type(‘Symbol’).required(true).name(‘Name’);
category.createField(‘slug’).type(‘Symbol’).required(true).name(‘URL Slug’).validations([{ “unique”: true }]);
category.createField(‘image’).type(‘Link’).linkType(‘Asset’).name(‘Image’);

Instead of using the createContentType functionality - i’m looking for a way to define the content type similar to this:

contentModel: [{
name: “Category”
displayField: “Category”,
name: {type: “Symbol”, required: true, name: “Name”} ,
slug: {type: “Symbol”, required: true, name: “Url Slug”, validations: [{“unique”: true}],
image: {type: “Link”, linkType: “Asset”, name: “Image”
}]

And in case we need to make some changes to the Content Model we would simply change the already defined model like this:

contentModel: [{
name: “Category Changed
displayField: “Category Changed”,
name: {type: “Symbol”, required: true, name: “Name”} ,
slug: {type: “Symbol”, required: true, name: “Url Slug”, validations: [{“unique”: true}]},
image: {type: “Link”, linkType: “Asset”, name: “Image”},
newField: {type: “Symbol”, required: true, name: “New Field”}
}]

I believe I’ve seen this somewhere, but can’t seem to find it anymore. Thanks

0 Likes

#4

You should be able to do so by using the editContentType(id[, opts] method as it uses the same options as createContentType, so you should be able to also declare a new field with createField() while using it.

0 Likes

#5

Hi @kobemeat,

At Watermark Church we’ve found that Migration scripts are the best way to go. Since we are a Rails shop, they fit nicely in the db/migrate directory next to the Rails migrations. To make the execution of these scripts automatic for us, we’ve even forked the Contentful Migration tool. Our fork scans a directory and only executes the migrations which have not already been executed in a space.

In order to make generating the migrations easier, we’ve also built a nodejs-based tool called contentful-schema-diff that will inspect two spaces (or two export files, or an export file against a space) and write out a set of migration scripts for the differences in content types. We use this to great effect in our development workflow - after making changes in our dev space, we simply run the diff tool, inspect the outputted migration scripts, and commit them to the source repository.

All of these tools are open-source at the moment, and we will continue supporting them as long as we are on the Contentful platform for our primary CMS.

0 Likes