From 5133f16e42724a7de73c676aff1b37227f4103a4 Mon Sep 17 00:00:00 2001 From: ricardo Date: Tue, 17 Mar 2020 19:49:21 -0400 Subject: [PATCH 1/2] :zap: Improvements to insert operation --- .../nodes/Postgres/Postgres.node.ts | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/nodes-base/nodes/Postgres/Postgres.node.ts b/packages/nodes-base/nodes/Postgres/Postgres.node.ts index c1da2fba35..3e11d8af8d 100644 --- a/packages/nodes-base/nodes/Postgres/Postgres.node.ts +++ b/packages/nodes-base/nodes/Postgres/Postgres.node.ts @@ -107,6 +107,21 @@ export class Postgres implements INodeType { // ---------------------------------- // insert // ---------------------------------- + { + displayName: 'Schema', + name: 'schema', + type: 'string', + displayOptions: { + show: { + operation: [ + 'insert' + ], + }, + }, + default: 'public', + required: true, + description: 'Name of the schema the table belongs to', + }, { displayName: 'Table', name: 'table', @@ -137,6 +152,20 @@ export class Postgres implements INodeType { placeholder: 'id,name,description', description: 'Comma separated list of the properties which should used as columns for the new rows.', }, + { + displayName: 'Return Fields', + name: 'returnFields', + type: 'string', + displayOptions: { + show: { + operation: [ + 'insert' + ], + }, + }, + default: '*', + description: 'Comma separated list of the fields that the operation will return', + }, // ---------------------------------- @@ -239,20 +268,23 @@ export class Postgres implements INodeType { // ---------------------------------- const table = this.getNodeParameter('table', 0) as string; + const schema = this.getNodeParameter('schema', 0) as string; + const returnFields = (this.getNodeParameter('returnFields', 0) as string).split(',') as string[]; const columnString = this.getNodeParameter('columns', 0) as string; - const columns = columnString.split(',').map(column => column.trim()); - const cs = new pgp.helpers.ColumnSet(columns, { table }); + const cs = new pgp.helpers.ColumnSet(columns); + + const te = new pgp.helpers.TableName({ table, schema }); // Prepare the data to insert and copy it to be returned const insertItems = getItemCopy(items, columns); // Generate the multi-row insert query and return the id of new row - const query = pgp.helpers.insert(insertItems, cs) + ' RETURNING id'; + const query = pgp.helpers.insert(insertItems, cs, te) + ((returnFields[0] !== '') ? ` RETURNING ${returnFields.join(',')}` : ''); // Executing the query to insert the data - const insertData = await db.many(query); + const insertData = await db.manyOrNone(query); // Add the id to the data for (let i = 0; i < insertData.length; i++) { From 0d5723f50abd126d35b7f95f6cb30ddea575b3b9 Mon Sep 17 00:00:00 2001 From: Jan Oberhauser Date: Wed, 18 Mar 2020 09:26:40 +0100 Subject: [PATCH 2/2] :zap: Small improvement to Postgres-Node --- packages/nodes-base/nodes/Postgres/Postgres.node.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/nodes-base/nodes/Postgres/Postgres.node.ts b/packages/nodes-base/nodes/Postgres/Postgres.node.ts index 3e11d8af8d..f9db1a01e1 100644 --- a/packages/nodes-base/nodes/Postgres/Postgres.node.ts +++ b/packages/nodes-base/nodes/Postgres/Postgres.node.ts @@ -269,7 +269,7 @@ export class Postgres implements INodeType { const table = this.getNodeParameter('table', 0) as string; const schema = this.getNodeParameter('schema', 0) as string; - const returnFields = (this.getNodeParameter('returnFields', 0) as string).split(',') as string[]; + let returnFields = (this.getNodeParameter('returnFields', 0) as string).split(',') as string[]; const columnString = this.getNodeParameter('columns', 0) as string; const columns = columnString.split(',').map(column => column.trim()); @@ -281,7 +281,8 @@ export class Postgres implements INodeType { const insertItems = getItemCopy(items, columns); // Generate the multi-row insert query and return the id of new row - const query = pgp.helpers.insert(insertItems, cs, te) + ((returnFields[0] !== '') ? ` RETURNING ${returnFields.join(',')}` : ''); + returnFields = returnFields.map(value => value.trim()).filter(value => !!value); + const query = pgp.helpers.insert(insertItems, cs, te) + (returnFields.length ? ` RETURNING ${returnFields.join(',')}` : ''); // Executing the query to insert the data const insertData = await db.manyOrNone(query);