Skip to content

Commit

Permalink
fix: correct reading of custom ormconfig.env files (#6922)
Browse files Browse the repository at this point in the history
update the ConnectionOptionsReader so that ormconfig.env files
are loaded as expected from the filesystem & included as a dotenv
configuration
  • Loading branch information
imnotjames committed Oct 17, 2020
1 parent 60a5dd5 commit a09fb7f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
12 changes: 6 additions & 6 deletions src/connection/ConnectionOptionsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@ export class ConnectionOptionsReader {
return PlatformTools.fileExist(this.baseFilePath + "." + format);
});

// Determine config file name
const configFile = fileExtension ? this.baseFilePath : this.baseFilePath + "." + foundFileFormat;

// if .env file found then load all its variables into process.env using dotenv package
if (foundFileFormat === "env") {
PlatformTools.dotenv(this.baseFilePath);
} else if (PlatformTools.fileExist(".env")) {
PlatformTools.dotenv(".env");
PlatformTools.dotenv(configFile);
} else if (PlatformTools.fileExist(this.baseDirectory + "/.env")) {
PlatformTools.dotenv(this.baseDirectory + "/.env");
}

// Determine config file name
const configFile = fileExtension ? this.baseFilePath : this.baseFilePath + "." + foundFileFormat;

// try to find connection options from any of available sources of configuration
if (PlatformTools.getEnvVariable("TYPEORM_CONNECTION") || PlatformTools.getEnvVariable("TYPEORM_URL")) {
connectionOptions = await new ConnectionOptionsEnvReader().read();
Expand Down
2 changes: 1 addition & 1 deletion test/functional/connection-options-reader/configs/.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
TYPEORM_CONNECTION = mysql
TYPEORM_DATABASE = test-js
TYPEORM_DATABASE = test-env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TYPEORM_CONNECTION = mysql
TYPEORM_DATABASE = test-ormconfig-env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default [{
type: "sqlite",
name: "file",
database: "test-js"
database: "test-js-esm"
}];
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import {promises as fs} from "fs";
import {expect} from "chai";
import {ConnectionOptions} from "../../../src/connection/ConnectionOptions";
import {ConnectionOptionsReader} from "../../../src/connection/ConnectionOptionsReader";
import path from "path";

async function createDotenvFiles() {
// These files may not always exist
await fs.writeFile(path.join(__dirname, "configs/.env"), "TYPEORM_CONNECTION = mysql\nTYPEORM_DATABASE = test-env");
await fs.writeFile(path.join(__dirname, "configs/ormconfig.env"), "TYPEORM_CONNECTION = mysql\nTYPEORM_DATABASE = test-ormconfig-env");
}

describe("ConnectionOptionsReader", () => {
beforeEach(() => {
delete process.env['TYPEORM_CONNECTION'];
delete process.env['TYPEORM_DATABASE'];
});

after(() => {
delete process.env.TYPEORM_CONNECTION;
delete process.env.TYPEORM_DATABASE;
Expand All @@ -26,28 +39,47 @@ describe("ConnectionOptionsReader", () => {
});

it("properly loads config with specified file path", async () => {
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/test-path-config.js" });
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/test-path-config" });
const fileOptions: ConnectionOptions = await connectionOptionsReader.get("file");
expect(fileOptions.database).to.have.string("/test-js");
});

it("properly loads asynchronous config with specified file path", async () => {
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/test-path-config-async.js" });
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/test-path-config-async" });
const fileOptions: ConnectionOptions = await connectionOptionsReader.get("file");
expect(fileOptions.database).to.have.string("/test-js-async");
});

it("properly loads config with specified file path from esm in js", async () => {
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/test-path-config-esm.js" });
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/test-path-config-esm" });
const fileOptions: ConnectionOptions = await connectionOptionsReader.get("file");
expect(fileOptions.database).to.have.string("/test-js");
expect(fileOptions.database).to.have.string("/test-js-esm");
});

// TODO This test requires the configs/.env file be moved to the matching directory in build/compiled
it.skip("properly loads config from .env file", async () => {
const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/.env" });
it("properly loads config from .env file", async () => {
await createDotenvFiles();

const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/.env" });
const [ fileOptions ]: ConnectionOptions[] = await connectionOptionsReader.all();
expect(fileOptions.database).to.have.string("test-env");
expect(process.env.TYPEORM_DATABASE).to.equal("test-env");
});

it("properly loads config from ormconfig.env file", async () => {
await createDotenvFiles();

const connectionOptionsReader = new ConnectionOptionsReader({ root: __dirname, configName: "configs/ormconfig.env" });
const [ fileOptions ]: ConnectionOptions[] = await connectionOptionsReader.all();
expect(fileOptions.database).to.have.string("test-ormconfig-env");
expect(process.env.TYPEORM_DATABASE).to.equal("test-ormconfig-env");
});

it("properly loads config ormconfig.env when given multiple choices", async () => {
await createDotenvFiles();

const connectionOptionsReader = new ConnectionOptionsReader({ root: path.join(__dirname, "configs") });
const [ fileOptions ]: ConnectionOptions[] = await connectionOptionsReader.all();
expect(fileOptions.database).to.have.string("test-js");
expect(process.env.TYPEORM_DATABASE).to.equal("test-js");
expect(fileOptions.database).to.have.string("test-ormconfig-env");
expect(process.env.TYPEORM_DATABASE).to.equal("test-ormconfig-env");
});
});

0 comments on commit a09fb7f

Please sign in to comment.