n8n/packages/nodes-base/nodes/Merge/Merge.node.ts

475 lines
13 KiB
TypeScript
Raw Normal View History

import { get } from 'lodash';
2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
GenericValue,
INodeExecutionData,
INodeType,
INodeTypeDescription,
IPairedItemData,
2019-06-23 03:35:23 -07:00
} from 'n8n-workflow';
export class Merge implements INodeType {
description: INodeTypeDescription = {
displayName: 'Merge',
name: 'merge',
icon: 'fa:code-branch',
2019-06-23 03:35:23 -07:00
group: ['transform'],
version: 1,
subtitle: '={{$parameter["mode"]}}',
description: 'Merges data of multiple streams once data from both is available',
2019-06-23 03:35:23 -07:00
defaults: {
name: 'Merge',
color: '#00bbcc',
2019-06-23 03:35:23 -07:00
},
inputs: ['main', 'main'],
outputs: ['main'],
inputNames: ['Input 1', 'Input 2'],
2019-06-23 03:35:23 -07:00
properties: [
{
displayName: 'Mode',
name: 'mode',
type: 'options',
options: [
{
name: 'Append',
value: 'append',
description: 'Combines data of both inputs. The output will contain items of input 1 and input 2.',
2019-06-23 03:35:23 -07:00
},
{
name: 'Keep Key Matches',
value: 'keepKeyMatches',
description: 'Keeps data of input 1 if it does find a match with data of input 2',
},
2019-06-23 03:35:23 -07:00
{
name: 'Merge By Index',
value: 'mergeByIndex',
description: 'Merges data of both inputs. The output will contain items of input 1 merged with data of input 2. Merge happens depending on the index of the items. So first item of input 1 will be merged with first item of input 2 and so on.',
},
{
name: 'Merge By Key',
value: 'mergeByKey',
description: 'Merges data of both inputs. The output will contain items of input 1 merged with data of input 2. Merge happens depending on a defined key.',
},
{
name: 'Multiplex',
value: 'multiplex',
2020-10-22 06:46:03 -07:00
description: 'Merges each value of one input with each value of the other input. The output will contain (m * n) items where (m) and (n) are lengths of the inputs.',
},
{
refactor: Apply more `eslint-plugin-n8n-nodes-base` autofixable rules (#3432) * :zap: Update `lintfix` script * :shirt: Remove unneeded lint exceptions * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-description-miscased-url` (#3441) * :shirt: Apply `rule node-param-placeholder-miscased-id` (#3443) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-option-name-wrong-for-upsert` (#3446) * :shirt: Apply `node-param-min-value-wrong-for-limit` (#3442) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-wrong-for-dynamic-options` (#3454) * :hammer: fix * :zap: Fix `Assigned To` fields Co-authored-by: Michael Kret <michael.k@radency.com> * :shirt: Apply `rule node-param-default-wrong-for-number` (#3453) * :shirt: Apply `node-param-default-wrong-for-string` (#3452) Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * Apply `node-param-display-name-miscased` (#3449) * :hammer: fix * :hammer: exceptions * :zap: review fixes * :shirt: Apply `node-param-description-lowercase-first-char` (#3451) * :zap: fix * :zap: review fixes * :zap: fix Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Apply `node-param-description-wrong-for-dynamic-options` (#3456) * Rule working as intended * Add rule * :fire: Remove repetitions * :shirt: Add exceptions Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :shirt: Small fix for `node-param-description-wrong-for-dynamic-options` * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3460) * :shirt: Apply `node-param-description-line-break-html-tag` (#3462) * :shirt: Run baseline `lintfix` * :shirt: Apply `node-param-options-type-unsorted-items` (#3459) * :zap: fix * :hammer: exceptions * Add exception for Salesmate and Zoom Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> * :zap: Restore `lintfix` command Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: brianinoa <54530642+brianinoa@users.noreply.github.com>
2022-06-03 10:23:49 -07:00
name: 'Pass-Through',
value: 'passThrough',
2019-11-27 06:07:37 -08:00
description: 'Passes through data of one input. The output will contain only items of the defined input.',
},
{
name: 'Remove Key Matches',
value: 'removeKeyMatches',
description: 'Keeps data of input 1 if it does NOT find match with data of input 2',
},
{
name: 'Wait',
value: 'wait',
description: 'Waits till data of both inputs is available and will then output a single empty item. Source Nodes must connect to both Input 1 and 2. This node only supports 2 Sources, if you need more Sources, connect multiple Merge nodes in series. This node will not output any data.',
2019-06-23 03:35:23 -07:00
},
],
default: 'append',
description: 'How data of branches should be merged',
},
{
displayName: 'Join',
name: 'join',
type: 'options',
displayOptions: {
show: {
mode: [
2020-10-22 06:46:03 -07:00
'mergeByIndex',
],
},
},
options: [
{
name: 'Inner Join',
value: 'inner',
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
description: 'Merges as many items as both inputs contain. (Example: Input1 = 5 items, Input2 = 3 items | Output will contain 3 items).',
},
{
name: 'Left Join',
value: 'left',
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
description: 'Merges as many items as first input contains. (Example: Input1 = 3 items, Input2 = 5 items | Output will contain 3 items).',
},
{
name: 'Outer Join',
value: 'outer',
refactor: Apply `eslint-plugin-n8n-nodes-base` autofixable rules (#3174) * :zap: Initial setup * :shirt: Update `.eslintignore` * :shirt: Autofix node-param-default-missing (#3173) * :fire: Remove duplicate key * :shirt: Add exceptions * :package: Update package-lock.json * :shirt: Apply `node-class-description-inputs-wrong-trigger-node` (#3176) * :shirt: Apply `node-class-description-inputs-wrong-regular-node` (#3177) * :shirt: Apply `node-class-description-outputs-wrong` (#3178) * :shirt: Apply `node-execute-block-double-assertion-for-items` (#3179) * :shirt: Apply `node-param-default-wrong-for-collection` (#3180) * :shirt: Apply node-param-default-wrong-for-boolean (#3181) * Autofixed default missing * Autofixed booleans, worked well * :zap: Fix params * :rewind: Undo exempted autofixes * :package: Update package-lock.json * :shirt: Apply node-class-description-missing-subtitle (#3182) * :zap: Fix missing comma * :shirt: Apply `node-param-default-wrong-for-fixed-collection` (#3184) * :shirt: Add exception for `node-class-description-missing-subtitle` * :shirt: Apply `node-param-default-wrong-for-multi-options` (#3185) * :shirt: Apply `node-param-collection-type-unsorted-items` (#3186) * Missing coma * :shirt: Apply `node-param-default-wrong-for-simplify` (#3187) * :shirt: Apply `node-param-description-comma-separated-hyphen` (#3190) * :shirt: Apply `node-param-description-empty-string` (#3189) * :shirt: Apply `node-param-description-excess-inner-whitespace` (#3191) * Rule looks good * Add whitespace rule in eslint config * :zao: fix * :shirt: Apply `node-param-description-identical-to-display-name` (#3193) * :shirt: Apply `node-param-description-missing-for-ignore-ssl-issues` (#3195) * :rewind: Revert ":zao: fix" This reverts commit ef8a76f3dfedffd1bdccf3178af8a8d90cf5a55c. * :shirt: Apply `node-param-description-missing-for-simplify` (#3196) * :shirt: Apply `node-param-description-missing-final-period` (#3194) * Rule working as intended * Add rule to eslint * :shirt: Apply node-param-description-missing-for-return-all (#3197) * :zap: Restore `lintfix` command Co-authored-by: agobrech <45268029+agobrech@users.noreply.github.com> Co-authored-by: Omar Ajoue <krynble@gmail.com> Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: Michael Kret <michael.k@radency.com>
2022-04-22 09:29:51 -07:00
description: 'Merges as many items as input contains with most items. (Example: Input1 = 3 items, Input2 = 5 items | Output will contain 5 items).',
},
],
default: 'left',
description: 'How many items the output will contain if inputs contain different amount of items',
2019-06-23 03:35:23 -07:00
},
{
displayName: 'Property Input 1',
name: 'propertyName1',
type: 'string',
default: '',
hint: 'The name of the field as text (e.g. “id”)',
required: true,
2019-06-23 03:35:23 -07:00
displayOptions: {
show: {
mode: [
'keepKeyMatches',
'mergeByKey',
'removeKeyMatches',
2019-06-23 03:35:23 -07:00
],
},
},
description: 'Name of property which decides which items to merge of input 1',
2019-06-23 03:35:23 -07:00
},
{
displayName: 'Property Input 2',
name: 'propertyName2',
type: 'string',
default: '',
hint: 'The name of the field as text (e.g. “id”)',
required: true,
2019-06-23 03:35:23 -07:00
displayOptions: {
show: {
mode: [
'keepKeyMatches',
'mergeByKey',
'removeKeyMatches',
2019-06-23 03:35:23 -07:00
],
},
},
description: 'Name of property which decides which items to merge of input 2',
2019-06-23 03:35:23 -07:00
},
{
displayName: 'Output Data',
name: 'output',
type: 'options',
displayOptions: {
show: {
mode: [
2020-10-22 06:46:03 -07:00
'passThrough',
],
},
},
options: [
{
name: 'Input 1',
value: 'input1',
},
{
name: 'Input 2',
value: 'input2',
},
],
default: 'input1',
description: 'Defines of which input the data should be used as output of node',
},
2020-01-18 20:01:56 -08:00
{
displayName: 'Overwrite',
name: 'overwrite',
type: 'options',
displayOptions: {
show: {
mode: [
'mergeByKey',
],
},
},
options: [
{
name: 'Always',
value: 'always',
description: 'Always overwrites everything',
2020-01-18 20:01:56 -08:00
},
{
name: 'If Blank',
value: 'blank',
description: 'Overwrites only values of "null", "undefined" or empty string',
2020-01-18 20:01:56 -08:00
},
{
name: 'If Missing',
value: 'undefined',
description: 'Only adds values which do not exist yet',
2020-01-18 20:01:56 -08:00
},
],
default: 'always',
description: 'Select when to overwrite the values from Input1 with values from Input 2',
2020-10-22 06:46:03 -07:00
},
],
2019-06-23 03:35:23 -07:00
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const returnData: INodeExecutionData[] = [];
const mode = this.getNodeParameter('mode', 0) as string;
if (mode === 'append') {
// Simply appends the data
for (let i = 0; i < 2; i++) {
returnData.push.apply(returnData, this.getInputData(i));
}
} else if (mode === 'mergeByIndex') {
// Merges data by index
const join = this.getNodeParameter('join', 0) as string;
const dataInput1 = this.getInputData(0);
const dataInput2 = this.getInputData(1);
if (dataInput1 === undefined || dataInput1.length === 0) {
if (['inner', 'left'].includes(join)) {
// When "inner" or "left" join return empty if first
// input does not contain any items
return [returnData];
}
// For "outer" return data of second input
return [dataInput2];
}
if (dataInput2 === undefined || dataInput2.length === 0) {
if (['left', 'outer'].includes(join)) {
// When "left" or "outer" join return data of first input
return [dataInput1];
}
// For "inner" return empty
return [returnData];
}
// Default "left"
let numEntries = dataInput1.length;
if (join === 'inner') {
numEntries = Math.min(dataInput1.length, dataInput2.length);
} else if (join === 'outer') {
numEntries = Math.max(dataInput1.length, dataInput2.length);
}
let newItem: INodeExecutionData;
for (let i = 0; i < numEntries; i++) {
if (i >= dataInput1.length) {
returnData.push(dataInput2[i]);
continue;
}
if (i >= dataInput2.length) {
returnData.push(dataInput1[i]);
continue;
}
newItem = {
json: {},
pairedItem: [
dataInput1[i].pairedItem as IPairedItemData,
dataInput2[i].pairedItem as IPairedItemData,
],
};
if (dataInput1[i].binary !== undefined) {
newItem.binary = {};
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
Object.assign(newItem.binary, dataInput1[i].binary);
}
// Create also a shallow copy of the json data
Object.assign(newItem.json, dataInput1[i].json);
// Copy json data
for (const key of Object.keys(dataInput2[i].json)) {
newItem.json[key] = dataInput2[i].json[key];
}
// Copy binary data
if (dataInput2[i].binary !== undefined) {
if (newItem.binary === undefined) {
newItem.binary = {};
}
for (const key of Object.keys(dataInput2[i].binary!)) {
newItem.binary[key] = dataInput2[i].binary![key] ?? newItem.binary[key];
}
}
returnData.push(newItem);
}
} else if (mode === 'multiplex') {
const dataInput1 = this.getInputData(0);
const dataInput2 = this.getInputData(1);
if (!dataInput1 || !dataInput2) {
return [returnData];
}
let entry1: INodeExecutionData;
let entry2: INodeExecutionData;
for (entry1 of dataInput1) {
for (entry2 of dataInput2) {
returnData.push({
json: {
...(entry1.json), ...(entry2.json),
},
pairedItem: [
entry1.pairedItem as IPairedItemData,
entry2.pairedItem as IPairedItemData,
],
});
}
}
return [returnData];
} else if (['keepKeyMatches', 'mergeByKey', 'removeKeyMatches'].includes(mode)) {
2019-06-23 03:35:23 -07:00
const dataInput1 = this.getInputData(0);
if (!dataInput1) {
// If it has no input data from first input return nothing
return [returnData];
}
const propertyName1 = this.getNodeParameter('propertyName1', 0) as string;
const propertyName2 = this.getNodeParameter('propertyName2', 0) as string;
2020-01-19 12:24:44 -08:00
const overwrite = this.getNodeParameter('overwrite', 0, 'always') as string;
2019-06-23 03:35:23 -07:00
const dataInput2 = this.getInputData(1);
if (!dataInput2 || !propertyName1 || !propertyName2) {
// Second input does not have any data or the property names are not defined
if (mode === 'keepKeyMatches') {
// For "keepKeyMatches" return nothing
return [returnData];
}
// For "mergeByKey" and "removeKeyMatches" return the data from the first input
2019-06-23 03:35:23 -07:00
return [dataInput1];
}
// Get the data to copy
const copyData: {
[key: string]: INodeExecutionData;
} = {};
let entry: INodeExecutionData;
for (entry of dataInput2) {
const key = get(entry.json, propertyName2);
if (!entry.json || !key) {
2019-06-23 03:35:23 -07:00
// Entry does not have the property so skip it
continue;
}
copyData[key as string] = entry;
2019-06-23 03:35:23 -07:00
}
// Copy data on entries or add matching entries
2019-06-23 03:35:23 -07:00
let referenceValue: GenericValue;
let key: string;
for (entry of dataInput1) {
referenceValue = get(entry.json, propertyName1);
2019-06-23 03:35:23 -07:00
if (referenceValue === undefined) {
// Entry does not have the property
if (mode === 'removeKeyMatches') {
// For "removeKeyMatches" add item
returnData.push(entry);
}
// For "mergeByKey" and "keepKeyMatches" skip item
2019-06-23 03:35:23 -07:00
continue;
}
if (!['string', 'number'].includes(typeof referenceValue)) {
if (referenceValue !== null && referenceValue.constructor.name !== 'Data') {
// Reference value is not of comparable type
if (mode === 'removeKeyMatches') {
// For "removeKeyMatches" add item
returnData.push(entry);
}
// For "mergeByKey" and "keepKeyMatches" skip item
continue;
}
2019-06-23 03:35:23 -07:00
}
if (typeof referenceValue === 'number') {
referenceValue = referenceValue.toString();
} else if (referenceValue !== null && referenceValue.constructor.name === 'Date') {
referenceValue = (referenceValue as Date).toISOString();
2019-06-23 03:35:23 -07:00
}
if (copyData.hasOwnProperty(referenceValue as string)) {
// Item with reference value got found
2019-06-23 03:35:23 -07:00
if (['null', 'undefined'].includes(typeof referenceValue)) {
// The reference value is null or undefined
if (mode === 'removeKeyMatches') {
// For "removeKeyMatches" add item
returnData.push(entry);
}
// For "mergeByKey" and "keepKeyMatches" skip item
2019-06-23 03:35:23 -07:00
continue;
}
// Match exists
if (mode === 'removeKeyMatches') {
// For "removeKeyMatches" we can skip the item as it has a match
continue;
} else if (mode === 'mergeByKey') {
// Copy the entry as the data gets changed
entry = JSON.parse(JSON.stringify(entry));
for (key of Object.keys(copyData[referenceValue as string].json)) {
if (key === propertyName2) {
continue;
}
2020-01-18 20:01:56 -08:00
// TODO: Currently only copies json data and no binary one
const value = copyData[referenceValue as string].json[key];
if (
overwrite === 'always' ||
(overwrite === 'undefined' && !entry.json.hasOwnProperty(key)) ||
(overwrite === 'blank' && [null, undefined, ''].includes(entry.json[key] as string))
) {
entry.json[key] = value;
2020-01-18 20:01:56 -08:00
}
}
} else {
// For "keepKeyMatches" we add it as it is
returnData.push(entry);
continue;
}
} else {
// No item for reference value got found
if (mode === 'removeKeyMatches') {
// For "removeKeyMatches" we can add it if not match got found
returnData.push(entry);
continue;
2019-06-23 03:35:23 -07:00
}
}
if (mode === 'mergeByKey') {
// For "mergeByKey" we always add the entry anyway but then the unchanged one
returnData.push(entry);
}
2019-06-23 03:35:23 -07:00
}
return [returnData];
} else if (mode === 'passThrough') {
const output = this.getNodeParameter('output', 0) as string;
if (output === 'input1') {
returnData.push.apply(returnData, this.getInputData(0));
} else {
returnData.push.apply(returnData, this.getInputData(1));
}
} else if (mode === 'wait') {
returnData.push({ json: {} });
2019-06-23 03:35:23 -07:00
}
return [returnData];
}
}