Model Content programmatically using the contentful-migration tool

Khaled Garbaya
InstructorKhaled Garbaya
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 3 years ago

In this lesson, you will learn how to model content programmatically using the contentful-migration tool.

Content modeling is hard and nobody can get it right the first time. You have to model your content at a time where you have the least (real) experience, at the start of your project. Your choices will haunt you for the rest of your product lifecycle in the case of the incumbents - with Contentful migration tool you could potentially start anywhere with your content model, then refine it as you learn what you truly need.

Prerequisite

You need to have the contentful-cli installed

npm install -g contentful-cli

and then login

contentful login

and follow the steps to get you setup

Instructor: [00:00] Another way we can create content types in Contentful is using the Contentful migration tool. This will allow us to programmatically create them. Let's go ahead and code our instructor content type. First thing, we need to export the function.

[00:20] Inside of this function, we'll have access to the migration object that will allow us to do any sort of manipulations to our content type. First thing we need to do is create the content type, then we need to define its fields.

[00:41] We can define the appearances or the slug field, for example. To create the content type, we can call the migration.createContentTypes, and we give it the ID, the name, and description. Now, we have an instance of our created content type. We can create the fields by calling createField on the instance.

[01:08] A field will have the ID, the name, and type. Let's define the rest of the fields. Now, this is done, we need to tell Contentful how to deal with the slug field. Remember, the previous lesson, we set it to a type slug. We can do the same using the migration tool.

[01:38] Here, we call the change-editor-interface on the content type, and we give it the field ID and then the ID of the widget. We can do the same for the website field and other similar fields.

[01:58] Let's save our file, and now, it's time to run our migration. I already gone ahead and installed the Contentful CLI. Then after that's done, you need to call Contentful login. This will open up a browser window and authenticate you to Contentful.

[02:21] What we need to do here is to call the migration on the space that we have. We can do Contentful, space, migration. Here, we give it the space ID. The space ID, we can get from Contentful. It's basically the URL.

[02:52] Here, we pass it the file that we want to run, basically our migration code. We hit enter. You can see here, the tool gives us the summary of all the migration, and it's waiting for us to say yes. We say yes, and now it's creating our content type.

[03:23] That's done. Let's check. You can see here, we have our instructor content type, with all the fields. Let's do the same for our SEO content type. First thing, exporting the function. Then we create the content type. Lastly, we define the fields.

[04:16] We save, and let's run our migration. Here, instead of passing the instructor, we will pass the SEO JS file. Again, we work through the summary. Everything looks OK, and we accept our migration. Lastly, we can define the lesson content type that will link to both these content types.

[04:56] We create the content type again. We define the fields. We have the title, the slug, the body of type rich text. This is the instructor, and the syntax is a bit different. It's type link, and in the validations we tell to only link to instructor field. For the image, we add our SEO, and we tell it also to only link to the SEO content type.

[05:41] The last thing is to change the appearance of the slug field. We run our migration again for the lesson. Summary looks good, and we hit enter. Now, if we go to Contentful and refresh this page, we should see all our three content types.

[06:18] That's how you can create them programmatically. This code should live next to your website in the repository, so another developer can bootstrap a separate space. For example, for testing.

egghead
egghead
~ 3 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today