UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

1,051 lines (1,049 loc) 61 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ItemListsV1 = void 0; const get_1 = __importDefault(require("lodash/get")); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const isEqual_1 = __importDefault(require("lodash/isEqual")); const lt_1 = __importDefault(require("lodash/lt")); const pick_1 = __importDefault(require("lodash/pick")); const set_1 = __importDefault(require("lodash/set")); const unset_1 = __importDefault(require("lodash/unset")); const n8n_workflow_1 = require("n8n-workflow"); const utilities_1 = require("../../../utils/utilities"); const summarize = __importStar(require("./summarize.operation")); const utils_1 = require("../V3/helpers/utils"); class ItemListsV1 { description; constructor(baseDescription) { this.description = { ...baseDescription, version: 1, defaults: { name: 'Item Lists', }, inputs: [n8n_workflow_1.NodeConnectionTypes.Main], outputs: [n8n_workflow_1.NodeConnectionTypes.Main], credentials: [], properties: [ { displayName: 'Resource', name: 'resource', type: 'hidden', options: [ { name: 'Item List', value: 'itemList', }, ], default: 'itemList', }, { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Concatenate Items', value: 'aggregateItems', description: 'Combine fields into a list in a single new item', action: 'Combine fields into a list in a single new item', }, { name: 'Limit', value: 'limit', description: 'Remove items if there are too many', action: 'Remove items if there are too many', }, { name: 'Remove Duplicates', value: 'removeDuplicates', description: 'Remove extra items that are similar', action: 'Remove extra items that are similar', }, { name: 'Sort', value: 'sort', description: 'Change the item order', action: 'Change the item order', }, { name: 'Split Out Items', value: 'splitOutItems', description: 'Turn a list inside item(s) into separate items', action: 'Turn a list inside item(s) into separate items', }, { name: 'Summarize', value: 'summarize', description: 'Aggregate items together (pivot table)', action: 'Aggregate items together (pivot table)', }, ], default: 'splitOutItems', }, // Split out items - Fields { displayName: 'Field To Split Out', name: 'fieldToSplitOut', type: 'string', default: '', required: true, displayOptions: { show: { resource: ['itemList'], operation: ['splitOutItems'], }, }, description: 'The name of the input field to break out into separate items', requiresDataPath: 'single', }, { displayName: 'Include', name: 'include', type: 'options', options: [ { name: 'No Other Fields', value: 'noOtherFields', }, { name: 'All Other Fields', value: 'allOtherFields', }, { name: 'Selected Other Fields', value: 'selectedOtherFields', }, ], default: 'noOtherFields', description: 'Whether to copy any other fields into the new items', displayOptions: { show: { resource: ['itemList'], operation: ['splitOutItems'], }, }, }, { displayName: 'Fields To Include', name: 'fieldsToInclude', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Include', default: {}, displayOptions: { show: { resource: ['itemList'], operation: ['splitOutItems'], include: ['selectedOtherFields'], }, }, options: [ { displayName: '', name: 'fields', values: [ { displayName: 'Field Name', name: 'fieldName', type: 'string', default: '', description: 'A field in the input items to aggregate together', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, ], }, ], }, // Aggregate Items { displayName: 'Aggregate', name: 'aggregate', type: 'options', default: 'aggregateIndividualFields', options: [ { name: 'Individual Fields', value: 'aggregateIndividualFields', }, { name: 'All Item Data (Into a Single List)', value: 'aggregateAllItemData', }, ], displayOptions: { show: { resource: ['itemList'], operation: ['aggregateItems'], }, }, }, // Aggregate Individual Fields { displayName: 'Fields To Aggregate', name: 'fieldsToAggregate', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Aggregate', default: { fieldToAggregate: [{ fieldToAggregate: '', renameField: false }] }, displayOptions: { show: { resource: ['itemList'], operation: ['aggregateItems'], aggregate: ['aggregateIndividualFields'], }, }, options: [ { displayName: '', name: 'fieldToAggregate', values: [ { displayName: 'Input Field Name', name: 'fieldToAggregate', type: 'string', default: '', description: 'The name of a field in the input items to aggregate together', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, { displayName: 'Rename Field', name: 'renameField', type: 'boolean', default: false, description: 'Whether to give the field a different name in the output', }, { displayName: 'Output Field Name', name: 'outputFieldName', displayOptions: { show: { renameField: [true], }, }, type: 'string', default: '', description: 'The name of the field to put the aggregated data in. Leave blank to use the input field name.', requiresDataPath: 'single', }, ], }, ], }, // Aggregate All Item Data { displayName: 'Put Output in Field', name: 'destinationFieldName', type: 'string', displayOptions: { show: { resource: ['itemList'], operation: ['aggregateItems'], aggregate: ['aggregateAllItemData'], }, }, default: 'data', description: 'The name of the output field to put the data in', }, { displayName: 'Include', name: 'include', type: 'options', default: 'allFields', options: [ { name: 'All Fields', value: 'allFields', }, { name: 'Specified Fields', value: 'specifiedFields', }, { name: 'All Fields Except', value: 'allFieldsExcept', }, ], displayOptions: { show: { resource: ['itemList'], operation: ['aggregateItems'], aggregate: ['aggregateAllItemData'], }, }, }, { displayName: 'Fields To Exclude', name: 'fieldsToExclude', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Exclude', default: {}, options: [ { displayName: '', name: 'fields', values: [ { displayName: 'Field Name', name: 'fieldName', type: 'string', default: '', description: 'A field in the input to exclude from the object in output array', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, ], }, ], displayOptions: { show: { resource: ['itemList'], operation: ['aggregateItems'], aggregate: ['aggregateAllItemData'], include: ['allFieldsExcept'], }, }, }, { displayName: 'Fields To Include', name: 'fieldsToInclude', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Include', default: {}, options: [ { displayName: '', name: 'fields', values: [ { displayName: 'Field Name', name: 'fieldName', type: 'string', default: '', description: 'Specify fields that will be included in output array', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, ], }, ], displayOptions: { show: { resource: ['itemList'], operation: ['aggregateItems'], aggregate: ['aggregateAllItemData'], include: ['specifiedFields'], }, }, }, // Remove duplicates - Fields { displayName: 'Compare', name: 'compare', type: 'options', options: [ { name: 'All Fields', value: 'allFields', }, { name: 'All Fields Except', value: 'allFieldsExcept', }, { name: 'Selected Fields', value: 'selectedFields', }, ], default: 'allFields', description: 'The fields of the input items to compare to see if they are the same', displayOptions: { show: { resource: ['itemList'], operation: ['removeDuplicates'], }, }, }, { displayName: 'Fields To Exclude', name: 'fieldsToExclude', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Exclude', default: {}, displayOptions: { show: { resource: ['itemList'], operation: ['removeDuplicates'], compare: ['allFieldsExcept'], }, }, options: [ { displayName: '', name: 'fields', values: [ { displayName: 'Field Name', name: 'fieldName', type: 'string', default: '', description: 'A field in the input to exclude from the comparison', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, ], }, ], }, { displayName: 'Fields To Compare', name: 'fieldsToCompare', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Compare', default: {}, displayOptions: { show: { resource: ['itemList'], operation: ['removeDuplicates'], compare: ['selectedFields'], }, }, options: [ { displayName: '', name: 'fields', values: [ { displayName: 'Field Name', name: 'fieldName', type: 'string', default: '', description: 'A field in the input to add to the comparison', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, ], }, ], }, // Sort - Fields { displayName: 'Type', name: 'type', type: 'options', options: [ { name: 'Simple', value: 'simple', }, { name: 'Random', value: 'random', }, { name: 'Code', value: 'code', }, ], default: 'simple', description: 'The fields of the input items to compare to see if they are the same', displayOptions: { show: { resource: ['itemList'], operation: ['sort'], }, }, }, { displayName: 'Fields To Sort By', name: 'sortFieldsUi', type: 'fixedCollection', typeOptions: { multipleValues: true, }, placeholder: 'Add Field To Sort By', options: [ { displayName: '', name: 'sortField', values: [ { displayName: 'Field Name', name: 'fieldName', type: 'string', required: true, default: '', description: 'The field to sort by', // eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id placeholder: 'e.g. id', hint: ' Enter the field name as text', requiresDataPath: 'single', }, { displayName: 'Order', name: 'order', type: 'options', options: [ { name: 'Ascending', value: 'ascending', }, { name: 'Descending', value: 'descending', }, ], default: 'ascending', description: 'The order to sort by', }, ], }, ], default: {}, description: 'The fields of the input items to compare to see if they are the same', displayOptions: { show: { resource: ['itemList'], operation: ['sort'], type: ['simple'], }, }, }, { displayName: 'Code', name: 'code', type: 'string', typeOptions: { alwaysOpenEditWindow: true, editor: 'jsEditor', rows: 10, }, default: `// The two items to compare are in the variables a and b // Access the fields in a.json and b.json // Return -1 if a should go before b // Return 1 if b should go before a // Return 0 if there's no difference fieldName = 'myField'; if (a.json[fieldName] < b.json[fieldName]) { return -1; } if (a.json[fieldName] > b.json[fieldName]) { return 1; } return 0;`, description: 'Javascript code to determine the order of any two items', displayOptions: { show: { resource: ['itemList'], operation: ['sort'], type: ['code'], }, }, }, // Limit - Fields { displayName: 'Max Items', name: 'maxItems', type: 'number', typeOptions: { minValue: 1, }, default: 1, description: 'If there are more items than this number, some are removed', displayOptions: { show: { resource: ['itemList'], operation: ['limit'], }, }, }, { displayName: 'Keep', name: 'keep', type: 'options', options: [ { name: 'First Items', value: 'firstItems', }, { name: 'Last Items', value: 'lastItems', }, ], default: 'firstItems', description: 'When removing items, whether to keep the ones at the start or the ending', displayOptions: { show: { resource: ['itemList'], operation: ['limit'], }, }, }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Field', default: {}, displayOptions: { show: { resource: ['itemList'], operation: ['removeDuplicates'], compare: ['allFieldsExcept', 'selectedFields'], }, }, options: [ { displayName: 'Remove Other Fields', name: 'removeOtherFields', type: 'boolean', default: false, description: 'Whether to remove any fields that are not being compared. If disabled, will keep the values from the first of the duplicates.', }, { displayName: 'Disable Dot Notation', name: 'disableDotNotation', type: 'boolean', default: false, description: 'Whether to disallow referencing child fields using `parent.child` in the field name', }, ], }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Field', default: {}, displayOptions: { show: { resource: ['itemList'], operation: ['sort'], type: ['simple'], }, }, options: [ { displayName: 'Disable Dot Notation', name: 'disableDotNotation', type: 'boolean', default: false, description: 'Whether to disallow referencing child fields using `parent.child` in the field name', }, ], }, { displayName: 'Options', name: 'options', type: 'collection', placeholder: 'Add Field', default: {}, displayOptions: { show: { resource: ['itemList'], operation: ['splitOutItems', 'aggregateItems'], }, hide: { aggregate: ['aggregateAllItemData'], }, }, options: [ { displayName: 'Disable Dot Notation', name: 'disableDotNotation', type: 'boolean', displayOptions: { show: { '/operation': ['splitOutItems', 'aggregateItems'], }, }, default: false, description: 'Whether to disallow referencing child fields using `parent.child` in the field name', }, { displayName: 'Destination Field Name', name: 'destinationFieldName', type: 'string', displayOptions: { show: { '/operation': ['splitOutItems'], }, }, default: '', description: 'The field in the output under which to put the split field contents', }, { displayName: 'Merge Lists', name: 'mergeLists', type: 'boolean', displayOptions: { show: { '/operation': ['aggregateItems'], }, }, default: false, description: 'Whether to merge the output into a single flat list (rather than a list of lists), if the field to aggregate is a list', }, { displayName: 'Keep Missing And Null Values', name: 'keepMissing', type: 'boolean', displayOptions: { show: { '/operation': ['aggregateItems'], }, }, default: false, description: 'Whether to add a null entry to the aggregated list when there is a missing or null value', }, ], }, // Remove duplicates - Fields ...summarize.description, ], }; } async execute() { const items = this.getInputData(); const length = items.length; const returnData = []; const resource = this.getNodeParameter('resource', 0); const operation = this.getNodeParameter('operation', 0); if (resource === 'itemList') { if (operation === 'splitOutItems') { for (let i = 0; i < length; i++) { const fieldToSplitOut = this.getNodeParameter('fieldToSplitOut', i); const disableDotNotation = this.getNodeParameter('options.disableDotNotation', 0, false); const destinationFieldName = this.getNodeParameter('options.destinationFieldName', i, ''); const include = this.getNodeParameter('include', i); let arrayToSplit; if (!disableDotNotation) { arrayToSplit = (0, get_1.default)(items[i].json, fieldToSplitOut); } else { arrayToSplit = items[i].json[fieldToSplitOut]; } if (arrayToSplit === undefined) { if (fieldToSplitOut.includes('.') && disableDotNotation) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Couldn't find the field '${fieldToSplitOut}' in the input data`, { description: "If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options", }); } else { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Couldn't find the field '${fieldToSplitOut}' in the input data`, { itemIndex: i }); } } if (!Array.isArray(arrayToSplit)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The provided field '${fieldToSplitOut}' is not an array`, { itemIndex: i }); } else { for (const element of arrayToSplit) { let newItem = {}; if (include === 'selectedOtherFields') { const fieldsToInclude = this.getNodeParameter('fieldsToInclude.fields', i, []).map((field) => field.fieldName); if (!fieldsToInclude.length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No fields specified', { description: 'Please add a field to include', }); } newItem = { ...fieldsToInclude.reduce((prev, field) => { if (field === fieldToSplitOut) { return prev; } let value; if (!disableDotNotation) { value = (0, get_1.default)(items[i].json, field); } else { value = items[i].json[field]; } prev = { ...prev, [field]: value }; return prev; }, {}), }; } else if (include === 'allOtherFields') { const keys = Object.keys(items[i].json); newItem = { ...keys.reduce((prev, field) => { let value; if (!disableDotNotation) { value = (0, get_1.default)(items[i].json, field); } else { value = items[i].json[field]; } prev = { ...prev, [field]: value }; return prev; }, {}), }; (0, unset_1.default)(newItem, fieldToSplitOut); } if (typeof element === 'object' && include === 'noOtherFields' && destinationFieldName === '') { newItem = { ...newItem, ...element }; } else { newItem = { ...newItem, [destinationFieldName || fieldToSplitOut]: element, }; } returnData.push({ json: newItem, pairedItem: { item: i, }, }); } } } return [returnData]; } else if (operation === 'aggregateItems') { const aggregate = this.getNodeParameter('aggregate', 0, ''); if (aggregate === 'aggregateIndividualFields') { const disableDotNotation = this.getNodeParameter('options.disableDotNotation', 0, false); const mergeLists = this.getNodeParameter('options.mergeLists', 0, false); const fieldsToAggregate = this.getNodeParameter('fieldsToAggregate.fieldToAggregate', 0, []); const keepMissing = this.getNodeParameter('options.keepMissing', 0, false); if (!fieldsToAggregate.length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No fields specified', { description: 'Please add a field to aggregate', }); } for (const { fieldToAggregate } of fieldsToAggregate) { let found = false; for (const item of items) { if (fieldToAggregate === '') { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Field to aggregate is blank', { description: 'Please add a field to aggregate', }); } if (!disableDotNotation) { if ((0, get_1.default)(item.json, fieldToAggregate) !== undefined) { found = true; } } else if (item.json.hasOwnProperty(fieldToAggregate)) { found = true; } } if (!found && disableDotNotation && fieldToAggregate.includes('.')) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Couldn't find the field '${fieldToAggregate}' in the input data`, { description: "If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options", }); } else if (!found && !keepMissing) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `Couldn't find the field '${fieldToAggregate}' in the input data`); } } const newItem = { json: {}, pairedItem: Array.from({ length }, (_, i) => i).map((index) => { return { item: index, }; }), }; const values = {}; const outputFields = []; for (const { fieldToAggregate, outputFieldName, renameField } of fieldsToAggregate) { const field = renameField ? outputFieldName : fieldToAggregate; if (outputFields.includes(field)) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), `The '${field}' output field is used more than once`, { description: 'Please make sure each output field name is unique' }); } else { outputFields.push(field); } const getFieldToAggregate = () => !disableDotNotation && fieldToAggregate.includes('.') ? fieldToAggregate.split('.').pop() : fieldToAggregate; const _outputFieldName = outputFieldName ? outputFieldName : getFieldToAggregate(); if (fieldToAggregate !== '') { values[_outputFieldName] = []; for (let i = 0; i < length; i++) { if (!disableDotNotation) { let value = (0, get_1.default)(items[i].json, fieldToAggregate); if (!keepMissing) { if (Array.isArray(value)) { value = value.filter((entry) => entry !== null); } else if (value === null || value === undefined) { continue; } } if (Array.isArray(value) && mergeLists) { values[_outputFieldName].push(...value); } else { values[_outputFieldName].push(value); } } else { let value = items[i].json[fieldToAggregate]; if (!keepMissing) { if (Array.isArray(value)) { value = value.filter((entry) => entry !== null); } else if (value === null || value === undefined) { continue; } } if (Array.isArray(value) && mergeLists) { values[_outputFieldName].push(...value); } else { values[_outputFieldName].push(value); } } } } } for (const key of Object.keys(values)) { if (!disableDotNotation) { (0, set_1.default)(newItem.json, key, values[key]); } else { newItem.json[key] = values[key]; } } returnData.push(newItem); return [returnData]; } else { let newItems = items.map((item) => item.json); const destinationFieldName = this.getNodeParameter('destinationFieldName', 0); const fieldsToExclude = this.getNodeParameter('fieldsToExclude.fields', 0, []).map((entry) => entry.fieldName); const fieldsToInclude = this.getNodeParameter('fieldsToInclude.fields', 0, []).map((entry) => entry.fieldName); if (fieldsToExclude.length || fieldsToInclude.length) { newItems = newItems.reduce((acc, item) => { const newItem = {}; let outputFields = Object.keys(item); if (fieldsToExclude.length) { outputFields = outputFields.filter((key) => !fieldsToExclude.includes(key)); } if (fieldsToInclude.length) { outputFields = outputFields.filter((key) => fieldsToInclude.length ? fieldsToInclude.includes(key) : true); } outputFields.forEach((key) => { newItem[key] = item[key]; }); if ((0, isEmpty_1.default)(newItem)) { return acc; } return acc.concat([newItem]); }, []); } return [[{ json: { [destinationFieldName]: newItems } }]]; } } else if (operation === 'removeDuplicates') { const compare = this.getNodeParameter('compare', 0); const disableDotNotation = this.getNodeParameter('options.disableDotNotation', 0, false); const removeOtherFields = this.getNodeParameter('options.removeOtherFields', 0, false); let keys = disableDotNotation ? Object.keys(items[0].json) : Object.keys((0, utilities_1.flattenKeys)(items[0].json)); for (const item of items) { for (const key of disableDotNotation ? Object.keys(item.json) : Object.keys((0, utilities_1.flattenKeys)(item.json))) { if (!keys.includes(key)) { keys.push(key); } } } if (compare === 'allFieldsExcept') { const fieldsToExclude = this.getNodeParameter('fieldsToExclude.fields', 0, []).map((field) => field.fieldName); if (!fieldsToExclude.length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No fields specified. Please add a field to exclude from comparison'); } if (!disableDotNotation) { keys = Object.keys((0, utilities_1.flattenKeys)(items[0].json)); } keys = keys.filter((key) => !fieldsToExclude.includes(key)); } if (compare === 'selectedFields') { const fieldsToCompare = this.getNodeParameter('fieldsToCompare.fields', 0, []).map((field) => field.fieldName); if (!fieldsToCompare.length) { throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'No fields specified. Please add a field to compare on'); } if (!disableDotNotation) { keys = Object.keys((0, utilities_1.flattenKeys)(items[0].json)); } keys = fieldsToCompare.map((key) => key.trim()); } // T