2023-01-13 09:11:56 -08:00
|
|
|
import { NodeVM, NodeVMOptions } from 'vm2';
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
2021-09-15 00:55:36 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INode,
|
|
|
|
INodeExecutionData,
|
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
NodeOperationError,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { get, isEmpty, isEqual, isObject, lt, merge, pick, reduce, set, unset } from 'lodash';
|
|
|
|
|
2023-01-13 09:11:56 -08:00
|
|
|
const compareItems = (
|
|
|
|
obj: INodeExecutionData,
|
|
|
|
obj2: INodeExecutionData,
|
|
|
|
keys: string[],
|
|
|
|
disableDotNotation: boolean,
|
|
|
|
_node: INode,
|
|
|
|
) => {
|
|
|
|
let result = true;
|
|
|
|
for (const key of keys) {
|
|
|
|
if (!disableDotNotation) {
|
|
|
|
if (!isEqual(get(obj.json, key), get(obj2.json, key))) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!isEqual(obj.json[key], obj2.json[key])) {
|
|
|
|
result = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
const flattenKeys = (obj: IDataObject, path: string[] = []): IDataObject => {
|
|
|
|
return !isObject(obj)
|
|
|
|
? { [path.join('.')]: obj }
|
|
|
|
: reduce(obj, (cum, next, key) => merge(cum, flattenKeys(next as IDataObject, [...path, key])), {}); //prettier-ignore
|
|
|
|
};
|
|
|
|
|
|
|
|
const shuffleArray = (array: any[]) => {
|
|
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
|
|
}
|
|
|
|
};
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2023-01-17 08:40:28 -08:00
|
|
|
import * as summarize from './summarize.operation';
|
|
|
|
|
2021-09-15 00:55:36 -07:00
|
|
|
export class ItemLists implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'Item Lists',
|
|
|
|
name: 'itemLists',
|
|
|
|
icon: 'file:itemLists.svg',
|
|
|
|
group: ['input'],
|
|
|
|
version: 1,
|
|
|
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
|
|
|
description: 'Helper for working with lists of items and transforming arrays',
|
|
|
|
defaults: {
|
|
|
|
name: 'Item Lists',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'hidden',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'Item List',
|
|
|
|
value: 'itemList',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'itemList',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Operation',
|
|
|
|
name: 'operation',
|
|
|
|
type: 'options',
|
2022-05-20 14:47:24 -07:00
|
|
|
noDataExpression: true,
|
2021-09-15 00:55:36 -07:00
|
|
|
options: [
|
|
|
|
{
|
2023-01-17 08:40:28 -08:00
|
|
|
name: 'Concatenate Items',
|
2021-09-15 00:55:36 -07:00
|
|
|
value: 'aggregateItems',
|
2023-01-17 08:40:28 -08:00
|
|
|
description: 'Combine fields into a list in a single new item',
|
|
|
|
action: 'Combine fields into a list in a single new item',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
2022-06-03 10:23:49 -07:00
|
|
|
{
|
|
|
|
name: 'Limit',
|
|
|
|
value: 'limit',
|
|
|
|
description: 'Remove items if there are too many',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Remove items if there are too many',
|
2022-06-03 10:23:49 -07:00
|
|
|
},
|
2021-09-15 00:55:36 -07:00
|
|
|
{
|
|
|
|
name: 'Remove Duplicates',
|
|
|
|
value: 'removeDuplicates',
|
|
|
|
description: 'Remove extra items that are similar',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Remove extra items that are similar',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Sort',
|
|
|
|
value: 'sort',
|
|
|
|
description: 'Change the item order',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Change the item order',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Split Out Items',
|
|
|
|
value: 'splitOutItems',
|
|
|
|
description: 'Turn a list inside item(s) into separate items',
|
2022-07-10 13:50:51 -07:00
|
|
|
action: 'Turn a list inside item(s) into separate items',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
2023-01-17 08:40:28 -08:00
|
|
|
{
|
|
|
|
name: 'Summarize',
|
|
|
|
value: 'summarize',
|
|
|
|
description: 'Aggregate items together (pivot table)',
|
|
|
|
action: 'Aggregate items together (pivot table)',
|
|
|
|
},
|
2021-09-15 00:55:36 -07:00
|
|
|
],
|
|
|
|
default: 'splitOutItems',
|
|
|
|
},
|
|
|
|
// Split out items - Fields
|
|
|
|
{
|
|
|
|
displayName: 'Field To Split Out',
|
|
|
|
name: 'fieldToSplitOut',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['splitOutItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
description: 'The name of the input field to break out into separate items',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['splitOutItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Fields To Include',
|
|
|
|
name: 'fieldsToInclude',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
|
|
|
placeholder: 'Add Field To Include',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['splitOutItems'],
|
|
|
|
include: ['selectedOtherFields'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: '',
|
|
|
|
name: 'fields',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Field Name',
|
|
|
|
name: 'fieldName',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'A field in the input items to aggregate together',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2022-07-27 07:19:50 -07:00
|
|
|
// 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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['aggregateItems'],
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Aggregate Individual Fields
|
2021-09-15 00:55:36 -07:00
|
|
|
{
|
|
|
|
displayName: 'Fields To Aggregate',
|
|
|
|
name: 'fieldsToAggregate',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
|
|
|
placeholder: 'Add Field To Aggregate',
|
2022-08-17 08:50:24 -07:00
|
|
|
default: { fieldToAggregate: [{ fieldToAggregate: '', renameField: false }] },
|
2021-09-15 00:55:36 -07:00
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['aggregateItems'],
|
|
|
|
aggregate: ['aggregateIndividualFields'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
renameField: [true],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'The name of the field to put the aggregated data in. Leave blank to use the input field name.',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2022-07-27 07:19:50 -07:00
|
|
|
// Aggregate All Item Data
|
|
|
|
{
|
|
|
|
displayName: 'Put Output in Field',
|
|
|
|
name: 'destinationFieldName',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['aggregateItems'],
|
|
|
|
aggregate: ['aggregateAllItemData'],
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['aggregateItems'],
|
|
|
|
aggregate: ['aggregateAllItemData'],
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['aggregateItems'],
|
|
|
|
aggregate: ['aggregateAllItemData'],
|
|
|
|
include: ['allFieldsExcept'],
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['aggregateItems'],
|
|
|
|
aggregate: ['aggregateAllItemData'],
|
|
|
|
include: ['specifiedFields'],
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-15 00:55:36 -07:00
|
|
|
// 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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['removeDuplicates'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Fields To Exclude',
|
|
|
|
name: 'fieldsToExclude',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
|
|
|
placeholder: 'Add Field To Exclude',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['removeDuplicates'],
|
|
|
|
compare: ['allFieldsExcept'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: '',
|
|
|
|
name: 'fields',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Field Name',
|
|
|
|
name: 'fieldName',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'A field in the input to exclude from the comparison',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Fields To Compare',
|
|
|
|
name: 'fieldsToCompare',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
2022-02-11 00:24:36 -08:00
|
|
|
placeholder: 'Add Field To Compare',
|
2021-09-15 00:55:36 -07:00
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['removeDuplicates'],
|
|
|
|
compare: ['selectedFields'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: '',
|
|
|
|
name: 'fields',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Field Name',
|
|
|
|
name: 'fieldName',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
description: 'A field in the input to add to the comparison',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
// 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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['sort'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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',
|
2022-09-12 22:59:49 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-placeholder-miscased-id
|
|
|
|
placeholder: 'e.g. id',
|
|
|
|
hint: ' Enter the field name as text',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['sort'],
|
|
|
|
type: ['simple'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Code',
|
|
|
|
name: 'code',
|
|
|
|
type: 'string',
|
|
|
|
typeOptions: {
|
|
|
|
alwaysOpenEditWindow: true,
|
|
|
|
editor: 'code',
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['sort'],
|
|
|
|
type: ['code'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// 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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['limit'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['limit'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['removeDuplicates'],
|
|
|
|
compare: ['allFieldsExcept', 'selectedFields'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Remove Other Fields',
|
|
|
|
name: 'removeOtherFields',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Whether to remove any fields that are not being compared. If disabled, will keep the values from the first of the duplicates.',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Disable Dot Notation',
|
|
|
|
name: 'disableDotNotation',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Whether to disallow referencing child fields using `parent.child` in the field name',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['sort'],
|
|
|
|
type: ['simple'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Disable Dot Notation',
|
|
|
|
name: 'disableDotNotation',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Whether to disallow referencing child fields using `parent.child` in the field name',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Options',
|
|
|
|
name: 'options',
|
|
|
|
type: 'collection',
|
|
|
|
placeholder: 'Add Field',
|
|
|
|
default: {},
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: ['itemList'],
|
|
|
|
operation: ['splitOutItems', 'aggregateItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
2022-07-27 07:19:50 -07:00
|
|
|
hide: {
|
2022-08-17 08:50:24 -07:00
|
|
|
aggregate: ['aggregateAllItemData'],
|
2022-07-27 07:19:50 -07:00
|
|
|
},
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
displayName: 'Disable Dot Notation',
|
|
|
|
name: 'disableDotNotation',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
'/operation': ['splitOutItems', 'aggregateItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: false,
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Whether to disallow referencing child fields using `parent.child` in the field name',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Destination Field Name',
|
|
|
|
name: 'destinationFieldName',
|
|
|
|
type: 'string',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
'/operation': ['splitOutItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: '',
|
|
|
|
description: 'The field in the output under which to put the split field contents',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Merge Lists',
|
|
|
|
name: 'mergeLists',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
'/operation': ['aggregateItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: false,
|
2022-08-17 08:50:24 -07:00
|
|
|
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',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Keep Missing And Null Values',
|
|
|
|
name: 'keepMissing',
|
|
|
|
type: 'boolean',
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
2022-08-17 08:50:24 -07:00
|
|
|
'/operation': ['aggregateItems'],
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
default: false,
|
2022-08-17 08:50:24 -07:00
|
|
|
description:
|
|
|
|
'Whether to add a null entry to the aggregated list when there is a missing or null value',
|
2021-09-15 00:55:36 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2023-01-17 08:40:28 -08:00
|
|
|
// Remove duplicates - Fields
|
|
|
|
...summarize.description,
|
2021-09-15 00:55:36 -07:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2021-09-15 00:55:36 -07:00
|
|
|
const returnData: INodeExecutionData[] = [];
|
2022-12-02 03:53:59 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
2021-09-15 00:55:36 -07:00
|
|
|
if (resource === 'itemList') {
|
|
|
|
if (operation === 'splitOutItems') {
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const fieldToSplitOut = this.getNodeParameter('fieldToSplitOut', i) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const disableDotNotation = this.getNodeParameter(
|
|
|
|
'options.disableDotNotation',
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
) as boolean;
|
|
|
|
const destinationFieldName = this.getNodeParameter(
|
|
|
|
'options.destinationFieldName',
|
|
|
|
i,
|
|
|
|
'',
|
|
|
|
) as string;
|
2021-09-15 00:55:36 -07:00
|
|
|
const include = this.getNodeParameter('include', i) as string;
|
|
|
|
|
|
|
|
let arrayToSplit;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
arrayToSplit = get(items[i].json, fieldToSplitOut);
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
arrayToSplit = items[i].json[fieldToSplitOut];
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arrayToSplit === undefined) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (fieldToSplitOut.includes('.') && disableDotNotation) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Couldn't find the field '${fieldToSplitOut}' in the input data`,
|
|
|
|
{
|
2022-12-29 03:20:43 -08:00
|
|
|
description:
|
|
|
|
"If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options",
|
2022-08-17 08:50:24 -07:00
|
|
|
},
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Couldn't find the field '${fieldToSplitOut}' in the input data`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Array.isArray(arrayToSplit)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The provided field '${fieldToSplitOut}' is not an array`,
|
|
|
|
{ itemIndex: i },
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
} else {
|
|
|
|
for (const element of arrayToSplit) {
|
|
|
|
let newItem = {};
|
|
|
|
|
|
|
|
if (include === 'selectedOtherFields') {
|
2022-08-17 08:50:24 -07:00
|
|
|
const fieldsToInclude = (
|
|
|
|
this.getNodeParameter('fieldsToInclude.fields', i, []) as [{ fieldName: string }]
|
|
|
|
).map((field) => field.fieldName);
|
2021-09-15 00:55:36 -07:00
|
|
|
|
|
|
|
if (!fieldsToInclude.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No fields specified', {
|
|
|
|
description: 'Please add a field to include',
|
|
|
|
});
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
newItem = {
|
|
|
|
...fieldsToInclude.reduce((prev, field) => {
|
|
|
|
if (field === fieldToSplitOut) {
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
let value;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
value = get(items[i].json, field);
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
value = items[i].json[field];
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
prev = { ...prev, [field]: value };
|
2021-09-15 00:55:36 -07:00
|
|
|
return prev;
|
|
|
|
}, {}),
|
|
|
|
};
|
|
|
|
} else if (include === 'allOtherFields') {
|
|
|
|
const keys = Object.keys(items[i].json);
|
|
|
|
|
|
|
|
newItem = {
|
|
|
|
...keys.reduce((prev, field) => {
|
|
|
|
let value;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
value = get(items[i].json, field);
|
|
|
|
} else {
|
2022-12-02 12:54:28 -08:00
|
|
|
value = items[i].json[field];
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
prev = { ...prev, [field]: value };
|
2021-09-15 00:55:36 -07:00
|
|
|
return prev;
|
|
|
|
}, {}),
|
|
|
|
};
|
|
|
|
|
|
|
|
unset(newItem, fieldToSplitOut);
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (
|
|
|
|
typeof element === 'object' &&
|
|
|
|
include === 'noOtherFields' &&
|
|
|
|
destinationFieldName === ''
|
|
|
|
) {
|
2021-09-15 00:55:36 -07:00
|
|
|
newItem = { ...newItem, ...element };
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
newItem = {
|
|
|
|
...newItem,
|
2022-12-02 12:54:28 -08:00
|
|
|
[destinationFieldName || fieldToSplitOut]: element,
|
2022-08-17 08:50:24 -07:00
|
|
|
};
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
2022-06-03 08:25:07 -07:00
|
|
|
returnData.push({
|
|
|
|
json: newItem,
|
|
|
|
pairedItem: {
|
|
|
|
item: i,
|
|
|
|
},
|
|
|
|
});
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prepareOutputData(returnData);
|
|
|
|
} else if (operation === 'aggregateItems') {
|
2022-07-27 07:19:50 -07:00
|
|
|
const aggregate = this.getNodeParameter('aggregate', 0, '') as string;
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
if (aggregate === 'aggregateIndividualFields') {
|
|
|
|
const disableDotNotation = this.getNodeParameter(
|
|
|
|
'options.disableDotNotation',
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
) as boolean;
|
2022-07-27 07:19:50 -07:00
|
|
|
const mergeLists = this.getNodeParameter('options.mergeLists', 0, false) as boolean;
|
2022-08-17 08:50:24 -07:00
|
|
|
const fieldsToAggregate = this.getNodeParameter(
|
|
|
|
'fieldsToAggregate.fieldToAggregate',
|
|
|
|
0,
|
|
|
|
[],
|
|
|
|
) as [{ fieldToAggregate: string; renameField: boolean; outputFieldName: string }];
|
2022-07-27 07:19:50 -07:00
|
|
|
const keepMissing = this.getNodeParameter('options.keepMissing', 0, false) as boolean;
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (!fieldsToAggregate.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No fields specified', {
|
|
|
|
description: 'Please add a field to aggregate',
|
|
|
|
});
|
2022-07-27 07:19:50 -07:00
|
|
|
}
|
|
|
|
for (const { fieldToAggregate } of fieldsToAggregate) {
|
|
|
|
let found = false;
|
|
|
|
for (const item of items) {
|
|
|
|
if (fieldToAggregate === '') {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Field to aggregate is blank', {
|
|
|
|
description: 'Please add a field to aggregate',
|
|
|
|
});
|
2022-07-27 07:19:50 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2022-07-27 07:19:50 -07:00
|
|
|
if (get(item.json, fieldToAggregate) !== undefined) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
} else if (item.json.hasOwnProperty(fieldToAggregate)) {
|
2021-09-15 00:55:36 -07:00
|
|
|
found = true;
|
|
|
|
}
|
2022-07-27 07:19:50 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!found && disableDotNotation && fieldToAggregate.includes('.')) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Couldn't find the field '${fieldToAggregate}' in the input data`,
|
|
|
|
{
|
2022-12-29 03:20:43 -08:00
|
|
|
description:
|
|
|
|
"If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options",
|
2022-08-17 08:50:24 -07:00
|
|
|
},
|
|
|
|
);
|
2022-12-02 12:54:28 -08:00
|
|
|
} else if (!found && !keepMissing) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Couldn't find the field '${fieldToAggregate}' in the input data`,
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
const newItem: INodeExecutionData = {
|
2022-07-27 07:19:50 -07:00
|
|
|
json: {},
|
2022-08-17 08:50:24 -07:00
|
|
|
pairedItem: Array.from({ length }, (_, i) => i).map((index) => {
|
2022-07-27 07:19:50 -07:00
|
|
|
return {
|
|
|
|
item: index,
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
};
|
2022-06-03 08:25:07 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
const values: { [key: string]: any } = {};
|
|
|
|
const outputFields: string[] = [];
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
for (const { fieldToAggregate, outputFieldName, renameField } of fieldsToAggregate) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const field = renameField ? outputFieldName : fieldToAggregate;
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (outputFields.includes(field)) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The '${field}' output field is used more than once`,
|
2022-12-29 03:20:43 -08:00
|
|
|
{ description: 'Please make sure each output field name is unique' },
|
2022-08-17 08:50:24 -07:00
|
|
|
);
|
2022-07-27 07:19:50 -07:00
|
|
|
} else {
|
|
|
|
outputFields.push(field);
|
|
|
|
}
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const getFieldToAggregate = () =>
|
2022-12-02 12:54:28 -08:00
|
|
|
!disableDotNotation && fieldToAggregate.includes('.')
|
2022-08-17 08:50:24 -07:00
|
|
|
? fieldToAggregate.split('.').pop()
|
|
|
|
: fieldToAggregate;
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const _outputFieldName = outputFieldName
|
|
|
|
? outputFieldName
|
|
|
|
: (getFieldToAggregate() as string);
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (fieldToAggregate !== '') {
|
|
|
|
values[_outputFieldName] = [];
|
|
|
|
for (let i = 0; i < length; i++) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2022-07-27 07:19:50 -07:00
|
|
|
let value = get(items[i].json, fieldToAggregate);
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (!keepMissing) {
|
|
|
|
if (Array.isArray(value)) {
|
2022-12-02 12:54:28 -08:00
|
|
|
value = value.filter((entry) => entry !== null);
|
2022-07-27 07:19:50 -07:00
|
|
|
} else if (value === null || value === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (Array.isArray(value) && mergeLists) {
|
|
|
|
values[_outputFieldName].push(...value);
|
|
|
|
} else {
|
|
|
|
values[_outputFieldName].push(value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let value = items[i].json[fieldToAggregate];
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (!keepMissing) {
|
|
|
|
if (Array.isArray(value)) {
|
2022-12-02 12:54:28 -08:00
|
|
|
value = value.filter((entry) => entry !== null);
|
2022-07-27 07:19:50 -07:00
|
|
|
} else if (value === null || value === undefined) {
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
if (Array.isArray(value) && mergeLists) {
|
|
|
|
values[_outputFieldName].push(...value);
|
|
|
|
} else {
|
|
|
|
values[_outputFieldName].push(value);
|
|
|
|
}
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
for (const key of Object.keys(values)) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2022-07-27 07:19:50 -07:00
|
|
|
set(newItem.json, key, values[key]);
|
|
|
|
} else {
|
|
|
|
newItem.json[key] = values[key];
|
|
|
|
}
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
returnData.push(newItem);
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-07-27 07:19:50 -07:00
|
|
|
return this.prepareOutputData(returnData);
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
let newItems: IDataObject[] = items.map((item) => item.json);
|
2022-07-27 07:19:50 -07:00
|
|
|
const destinationFieldName = this.getNodeParameter('destinationFieldName', 0) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const fieldsToExclude = (
|
|
|
|
this.getNodeParameter('fieldsToExclude.fields', 0, []) as IDataObject[]
|
|
|
|
).map((entry) => entry.fieldName);
|
|
|
|
const fieldsToInclude = (
|
|
|
|
this.getNodeParameter('fieldsToInclude.fields', 0, []) as IDataObject[]
|
|
|
|
).map((entry) => entry.fieldName);
|
2022-07-27 07:19:50 -07:00
|
|
|
|
|
|
|
if (fieldsToExclude.length || fieldsToInclude.length) {
|
|
|
|
newItems = newItems.reduce((acc, item) => {
|
2022-08-17 08:50:24 -07:00
|
|
|
const newItem: IDataObject = {};
|
2022-07-27 07:19:50 -07:00
|
|
|
let outputFields = Object.keys(item);
|
|
|
|
|
|
|
|
if (fieldsToExclude.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
outputFields = outputFields.filter((key) => !fieldsToExclude.includes(key));
|
2022-07-27 07:19:50 -07:00
|
|
|
}
|
|
|
|
if (fieldsToInclude.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
outputFields = outputFields.filter((key) =>
|
|
|
|
fieldsToInclude.length ? fieldsToInclude.includes(key) : true,
|
|
|
|
);
|
2022-07-27 07:19:50 -07:00
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
outputFields.forEach((key) => {
|
2022-07-27 07:19:50 -07:00
|
|
|
newItem[key] = item[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isEmpty(newItem)) {
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
return acc.concat([newItem]);
|
|
|
|
}, [] as IDataObject[]);
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const output: INodeExecutionData = { json: { [destinationFieldName]: newItems } };
|
2022-07-27 07:19:50 -07:00
|
|
|
|
|
|
|
return this.prepareOutputData([output]);
|
|
|
|
}
|
2021-09-15 00:55:36 -07:00
|
|
|
} else if (operation === 'removeDuplicates') {
|
|
|
|
const compare = this.getNodeParameter('compare', 0) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const disableDotNotation = this.getNodeParameter(
|
|
|
|
'options.disableDotNotation',
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
) as boolean;
|
|
|
|
const removeOtherFields = this.getNodeParameter(
|
|
|
|
'options.removeOtherFields',
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
) as boolean;
|
|
|
|
|
|
|
|
let keys = disableDotNotation
|
|
|
|
? Object.keys(items[0].json)
|
|
|
|
: Object.keys(flattenKeys(items[0].json));
|
2021-09-15 00:55:36 -07:00
|
|
|
|
|
|
|
for (const item of items) {
|
2022-08-17 08:50:24 -07:00
|
|
|
for (const key of disableDotNotation
|
|
|
|
? Object.keys(item.json)
|
|
|
|
: Object.keys(flattenKeys(item.json))) {
|
2021-09-15 00:55:36 -07:00
|
|
|
if (!keys.includes(key)) {
|
|
|
|
keys.push(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (compare === 'allFieldsExcept') {
|
2022-08-17 08:50:24 -07:00
|
|
|
const fieldsToExclude = (
|
|
|
|
this.getNodeParameter('fieldsToExclude.fields', 0, []) as [{ fieldName: string }]
|
|
|
|
).map((field) => field.fieldName);
|
2021-09-15 00:55:36 -07:00
|
|
|
if (!fieldsToExclude.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'No fields specified. Please add a field to exclude from comparison',
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
keys = Object.keys(flattenKeys(items[0].json));
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
keys = keys.filter((key) => !fieldsToExclude.includes(key));
|
|
|
|
}
|
|
|
|
if (compare === 'selectedFields') {
|
|
|
|
const fieldsToCompare = (
|
|
|
|
this.getNodeParameter('fieldsToCompare.fields', 0, []) as [{ fieldName: string }]
|
|
|
|
).map((field) => field.fieldName);
|
2021-09-15 00:55:36 -07:00
|
|
|
if (!fieldsToCompare.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'No fields specified. Please add a field to compare on',
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
keys = Object.keys(flattenKeys(items[0].json));
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
keys = fieldsToCompare.map((key) => key.trim());
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
2022-06-03 08:25:07 -07:00
|
|
|
|
2021-09-15 00:55:36 -07:00
|
|
|
// This solution is O(nlogn)
|
|
|
|
// add original index to the items
|
2022-08-17 08:50:24 -07:00
|
|
|
const newItems = items.map(
|
|
|
|
(item, index) =>
|
|
|
|
({
|
2022-12-02 12:54:28 -08:00
|
|
|
json: { ...item.json, __INDEX: index },
|
2022-08-17 08:50:24 -07:00
|
|
|
pairedItem: { item: index },
|
|
|
|
} as INodeExecutionData),
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
//sort items using the compare keys
|
|
|
|
newItems.sort((a, b) => {
|
|
|
|
let result = 0;
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
let equal;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
equal = isEqual(get(a.json, key), get(b.json, key));
|
|
|
|
} else {
|
|
|
|
equal = isEqual(a.json[key], b.json[key]);
|
|
|
|
}
|
|
|
|
if (!equal) {
|
|
|
|
let lessThan;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
lessThan = lt(get(a.json, key), get(b.json, key));
|
|
|
|
} else {
|
|
|
|
lessThan = lt(a.json[key], b.json[key]);
|
|
|
|
}
|
|
|
|
result = lessThan ? -1 : 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const key of keys) {
|
|
|
|
let type: any = undefined;
|
|
|
|
for (const item of newItems) {
|
|
|
|
if (key === '') {
|
2022-12-29 03:20:43 -08:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Name of field to compare is blank');
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
const value = !disableDotNotation ? get(item.json, key) : item.json[key];
|
2021-09-15 00:55:36 -07:00
|
|
|
if (value === undefined && disableDotNotation && key.includes('.')) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`'${key}' field is missing from some input items`,
|
|
|
|
{
|
2022-12-29 03:20:43 -08:00
|
|
|
description:
|
|
|
|
"If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options",
|
2022-08-17 08:50:24 -07:00
|
|
|
},
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
} else if (value === undefined) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`'${key}' field is missing from some input items`,
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
if (type !== undefined && value !== undefined && type !== typeof value) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), `'${key}' isn't always the same type`, {
|
|
|
|
description: 'The type of this field varies between items',
|
|
|
|
});
|
2021-09-15 00:55:36 -07:00
|
|
|
} else {
|
|
|
|
type = typeof value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect the original indexes of items to be removed
|
|
|
|
const removedIndexes: number[] = [];
|
|
|
|
let temp = newItems[0];
|
|
|
|
for (let index = 1; index < newItems.length; index++) {
|
|
|
|
if (compareItems(newItems[index], temp, keys, disableDotNotation, this.getNode())) {
|
|
|
|
removedIndexes.push(newItems[index].json.__INDEX as unknown as number);
|
|
|
|
} else {
|
|
|
|
temp = newItems[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let data = items.filter((_, index) => !removedIndexes.includes(index));
|
|
|
|
|
|
|
|
if (removeOtherFields) {
|
2022-08-17 08:50:24 -07:00
|
|
|
data = data.map((item, index) => ({
|
|
|
|
json: pick(item.json, ...keys),
|
|
|
|
pairedItem: { item: index },
|
|
|
|
}));
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the filtered items
|
|
|
|
return this.prepareOutputData(data);
|
|
|
|
} else if (operation === 'sort') {
|
|
|
|
let newItems = [...items];
|
|
|
|
const type = this.getNodeParameter('type', 0) as string;
|
2022-08-17 08:50:24 -07:00
|
|
|
const disableDotNotation = this.getNodeParameter(
|
|
|
|
'options.disableDotNotation',
|
|
|
|
0,
|
|
|
|
false,
|
|
|
|
) as boolean;
|
2021-09-15 00:55:36 -07:00
|
|
|
|
|
|
|
if (type === 'random') {
|
|
|
|
shuffleArray(newItems);
|
|
|
|
return this.prepareOutputData(newItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'simple') {
|
|
|
|
const sortFieldsUi = this.getNodeParameter('sortFieldsUi', 0) as IDataObject;
|
|
|
|
const sortFields = sortFieldsUi.sortField as Array<{
|
|
|
|
fieldName: string;
|
2022-08-17 08:50:24 -07:00
|
|
|
order: 'ascending' | 'descending';
|
2021-09-15 00:55:36 -07:00
|
|
|
}>;
|
2021-12-23 04:30:35 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!sortFields?.length) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
'No sorting specified. Please add a field to sort by',
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const { fieldName } of sortFields) {
|
|
|
|
let found = false;
|
|
|
|
for (const item of items) {
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2021-09-15 00:55:36 -07:00
|
|
|
if (get(item.json, fieldName) !== undefined) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
} else if (item.json.hasOwnProperty(fieldName)) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!found && disableDotNotation && fieldName.includes('.')) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Couldn't find the field '${fieldName}' in the input data`,
|
|
|
|
{
|
2022-12-29 03:20:43 -08:00
|
|
|
description:
|
|
|
|
"If you're trying to use a nested field, make sure you turn off 'disable dot notation' in the node options",
|
2022-08-17 08:50:24 -07:00
|
|
|
},
|
|
|
|
);
|
2022-12-02 12:54:28 -08:00
|
|
|
} else if (!found) {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`Couldn't find the field '${fieldName}' in the input data`,
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
const sortFieldsWithDirection = sortFields.map((field) => ({
|
|
|
|
name: field.fieldName,
|
|
|
|
dir: field.order === 'ascending' ? 1 : -1,
|
|
|
|
}));
|
2021-09-15 00:55:36 -07:00
|
|
|
|
|
|
|
newItems.sort((a, b) => {
|
|
|
|
let result = 0;
|
|
|
|
for (const field of sortFieldsWithDirection) {
|
|
|
|
let equal;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const _a =
|
|
|
|
typeof get(a.json, field.name) === 'string'
|
|
|
|
? (get(a.json, field.name) as string).toLowerCase()
|
|
|
|
: get(a.json, field.name);
|
|
|
|
const _b =
|
|
|
|
typeof get(b.json, field.name) === 'string'
|
|
|
|
? (get(b.json, field.name) as string).toLowerCase()
|
|
|
|
: get(b.json, field.name);
|
2021-09-15 00:55:36 -07:00
|
|
|
equal = isEqual(_a, _b);
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const _a =
|
2022-12-02 12:54:28 -08:00
|
|
|
typeof a.json[field.name] === 'string'
|
|
|
|
? (a.json[field.name] as string).toLowerCase()
|
|
|
|
: a.json[field.name];
|
2022-08-17 08:50:24 -07:00
|
|
|
const _b =
|
2022-12-02 12:54:28 -08:00
|
|
|
typeof b.json[field.name] === 'string'
|
|
|
|
? (b.json[field.name] as string).toLowerCase()
|
|
|
|
: b.json[field.name];
|
2021-09-15 00:55:36 -07:00
|
|
|
equal = isEqual(_a, _b);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!equal) {
|
|
|
|
let lessThan;
|
2022-12-02 12:54:28 -08:00
|
|
|
if (!disableDotNotation) {
|
2022-08-17 08:50:24 -07:00
|
|
|
const _a =
|
|
|
|
typeof get(a.json, field.name) === 'string'
|
|
|
|
? (get(a.json, field.name) as string).toLowerCase()
|
|
|
|
: get(a.json, field.name);
|
|
|
|
const _b =
|
|
|
|
typeof get(b.json, field.name) === 'string'
|
|
|
|
? (get(b.json, field.name) as string).toLowerCase()
|
|
|
|
: get(b.json, field.name);
|
2021-09-15 00:55:36 -07:00
|
|
|
lessThan = lt(_a, _b);
|
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
const _a =
|
2022-12-02 12:54:28 -08:00
|
|
|
typeof a.json[field.name] === 'string'
|
|
|
|
? (a.json[field.name] as string).toLowerCase()
|
|
|
|
: a.json[field.name];
|
2022-08-17 08:50:24 -07:00
|
|
|
const _b =
|
2022-12-02 12:54:28 -08:00
|
|
|
typeof b.json[field.name] === 'string'
|
|
|
|
? (b.json[field.name] as string).toLowerCase()
|
|
|
|
: b.json[field.name];
|
2021-09-15 00:55:36 -07:00
|
|
|
lessThan = lt(_a, _b);
|
|
|
|
}
|
|
|
|
if (lessThan) {
|
|
|
|
result = -1 * field.dir;
|
|
|
|
} else {
|
|
|
|
result = 1 * field.dir;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
const code = this.getNodeParameter('code', 0) as string;
|
|
|
|
const regexCheck = /\breturn\b/g.exec(code);
|
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
if (regexCheck?.length) {
|
2021-09-15 00:55:36 -07:00
|
|
|
const sandbox = {
|
|
|
|
newItems,
|
|
|
|
};
|
|
|
|
const mode = this.getMode();
|
|
|
|
const options = {
|
2022-08-17 08:50:24 -07:00
|
|
|
console: mode === 'manual' ? 'redirect' : 'inherit',
|
2021-09-15 00:55:36 -07:00
|
|
|
sandbox,
|
|
|
|
};
|
2023-01-13 09:11:56 -08:00
|
|
|
const vm = new NodeVM(options as unknown as NodeVMOptions);
|
2021-09-15 00:55:36 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
newItems = await vm.run(
|
|
|
|
`
|
2021-09-15 00:55:36 -07:00
|
|
|
module.exports = async function() {
|
|
|
|
newItems.sort( (a,b) => {
|
|
|
|
${code}
|
|
|
|
})
|
|
|
|
return newItems;
|
2022-08-17 08:50:24 -07:00
|
|
|
}()`,
|
|
|
|
__dirname,
|
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
} else {
|
2022-08-17 08:50:24 -07:00
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
2022-12-29 03:20:43 -08:00
|
|
|
"Sort code doesn't return. Please add a 'return' statement to your code",
|
2022-08-17 08:50:24 -07:00
|
|
|
);
|
2021-09-15 00:55:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.prepareOutputData(newItems);
|
|
|
|
} else if (operation === 'limit') {
|
|
|
|
let newItems = items;
|
|
|
|
const maxItems = this.getNodeParameter('maxItems', 0) as number;
|
|
|
|
const keep = this.getNodeParameter('keep', 0) as string;
|
|
|
|
|
|
|
|
if (maxItems > items.length) {
|
|
|
|
return this.prepareOutputData(newItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keep === 'firstItems') {
|
|
|
|
newItems = items.slice(0, maxItems);
|
|
|
|
} else {
|
|
|
|
newItems = items.slice(items.length - maxItems, items.length);
|
|
|
|
}
|
|
|
|
return this.prepareOutputData(newItems);
|
2023-01-17 08:40:28 -08:00
|
|
|
} else if (operation === 'summarize') {
|
|
|
|
return summarize.execute.call(this, items);
|
2021-09-15 00:55:36 -07:00
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), `Operation '${operation}' is not recognized`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new NodeOperationError(this.getNode(), `Resource '${resource}' is not recognized`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|