* Delete a single collection This will delete the table and all records within. It'll also * delete any fields, presets, activity, revisions, and permissions relating to this collection
(collectionKey: string, opts?: MutationOptions)
| 632 | * delete any fields, presets, activity, revisions, and permissions relating to this collection |
| 633 | */ |
| 634 | async deleteOne(collectionKey: string, opts?: MutationOptions): Promise<string> { |
| 635 | if (this.accountability && this.accountability.admin !== true) { |
| 636 | throw new ForbiddenError(); |
| 637 | } |
| 638 | |
| 639 | const nestedActionEvents: ActionEventParams[] = []; |
| 640 | |
| 641 | try { |
| 642 | const collections = await this.readByQuery(); |
| 643 | |
| 644 | const collectionToBeDeleted = collections.find((collection) => collection.collection === collectionKey); |
| 645 | |
| 646 | if (!!collectionToBeDeleted === false) { |
| 647 | throw new ForbiddenError(); |
| 648 | } |
| 649 | |
| 650 | await transaction(this.knex, async (trx) => { |
| 651 | if (collectionToBeDeleted!.schema) { |
| 652 | await trx.schema.dropTable(collectionKey); |
| 653 | } |
| 654 | |
| 655 | // Make sure this collection isn't used as a group in any other collections |
| 656 | await trx('directus_collections').update({ group: null }).where({ group: collectionKey }); |
| 657 | |
| 658 | if (collectionToBeDeleted!.meta) { |
| 659 | const collectionsItemsService = new ItemsService('directus_collections', { |
| 660 | knex: trx, |
| 661 | accountability: this.accountability, |
| 662 | schema: this.schema, |
| 663 | }); |
| 664 | |
| 665 | await collectionsItemsService.deleteOne(collectionKey, { |
| 666 | bypassEmitAction: (params) => |
| 667 | opts?.bypassEmitAction ? opts.bypassEmitAction(params) : nestedActionEvents.push(params), |
| 668 | }); |
| 669 | } |
| 670 | |
| 671 | if (collectionToBeDeleted!.schema) { |
| 672 | const fieldsService = new FieldsService({ |
| 673 | knex: trx, |
| 674 | accountability: this.accountability, |
| 675 | schema: this.schema, |
| 676 | }); |
| 677 | |
| 678 | const fieldItemsService = new ItemsService('directus_fields', { |
| 679 | knex: trx, |
| 680 | accountability: this.accountability, |
| 681 | schema: this.schema, |
| 682 | }); |
| 683 | |
| 684 | await fieldItemsService.deleteByQuery( |
| 685 | { |
| 686 | filter: { |
| 687 | collection: { _eq: collectionKey }, |
| 688 | }, |
| 689 | }, |
| 690 | { |
| 691 | bypassEmitAction: (params) => |
no test coverage detected