mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08:00
feat(MySql Node): use resource locator component for table parameter (#4313)
insert/update operation table parameter as resource locator
This commit is contained in:
parent
3e157f73a4
commit
9a06c6df25
|
@ -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<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 };
|
||||
}
|
||||
|
|
|
@ -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<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 {
|
||||
|
@ -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[] = [];
|
||||
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue