From fe8476c5dd390b74ed8f49ddf10fff387472b7d3 Mon Sep 17 00:00:00 2001 From: chenos Date: Mon, 21 Dec 2020 10:44:13 +0800 Subject: [PATCH] fix: after the field is updated, reload the collection configuration --- .../src/hooks/fields-after-update.ts | 16 ++++++++++++++++ packages/plugin-collections/src/hooks/index.ts | 2 ++ 2 files changed, 18 insertions(+) create mode 100644 packages/plugin-collections/src/hooks/fields-after-update.ts diff --git a/packages/plugin-collections/src/hooks/fields-after-update.ts b/packages/plugin-collections/src/hooks/fields-after-update.ts new file mode 100644 index 000000000..e177ef205 --- /dev/null +++ b/packages/plugin-collections/src/hooks/fields-after-update.ts @@ -0,0 +1,16 @@ +import FieldModel from '../models/field'; + +export default async function (model: FieldModel, options: any = {}) { + const { migrate = true } = options; + if (migrate) { + await model.migrate(options); + } + if (model.get('collection_name') && model.get('parent_id')) { + const parent = await model.getParent({ + ...options, + }); + const Collection = model.database.getModel('collections'); + console.log('fields-after-update', parent.get('collection_name')); + await Collection.load({...options, where: {name: parent.get('collection_name')}}); + } +} diff --git a/packages/plugin-collections/src/hooks/index.ts b/packages/plugin-collections/src/hooks/index.ts index f5ce259a2..7bec49083 100644 --- a/packages/plugin-collections/src/hooks/index.ts +++ b/packages/plugin-collections/src/hooks/index.ts @@ -5,6 +5,7 @@ import collectionsAfterUpdate from './collections-after-update'; import fieldsBeforeValidate from './fields-before-validate'; import fieldsAfterCreate from './fields-after-create'; import fieldsAfterBulkUpdate from './fields-after-bulk-update'; +import fieldsAfterUpdate from './fields-after-update'; import generateName from './generateName'; @@ -17,6 +18,7 @@ export default { fields: { beforeValidate: fieldsBeforeValidate, afterCreate: fieldsAfterCreate, + afterUpdate: fieldsAfterUpdate, afterBulkUpdate: fieldsAfterBulkUpdate, }, actions: {