From 9a06c6df251d5502b9f27320b49458cfb1ee3d57 Mon Sep 17 00:00:00 2001 From: Marcus <56945030+maspio@users.noreply.github.com> Date: Wed, 12 Oct 2022 11:54:38 +0200 Subject: [PATCH] feat(MySql Node): use resource locator component for table parameter (#4313) insert/update operation table parameter as resource locator --- .../nodes/MySql/GenericFunctions.ts | 44 ++++++- packages/nodes-base/nodes/MySql/MySql.node.ts | 107 +++++++++--------- 2 files changed, 99 insertions(+), 52 deletions(-) diff --git a/packages/nodes-base/nodes/MySql/GenericFunctions.ts b/packages/nodes-base/nodes/MySql/GenericFunctions.ts index 761289de76..bd49674e82 100644 --- a/packages/nodes-base/nodes/MySql/GenericFunctions.ts +++ b/packages/nodes-base/nodes/MySql/GenericFunctions.ts @@ -1,4 +1,5 @@ -import { IDataObject, INodeExecutionData } from 'n8n-workflow'; +import { ICredentialDataDecryptedObject, IDataObject, ILoadOptionsFunctions, INodeExecutionData, INodeListSearchResult } from 'n8n-workflow'; +import mysql2 from 'mysql2/promise'; /** * Returns of copy of the items which only contains the json data and @@ -22,3 +23,44 @@ export function copyInputItems(items: INodeExecutionData[], properties: string[] return newItem; }); } + +export function createConnection(credentials: ICredentialDataDecryptedObject): Promise { + const { ssl, caCertificate, clientCertificate, clientPrivateKey, ...baseCredentials } = + credentials; + + if (ssl) { + baseCredentials.ssl = {}; + + if (caCertificate) { + baseCredentials.ssl.ca = caCertificate; + } + + if (clientCertificate || clientPrivateKey) { + baseCredentials.ssl.cert = clientCertificate; + baseCredentials.ssl.key = clientPrivateKey; + } + } + + return mysql2.createConnection(baseCredentials); +} + +export async function searchTables( + this: ILoadOptionsFunctions, + query?: string, +): Promise { + const credentials = await this.getCredentials('mySql'); + const connection = await createConnection(credentials); + const sql = ` + SELECT table_name FROM information_schema.tables + WHERE table_schema = '${credentials.database}' + and table_name like '%${query || ''}%' + ORDER BY table_name + `; + const [rows] = await connection.query(sql); + const results = (rows as IDataObject[]).map(r => ({ + name: r.TABLE_NAME as string, + value: r.TABLE_NAME as string, + })); + connection.end(); + return { results }; +} diff --git a/packages/nodes-base/nodes/MySql/MySql.node.ts b/packages/nodes-base/nodes/MySql/MySql.node.ts index f9e793201f..c145c11454 100644 --- a/packages/nodes-base/nodes/MySql/MySql.node.ts +++ b/packages/nodes-base/nodes/MySql/MySql.node.ts @@ -12,7 +12,7 @@ import { // @ts-ignore import mysql2 from 'mysql2/promise'; -import { copyInputItems } from './GenericFunctions'; +import { copyInputItems, createConnection, searchTables } from './GenericFunctions'; import { IExecuteFunctions } from 'n8n-core'; export class MySql implements INodeType { @@ -91,14 +91,33 @@ export class MySql implements INodeType { { displayName: 'Table', name: 'table', - type: 'string', + type: 'resourceLocator', + default: { mode: 'list', value: '' }, + required: true, + modes: [ + { + displayName: 'From List', + name: 'list', + type: 'list', + placeholder: 'Select a Table...', + typeOptions: { + searchListMethod: 'searchTables', + searchFilterRequired: false, + searchable: true, + }, + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + placeholder: 'table_name', + }, + ], displayOptions: { show: { operation: ['insert'], }, }, - default: '', - required: true, description: 'Name of the table in which to insert data to', }, { @@ -167,14 +186,33 @@ export class MySql implements INodeType { { displayName: 'Table', name: 'table', - type: 'string', + type: 'resourceLocator', + default: { mode: 'list', value: '' }, + required: true, + modes: [ + { + displayName: 'From List', + name: 'list', + type: 'list', + placeholder: 'Select a Table...', + typeOptions: { + searchListMethod: 'searchTables', + searchFilterRequired: false, + searchable: true, + }, + }, + { + displayName: 'Name', + name: 'name', + type: 'string', + placeholder: 'table_name', + }, + ], displayOptions: { show: { operation: ['update'], }, }, - default: '', - required: true, description: 'Name of the table in which to update data in', }, { @@ -217,23 +255,7 @@ export class MySql implements INodeType { ): Promise { const credentials = credential.data as ICredentialDataDecryptedObject; try { - const { ssl, caCertificate, clientCertificate, clientPrivateKey, ...baseCredentials } = - credentials; - - if (ssl) { - baseCredentials.ssl = {}; - - if (caCertificate) { - baseCredentials.ssl.ca = caCertificate; - } - - if (clientCertificate || clientPrivateKey) { - baseCredentials.ssl.cert = clientCertificate; - baseCredentials.ssl.key = clientPrivateKey; - } - } - - const connection = await mysql2.createConnection(baseCredentials); + const connection = await createConnection(credentials); connection.end(); } catch (error) { return { @@ -247,30 +269,14 @@ export class MySql implements INodeType { }; }, }, + listSearch: { + searchTables, + }, }; async execute(this: IExecuteFunctions): Promise { const credentials = await this.getCredentials('mySql'); - - // Destructuring SSL configuration - const { ssl, caCertificate, clientCertificate, clientPrivateKey, ...baseCredentials } = - credentials; - - if (ssl) { - baseCredentials.ssl = {}; - - if (caCertificate) { - baseCredentials.ssl.ca = caCertificate; - } - - // client certificates might not be required - if (clientCertificate || clientPrivateKey) { - baseCredentials.ssl.cert = clientCertificate; - baseCredentials.ssl.key = clientPrivateKey; - } - } - - const connection = await mysql2.createConnection(baseCredentials); + const connection = await createConnection(credentials); const items = this.getInputData(); const operation = this.getNodeParameter('operation', 0) as string; let returnItems: INodeExecutionData[] = []; @@ -314,7 +320,7 @@ export class MySql implements INodeType { // ---------------------------------- try { - const table = this.getNodeParameter('table', 0) as string; + 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); @@ -323,11 +329,10 @@ export class MySql implements INodeType { const insertIgnore = options.ignore as boolean; const insertPriority = options.priority as string; - const insertSQL = `INSERT ${insertPriority || ''} ${ - insertIgnore ? 'IGNORE' : '' - } INTO ${table}(${columnString}) VALUES ${items - .map((item) => insertPlaceholder) - .join(',')};`; + const insertSQL = `INSERT ${insertPriority || ''} ${insertIgnore ? 'IGNORE' : '' + } INTO ${table}(${columnString}) VALUES ${items + .map((item) => insertPlaceholder) + .join(',')};`; const queryItems = insertItems.reduce( (collection, item) => collection.concat(Object.values(item as any)), // tslint:disable-line:no-any [], @@ -350,7 +355,7 @@ export class MySql implements INodeType { // ---------------------------------- try { - const table = this.getNodeParameter('table', 0) as string; + const table = this.getNodeParameter('table', 0, '', { extractValue: true }) as string; const updateKey = this.getNodeParameter('updateKey', 0) as string; const columnString = this.getNodeParameter('columns', 0) as string; const columns = columnString.split(',').map((column) => column.trim());