Skip to content

Commit

Permalink
fix: correctly load yml in ConnectionOptionsYmlReader (#7743)
Browse files Browse the repository at this point in the history
this uses the correct `load` function instead of `loadAll`
  • Loading branch information
imnotjames committed Jun 16, 2021
1 parent 7d614e9 commit 57f9254
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/connection/options-reader/ConnectionOptionsYmlReader.ts
Expand Up @@ -18,14 +18,14 @@ export class ConnectionOptionsYmlReader {
const contentsBuffer = PlatformTools.readFileSync(path);
const contents = contentsBuffer.toString();

const config: undefined | string | {[key: string]: any} = ymlParser.loadAll(contents);
const config = ymlParser.load(contents);

if (typeof config !== 'object') {
if (!config || typeof config !== 'object') {
return [];
}

return Object.keys(config).map(connectionName => {
return Object.assign({ name: connectionName }, config[connectionName]);
return Object.assign({ name: connectionName }, (config as any)[connectionName]);
});
}

Expand Down
@@ -0,0 +1,3 @@
- type: "sqlite"
name: "file"
database: "test-yaml"
Expand Up @@ -5,9 +5,14 @@ import {ConnectionOptionsReader} from "../../../src/connection/ConnectionOptions
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");
// 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");
}

async function createYamlFiles() {
await fs.mkdir(path.join(__dirname, "configs/yaml"));
await fs.writeFile(path.join(__dirname, "configs/yaml/test-yaml.yaml"), "- type: \"sqlite\"\n name: \"file\"\n database: \"test-yaml\"");
}

describe("ConnectionOptionsReader", () => {
Expand Down Expand Up @@ -82,4 +87,12 @@ describe("ConnectionOptionsReader", () => {
expect(fileOptions.database).to.have.string("test-ormconfig-env");
expect(process.env.TYPEORM_DATABASE).to.equal("test-ormconfig-env");
});

it.only("properly loads config from yaml", async () => {
await createYamlFiles();

const connectionOptionsReader = new ConnectionOptionsReader({ root: path.join(__dirname, "configs/yaml"), configName: "test-yaml" });
const fileOptions: ConnectionOptions = await connectionOptionsReader.get("file");
expect(fileOptions.database).to.have.string("/test-yaml");
})
});

0 comments on commit 57f9254

Please sign in to comment.