Files
n8n-enterprise-unlocked/packages/nodes-base/nodes/MySql/v2/methods/listSearch.ts
Iván Ovejero 62c096710f refactor: Run lintfix (no-changelog) (#7537)
- Fix autofixable violations
- Remove unused directives
- Allow for PascalCased variables - needed for dynamically imported or
assigned classes, decorators, routers, etc.
2023-10-27 14:15:02 +02:00

44 lines
1.3 KiB
TypeScript

import type { IDataObject, ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
import { Client } from 'ssh2';
import { createPool } from '../transport';
export async function searchTables(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
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 query = 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = ?';
const values = [credentials.database as string];
const formatedQuery = connection.format(query, values);
const response = (await connection.query(formatedQuery))[0];
connection.release();
const results = (response as IDataObject[]).map((table) => ({
name: (table.table_name as string) || (table.TABLE_NAME as string),
value: (table.table_name as string) || (table.TABLE_NAME as string),
}));
return { results };
} catch (error) {
throw error;
} finally {
if (sshClient) {
sshClient.end();
}
await pool.end();
}
}