🎨 Extract postgres functionality (#737)

* 🎉 executeQuery function extracted

* 🚧 add prettierrc to gitignore

* 🚧 insert function extracted

*  extract update function

* 💡 fix function docs

* 💡 add in code documentation

* 🎨 fix format

* 🎨 fix format
This commit is contained in:
Ben Hesseldieck 2020-07-10 10:48:19 +02:00 committed by GitHub
parent db972b384f
commit a00fedb351
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 140 additions and 85 deletions

1
.gitignore vendored
View file

@ -12,3 +12,4 @@ _START_PACKAGE
.env .env
.vscode .vscode
.idea .idea
.prettierrc.js

View file

@ -0,0 +1,129 @@
import { IDataObject, INodeExecutionData } from 'n8n-workflow';
import pgPromise = require('pg-promise');
import pg = require('pg-promise/typescript/pg-subset');
/**
* 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;
});
}
/**
* Executes the given SQL query on the database.
*
* @param {Function} getNodeParam The getter for the Node's parameters
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
* @param {input[]} input The Node's input data
* @returns Promise<Array<object>>
*/
export function pgQuery(
getNodeParam: Function,
pgp: pgPromise.IMain<{}, pg.IClient>,
db: pgPromise.IDatabase<{}, pg.IClient>,
input: INodeExecutionData[],
): Promise<Array<object>> {
const queries: string[] = [];
for (let i = 0; i < input.length; i++) {
queries.push(getNodeParam('query', i) as string);
}
return db.any(pgp.helpers.concat(queries));
}
/**
* Inserts the given items into the database.
*
* @param {Function} getNodeParam The getter for the Node's parameters
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
* @param {INodeExecutionData[]} items The items to be inserted
* @returns Promise<Array<IDataObject>>
*/
export async function pgInsert(
getNodeParam: Function,
pgp: pgPromise.IMain<{}, pg.IClient>,
db: pgPromise.IDatabase<{}, pg.IClient>,
items: INodeExecutionData[],
): Promise<Array<IDataObject[]>> {
const table = getNodeParam('table', 0) as string;
const schema = getNodeParam('schema', 0) as string;
let returnFields = (getNodeParam('returnFields', 0) as string).split(',') as string[];
const columnString = getNodeParam('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);
return [insertData, insertItems];
}
/**
* Updates the given items in the database.
*
* @param {Function} getNodeParam The getter for the Node's parameters
* @param {pgPromise.IMain<{}, pg.IClient>} pgp The pgPromise instance
* @param {pgPromise.IDatabase<{}, pg.IClient>} db The pgPromise database connection
* @param {INodeExecutionData[]} items The items to be updated
* @returns Promise<Array<IDataObject>>
*/
export async function pgUpdate(
getNodeParam: Function,
pgp: pgPromise.IMain<{}, pg.IClient>,
db: pgPromise.IDatabase<{}, pg.IClient>,
items: INodeExecutionData[],
): Promise<Array<IDataObject>> {
const table = getNodeParam('table', 0) as string;
const updateKey = getNodeParam('updateKey', 0) as string;
const columnString = getNodeParam('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);
return updateItems;
}

View file

@ -3,36 +3,12 @@ import {
IDataObject, IDataObject,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription
} from 'n8n-workflow'; } from 'n8n-workflow';
import * as pgPromise from 'pg-promise'; import * as pgPromise from 'pg-promise';
import { pgInsert, pgQuery, pgUpdate } from './Postgres.node.functions';
/**
* 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;
});
}
export class Postgres implements INodeType { export class Postgres implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
@ -52,7 +28,7 @@ export class Postgres implements INodeType {
{ {
name: 'postgres', name: 'postgres',
required: true, required: true,
} },
], ],
properties: [ properties: [
{ {
@ -103,7 +79,6 @@ export class Postgres implements INodeType {
description: 'The SQL query to execute.', description: 'The SQL query to execute.',
}, },
// ---------------------------------- // ----------------------------------
// insert // insert
// ---------------------------------- // ----------------------------------
@ -143,9 +118,7 @@ export class Postgres implements INodeType {
type: 'string', type: 'string',
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: ['insert'],
'insert'
],
}, },
}, },
default: '', default: '',
@ -167,7 +140,6 @@ export class Postgres implements INodeType {
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 // update
// ---------------------------------- // ----------------------------------
@ -216,13 +188,10 @@ export class Postgres implements INodeType {
placeholder: 'name,description', 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[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = this.getCredentials('postgres'); const credentials = this.getCredentials('postgres');
if (credentials === undefined) { if (credentials === undefined) {
@ -253,39 +222,15 @@ export class Postgres implements INodeType {
// executeQuery // executeQuery
// ---------------------------------- // ----------------------------------
const queries: string[] = []; const queryResult = await pgQuery(this.getNodeParameter, pgp, db, items);
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));
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]); returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
} else if (operation === 'insert') { } else if (operation === 'insert') {
// ---------------------------------- // ----------------------------------
// insert // insert
// ---------------------------------- // ----------------------------------
const table = this.getNodeParameter('table', 0) as string; const [insertData, insertItems] = await pgInsert(this.getNodeParameter, pgp, db, items);
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);
// 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++) {
@ -293,37 +238,17 @@ export class Postgres implements INodeType {
json: { json: {
...insertData[i], ...insertData[i],
...insertItems[i], ...insertItems[i],
} },
}); });
} }
} else if (operation === 'update') { } else if (operation === 'update') {
// ---------------------------------- // ----------------------------------
// update // update
// ---------------------------------- // ----------------------------------
const table = this.getNodeParameter('table', 0) as string; const updateItems = await pgUpdate(this.getNodeParameter, pgp, db, items);
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[]);
returnItems = this.helpers.returnJsonArray(updateItems);
} 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!`);