1
1
import type {
2
2
ICredentialsDecrypted ,
3
3
ICredentialTestFunctions ,
4
- IDataObject ,
5
4
IExecuteFunctions ,
6
5
INodeCredentialTestResult ,
7
6
INodeExecutionData ,
@@ -10,11 +9,12 @@ import type {
10
9
INodeTypeDescription ,
11
10
} from 'n8n-workflow' ;
12
11
import { NodeConnectionType , NodeOperationError } from 'n8n-workflow' ;
13
- import pgPromise from 'pg-promise' ;
14
12
15
13
import { oldVersionNotice } from '@utils/descriptions' ;
16
14
17
15
import { pgInsertV2 , pgQueryV2 , pgUpdate , wrapData } from './genericFunctions' ;
16
+ import { configurePostgres } from '../transport' ;
17
+ import type { PgpConnection , PostgresNodeCredentials } from '../v2/helpers/interfaces' ;
18
18
19
19
const versionDescription : INodeTypeDescription = {
20
20
displayName : 'Postgres' ,
@@ -298,33 +298,27 @@ export class PostgresV1 implements INodeType {
298
298
this : ICredentialTestFunctions ,
299
299
credential : ICredentialsDecrypted ,
300
300
) : Promise < INodeCredentialTestResult > {
301
- const credentials = credential . data as IDataObject ;
302
- try {
303
- const pgp = pgPromise ( ) ;
304
- const config : IDataObject = {
305
- host : credentials . host as string ,
306
- port : credentials . port as number ,
307
- database : credentials . database as string ,
308
- user : credentials . user as string ,
309
- password : credentials . password as string ,
310
- } ;
301
+ const credentials = credential . data as PostgresNodeCredentials ;
311
302
312
- if ( credentials . allowUnauthorizedCerts === true ) {
313
- config . ssl = {
314
- rejectUnauthorized : false ,
315
- } ;
316
- } else {
317
- config . ssl = ! [ 'disable' , undefined ] . includes ( credentials . ssl as string | undefined ) ;
318
- config . sslmode = ( credentials . ssl as string ) || 'disable' ;
319
- }
303
+ let connection : PgpConnection | undefined ;
320
304
321
- const db = pgp ( config ) ;
322
- await db . connect ( ) ;
305
+ try {
306
+ const { db } = await configurePostgres . call ( this , credentials , { } ) ;
307
+
308
+ // Acquires a new connection that can be used to to run multiple
309
+ // queries on the same connection and must be released again
310
+ // manually.
311
+ connection = await db . connect ( ) ;
323
312
} catch ( error ) {
324
313
return {
325
314
status : 'Error' ,
326
315
message : error . message ,
327
316
} ;
317
+ } finally {
318
+ if ( connection ) {
319
+ // release connection
320
+ await connection . done ( ) ;
321
+ }
328
322
}
329
323
return {
330
324
status : 'OK' ,
@@ -335,42 +329,19 @@ export class PostgresV1 implements INodeType {
335
329
} ;
336
330
337
331
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
338
- const credentials = await this . getCredentials ( 'postgres' ) ;
332
+ const credentials = await this . getCredentials < PostgresNodeCredentials > ( 'postgres' ) ;
339
333
const largeNumbersOutput = this . getNodeParameter (
340
334
'additionalFields.largeNumbersOutput' ,
341
335
0 ,
342
336
'' ,
343
337
) as string ;
344
338
345
- const pgp = pgPromise ( ) ;
346
-
347
- if ( largeNumbersOutput === 'numbers' ) {
348
- pgp . pg . types . setTypeParser ( 20 , ( value : string ) => {
349
- return parseInt ( value , 10 ) ;
350
- } ) ;
351
- pgp . pg . types . setTypeParser ( 1700 , ( value : string ) => {
352
- return parseFloat ( value ) ;
353
- } ) ;
354
- }
355
-
356
- const config : IDataObject = {
357
- host : credentials . host as string ,
358
- port : credentials . port as number ,
359
- database : credentials . database as string ,
360
- user : credentials . user as string ,
361
- password : credentials . password as string ,
362
- } ;
363
-
364
- if ( credentials . allowUnauthorizedCerts === true ) {
365
- config . ssl = {
366
- rejectUnauthorized : false ,
367
- } ;
368
- } else {
369
- config . ssl = ! [ 'disable' , undefined ] . includes ( credentials . ssl as string | undefined ) ;
370
- config . sslmode = ( credentials . ssl as string ) || 'disable' ;
371
- }
372
-
373
- const db = pgp ( config ) ;
339
+ const { db, pgp } = await configurePostgres . call ( this , credentials , {
340
+ largeNumbersOutput :
341
+ largeNumbersOutput === 'numbers' || largeNumbersOutput === 'text'
342
+ ? largeNumbersOutput
343
+ : undefined ,
344
+ } ) ;
374
345
375
346
let returnItems : INodeExecutionData [ ] = [ ] ;
376
347
0 commit comments