Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Summarize Node): Preserves original field data type #12999

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/nodes-base/nodes/Transform/Summarize/Summarize.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import {
type INodeTypeDescription,
NodeExecutionOutput,
type NodeExecutionHint,
type IDataObject,
} from 'n8n-workflow';

import {
type Aggregations,
NUMERICAL_AGGREGATIONS,
type SummarizeOptions,
aggregationToArray,
aggregationToArrayWithOriginalTypes,
checkIfFieldExists,
fieldValueGetter,
splitData,
Expand Down Expand Up @@ -362,7 +364,14 @@ export class Summarize implements INodeType {
};
return new NodeExecutionOutput([[executionData]], fieldsNotFound);
}
const returnData = aggregationToArray(aggregationResult, fieldsToSplitBy);

let returnData: IDataObject[] = [];
if (nodeVersion > 1) {
returnData = aggregationToArrayWithOriginalTypes(aggregationResult, fieldsToSplitBy);
} else {
returnData = aggregationToArray(aggregationResult, fieldsToSplitBy);
}

const executionData = returnData.map((item) => {
const { pairedItems, ...json } = item;
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
{
"name": "Summarize Test",
"nodes": [
{
"parameters": {},
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [-320, -80],
"id": "509c50eb-a5c3-4172-a553-82cce7ca0455",
"name": "When clicking ‘Test workflow’"
},
{
"parameters": {
"jsCode": "return [\n {\n \"uid\": \"a0e797c2-ffcf-495b-96f3-982f7bc4eae5\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Charlie\",\n \"lastname\": \"Jacobi\",\n \"password\": \"34~XLKdGit\",\n \"Group\": 0\n },\n {\n \"uid\": \"5713682e-4f32-47dc-ae89-ae4a8ac28a46\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Leonard\",\n \"lastname\": \"Blick\",\n \"password\": \"4sh5z!R\",\n \"Group\": 0\n },\n {\n \"uid\": \"1bb2a432-d2fb-48e4-a7b0-39030ef0d784\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Debra\",\n \"lastname\": \"Corkery\",\n \"password\": \"-8X1CKMFh\",\n \"Group\": 1\n },\n {\n \"uid\": \"4edb11bc-9256-4f26-9be6-f8b04e90ecb2\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Wendy\",\n \"lastname\": \"Schiller\",\n \"password\": \"WF373t,fe\",\n \"Group\": 2\n },\n {\n \"uid\": \"ec6efd3d-508c-4f1e-acae-c9467b74f5a6\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Dolores\",\n \"lastname\": \"Walter\",\n \"password\": \".45KCBk\",\n \"Group\": 2\n },\n {\n \"uid\": \"173ee780-4e4f-4164-aa39-41a0402cd2ad\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Leonard\",\n \"lastname\": \"Krajcik\",\n \"password\": \"6x6-MN8vch\",\n \"Group\": null\n },\n {\n \"uid\": \"b1f191d8-4f95-49f6-8533-042048b4c162\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Terri\",\n \"lastname\": \"Kuphal\",\n \"password\": \"62+BNNCk\",\n \"Group\": null\n },\n {\n \"uid\": \"ee8c6d18-1e77-435d-8c9a-5215141d7697\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Clinton\",\n \"lastname\": \"Murphy\",\n \"password\": \"2+R8IRxHL\",\n \"Group\": null\n },\n {\n \"uid\": \"feeb4357-0e20-4b15-9199-f161c23da1a2\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Edward\",\n \"lastname\": \"Schaden\",\n \"password\": \"Kv23S1_H\",\n \"Group\": 0\n },\n {\n \"uid\": \"676003ec-8a40-4105-8237-ba1d9eb51fc2\",\n \"email\": \"[email protected]\",\n \"firstname\": \"Esther\",\n \"lastname\": \"Monahan\",\n \"password\": \"5q$RyBhp\",\n \"Group\": 2\n }\n]"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [-100, -80],
"id": "691f2744-8689-4b5c-91e7-2bb378691ecf",
"name": "sample data"
},
{
"parameters": {
"fieldsToSummarize": {
"values": [
{
"aggregation": "append",
"field": "email",
"includeEmpty": true
}
]
},
"fieldsToSplitBy": "Group",
"options": {
"outputFormat": "separateItems"
}
},
"type": "n8n-nodes-base.summarize",
"typeVersion": 1.1,
"position": [160, 0],
"id": "d1267da5-0cab-453c-9450-1bbd78346bd6",
"name": "Summarize v1.1"
},
{
"parameters": {
"fieldsToSummarize": {
"values": [
{
"aggregation": "append",
"field": "email",
"includeEmpty": true
}
]
},
"fieldsToSplitBy": "Group",
"options": {
"continueIfFieldNotFound": false,
"outputFormat": "separateItems"
}
},
"type": "n8n-nodes-base.summarize",
"typeVersion": 1,
"position": [160, -160],
"id": "680b7269-c920-4cce-b9f5-b2189955848d",
"name": "Summarize v1"
}
],
"pinData": {
"Summarize v1": [
{
"json": {
"Group": "0",
"appended_email": [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
},
{
"json": {
"Group": "1",
"appended_email": ["[email protected]"]
}
},
{
"json": {
"Group": "2",
"appended_email": [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
},
{
"json": {
"Group": "null",
"appended_email": [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
}
],
"Summarize v1.1": [
{
"json": {
"Group": 0,
"appended_email": [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
},
{
"json": {
"Group": 1,
"appended_email": ["[email protected]"]
}
},
{
"json": {
"Group": 2,
"appended_email": [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
},
{
"json": {
"Group": null,
"appended_email": [
"[email protected]",
"[email protected]",
"[email protected]"
]
}
}
]
},
"connections": {
"When clicking ‘Test workflow’": {
"main": [
[
{
"node": "sample data",
"type": "main",
"index": 0
}
]
]
},
"sample data": {
"main": [
[
{
"node": "Summarize v1",
"type": "main",
"index": 0
},
{
"node": "Summarize v1.1",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {
"executionOrder": "v1"
},
"versionId": "7304eae2-3bae-4934-8d1f-1135a0ff8ab8",
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "b0d1a7f453aa2195078ce658269afa7743e3dc59edb733395b6c75bde57da7ff"
},
"id": "8ja9H2443AhLdBsS",
"tags": []
}
36 changes: 36 additions & 0 deletions packages/nodes-base/nodes/Transform/Summarize/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNaN } from 'lodash';
import get from 'lodash/get';
import {
type IDataObject,
Expand Down Expand Up @@ -287,3 +288,38 @@ export function aggregationToArray(
return returnData;
}
}

export function aggregationToArrayWithOriginalTypes(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is too similar to aggregationToArray, I suggest you reuse the repeated parts to avoid code duplication

aggregationResult: IDataObject,
fieldsToSplitBy: string[],
previousStage: IDataObject = {},
) {
const returnData: IDataObject[] = [];
fieldsToSplitBy = parseFieldName(fieldsToSplitBy);
const splitFieldName = fieldsToSplitBy[0];
const isNext = fieldsToSplitBy[1];

if (isNext === undefined) {
for (const fieldName of Object.keys(aggregationResult)) {
const originalFieldValue =
fieldName === 'null' ? null : isNaN(Number(fieldName)) ? fieldName : Number(fieldName);
returnData.push({
...previousStage,
[splitFieldName]: originalFieldValue,
...(aggregationResult[fieldName] as IDataObject),
});
}
return returnData;
} else {
for (const key of Object.keys(aggregationResult)) {
const originalKeyValue = key === 'null' ? null : isNaN(Number(key)) ? key : Number(key);
returnData.push(
...aggregationToArray(aggregationResult[key] as IDataObject, fieldsToSplitBy.slice(1), {
...previousStage,
[splitFieldName]: originalKeyValue,
}),
);
}
return returnData;
}
}
Loading