From ca5b0a2adf0f614b743b0200b616e4647da1c4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Sant=C3=A1ngelo?= Date: Tue, 7 Jun 2022 00:49:15 -0300 Subject: [PATCH] feat: support a connection string --- README.md | 3 +++ src/index.ts | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index adda1ae..ef5f019 100644 --- a/README.md +++ b/README.md @@ -57,12 +57,15 @@ It supports the following ENV variables: First everything related with connecting to [Postgres](https://www.postgresql.org/): +- `PG_COMPONENT_PSQL_CONNECTION_STRING` - `PG_COMPONENT_PSQL_PORT` - `PG_COMPONENT_PSQL_HOST` - `PG_COMPONENT_PSQL_DATABASE` - `PG_COMPONENT_PSQL_USER` - `PG_COMPONENT_PSQL_PASSWORD` +You'll probably use either the CONNECTION_STRING **or** the other params and not both. + Then the variables related to [Postgres](https://www.postgresql.org/)'s query timeouts: - `PG_COMPONENT_IDLE_TIMEOUT` diff --git a/src/index.ts b/src/index.ts index cb121ba..d4df835 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,7 +22,8 @@ export async function createPgComponent( const logger = logs.getLogger("pg-component") // Environment - const [port, host, database, user, password, idleTimeoutMillis, query_timeout] = await Promise.all([ + const [connectionString, port, host, database, user, password, idleTimeoutMillis, query_timeout] = await Promise.all([ + config.getString("PG_COMPONENT_PSQL_CONNECTION_STRING"), config.getNumber("PG_COMPONENT_PSQL_PORT"), config.getString("PG_COMPONENT_PSQL_HOST"), config.getString("PG_COMPONENT_PSQL_DATABASE"), @@ -31,7 +32,16 @@ export async function createPgComponent( config.getNumber("PG_COMPONENT_IDLE_TIMEOUT"), config.getNumber("PG_COMPONENT_QUERY_TIMEOUT"), ]) - const defaultOptions = { port, host, database, user, password, idleTimeoutMillis, query_timeout } + const defaultOptions: PoolConfig = { + connectionString, + port, + host, + database, + user, + password, + idleTimeoutMillis, + query_timeout, + } const STREAM_QUERY_TIMEOUT = await config.getNumber("PG_COMPONENT_STREAM_QUERY_TIMEOUT") const GRACE_PERIODS = (await config.getNumber("PG_COMPONENT_GRACE_PERIODS")) || 10