mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-16 01:24:05 -08:00
88dea330b9
* ⚡ Update `lintfix` script * ⚡ Run baseline `lintfix` * 🔥 Remove unneeded exceptions (#3538) * 🔥 Remove exceptions for `node-param-default-wrong-for-simplify` * 🔥 Remove exceptions for `node-param-placeholder-miscased-id` * ⚡ Update version * 👕 Apply `node-param-placeholder-missing` (#3542) * 👕 Apply `filesystem-wrong-cred-filename` (#3543) * 👕 Apply `node-param-description-missing-from-dynamic-options` (#3545) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-class-description-empty-string` (#3546) * 👕 Apply `node-class-description-icon-not-svg` (#3548) * 👕 Apply `filesystem-wrong-node-filename` (#3549) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Expand lintings to credentials (#3550) * 👕 Apply `node-param-multi-options-type-unsorted-items` (#3552) * ⚡ fix * ⚡ Minor fixes Co-authored-by: Michael Kret <michael.k@radency.com> * 👕 Apply `node-param-description-wrong-for-dynamic-multi-options` (#3541) * ⚡ Add new lint rule, node-param-description-wrong-for-dynamic-multi-options * ⚡ Fix with updated linting rules * ⚡ Minor fixes Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply `node-param-description-boolean-without-whether` (#3553) * ⚡ fix * Update packages/nodes-base/nodes/Clockify/ProjectDescription.ts Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * 👕 Apply node-param-display-name-wrong-for-dynamic-multi-options (#3537) * 👕 Add exceptions * 👕 Add exception * ✏️ Alphabetize rules * ⚡ Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
106 lines
2.8 KiB
TypeScript
106 lines
2.8 KiB
TypeScript
import { IExecuteFunctions } from 'n8n-core';
|
|
import {
|
|
IDataObject,
|
|
INodeExecutionData,
|
|
INodeType,
|
|
INodeTypeDescription,
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
export class SplitInBatches implements INodeType {
|
|
description: INodeTypeDescription = {
|
|
displayName: 'Split In Batches',
|
|
name: 'splitInBatches',
|
|
icon: 'fa:th-large',
|
|
group: ['organization'],
|
|
version: 1,
|
|
description: 'Split data into batches and iterate over each batch',
|
|
defaults: {
|
|
name: 'SplitInBatches',
|
|
color: '#007755',
|
|
},
|
|
inputs: ['main'],
|
|
outputs: ['main'],
|
|
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: 10,
|
|
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, {}) as IDataObject;
|
|
|
|
if (nodeContext.items === undefined || options.reset === true) {
|
|
// Is the first time the node runs
|
|
|
|
nodeContext.currentRunIndex = 0;
|
|
nodeContext.maxRunIndex = Math.ceil(items.length / batchSize);
|
|
|
|
// Get the items which should be returned
|
|
returnItems.push.apply(returnItems, items.splice(0, batchSize));
|
|
|
|
// Set the other items to be saved in the context to return at later runs
|
|
nodeContext.items = items;
|
|
} else {
|
|
// The node has been called before. So return the next batch of items.
|
|
nodeContext.currentRunIndex += 1;
|
|
returnItems.push.apply(returnItems, nodeContext.items.splice(0, batchSize));
|
|
}
|
|
|
|
nodeContext.noItemsLeft = nodeContext.items.length === 0;
|
|
|
|
if (returnItems.length === 0) {
|
|
// No data left to return so stop execution of the branch
|
|
return null;
|
|
}
|
|
|
|
returnItems.map((item, index) => {
|
|
item.pairedItem = {
|
|
item: index,
|
|
};
|
|
});
|
|
|
|
return this.prepareOutputData(returnItems);
|
|
}
|
|
}
|