Skip to content

Commit

Permalink
Merge pull request #254 from borsemayur2/postgres-connect-with-url-st…
Browse files Browse the repository at this point in the history
…ring

Postgres connect with url string
  • Loading branch information
eveningkid committed May 1, 2021
2 parents eb3830b + 84ad078 commit 18a6073
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions lib/connectors/postgres-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ 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 PostgresOptionsWithConfig extends ConnectorOptions {
database: string;
host: string;
username: string;
password: string;
port?: number;
}

interface PostgresOptionsWithURI extends ConnectorOptions {
uri: string;
}

export type PostgresOptions =
| PostgresOptionsWithConfig
| PostgresOptionsWithURI;

export class PostgresConnector implements Connector {
_dialect: SupportedSQLDatabaseDialect = "postgres";

Expand All @@ -24,13 +32,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 ("uri" in options) {
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);
}

Expand Down

0 comments on commit 18a6073

Please sign in to comment.