mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
⚡ extract update function
This commit is contained in:
parent
0108538fcc
commit
2fea79f5f1
|
@ -38,7 +38,7 @@ function getItemCopy(
|
|||
* @param {input[]} input The Node's input data
|
||||
* @returns Promise<Array<object>>
|
||||
*/
|
||||
export function executeQuery(
|
||||
export function pgQuery(
|
||||
getNodeParam: Function,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
|
@ -53,16 +53,15 @@ export function executeQuery(
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns of copy of the items which only contains the json data and
|
||||
* of that only the define properties
|
||||
* Inserts the given items into 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<object>
|
||||
* @returns Promise<Array<IDataObject>>
|
||||
*/
|
||||
export async function executeInsert(
|
||||
export async function pgInsert(
|
||||
getNodeParam: Function,
|
||||
pgp: pgPromise.IMain<{}, pg.IClient>,
|
||||
db: pgPromise.IDatabase<{}, pg.IClient>,
|
||||
|
@ -96,3 +95,46 @@ export async function executeInsert(
|
|||
|
||||
return [insertData, insertItems];
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the given items in 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<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;
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ import {
|
|||
|
||||
import * as pgPromise from 'pg-promise';
|
||||
|
||||
import { executeInsert, executeQuery } from './Postgres.node.functions';
|
||||
import { pgInsert, pgQuery, pgUpdate } from './Postgres.node.functions';
|
||||
|
||||
export class Postgres implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
|
@ -214,12 +214,7 @@ export class Postgres implements INodeType {
|
|||
// executeQuery
|
||||
// ----------------------------------
|
||||
|
||||
const queryResult = await executeQuery(
|
||||
this.getNodeParameter,
|
||||
pgp,
|
||||
db,
|
||||
items,
|
||||
);
|
||||
const queryResult = await pgQuery(this.getNodeParameter, pgp, db, items);
|
||||
|
||||
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
|
||||
} else if (operation === 'insert') {
|
||||
|
@ -227,7 +222,7 @@ export class Postgres implements INodeType {
|
|||
// insert
|
||||
// ----------------------------------
|
||||
|
||||
const [insertData, insertItems] = await executeInsert(
|
||||
const [insertData, insertItems] = await pgInsert(
|
||||
this.getNodeParameter,
|
||||
pgp,
|
||||
db,
|
||||
|
@ -248,32 +243,9 @@ export class Postgres implements INodeType {
|
|||
// 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 updateItems = await pgUpdate(this.getNodeParameter, pgp, db, items);
|
||||
|
||||
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 {
|
||||
await pgp.end();
|
||||
throw new Error(`The operation "${operation}" is not supported!`);
|
||||
|
|
Loading…
Reference in a new issue