🔀 Merge branch 'TMaYaD-merge-overwrite'

This commit is contained in:
Jan Oberhauser 2020-01-19 14:01:34 -06:00
commit fa93496397

View file

@ -164,6 +164,37 @@ 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',
description: 'Always overwrites everything.',
},
{
name: 'If Blank',
value: 'blank',
description: 'Overwrites only values of "null", "undefined" or empty string.',
},
{
name: 'If Missing',
value: 'undefined',
description: 'Only adds values which do not exist yet.',
},
],
default: 'always',
description: 'Select when to overwrite the values from Input1 with values from Input 2.',
}
]
};
@ -285,6 +316,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) {
@ -375,8 +407,19 @@ export class Merge implements INodeType {
entry = JSON.parse(JSON.stringify(entry));
for (key of Object.keys(copyData[referenceValue as string].json)) {
if (key === propertyName2) {
continue;
}
// TODO: Currently only copies json data and no binary one
entry.json[key] = copyData[referenceValue as string].json[key];
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 {
// For "keepKeyMatches" we add it as it is