mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-26 05:04:05 -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
|
* 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;
|
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
|
// @ts-ignore
|
||||||
import mysql2 from 'mysql2/promise';
|
import mysql2 from 'mysql2/promise';
|
||||||
|
|
||||||
import { copyInputItems } from './GenericFunctions';
|
import { copyInputItems, createConnection, searchTables } from './GenericFunctions';
|
||||||
import { IExecuteFunctions } from 'n8n-core';
|
import { IExecuteFunctions } from 'n8n-core';
|
||||||
|
|
||||||
export class MySql implements INodeType {
|
export class MySql implements INodeType {
|
||||||
|
@ -91,14 +91,33 @@ export class MySql implements INodeType {
|
||||||
{
|
{
|
||||||
displayName: 'Table',
|
displayName: 'Table',
|
||||||
name: 'table',
|
name: 'table',
|
||||||
|
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',
|
type: 'string',
|
||||||
|
placeholder: 'table_name',
|
||||||
|
},
|
||||||
|
],
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: ['insert'],
|
operation: ['insert'],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: '',
|
|
||||||
required: true,
|
|
||||||
description: 'Name of the table in which to insert data to',
|
description: 'Name of the table in which to insert data to',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -167,14 +186,33 @@ export class MySql implements INodeType {
|
||||||
{
|
{
|
||||||
displayName: 'Table',
|
displayName: 'Table',
|
||||||
name: 'table',
|
name: 'table',
|
||||||
|
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',
|
type: 'string',
|
||||||
|
placeholder: 'table_name',
|
||||||
|
},
|
||||||
|
],
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: ['update'],
|
operation: ['update'],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: '',
|
|
||||||
required: true,
|
|
||||||
description: 'Name of the table in which to update data in',
|
description: 'Name of the table in which to update data in',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -217,23 +255,7 @@ export class MySql implements INodeType {
|
||||||
): Promise<INodeCredentialTestResult> {
|
): Promise<INodeCredentialTestResult> {
|
||||||
const credentials = credential.data as ICredentialDataDecryptedObject;
|
const credentials = credential.data as ICredentialDataDecryptedObject;
|
||||||
try {
|
try {
|
||||||
const { ssl, caCertificate, clientCertificate, clientPrivateKey, ...baseCredentials } =
|
const connection = await createConnection(credentials);
|
||||||
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);
|
|
||||||
connection.end();
|
connection.end();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return {
|
return {
|
||||||
|
@ -247,30 +269,14 @@ export class MySql implements INodeType {
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
listSearch: {
|
||||||
|
searchTables,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const credentials = await this.getCredentials('mySql');
|
const credentials = await this.getCredentials('mySql');
|
||||||
|
const connection = await createConnection(credentials);
|
||||||
// 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 items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
let returnItems: INodeExecutionData[] = [];
|
let returnItems: INodeExecutionData[] = [];
|
||||||
|
@ -314,7 +320,7 @@ export class MySql implements INodeType {
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
try {
|
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 columnString = this.getNodeParameter('columns', 0) as string;
|
||||||
const columns = columnString.split(',').map((column) => column.trim());
|
const columns = columnString.split(',').map((column) => column.trim());
|
||||||
const insertItems = copyInputItems(items, columns);
|
const insertItems = copyInputItems(items, columns);
|
||||||
|
@ -323,8 +329,7 @@ export class MySql implements INodeType {
|
||||||
const insertIgnore = options.ignore as boolean;
|
const insertIgnore = options.ignore as boolean;
|
||||||
const insertPriority = options.priority as string;
|
const insertPriority = options.priority as string;
|
||||||
|
|
||||||
const insertSQL = `INSERT ${insertPriority || ''} ${
|
const insertSQL = `INSERT ${insertPriority || ''} ${insertIgnore ? 'IGNORE' : ''
|
||||||
insertIgnore ? 'IGNORE' : ''
|
|
||||||
} INTO ${table}(${columnString}) VALUES ${items
|
} INTO ${table}(${columnString}) VALUES ${items
|
||||||
.map((item) => insertPlaceholder)
|
.map((item) => insertPlaceholder)
|
||||||
.join(',')};`;
|
.join(',')};`;
|
||||||
|
@ -350,7 +355,7 @@ export class MySql implements INodeType {
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
|
||||||
try {
|
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 updateKey = this.getNodeParameter('updateKey', 0) as string;
|
||||||
const columnString = this.getNodeParameter('columns', 0) as string;
|
const columnString = this.getNodeParameter('columns', 0) as string;
|
||||||
const columns = columnString.split(',').map((column) => column.trim());
|
const columns = columnString.split(',').map((column) => column.trim());
|
||||||
|
|
Loading…
Reference in a new issue