complete QuestDB Node

This commit is contained in:
Ben Hesseldieck 2020-07-08 16:30:21 +02:00
parent 51bd1b473f
commit 8405d48e0c
2 changed files with 40 additions and 112 deletions

View file

@ -8,31 +8,11 @@ import {
import * as pgPromise from 'pg-promise';
/**
* Returns of copy of the items which only contains the json data and
* of that only the define properties
*
* @param {INodeExecutionData[]} items The items to copy
* @param {string[]} properties The properties it should include
* @returns
*/
function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataObject[] {
// Prepare the data to insert and copy it to be returned
let newItem: IDataObject;
return items.map((item) => {
newItem = {};
for (const property of properties) {
if (item.json[property] === undefined) {
newItem[property] = null;
} else {
newItem[property] = JSON.parse(JSON.stringify(item.json[property]));
}
}
return newItem;
});
}
import {
pgInsert,
pgQuery,
pgUpdate,
} from '../Postgres/Postgres.node.functions';
export class QuestDB implements INodeType {
description: INodeTypeDescription = {
@ -44,7 +24,7 @@ export class QuestDB implements INodeType {
description: 'Gets, add and update data in QuestDB.',
defaults: {
name: 'QuestDB',
color: '#336791',
color: '#2C4A79',
},
inputs: ['main'],
outputs: ['main'],
@ -52,7 +32,7 @@ export class QuestDB implements INodeType {
{
name: 'questdb',
required: true,
}
},
],
properties: [
{
@ -92,9 +72,7 @@ export class QuestDB implements INodeType {
},
displayOptions: {
show: {
operation: [
'executeQuery'
],
operation: ['executeQuery'],
},
},
default: '',
@ -103,7 +81,6 @@ export class QuestDB implements INodeType {
description: 'The SQL query to execute.',
},
// ----------------------------------
// insert
// ----------------------------------
@ -113,9 +90,7 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'insert'
],
operation: ['insert'],
},
},
default: 'public',
@ -128,9 +103,7 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'insert'
],
operation: ['insert'],
},
},
default: '',
@ -143,14 +116,13 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'insert'
],
operation: ['insert'],
},
},
default: '',
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',
@ -158,16 +130,14 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'insert'
],
operation: ['insert'],
},
},
default: '*',
description: 'Comma separated list of the fields that the operation will return',
description:
'Comma separated list of the fields that the operation will return',
},
// ----------------------------------
// update
// ----------------------------------
@ -177,9 +147,7 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'update'
],
operation: ['update'],
},
},
default: '',
@ -192,14 +160,13 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'update'
],
operation: ['update'],
},
},
default: 'id',
required: true,
description: 'Name of the property which decides which rows in the database should be updated. Normally that would be "id".',
description:
'Name of the property which decides which rows in the database should be updated. Normally that would be "id".',
},
{
displayName: 'Columns',
@ -207,22 +174,18 @@ export class QuestDB implements INodeType {
type: 'string',
displayOptions: {
show: {
operation: [
'update'
],
operation: ['update'],
},
},
default: '',
placeholder: 'name,description',
description: 'Comma separated list of the properties which should used as columns for rows to update.',
description:
'Comma separated list of the properties which should used as columns for rows to update.',
},
]
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials('questdb');
if (credentials === undefined) {
@ -237,8 +200,10 @@ export class QuestDB implements INodeType {
database: credentials.database as string,
user: credentials.user as string,
password: credentials.password as string,
ssl: !['disable', undefined].includes(credentials.ssl as string | undefined),
sslmode: credentials.ssl as string || 'disable',
ssl: !['disable', undefined].includes(
credentials.ssl as string | undefined,
),
sslmode: (credentials.ssl as string) || 'disable',
};
const db = pgp(config);
@ -253,39 +218,20 @@ export class QuestDB implements INodeType {
// executeQuery
// ----------------------------------
const queries: string[] = [];
for (let i = 0; i < items.length; i++) {
queries.push(this.getNodeParameter('query', i) as string);
}
const queryResult = await db.any(pgp.helpers.concat(queries));
const queryResult = await pgQuery(this.getNodeParameter, pgp, db, items);
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
} else if (operation === 'insert') {
// ----------------------------------
// insert
// ----------------------------------
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 columns = columnString.split(',').map(column => column.trim());
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
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);
const [insertData, insertItems] = await pgInsert(
this.getNodeParameter,
pgp,
db,
items,
);
// Add the id to the data
for (let i = 0; i < insertData.length; i++) {
@ -293,37 +239,17 @@ export class QuestDB implements INodeType {
json: {
...insertData[i],
...insertItems[i],
}
},
});
}
} else if (operation === 'update') {
// ----------------------------------
// 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 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[]);
const updateItems = await pgUpdate(this.getNodeParameter, pgp, db, items);
returnItems = this.helpers.returnJsonArray(updateItems);
} else {
await pgp.end();
throw new Error(`The operation "${operation}" is not supported!`);

View file

@ -112,6 +112,7 @@
"dist/credentials/PipedriveApi.credentials.js",
"dist/credentials/Postgres.credentials.js",
"dist/credentials/PostmarkApi.credentials.js",
"dist/credentials/Questdb.credentials.js",
"dist/credentials/Redis.credentials.js",
"dist/credentials/RocketchatApi.credentials.js",
"dist/credentials/RundeckApi.credentials.js",
@ -260,6 +261,7 @@
"dist/nodes/Pipedrive/PipedriveTrigger.node.js",
"dist/nodes/Postgres/Postgres.node.js",
"dist/nodes/Postmark/PostmarkTrigger.node.js",
"dist/nodes/QuestDB/QuestDB.node.js",
"dist/nodes/ReadBinaryFile.node.js",
"dist/nodes/ReadBinaryFiles.node.js",
"dist/nodes/ReadPdf.node.js",