@@ -11,6 +11,10 @@ import ConnectionsFileSchema from './domain/ConnectionsFileSchema';
1111import { prepareInjectionConfigVars } from './service/configInjection' ;
1212import { DEFAULT_CONFIG , CONFIG_FILENAME , CONNECTIONS_FILENAME , REQUIRED_ENV_KEYS } from './constants' ;
1313
14+ interface ConnectionResolver {
15+ resolve : ( ) => Promise < ConnectionConfig [ ] > ;
16+ }
17+
1418/**
1519 * Check if this is being run via the sync-db cli or not.
1620 *
@@ -88,18 +92,23 @@ export function validate(config: Configuration) {
8892 *
8993 * @returns {Promise<ConnectionConfig[]> }
9094 */
91- export async function resolveConnections ( ) : Promise < ConnectionConfig [ ] > {
95+
96+ export async function resolveConnections ( config : Configuration , resolver ?: string ) : Promise < ConnectionConfig [ ] > {
9297 log ( 'Resolving database connections.' ) ;
9398
9499 const filename = path . resolve ( process . cwd ( ) , CONNECTIONS_FILENAME ) ;
95100 const connectionsFileExists = await fs . exists ( filename ) ;
96101
97- let connections ;
102+ let connections : ConnectionConfig [ ] ;
98103
99- // If connections file exists, resolve connections from that.
100- // otherwise fallback to getting the connection from the env vars.
104+ // Connection resolution process:
105+ // 1. If connections file exists, use that to resolve connections.
106+ // 2. If connection resolver is set via flag or configuration, use it.
107+ // 3. If 1 & 2 are false, try resolving the connections from the environment. If not found fail with error.
101108 if ( connectionsFileExists ) {
102109 connections = await resolveConnectionsFromFile ( filename ) ;
110+ } else if ( resolver || config . connectionResolver ) {
111+ connections = await resolveConnectionsUsingResolver ( resolver || config . connectionResolver ) ;
103112 } else {
104113 log ( 'Connections file not provided.' ) ;
105114
@@ -121,6 +130,27 @@ export async function resolveConnections(): Promise<ConnectionConfig[]> {
121130 return connections ;
122131}
123132
133+ /**
134+ * Resolve connections using the provided connection resolver.
135+ *
136+ * @param {Configuration } config
137+ * @param {string } resolver
138+ * @returns {Promise<ConnectionConfig[]> }
139+ */
140+ export async function resolveConnectionsUsingResolver ( resolver : string ) : Promise < ConnectionConfig [ ] > {
141+ log ( 'Resolving connection resolver: %s' , resolver ) ;
142+
143+ const resolverPath = resolver ? path . resolve ( process . cwd ( ) , resolver ) : '' ;
144+
145+ const { resolve } = ( await import ( resolverPath ) ) as ConnectionResolver ;
146+
147+ if ( ! resolve ) {
148+ throw new Error ( `Resolver '${ resolver } ' does not expose a 'resolve' function.` ) ;
149+ }
150+
151+ return resolve ( ) ;
152+ }
153+
124154/**
125155 * Get the connection id from the config.
126156 *
@@ -186,7 +216,7 @@ export function resolveConnectionsFromEnv(): ConnectionConfig[] {
186216 * @returns {Promise<ConnectionConfig[]> }
187217 */
188218async function resolveConnectionsFromFile ( filename : string ) : Promise < ConnectionConfig [ ] > {
189- log ( 'Resolving file: %s' , filename ) ;
219+ log ( 'Resolving connections file: %s' , filename ) ;
190220
191221 const loaded = await fs . read ( filename ) ;
192222 const { connections } = JSON . parse ( loaded ) as ConnectionsFileSchema ;
0 commit comments