MCPcopy
hub / github.com/directus/directus / updateField

Method updateField

api/src/services/fields.ts:503–656  ·  view source on GitHub ↗
(collection: string, field: RawField, opts?: FieldMutationOptions)

Source from the content-addressed store, hash-verified

501 }
502
503 async updateField(collection: string, field: RawField, opts?: FieldMutationOptions): Promise<string> {
504 if (this.accountability && this.accountability.admin !== true) {
505 throw new ForbiddenError();
506 }
507
508 const runPostColumnChange = await this.helpers.schema.preColumnChange();
509 const nestedActionEvents: ActionEventParams[] = [];
510
511 // 'type' is required for further checks on schema update
512 if (field.schema && !field.type) {
513 const existingType = this.schema.collections[collection]?.fields[field.field]?.type;
514 if (existingType) field.type = existingType;
515 }
516
517 try {
518 const hookAdjustedField =
519 opts?.emitEvents !== false
520 ? await emitter.emitFilter(
521 `fields.update`,
522 field,
523 {
524 keys: [field.field],
525 collection: collection,
526 },
527 {
528 database: this.knex,
529 schema: this.schema,
530 accountability: this.accountability,
531 },
532 )
533 : field;
534
535 const record = field.meta
536 ? await this.knex.select('id').from('directus_fields').where({ collection, field: field.field }).first()
537 : null;
538
539 if (
540 hookAdjustedField.type &&
541 (hookAdjustedField.type === 'alias' ||
542 this.schema.collections[collection]!.fields[field.field]?.type === 'alias') &&
543 hookAdjustedField.type !== (this.schema.collections[collection]!.fields[field.field]?.type ?? 'alias')
544 ) {
545 throw new InvalidPayloadError({ reason: 'Alias type cannot be changed' });
546 }
547
548 if (hookAdjustedField.schema) {
549 const existingColumn = await this.columnInfo(collection, hookAdjustedField.field);
550
551 if (existingColumn.is_primary_key) {
552 if (hookAdjustedField.schema?.is_nullable === true) {
553 throw new InvalidPayloadError({ reason: 'Primary key cannot be null' });
554 }
555 }
556
557 // Sanitize column only when applying snapshot diff as opts is only passed from /utils/apply-diff.ts
558 const columnToCompare =
559 opts?.bypassLimits && opts.autoPurgeSystemCache === false ? sanitizeColumn(existingColumn) : existingColumn;
560

Callers 3

applyDiffFunction · 0.95
handlerFunction · 0.95
updateFieldsMethod · 0.95

Calls 15

columnInfoMethod · 0.95
addColumnToTableMethod · 0.95
addColumnIndexMethod · 0.95
isSystemFieldFunction · 0.90
sanitizeColumnFunction · 0.85
transactionFunction · 0.85
translateDatabaseErrorFunction · 0.85
getDatabaseFunction · 0.85
shouldClearCacheFunction · 0.85
clearSystemCacheFunction · 0.85
getSchemaFunction · 0.85
preColumnChangeMethod · 0.80

Tested by

no test coverage detected