From 993fa7335289d9c3d3c599f85166b69425635766 Mon Sep 17 00:00:00 2001 From: Mayur Borse Date: Fri, 30 Apr 2021 07:57:38 +0530 Subject: [PATCH 1/3] accept url string as uri (optional) --- lib/connectors/postgres-connector.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index ba4f072..6c07b9b 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -11,6 +11,7 @@ export interface PostgresOptions extends ConnectorOptions { username: string; password: string; port?: number; + uri?: string; } export class PostgresConnector implements Connector { @@ -24,13 +25,17 @@ export class PostgresConnector implements Connector { /** Create a PostgreSQL connection. */ constructor(options: PostgresOptions) { this._options = options; - this._client = new PostgresClient({ - hostname: options.host, - user: options.username, - password: options.password, - database: options.database, - port: options.port ?? 5432, - }); + if (options.hasOwnProperty("uri")) { + this._client = new PostgresClient(options.uri); + } else { + this._client = new PostgresClient({ + hostname: options.host, + user: options.username, + password: options.password, + database: options.database, + port: options.port ?? 5432, + }); + } this._translator = new SQLTranslator(this._dialect); } From 0f076edadd18e9b154e2a331d3ff40ca67c0bf38 Mon Sep 17 00:00:00 2001 From: Mayur Borse Date: Fri, 30 Apr 2021 08:31:23 +0530 Subject: [PATCH 2/3] never used for unused types --- lib/connectors/postgres-connector.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index 6c07b9b..481f337 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -5,15 +5,28 @@ import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator. import type { QueryDescription } from "../query-builder.ts"; import type { Values } from "../data-types.ts"; -export interface PostgresOptions extends ConnectorOptions { +interface PostgresOptionsBase extends ConnectorOptions { database: string; host: string; username: string; password: string; port?: number; - uri?: string; + // unused types + uri?: never; } +interface PostgresOptionsWithURI extends ConnectorOptions { + uri: string; + // unused types + database?: never; + host?: never; + username?: never; + password?: never; + port?: never; +} + +export type PostgresOptions = PostgresOptionsWithURI | PostgresOptionsBase; + export class PostgresConnector implements Connector { _dialect: SupportedSQLDatabaseDialect = "postgres"; From 84ad0786dbdadb0eea36bd7377302e0db354ade3 Mon Sep 17 00:00:00 2001 From: Mayur Borse Date: Sat, 1 May 2021 17:46:23 +0530 Subject: [PATCH 3/3] using 'in' as type guard --- lib/connectors/postgres-connector.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/lib/connectors/postgres-connector.ts b/lib/connectors/postgres-connector.ts index 481f337..dc992c1 100644 --- a/lib/connectors/postgres-connector.ts +++ b/lib/connectors/postgres-connector.ts @@ -5,27 +5,21 @@ import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator. import type { QueryDescription } from "../query-builder.ts"; import type { Values } from "../data-types.ts"; -interface PostgresOptionsBase extends ConnectorOptions { +interface PostgresOptionsWithConfig extends ConnectorOptions { database: string; host: string; username: string; password: string; port?: number; - // unused types - uri?: never; } interface PostgresOptionsWithURI extends ConnectorOptions { uri: string; - // unused types - database?: never; - host?: never; - username?: never; - password?: never; - port?: never; } -export type PostgresOptions = PostgresOptionsWithURI | PostgresOptionsBase; +export type PostgresOptions = + | PostgresOptionsWithConfig + | PostgresOptionsWithURI; export class PostgresConnector implements Connector { _dialect: SupportedSQLDatabaseDialect = "postgres"; @@ -38,7 +32,7 @@ export class PostgresConnector implements Connector { /** Create a PostgreSQL connection. */ constructor(options: PostgresOptions) { this._options = options; - if (options.hasOwnProperty("uri")) { + if ("uri" in options) { this._client = new PostgresClient(options.uri); } else { this._client = new PostgresClient({