mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
fix(Split In Batches Node): Fix issue with pairedItem (#4873)
fix(SplitInBatches Node): Fix issue with pairedItem
This commit is contained in:
parent
19e0e96271
commit
38d7300d2a
|
@ -39,6 +39,7 @@ import {
|
||||||
IOAuth2Options,
|
IOAuth2Options,
|
||||||
IPollFunctions,
|
IPollFunctions,
|
||||||
IRunExecutionData,
|
IRunExecutionData,
|
||||||
|
ISourceData,
|
||||||
ITaskDataConnections,
|
ITaskDataConnections,
|
||||||
ITriggerFunctions,
|
ITriggerFunctions,
|
||||||
IWebhookData,
|
IWebhookData,
|
||||||
|
@ -2320,6 +2321,13 @@ export function getExecuteFunctions(
|
||||||
|
|
||||||
return inputData[inputName][inputIndex] as INodeExecutionData[];
|
return inputData[inputName][inputIndex] as INodeExecutionData[];
|
||||||
},
|
},
|
||||||
|
getInputSourceData: (inputIndex = 0, inputName = 'main') => {
|
||||||
|
if (executeData?.source === null) {
|
||||||
|
// Should never happen as n8n sets it automatically
|
||||||
|
throw new Error('Source data is missing!');
|
||||||
|
}
|
||||||
|
return executeData.source[inputName][inputIndex];
|
||||||
|
},
|
||||||
getNodeParameter: (
|
getNodeParameter: (
|
||||||
parameterName: string,
|
parameterName: string,
|
||||||
itemIndex: number,
|
itemIndex: number,
|
||||||
|
@ -2580,6 +2588,13 @@ export function getExecuteSingleFunctions(
|
||||||
|
|
||||||
return allItems[itemIndex];
|
return allItems[itemIndex];
|
||||||
},
|
},
|
||||||
|
getInputSourceData: (inputIndex = 0, inputName = 'main') => {
|
||||||
|
if (executeData?.source === null) {
|
||||||
|
// Should never happen as n8n sets it automatically
|
||||||
|
throw new Error('Source data is missing!');
|
||||||
|
}
|
||||||
|
return executeData.source[inputName][inputIndex] as ISourceData;
|
||||||
|
},
|
||||||
getItemIndex() {
|
getItemIndex() {
|
||||||
return itemIndex;
|
return itemIndex;
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,5 +1,11 @@
|
||||||
import { IExecuteFunctions } from 'n8n-core';
|
import { IExecuteFunctions } from 'n8n-core';
|
||||||
import { INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow';
|
import {
|
||||||
|
deepCopy,
|
||||||
|
INodeExecutionData,
|
||||||
|
INodeType,
|
||||||
|
INodeTypeDescription,
|
||||||
|
IPairedItemData,
|
||||||
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
export class SplitInBatches implements INodeType {
|
export class SplitInBatches implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
|
@ -69,18 +75,56 @@ export class SplitInBatches implements INodeType {
|
||||||
if (nodeContext.items === undefined || options.reset === true) {
|
if (nodeContext.items === undefined || options.reset === true) {
|
||||||
// Is the first time the node runs
|
// Is the first time the node runs
|
||||||
|
|
||||||
|
const sourceData = this.getInputSourceData();
|
||||||
|
|
||||||
nodeContext.currentRunIndex = 0;
|
nodeContext.currentRunIndex = 0;
|
||||||
nodeContext.maxRunIndex = Math.ceil(items.length / batchSize);
|
nodeContext.maxRunIndex = Math.ceil(items.length / batchSize);
|
||||||
|
nodeContext.sourceData = deepCopy(sourceData);
|
||||||
|
|
||||||
// Get the items which should be returned
|
// Get the items which should be returned
|
||||||
returnItems.push.apply(returnItems, items.splice(0, batchSize));
|
returnItems.push.apply(returnItems, items.splice(0, batchSize));
|
||||||
|
|
||||||
// Set the other items to be saved in the context to return at later runs
|
// Set the other items to be saved in the context to return at later runs
|
||||||
nodeContext.items = items;
|
nodeContext.items = [...items];
|
||||||
} else {
|
} else {
|
||||||
// The node has been called before. So return the next batch of items.
|
// The node has been called before. So return the next batch of items.
|
||||||
nodeContext.currentRunIndex += 1;
|
nodeContext.currentRunIndex += 1;
|
||||||
returnItems.push.apply(returnItems, nodeContext.items.splice(0, batchSize));
|
returnItems.push.apply(returnItems, nodeContext.items.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
returnItems.map((item) => {
|
||||||
|
item.pairedItem = getPairedItemInformation(item);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeContext.noItemsLeft = nodeContext.items.length === 0;
|
nodeContext.noItemsLeft = nodeContext.items.length === 0;
|
||||||
|
@ -90,12 +134,6 @@ export class SplitInBatches implements INodeType {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
returnItems.map((item, index) => {
|
return [returnItems];
|
||||||
item.pairedItem = {
|
|
||||||
item: index,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
return this.prepareOutputData(returnItems);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -619,6 +619,7 @@ export type IExecuteFunctions = ExecuteFunctions.GetNodeParameterFn & {
|
||||||
getContext(type: string): IContextObject;
|
getContext(type: string): IContextObject;
|
||||||
getCredentials(type: string, itemIndex?: number): Promise<ICredentialDataDecryptedObject>;
|
getCredentials(type: string, itemIndex?: number): Promise<ICredentialDataDecryptedObject>;
|
||||||
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[];
|
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData[];
|
||||||
|
getInputSourceData(inputIndex?: number, inputName?: string): ISourceData;
|
||||||
getMode(): WorkflowExecuteMode;
|
getMode(): WorkflowExecuteMode;
|
||||||
getNode(): INode;
|
getNode(): INode;
|
||||||
getWorkflowDataProxy(itemIndex: number): IWorkflowDataProxyData;
|
getWorkflowDataProxy(itemIndex: number): IWorkflowDataProxyData;
|
||||||
|
@ -654,6 +655,7 @@ export interface IExecuteSingleFunctions {
|
||||||
getContext(type: string): IContextObject;
|
getContext(type: string): IContextObject;
|
||||||
getCredentials(type: string): Promise<ICredentialDataDecryptedObject>;
|
getCredentials(type: string): Promise<ICredentialDataDecryptedObject>;
|
||||||
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData;
|
getInputData(inputIndex?: number, inputName?: string): INodeExecutionData;
|
||||||
|
getInputSourceData(inputIndex?: number, inputName?: string): ISourceData;
|
||||||
getItemIndex(): number;
|
getItemIndex(): number;
|
||||||
getMode(): WorkflowExecuteMode;
|
getMode(): WorkflowExecuteMode;
|
||||||
getNode(): INode;
|
getNode(): INode;
|
||||||
|
@ -926,6 +928,7 @@ export interface IBinaryKeyData {
|
||||||
export interface IPairedItemData {
|
export interface IPairedItemData {
|
||||||
item: number;
|
item: number;
|
||||||
input?: number; // If undefined "0" gets used
|
input?: number; // If undefined "0" gets used
|
||||||
|
sourceOverwrite?: ISourceData;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface INodeExecutionData {
|
export interface INodeExecutionData {
|
||||||
|
|
|
@ -710,6 +710,10 @@ export class WorkflowDataProxy {
|
||||||
|
|
||||||
let sourceData: ISourceData | null = incomingSourceData;
|
let sourceData: ISourceData | null = incomingSourceData;
|
||||||
|
|
||||||
|
if (pairedItem.sourceOverwrite) {
|
||||||
|
sourceData = pairedItem.sourceOverwrite;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof pairedItem === 'number') {
|
if (typeof pairedItem === 'number') {
|
||||||
pairedItem = {
|
pairedItem = {
|
||||||
item: pairedItem,
|
item: pairedItem,
|
||||||
|
@ -871,6 +875,10 @@ export class WorkflowDataProxy {
|
||||||
|
|
||||||
nodeBeforeLast = sourceData.previousNode;
|
nodeBeforeLast = sourceData.previousNode;
|
||||||
sourceData = taskData.source[pairedItem.input || 0] || null;
|
sourceData = taskData.source[pairedItem.input || 0] || null;
|
||||||
|
|
||||||
|
if (pairedItem.sourceOverwrite) {
|
||||||
|
sourceData = pairedItem.sourceOverwrite;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sourceData === null) {
|
if (sourceData === null) {
|
||||||
|
|
Loading…
Reference in a new issue