🚧 Removed update function, fixed insert function

This commit is contained in:
Rupenieks 2020-07-23 11:14:03 +02:00
parent 914f068fce
commit 5545bc7dfc

View file

@ -4,6 +4,7 @@ import { IDataObject, INodeExecutionData, INodeType, INodeTypeDescription } from
import * as pgPromise from 'pg-promise'; import * as pgPromise from 'pg-promise';
import { pgInsert, pgQuery, pgUpdate } from '../Postgres/Postgres.node.functions'; import { pgInsert, pgQuery, pgUpdate } from '../Postgres/Postgres.node.functions';
import { table } from 'console';
export class QuestDb implements INodeType { export class QuestDb implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -40,12 +41,7 @@ export class QuestDb implements INodeType {
name: 'Insert', name: 'Insert',
value: 'insert', value: 'insert',
description: 'Insert rows in database.', description: 'Insert rows in database.',
}, }
{
name: 'Update',
value: 'update',
description: 'Updates rows in database.',
},
], ],
default: 'insert', default: 'insert',
description: 'The operation to perform.', description: 'The operation to perform.',
@ -131,47 +127,47 @@ export class QuestDb implements INodeType {
// ---------------------------------- // ----------------------------------
// update // update
// ---------------------------------- // ----------------------------------
{ // {
displayName: 'Table', // displayName: 'Table',
name: 'table', // name: 'table',
type: 'string', // type: 'string',
displayOptions: { // displayOptions: {
show: { // show: {
operation: ['update'], // operation: ['update'],
}, // },
}, // },
default: '', // default: '',
required: true, // required: true,
description: 'Name of the table in which to update data in', // description: 'Name of the table in which to update data in',
}, // },
{ // {
displayName: 'Update Key', // displayName: 'Update Key',
name: 'updateKey', // name: 'updateKey',
type: 'string', // type: 'string',
displayOptions: { // displayOptions: {
show: { // show: {
operation: ['update'], // operation: ['update'],
}, // },
}, // },
default: 'id', // default: 'id',
required: true, // required: true,
description: // description:
'Name of the property which decides which rows in the database should be updated. Normally that would be "id".', // 'Name of the property which decides which rows in the database should be updated. Normally that would be "id".',
}, // },
{ // {
displayName: 'Columns', // displayName: 'Columns',
name: 'columns', // name: 'columns',
type: 'string', // type: 'string',
displayOptions: { // displayOptions: {
show: { // show: {
operation: ['update'], // operation: ['update'],
}, // },
}, // },
default: '', // default: '',
placeholder: 'name,description', // placeholder: 'name,description',
description: // description:
'Comma separated list of the properties which should used as columns for rows to update.', // 'Comma separated list of the properties which should used as columns for rows to update.',
}, // },
], ],
}; };
@ -213,26 +209,30 @@ export class QuestDb implements INodeType {
// ---------------------------------- // ----------------------------------
// insert // insert
// ---------------------------------- // ----------------------------------
const tableName = this.getNodeParameter('table', 0) as string;
const returnFields = this.getNodeParameter('returnFields', 0) as string;
let queries : string[] = [];
items.map(item => {
let columns = Object.keys(item.json);
const [insertData, insertItems] = await pgInsert(this.getNodeParameter, pgp, db, items); let values : string = columns.map((col : string) => {
if (typeof item.json[col] === 'string') {
return `\'${item.json[col]}\'`;
} else {
return item.json[col];
}
}).join(',');
// Add the id to the data let query = `INSERT INTO ${tableName} (${columns.join(',')}) VALUES (${values});`;
for (let i = 0; i < insertData.length; i++) { queries.push(query);
returnItems.push({ });
json: {
...insertData[i], await db.any(pgp.helpers.concat(queries));
...insertItems[i],
},
});
}
} else if (operation === 'update') {
// ----------------------------------
// update
// ----------------------------------
const updateItems = await pgUpdate(this.getNodeParameter, pgp, db, items); let returnedItems = await db.any(`SELECT ${returnFields} from ${tableName}`);
returnItems = this.helpers.returnJsonArray(updateItems); returnItems = this.helpers.returnJsonArray(returnedItems as IDataObject[]);
} else { } else {
await pgp.end(); await pgp.end();
throw new Error(`The operation "${operation}" is not supported!`); throw new Error(`The operation "${operation}" is not supported!`);