Skip to content

Creating a new content type with recursive parent child relationship

Sam Bauers edited this page Aug 13, 2022 · 3 revisions

This migration script will create a content type called page. It will have a short text title, which will be the content type's display field.

page will also have an entry link to itself called parentPage. This will allow any entry to store a link to another "parent" page entry.

import type { MigrationFunction } from 'contentful-migration'

const migrationFunction: MigrationFunction = function (migration) {
  // create the page content type
  const page = migration.createContentType('page').name('Page')

  // create a required title field
  page.createField('title').type('Symbol').name('Title').required(true)
  page.displayField('title')

  // create a parent page field which links back to this content type
  page
    .createField('parentPage')
    .type('Link')
    .linkType('Entry')
    .name('Parent page')
    .validations([
      {
        linkContentType: ['page'],
      },
    ])

  // create a text content field
  page.createField('content').type('Text').name('Content')
}

export = migrationFunction