diff --git a/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts b/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts index 5999dc91f9..4aa41b87a8 100644 --- a/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts +++ b/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts @@ -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); diff --git a/packages/nodes-base/nodes/Postgres/Postgres.node.ts b/packages/nodes-base/nodes/Postgres/Postgres.node.ts index a840cddb76..d93620741a 100644 --- a/packages/nodes-base/nodes/Postgres/Postgres.node.ts +++ b/packages/nodes-base/nodes/Postgres/Postgres.node.ts @@ -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(