mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 01:56:46 +00:00
feat(Loop Over Items (Split in Batches) Node): Automatically add a loop + rename (#7228)
Github issue / Community forum post (link here to close automatically): --------- Co-authored-by: Michael Kret <michael.k@radency.com>
This commit is contained in:
@@ -22,7 +22,7 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"alias": ["Loop", "concatenate"],
|
||||
"alias": ["Loop", "Concatenate", "Batch", "Split", "Split In Batches"],
|
||||
"subcategories": {
|
||||
"Core Nodes": ["Flow"]
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { VersionedNodeType } from 'n8n-workflow';
|
||||
|
||||
import { SplitInBatchesV1 } from './v1/SplitInBatchesV1.node';
|
||||
import { SplitInBatchesV2 } from './v2/SplitInBatchesV2.node';
|
||||
import { SplitInBatchesV3 } from './v3/SplitInBatchesV3.node';
|
||||
|
||||
export class SplitInBatches extends VersionedNodeType {
|
||||
constructor() {
|
||||
@@ -12,12 +13,13 @@ export class SplitInBatches extends VersionedNodeType {
|
||||
icon: 'fa:th-large',
|
||||
group: ['organization'],
|
||||
description: 'Split data into batches and iterate over each batch',
|
||||
defaultVersion: 2,
|
||||
defaultVersion: 3,
|
||||
};
|
||||
|
||||
const nodeVersions: IVersionedNodeType['nodeVersions'] = {
|
||||
1: new SplitInBatchesV1(),
|
||||
2: new SplitInBatchesV2(),
|
||||
3: new SplitInBatchesV3(),
|
||||
};
|
||||
|
||||
super(nodeVersions, baseDescription);
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
IPairedItemData,
|
||||
} from 'n8n-workflow';
|
||||
import { deepCopy } from 'n8n-workflow';
|
||||
|
||||
export class SplitInBatchesV3 implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Loop Over Items (Split in Batches)',
|
||||
name: 'splitInBatches',
|
||||
icon: 'fa:sync',
|
||||
group: ['organization'],
|
||||
version: 3,
|
||||
description: 'Split data into batches and iterate over each batch',
|
||||
defaults: {
|
||||
name: 'Loop Over Items',
|
||||
color: '#007755',
|
||||
},
|
||||
inputs: ['main'],
|
||||
// eslint-disable-next-line n8n-nodes-base/node-class-description-outputs-wrong
|
||||
outputs: ['main', 'main'],
|
||||
outputNames: ['done', 'loop'],
|
||||
properties: [
|
||||
{
|
||||
displayName:
|
||||
'You may not need this node — n8n nodes automatically run once for each input item. <a href="https://docs.n8n.io/getting-started/key-concepts/looping.html#using-loops-in-n8n" target="_blank">More info</a>',
|
||||
name: 'splitInBatchesNotice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Batch Size',
|
||||
name: 'batchSize',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 1,
|
||||
description: 'The number of items to return with each call',
|
||||
},
|
||||
{
|
||||
displayName: 'Options',
|
||||
name: 'options',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Option',
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Reset',
|
||||
name: 'reset',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description:
|
||||
'Whether the node will be reset and so with the current input-data newly initialized',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][] | null> {
|
||||
// Get the input data and create a new array so that we can remove
|
||||
// items without a problem
|
||||
const items = this.getInputData().slice();
|
||||
|
||||
const nodeContext = this.getContext('node');
|
||||
|
||||
const batchSize = this.getNodeParameter('batchSize', 0) as number;
|
||||
|
||||
const returnItems: INodeExecutionData[] = [];
|
||||
|
||||
const options = this.getNodeParameter('options', 0, {});
|
||||
|
||||
if (nodeContext.items === undefined || options.reset === true) {
|
||||
// Is the first time the node runs
|
||||
|
||||
const sourceData = this.getInputSourceData();
|
||||
|
||||
nodeContext.currentRunIndex = 0;
|
||||
nodeContext.maxRunIndex = Math.ceil(items.length / batchSize);
|
||||
nodeContext.sourceData = deepCopy(sourceData);
|
||||
|
||||
// Get the items which should be returned
|
||||
returnItems.push.apply(returnItems, items.splice(0, batchSize));
|
||||
|
||||
// Save the incoming items to be able to return them for later runs
|
||||
nodeContext.items = [...items];
|
||||
|
||||
// Reset processedItems as they get only added starting from the first iteration
|
||||
nodeContext.processedItems = [];
|
||||
} else {
|
||||
// The node has been called before. So return the next batch of items.
|
||||
nodeContext.currentRunIndex += 1;
|
||||
returnItems.push.apply(
|
||||
returnItems,
|
||||
(nodeContext.items as INodeExecutionData[]).splice(0, batchSize),
|
||||
);
|
||||
|
||||
const addSourceOverwrite = (pairedItem: IPairedItemData | number): IPairedItemData => {
|
||||
if (typeof pairedItem === 'number') {
|
||||
return {
|
||||
item: pairedItem,
|
||||
sourceOverwrite: nodeContext.sourceData,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...pairedItem,
|
||||
sourceOverwrite: nodeContext.sourceData,
|
||||
};
|
||||
};
|
||||
|
||||
function getPairedItemInformation(
|
||||
item: INodeExecutionData,
|
||||
): IPairedItemData | IPairedItemData[] {
|
||||
if (item.pairedItem === undefined) {
|
||||
return {
|
||||
item: 0,
|
||||
sourceOverwrite: nodeContext.sourceData,
|
||||
};
|
||||
}
|
||||
|
||||
if (Array.isArray(item.pairedItem)) {
|
||||
return item.pairedItem.map(addSourceOverwrite);
|
||||
}
|
||||
|
||||
return addSourceOverwrite(item.pairedItem);
|
||||
}
|
||||
|
||||
const sourceOverwrite = this.getInputSourceData();
|
||||
|
||||
const newItems = items.map((item, index) => {
|
||||
return {
|
||||
...item,
|
||||
pairedItem: {
|
||||
sourceOverwrite,
|
||||
item: index,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
nodeContext.processedItems = [...nodeContext.processedItems, ...newItems];
|
||||
|
||||
returnItems.map((item) => {
|
||||
item.pairedItem = getPairedItemInformation(item);
|
||||
});
|
||||
}
|
||||
|
||||
nodeContext.noItemsLeft = nodeContext.items.length === 0;
|
||||
|
||||
if (returnItems.length === 0) {
|
||||
nodeContext.done = true;
|
||||
return [nodeContext.processedItems, []];
|
||||
}
|
||||
|
||||
nodeContext.done = false;
|
||||
|
||||
return [[], returnItems];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user