Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add possibility to pass application_name via postgres specific config #7989

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
})));
});