@@ -9,7 +9,7 @@ import DbConfig from './domain/DbConfig';
99import SyncConfig from './domain/SyncConfig' ;
1010import ConnectionConfig from './domain/ConnectionConfig' ;
1111import { 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 */
111117export 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+ }
0 commit comments