From 6e2edf8d14f4a30bec2263181c9d1235637c5956 Mon Sep 17 00:00:00 2001 From: Jan Hoffmann Date: Fri, 30 Jul 2021 14:21:29 +0200 Subject: [PATCH 1/2] feat: add possibility to pass application_name via postgres specific config as the "extra" parameter should be deprecated in the future, the need for passing driver specific options via the respective config objects is given. This change also enables the user to use code completion in case they didn't even knew this options exists --- .../postgres/PostgresConnectionOptions.ts | 6 +++++ src/driver/postgres/PostgresDriver.ts | 3 ++- .../driver/postgres/specific-options.ts | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 test/functional/driver/postgres/specific-options.ts diff --git a/src/driver/postgres/PostgresConnectionOptions.ts b/src/driver/postgres/PostgresConnectionOptions.ts index 1353c3713f..c0f62046ab 100644 --- a/src/driver/postgres/PostgresConnectionOptions.ts +++ b/src/driver/postgres/PostgresConnectionOptions.ts @@ -67,4 +67,10 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr * Automatically install postgres extensions */ readonly installExtensions?: boolean; + + /** + * sets the application_name var to help db administrators identify + * the service using this connection. Defaults to 'undefined' + */ + readonly applicationName?: string; } diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index cdc6cc8cbd..7aac3019d2 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -1101,7 +1101,8 @@ export class PostgresDriver implements Driver { database: credentials.database, port: credentials.port, ssl: credentials.ssl, - connectionTimeoutMillis: options.connectTimeoutMS + connectionTimeoutMillis: options.connectTimeoutMS, + application_name: options.applicationName }, options.extra || {}); // create a connection pool diff --git a/test/functional/driver/postgres/specific-options.ts b/test/functional/driver/postgres/specific-options.ts new file mode 100644 index 0000000000..4c1342d11c --- /dev/null +++ b/test/functional/driver/postgres/specific-options.ts @@ -0,0 +1,24 @@ +import "reflect-metadata"; +import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../../utils/test-utils"; +import { Connection } from "../../../../src/connection/Connection"; +import { expect } from "chai"; + +describe("postgres specific options", () => { + let connections: Connection[]; + before(async () => connections = await createTestingConnections({ + enabledDrivers: ["postgres"], + driverSpecific: { + applicationName: "some test name" + } + })); + beforeEach(() => reloadTestingDatabases(connections)); + after(() => closeTestingConnections(connections)); + + it("should set application_name", () => Promise.all(connections.map(async connection => { + const result = await connection.query( + "select current_setting('application_name') as application_name" + ); + expect(result.length).equals(1); + expect(result[0].application_name).equals("some test name"); + }))); +}); From 12766c632d218dfb88c5f06b8e0e84e343d50842 Mon Sep 17 00:00:00 2001 From: Jan Hoffmann Date: Fri, 30 Jul 2021 15:31:26 +0200 Subject: [PATCH 2/2] docs: update docs for new postgres specific option "applicationName" add useful information about the parameter and show its default value --- docs/connection-options.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/connection-options.md b/docs/connection-options.md index e0ec1f1e0d..a965de94d1 100644 --- a/docs/connection-options.md +++ b/docs/connection-options.md @@ -191,6 +191,8 @@ See [SSL options](https://github.com/mysqljs/mysql#ssl-options). * `installExtensions` - A boolean to control whether to install necessary postgres extensions automatically or not (default: `true`) +* `applicationName` - A string visible in statistics and logs to help referencing an application to a connection (default: `undefined`) + ## `sqlite` connection options * `database` - Database path. For example "./mydb.sql"