Skip to content

Commit

Permalink
feat: support ESM in ormconfig js & ts (#6853)
Browse files Browse the repository at this point in the history
fixes #5003
  • Loading branch information
imnotjames committed Oct 5, 2020
1 parent 161432a commit 7ebca2b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
13 changes: 13 additions & 0 deletions docs/using-ormconfig.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@ module.exports = {
}
```

Alternatively, you may use the ECMAScript module format if your environment supports it:

```javascript
export default {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test"
}
```

You can specify any other options from [ConnectionOptions](./connection-options.md).
If you want to create multiple connections then simply create multiple connections in a single array and return it.

Expand Down
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 7ebca2b

Please sign in to comment.