2023-07-12 02:15:38 -07:00
|
|
|
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
2023-04-12 07:24:17 -07:00
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
|
|
|
|
|
|
|
import { createPool } from '../transport';
|
2024-07-04 03:29:44 -07:00
|
|
|
import type { MysqlNodeCredentials, QueryRunner } from '../helpers/interfaces';
|
2023-04-12 07:24:17 -07:00
|
|
|
import { configureQueryRunner } from '../helpers/utils';
|
|
|
|
import * as database from './database/Database.resource';
|
|
|
|
import type { MySqlType } from './node.type';
|
|
|
|
|
|
|
|
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
let returnData: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
const resource = this.getNodeParameter<MySqlType>('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
const nodeOptions = this.getNodeParameter('options', 0);
|
|
|
|
|
2023-05-03 08:45:21 -07:00
|
|
|
nodeOptions.nodeVersion = this.getNode().typeVersion;
|
|
|
|
|
2024-08-27 06:23:58 -07:00
|
|
|
const credentials = await this.getCredentials<MysqlNodeCredentials>('mySql');
|
2023-04-12 07:24:17 -07:00
|
|
|
|
2024-07-04 03:29:44 -07:00
|
|
|
const pool = await createPool.call(this, credentials, nodeOptions);
|
2023-04-12 07:24:17 -07:00
|
|
|
|
|
|
|
const runQueries: QueryRunner = configureQueryRunner.call(this, nodeOptions, pool);
|
|
|
|
|
|
|
|
const mysqlNodeData = {
|
|
|
|
resource,
|
|
|
|
operation,
|
|
|
|
} as MySqlType;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (mysqlNodeData.resource) {
|
|
|
|
case 'database':
|
|
|
|
const items = this.getInputData();
|
|
|
|
|
|
|
|
returnData = await database[mysqlNodeData.operation].execute.call(
|
|
|
|
this,
|
|
|
|
items,
|
|
|
|
runQueries,
|
|
|
|
nodeOptions,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not supported!`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await pool.end();
|
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2023-04-12 07:24:17 -07:00
|
|
|
}
|