mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-24 20:24:05 -08:00
fix(Postgres Node): Fix issue with columns containing spaces (#2989)
* 🔨 fixed error when column name containes spaces * ⚡ added lock fille to commit * 🔨 fix for column names wraped in square braces * 🔨 added lock file * 🔨 fix for update key not included in update columns * ⚡ Revert imports Co-authored-by: Ricardo Espinoza <ricardo@n8n.io> Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
parent
5d1ddb0e9b
commit
0081d02b97
|
@ -1,4 +1,4 @@
|
|||
import { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
||||
import { IDataObject, INodeExecutionData, JsonObject } from 'n8n-workflow';
|
||||
import pgPromise from 'pg-promise';
|
||||
import pg from 'pg-promise/typescript/pg-subset';
|
||||
|
||||
|
@ -10,12 +10,18 @@ import pg from 'pg-promise/typescript/pg-subset';
|
|||
* @param {string[]} properties The properties it should include
|
||||
* @returns
|
||||
*/
|
||||
export function getItemsCopy(items: INodeExecutionData[], properties: string[]): IDataObject[] {
|
||||
export function getItemsCopy(items: INodeExecutionData[], properties: string[], guardedColumns?: {[key: string]: string}): IDataObject[] {
|
||||
let newItem: IDataObject;
|
||||
return items.map(item => {
|
||||
newItem = {};
|
||||
for (const property of properties) {
|
||||
newItem[property] = item.json[property];
|
||||
if (guardedColumns) {
|
||||
Object.keys(guardedColumns).forEach( column => {
|
||||
newItem[column] = item.json[guardedColumns[column]];
|
||||
});
|
||||
} else {
|
||||
for (const property of properties) {
|
||||
newItem[property] = item.json[property];
|
||||
}
|
||||
}
|
||||
return newItem;
|
||||
});
|
||||
|
@ -29,10 +35,16 @@ export function getItemsCopy(items: INodeExecutionData[], properties: string[]):
|
|||
* @param {string[]} properties The properties it should include
|
||||
* @returns
|
||||
*/
|
||||
export function getItemCopy(item: INodeExecutionData, properties: string[]): IDataObject {
|
||||
export function getItemCopy(item: INodeExecutionData, properties: string[], guardedColumns?: {[key: string]: string}): IDataObject {
|
||||
const newItem: IDataObject = {};
|
||||
for (const property of properties) {
|
||||
newItem[property] = item.json[property];
|
||||
if (guardedColumns) {
|
||||
Object.keys(guardedColumns).forEach( column => {
|
||||
newItem[column] = item.json[guardedColumns[column]];
|
||||
});
|
||||
} else {
|
||||
for (const property of properties) {
|
||||
newItem[property] = item.json[property];
|
||||
}
|
||||
}
|
||||
return newItem;
|
||||
}
|
||||
|
@ -93,7 +105,7 @@ export async function pgQuery(
|
|||
Array.prototype.push.apply(result, await t.any(allQueries[i].query, allQueries[i].values));
|
||||
} catch (err) {
|
||||
if (continueOnFail === false) throw err;
|
||||
result.push({ ...items[i].json, code: err.code, message: err.message });
|
||||
result.push({ ...items[i].json, code: (err as JsonObject).code, message: (err as JsonObject).message });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -107,7 +119,7 @@ export async function pgQuery(
|
|||
Array.prototype.push.apply(result, await t.any(allQueries[i].query, allQueries[i].values));
|
||||
} catch (err) {
|
||||
if (continueOnFail === false) throw err;
|
||||
result.push({ ...items[i].json, code: err.code, message: err.message });
|
||||
result.push({ ...items[i].json, code: (err as JsonObject).code, message: (err as JsonObject).message });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -136,9 +148,15 @@ export async function pgInsert(
|
|||
const table = getNodeParam('table', 0) as string;
|
||||
const schema = getNodeParam('schema', 0) as string;
|
||||
const columnString = getNodeParam('columns', 0) as string;
|
||||
const guardedColumns: {[key: string]: string} = {};
|
||||
|
||||
const columns = columnString.split(',')
|
||||
.map(column => column.trim().split(':'))
|
||||
.map(([name, cast]) => ({ name, cast }));
|
||||
.map(([name, cast], i) => {
|
||||
guardedColumns[`column${i}`] = name;
|
||||
return { name, cast, prop: `column${i}` };
|
||||
});
|
||||
|
||||
const columnNames = columns.map(column => column.name);
|
||||
|
||||
const cs = new pgp.helpers.ColumnSet(columns, { table: { table, schema } });
|
||||
|
@ -148,18 +166,18 @@ export async function pgInsert(
|
|||
|
||||
const returning = generateReturning(pgp, getNodeParam('returnFields', 0) as string);
|
||||
if (mode === 'multiple') {
|
||||
const query = pgp.helpers.insert(getItemsCopy(items, columnNames), cs) + returning;
|
||||
const query = pgp.helpers.insert(getItemsCopy(items, columnNames, guardedColumns), cs) + returning;
|
||||
return db.any(query);
|
||||
} else if (mode === 'transaction') {
|
||||
return db.tx(async t => {
|
||||
const result: IDataObject[] = [];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const itemCopy = getItemCopy(items[i], columnNames);
|
||||
const itemCopy = getItemCopy(items[i], columnNames, guardedColumns);
|
||||
try {
|
||||
result.push(await t.one(pgp.helpers.insert(itemCopy, cs) + returning));
|
||||
} catch (err) {
|
||||
if (continueOnFail === false) throw err;
|
||||
result.push({ ...itemCopy, code: err.code, message: err.message });
|
||||
result.push({ ...itemCopy, code: (err as JsonObject).code, message: (err as JsonObject).message });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -169,7 +187,7 @@ export async function pgInsert(
|
|||
return db.task(async t => {
|
||||
const result: IDataObject[] = [];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const itemCopy = getItemCopy(items[i], columnNames);
|
||||
const itemCopy = getItemCopy(items[i], columnNames, guardedColumns);
|
||||
try {
|
||||
const insertResult = await t.oneOrNone(pgp.helpers.insert(itemCopy, cs) + returning);
|
||||
if (insertResult !== null) {
|
||||
|
@ -179,7 +197,7 @@ export async function pgInsert(
|
|||
if (continueOnFail === false) {
|
||||
throw err;
|
||||
}
|
||||
result.push({ ...itemCopy, code: err.code, message: err.message });
|
||||
result.push({ ...itemCopy, code: (err as JsonObject).code, message: (err as JsonObject).message });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
@ -209,15 +227,21 @@ export async function pgUpdate(
|
|||
const schema = getNodeParam('schema', 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().split(':'))
|
||||
.map(([name, cast]) => ({ name, cast }));
|
||||
const guardedColumns: {[key: string]: string} = {};
|
||||
|
||||
const updateKeys = updateKey.split(',').map(key => {
|
||||
const columns: Array<{name:string, cast: string, prop:string}> = columnString.split(',')
|
||||
.map(column => column.trim().split(':'))
|
||||
.map(([name, cast], i) => {
|
||||
guardedColumns[`column${i}`] = name;
|
||||
return { name, cast, prop: `column${i}` };
|
||||
});
|
||||
|
||||
const updateKeys = updateKey.split(',').map((key, i) => {
|
||||
const [name, cast] = key.trim().split(':');
|
||||
const updateColumn = { name, cast };
|
||||
const targetCol = columns.find((column) => column.name === name);
|
||||
const updateColumn = { name, cast, prop: targetCol ? targetCol.prop : `updateColumn${i}` };
|
||||
if (!targetCol) {
|
||||
guardedColumns[updateColumn.prop] = name;
|
||||
columns.unshift(updateColumn);
|
||||
}
|
||||
else if (!targetCol.cast) {
|
||||
|
@ -233,7 +257,7 @@ export async function pgUpdate(
|
|||
|
||||
// Prepare the data to update and copy it to be returned
|
||||
const columnNames = columns.map(column => column.name);
|
||||
const updateItems = getItemsCopy(items, columnNames);
|
||||
const updateItems = getItemsCopy(items, columnNames, guardedColumns);
|
||||
|
||||
const returning = generateReturning(pgp, getNodeParam('returnFields', 0) as string);
|
||||
if (mode === 'multiple') {
|
||||
|
@ -246,17 +270,19 @@ export async function pgUpdate(
|
|||
+ returning;
|
||||
return await db.any(query);
|
||||
} else {
|
||||
const where = ' WHERE ' + updateKeys.map(updateKey => pgp.as.name(updateKey.name) + ' = ${' + updateKey.name + '}').join(' AND ');
|
||||
const where = ' WHERE ' +
|
||||
updateKeys.map(updateKey => pgp.as.name(updateKey.name) +
|
||||
' = ${' + updateKey.prop + '}').join(' AND ');
|
||||
if (mode === 'transaction') {
|
||||
return db.tx(async t => {
|
||||
const result: IDataObject[] = [];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const itemCopy = getItemCopy(items[i], columnNames);
|
||||
const itemCopy = getItemCopy(items[i], columnNames, guardedColumns);
|
||||
try {
|
||||
Array.prototype.push.apply(result, await t.any(pgp.helpers.update(itemCopy, cs) + pgp.as.format(where, itemCopy) + returning));
|
||||
} catch (err) {
|
||||
if (continueOnFail === false) throw err;
|
||||
result.push({ ...itemCopy, code: err.code, message: err.message });
|
||||
result.push({ ...itemCopy, code: (err as JsonObject).code, message: (err as JsonObject).message });
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -266,12 +292,12 @@ export async function pgUpdate(
|
|||
return db.task(async t => {
|
||||
const result: IDataObject[] = [];
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const itemCopy = getItemCopy(items[i], columnNames);
|
||||
const itemCopy = getItemCopy(items[i], columnNames, guardedColumns);
|
||||
try {
|
||||
Array.prototype.push.apply(result, await t.any(pgp.helpers.update(itemCopy, cs) + pgp.as.format(where, itemCopy) + returning));
|
||||
} catch (err) {
|
||||
if (continueOnFail === false) throw err;
|
||||
result.push({ ...itemCopy, code: err.code, message: err.message });
|
||||
result.push({ ...itemCopy, code: (err as JsonObject).code, message: (err as JsonObject).message });
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
|
Loading…
Reference in a new issue