mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
🎉 executeQuery function extracted
This commit is contained in:
parent
b1ee1efcb1
commit
de707f039b
|
@ -0,0 +1,48 @@
|
||||||
|
import { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
||||||
|
import pgPromise = require('pg-promise');
|
||||||
|
import pg = require('pg-promise/typescript/pg-subset');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the given SQL query on the database.
|
||||||
|
*
|
||||||
|
* @param {Function} getNodeParam The getter of the Node
|
||||||
|
* @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<any>
|
||||||
|
*/
|
||||||
|
export function executeQuery(
|
||||||
|
getNodeParam: Function,
|
||||||
|
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||||
|
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||||
|
input: INodeExecutionData[]
|
||||||
|
): Promise<any> {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * Returns of copy of the items which only contains the json data and
|
||||||
|
// * of that only the define properties
|
||||||
|
// *
|
||||||
|
// * @param {items[]} items The items execute the query with
|
||||||
|
// * @param {string[]} properties The properties it should include
|
||||||
|
// * @returns
|
||||||
|
// */
|
||||||
|
// export function insert(
|
||||||
|
// getNodeParam: Function,
|
||||||
|
// pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||||
|
// db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||||
|
// items: INodeExecutionData[]
|
||||||
|
// ): Promise<any> {
|
||||||
|
// const queries: string[] = [];
|
||||||
|
// for (let i = 0; i < items.length; i++) {
|
||||||
|
// queries.push(getNodeParam('query', i) as string);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return db.any(pgp.helpers.concat(queries));
|
||||||
|
// }
|
|
@ -3,11 +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 { executeQuery } from './Postgres.node.functions';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns of copy of the items which only contains the json data and
|
* Returns of copy of the items which only contains the json data and
|
||||||
|
@ -17,10 +18,13 @@ import * as pgPromise from 'pg-promise';
|
||||||
* @param {string[]} properties The properties it should include
|
* @param {string[]} properties The properties it should include
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataObject[] {
|
function getItemCopy(
|
||||||
|
items: INodeExecutionData[],
|
||||||
|
properties: string[]
|
||||||
|
): IDataObject[] {
|
||||||
// Prepare the data to insert and copy it to be returned
|
// Prepare the data to insert and copy it to be returned
|
||||||
let newItem: IDataObject;
|
let newItem: IDataObject;
|
||||||
return items.map((item) => {
|
return items.map(item => {
|
||||||
newItem = {};
|
newItem = {};
|
||||||
for (const property of properties) {
|
for (const property of properties) {
|
||||||
if (item.json[property] === undefined) {
|
if (item.json[property] === undefined) {
|
||||||
|
@ -33,7 +37,6 @@ function getItemCopy(items: INodeExecutionData[], properties: string[]): IDataOb
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export class Postgres implements INodeType {
|
export class Postgres implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'Postgres',
|
displayName: 'Postgres',
|
||||||
|
@ -44,14 +47,14 @@ export class Postgres implements INodeType {
|
||||||
description: 'Gets, add and update data in Postgres.',
|
description: 'Gets, add and update data in Postgres.',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: 'Postgres',
|
name: 'Postgres',
|
||||||
color: '#336791',
|
color: '#336791'
|
||||||
},
|
},
|
||||||
inputs: ['main'],
|
inputs: ['main'],
|
||||||
outputs: ['main'],
|
outputs: ['main'],
|
||||||
credentials: [
|
credentials: [
|
||||||
{
|
{
|
||||||
name: 'postgres',
|
name: 'postgres',
|
||||||
required: true,
|
required: true
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
properties: [
|
properties: [
|
||||||
|
@ -63,21 +66,21 @@ export class Postgres implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'Execute Query',
|
name: 'Execute Query',
|
||||||
value: 'executeQuery',
|
value: 'executeQuery',
|
||||||
description: 'Executes a SQL query.',
|
description: 'Executes a SQL query.'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Insert',
|
name: 'Insert',
|
||||||
value: 'insert',
|
value: 'insert',
|
||||||
description: 'Insert rows in database.',
|
description: 'Insert rows in database.'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Update',
|
name: 'Update',
|
||||||
value: 'update',
|
value: 'update',
|
||||||
description: 'Updates rows in database.',
|
description: 'Updates rows in database.'
|
||||||
},
|
}
|
||||||
],
|
],
|
||||||
default: 'insert',
|
default: 'insert',
|
||||||
description: 'The operation to perform.',
|
description: 'The operation to perform.'
|
||||||
},
|
},
|
||||||
|
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
@ -88,22 +91,19 @@ export class Postgres implements INodeType {
|
||||||
name: 'query',
|
name: 'query',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
typeOptions: {
|
typeOptions: {
|
||||||
rows: 5,
|
rows: 5
|
||||||
},
|
},
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['executeQuery']
|
||||||
'executeQuery'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: '',
|
default: '',
|
||||||
placeholder: 'SELECT id, name FROM product WHERE id < 40',
|
placeholder: 'SELECT id, name FROM product WHERE id < 40',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'The SQL query to execute.',
|
description: 'The SQL query to execute.'
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
// insert
|
// insert
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
@ -113,14 +113,12 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['insert']
|
||||||
'insert'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: 'public',
|
default: 'public',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'Name of the schema the table belongs to',
|
description: 'Name of the schema the table belongs to'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Table',
|
displayName: 'Table',
|
||||||
|
@ -128,14 +126,12 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['insert']
|
||||||
'insert'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: '',
|
default: '',
|
||||||
required: true,
|
required: true,
|
||||||
description: 'Name of the table in which to insert data to.',
|
description: 'Name of the table in which to insert data to.'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Columns',
|
displayName: 'Columns',
|
||||||
|
@ -143,14 +139,13 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['insert']
|
||||||
'insert'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: '',
|
default: '',
|
||||||
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',
|
displayName: 'Return Fields',
|
||||||
|
@ -158,16 +153,14 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['insert']
|
||||||
'insert'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: '*',
|
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
|
// update
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
@ -177,14 +170,12 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['update']
|
||||||
'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',
|
||||||
|
@ -192,14 +183,13 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['update']
|
||||||
'update'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: 'id',
|
default: 'id',
|
||||||
required: true,
|
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',
|
displayName: 'Columns',
|
||||||
|
@ -207,22 +197,18 @@ export class Postgres implements INodeType {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
operation: ['update']
|
||||||
'update'
|
}
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
default: '',
|
default: '',
|
||||||
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) {
|
||||||
|
@ -237,8 +223,10 @@ export class Postgres implements INodeType {
|
||||||
database: credentials.database as string,
|
database: credentials.database as string,
|
||||||
user: credentials.user as string,
|
user: credentials.user as string,
|
||||||
password: credentials.password as string,
|
password: credentials.password as string,
|
||||||
ssl: !['disable', undefined].includes(credentials.ssl as string | undefined),
|
ssl: !['disable', undefined].includes(
|
||||||
sslmode: credentials.ssl as string || 'disable',
|
credentials.ssl as string | undefined
|
||||||
|
),
|
||||||
|
sslmode: (credentials.ssl as string) || 'disable'
|
||||||
};
|
};
|
||||||
|
|
||||||
const db = pgp(config);
|
const db = pgp(config);
|
||||||
|
@ -253,15 +241,14 @@ export class Postgres implements INodeType {
|
||||||
// executeQuery
|
// executeQuery
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
const queries: string[] = [];
|
const queryResult = await executeQuery(
|
||||||
for (let i = 0; i < items.length; i++) {
|
this.getNodeParameter,
|
||||||
queries.push(this.getNodeParameter('query', i) as string);
|
pgp,
|
||||||
}
|
db,
|
||||||
|
items
|
||||||
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
|
||||||
|
@ -269,7 +256,10 @@ 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;
|
const schema = this.getNodeParameter('schema', 0) as string;
|
||||||
let 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 columnString = this.getNodeParameter('columns', 0) as string;
|
||||||
const columns = columnString.split(',').map(column => column.trim());
|
const columns = columnString.split(',').map(column => column.trim());
|
||||||
|
|
||||||
|
@ -281,8 +271,12 @@ export class Postgres implements INodeType {
|
||||||
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
|
||||||
returnFields = returnFields.map(value => value.trim()).filter(value => !!value);
|
returnFields = returnFields
|
||||||
const query = pgp.helpers.insert(insertItems, cs, te) + (returnFields.length ? ` RETURNING ${returnFields.join(',')}` : '');
|
.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.manyOrNone(query);
|
const insertData = await db.manyOrNone(query);
|
||||||
|
@ -292,11 +286,10 @@ export class Postgres implements INodeType {
|
||||||
returnItems.push({
|
returnItems.push({
|
||||||
json: {
|
json: {
|
||||||
...insertData[i],
|
...insertData[i],
|
||||||
...insertItems[i],
|
...insertItems[i]
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (operation === 'update') {
|
} else if (operation === 'update') {
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
// update
|
// update
|
||||||
|
@ -317,13 +310,17 @@ export class Postgres implements INodeType {
|
||||||
const updateItems = getItemCopy(items, columns);
|
const updateItems = getItemCopy(items, columns);
|
||||||
|
|
||||||
// Generate the multi-row update query
|
// Generate the multi-row update query
|
||||||
const query = pgp.helpers.update(updateItems, columns, table) + ' WHERE v.' + updateKey + ' = t.' + updateKey;
|
const query =
|
||||||
|
pgp.helpers.update(updateItems, columns, table) +
|
||||||
|
' WHERE v.' +
|
||||||
|
updateKey +
|
||||||
|
' = t.' +
|
||||||
|
updateKey;
|
||||||
|
|
||||||
// Executing the query to update the data
|
// Executing the query to update the data
|
||||||
await db.none(query);
|
await db.none(query);
|
||||||
|
|
||||||
returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]);
|
returnItems = this.helpers.returnJsonArray(updateItems 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!`);
|
||||||
|
|
Loading…
Reference in a new issue