2023-04-03 08:18:01 -07:00
|
|
|
import type { IExecuteFunctions, INodeExecutionData } from 'n8n-workflow';
|
|
|
|
import { NodeOperationError } from 'n8n-workflow';
|
|
|
|
|
2023-09-19 03:16:35 -07:00
|
|
|
import { configurePostgres } from '../transport';
|
|
|
|
import { configureQueryRunner } from '../helpers/utils';
|
2023-04-03 08:18:01 -07:00
|
|
|
import type { PostgresType } from './node.type';
|
|
|
|
|
|
|
|
import * as database from './database/Database.resource';
|
2024-04-09 08:41:51 -07:00
|
|
|
import type { PostgresNodeCredentials, PostgresNodeOptions } from '../helpers/interfaces';
|
2023-04-03 08:18:01 -07:00
|
|
|
|
|
|
|
export async function router(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
let returnData: INodeExecutionData[] = [];
|
|
|
|
|
|
|
|
const items = this.getInputData();
|
|
|
|
const resource = this.getNodeParameter<PostgresType>('resource', 0);
|
|
|
|
const operation = this.getNodeParameter('operation', 0);
|
|
|
|
|
2024-04-09 08:41:51 -07:00
|
|
|
const credentials = (await this.getCredentials('postgres')) as PostgresNodeCredentials;
|
|
|
|
const options = this.getNodeParameter('options', 0, {}) as PostgresNodeOptions;
|
2023-05-04 08:25:54 -07:00
|
|
|
options.nodeVersion = this.getNode().typeVersion;
|
2023-07-19 07:15:43 -07:00
|
|
|
options.operation = operation;
|
2023-04-03 08:18:01 -07:00
|
|
|
|
2023-05-19 06:42:24 -07:00
|
|
|
const { db, pgp, sshClient } = await configurePostgres(credentials, options);
|
2023-04-03 08:18:01 -07:00
|
|
|
|
2023-07-12 02:15:38 -07:00
|
|
|
const runQueries = configureQueryRunner.call(
|
|
|
|
this,
|
2023-04-03 08:18:01 -07:00
|
|
|
this.getNode(),
|
|
|
|
this.continueOnFail(),
|
|
|
|
pgp,
|
|
|
|
db,
|
|
|
|
);
|
|
|
|
|
|
|
|
const postgresNodeData = {
|
|
|
|
resource,
|
|
|
|
operation,
|
|
|
|
} as PostgresType;
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch (postgresNodeData.resource) {
|
|
|
|
case 'database':
|
|
|
|
returnData = await database[postgresNodeData.operation].execute.call(
|
|
|
|
this,
|
|
|
|
runQueries,
|
|
|
|
items,
|
|
|
|
options,
|
|
|
|
db,
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new NodeOperationError(
|
|
|
|
this.getNode(),
|
|
|
|
`The operation "${operation}" is not supported!`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
|
|
|
} finally {
|
|
|
|
if (sshClient) {
|
|
|
|
sshClient.end();
|
|
|
|
}
|
2023-09-01 12:19:10 -07:00
|
|
|
|
2024-02-21 02:27:21 -08:00
|
|
|
if (!db.$pool.ending) await db.$pool.end();
|
2023-04-03 08:18:01 -07:00
|
|
|
}
|
|
|
|
|
2023-09-05 03:59:02 -07:00
|
|
|
return [returnData];
|
2023-04-03 08:18:01 -07:00
|
|
|
}
|