Skip to content

Commit 18a6073

Browse files
authored
Merge pull request #254 from borsemayur2/postgres-connect-with-url-string
Postgres connect with url string
2 parents eb3830b + 84ad078 commit 18a6073

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

lib/connectors/postgres-connector.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@ import type { SupportedSQLDatabaseDialect } from "../translators/sql-translator.
55
import type { QueryDescription } from "../query-builder.ts";
66
import type { Values } from "../data-types.ts";
77

8-
export interface PostgresOptions extends ConnectorOptions {
8+
interface PostgresOptionsWithConfig extends ConnectorOptions {
99
database: string;
1010
host: string;
1111
username: string;
1212
password: string;
1313
port?: number;
1414
}
1515

16+
interface PostgresOptionsWithURI extends ConnectorOptions {
17+
uri: string;
18+
}
19+
20+
export type PostgresOptions =
21+
| PostgresOptionsWithConfig
22+
| PostgresOptionsWithURI;
23+
1624
export class PostgresConnector implements Connector {
1725
_dialect: SupportedSQLDatabaseDialect = "postgres";
1826

@@ -24,13 +32,17 @@ export class PostgresConnector implements Connector {
2432
/** Create a PostgreSQL connection. */
2533
constructor(options: PostgresOptions) {
2634
this._options = options;
27-
this._client = new PostgresClient({
28-
hostname: options.host,
29-
user: options.username,
30-
password: options.password,
31-
database: options.database,
32-
port: options.port ?? 5432,
33-
});
35+
if ("uri" in options) {
36+
this._client = new PostgresClient(options.uri);
37+
} else {
38+
this._client = new PostgresClient({
39+
hostname: options.host,
40+
user: options.username,
41+
password: options.password,
42+
database: options.database,
43+
port: options.port ?? 5432,
44+
});
45+
}
3446
this._translator = new SQLTranslator(this._dialect);
3547
}
3648

0 commit comments

Comments
 (0)