MCPcopy
hub / github.com/directus/directus / deleteField

Method deleteField

api/src/services/fields.ts:697–903  ·  view source on GitHub ↗
(collection: string, field: string, opts?: MutationOptions)

Source from the content-addressed store, hash-verified

695 }
696
697 async deleteField(collection: string, field: string, opts?: MutationOptions): Promise<void> {
698 if (this.accountability && this.accountability.admin !== true) {
699 throw new ForbiddenError();
700 }
701
702 const runPostColumnChange = await this.helpers.schema.preColumnChange();
703 const nestedActionEvents: ActionEventParams[] = [];
704
705 try {
706 if (opts?.emitEvents !== false) {
707 await emitter.emitFilter(
708 'fields.delete',
709 [field],
710 {
711 collection: collection,
712 },
713 {
714 database: this.knex,
715 schema: this.schema,
716 accountability: this.accountability,
717 },
718 );
719 }
720
721 await transaction(this.knex, async (trx) => {
722 const relations = getRelations(this.schema.relations, collection, field);
723
724 const relationsService = new RelationsService({
725 knex: trx,
726 accountability: this.accountability,
727 schema: this.schema,
728 });
729
730 const fieldsService = new FieldsService({
731 knex: trx,
732 accountability: this.accountability,
733 schema: this.schema,
734 });
735
736 for (const relation of relations) {
737 const isM2O = relation.collection === collection && relation.field === field;
738
739 // If the current field is a m2o, delete the related o2m if it exists and remove the relationship
740 if (isM2O) {
741 await relationsService.deleteOne(collection, field, {
742 autoPurgeSystemCache: false,
743 bypassEmitAction: (params) =>
744 opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params),
745 });
746
747 if (
748 relation.related_collection &&
749 relation.meta?.one_field &&
750 relation.related_collection !== collection &&
751 relation.meta.one_field !== field
752 ) {
753 await fieldsService.deleteField(relation.related_collection, relation.meta.one_field, {
754 autoPurgeCache: false,

Callers 3

applyDiffFunction · 0.95
handlerFunction · 0.95
deleteOneMethod · 0.95

Calls 15

deleteOneMethod · 0.95
deleteByQueryMethod · 0.95
getRelationsFunction · 0.90
transactionFunction · 0.85
getCollectionMetaUpdatesFunction · 0.85
shouldClearCacheFunction · 0.85
clearSystemCacheFunction · 0.85
getSchemaFunction · 0.85
preColumnChangeMethod · 0.80
emitFilterMethod · 0.80

Tested by

no test coverage detected