-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#90): Could not connect to the postgres database when TYPEORM_POR…
…T is set (#92)
- Loading branch information
Showing
4 changed files
with
64 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|