Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Content model migration
module.exports = function (migration) {
// create a new content type “Conference”
const conference = migration.createContentType('conference')
.name('Conference/Meetup')
.displayField('name')
// set up the new fields
conference.createField('name').type('Symbol').required(true).name('Conference/Meetup name')
conference.createField('country').type('Symbol').required(true).name('Country Code')
conference.createField('city').type('Symbol').required(true).name('City')
// create the new reference fields on the “Event” content type
const event = migration.editContentType('event')
event.createField('conference')
.name('Conference')
.type('Link')
.linkType('Entry')
.validations([
{
"linkContentType": ['conference']
}
])
// create new entries and link them
migration.deriveLinkedEntries({
// take all events
contentType: 'event',
// the fields you want to use to derive entries
from: ['name', 'country', 'city'],
// to which field the new entries will be linked to
toReferenceField: 'conference',
// the content type of the new entries
derivedContentType: 'conference',
// the fields you want to set
derivedFields: ['name', 'country', 'city'],
// the ID of the newly created entries
identityKey: async (from) => {
// remove special characters and spacing
return getId(from.name['en-US'])
},
// set the values for the newly created entries
deriveEntryForLocale: async (inputFields, locale) => {
return {
name: inputFields.name[locale].replace(/\s(\d{2,4}|#\d+)/g, ''),
country: inputFields.country[locale] || 'N/A',
city: inputFields.city[locale] || 'N/A'
}
}
})
// move the new field up
// newly created fields are at the end usually
event.moveField('conference').afterField('name')
// clean up the things you don't need anymore
event.deleteField('country')
event.deleteField('city')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment