Add knexConfig sections to the config

Signed-off-by: Andrew Shirley <andrew.shirley@sainsburys.co.uk>
This commit is contained in:
Andrew Shirley
2021-09-02 14:39:17 +01:00
committed by Fredrik Adelöw
parent c2da6492fb
commit 6298de32dd
3 changed files with 44 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Add knexConfig config section
+14
View File
@@ -96,6 +96,12 @@ export interface Config {
* @default database
*/
pluginDivisionMode?: 'database' | 'schema';
/**
* Arbitrary config object to pass to knex when initializing
* (https://knexjs.org/#Installation-client). Most notable is the debug
* and asyncStackTraces booleans
*/
knexConfig?: object;
/** Plugin specific database configuration and client override */
plugin?: {
[pluginId: string]: {
@@ -111,6 +117,14 @@ export interface Config {
* Defaults to base config if unspecified.
*/
ensureExists?: boolean;
/**
* Arbitrary config object to pass to knex when initializing
* (https://knexjs.org/#Installation-client). Most notable is the
* debug and asyncStackTraces booleans.
*
* This is merged recursively into the base knexConfig
*/
knexConfig: object;
};
};
};
@@ -14,20 +14,20 @@
* limitations under the License.
*/
import { Knex } from 'knex';
import { omit } from 'lodash';
import { Config, ConfigReader } from '@backstage/config';
import { JsonObject } from '@backstage/types';
import { Knex } from 'knex';
import { merge, omit } from 'lodash';
import { mergeDatabaseConfig } from './config';
import {
createNameOverride,
ensureDatabaseExists,
normalizeConnection,
createSchemaOverride,
ensureSchemaExists,
createDatabaseClient,
createNameOverride,
createSchemaOverride,
ensureDatabaseExists,
ensureSchemaExists,
normalizeConnection,
} from './connection';
import { PluginDatabaseManager } from './types';
import { mergeDatabaseConfig } from './config';
/**
* Provides a config lookup path for a plugin's config block.
@@ -138,6 +138,22 @@ export class DatabaseManager {
};
}
/**
* Provides the knexConfig which should be used for a given plugin.
*
* @param pluginId Plugin to get the knexConfig for
* @returns the merged kexConfig value or undefined if it isn't specified
*/
private getAdditionalKnexConfig(pluginId: string): JsonObject | undefined {
const pluginConfig = this.config.getOptional<JsonObject>(
`${pluginPath(pluginId)}.knexConfig`,
);
const baseConfig = this.config.getOptional<JsonObject>('knexConfig');
return merge(baseConfig, pluginConfig);
}
private getEnsureExistsConfig(pluginId: string): boolean {
const baseConfig = this.config.getOptionalBoolean('ensureExists') ?? true;
return (
@@ -200,6 +216,7 @@ export class DatabaseManager {
const { client } = this.getClientType(pluginId);
return {
...this.getAdditionalKnexConfig(pluginId),
client,
connection: this.getConnectionConfig(pluginId),
};