Skip to content

Commit

Permalink
fix: support ESM in ormconfig js & ts
Browse files Browse the repository at this point in the history
  • Loading branch information
imnotjames committed Oct 5, 2020
1 parent 161432a commit bbdd4d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/connection/ConnectionOptionsReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ export class ConnectionOptionsReader {
if (PlatformTools.getEnvVariable("TYPEORM_CONNECTION") || PlatformTools.getEnvVariable("TYPEORM_URL")) {
connectionOptions = await new ConnectionOptionsEnvReader().read();

} else if (foundFileFormat === "js" || foundFileFormat === "cjs") {
connectionOptions = await require(configFile);
} else if (foundFileFormat === "js" || foundFileFormat === "cjs" || foundFileFormat === "ts") {
const configModule = await require(configFile);

} else if (foundFileFormat === "ts") {
connectionOptions = await require(configFile);
if (configModule && "__esModule" in configModule && "default" in configModule) {
connectionOptions = configModule.default;
} else {
connectionOptions = configModule;
}

} else if (foundFileFormat === "json") {
connectionOptions = require(configFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default [{
type: "sqlite",
name: "file",
database: "test-js"
}];
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ describe("ConnectionOptionsReader", () => {
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 fileOptions: ConnectionOptions = await connectionOptionsReader.get("file");
expect(fileOptions.database).to.have.string("/test-js");
});

// 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" });
Expand Down

0 comments on commit bbdd4d5

Please sign in to comment.