fix(Postgres Node): Fix for tables containing field named json

This commit is contained in:
Michael Kret 2023-02-21 14:16:18 +02:00 committed by GitHub
parent f58573dba3
commit 5d74a2f89a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 13 deletions

View file

@ -71,6 +71,15 @@ export function generateReturning(pgp: pgPromise.IMain<{}, pg.IClient>, returnin
);
}
export function wrapData(data: IDataObject[]): INodeExecutionData[] {
if (!Array.isArray(data)) {
return [{ json: data }];
}
return data.map((item) => ({
json: item,
}));
}
/**
* Executes the given SQL query on the database.
*
@ -185,7 +194,7 @@ export async function pgQueryV2(
if (mode === 'multiple') {
return (await db.multi(pgp.helpers.concat(allQueries)))
.map((result, i) => {
return this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(result), {
return this.helpers.constructExecutionMetaData(wrapData(result), {
itemData: { item: i },
});
})
@ -197,7 +206,7 @@ export async function pgQueryV2(
try {
const transactionResult = await t.any(allQueries[i].query, allQueries[i].values);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(transactionResult),
wrapData(transactionResult),
{ itemData: { item: i } },
);
result.push(...executionData);
@ -221,7 +230,7 @@ export async function pgQueryV2(
try {
const transactionResult = await t.any(allQueries[i].query, allQueries[i].values);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(transactionResult),
wrapData(transactionResult),
{ itemData: { item: i } },
);
result.push(...executionData);
@ -373,7 +382,7 @@ export async function pgInsertV2(
const queryResult = await db.any(query);
return queryResult
.map((result, i) => {
return this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(result), {
return this.helpers.constructExecutionMetaData(wrapData(result), {
itemData: { item: i },
});
})
@ -386,7 +395,7 @@ export async function pgInsertV2(
try {
const insertResult = await t.one(pgp.helpers.insert(itemCopy, cs) + returning);
result.push(
...this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(insertResult), {
...this.helpers.constructExecutionMetaData(wrapData(insertResult), {
itemData: { item: i },
}),
);
@ -411,10 +420,9 @@ export async function pgInsertV2(
try {
const insertResult = await t.oneOrNone(pgp.helpers.insert(itemCopy, cs) + returning);
if (insertResult !== null) {
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(insertResult),
{ itemData: { item: i } },
);
const executionData = this.helpers.constructExecutionMetaData(wrapData(insertResult), {
itemData: { item: i },
});
result.push(...executionData);
}
} catch (err) {
@ -644,7 +652,7 @@ export async function pgUpdateV2(
returning,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(transactionResult),
wrapData(transactionResult),
{ itemData: { item: i } },
);
result.push(...executionData);
@ -672,7 +680,7 @@ export async function pgUpdateV2(
returning,
);
const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(independentResult),
wrapData(independentResult),
{ itemData: { item: i } },
);
result.push(...executionData);

View file

@ -12,7 +12,7 @@ import { NodeOperationError } from 'n8n-workflow';
import pgPromise from 'pg-promise';
import { pgInsertV2, pgQueryV2, pgUpdate } from './Postgres.node.functions';
import { pgInsertV2, pgQueryV2, pgUpdate, wrapData } from './Postgres.node.functions';
export class Postgres implements INodeType {
description: INodeTypeDescription = {
@ -389,7 +389,7 @@ export class Postgres implements INodeType {
this.continueOnFail(),
);
returnItems = this.helpers.returnJsonArray(updateItems);
returnItems = wrapData(updateItems);
} else {
pgp.end();
throw new NodeOperationError(