From 4114f70027144ad22e63754634ea078f4cdf876d Mon Sep 17 00:00:00 2001 From: Rupenieks Date: Fri, 31 Jul 2020 15:24:43 +0100 Subject: [PATCH] :zap: Fixed CrateDB Update operation --- .../nodes-base/nodes/CrateDb/CrateDb.node.ts | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/nodes-base/nodes/CrateDb/CrateDb.node.ts b/packages/nodes-base/nodes/CrateDb/CrateDb.node.ts index a1a6a1bc63..9175fc431d 100644 --- a/packages/nodes-base/nodes/CrateDb/CrateDb.node.ts +++ b/packages/nodes-base/nodes/CrateDb/CrateDb.node.ts @@ -229,10 +229,41 @@ export class CrateDb implements INodeType { // ---------------------------------- // update // ---------------------------------- + const tableName = this.getNodeParameter('table', 0) as string; + const updateKey = this.getNodeParameter('updateKey', 0) as string; - const updateItems = await pgUpdate(this.getNodeParameter, pgp, db, items); + const queries : string[] = []; + let updateKeyValue : string | number; + let columns : string[] = []; - returnItems = this.helpers.returnJsonArray(updateItems); + items.map(item => { + const setOperations : string[] = []; + columns = Object.keys(item.json); + columns.map((col : string) => { + if (col !== updateKey) { + if (typeof item.json[col] === 'string') { + setOperations.push(`${col} = \'${item.json[col]}\'`); + } else { + setOperations.push(`${col} = ${item.json[col]}`); + } + } + }); + + updateKeyValue = item.json[updateKey] as string | number; + + if (updateKeyValue === undefined) { + throw new Error('No value found for update key!'); + } + + const query = `UPDATE "${tableName}" SET ${setOperations.join(',')} WHERE ${updateKey} = ${updateKeyValue};`; + queries.push(query); + }); + + await db.any(pgp.helpers.concat(queries)); + + const returnedItems = await db.any(`SELECT ${columns.join(',')} from ${tableName}`); + + returnItems = this.helpers.returnJsonArray(returnedItems as IDataObject[]); } else { await pgp.end(); throw new Error(`The operation "${operation}" is not supported!`);