feat(db-config): support a ensureExists config option

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2021-07-08 17:53:19 -04:00
parent 9eccc589b7
commit 5f6f2fd96f
3 changed files with 43 additions and 7 deletions
+10
View File
@@ -64,6 +64,11 @@ export interface Config {
connection: string | object;
/** Database name prefix override */
prefix?: string;
/**
* Whether to ensure the given database exists by creating it if it does not.
* Defaults to true if unspecified.
*/
ensureExists?: boolean;
/** Plugin specific database configuration and client override */
plugin?: {
[pluginId: string]: {
@@ -74,6 +79,11 @@ export interface Config {
* @secret
*/
connection?: string | object;
/**
* Whether to ensure the given database exists by creating it if it does not.
* Defaults to base config if unspecified.
*/
ensureExists?: boolean;
};
};
};
@@ -128,6 +128,14 @@ export class DatabaseManager {
};
}
private getEnsureExistsConfig(pluginId: string): boolean {
const baseConfig = this.config.getOptionalBoolean('ensureExists') ?? true;
return (
this.config.getOptionalBoolean(`${pluginPath(pluginId)}.ensureExists`) ??
baseConfig
);
}
/**
* Provides a Knex connection plugin config by combining base and plugin config.
*
@@ -203,13 +211,15 @@ export class DatabaseManager {
this.getConfigForPlugin(pluginId) as JsonObject,
);
const databaseName = this.getDatabaseName(pluginId);
try {
await ensureDatabaseExists(pluginConfig, databaseName);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
);
if (this.getEnsureExistsConfig(pluginId)) {
const databaseName = this.getDatabaseName(pluginId);
try {
await ensureDatabaseExists(pluginConfig, databaseName);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
);
}
}
return createDatabaseClient(