refactor(Postgres Node): Backport connection pooling to postgres v1 (#12484)

This commit is contained in:
Danny Martini
2025-01-16 11:09:12 +01:00
committed by GitHub
parent c97bd48a77
commit 35cb10c5e7
12 changed files with 49 additions and 69 deletions

View File

@@ -1,7 +1,6 @@
import type {
ICredentialsDecrypted,
ICredentialTestFunctions,
IDataObject,
IExecuteFunctions,
INodeCredentialTestResult,
INodeExecutionData,
@@ -10,11 +9,12 @@ import type {
INodeTypeDescription,
} from 'n8n-workflow';
import { NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import pgPromise from 'pg-promise';
import { oldVersionNotice } from '@utils/descriptions';
import { pgInsertV2, pgQueryV2, pgUpdate, wrapData } from './genericFunctions';
import { configurePostgres } from '../transport';
import type { PgpConnection, PostgresNodeCredentials } from '../v2/helpers/interfaces';
const versionDescription: INodeTypeDescription = {
displayName: 'Postgres',
@@ -298,33 +298,27 @@ export class PostgresV1 implements INodeType {
this: ICredentialTestFunctions,
credential: ICredentialsDecrypted,
): Promise<INodeCredentialTestResult> {
const credentials = credential.data as IDataObject;
const credentials = credential.data as PostgresNodeCredentials;
let connection: PgpConnection | undefined;
try {
const pgp = pgPromise();
const config: IDataObject = {
host: credentials.host as string,
port: credentials.port as number,
database: credentials.database as string,
user: credentials.user as string,
password: credentials.password as string,
};
const { db } = await configurePostgres.call(this, credentials, {});
if (credentials.allowUnauthorizedCerts === true) {
config.ssl = {
rejectUnauthorized: false,
};
} else {
config.ssl = !['disable', undefined].includes(credentials.ssl as string | undefined);
config.sslmode = (credentials.ssl as string) || 'disable';
}
const db = pgp(config);
await db.connect();
// Acquires a new connection that can be used to to run multiple
// queries on the same connection and must be released again
// manually.
connection = await db.connect();
} catch (error) {
return {
status: 'Error',
message: error.message,
};
} finally {
if (connection) {
// release connection
await connection.done();
}
}
return {
status: 'OK',
@@ -335,42 +329,19 @@ export class PostgresV1 implements INodeType {
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const credentials = await this.getCredentials('postgres');
const credentials = await this.getCredentials<PostgresNodeCredentials>('postgres');
const largeNumbersOutput = this.getNodeParameter(
'additionalFields.largeNumbersOutput',
0,
'',
) as string;
const pgp = pgPromise();
if (largeNumbersOutput === 'numbers') {
pgp.pg.types.setTypeParser(20, (value: string) => {
return parseInt(value, 10);
});
pgp.pg.types.setTypeParser(1700, (value: string) => {
return parseFloat(value);
});
}
const config: IDataObject = {
host: credentials.host as string,
port: credentials.port as number,
database: credentials.database as string,
user: credentials.user as string,
password: credentials.password as string,
};
if (credentials.allowUnauthorizedCerts === true) {
config.ssl = {
rejectUnauthorized: false,
};
} else {
config.ssl = !['disable', undefined].includes(credentials.ssl as string | undefined);
config.sslmode = (credentials.ssl as string) || 'disable';
}
const db = pgp(config);
const { db, pgp } = await configurePostgres.call(this, credentials, {
largeNumbersOutput:
largeNumbersOutput === 'numbers' || largeNumbersOutput === 'text'
? largeNumbersOutput
: undefined,
});
let returnItems: INodeExecutionData[] = [];