refactor(core): Move copyInputItems to node helpers (no-changelog) (#7299)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-10-06 16:25:58 +02:00
committed by GitHub
parent 34bda535e6
commit 597669aa62
9 changed files with 84 additions and 95 deletions

View File

@@ -2,35 +2,10 @@ import type {
ICredentialDataDecryptedObject,
IDataObject,
ILoadOptionsFunctions,
INodeExecutionData,
INodeListSearchResult,
} from 'n8n-workflow';
import { deepCopy } from 'n8n-workflow';
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(
credentials: ICredentialDataDecryptedObject,
): Promise<mysql2.Connection> {

View File

@@ -15,7 +15,7 @@ import { NodeOperationError } from 'n8n-workflow';
import type mysql2 from 'mysql2/promise';
import { copyInputItems, createConnection, searchTables } from './GenericFunctions';
import { createConnection, searchTables } from './GenericFunctions';
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 columnString = this.getNodeParameter('columns', 0) as string;
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 options = this.getNodeParameter('options', 0);
const insertIgnore = options.ignore as boolean;
@@ -387,7 +387,7 @@ export class MySqlV1 implements INodeType {
columns.unshift(updateKey);
}
const updateItems = copyInputItems(items, columns);
const updateItems = this.helpers.copyInputItems(items, columns);
const updateSQL = `UPDATE ${table} SET ${columns
.map((column) => `${column} = ?`)
.join(',')} WHERE ${updateKey} = ?;`;

View File

@@ -16,7 +16,7 @@ import { AUTO_MAP, BATCH_MODE, DATA_MODE } from '../../helpers/interfaces';
import { updateDisplayOptions } from '@utils/utilities';
import { copyInputItems, replaceEmptyStringsByNulls } from '../../helpers/utils';
import { replaceEmptyStringsByNulls } from '../../helpers/utils';
import { optionsCollection } from '../common.descriptions';
@@ -146,7 +146,7 @@ export async function execute(
}, [] as string[]),
),
];
insertItems = copyInputItems(items, columns);
insertItems = this.helpers.copyInputItems(items, columns);
}
if (dataMode === DATA_MODE.MANUAL) {

View File

@@ -7,7 +7,7 @@ import type {
NodeExecutionWithMetadata,
} from 'n8n-workflow';
import { NodeOperationError, deepCopy } from 'n8n-workflow';
import { NodeOperationError } from 'n8n-workflow';
import type {
Mysql2Pool,
@@ -20,22 +20,6 @@ import type {
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) => {
if (replacements === undefined) {
return { query: rawQuery, values: [] };