Skip to content

Commit

Permalink
fix(#90): Could not connect to the postgres database when TYPEORM_POR…
Browse files Browse the repository at this point in the history
…T is set (#92)
  • Loading branch information
uki00a authored Jul 25, 2020
1 parent a8cf0d4 commit 59394ea
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,7 @@ export class PostgresDriver implements Driver {
user: credentials.username,
password: credentials.password,
database: credentials.database,
port: credentials.port,
port: credentials.port != null ? Number(credentials.port) : null,
ssl: credentials.ssl
}, options.extra || {});

Expand Down
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,5 +516,6 @@ import "./test/other-issues/update-relational-column-on-relation-change/update-r

// Tests for denolib issues
import "./test/denolib-issues/70/issue-70.ts";
import "./test/denolib-issues/90/issue-90.ts";

runTests();
61 changes: 61 additions & 0 deletions test/denolib-issues/90/issue-90.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {runIfMain} from "../../deps/mocha.ts";
import {setupTestingConnections} from "../../utils/test-utils.ts";
import {PostgresConnectionOptions} from "../../../src/driver/postgres/PostgresConnectionOptions.ts";
import { createConnection } from "../../../src/index.ts";
import { ConnectionOptionsReader } from "../../../src/connection/ConnectionOptionsReader.ts";

describe("denolib issues > #90 Could not connect to the postgres database when TYPEORM_PORT is set", () => {
it("can connect to postgres database when TYPEORM_PORT is set", async () => {
const connectionOptionsArray = setupTestingConnections({
enabledDrivers: ["postgres"],
});

if (connectionOptionsArray.length === 0) {
return;
}

// Override environment variables.
const connectionOptions = connectionOptionsArray[0] as PostgresConnectionOptions;
const envVars = {
"TYPEORM_CONNECTION": "postgres",
"TYPEORM_PORT": String(connectionOptions.port || 5432),
"TYPEORM_HOST": connectionOptions.host || "localhost",
"TYPEORM_DATABASE": connectionOptions.database,
"TYPEORM_USERNAME": connectionOptions.username,
"TYPEORM_PASSWORD": connectionOptions.password,
} as { [env: string]: string | undefined; };
const origEnvVars = Object.keys(envVars).reduce((origEnv, env) => {
origEnv[env] = Deno.env.get(env);
return origEnv;
}, {} as { [env: string]: string | undefined });

for (const env of Object.keys(envVars)) {
if (envVars[env] != null) {
Deno.env.set(env, envVars[env]!);
}
}

try {
// Read config from `TYPEORM_*` variables.
const options = await new ConnectionOptionsReader({
root: Deno.cwd(),
}).get("default");

// We can connect to database.
const connection = await createConnection(options);
await connection.close();
} finally {
// Cleanup environment variables.
for (const [env, value] of Object.entries(origEnvVars)) {
if (value != null) {
Deno.env.set(env, value);
} else {
Deno.env.delete(env);
}
}
}
});
});

runIfMain(import.meta);

1 change: 1 addition & 0 deletions test/deps/mocha.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @deno-types="https://unpkg.com/@types/[email protected]/index.d.ts"
import "https://unpkg.com/[email protected]/mocha.js";
import "./global.ts";
mocha.setup({ ui: 'bdd', reporter: 'spec' });

export function runTests(): void {
Expand Down

0 comments on commit 59394ea

Please sign in to comment.