2023-04-12 07:24:17 -07:00
|
|
|
import type { IDataObject, ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
|
|
|
|
import { Client } from 'ssh2';
|
|
|
|
import { createPool } from '../transport';
|
2024-01-10 05:41:00 -08:00
|
|
|
import { escapeSqlIdentifier } from '../helpers/utils';
|
2023-04-12 07:24:17 -07:00
|
|
|
|
|
|
|
export async function getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const credentials = await this.getCredentials('mySql');
|
|
|
|
const nodeOptions = this.getNodeParameter('options', 0) as IDataObject;
|
|
|
|
|
|
|
|
let sshClient: Client | undefined = undefined;
|
|
|
|
|
|
|
|
if (credentials.sshTunnel) {
|
|
|
|
sshClient = new Client();
|
|
|
|
}
|
|
|
|
const pool = await createPool(credentials, nodeOptions, sshClient);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const connection = await pool.getConnection();
|
|
|
|
|
|
|
|
const table = this.getNodeParameter('table', 0, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
|
|
|
|
|
|
|
const columns = (
|
|
|
|
await connection.query(
|
2024-01-10 05:41:00 -08:00
|
|
|
`SHOW COLUMNS FROM ${escapeSqlIdentifier(table)} FROM ${escapeSqlIdentifier(
|
|
|
|
credentials.database as string,
|
|
|
|
)}`,
|
2023-04-12 07:24:17 -07:00
|
|
|
)
|
|
|
|
)[0] as IDataObject[];
|
|
|
|
|
|
|
|
connection.release();
|
|
|
|
|
|
|
|
return (columns || []).map((column: IDataObject) => ({
|
|
|
|
name: column.Field as string,
|
|
|
|
value: column.Field as string,
|
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-lowercase-first-char
|
|
|
|
description: `type: ${(column.Type as string).toUpperCase()}, nullable: ${
|
|
|
|
column.Null as string
|
|
|
|
}`,
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
if (sshClient) {
|
|
|
|
sshClient.end();
|
|
|
|
}
|
|
|
|
await pool.end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getColumnsMultiOptions(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData = await getColumns.call(this);
|
|
|
|
const returnAll = { name: '*', value: '*', description: 'All columns' };
|
|
|
|
return [returnAll, ...returnData];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getColumnsWithoutColumnToMatchOn(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const columnToMatchOn = this.getNodeParameter('columnToMatchOn') as string;
|
|
|
|
const returnData = await getColumns.call(this);
|
|
|
|
return returnData.filter((column) => column.value !== columnToMatchOn);
|
|
|
|
}
|