@@ -115,35 +115,42 @@ export async function connectPostgres({
115115 return sql ;
116116}
117117
118+ /**
119+ * Retrieve a postgres ENV value depending on the target database server (read-replica/default or
120+ * primary). We will fall back to read-replica values if a primary value was not given. See the
121+ * `.env` file for more information on these options.
122+ */
123+ export function getPgConnectionEnvValue (
124+ name : string ,
125+ pgServer : PgServer = PgServer . default
126+ ) : string | undefined {
127+ return pgServer === PgServer . primary
128+ ? process . env [ `PG_PRIMARY_${ name } ` ] ?? process . env [ `PG_${ name } ` ]
129+ : process . env [ `PG_${ name } ` ] ;
130+ }
131+
118132export function getPostgres ( {
119133 usageName,
120134 pgServer,
121135} : {
122136 usageName : string ;
123137 pgServer ?: PgServer ;
124138} ) : PgSqlClient {
125- // Retrieve a postgres ENV value depending on the target database server (read-replica/default or primary).
126- // We will fall back to read-replica values if a primary value was not given.
127- // See the `.env` file for more information on these options.
128- const pgEnvValue = ( name : string ) : string | undefined =>
129- pgServer === PgServer . primary
130- ? process . env [ `PG_PRIMARY_${ name } ` ] ?? process . env [ `PG_${ name } ` ]
131- : process . env [ `PG_${ name } ` ] ;
132139 const pgEnvVars = {
133- database : pgEnvValue ( 'DATABASE' ) ,
134- user : pgEnvValue ( 'USER' ) ,
135- password : pgEnvValue ( 'PASSWORD' ) ,
136- host : pgEnvValue ( 'HOST' ) ,
137- port : pgEnvValue ( 'PORT' ) ,
138- ssl : pgEnvValue ( 'SSL' ) ,
139- schema : pgEnvValue ( 'SCHEMA' ) ,
140- applicationName : pgEnvValue ( 'APPLICATION_NAME' ) ,
141- idleTimeout : parseInt ( pgEnvValue ( 'IDLE_TIMEOUT' ) ?? '30' ) ,
142- maxLifetime : parseInt ( pgEnvValue ( 'MAX_LIFETIME' ) ?? '60' ) ,
140+ database : getPgConnectionEnvValue ( 'DATABASE' , pgServer ) ,
141+ user : getPgConnectionEnvValue ( 'USER' , pgServer ) ,
142+ password : getPgConnectionEnvValue ( 'PASSWORD' , pgServer ) ,
143+ host : getPgConnectionEnvValue ( 'HOST' , pgServer ) ,
144+ port : getPgConnectionEnvValue ( 'PORT' , pgServer ) ,
145+ ssl : getPgConnectionEnvValue ( 'SSL' , pgServer ) ,
146+ schema : getPgConnectionEnvValue ( 'SCHEMA' , pgServer ) ,
147+ applicationName : getPgConnectionEnvValue ( 'APPLICATION_NAME' , pgServer ) ,
148+ idleTimeout : parseInt ( getPgConnectionEnvValue ( 'IDLE_TIMEOUT' , pgServer ) ?? '30' ) ,
149+ maxLifetime : parseInt ( getPgConnectionEnvValue ( 'MAX_LIFETIME' , pgServer ) ?? '60' ) ,
143150 poolMax : parseInt ( process . env [ 'PG_CONNECTION_POOL_MAX' ] ?? '10' ) ,
144151 } ;
145152 const defaultAppName = 'stacks-blockchain-api' ;
146- const pgConnectionUri = pgEnvValue ( 'CONNECTION_URI' ) ;
153+ const pgConnectionUri = getPgConnectionEnvValue ( 'CONNECTION_URI' , pgServer ) ;
147154 const pgConfigEnvVar = Object . entries ( pgEnvVars ) . find ( ( [ , v ] ) => typeof v === 'string' ) ?. [ 0 ] ;
148155 if ( pgConfigEnvVar && pgConnectionUri ) {
149156 throw new Error (
0 commit comments