mirror of
https://github.com/n8n-io/n8n.git
synced 2026-05-27 23:07:12 +02:00
fix(PostgreSQL Node): Input items with array being modified (#22426)
This commit is contained in:
parent
79ea109581
commit
42cda59ee5
|
|
@ -535,9 +535,9 @@ describe('Test PostgresV2, convertArraysToPostgresFormat', () => {
|
|||
},
|
||||
];
|
||||
|
||||
convertArraysToPostgresFormat(item, schema, node, 0);
|
||||
const result = convertArraysToPostgresFormat(item, schema, node, 0);
|
||||
|
||||
expect(item).toEqual({
|
||||
expect(result).toEqual({
|
||||
jsonb_array: '{"{\\"key\\":\\"value44\\"}"}',
|
||||
json_array: '{"{\\"key\\":\\"value54\\"}"}',
|
||||
int_array: '{1,2,5}',
|
||||
|
|
@ -545,4 +545,29 @@ describe('Test PostgresV2, convertArraysToPostgresFormat', () => {
|
|||
bool_array: '{"true","false"}',
|
||||
});
|
||||
});
|
||||
|
||||
it('should not modify the original data object', () => {
|
||||
const referenceItem = {
|
||||
arr: [1, 2, 3],
|
||||
};
|
||||
const item = {
|
||||
arr: [1, 2, 3],
|
||||
};
|
||||
const schema: ColumnInfo[] = [
|
||||
{
|
||||
column_name: 'arr',
|
||||
data_type: 'ARRAY',
|
||||
is_nullable: 'YES',
|
||||
udt_name: '_int4',
|
||||
column_default: null,
|
||||
},
|
||||
];
|
||||
|
||||
const result = convertArraysToPostgresFormat(item, schema, node, 0);
|
||||
|
||||
expect(result).toEqual({
|
||||
arr: '{1,2,3}',
|
||||
});
|
||||
expect(item).toEqual(referenceItem);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -234,7 +234,7 @@ export async function execute(
|
|||
tableSchema = await updateTableSchema(db, tableSchema, schema, table);
|
||||
|
||||
if (nodeVersion >= 2.4) {
|
||||
convertArraysToPostgresFormat(item, tableSchema, this.getNode(), i);
|
||||
item = convertArraysToPostgresFormat(item, tableSchema, this.getNode(), i);
|
||||
}
|
||||
|
||||
values.push(checkItemAgainstSchema(this.getNode(), item, tableSchema, i));
|
||||
|
|
|
|||
|
|
@ -302,7 +302,7 @@ export async function execute(
|
|||
tableSchema = await updateTableSchema(db, tableSchema, schema, table);
|
||||
|
||||
if (nodeVersion >= 2.4) {
|
||||
convertArraysToPostgresFormat(item, tableSchema, this.getNode(), i);
|
||||
item = convertArraysToPostgresFormat(item, tableSchema, this.getNode(), i);
|
||||
}
|
||||
|
||||
item = checkItemAgainstSchema(this.getNode(), item, tableSchema, i);
|
||||
|
|
|
|||
|
|
@ -271,7 +271,7 @@ export async function execute(
|
|||
tableSchema = await updateTableSchema(db, tableSchema, schema, table);
|
||||
|
||||
if (nodeVersion >= 2.4) {
|
||||
convertArraysToPostgresFormat(item, tableSchema, this.getNode(), i);
|
||||
item = convertArraysToPostgresFormat(item, tableSchema, this.getNode(), i);
|
||||
}
|
||||
|
||||
item = checkItemAgainstSchema(this.getNode(), item, tableSchema, i);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import type {
|
|||
INodePropertyOptions,
|
||||
NodeParameterValueType,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeOperationError, jsonParse } from 'n8n-workflow';
|
||||
import { NodeOperationError, deepCopy, jsonParse } from 'n8n-workflow';
|
||||
|
||||
import type {
|
||||
ColumnInfo,
|
||||
|
|
@ -570,6 +570,7 @@ export const configureTableSchemaUpdater = (initialSchema: string, initialTable:
|
|||
* @param schema table schema
|
||||
* @param node INode
|
||||
* @param itemIndex the index of the current item
|
||||
* @returns a new data object with the arrays converted to postgres format
|
||||
*/
|
||||
export const convertArraysToPostgresFormat = (
|
||||
data: IDataObject,
|
||||
|
|
@ -577,10 +578,11 @@ export const convertArraysToPostgresFormat = (
|
|||
node: INode,
|
||||
itemIndex = 0,
|
||||
) => {
|
||||
const newData = deepCopy(data);
|
||||
for (const columnInfo of schema) {
|
||||
//in case column type is array we need to convert it to fornmat that postgres understands
|
||||
// in case column type is array we need to convert it to fornmat that postgres understands
|
||||
if (columnInfo.data_type.toUpperCase() === 'ARRAY') {
|
||||
let columnValue = data[columnInfo.column_name];
|
||||
let columnValue = newData[columnInfo.column_name];
|
||||
|
||||
if (typeof columnValue === 'string') {
|
||||
columnValue = jsonParse(columnValue);
|
||||
|
|
@ -607,8 +609,8 @@ export const convertArraysToPostgresFormat = (
|
|||
return entry;
|
||||
});
|
||||
|
||||
//wrap in {} instead of [] as postgres does and join with ,
|
||||
data[columnInfo.column_name] = `{${arrayEntries.join(',')}}`;
|
||||
// wrap in {} instead of [] as postgres does and join with ,
|
||||
newData[columnInfo.column_name] = `{${arrayEntries.join(',')}}`;
|
||||
} else {
|
||||
if (columnInfo.is_nullable === 'NO') {
|
||||
throw new NodeOperationError(
|
||||
|
|
@ -622,4 +624,6 @@ export const convertArraysToPostgresFormat = (
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return newData;
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user