🔀 Merge branch 'RicardoE105-feature/postgres'

This commit is contained in:
Jan Oberhauser 2020-03-18 22:49:04 +01:00
commit b6a2e87f38

View file

@ -107,6 +107,21 @@ export class Postgres implements INodeType {
// ---------------------------------- // ----------------------------------
// insert // 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', displayName: 'Table',
name: 'table', name: 'table',
@ -137,6 +152,20 @@ export class Postgres implements INodeType {
placeholder: 'id,name,description', placeholder: 'id,name,description',
description: 'Comma separated list of the properties which should used as columns for the new rows.', 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,24 @@ export class Postgres implements INodeType {
// ---------------------------------- // ----------------------------------
const table = this.getNodeParameter('table', 0) as string; const table = this.getNodeParameter('table', 0) as string;
const schema = this.getNodeParameter('schema', 0) as string;
let returnFields = (this.getNodeParameter('returnFields', 0) as string).split(',') as string[];
const columnString = this.getNodeParameter('columns', 0) as string; const columnString = this.getNodeParameter('columns', 0) as string;
const columns = columnString.split(',').map(column => column.trim()); 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 // Prepare the data to insert and copy it to be returned
const insertItems = getItemCopy(items, columns); const insertItems = getItemCopy(items, columns);
// Generate the multi-row insert query and return the id of new row // Generate the multi-row insert query and return the id of new row
const query = pgp.helpers.insert(insertItems, cs) + ' RETURNING id'; 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 // 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 // Add the id to the data
for (let i = 0; i < insertData.length; i++) { for (let i = 0; i < insertData.length; i++) {