feat(plugin-workflow): add failOnEmpty option for query node (#1599)

This commit is contained in:
Junyi 2023-03-25 13:15:03 +07:00 committed by GitHub
parent cc50b38a12
commit d8e8f5ec45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 3 deletions

View File

@ -134,6 +134,7 @@ export default {
'Update record': '更新数据',
'Query record': '查询数据',
'Multiple records': '多条数据',
'Fail on no data': '无数据时报错',
'Please select collection first': '请先选择数据表',
'Only update records matching conditions': '只更新满足条件的数据',
'Please add at least one condition': '请添加至少一个条件',

View File

@ -16,10 +16,9 @@ export default {
group: 'collection',
fieldset: {
collection,
// 'config.multiple': {
// multiple: {
// type: 'boolean',
// title: `{{t("Multiple records", { ns: "${NAMESPACE}" })}}`,
// name: 'config.multiple',
// 'x-decorator': 'FormItem',
// 'x-component': 'Checkbox',
// 'x-component-props': {
@ -32,6 +31,12 @@ export default {
filter,
appends
}
},
failOnEmpty: {
type: 'boolean',
title: `{{t("Fail on no data", { ns: "${NAMESPACE}" })}}`,
'x-decorator': 'FormItem',
'x-component': 'Checkbox',
}
},
view: {

View File

@ -1,6 +1,7 @@
import { Application } from '@nocobase/server';
import Database from '@nocobase/database';
import { getApp, sleep } from '..';
import { EXECUTION_STATUS, JOB_STATUS } from '../../constants';
@ -315,4 +316,46 @@ describe('workflow > instructions > query', () => {
expect(job.result.length).toBe(0);
});
});
describe('failOnEmpty', () => {
it('failOnEmpty', async () => {
const n1 = await workflow.createNode({
type: 'query',
config: {
collection: 'categories',
failOnEmpty: true
}
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toBe(EXECUTION_STATUS.FAILED);
const [job] = await execution.getJobs();
expect(job.status).toBe(JOB_STATUS.FAILED);
expect(job.result).toBe(null);
});
it('failOnEmpty && multiple', async () => {
const n1 = await workflow.createNode({
type: 'query',
config: {
collection: 'categories',
multiple: true,
failOnEmpty: true
}
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const [execution] = await workflow.getExecutions();
expect(execution.status).toBe(EXECUTION_STATUS.FAILED);
const [job] = await execution.getJobs();
expect(job.result).toMatchObject([]);
});
});
});

View File

@ -7,7 +7,8 @@ export default {
const {
collection,
multiple,
params = {}
params = {},
failOnEmpty = false
} = node.config;
const repo = (<typeof FlowNodeModel>node.constructor).database.getRepository(collection);
@ -20,6 +21,13 @@ export default {
transaction: processor.transaction
});
if (failOnEmpty && (multiple ? !result.length : !result)) {
return {
result,
status: JOB_STATUS.FAILED
}
}
// NOTE: `toJSON()` to avoid getting undefined value from Proxied model instance (#380)
// e.g. Object.prototype.hasOwnProperty.call(result, 'id') // false
// so the properties can not be get by json-templates(object-path)