Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(MySql Node): use resource locator component for table parameter #4313

Merged
merged 1 commit into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion packages/nodes-base/nodes/MySql/GenericFunctions.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,3 +23,44 @@ export function copyInputItems(items: INodeExecutionData[], properties: string[]
return newItem;
});
}

export function createConnection(credentials: ICredentialDataDecryptedObject): Promise<mysql2.Connection> {
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<INodeListSearchResult> {
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 };
}
107 changes: 56 additions & 51 deletions packages/nodes-base/nodes/MySql/MySql.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
},
{
Expand Down Expand Up @@ -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',
},
{
Expand Down Expand Up @@ -217,23 +255,7 @@ export class MySql implements INodeType {
): Promise<INodeCredentialTestResult> {
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 {
Expand All @@ -247,30 +269,14 @@ export class MySql implements INodeType {
};
},
},
listSearch: {
searchTables,
},
};

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
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[] = [];
Expand Down Expand Up @@ -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);
Expand All @@ -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
[],
Expand All @@ -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());
Expand Down