fix: make default view/tab cannot be destroyed (#63)

* fix: make default view/tab cannot be destroyed

* refactor: return count for destroy action
This commit is contained in:
Junyi 2021-02-08 15:13:07 +08:00 committed by GitHub
parent eb5581646c
commit cef64bb7f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 5 deletions

View File

@ -16,7 +16,7 @@ describe('destroy', () => {
const response = await agent const response = await agent
.delete(`/posts/${post.id}`); .delete(`/posts/${post.id}`);
// console.log(response.body); // console.log(response.body);
expect(response.body).toBe(post.id); expect(response.body.count).toBe(1);
}); });
it('batch delete by filter', async () => { it('batch delete by filter', async () => {

View File

@ -442,6 +442,7 @@ export async function destroy(ctx: Context, next: Next) {
} = ctx.action.params; } = ctx.action.params;
const transaction = await ctx.db.sequelize.transaction(); const transaction = await ctx.db.sequelize.transaction();
const commonOptions = { transaction, context: ctx }; const commonOptions = { transaction, context: ctx };
let count;
if (associated && resourceField) { if (associated && resourceField) {
const AssociatedModel = ctx.db.getModel(associatedName); const AssociatedModel = ctx.db.getModel(associatedName);
if (!(associated instanceof AssociatedModel)) { if (!(associated instanceof AssociatedModel)) {
@ -456,7 +457,7 @@ export async function destroy(ctx: Context, next: Next) {
// TODO不能程序上解除关系直接通过 onDelete 触发,或者通过 afterDestroy 处理 // TODO不能程序上解除关系直接通过 onDelete 触发,或者通过 afterDestroy 处理
// await associated[setAccessor](null, commonOptions); // await associated[setAccessor](null, commonOptions);
// @ts-ignore // @ts-ignore
ctx.body = await model.destroy(commonOptions); count = await model.destroy(commonOptions);
} else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) { } else if (resourceField instanceof HASMANY || resourceField instanceof BELONGSTOMANY) {
const primaryKey = resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute; const primaryKey = resourceKeyAttribute || resourceField.options.targetKey || TargetModel.primaryKeyAttribute;
const models: Model[] = await associated[getAccessor]({ const models: Model[] = await associated[getAccessor]({
@ -466,7 +467,7 @@ export async function destroy(ctx: Context, next: Next) {
// TODO不能程序上解除关系直接通过 onDelete 触发,或者通过 afterDestroy 处理 // TODO不能程序上解除关系直接通过 onDelete 触发,或者通过 afterDestroy 处理
// await associated[removeAccessor](models, commonOptions); // await associated[removeAccessor](models, commonOptions);
// @ts-ignore // @ts-ignore
ctx.body = await TargetModel.destroy({ count = await TargetModel.destroy({
where: { [primaryKey]: { [Op.in]: models.map(item => item[primaryKey]) } }, where: { [primaryKey]: { [Op.in]: models.map(item => item[primaryKey]) } },
...commonOptions, ...commonOptions,
individualHooks: true, individualHooks: true,
@ -476,14 +477,14 @@ export async function destroy(ctx: Context, next: Next) {
const Model = ctx.db.getModel(resourceName); const Model = ctx.db.getModel(resourceName);
const { where } = Model.parseApiJson({ filter, context: ctx }); const { where } = Model.parseApiJson({ filter, context: ctx });
const primaryKey = resourceKeyAttribute || Model.primaryKeyAttribute; const primaryKey = resourceKeyAttribute || Model.primaryKeyAttribute;
const data = await Model.destroy({ count = await Model.destroy({
where: resourceKey ? { [primaryKey]: resourceKey } : where, where: resourceKey ? { [primaryKey]: resourceKey } : where,
// @ts-ignore hooks 里添加 context // @ts-ignore hooks 里添加 context
...commonOptions, ...commonOptions,
individualHooks: true, individualHooks: true,
}); });
ctx.body = data;
} }
ctx.body = { count };
await transaction.commit(); await transaction.commit();
await next(); await next();
} }

View File

@ -274,6 +274,11 @@ export default {
list: { list: {
sort: 'sort', sort: 'sort',
}, },
destroy: {
filter: {
default: false
}
}
}, },
component: { component: {
type: 'drawerSelect', type: 'drawerSelect',
@ -290,6 +295,11 @@ export default {
list: { list: {
sort: 'sort', sort: 'sort',
}, },
destroy: {
filter: {
default: false
}
}
}, },
component: { component: {
type: 'drawerSelect', type: 'drawerSelect',

View File

@ -349,6 +349,9 @@ export default {
type: 'destroy', type: 'destroy',
name: 'destroy', name: 'destroy',
title: '删除', title: '删除',
filter: {
default: false
}
}, },
{ {
type: 'create', type: 'create',

View File

@ -353,6 +353,9 @@ export default {
type: 'destroy', type: 'destroy',
name: 'destroy', name: 'destroy',
title: '删除', title: '删除',
filter: {
default: false
}
}, },
{ {
type: 'create', type: 'create',