forked from typeorm/typeorm
/
ConnectionOptionsYmlReader.ts
32 lines (25 loc) · 1 KB
/
ConnectionOptionsYmlReader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import ymlParser from 'js-yaml';
import {PlatformTools} from "../../platform/PlatformTools";
import {ConnectionOptions} from "../ConnectionOptions";
/**
* Reads connection options defined in the yml file.
*/
export class ConnectionOptionsYmlReader {
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Reads connection options from given yml file.
*/
async read(path: string): Promise<ConnectionOptions[]> {
const contentsBuffer = PlatformTools.readFileSync(path);
const contents = contentsBuffer.toString();
const config: undefined | string | {[key: string]: any} = ymlParser.safeLoad(contents);
if (typeof config !== 'object') {
return [];
}
return Object.keys(config).map(connectionName => {
return Object.assign({ name: connectionName }, config[connectionName]);
});
}
}