From 2fea79f5f11f8092aed1fe21d3bbc21567612d85 Mon Sep 17 00:00:00 2001 From: Ben Hesseldieck Date: Wed, 8 Jul 2020 14:36:40 +0200 Subject: [PATCH] :zap: extract update function --- .../nodes/Postgres/Postgres.node.functions.ts | 52 +++++++++++++++++-- .../nodes/Postgres/Postgres.node.ts | 38 ++------------ 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts b/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts index 83a5701bad..8c46ef7e3a 100644 --- a/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts +++ b/packages/nodes-base/nodes/Postgres/Postgres.node.functions.ts @@ -38,7 +38,7 @@ function getItemCopy( * @param {input[]} input The Node's input data * @returns Promise> */ -export function executeQuery( +export function pgQuery( getNodeParam: Function, pgp: pgPromise.IMain<{}, pg.IClient>, db: pgPromise.IDatabase<{}, pg.IClient>, @@ -53,16 +53,15 @@ export function executeQuery( } /** - * Returns of copy of the items which only contains the json data and - * of that only the define properties + * Inserts the given items into the database. * * @param {Function} getNodeParam The getter of the Node * @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance * @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection * @param {input[]} input The Node's input data - * @returns Promise + * @returns Promise> */ -export async function executeInsert( +export async function pgInsert( getNodeParam: Function, pgp: pgPromise.IMain<{}, pg.IClient>, db: pgPromise.IDatabase<{}, pg.IClient>, @@ -96,3 +95,46 @@ export async function executeInsert( return [insertData, insertItems]; } + +/** + * Updates the given items in the database. + * + * @param {Function} getNodeParam The getter of the Node + * @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance + * @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection + * @param {input[]} input The Node's input data + * @returns Promise> + */ +export async function pgUpdate( + getNodeParam: Function, + pgp: pgPromise.IMain<{}, pg.IClient>, + db: pgPromise.IDatabase<{}, pg.IClient>, + items: INodeExecutionData[], +): Promise> { + const table = getNodeParam('table', 0) as string; + const updateKey = getNodeParam('updateKey', 0) as string; + const columnString = getNodeParam('columns', 0) as string; + + const columns = columnString.split(',').map(column => column.trim()); + + // Make sure that the updateKey does also get queried + if (!columns.includes(updateKey)) { + columns.unshift(updateKey); + } + + // Prepare the data to update and copy it to be returned + const updateItems = getItemCopy(items, columns); + + // Generate the multi-row update query + const query = + pgp.helpers.update(updateItems, columns, table) + + ' WHERE v.' + + updateKey + + ' = t.' + + updateKey; + + // Executing the query to update the data + await db.none(query); + + return updateItems; +} diff --git a/packages/nodes-base/nodes/Postgres/Postgres.node.ts b/packages/nodes-base/nodes/Postgres/Postgres.node.ts index a7fe2b72d8..702799c5c2 100644 --- a/packages/nodes-base/nodes/Postgres/Postgres.node.ts +++ b/packages/nodes-base/nodes/Postgres/Postgres.node.ts @@ -8,7 +8,7 @@ import { import * as pgPromise from 'pg-promise'; -import { executeInsert, executeQuery } from './Postgres.node.functions'; +import { pgInsert, pgQuery, pgUpdate } from './Postgres.node.functions'; export class Postgres implements INodeType { description: INodeTypeDescription = { @@ -214,12 +214,7 @@ export class Postgres implements INodeType { // executeQuery // ---------------------------------- - const queryResult = await executeQuery( - this.getNodeParameter, - pgp, - db, - items, - ); + const queryResult = await pgQuery(this.getNodeParameter, pgp, db, items); returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); } else if (operation === 'insert') { @@ -227,7 +222,7 @@ export class Postgres implements INodeType { // insert // ---------------------------------- - const [insertData, insertItems] = await executeInsert( + const [insertData, insertItems] = await pgInsert( this.getNodeParameter, pgp, db, @@ -248,32 +243,9 @@ export class Postgres implements INodeType { // update // ---------------------------------- - const table = this.getNodeParameter('table', 0) as string; - const updateKey = this.getNodeParameter('updateKey', 0) as string; - const columnString = this.getNodeParameter('columns', 0) as string; + const updateItems = await pgUpdate(this.getNodeParameter, pgp, db, items); - const columns = columnString.split(',').map(column => column.trim()); - - // // Make sure that the updateKey does also get queried - // if (!columns.includes(updateKey)) { - // columns.unshift(updateKey); - // } - - // // Prepare the data to update and copy it to be returned - // const updateItems = getItemCopy(items, columns); - - // // Generate the multi-row update query - // const query = - // pgp.helpers.update(updateItems, columns, table) + - // ' WHERE v.' + - // updateKey + - // ' = t.' + - // updateKey; - - // // Executing the query to update the data - // await db.none(query); - - // returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]); + returnItems = this.helpers.returnJsonArray(updateItems); } else { await pgp.end(); throw new Error(`The operation "${operation}" is not supported!`);