directus
82 строки · 2.7 Кб
1import { parseJSON } from '@directus/utils';2import type { Knex } from 'knex';3
4// [before, after, after-option additions]
5const changes: [string, string, Record<string, any>?][] = [6['button-links', 'presentation-links'],7['checkboxes', 'select-multiple-checkbox'],8['code', 'input-code'],9['color', 'select-color'],10['datetime', 'datetime'],11['divider', 'presentation-divider'],12['dropdown', 'select-dropdown'],13['dropdown-multiselect', 'select-multiple-dropdown'],14['file', 'file'],15['hash', 'input-hash'],16['icon', 'select-icon'],17['image', 'file-image'],18['m2a-builder', 'list-m2a'],19['many-to-many', 'list-m2m'],20['many-to-one', 'select-dropdown-m2o'],21['markdown', 'input-rich-text-md'],22['notice', 'presentation-notice'],23['numeric', 'input'],24['one-to-many', 'list-o2m'],25['radio-buttons', 'select-radio'],26['repeater', 'list'],27['slider', 'slider'],28['slug', 'input', { slug: true }],29['tags', 'tags'],30['text-input', 'input'],31['textarea', 'input-multiline'],32['toggle', 'boolean'],33['translations', 'translations'],34['tree-view', 'list-o2m-tree-view'],35['user', 'select-dropdown-m2o', { template: '{{avatar.$thumbnail}} {{first_name}} {{last_name}}' }],36['wysiwyg', 'input-rich-text-html'],37
38// System:39['collection', 'system-collection'],40['collections', 'system-collection-multiple'],41['display-template', 'system-display-template'],42['field', 'system-field'],43['interface', 'system-interface'],44['interface-options', 'system-interface-options'],45['scope', 'system-scope'],46['system-language', 'system-language'],47['tfa-setup', 'system-mfa-setup'],48];49
50export async function up(knex: Knex): Promise<void> {51for (const [before, after, options] of changes) {52// If any options need to be added, update the fields one by one in order to update the pre-existing field options53if (options) {54const fields = await knex55.select<{ id: number; options: Record<string, unknown> }[]>('id', 'options')56.from('directus_fields')57.where({ interface: before });58
59for (const { id, options: existingOptionsRaw } of fields) {60const existingOptions =61typeof existingOptionsRaw === 'string' ? parseJSON(existingOptionsRaw) : existingOptionsRaw;62
63const newOptions = {64...(existingOptions || {}),65...options,66};67
68await knex('directus_fields')69.update({ interface: after, options: JSON.stringify(newOptions) })70.where({ id });71}72} else {73await knex('directus_fields').update({ interface: after }).where({ interface: before });74}75}76}
77
78export async function down(knex: Knex): Promise<void> {79for (const [before, after] of changes) {80await knex('directus_fields').update({ interface: before }).where({ interface: after });81}82}
83