2022-10-21 08:24:58 -07:00
|
|
|
import {
|
|
|
|
deepCopy,
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
IDataObject,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
INodeExecutionData,
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns of copy of the items which only contains the json data and
|
|
|
|
* of that only the define properties
|
|
|
|
*
|
|
|
|
* @param {INodeExecutionData[]} items The items to copy
|
|
|
|
* @param {string[]} properties The properties it should include
|
|
|
|
*/
|
|
|
|
export function copyInputItems(items: INodeExecutionData[], properties: string[]): IDataObject[] {
|
|
|
|
// Prepare the data to insert and copy it to be returned
|
|
|
|
let newItem: IDataObject;
|
|
|
|
return items.map((item) => {
|
|
|
|
newItem = {};
|
|
|
|
for (const property of properties) {
|
|
|
|
if (item.json[property] === undefined) {
|
|
|
|
newItem[property] = null;
|
|
|
|
} else {
|
2022-10-21 08:24:58 -07:00
|
|
|
newItem[property] = deepCopy(item.json[property]);
|
2019-11-19 00:36:56 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newItem;
|
|
|
|
});
|
2022-08-17 08:50:24 -07:00
|
|
|
}
|
2022-10-12 02:54:38 -07: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-13 09:11:56 -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 };
|
|
|
|
}
|