From 7ddbff2669c10c5440b5853e65ee9682ba231492 Mon Sep 17 00:00:00 2001 From: Salvatore Novelli Date: Thu, 7 Apr 2022 19:55:56 +0100 Subject: [PATCH] feat: add "application_name" as an optional DB option #1170 (#1478) * implemented changes to resolve issue 1170 * added applicationName to the list of db options in the documentation' Co-authored-by: Daniele Casal Co-authored-by: Sukhvinder Panesar <79143027+esspee-lbg@users.noreply.github.com> --- src/lib/__snapshots__/create-config.test.ts.snap | 1 + src/lib/create-config.ts | 1 + src/lib/db/db-pool.ts | 5 ++++- src/lib/types/option.ts | 1 + src/server-dev.ts | 1 + website/docs/deploy/configuring-unleash.md | 1 + 6 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/__snapshots__/create-config.test.ts.snap b/src/lib/__snapshots__/create-config.test.ts.snap index 609b84065b..790e727edd 100644 --- a/src/lib/__snapshots__/create-config.test.ts.snap +++ b/src/lib/__snapshots__/create-config.test.ts.snap @@ -11,6 +11,7 @@ Object { }, "db": Object { "acquireConnectionTimeout": 30000, + "applicationName": "unleash", "database": "unleash_db", "disableMigration": false, "driver": "postgres", diff --git a/src/lib/create-config.ts b/src/lib/create-config.ts index 9ab90b57c6..dc0f6090be 100644 --- a/src/lib/create-config.ts +++ b/src/lib/create-config.ts @@ -95,6 +95,7 @@ const defaultDbOptions: IDBOption = { }, schema: process.env.DATABASE_SCHEMA || 'public', disableMigration: false, + applicationName: process.env.DATABASE_APPLICATION_NAME || 'unleash', }; const defaultSessionOption: ISessionOption = { diff --git a/src/lib/db/db-pool.ts b/src/lib/db/db-pool.ts index 0bcb943f9d..e59b18b6fe 100644 --- a/src/lib/db/db-pool.ts +++ b/src/lib/db/db-pool.ts @@ -9,7 +9,10 @@ export function createDb({ return knex({ client: 'pg', version: db.version, - connection: db, + connection: { + ...db, + application_name: db.applicationName, + }, pool: db.pool, searchPath: db.schema, asyncStackTraces: true, diff --git a/src/lib/types/option.ts b/src/lib/types/option.ts index f0d6509fc4..185c662f61 100644 --- a/src/lib/types/option.ts +++ b/src/lib/types/option.ts @@ -30,6 +30,7 @@ export interface IDBOption { }; schema: string; disableMigration: boolean; + applicationName?: string; } export interface ISessionOption { diff --git a/src/server-dev.ts b/src/server-dev.ts index 45bda849ca..d790a9e763 100644 --- a/src/server-dev.ts +++ b/src/server-dev.ts @@ -16,6 +16,7 @@ process.nextTick(async () => { database: process.env.UNLEASH_DATABASE_NAME || 'unleash', schema: process.env.UNLEASH_DATABASE_SCHEMA, ssl: false, + applicationName: 'unleash', }, server: { enableRequestLogger: true, diff --git a/website/docs/deploy/configuring-unleash.md b/website/docs/deploy/configuring-unleash.md index b6701a3d17..b2cc00c464 100644 --- a/website/docs/deploy/configuring-unleash.md +++ b/website/docs/deploy/configuring-unleash.md @@ -205,6 +205,7 @@ The available options are listed in the table below. Options can be specified ei | `pool.min` | `DATABASE_POOL_MIN` | 0 | The minimum number of connections in the connection pool. | | `pool.max` | `DATABASE_POOL_MAX` | 4 | The maximum number of connections in the connection pool. | | `pool.idleTimeoutMillis` | `DATABASE_POOL_IDLE_TIMEOUT_MS` | 30000 | The amount of time (in milliseconds) that a connection must be idle for before it is marked as a candidate for eviction. | +| `applicationName` | `DATABASE_APPLICATION_NAME` | `unleash` | The name of the application that created this Client instance. | Alternatively, you can use a [libpq connection string](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING) to connect to the database. You can provide it directly or from a file by using one of the below options. In JavaScript, these are top-level properties of the root configuration object, *not* the `db` object.