mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 11:01:15 +00:00
fix(Postgres Node): Fix inserting null or undefined in type=json columns (#14672)
This commit is contained in:
@@ -429,36 +429,47 @@ describe('Test PostgresV2, hasJsonDataType', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('Test PostgresV2, convertValuesToJsonWithPgp', () => {
|
describe('Test PostgresV2, convertValuesToJsonWithPgp', () => {
|
||||||
it('should use pgp to properly convert values to JSON', () => {
|
const pgp = pgPromise();
|
||||||
const pgp = pgPromise();
|
const pgpJsonSpy = jest.spyOn(pgp.as, 'json');
|
||||||
const pgpJsonSpy = jest.spyOn(pgp.as, 'json');
|
const schema: ColumnInfo[] = [
|
||||||
|
{ column_name: 'data', data_type: 'json', is_nullable: 'YES' },
|
||||||
|
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' },
|
||||||
|
];
|
||||||
|
|
||||||
const schema: ColumnInfo[] = [
|
beforeEach(() => {
|
||||||
{ column_name: 'data', data_type: 'json', is_nullable: 'YES' },
|
pgpJsonSpy.mockClear();
|
||||||
{ column_name: 'id', data_type: 'integer', is_nullable: 'NO' },
|
});
|
||||||
];
|
|
||||||
const values = [
|
|
||||||
{
|
|
||||||
value: { data: [], id: 1 },
|
|
||||||
expected: { data: '[]', id: 1 },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: { data: [0], id: 1 },
|
|
||||||
expected: { data: '[0]', id: 1 },
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: { data: { key: 2 }, id: 1 },
|
|
||||||
expected: { data: '{"key":2}', id: 1 },
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
values.forEach((value) => {
|
it.each([
|
||||||
const data = value.value.data;
|
{
|
||||||
|
value: { data: [], id: 1 },
|
||||||
expect(convertValuesToJsonWithPgp(pgp, schema, value.value)).toEqual(value.expected);
|
expected: { data: '[]', id: 1 },
|
||||||
expect(value.value).toEqual(value.expected);
|
},
|
||||||
|
{
|
||||||
|
value: { data: [0], id: 1 },
|
||||||
|
expected: { data: '[0]', id: 1 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: { data: { key: 2 }, id: 1 },
|
||||||
|
expected: { data: '{"key":2}', id: 1 },
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: { data: null, id: 1 },
|
||||||
|
expected: { data: null, id: 1 },
|
||||||
|
shouldSkipPgp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: { data: undefined, id: 1 },
|
||||||
|
expected: { data: undefined, id: 1 },
|
||||||
|
shouldSkipPgp: true,
|
||||||
|
},
|
||||||
|
])('should convert $value.data to json correctly', ({ value, expected, shouldSkipPgp }) => {
|
||||||
|
const data = value.data;
|
||||||
|
expect(convertValuesToJsonWithPgp(pgp, schema, value)).toEqual(expected);
|
||||||
|
expect(value).toEqual(expected);
|
||||||
|
if (!shouldSkipPgp) {
|
||||||
expect(pgpJsonSpy).toHaveBeenCalledWith(data, true);
|
expect(pgpJsonSpy).toHaveBeenCalledWith(data, true);
|
||||||
});
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -414,7 +414,10 @@ export function convertValuesToJsonWithPgp(
|
|||||||
values: IDataObject,
|
values: IDataObject,
|
||||||
) {
|
) {
|
||||||
schema
|
schema
|
||||||
.filter(({ data_type }: { data_type: string }) => data_type === 'json')
|
.filter(
|
||||||
|
({ data_type, column_name }) =>
|
||||||
|
data_type === 'json' && values[column_name] !== null && values[column_name] !== undefined,
|
||||||
|
)
|
||||||
.forEach(({ column_name }) => {
|
.forEach(({ column_name }) => {
|
||||||
values[column_name] = pgp.as.json(values[column_name], true);
|
values[column_name] = pgp.as.json(values[column_name], true);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user