@@ -115,35 +115,42 @@ export async function connectPostgres({
115
115
return sql ;
116
116
}
117
117
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
+
118
132
export function getPostgres ( {
119
133
usageName,
120
134
pgServer,
121
135
} : {
122
136
usageName : string ;
123
137
pgServer ?: PgServer ;
124
138
} ) : 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 } ` ] ;
132
139
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' ) ,
143
150
poolMax : parseInt ( process . env [ 'PG_CONNECTION_POOL_MAX' ] ?? '10' ) ,
144
151
} ;
145
152
const defaultAppName = 'stacks-blockchain-api' ;
146
- const pgConnectionUri = pgEnvValue ( 'CONNECTION_URI' ) ;
153
+ const pgConnectionUri = getPgConnectionEnvValue ( 'CONNECTION_URI' , pgServer ) ;
147
154
const pgConfigEnvVar = Object . entries ( pgEnvVars ) . find ( ( [ , v ] ) => typeof v === 'string' ) ?. [ 0 ] ;
148
155
if ( pgConfigEnvVar && pgConnectionUri ) {
149
156
throw new Error (
0 commit comments