2023-04-03 08:18:01 -07:00
|
|
|
import type { ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';
|
2023-05-19 06:42:24 -07:00
|
|
|
|
2023-04-03 08:18:01 -07:00
|
|
|
import { getTableSchema } from '../helpers/utils';
|
2023-05-19 06:42:24 -07:00
|
|
|
import { configurePostgres } from '../transport';
|
2023-04-03 08:18:01 -07:00
|
|
|
|
|
|
|
export async function getColumns(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
|
|
|
const credentials = await this.getCredentials('postgres');
|
2023-05-04 08:25:54 -07:00
|
|
|
const options = { nodeVersion: this.getNode().typeVersion };
|
2023-04-03 08:18:01 -07:00
|
|
|
|
2023-09-01 12:19:10 -07:00
|
|
|
const { db, sshClient } = await configurePostgres(credentials, options);
|
2023-04-03 08:18:01 -07:00
|
|
|
|
|
|
|
const schema = this.getNodeParameter('schema', 0, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
|
|
|
|
|
|
|
const table = this.getNodeParameter('table', 0, {
|
|
|
|
extractValue: true,
|
|
|
|
}) as string;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const columns = await getTableSchema(db, schema, table);
|
|
|
|
|
|
|
|
return columns.map((column) => ({
|
|
|
|
name: column.column_name,
|
|
|
|
value: column.column_name,
|
|
|
|
description: `Type: ${column.data_type.toUpperCase()}, Nullable: ${column.is_nullable}`,
|
|
|
|
}));
|
|
|
|
} catch (error) {
|
|
|
|
throw error;
|
2023-05-19 06:42:24 -07:00
|
|
|
} finally {
|
|
|
|
if (sshClient) {
|
|
|
|
sshClient.end();
|
|
|
|
}
|
2023-09-01 12:19:10 -07:00
|
|
|
await db.$pool.end();
|
2023-04-03 08:18:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getColumnsMultiOptions(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const returnData = await getColumns.call(this);
|
|
|
|
const returnAll = { name: '*', value: '*', description: 'All columns' };
|
|
|
|
return [returnAll, ...returnData];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getColumnsWithoutColumnToMatchOn(
|
|
|
|
this: ILoadOptionsFunctions,
|
|
|
|
): Promise<INodePropertyOptions[]> {
|
|
|
|
const columnToMatchOn = this.getNodeParameter('columnToMatchOn') as string;
|
|
|
|
const returnData = await getColumns.call(this);
|
|
|
|
return returnData.filter((column) => column.value !== columnToMatchOn);
|
|
|
|
}
|