mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(core): Move copyInputItems
to node helpers (no-changelog) (#7299)
This commit is contained in:
parent
34bda535e6
commit
597669aa62
|
@ -2709,6 +2709,24 @@ const getBinaryHelperFunctions = (
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a copy of the items which only contains the json data and
|
||||||
|
* of that only the defined properties
|
||||||
|
*/
|
||||||
|
export function copyInputItems(items: INodeExecutionData[], properties: string[]): IDataObject[] {
|
||||||
|
return items.map((item) => {
|
||||||
|
const newItem: IDataObject = {};
|
||||||
|
for (const property of properties) {
|
||||||
|
if (item.json[property] === undefined) {
|
||||||
|
newItem[property] = null;
|
||||||
|
} else {
|
||||||
|
newItem[property] = deepCopy(item.json[property]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newItem;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the execute functions the poll nodes have access to.
|
* Returns the execute functions the poll nodes have access to.
|
||||||
*/
|
*/
|
||||||
|
@ -3239,6 +3257,7 @@ export function getExecuteFunctions(
|
||||||
},
|
},
|
||||||
helpers: {
|
helpers: {
|
||||||
createDeferredPromise,
|
createDeferredPromise,
|
||||||
|
copyInputItems,
|
||||||
...getRequestHelperFunctions(workflow, node, additionalData),
|
...getRequestHelperFunctions(workflow, node, additionalData),
|
||||||
...getFileSystemHelperFunctions(node),
|
...getFileSystemHelperFunctions(node),
|
||||||
...getBinaryHelperFunctions(additionalData, workflow.id),
|
...getBinaryHelperFunctions(additionalData, workflow.id),
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import {
|
import {
|
||||||
|
copyInputItems,
|
||||||
getBinaryDataBuffer,
|
getBinaryDataBuffer,
|
||||||
parseIncomingMessage,
|
parseIncomingMessage,
|
||||||
proxyRequestToAxios,
|
proxyRequestToAxios,
|
||||||
|
@ -297,4 +298,52 @@ describe('NodeExecuteFunctions', () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('copyInputItems', () => {
|
||||||
|
it('should pick only selected properties', () => {
|
||||||
|
const output = copyInputItems(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
a: 1,
|
||||||
|
b: true,
|
||||||
|
c: {},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
['a'],
|
||||||
|
);
|
||||||
|
expect(output).toEqual([{ a: 1 }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should convert undefined to null', () => {
|
||||||
|
const output = copyInputItems(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
json: {
|
||||||
|
a: undefined,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
['a'],
|
||||||
|
);
|
||||||
|
expect(output).toEqual([{ a: null }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should clone objects', () => {
|
||||||
|
const input = {
|
||||||
|
a: { b: 5 },
|
||||||
|
};
|
||||||
|
const output = copyInputItems(
|
||||||
|
[
|
||||||
|
{
|
||||||
|
json: input,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
['a'],
|
||||||
|
);
|
||||||
|
expect(output[0].a).toEqual(input.a);
|
||||||
|
expect(output[0].a === input.a).toEqual(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -2,35 +2,10 @@ import type {
|
||||||
ICredentialDataDecryptedObject,
|
ICredentialDataDecryptedObject,
|
||||||
IDataObject,
|
IDataObject,
|
||||||
ILoadOptionsFunctions,
|
ILoadOptionsFunctions,
|
||||||
INodeExecutionData,
|
|
||||||
INodeListSearchResult,
|
INodeListSearchResult,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import { deepCopy } from 'n8n-workflow';
|
|
||||||
import mysql2 from 'mysql2/promise';
|
import mysql2 from 'mysql2/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
|
|
||||||
*/
|
|
||||||
export function copyInputItems(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] = deepCopy(item.json[property]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newItem;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function createConnection(
|
export async function createConnection(
|
||||||
credentials: ICredentialDataDecryptedObject,
|
credentials: ICredentialDataDecryptedObject,
|
||||||
): Promise<mysql2.Connection> {
|
): Promise<mysql2.Connection> {
|
||||||
|
|
|
@ -15,7 +15,7 @@ import { NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
import type mysql2 from 'mysql2/promise';
|
import type mysql2 from 'mysql2/promise';
|
||||||
|
|
||||||
import { copyInputItems, createConnection, searchTables } from './GenericFunctions';
|
import { createConnection, searchTables } from './GenericFunctions';
|
||||||
|
|
||||||
import { oldVersionNotice } from '@utils/descriptions';
|
import { oldVersionNotice } from '@utils/descriptions';
|
||||||
|
|
||||||
|
@ -344,7 +344,7 @@ export class MySqlV1 implements INodeType {
|
||||||
const table = this.getNodeParameter('table', 0, '', { extractValue: true }) as string;
|
const table = this.getNodeParameter('table', 0, '', { extractValue: true }) 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());
|
||||||
const insertItems = copyInputItems(items, columns);
|
const insertItems = this.helpers.copyInputItems(items, columns);
|
||||||
const insertPlaceholder = `(${columns.map((_column) => '?').join(',')})`;
|
const insertPlaceholder = `(${columns.map((_column) => '?').join(',')})`;
|
||||||
const options = this.getNodeParameter('options', 0);
|
const options = this.getNodeParameter('options', 0);
|
||||||
const insertIgnore = options.ignore as boolean;
|
const insertIgnore = options.ignore as boolean;
|
||||||
|
@ -387,7 +387,7 @@ export class MySqlV1 implements INodeType {
|
||||||
columns.unshift(updateKey);
|
columns.unshift(updateKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateItems = copyInputItems(items, columns);
|
const updateItems = this.helpers.copyInputItems(items, columns);
|
||||||
const updateSQL = `UPDATE ${table} SET ${columns
|
const updateSQL = `UPDATE ${table} SET ${columns
|
||||||
.map((column) => `${column} = ?`)
|
.map((column) => `${column} = ?`)
|
||||||
.join(',')} WHERE ${updateKey} = ?;`;
|
.join(',')} WHERE ${updateKey} = ?;`;
|
||||||
|
|
|
@ -16,7 +16,7 @@ import { AUTO_MAP, BATCH_MODE, DATA_MODE } from '../../helpers/interfaces';
|
||||||
|
|
||||||
import { updateDisplayOptions } from '@utils/utilities';
|
import { updateDisplayOptions } from '@utils/utilities';
|
||||||
|
|
||||||
import { copyInputItems, replaceEmptyStringsByNulls } from '../../helpers/utils';
|
import { replaceEmptyStringsByNulls } from '../../helpers/utils';
|
||||||
|
|
||||||
import { optionsCollection } from '../common.descriptions';
|
import { optionsCollection } from '../common.descriptions';
|
||||||
|
|
||||||
|
@ -146,7 +146,7 @@ export async function execute(
|
||||||
}, [] as string[]),
|
}, [] as string[]),
|
||||||
),
|
),
|
||||||
];
|
];
|
||||||
insertItems = copyInputItems(items, columns);
|
insertItems = this.helpers.copyInputItems(items, columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dataMode === DATA_MODE.MANUAL) {
|
if (dataMode === DATA_MODE.MANUAL) {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import type {
|
||||||
NodeExecutionWithMetadata,
|
NodeExecutionWithMetadata,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import { NodeOperationError, deepCopy } from 'n8n-workflow';
|
import { NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
Mysql2Pool,
|
Mysql2Pool,
|
||||||
|
@ -20,22 +20,6 @@ import type {
|
||||||
|
|
||||||
import { BATCH_MODE } from './interfaces';
|
import { BATCH_MODE } from './interfaces';
|
||||||
|
|
||||||
export function copyInputItems(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] = deepCopy(item.json[property]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newItem;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export const prepareQueryAndReplacements = (rawQuery: string, replacements?: QueryValues) => {
|
export const prepareQueryAndReplacements = (rawQuery: string, replacements?: QueryValues) => {
|
||||||
if (replacements === undefined) {
|
if (replacements === undefined) {
|
||||||
return { query: rawQuery, values: [] };
|
return { query: rawQuery, values: [] };
|
||||||
|
|
|
@ -1,31 +1,14 @@
|
||||||
import type { IDataObject, INodeExecutionData } from 'n8n-workflow';
|
|
||||||
import { deepCopy } from 'n8n-workflow';
|
|
||||||
|
|
||||||
import type snowflake from 'snowflake-sdk';
|
import type snowflake from 'snowflake-sdk';
|
||||||
|
|
||||||
export async function connect(conn: snowflake.Connection) {
|
export async function connect(conn: snowflake.Connection) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
conn.connect((err, _conn) => {
|
conn.connect((error) => (error ? reject(error) : resolve()));
|
||||||
if (!err) {
|
|
||||||
// @ts-ignore
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function destroy(conn: snowflake.Connection) {
|
export async function destroy(conn: snowflake.Connection) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<void>((resolve, reject) => {
|
||||||
conn.destroy((err, _conn) => {
|
conn.destroy((error) => (error ? reject(error) : resolve()));
|
||||||
if (!err) {
|
|
||||||
// @ts-ignore
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,33 +17,11 @@ export async function execute(
|
||||||
sqlText: string,
|
sqlText: string,
|
||||||
binds: snowflake.InsertBinds,
|
binds: snowflake.InsertBinds,
|
||||||
) {
|
) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise<any[] | undefined>((resolve, reject) => {
|
||||||
conn.execute({
|
conn.execute({
|
||||||
sqlText,
|
sqlText,
|
||||||
binds,
|
binds,
|
||||||
complete: (err, stmt, rows) => {
|
complete: (error, stmt, rows) => (error ? reject(error) : resolve(rows)),
|
||||||
if (!err) {
|
|
||||||
resolve(rows);
|
|
||||||
} else {
|
|
||||||
reject(err);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function copyInputItems(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] = deepCopy(item.json[property]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newItem;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ import type {
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import { connect, copyInputItems, destroy, execute } from './GenericFunctions';
|
import { connect, destroy, execute } from './GenericFunctions';
|
||||||
|
|
||||||
import snowflake from 'snowflake-sdk';
|
import snowflake from 'snowflake-sdk';
|
||||||
import { getResolvables } from '@utils/utilities';
|
import { getResolvables } from '@utils/utilities';
|
||||||
|
@ -207,7 +207,7 @@ export class Snowflake implements INodeType {
|
||||||
const query = `INSERT INTO ${table}(${columns.join(',')}) VALUES (${columns
|
const query = `INSERT INTO ${table}(${columns.join(',')}) VALUES (${columns
|
||||||
.map((_column) => '?')
|
.map((_column) => '?')
|
||||||
.join(',')})`;
|
.join(',')})`;
|
||||||
const data = copyInputItems(items, columns);
|
const data = this.helpers.copyInputItems(items, columns);
|
||||||
const binds = data.map((element) => Object.values(element));
|
const binds = data.map((element) => Object.values(element));
|
||||||
await execute(connection, query, binds as unknown as snowflake.InsertBinds);
|
await execute(connection, query, binds as unknown as snowflake.InsertBinds);
|
||||||
data.forEach((d, i) => {
|
data.forEach((d, i) => {
|
||||||
|
@ -236,7 +236,7 @@ export class Snowflake implements INodeType {
|
||||||
const query = `UPDATE ${table} SET ${columns
|
const query = `UPDATE ${table} SET ${columns
|
||||||
.map((column) => `${column} = ?`)
|
.map((column) => `${column} = ?`)
|
||||||
.join(',')} WHERE ${updateKey} = ?;`;
|
.join(',')} WHERE ${updateKey} = ?;`;
|
||||||
const data = copyInputItems(items, columns);
|
const data = this.helpers.copyInputItems(items, columns);
|
||||||
const binds = data.map((element) => Object.values(element).concat(element[updateKey]));
|
const binds = data.map((element) => Object.values(element).concat(element[updateKey]));
|
||||||
for (let i = 0; i < binds.length; i++) {
|
for (let i = 0; i < binds.length; i++) {
|
||||||
await execute(connection, query, binds[i] as unknown as snowflake.InsertBinds);
|
await execute(connection, query, binds[i] as unknown as snowflake.InsertBinds);
|
||||||
|
|
|
@ -806,6 +806,7 @@ export type IExecuteFunctions = ExecuteFunctions.GetNodeParameterFn &
|
||||||
): NodeExecutionWithMetadata[];
|
): NodeExecutionWithMetadata[];
|
||||||
assertBinaryData(itemIndex: number, propertyName: string): IBinaryData;
|
assertBinaryData(itemIndex: number, propertyName: string): IBinaryData;
|
||||||
getBinaryDataBuffer(itemIndex: number, propertyName: string): Promise<Buffer>;
|
getBinaryDataBuffer(itemIndex: number, propertyName: string): Promise<Buffer>;
|
||||||
|
copyInputItems(items: INodeExecutionData[], properties: string[]): IDataObject[];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue