From cef64bb7f5a329603ff2e14857992a1d95d75328 Mon Sep 17 00:00:00 2001 From: Junyi Date: Mon, 8 Feb 2021 15:13:07 +0800 Subject: [PATCH] fix: make default view/tab cannot be destroyed (#63) * fix: make default view/tab cannot be destroyed * refactor: return count for destroy action --- packages/actions/src/__tests__/destroy.test.ts | 2 +- packages/actions/src/actions/common.ts | 9 +++++---- .../plugin-collections/src/collections/collections.ts | 10 ++++++++++ packages/plugin-collections/src/collections/tabs.ts | 3 +++ packages/plugin-collections/src/collections/views.ts | 3 +++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/packages/actions/src/__tests__/destroy.test.ts b/packages/actions/src/__tests__/destroy.test.ts index aee4db3b6b..61e6dcaede 100644 --- a/packages/actions/src/__tests__/destroy.test.ts +++ b/packages/actions/src/__tests__/destroy.test.ts @@ -16,7 +16,7 @@ describe('destroy', () => { const response = await agent .delete(`/posts/${post.id}`); // console.log(response.body); - expect(response.body).toBe(post.id); + expect(response.body.count).toBe(1); }); it('batch delete by filter', async () => { diff --git a/packages/actions/src/actions/common.ts b/packages/actions/src/actions/common.ts index 511e644592..747bee9ebe 100644 --- a/packages/actions/src/actions/common.ts +++ b/packages/actions/src/actions/common.ts @@ -442,6 +442,7 @@ export async function destroy(ctx: Context, next: Next) { } = ctx.action.params; const transaction = await ctx.db.sequelize.transaction(); const commonOptions = { transaction, context: ctx }; + let count; if (associated && resourceField) { const AssociatedModel = ctx.db.getModel(associatedName); if (!(associated instanceof AssociatedModel)) { @@ -456,7 +457,7 @@ export async function destroy(ctx: Context, next: Next) { // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理 // await associated[setAccessor](null, commonOptions); // @ts-ignore - ctx.body = await model.destroy(commonOptions); + count = await model.destroy(commonOptions); } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) { const primaryKey = resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute; const models: Model[] = await associated[getAccessor]({ @@ -466,7 +467,7 @@ export async function destroy(ctx: Context, next: Next) { // TODO:不能程序上解除关系,直接通过 onDelete 触发,或者通过 afterDestroy 处理 // await associated[removeAccessor](models, commonOptions); // @ts-ignore - ctx.body = await TargetModel.destroy({ + count = await TargetModel.destroy({ where: { [primaryKey]: { [Op.in]: models.map(item => item[primaryKey]) } }, ...commonOptions, individualHooks: true, @@ -476,14 +477,14 @@ export async function destroy(ctx: Context, next: Next) { const Model = ctx.db.getModel(resourceName); const { where } = Model.parseApiJson({ filter, context: ctx }); const primaryKey = resourceKeyAttribute || Model.primaryKeyAttribute; - const data = await Model.destroy({ + count = await Model.destroy({ where: resourceKey ? { [primaryKey]: resourceKey } : where, // @ts-ignore hooks 里添加 context ...commonOptions, individualHooks: true, }); - ctx.body = data; } + ctx.body = { count }; await transaction.commit(); await next(); } diff --git a/packages/plugin-collections/src/collections/collections.ts b/packages/plugin-collections/src/collections/collections.ts index e258890afe..80425b057b 100644 --- a/packages/plugin-collections/src/collections/collections.ts +++ b/packages/plugin-collections/src/collections/collections.ts @@ -274,6 +274,11 @@ export default { list: { sort: 'sort', }, + destroy: { + filter: { + default: false + } + } }, component: { type: 'drawerSelect', @@ -290,6 +295,11 @@ export default { list: { sort: 'sort', }, + destroy: { + filter: { + default: false + } + } }, component: { type: 'drawerSelect', diff --git a/packages/plugin-collections/src/collections/tabs.ts b/packages/plugin-collections/src/collections/tabs.ts index d46599516d..4298a4d2a5 100644 --- a/packages/plugin-collections/src/collections/tabs.ts +++ b/packages/plugin-collections/src/collections/tabs.ts @@ -349,6 +349,9 @@ export default { type: 'destroy', name: 'destroy', title: '删除', + filter: { + default: false + } }, { type: 'create', diff --git a/packages/plugin-collections/src/collections/views.ts b/packages/plugin-collections/src/collections/views.ts index cf6b2d4b3e..49e77a7792 100644 --- a/packages/plugin-collections/src/collections/views.ts +++ b/packages/plugin-collections/src/collections/views.ts @@ -353,6 +353,9 @@ export default { type: 'destroy', name: 'destroy', title: '删除', + filter: { + default: false + } }, { type: 'create',