Fix review comments
- Combine the various exported types; the only API surface is now the `PluginDatabaseClientFactory` which is a free function. - Rename to the SingleConnectionManager class. - Refactor the PluginEnvironment type to use databaseClientFactory for clarity. - Fix mergeDatabaseConfig to not override the input dictionary.
This commit is contained in:
@@ -8,14 +8,12 @@
|
||||
|
||||
import Router from 'express-promise-router';
|
||||
import {
|
||||
ensureDatabaseExists,
|
||||
createServiceBuilder,
|
||||
loadBackendConfig,
|
||||
getRootLogger,
|
||||
useHotMemoize,
|
||||
notFoundHandler,
|
||||
SingleDatabaseConfiguration,
|
||||
SingleDatabaseManager,
|
||||
SingleConnectionManager,
|
||||
SingleHostDiscovery,
|
||||
UrlReaders,
|
||||
} from '@backstage/backend-common';
|
||||
@@ -34,15 +32,20 @@ function makeCreateEnv(config: ConfigReader) {
|
||||
|
||||
root.info(`Created UrlReader ${reader}`);
|
||||
|
||||
const databaseConfig = new SingleDatabaseConfiguration(
|
||||
config.getConfig('backend.database'),
|
||||
)
|
||||
const databaseManager = new SingleDatabaseManager(databaseConfig);
|
||||
const databaseManager = new SingleConnectionManager(
|
||||
config.getConfig('backend.database')
|
||||
);
|
||||
|
||||
return (plugin: string): PluginEnvironment => {
|
||||
const logger = root.child({ type: 'plugin', plugin });
|
||||
const databaseFactory = databaseManager.getDatabaseFactory(plugin);
|
||||
return { logger, database: databaseFactory, config, reader, discovery };
|
||||
const databaseFactory = databaseManager.getDatabaseClientFactory(plugin);
|
||||
return {
|
||||
logger,
|
||||
databaseClientFactory: databaseFactory,
|
||||
config,
|
||||
reader,
|
||||
discovery
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -50,11 +53,6 @@ async function main() {
|
||||
const configs = await loadBackendConfig();
|
||||
const configReader = ConfigReader.fromConfigs(configs);
|
||||
const createEnv = makeCreateEnv(configReader);
|
||||
await ensureDatabaseExists(
|
||||
configReader.getConfig('backend.database'),
|
||||
'backstage_plugin_catalog',
|
||||
'backstage_plugin_auth',
|
||||
);
|
||||
|
||||
const catalogEnv = useHotMemoize(module, () => createEnv('catalog'));
|
||||
const scaffolderEnv = useHotMemoize(module, () => createEnv('scaffolder'));
|
||||
|
||||
@@ -3,9 +3,14 @@ import { PluginEnvironment } from '../types';
|
||||
|
||||
export default async function createPlugin({
|
||||
logger,
|
||||
database,
|
||||
databaseClientFactory,
|
||||
config,
|
||||
discovery,
|
||||
}: PluginEnvironment) {
|
||||
return await createRouter({ logger, config, database, discovery });
|
||||
return await createRouter({
|
||||
logger,
|
||||
config,
|
||||
database: databaseClientFactory,
|
||||
discovery
|
||||
});
|
||||
}
|
||||
|
||||
@@ -14,11 +14,14 @@ export default async function createPlugin({
|
||||
logger,
|
||||
config,
|
||||
reader,
|
||||
database,
|
||||
databaseClientFactory,
|
||||
}: PluginEnvironment) {
|
||||
const locationReader = new LocationReaders({ logger, reader, config });
|
||||
|
||||
const db = await DatabaseManager.createDatabase(database, { logger });
|
||||
const db = await DatabaseManager.createDatabase(
|
||||
await databaseClientFactory(),
|
||||
{ logger },
|
||||
);
|
||||
const entitiesCatalog = new DatabaseEntitiesCatalog(db);
|
||||
const locationsCatalog = new DatabaseLocationsCatalog(db);
|
||||
const higherOrderOperation = new HigherOrderOperations(
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
import Knex from 'knex';
|
||||
import { Logger } from 'winston';
|
||||
import { Config } from '@backstage/config';
|
||||
import { PluginEndpointDiscovery, UrlReader } from '@backstage/backend-common';
|
||||
import {
|
||||
PluginDatabaseClientFactory,
|
||||
PluginEndpointDiscovery,
|
||||
UrlReader
|
||||
} from '@backstage/backend-common';
|
||||
|
||||
export type PluginEnvironment = {
|
||||
logger: Logger;
|
||||
database: Knex;
|
||||
databaseClientFactory: PluginDatabaseClientFactory;
|
||||
config: Config;
|
||||
reader: UrlReader
|
||||
discovery: PluginEndpointDiscovery;
|
||||
|
||||
Reference in New Issue
Block a user