From 64565332aac80aa8059377692cc9a2c49949cf40 Mon Sep 17 00:00:00 2001 From: clarkdave Date: Fri, 23 Nov 2018 14:44:18 +0000 Subject: [PATCH] add support to disable connection pooling for postgres driver --- .../postgres/PostgresConnectionOptions.ts | 5 ++++ src/driver/postgres/PostgresDriver.ts | 28 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/driver/postgres/PostgresConnectionOptions.ts b/src/driver/postgres/PostgresConnectionOptions.ts index aa70411217..83cacdb99d 100644 --- a/src/driver/postgres/PostgresConnectionOptions.ts +++ b/src/driver/postgres/PostgresConnectionOptions.ts @@ -16,6 +16,11 @@ export interface PostgresConnectionOptions extends BaseConnectionOptions, Postgr */ readonly schema?: string; + /** + * Disable built-in connection pooling + */ + readonly disablePooling?: boolean; + /** * Replication setup. */ diff --git a/src/driver/postgres/PostgresDriver.ts b/src/driver/postgres/PostgresDriver.ts index 7072b96c3d..04660ede0d 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -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; } } @@ -368,6 +372,11 @@ export class PostgresDriver implements Driver { * Closes connection with database. */ async disconnect(): Promise { + if (this.options.disablePooling) { + await Promise.all(this.connectedQueryRunners.map(queryRunner => queryRunner.release())); + return; + } + if (!this.master) return Promise.reject(new ConnectionIsNotSetError("postgres")); @@ -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 { + async obtainMasterConnection(): Promise { + 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]);