UNPKG

n8n-nodes-changed

Version:

n8n node to detect if something changed between the current execution and the previous one.

67 lines 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Changed = void 0; const hash = require('object-hash'); class Changed { constructor() { this.description = { displayName: 'Has Changed', name: 'hasChanged', icon: 'fa:map-signs', group: ['transform'], version: 1, description: 'Detect if something has changed between the current execution and the previous one.', defaults: { name: 'hasChanged', color: '#1e4873', }, inputs: ['main'], outputs: ['main', 'main'], outputNames: ['true', 'false'], properties: [ { displayName: 'Default has changed', name: 'defaultValue', type: 'boolean', default: true, description: 'When there is no previous execution, this value will define if has changed or not.', }, ], }; } async execute() { const items = this.getInputData(); let returnAllData = items; const returnNoData = []; const defaultValue = this.getNodeParameter('defaultValue', 0); const workflowMetadata = this.getWorkflow(); const node = this.getNode(); const nodeKeys = { "workflowId": `${workflowMetadata.id}`, "nodeName": `${node.name}`, }; const nodeHash = hash(nodeKeys); const staticData = this.getWorkflowStaticData('node'); staticData.hashesIndex = staticData.hashesIndex || {}; const hashesIndex = staticData.hashesIndex; let compareResult; const oldHash = hashesIndex[nodeHash]; const newHash = hash(items.map(item => item.json)); if (!oldHash) { compareResult = defaultValue; hashesIndex[nodeHash] = newHash; } else { compareResult = newHash !== oldHash; } if (compareResult) { hashesIndex[nodeHash] = newHash; return [returnAllData, returnNoData]; } else { return [returnNoData, returnAllData]; } } } exports.Changed = Changed; //# sourceMappingURL=Changed.node.js.map