feat(MySQL Node): Overhaul
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import * as deleteTable from './deleteTable.operation';
|
||||
import * as executeQuery from './executeQuery.operation';
|
||||
import * as insert from './insert.operation';
|
||||
import * as select from './select.operation';
|
||||
import * as update from './update.operation';
|
||||
import * as upsert from './upsert.operation';
|
||||
import { tableRLC } from '../common.descriptions';
|
||||
|
||||
export { deleteTable, executeQuery, insert, select, update, upsert };
|
||||
|
||||
export const description: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Delete',
|
||||
value: 'deleteTable',
|
||||
description: 'Delete an entire table or rows in a table',
|
||||
action: 'Delete table or rows',
|
||||
},
|
||||
{
|
||||
name: 'Execute SQL',
|
||||
value: 'executeQuery',
|
||||
description: 'Execute an SQL query',
|
||||
action: 'Execute a SQL query',
|
||||
},
|
||||
{
|
||||
name: 'Insert',
|
||||
value: 'insert',
|
||||
description: 'Insert rows in a table',
|
||||
action: 'Insert rows in a table',
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-option-name-wrong-for-upsert
|
||||
name: 'Insert or Update',
|
||||
value: 'upsert',
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-description-wrong-for-upsert
|
||||
description: 'Insert or update rows in a table',
|
||||
action: 'Insert or update rows in a table',
|
||||
},
|
||||
{
|
||||
name: 'Select',
|
||||
value: 'select',
|
||||
description: 'Select rows from a table',
|
||||
action: 'Select rows from a table',
|
||||
},
|
||||
{
|
||||
name: 'Update',
|
||||
value: 'update',
|
||||
description: 'Update rows in a table',
|
||||
action: 'Update rows in a table',
|
||||
},
|
||||
],
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
},
|
||||
},
|
||||
default: 'insert',
|
||||
},
|
||||
{
|
||||
...tableRLC,
|
||||
displayOptions: { hide: { operation: ['executeQuery'] } },
|
||||
},
|
||||
...deleteTable.description,
|
||||
...executeQuery.description,
|
||||
...insert.description,
|
||||
...select.description,
|
||||
...update.description,
|
||||
...upsert.description,
|
||||
];
|
||||
@@ -0,0 +1,137 @@
|
||||
import type { IExecuteFunctions } from 'n8n-core';
|
||||
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import type {
|
||||
QueryRunner,
|
||||
QueryValues,
|
||||
QueryWithValues,
|
||||
WhereClause,
|
||||
} from '../../helpers/interfaces';
|
||||
|
||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||
|
||||
import { addWhereClauses } from '../../helpers/utils';
|
||||
|
||||
import {
|
||||
optionsCollection,
|
||||
selectRowsFixedCollection,
|
||||
combineConditionsCollection,
|
||||
} from '../common.descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Command',
|
||||
name: 'deleteCommand',
|
||||
type: 'options',
|
||||
default: 'truncate',
|
||||
options: [
|
||||
{
|
||||
name: 'Truncate',
|
||||
value: 'truncate',
|
||||
description: "Only removes the table's data and preserves the table's structure",
|
||||
},
|
||||
{
|
||||
name: 'Delete',
|
||||
value: 'delete',
|
||||
description:
|
||||
"Delete the rows that match the 'Select Rows' conditions below. If no selection is made, all rows in the table are deleted.",
|
||||
},
|
||||
{
|
||||
name: 'Drop',
|
||||
value: 'drop',
|
||||
description: "Deletes the table's data and also the table's structure permanently",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
...selectRowsFixedCollection,
|
||||
displayOptions: {
|
||||
show: {
|
||||
deleteCommand: ['delete'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
...combineConditionsCollection,
|
||||
displayOptions: {
|
||||
show: {
|
||||
deleteCommand: ['delete'],
|
||||
},
|
||||
},
|
||||
},
|
||||
optionsCollection,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
operation: ['deleteTable'],
|
||||
},
|
||||
hide: {
|
||||
table: [''],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
inputItems: INodeExecutionData[],
|
||||
runQueries: QueryRunner,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
|
||||
const queries: QueryWithValues[] = [];
|
||||
|
||||
for (let i = 0; i < inputItems.length; i++) {
|
||||
const table = this.getNodeParameter('table', i, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
const deleteCommand = this.getNodeParameter('deleteCommand', i) as string;
|
||||
|
||||
let query = '';
|
||||
let values: QueryValues = [];
|
||||
|
||||
if (deleteCommand === 'drop') {
|
||||
query = `DROP TABLE IF EXISTS \`${table}\``;
|
||||
}
|
||||
|
||||
if (deleteCommand === 'truncate') {
|
||||
query = `TRUNCATE TABLE \`${table}\``;
|
||||
}
|
||||
|
||||
if (deleteCommand === 'delete') {
|
||||
const whereClauses =
|
||||
((this.getNodeParameter('where', i, []) as IDataObject).values as WhereClause[]) || [];
|
||||
|
||||
const combineConditions = this.getNodeParameter('combineConditions', i, 'AND') as string;
|
||||
|
||||
[query, values] = addWhereClauses(
|
||||
this.getNode(),
|
||||
i,
|
||||
`DELETE FROM \`${table}\``,
|
||||
whereClauses,
|
||||
values,
|
||||
combineConditions,
|
||||
);
|
||||
}
|
||||
|
||||
if (query === '') {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
'Invalid delete command, only drop, delete and truncate are supported ',
|
||||
{ itemIndex: i },
|
||||
);
|
||||
}
|
||||
|
||||
const queryWithValues = { query, values };
|
||||
|
||||
queries.push(queryWithValues);
|
||||
}
|
||||
|
||||
returnData = await runQueries(queries);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import type { IExecuteFunctions } from 'n8n-core';
|
||||
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
import { NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import type { QueryRunner, QueryWithValues } from '../../helpers/interfaces';
|
||||
|
||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||
|
||||
import { prepareQueryAndReplacements, replaceEmptyStringsByNulls } from '../../helpers/utils';
|
||||
|
||||
import { optionsCollection } from '../common.descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Query',
|
||||
name: 'query',
|
||||
type: 'string',
|
||||
default: '',
|
||||
placeholder: 'e.g. SELECT id, name FROM product WHERE id < 40',
|
||||
required: true,
|
||||
description:
|
||||
"The SQL query to execute. You can use n8n expressions and $1, $2, $3, etc to refer to the 'Query Parameters' set in options below.",
|
||||
typeOptions: {
|
||||
rows: 3,
|
||||
},
|
||||
hint: 'Prefer using query parameters over n8n expressions to avoid SQL injection attacks',
|
||||
},
|
||||
{
|
||||
displayName: `
|
||||
To use query parameters in your SQL query, reference them as $1, $2, $3, etc in the corresponding order. <a target="_blank" href="https://docs.n8n.io/integrations/builtin/app-nodes/n8n-nodes-base.mysql/">More info</a>.
|
||||
`,
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
},
|
||||
optionsCollection,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
operation: ['executeQuery'],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
inputItems: INodeExecutionData[],
|
||||
runQueries: QueryRunner,
|
||||
nodeOptions: IDataObject,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
const items = replaceEmptyStringsByNulls(inputItems, nodeOptions.replaceEmptyStrings as boolean);
|
||||
|
||||
const queries: QueryWithValues[] = [];
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const rawQuery = this.getNodeParameter('query', i) as string;
|
||||
|
||||
const options = this.getNodeParameter('options', i, {});
|
||||
|
||||
let values;
|
||||
let queryReplacement = options.queryReplacement || [];
|
||||
|
||||
if (typeof queryReplacement === 'string') {
|
||||
queryReplacement = queryReplacement.split(',').map((entry) => entry.trim());
|
||||
}
|
||||
|
||||
if (Array.isArray(queryReplacement)) {
|
||||
values = queryReplacement as IDataObject[];
|
||||
} else {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
'Query Replacement must be a string of comma-separated values, or an array of values',
|
||||
{ itemIndex: i },
|
||||
);
|
||||
}
|
||||
|
||||
const preparedQuery = prepareQueryAndReplacements(rawQuery, values);
|
||||
|
||||
queries.push(preparedQuery);
|
||||
}
|
||||
|
||||
returnData = await runQueries(queries);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
import type { IExecuteFunctions } from 'n8n-core';
|
||||
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import type {
|
||||
QueryMode,
|
||||
QueryRunner,
|
||||
QueryValues,
|
||||
QueryWithValues,
|
||||
} from '../../helpers/interfaces';
|
||||
|
||||
import { AUTO_MAP, BATCH_MODE, DATA_MODE } from '../../helpers/interfaces';
|
||||
|
||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||
|
||||
import { copyInputItems, replaceEmptyStringsByNulls } from '../../helpers/utils';
|
||||
|
||||
import { optionsCollection } from '../common.descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Data Mode',
|
||||
name: 'dataMode',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Auto-Map Input Data to Columns',
|
||||
value: DATA_MODE.AUTO_MAP,
|
||||
description: 'Use when node input properties names exactly match the table column names',
|
||||
},
|
||||
{
|
||||
name: 'Map Each Column Manually',
|
||||
value: DATA_MODE.MANUAL,
|
||||
description: 'Set the value for each destination column manually',
|
||||
},
|
||||
],
|
||||
default: AUTO_MAP,
|
||||
description:
|
||||
'Whether to map node input properties and the table data automatically or manually',
|
||||
},
|
||||
{
|
||||
displayName: `
|
||||
In this mode, make sure incoming data fields are named the same as the columns in your table. If needed, use a 'Set' node before this node to change the field names.
|
||||
`,
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.AUTO_MAP],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Values to Send',
|
||||
name: 'valuesToSend',
|
||||
placeholder: 'Add Value',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValueButtonText: 'Add Value',
|
||||
multipleValues: true,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.MANUAL],
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
|
||||
displayName: 'Column',
|
||||
name: 'column',
|
||||
type: 'options',
|
||||
description:
|
||||
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getColumns',
|
||||
loadOptionsDependsOn: ['table.value'],
|
||||
},
|
||||
default: [],
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'value',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
optionsCollection,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
operation: ['insert'],
|
||||
},
|
||||
hide: {
|
||||
table: [''],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
inputItems: INodeExecutionData[],
|
||||
runQueries: QueryRunner,
|
||||
nodeOptions: IDataObject,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
const items = replaceEmptyStringsByNulls(inputItems, nodeOptions.replaceEmptyStrings as boolean);
|
||||
|
||||
const table = this.getNodeParameter('table', 0, '', { extractValue: true }) as string;
|
||||
|
||||
const dataMode = this.getNodeParameter('dataMode', 0) as string;
|
||||
const queryBatching = (nodeOptions.queryBatching as QueryMode) || BATCH_MODE.SINGLE;
|
||||
|
||||
const queries: QueryWithValues[] = [];
|
||||
|
||||
if (queryBatching === BATCH_MODE.SINGLE) {
|
||||
let columns: string[] = [];
|
||||
let insertItems: IDataObject[] = [];
|
||||
|
||||
const priority = (nodeOptions.priority as string) || '';
|
||||
const ignore = (nodeOptions.skipOnConflict as boolean) ? 'IGNORE' : '';
|
||||
|
||||
if (dataMode === DATA_MODE.AUTO_MAP) {
|
||||
columns = [
|
||||
...new Set(
|
||||
items.reduce((acc, item) => {
|
||||
const itemColumns = Object.keys(item.json);
|
||||
|
||||
return acc.concat(itemColumns);
|
||||
}, [] as string[]),
|
||||
),
|
||||
];
|
||||
insertItems = copyInputItems(items, columns);
|
||||
}
|
||||
|
||||
if (dataMode === DATA_MODE.MANUAL) {
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const valuesToSend = (this.getNodeParameter('valuesToSend', i, []) as IDataObject)
|
||||
.values as IDataObject[];
|
||||
|
||||
const item = valuesToSend.reduce((acc, { column, value }) => {
|
||||
acc[column as string] = value;
|
||||
return acc;
|
||||
}, {} as IDataObject);
|
||||
|
||||
insertItems.push(item);
|
||||
}
|
||||
columns = [
|
||||
...new Set(
|
||||
insertItems.reduce((acc, item) => {
|
||||
const itemColumns = Object.keys(item);
|
||||
|
||||
return acc.concat(itemColumns);
|
||||
}, [] as string[]),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
const escapedColumns = columns.map((column) => `\`${column}\``).join(', ');
|
||||
const placeholder = `(${columns.map(() => '?').join(',')})`;
|
||||
const replacements = items.map(() => placeholder).join(',');
|
||||
|
||||
const query = `INSERT ${priority} ${ignore} INTO \`${table}\` (${escapedColumns}) VALUES ${replacements}`;
|
||||
|
||||
const values = insertItems.reduce(
|
||||
(acc: IDataObject[], item) => acc.concat(Object.values(item) as IDataObject[]),
|
||||
[],
|
||||
);
|
||||
|
||||
queries.push({ query, values });
|
||||
} else {
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
let columns: string[] = [];
|
||||
let insertItem: IDataObject = {};
|
||||
|
||||
const options = this.getNodeParameter('options', i);
|
||||
const priority = (options.priority as string) || '';
|
||||
const ignore = (options.skipOnConflict as boolean) ? 'IGNORE' : '';
|
||||
|
||||
if (dataMode === DATA_MODE.AUTO_MAP) {
|
||||
columns = Object.keys(items[i].json);
|
||||
insertItem = columns.reduce((acc, key) => {
|
||||
if (columns.includes(key)) {
|
||||
acc[key] = items[i].json[key];
|
||||
}
|
||||
return acc;
|
||||
}, {} as IDataObject);
|
||||
}
|
||||
|
||||
if (dataMode === DATA_MODE.MANUAL) {
|
||||
const valuesToSend = (this.getNodeParameter('valuesToSend', i, []) as IDataObject)
|
||||
.values as IDataObject[];
|
||||
|
||||
insertItem = valuesToSend.reduce((acc, { column, value }) => {
|
||||
acc[column as string] = value;
|
||||
return acc;
|
||||
}, {} as IDataObject);
|
||||
|
||||
columns = Object.keys(insertItem);
|
||||
}
|
||||
|
||||
const escapedColumns = columns.map((column) => `\`${column}\``).join(', ');
|
||||
const placeholder = `(${columns.map(() => '?').join(',')})`;
|
||||
|
||||
const query = `INSERT ${priority} ${ignore} INTO \`${table}\` (${escapedColumns}) VALUES ${placeholder};`;
|
||||
|
||||
const values = Object.values(insertItem) as QueryValues;
|
||||
|
||||
queries.push({ query, values });
|
||||
}
|
||||
}
|
||||
|
||||
returnData = await runQueries(queries);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
import type { IExecuteFunctions } from 'n8n-core';
|
||||
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import type {
|
||||
QueryRunner,
|
||||
QueryValues,
|
||||
QueryWithValues,
|
||||
SortRule,
|
||||
WhereClause,
|
||||
} from '../../helpers/interfaces';
|
||||
|
||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||
|
||||
import { addSortRules, addWhereClauses } from '../../helpers/utils';
|
||||
|
||||
import {
|
||||
optionsCollection,
|
||||
sortFixedCollection,
|
||||
selectRowsFixedCollection,
|
||||
combineConditionsCollection,
|
||||
} from '../common.descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Whether to return all results or only up to a given limit',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['event'],
|
||||
operation: ['getAll'],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
default: 50,
|
||||
description: 'Max number of results to return',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
returnAll: [false],
|
||||
},
|
||||
},
|
||||
},
|
||||
selectRowsFixedCollection,
|
||||
combineConditionsCollection,
|
||||
sortFixedCollection,
|
||||
optionsCollection,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
operation: ['select'],
|
||||
},
|
||||
hide: {
|
||||
table: [''],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
inputItems: INodeExecutionData[],
|
||||
runQueries: QueryRunner,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
|
||||
const queries: QueryWithValues[] = [];
|
||||
|
||||
for (let i = 0; i < inputItems.length; i++) {
|
||||
const table = this.getNodeParameter('table', i, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
const outputColumns = this.getNodeParameter('options.outputColumns', i, ['*']) as string[];
|
||||
const selectDistinct = this.getNodeParameter('options.selectDistinct', i, false) as boolean;
|
||||
|
||||
let query = '';
|
||||
const SELECT = selectDistinct ? 'SELECT DISTINCT' : 'SELECT';
|
||||
|
||||
if (outputColumns.includes('*')) {
|
||||
query = `${SELECT} * FROM \`${table}\``;
|
||||
} else {
|
||||
const escapedColumns = outputColumns.map((column) => `\`${column}\``).join(', ');
|
||||
query = `${SELECT} ${escapedColumns} FROM \`${table}\``;
|
||||
}
|
||||
|
||||
let values: QueryValues = [];
|
||||
|
||||
const whereClauses =
|
||||
((this.getNodeParameter('where', i, []) as IDataObject).values as WhereClause[]) || [];
|
||||
|
||||
const combineConditions = this.getNodeParameter('combineConditions', i, 'AND') as string;
|
||||
|
||||
[query, values] = addWhereClauses(
|
||||
this.getNode(),
|
||||
i,
|
||||
query,
|
||||
whereClauses,
|
||||
values,
|
||||
combineConditions,
|
||||
);
|
||||
|
||||
const sortRules =
|
||||
((this.getNodeParameter('sort', i, []) as IDataObject).values as SortRule[]) || [];
|
||||
|
||||
[query, values] = addSortRules(query, sortRules, values);
|
||||
|
||||
const returnAll = this.getNodeParameter('returnAll', i, false);
|
||||
if (!returnAll) {
|
||||
const limit = this.getNodeParameter('limit', i, 50);
|
||||
query += ' LIMIT ?';
|
||||
values.push(limit);
|
||||
}
|
||||
|
||||
queries.push({ query, values });
|
||||
}
|
||||
|
||||
returnData = await runQueries(queries);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
import type { IExecuteFunctions } from 'n8n-core';
|
||||
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import type { QueryRunner, QueryValues, QueryWithValues } from '../../helpers/interfaces';
|
||||
import { AUTO_MAP, DATA_MODE } from '../../helpers/interfaces';
|
||||
|
||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||
|
||||
import { replaceEmptyStringsByNulls } from '../../helpers/utils';
|
||||
|
||||
import { optionsCollection } from '../common.descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Data Mode',
|
||||
name: 'dataMode',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Auto-Map Input Data to Columns',
|
||||
value: DATA_MODE.AUTO_MAP,
|
||||
description: 'Use when node input properties names exactly match the table column names',
|
||||
},
|
||||
{
|
||||
name: 'Map Each Column Below',
|
||||
value: DATA_MODE.MANUAL,
|
||||
description: 'Set the value for each destination column manually',
|
||||
},
|
||||
],
|
||||
default: AUTO_MAP,
|
||||
description:
|
||||
'Whether to map node input properties and the table data automatically or manually',
|
||||
},
|
||||
{
|
||||
displayName: `
|
||||
In this mode, make sure incoming data fields are named the same as the columns in your table. If needed, use a 'Set' node before this node to change the field names.
|
||||
`,
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.AUTO_MAP],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased, n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
|
||||
displayName: 'Column to Match On',
|
||||
name: 'columnToMatchOn',
|
||||
type: 'options',
|
||||
required: true,
|
||||
description:
|
||||
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getColumns',
|
||||
loadOptionsDependsOn: ['schema.value', 'table.value'],
|
||||
},
|
||||
default: '',
|
||||
hint: "Used to find the correct row to update. Doesn't get changed.",
|
||||
},
|
||||
{
|
||||
displayName: 'Value of Column to Match On',
|
||||
name: 'valueToMatchOn',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description:
|
||||
'Rows with a value in the specified "Column to Match On" that corresponds to the value in this field will be updated',
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.MANUAL],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Values to Send',
|
||||
name: 'valuesToSend',
|
||||
placeholder: 'Add Value',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValueButtonText: 'Add Value',
|
||||
multipleValues: true,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.MANUAL],
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
|
||||
displayName: 'Column',
|
||||
name: 'column',
|
||||
type: 'options',
|
||||
description:
|
||||
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getColumnsWithoutColumnToMatchOn',
|
||||
loadOptionsDependsOn: ['schema.value', 'table.value'],
|
||||
},
|
||||
default: [],
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'value',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
optionsCollection,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
operation: ['update'],
|
||||
},
|
||||
hide: {
|
||||
table: [''],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
inputItems: INodeExecutionData[],
|
||||
runQueries: QueryRunner,
|
||||
nodeOptions: IDataObject,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
const items = replaceEmptyStringsByNulls(inputItems, nodeOptions.replaceEmptyStrings as boolean);
|
||||
|
||||
const queries: QueryWithValues[] = [];
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const table = this.getNodeParameter('table', i, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
const columnToMatchOn = this.getNodeParameter('columnToMatchOn', i) as string;
|
||||
|
||||
const dataMode = this.getNodeParameter('dataMode', i) as string;
|
||||
|
||||
let item: IDataObject = {};
|
||||
let valueToMatchOn: string | IDataObject = '';
|
||||
|
||||
if (dataMode === DATA_MODE.AUTO_MAP) {
|
||||
item = items[i].json;
|
||||
valueToMatchOn = item[columnToMatchOn] as string;
|
||||
}
|
||||
|
||||
if (dataMode === DATA_MODE.MANUAL) {
|
||||
const valuesToSend = (this.getNodeParameter('valuesToSend', i, []) as IDataObject)
|
||||
.values as IDataObject[];
|
||||
|
||||
item = valuesToSend.reduce((acc, { column, value }) => {
|
||||
acc[column as string] = value;
|
||||
return acc;
|
||||
}, {} as IDataObject);
|
||||
|
||||
valueToMatchOn = this.getNodeParameter('valueToMatchOn', i) as string;
|
||||
}
|
||||
|
||||
const values: QueryValues = [];
|
||||
|
||||
const updateColumns = Object.keys(item).filter((column) => column !== columnToMatchOn);
|
||||
|
||||
const updates: string[] = [];
|
||||
|
||||
for (const column of updateColumns) {
|
||||
updates.push(`\`${column}\` = ?`);
|
||||
values.push(item[column] as string);
|
||||
}
|
||||
|
||||
const condition = `\`${columnToMatchOn}\` = ?`;
|
||||
values.push(valueToMatchOn);
|
||||
|
||||
const query = `UPDATE \`${table}\` SET ${updates.join(', ')} WHERE ${condition}`;
|
||||
|
||||
queries.push({ query, values });
|
||||
}
|
||||
|
||||
returnData = await runQueries(queries);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
import type { IExecuteFunctions } from 'n8n-core';
|
||||
import type { IDataObject, INodeExecutionData, INodeProperties } from 'n8n-workflow';
|
||||
|
||||
import type { QueryRunner, QueryValues, QueryWithValues } from '../../helpers/interfaces';
|
||||
import { AUTO_MAP, DATA_MODE } from '../../helpers/interfaces';
|
||||
|
||||
import { updateDisplayOptions } from '../../../../../utils/utilities';
|
||||
|
||||
import { replaceEmptyStringsByNulls } from '../../helpers/utils';
|
||||
|
||||
import { optionsCollection } from '../common.descriptions';
|
||||
|
||||
const properties: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Data Mode',
|
||||
name: 'dataMode',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Auto-Map Input Data to Columns',
|
||||
value: DATA_MODE.AUTO_MAP,
|
||||
description: 'Use when node input properties names exactly match the table column names',
|
||||
},
|
||||
{
|
||||
name: 'Map Each Column Below',
|
||||
value: DATA_MODE.MANUAL,
|
||||
description: 'Set the value for each destination column manually',
|
||||
},
|
||||
],
|
||||
default: AUTO_MAP,
|
||||
description:
|
||||
'Whether to map node input properties and the table data automatically or manually',
|
||||
},
|
||||
{
|
||||
displayName: `
|
||||
In this mode, make sure incoming data fields are named the same as the columns in your table. If needed, use a 'Set' node before this node to change the field names.
|
||||
`,
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.AUTO_MAP],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased, n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
|
||||
displayName: 'Column to Match On',
|
||||
name: 'columnToMatchOn',
|
||||
type: 'options',
|
||||
required: true,
|
||||
description:
|
||||
'The column to compare when finding the rows to update. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>.',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getColumns',
|
||||
loadOptionsDependsOn: ['schema.value', 'table.value'],
|
||||
},
|
||||
default: '',
|
||||
hint: "Used to find the correct row to update. Doesn't get changed. Has to be unique.",
|
||||
},
|
||||
{
|
||||
displayName: 'Value of Column to Match On',
|
||||
name: 'valueToMatchOn',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description:
|
||||
'Rows with a value in the specified "Column to Match On" that corresponds to the value in this field will be updated. New rows will be created for non-matching items.',
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.MANUAL],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Values to Send',
|
||||
name: 'valuesToSend',
|
||||
placeholder: 'Add Value',
|
||||
type: 'fixedCollection',
|
||||
typeOptions: {
|
||||
multipleValueButtonText: 'Add Value',
|
||||
multipleValues: true,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
dataMode: [DATA_MODE.MANUAL],
|
||||
},
|
||||
},
|
||||
default: {},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Values',
|
||||
name: 'values',
|
||||
values: [
|
||||
{
|
||||
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-wrong-for-dynamic-options
|
||||
displayName: 'Column',
|
||||
name: 'column',
|
||||
type: 'options',
|
||||
description:
|
||||
'Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code-examples/expressions/">expression</a>',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getColumnsWithoutColumnToMatchOn',
|
||||
loadOptionsDependsOn: ['schema.value', 'table.value'],
|
||||
},
|
||||
default: [],
|
||||
},
|
||||
{
|
||||
displayName: 'Value',
|
||||
name: 'value',
|
||||
type: 'string',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
optionsCollection,
|
||||
];
|
||||
|
||||
const displayOptions = {
|
||||
show: {
|
||||
resource: ['database'],
|
||||
operation: ['upsert'],
|
||||
},
|
||||
hide: {
|
||||
table: [''],
|
||||
},
|
||||
};
|
||||
|
||||
export const description = updateDisplayOptions(displayOptions, properties);
|
||||
|
||||
export async function execute(
|
||||
this: IExecuteFunctions,
|
||||
inputItems: INodeExecutionData[],
|
||||
runQueries: QueryRunner,
|
||||
nodeOptions: IDataObject,
|
||||
): Promise<INodeExecutionData[]> {
|
||||
let returnData: INodeExecutionData[] = [];
|
||||
|
||||
const items = replaceEmptyStringsByNulls(inputItems, nodeOptions.replaceEmptyStrings as boolean);
|
||||
|
||||
const queries: QueryWithValues[] = [];
|
||||
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const table = this.getNodeParameter('table', i, undefined, {
|
||||
extractValue: true,
|
||||
}) as string;
|
||||
|
||||
const columnToMatchOn = this.getNodeParameter('columnToMatchOn', i) as string;
|
||||
|
||||
const dataMode = this.getNodeParameter('dataMode', i) as string;
|
||||
|
||||
let item: IDataObject = {};
|
||||
|
||||
if (dataMode === DATA_MODE.AUTO_MAP) {
|
||||
item = items[i].json;
|
||||
}
|
||||
|
||||
if (dataMode === DATA_MODE.MANUAL) {
|
||||
const valuesToSend = (this.getNodeParameter('valuesToSend', i, []) as IDataObject)
|
||||
.values as IDataObject[];
|
||||
|
||||
item = valuesToSend.reduce((acc, { column, value }) => {
|
||||
acc[column as string] = value;
|
||||
return acc;
|
||||
}, {} as IDataObject);
|
||||
|
||||
item[columnToMatchOn] = this.getNodeParameter('valueToMatchOn', i) as string;
|
||||
}
|
||||
|
||||
const onConflict = 'ON DUPLICATE KEY UPDATE';
|
||||
|
||||
const columns = Object.keys(item);
|
||||
const escapedColumns = columns.map((column) => `\`${column}\``).join(', ');
|
||||
const placeholder = `${columns.map(() => '?').join(',')}`;
|
||||
|
||||
const insertQuery = `INSERT INTO \`${table}\`(${escapedColumns}) VALUES(${placeholder})`;
|
||||
|
||||
const values = Object.values(item) as QueryValues;
|
||||
|
||||
const updateColumns = Object.keys(item).filter((column) => column !== columnToMatchOn);
|
||||
|
||||
const updates: string[] = [];
|
||||
|
||||
for (const column of updateColumns) {
|
||||
updates.push(`\`${column}\` = ?`);
|
||||
values.push(item[column] as string);
|
||||
}
|
||||
|
||||
const query = `${insertQuery} ${onConflict} ${updates.join(', ')}`;
|
||||
|
||||
queries.push({ query, values });
|
||||
}
|
||||
|
||||
returnData = await runQueries(queries);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
Reference in New Issue
Block a user