Skip to content

Commit 2e575e4

Browse files
authored
Merge pull request #45 from leapfrogtechnology/env-vars
Support resolving connections from the environment directly as a fallback option
2 parents 3d6fcbc + 603c0ce commit 2e575e4

File tree

3 files changed

+76
-8
lines changed

3 files changed

+76
-8
lines changed

src/config.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import DbConfig from './domain/DbConfig';
99
import SyncConfig from './domain/SyncConfig';
1010
import ConnectionConfig from './domain/ConnectionConfig';
1111
import { prepareInjectionConfigVars } from './services/configInjection';
12-
import { DEFAULT_CONFIG, CONFIG_FILENAME, CONNECTIONS_FILENAME, ENV_KEYS } from './constants';
12+
import { DEFAULT_CONFIG, CONFIG_FILENAME, CONNECTIONS_FILENAME, REQUIRED_ENV_KEYS } from './constants';
1313

1414
/**
1515
* Load config yaml file.
@@ -63,13 +63,19 @@ export async function resolveConnections(): Promise<ConnectionConfig[]> {
6363
log('Resolving database connections.');
6464

6565
const filename = path.resolve(process.cwd(), CONNECTIONS_FILENAME);
66+
const connectionsFileExists = await fs.exists(filename);
6667

67-
log('Resolving file: %s', filename);
68+
let connections;
6869

69-
const loaded = await fs.read(filename);
70-
const { connections } = JSON.parse(loaded) as DbConfig;
70+
// If connections file exists, resolve connections from that.
71+
// otherwise fallback to getting the connection from the env vars.
72+
if (connectionsFileExists) {
73+
connections = await resolveConnectionsFromFile(filename);
74+
} else {
75+
log('Connections file not provided.');
7176

72-
// TODO: Validate the connections received from file.
77+
connections = resolveConnectionsFromEnv();
78+
}
7379

7480
log(
7581
'Resolved connections: %O',
@@ -109,10 +115,13 @@ function validateConnections(keys: string[]): void {
109115
* @returns {ConnectionConfig[]}
110116
*/
111117
export function resolveConnectionsFromEnv(): ConnectionConfig[] {
112-
validateConnections(ENV_KEYS);
118+
log('Resolving connections from the environment variables.');
119+
120+
validateConnections(REQUIRED_ENV_KEYS);
113121

114122
const connection = {
115123
client: process.env.DB_CLIENT,
124+
id: process.env.DB_ID,
116125
host: process.env.DB_HOST,
117126
port: process.env.DB_PORT ? +process.env.DB_PORT : null,
118127
user: process.env.DB_USERNAME,
@@ -125,3 +134,20 @@ export function resolveConnectionsFromEnv(): ConnectionConfig[] {
125134

126135
return [connection];
127136
}
137+
138+
/**
139+
* Resolve connections from the file.
140+
*
141+
* @param {string} filename
142+
* @returns {Promise<ConnectionConfig[]>}
143+
*/
144+
async function resolveConnectionsFromFile(filename: string): Promise<ConnectionConfig[]> {
145+
log('Resolving file: %s', filename);
146+
147+
const loaded = await fs.read(filename);
148+
const { connections } = JSON.parse(loaded) as DbConfig;
149+
150+
// TODO: Validate the connections received from file.
151+
152+
return connections;
153+
}

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ export const DEFAULT_SYNC_PARAMS: SyncParams = {
3232
onFailed: (context: ExecutionContext) => Promise.resolve()
3333
};
3434

35-
export const ENV_KEYS = ['DB_HOST', 'DB_PASSWORD', 'DB_NAME', 'DB_USERNAME', 'DB_PORT', 'DB_CLIENT', 'DB_ENCRYPTION'];
35+
export const REQUIRED_ENV_KEYS = ['DB_HOST', 'DB_PASSWORD', 'DB_NAME', 'DB_USERNAME', 'DB_PORT', 'DB_CLIENT'];

test/config.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
import { expect } from 'chai';
22
import { it, describe } from 'mocha';
33

4-
import { validate, getConnectionId } from '../src/config';
54
import ConnectionConfig from '../src/domain/ConnectionConfig';
5+
import { validate, getConnectionId, resolveConnectionsFromEnv } from '../src/config';
66

77
describe('config:', () => {
8+
describe('resolveConnectionsFromEnv', () => {
9+
it('should return the connection using the env vars.', () => {
10+
process.env.DB_CLIENT = 'mssql';
11+
process.env.DB_ID = 'mydb';
12+
process.env.DB_HOST = 'localhost';
13+
process.env.DB_PORT = '1234';
14+
process.env.DB_USERNAME = 'user';
15+
process.env.DB_PASSWORD = 'password';
16+
process.env.DB_NAME = 'database';
17+
18+
const connections = resolveConnectionsFromEnv();
19+
20+
expect(connections[0]).to.deep.equal({
21+
client: 'mssql',
22+
id: 'mydb',
23+
host: 'localhost',
24+
port: 1234,
25+
user: 'user',
26+
password: 'password',
27+
database: 'database',
28+
options: {
29+
encrypt: false
30+
}
31+
});
32+
33+
// Cleanup
34+
process.env.DB_CLIENT = '';
35+
process.env.DB_ID = '';
36+
process.env.DB_HOST = '';
37+
process.env.DB_PORT = '';
38+
process.env.DB_USERNAME = '';
39+
process.env.DB_PASSWORD = '';
40+
process.env.DB_NAME = '';
41+
});
42+
43+
it('should throw an error if the required env vars are not provided.', () => {
44+
expect(() => resolveConnectionsFromEnv()).to.throw(
45+
'Following environment variables were not set: DB_HOST, DB_PASSWORD, DB_NAME, DB_USERNAME, DB_PORT, DB_CLIENT'
46+
);
47+
});
48+
});
49+
850
describe('validate', () => {
951
it('throws error if injectedConfig.vars is not found or is invalid in the config.', () => {
1052
expect(() => validate({ injectedConfig: { vars: 'foobar' } } as any)).to.throw(

0 commit comments

Comments
 (0)