Skip to content

Commit

Permalink
feat: add postgres connection option applicationName (#7989)
Browse files Browse the repository at this point in the history
  • Loading branch information
das-mensch authored Jul 30, 2021
1 parent b797781 commit d365acc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/connection-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 6 additions & 0 deletions src/driver/postgres/PostgresConnectionOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions test/functional/driver/postgres/specific-options.ts
Original file line number Diff line number Diff line change
@@ -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");
})));
});

0 comments on commit d365acc

Please sign in to comment.