From 0d9b09167a73ae9b1cd1b23bd4719e4d54701f88 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 908360df6d..bda69dcff7 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 99a94da8a5..9d98b426de 100644 --- a/src/driver/postgres/PostgresDriver.ts +++ b/src/driver/postgres/PostgresDriver.ts @@ -291,7 +291,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; } } @@ -415,6 +419,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")); @@ -816,7 +825,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]);