@@ -4,6 +4,10 @@ import postgres from 'postgres';
44// Items cache which are added by an insert statement
55let insertedItems = [ ] ;
66
7+ // Use of the PostgreSQL connection pool to limit the number of sessions, see
8+ // https://github.com/porsager/postgres?tab=readme-ov-file#connection-details
9+ let postgresConnectionPool = null ;
10+
711/**
812 * Does run the given query against the database from the configuration. It caches all inserted items.
913 *
@@ -29,17 +33,20 @@ function queryTestDB(joomlaQuery, config) {
2933 insertedItems . push ( insertItem ) ;
3034 }
3135
32- // Check if the DB is from postgres
36+ // Do we use PostgreSQL?
3337 if ( config . env . db_type === 'pgsql' || config . env . db_type === 'PostgreSQL (PDO)' ) {
34- const connection = postgres ( {
35- host : config . env . db_host ,
36- port : config . env . db_port ,
37- database : config . env . db_name ,
38- username : config . env . db_user ,
39- password : config . env . db_password ,
40- idle_timeout : 1 ,
41- max_lifetime : 1 ,
42- } ) ;
38+
39+ if ( postgresConnectionPool === null ) {
40+ // Initialisation on the first call
41+ postgresConnectionPool = postgres ( {
42+ host : config . env . db_host ,
43+ port : config . env . db_port ,
44+ database : config . env . db_name ,
45+ username : config . env . db_user ,
46+ password : config . env . db_password ,
47+ max : 10 , // Use only this (unchanged default) maximum number of connections in the pool
48+ } ) ;
49+ }
4350
4451 // Postgres delivers the data direct as result of the insert query
4552 if ( insertItem ) {
@@ -49,7 +56,7 @@ function queryTestDB(joomlaQuery, config) {
4956 // Postgres needs double quotes
5057 query = query . replaceAll ( '`' , '"' ) ;
5158
52- return connection . unsafe ( query ) . then ( ( result ) => {
59+ return postgresConnectionPool . unsafe ( query ) . then ( ( result ) => {
5360 // Select query should always return an array
5461 if ( query . indexOf ( 'SELECT' ) === 0 && ! Array . isArray ( result ) ) {
5562 return [ result ] ;
@@ -64,12 +71,12 @@ function queryTestDB(joomlaQuery, config) {
6471 insertItem . rows . push ( result [ 0 ] . id ) ;
6572 }
6673
67- // Normalize the object
74+ // Normalize the object and return from PostgreSQL
6875 return { insertId : result [ 0 ] . id } ;
6976 } ) ;
7077 }
7178
72- // Return a promise which runs the query
79+ // Return a promise which runs the query for MariaDB / MySQL
7380 return new Promise ( ( resolve , reject ) => {
7481 // Create the connection and connect
7582 const connection = mysql . createConnection ( {
@@ -94,7 +101,7 @@ function queryTestDB(joomlaQuery, config) {
94101 insertItem . rows . push ( results . insertId ) ;
95102 }
96103
97- // Resolve the result
104+ // Resolve the result from MariaDB / MySQL
98105 return resolve ( results ) ;
99106 } ) ;
100107 } ) ;
0 commit comments