2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2022-10-21 08:24:58 -07:00
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeListSearchResult,
|
|
|
|
} from 'n8n-workflow';
|
2022-10-12 02:54:38 -07:00
|
|
|
import mysql2 from 'mysql2/promise';
|
2019-11-19 00:36:56 -08:00
|
|
|
|
2022-12-02 12:54:28 -08:00
|
|
|
export async function createConnection(
|
2022-10-21 08:24:58 -07:00
|
|
|
credentials: ICredentialDataDecryptedObject,
|
|
|
|
): Promise<mysql2.Connection> {
|
2022-10-12 02:54:38 -07:00
|
|
|
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}'
|
2023-01-19 04:37:19 -08:00
|
|
|
and table_name like '%${query || ''}%'
|
2022-10-12 02:54:38 -07:00
|
|
|
ORDER BY table_name
|
|
|
|
`;
|
|
|
|
const [rows] = await connection.query(sql);
|
2022-10-21 08:24:58 -07:00
|
|
|
const results = (rows as IDataObject[]).map((r) => ({
|
2022-10-12 02:54:38 -07:00
|
|
|
name: r.TABLE_NAME as string,
|
|
|
|
value: r.TABLE_NAME as string,
|
|
|
|
}));
|
2022-12-02 12:54:28 -08:00
|
|
|
await connection.end();
|
2022-10-12 02:54:38 -07:00
|
|
|
return { results };
|
|
|
|
}
|