Copy data on execution only if needed

This commit is contained in:
Jan Oberhauser
2019-08-01 22:55:33 +02:00
parent a8f1f9c0ba
commit 1b59d7b886
20 changed files with 182 additions and 65 deletions

View File

@@ -74,13 +74,27 @@ export class RenameKeys implements INodeType {
const items = this.getInputData();
const returnData: INodeExecutionData[] = [];
let item: INodeExecutionData;
let newItem: INodeExecutionData;
let renameKeys: IRenameKey[];
let value: any; // tslint:disable-line:no-any
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
renameKeys = this.getNodeParameter('keys.key', itemIndex, []) as IRenameKey[];
item = items[itemIndex];
// Copy the whole JSON data as data on any level can be renamed
newItem = {
json: JSON.parse(JSON.stringify(item.json)),
};
if (item.binary !== undefined) {
// Reference binary data if any exists. We can reference it
// as this nodes does not change it
newItem.binary = item.binary;
}
renameKeys.forEach((renameKey) => {
if (renameKey.currentKey === '' || renameKey.newKey === '') {
// Ignore all which do not have all the values set
@@ -90,12 +104,14 @@ export class RenameKeys implements INodeType {
if (value === undefined) {
return;
}
set(item.json, renameKey.newKey, value);
set(newItem.json, renameKey.newKey, value);
unset(item.json, renameKey.currentKey as string);
unset(newItem.json, renameKey.currentKey as string);
});
returnData.push(newItem);
}
return this.prepareOutputData(items);
return [returnData];
}
}