1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Model Content programmatically using the contentful-migration tool

    Khaled GarbayaKhaled Garbaya
    javascriptJavaScript

    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

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.

    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.