mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Flows improvements (#16773)
This commit is contained in:
@@ -5,28 +5,58 @@ import { getFlowManager } from '../../flows';
|
||||
type Options = {
|
||||
flow: string;
|
||||
payload?: Record<string, any> | Record<string, any>[] | string | null;
|
||||
iterationMode?: 'serial' | 'batch' | 'parallel';
|
||||
batchSize?: number;
|
||||
};
|
||||
|
||||
export default defineOperationApi<Options>({
|
||||
id: 'trigger',
|
||||
|
||||
handler: async ({ flow, payload }, context) => {
|
||||
handler: async ({ flow, payload, iterationMode, batchSize }, context) => {
|
||||
const flowManager = getFlowManager();
|
||||
|
||||
const payloadObject = optionToObject(payload) ?? null;
|
||||
|
||||
let result: unknown | unknown[];
|
||||
|
||||
if (Array.isArray(payloadObject)) {
|
||||
result = await Promise.all(
|
||||
payloadObject.map((payload) => {
|
||||
return flowManager.runOperationFlow(flow, payload, omit(context, 'data'));
|
||||
})
|
||||
);
|
||||
} else {
|
||||
result = await flowManager.runOperationFlow(flow, payloadObject, omit(context, 'data'));
|
||||
if (iterationMode === 'serial') {
|
||||
const result = [];
|
||||
|
||||
for (const payload of payloadObject) {
|
||||
result.push(await flowManager.runOperationFlow(flow, payload, omit(context, 'data')));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (iterationMode === 'batch') {
|
||||
const size = batchSize ?? 10;
|
||||
|
||||
const result = [];
|
||||
|
||||
for (let i = 0; i < payloadObject.length; i += size) {
|
||||
const batch = payloadObject.slice(i, i + size);
|
||||
|
||||
const batchResults = await Promise.all(
|
||||
batch.map((payload) => {
|
||||
return flowManager.runOperationFlow(flow, payload, omit(context, 'data'));
|
||||
})
|
||||
);
|
||||
|
||||
result.push(...batchResults);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
if (iterationMode === 'parallel' || !iterationMode) {
|
||||
return await Promise.all(
|
||||
payloadObject.map((payload) => {
|
||||
return flowManager.runOperationFlow(flow, payload, omit(context, 'data'));
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return await flowManager.runOperationFlow(flow, payloadObject, omit(context, 'data'));
|
||||
},
|
||||
});
|
||||
|
||||
@@ -2167,3 +2167,9 @@ operations:
|
||||
flows: Flows
|
||||
flow: Flow
|
||||
data: Data
|
||||
iteration_mode: Iteration Mode
|
||||
iteration_mode_note: Mode for iterating over arrays.
|
||||
serial: Serial
|
||||
parallel: Parallel
|
||||
batch: Batch
|
||||
batch_size: Batch Size
|
||||
|
||||
@@ -12,7 +12,7 @@ export default defineOperationApp({
|
||||
text: flow,
|
||||
},
|
||||
],
|
||||
options: () => {
|
||||
options: (panel) => {
|
||||
const flowStore = useFlowsStore();
|
||||
const flowChoices = flowStore.flows
|
||||
.filter((flow) => flow.trigger === 'operation')
|
||||
@@ -25,7 +25,7 @@ export default defineOperationApp({
|
||||
name: '$t:operations.trigger.flow',
|
||||
type: 'string',
|
||||
meta: {
|
||||
width: 'full',
|
||||
width: 'half',
|
||||
interface: 'select-dropdown',
|
||||
options: {
|
||||
choices: flowChoices,
|
||||
@@ -34,6 +34,51 @@ export default defineOperationApp({
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'iterationMode',
|
||||
name: '$t:operations.trigger.iteration_mode',
|
||||
type: 'json',
|
||||
meta: {
|
||||
width: 'half',
|
||||
interface: 'select-dropdown',
|
||||
note: '$t:operations.trigger.iteration_mode_note',
|
||||
options: {
|
||||
choices: [
|
||||
{
|
||||
text: '$t:operations.trigger.parallel',
|
||||
value: 'parallel',
|
||||
},
|
||||
{
|
||||
text: '$t:operations.trigger.serial',
|
||||
value: 'serial',
|
||||
},
|
||||
{
|
||||
text: '$t:operations.trigger.batch',
|
||||
value: 'batch',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
schema: {
|
||||
default_value: 'parallel',
|
||||
},
|
||||
},
|
||||
...(panel.iterationMode === 'batch'
|
||||
? [
|
||||
{
|
||||
field: 'batchSize',
|
||||
name: '$t:operations.trigger.batch_size',
|
||||
type: 'integer',
|
||||
meta: {
|
||||
width: 'half',
|
||||
interface: 'input',
|
||||
},
|
||||
schema: {
|
||||
default_value: 10,
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
{
|
||||
field: 'payload',
|
||||
name: '$t:payload',
|
||||
|
||||
Reference in New Issue
Block a user