2019-06-23 03:35:23 -07:00
import { IExecuteFunctions } from 'n8n-core' ;
import {
2022-10-21 08:24:58 -07:00
deepCopy ,
2019-06-23 03:35:23 -07:00
INodeExecutionData ,
INodeParameters ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
import { set } from 'lodash' ;
export class Set implements INodeType {
description : INodeTypeDescription = {
displayName : 'Set' ,
name : 'set' ,
2019-07-26 02:27:46 -07:00
icon : 'fa:pen' ,
2019-06-23 03:35:23 -07:00
group : [ 'input' ] ,
version : 1 ,
2021-07-03 05:40:16 -07:00
description : 'Sets values on items and optionally remove other values' ,
2019-06-23 03:35:23 -07:00
defaults : {
name : 'Set' ,
color : '#0000FF' ,
} ,
inputs : [ 'main' ] ,
outputs : [ 'main' ] ,
properties : [
{
displayName : 'Keep Only Set' ,
name : 'keepOnlySet' ,
type : 'boolean' ,
default : false ,
2022-08-17 08:50:24 -07:00
description :
'Whether only the values set on this node should be kept and all others removed' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Values to Set' ,
name : 'values' ,
placeholder : 'Add Value' ,
type : 'fixedCollection' ,
typeOptions : {
multipleValues : true ,
2021-02-15 00:53:19 -08:00
sortable : true ,
2019-06-23 03:35:23 -07:00
} ,
2022-05-06 14:01:25 -07:00
description : 'The value to set' ,
2019-06-23 03:35:23 -07:00
default : { } ,
options : [
{
name : 'boolean' ,
displayName : 'Boolean' ,
values : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : 'propertyName' ,
2022-08-17 08:50:24 -07:00
description :
'Name of the property to write data to. Supports dot-notation. Example: "data.person[0].name"' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'boolean' ,
default : false ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-05-06 14:01:25 -07:00
description : 'The boolean value to write in the property' ,
2019-06-23 03:35:23 -07:00
} ,
2020-10-22 06:46:03 -07:00
] ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'number' ,
displayName : 'Number' ,
values : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : 'propertyName' ,
2022-08-17 08:50:24 -07:00
description :
'Name of the property to write data to. Supports dot-notation. Example: "data.person[0].name"' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'number' ,
default : 0 ,
2022-05-06 14:01:25 -07:00
description : 'The number value to write in the property' ,
2019-06-23 03:35:23 -07:00
} ,
2020-10-22 06:46:03 -07:00
] ,
2019-06-23 03:35:23 -07:00
} ,
{
name : 'string' ,
displayName : 'String' ,
values : [
{
displayName : 'Name' ,
name : 'name' ,
type : 'string' ,
default : 'propertyName' ,
2022-08-17 08:50:24 -07:00
description :
'Name of the property to write data to. Supports dot-notation. Example: "data.person[0].name"' ,
2019-06-23 03:35:23 -07:00
} ,
{
displayName : 'Value' ,
name : 'value' ,
type : 'string' ,
default : '' ,
2022-05-06 14:01:25 -07:00
description : 'The string value to write in the property' ,
2019-06-23 03:35:23 -07:00
} ,
2020-10-22 06:46:03 -07:00
] ,
2019-06-23 03:35:23 -07:00
} ,
] ,
} ,
2020-03-21 09:43:23 -07:00
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
placeholder : 'Add Option' ,
default : { } ,
options : [
{
displayName : 'Dot Notation' ,
name : 'dotNotation' ,
type : 'boolean' ,
default : true ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-param-description-boolean-without-whether
2022-08-17 08:50:24 -07:00
description :
'<p>By default, dot-notation is used in property names. This means that "a.b" will set the property "b" underneath "a" so { "a": { "b": value} }.<p></p>If that is not intended this can be deactivated, it will then set { "a.b": value } instead.</p>.' ,
2020-03-21 09:43:23 -07:00
} ,
] ,
} ,
2020-10-22 06:46:03 -07:00
] ,
2019-06-23 03:35:23 -07:00
} ;
2022-12-02 12:54:28 -08:00
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
2019-06-23 03:35:23 -07:00
const items = this . getInputData ( ) ;
if ( items . length === 0 ) {
2022-08-17 08:50:24 -07:00
items . push ( { json : { } } ) ;
2019-06-23 03:35:23 -07:00
}
2019-08-01 13:55:33 -07:00
const returnData : INodeExecutionData [ ] = [ ] ;
2019-06-23 03:35:23 -07:00
let item : INodeExecutionData ;
let keepOnlySet : boolean ;
for ( let itemIndex = 0 ; itemIndex < items . length ; itemIndex ++ ) {
2021-03-08 09:45:35 -08:00
keepOnlySet = this . getNodeParameter ( 'keepOnlySet' , itemIndex , false ) as boolean ;
2019-06-23 03:35:23 -07:00
item = items [ itemIndex ] ;
2022-12-02 12:54:28 -08:00
const options = this . getNodeParameter ( 'options' , itemIndex , { } ) ;
2019-06-23 03:35:23 -07:00
2019-08-01 13:55:33 -07:00
const newItem : INodeExecutionData = {
json : { } ,
2022-06-03 08:25:07 -07:00
pairedItem : item.pairedItem ,
2019-08-01 13:55:33 -07:00
} ;
2022-12-02 12:54:28 -08:00
if ( ! keepOnlySet ) {
2019-08-01 13:55:33 -07:00
if ( item . binary !== undefined ) {
// Create a shallow copy of the binary data so that the old
// data references which do not get changed still stay behind
// but the incoming data does not get changed.
newItem . binary = { } ;
Object . assign ( newItem . binary , item . binary ) ;
}
2022-10-21 08:24:58 -07:00
newItem . json = deepCopy ( item . json ) ;
2019-06-23 03:35:23 -07:00
}
// Add boolean values
2022-08-17 08:50:24 -07:00
( this . getNodeParameter ( 'values.boolean' , itemIndex , [ ] ) as INodeParameters [ ] ) . forEach (
( setItem ) = > {
if ( options . dotNotation === false ) {
newItem . json [ setItem . name as string ] = ! ! setItem . value ;
} else {
set ( newItem . json , setItem . name as string , ! ! setItem . value ) ;
}
} ,
) ;
2019-06-23 03:35:23 -07:00
// Add number values
2022-08-17 08:50:24 -07:00
( this . getNodeParameter ( 'values.number' , itemIndex , [ ] ) as INodeParameters [ ] ) . forEach (
( setItem ) = > {
if ( options . dotNotation === false ) {
newItem . json [ setItem . name as string ] = setItem . value ;
} else {
set ( newItem . json , setItem . name as string , setItem . value ) ;
}
} ,
) ;
2019-06-23 03:35:23 -07:00
// Add string values
2022-08-17 08:50:24 -07:00
( this . getNodeParameter ( 'values.string' , itemIndex , [ ] ) as INodeParameters [ ] ) . forEach (
( setItem ) = > {
if ( options . dotNotation === false ) {
newItem . json [ setItem . name as string ] = setItem . value ;
} else {
set ( newItem . json , setItem . name as string , setItem . value ) ;
}
} ,
) ;
2019-08-01 13:55:33 -07:00
returnData . push ( newItem ) ;
2019-06-23 03:35:23 -07:00
}
2019-08-01 13:55:33 -07:00
return this . prepareOutputData ( returnData ) ;
2019-06-23 03:35:23 -07:00
}
}