Skip to content

Commit b28a76f

Browse files
authored
Merge pull request #69 from leapfrogtechnology/resolver
Support connection resolver
2 parents 93f5773 + 437be06 commit b28a76f

File tree

8 files changed

+64
-12
lines changed

8 files changed

+64
-12
lines changed

src/commands/migrate-latest.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class MigrateLatest extends Command {
1414
only: flags.string({
1515
helpValue: 'CONNECTION_ID',
1616
description: 'Filter only a single connection.'
17+
}),
18+
'connection-resolver': flags.string({
19+
helpValue: 'PATH',
20+
description: 'Path to the connection resolver.'
1721
})
1822
};
1923

@@ -60,7 +64,7 @@ class MigrateLatest extends Command {
6064
async run(): Promise<void> {
6165
const { flags: parsedFlags } = this.parse(MigrateLatest);
6266
const config = await loadConfig();
63-
const connections = await resolveConnections();
67+
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
6468

6569
const results = await migrateLatest(config, connections, {
6670
...parsedFlags,

src/commands/migrate-list.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class MigrateList extends Command {
1313
only: flags.string({
1414
helpValue: 'CONNECTION_ID',
1515
description: 'Filter only a single connection.'
16+
}),
17+
'connection-resolver': flags.string({
18+
helpValue: 'PATH',
19+
description: 'Path to the connection resolver.'
1620
})
1721
};
1822

@@ -64,7 +68,7 @@ class MigrateList extends Command {
6468
async run(): Promise<void> {
6569
const { flags: parsedFlags } = this.parse(MigrateList);
6670
const config = await loadConfig();
67-
const connections = await resolveConnections();
71+
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
6872

6973
const results = await migrateList(config, connections, {
7074
...parsedFlags,

src/commands/migrate-rollback.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class MigrateRollback extends Command {
1414
only: flags.string({
1515
helpValue: 'CONNECTION_ID',
1616
description: 'Filter only a single connection.'
17+
}),
18+
'connection-resolver': flags.string({
19+
helpValue: 'PATH',
20+
description: 'Path to the connection resolver.'
1721
})
1822
};
1923

@@ -60,7 +64,7 @@ class MigrateRollback extends Command {
6064
async run(): Promise<void> {
6165
const { flags: parsedFlags } = this.parse(MigrateRollback);
6266
const config = await loadConfig();
63-
const connections = await resolveConnections();
67+
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
6468

6569
const results = await migrateRollback(config, connections, {
6670
...parsedFlags,

src/commands/prune.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class Prune extends Command {
1313
only: flags.string({
1414
helpValue: 'CONNECTION_ID',
1515
description: 'Filter only a single connection.'
16+
}),
17+
'connection-resolver': flags.string({
18+
helpValue: 'PATH',
19+
description: 'Path to the connection resolver.'
1620
})
1721
};
1822

@@ -40,7 +44,7 @@ class Prune extends Command {
4044
async run(): Promise<void> {
4145
const { flags: parsedFlags } = this.parse(Prune);
4246
const config = await loadConfig();
43-
const connections = await resolveConnections();
47+
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
4448

4549
const results = await prune(config, connections, {
4650
...parsedFlags,

src/commands/synchronize.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ class Synchronize extends Command {
1515
* Available CLI flags.
1616
*/
1717
static flags = {
18-
force: flags.boolean({ char: 'f', description: 'Force synchronization' }),
19-
'skip-migration': flags.boolean({ description: 'Skip running migrations' }),
18+
force: flags.boolean({ char: 'f', description: 'Force synchronization.' }),
19+
'skip-migration': flags.boolean({ description: 'Skip running migrations.' }),
2020
only: flags.string({
2121
helpValue: 'CONNECTION_ID',
2222
description: 'Filter only a single connection.'
23+
}),
24+
'connection-resolver': flags.string({
25+
helpValue: 'PATH',
26+
description: 'Path to the connection resolver.'
2327
})
2428
};
2529

@@ -127,7 +131,7 @@ class Synchronize extends Command {
127131

128132
try {
129133
const config = await loadConfig();
130-
const connections = await resolveConnections();
134+
const connections = await resolveConnections(config, parsedFlags['connection-resolver']);
131135
const timeStart = process.hrtime();
132136

133137
await printLine('Synchronizing...\n');

src/config.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import ConnectionsFileSchema from './domain/ConnectionsFileSchema';
1111
import { prepareInjectionConfigVars } from './service/configInjection';
1212
import { 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
*/
188218
async 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;

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const CONNECTIONS_FILENAME = 'connections.sync-db.json';
1414
export const INJECTED_CONFIG_TABLE = '__sync_db_injected_config';
1515
export const DEFAULT_CONFIG: Configuration = {
1616
basePath: path.resolve(process.cwd(), 'src'),
17+
connectionResolver: '',
1718
execution: 'parallel',
1819
sql: [],
1920
hooks: {

src/domain/Configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface Configuration {
77
basePath: string;
88
execution: 'parallel' | 'sequential';
99
sql: string[];
10+
connectionResolver: string;
1011
hooks: {
1112
pre_sync: string[];
1213
post_sync: string[];

0 commit comments

Comments
 (0)