fix(Postgres Node): Allow passing in arrays to JSON columns for insert (#12452)

This commit is contained in:
Dana
2025-01-08 10:41:51 +01:00
committed by GitHub
parent 2c72047d0b
commit 9dd068632b
5 changed files with 185 additions and 10 deletions

View File

@@ -6,6 +6,7 @@ import type {
INode,
INodeParameters,
} from 'n8n-workflow';
import pgPromise from 'pg-promise';
import * as deleteTable from '../../v2/actions/database/deleteTable.operation';
import * as executeQuery from '../../v2/actions/database/executeQuery.operation';
@@ -506,6 +507,7 @@ describe('Test PostgresV2, insert operation', () => {
items,
nodeOptions,
createMockDb(columnsInfo),
pgPromise(),
);
expect(runQueries).toHaveBeenCalledWith(
@@ -579,6 +581,7 @@ describe('Test PostgresV2, insert operation', () => {
inputItems,
nodeOptions,
createMockDb(columnsInfo),
pgPromise(),
);
expect(runQueries).toHaveBeenCalledWith(
@@ -600,6 +603,96 @@ describe('Test PostgresV2, insert operation', () => {
nodeOptions,
);
});
it('dataMode: define, should accept an array with values if column is of type json', async () => {
const convertValuesToJsonWithPgpSpy = jest.spyOn(utils, 'convertValuesToJsonWithPgp');
const hasJsonDataTypeInSchemaSpy = jest.spyOn(utils, 'hasJsonDataTypeInSchema');
const values = [
{ value: { id: 1, json: [], foo: 'data 1' }, expected: { id: 1, json: '[]', foo: 'data 1' } },
{
value: {
id: 2,
json: [0, 1],
foo: 'data 2',
},
expected: {
id: 2,
json: '[0,1]',
foo: 'data 2',
},
},
{
value: {
id: 2,
json: [0],
foo: 'data 2',
},
expected: {
id: 2,
json: '[0]',
foo: 'data 2',
},
},
];
values.forEach(async (value) => {
const valuePassedIn = value.value;
const nodeParameters: IDataObject = {
schema: {
__rl: true,
mode: 'list',
value: 'public',
},
table: {
__rl: true,
value: 'my_table',
mode: 'list',
},
columns: {
mappingMode: 'defineBelow',
value: valuePassedIn,
},
options: { nodeVersion: 2.5 },
};
const columnsInfo: ColumnInfo[] = [
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO', udt_name: '' },
{ column_name: 'json', data_type: 'json', is_nullable: 'NO', udt_name: '' },
{ column_name: 'foo', data_type: 'text', is_nullable: 'NO', udt_name: '' },
];
const inputItems = [
{
json: valuePassedIn,
},
];
const nodeOptions = nodeParameters.options as IDataObject;
const pg = pgPromise();
await insert.execute.call(
createMockExecuteFunction(nodeParameters),
runQueries,
inputItems,
nodeOptions,
createMockDb(columnsInfo),
pg,
);
expect(runQueries).toHaveBeenCalledWith(
[
{
query: 'INSERT INTO $1:name.$2:name($3:name) VALUES($3:csv) RETURNING *',
values: ['public', 'my_table', value.expected],
},
],
inputItems,
nodeOptions,
);
expect(convertValuesToJsonWithPgpSpy).toHaveBeenCalledWith(pg, columnsInfo, valuePassedIn);
expect(hasJsonDataTypeInSchemaSpy).toHaveBeenCalledWith(columnsInfo);
});
});
});
describe('Test PostgresV2, select operation', () => {