From 37f5996a9965a663602772171589eb593044b7fa Mon Sep 17 00:00:00 2001 From: Subhash Chandra Date: Sun, 19 Jan 2020 09:31:56 +0530 Subject: [PATCH 1/2] Added overwrite option to merge node --- packages/nodes-base/nodes/Merge.node.ts | 35 ++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/packages/nodes-base/nodes/Merge.node.ts b/packages/nodes-base/nodes/Merge.node.ts index d63d5020a2..becf1ff17f 100644 --- a/packages/nodes-base/nodes/Merge.node.ts +++ b/packages/nodes-base/nodes/Merge.node.ts @@ -159,6 +159,34 @@ export class Merge implements INodeType { default: 'input1', description: 'Defines of which input the data should be used as output of node.', }, + { + displayName: 'Overwrite', + name: 'overwrite', + type: 'options', + displayOptions: { + show: { + mode: [ + 'mergeByKey', + ], + }, + }, + options: [ + { + name: 'Always', + value: 'always', + }, + { + name: 'If Missing', + value: 'undefined', + }, + { + name: 'If Blank', + value: 'blank' + }, + ], + default: 'always', + description: 'Select when to overwrite the values from Input1 with values from Input 2.', + } ] }; @@ -263,6 +291,7 @@ export class Merge implements INodeType { const propertyName1 = this.getNodeParameter('propertyName1', 0) as string; const propertyName2 = this.getNodeParameter('propertyName2', 0) as string; + const overwrite = this.getNodeParameter('overwrite', 0) as string; const dataInput2 = this.getInputData(1); if (!dataInput2 || !propertyName1 || !propertyName2) { @@ -354,7 +383,11 @@ export class Merge implements INodeType { for (key of Object.keys(copyData[referenceValue as string].json)) { // TODO: Currently only copies json data and no binary one - entry.json[key] = copyData[referenceValue as string].json[key]; + let value = copyData[referenceValue as string].json[key]; + + if (overwrite === 'always' || (overwrite === 'undefined' && ['null', 'undefined'].includes(typeof value)) || (overwrite === 'blank' && !value)) { + entry.json[key] = value + } } } else { // For "keepKeyMatches" we add it as it is From c5d4c6a1d318f782b44d3fb0c6795d108b37d3dc Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Sun, 19 Jan 2020 14:01:10 -0600 Subject: [PATCH 2/2] :zap: Fix overwrite option on Merge-Node --- packages/nodes-base/nodes/Merge.node.ts | 26 +++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/nodes-base/nodes/Merge.node.ts b/packages/nodes-base/nodes/Merge.node.ts index 644cee2101..59bf4cc185 100644 --- a/packages/nodes-base/nodes/Merge.node.ts +++ b/packages/nodes-base/nodes/Merge.node.ts @@ -179,14 +179,17 @@ export class Merge implements INodeType { { name: 'Always', value: 'always', + description: 'Always overwrites everything.', + }, + { + name: 'If Blank', + value: 'blank', + description: 'Overwrites only values of "null", "undefined" or empty string.', }, { name: 'If Missing', value: 'undefined', - }, - { - name: 'If Blank', - value: 'blank' + description: 'Only adds values which do not exist yet.', }, ], default: 'always', @@ -404,11 +407,18 @@ export class Merge implements INodeType { entry = JSON.parse(JSON.stringify(entry)); for (key of Object.keys(copyData[referenceValue as string].json)) { - // TODO: Currently only copies json data and no binary one - let value = copyData[referenceValue as string].json[key]; + if (key === propertyName2) { + continue; + } - if (overwrite === 'always' || (overwrite === 'undefined' && ['null', 'undefined'].includes(typeof value)) || (overwrite === 'blank' && !value)) { - entry.json[key] = value + // 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; } } } else {