Skip to content

Commit

Permalink
add support to disable connection pooling for postgres driver
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkdave committed Nov 19, 2019
1 parent 922de20 commit 6456533
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/driver/postgres/PostgresConnectionOptions.ts
Expand Up @@ -16,6 +16,11 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr
*/
readonly schema?: string;

/**
* Disable built-in connection pooling
*/
readonly disablePooling?: boolean;

/**
* Replication setup.
*/
Expand Down
28 changes: 26 additions & 2 deletions src/driver/postgres/PostgresDriver.ts
Expand Up @@ -284,7 +284,11 @@ export class PostgresDriver implements Driver {
this.database = this.options.replication.master.database;

} else {
this.master = await this.createPool(this.options, this.options);
if (this.options.disablePooling) {
this.master = undefined;
} else {
this.master = await this.createPool(this.options, this.options);
}
this.database = this.options.database;
}
}
Expand Down Expand Up @@ -368,6 +372,11 @@ export class PostgresDriver implements Driver {
* Closes connection with database.
*/
async disconnect(): Promise<void> {
if (this.options.disablePooling) {
await Promise.all(this.connectedQueryRunners.map(queryRunner => queryRunner.release()));
return;
}

if (!this.master)
return Promise.reject(new ConnectionIsNotSetError("postgres"));

Expand Down Expand Up @@ -767,7 +776,22 @@ export class PostgresDriver implements Driver {
* Used for replication.
* If replication is not setup then returns default connection's database connection.
*/
obtainMasterConnection(): Promise<any> {
async obtainMasterConnection(): Promise<any> {
if (this.options.disablePooling) {
const credentials = DriverUtils.buildDriverOptions(this.options);
const options = {
host: credentials.host,
user: credentials.username,
password: credentials.password,
database: credentials.database,
port: credentials.port,
ssl: credentials.ssl,
};
const connection = new this.postgres.Client(options);
await connection.connect();
return [connection, () => connection.end()];
}

return new Promise((ok, fail) => {
this.master.connect((err: any, connection: any, release: any) => {
err ? fail(err) : ok([connection, release]);
Expand Down

0 comments on commit 6456533

Please sign in to comment.