remove intermediate connector
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'@backstage/backend-defaults': patch
|
||||
---
|
||||
|
||||
Internal minor refactors of the database connectors
|
||||
@@ -14,6 +14,6 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
export * from './mysql';
|
||||
export * from './postgres';
|
||||
export * from './sqlite3';
|
||||
export { MysqlConnector } from './mysql';
|
||||
export { PgConnector } from './postgres';
|
||||
export { Sqlite3Connector } from './sqlite3';
|
||||
|
||||
@@ -25,7 +25,7 @@ import knexFactory, { Knex } from 'knex';
|
||||
import { merge, omit } from 'lodash';
|
||||
import limiterFactory from 'p-limit';
|
||||
import yn from 'yn';
|
||||
import { Connector, DatabaseConnector } from '../types';
|
||||
import { Connector } from '../types';
|
||||
import defaultNameOverride from './defaultNameOverride';
|
||||
import { mergeDatabaseConfig } from './mergeDatabaseConfig';
|
||||
|
||||
@@ -226,19 +226,6 @@ export async function dropMysqlDatabase(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* MySQL database connector.
|
||||
*
|
||||
* Exposes database connector functionality via an immutable object.
|
||||
*/
|
||||
export const mysqlConnector: DatabaseConnector = Object.freeze({
|
||||
createClient: createMysqlDatabaseClient,
|
||||
ensureDatabaseExists: ensureMysqlDatabaseExists,
|
||||
createNameOverride: defaultNameOverride,
|
||||
parseConnectionString: parseMysqlConnectionString,
|
||||
dropDatabase: dropMysqlDatabase,
|
||||
});
|
||||
|
||||
/**
|
||||
* Provides a config lookup path for a plugin's config block.
|
||||
*/
|
||||
@@ -248,14 +235,13 @@ function pluginPath(pluginId: string): string {
|
||||
|
||||
function normalizeConnection(
|
||||
connection: Knex.StaticConnectionConfig | JsonObject | string | undefined,
|
||||
client: string,
|
||||
): Partial<Knex.StaticConnectionConfig> {
|
||||
if (typeof connection === 'undefined' || connection === null) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return typeof connection === 'string' || connection instanceof String
|
||||
? mysqlConnector.parseConnectionString(connection as string, client)
|
||||
? parseMysqlConnectionString(connection as string)
|
||||
: connection;
|
||||
}
|
||||
|
||||
@@ -264,7 +250,7 @@ function createNameOverride(
|
||||
name: string,
|
||||
): Partial<Knex.Config> {
|
||||
try {
|
||||
return mysqlConnector.createNameOverride(name);
|
||||
return defaultNameOverride(name);
|
||||
} catch (e) {
|
||||
throw new InputError(
|
||||
`Unable to create database name override for '${client}' connector`,
|
||||
@@ -281,7 +267,7 @@ export class MysqlConnector implements Connector {
|
||||
|
||||
async getClient(
|
||||
pluginId: string,
|
||||
deps?: {
|
||||
_deps?: {
|
||||
lifecycle: LifecycleService;
|
||||
pluginMetadata: PluginMetadataService;
|
||||
},
|
||||
@@ -293,7 +279,7 @@ export class MysqlConnector implements Connector {
|
||||
const databaseName = this.getDatabaseName(pluginId);
|
||||
if (databaseName && this.getEnsureExistsConfig(pluginId)) {
|
||||
try {
|
||||
await mysqlConnector.ensureDatabaseExists!(pluginConfig, databaseName);
|
||||
await ensureMysqlDatabaseExists(pluginConfig, databaseName);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
|
||||
@@ -313,10 +299,9 @@ export class MysqlConnector implements Connector {
|
||||
this.getDatabaseOverrides(pluginId),
|
||||
);
|
||||
|
||||
const client = mysqlConnector.createClient(
|
||||
const client = createMysqlDatabaseClient(
|
||||
pluginConfig,
|
||||
databaseClientOverrides,
|
||||
deps,
|
||||
);
|
||||
|
||||
return client;
|
||||
@@ -418,12 +403,9 @@ export class MysqlConnector implements Connector {
|
||||
* unless `pluginDivisionMode` is set to `schema`.
|
||||
*/
|
||||
private getConnectionConfig(pluginId: string): Knex.StaticConnectionConfig {
|
||||
const { client, overridden } = this.getClientType(pluginId);
|
||||
const { overridden } = this.getClientType(pluginId);
|
||||
|
||||
let baseConnection = normalizeConnection(
|
||||
this.config.get('connection'),
|
||||
this.config.getString('client'),
|
||||
);
|
||||
let baseConnection = normalizeConnection(this.config.get('connection'));
|
||||
|
||||
// Databases cannot be shared unless the `pluginDivisionMode` is set to `schema`. The
|
||||
// `database` property from the base connection is omitted unless `pluginDivisionMode`
|
||||
@@ -435,7 +417,6 @@ export class MysqlConnector implements Connector {
|
||||
// get and normalize optional plugin specific database connection
|
||||
const connection = normalizeConnection(
|
||||
this.config.getOptional(`${pluginPath(pluginId)}.connection`),
|
||||
client,
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
@@ -25,7 +25,7 @@ import knexFactory, { Knex } from 'knex';
|
||||
import { merge, omit } from 'lodash';
|
||||
import limiterFactory from 'p-limit';
|
||||
import { Client } from 'pg';
|
||||
import { Connector, DatabaseConnector } from '../types';
|
||||
import { Connector } from '../types';
|
||||
import defaultNameOverride from './defaultNameOverride';
|
||||
import defaultSchemaOverride from './defaultSchemaOverride';
|
||||
import { mergeDatabaseConfig } from './mergeDatabaseConfig';
|
||||
@@ -232,16 +232,6 @@ export async function dropPgDatabase(
|
||||
}
|
||||
}
|
||||
|
||||
export const pgConnector: DatabaseConnector = Object.freeze({
|
||||
createClient: createPgDatabaseClient,
|
||||
ensureDatabaseExists: ensurePgDatabaseExists,
|
||||
ensureSchemaExists: ensurePgSchemaExists,
|
||||
createNameOverride: defaultNameOverride,
|
||||
createSchemaOverride: defaultSchemaOverride,
|
||||
parseConnectionString: parsePgConnectionString,
|
||||
dropDatabase: dropPgDatabase,
|
||||
});
|
||||
|
||||
/**
|
||||
* Provides a config lookup path for a plugin's config block.
|
||||
*/
|
||||
@@ -251,14 +241,13 @@ function pluginPath(pluginId: string): string {
|
||||
|
||||
function normalizeConnection(
|
||||
connection: Knex.StaticConnectionConfig | JsonObject | string | undefined,
|
||||
client: string,
|
||||
): Partial<Knex.StaticConnectionConfig> {
|
||||
if (typeof connection === 'undefined' || connection === null) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return typeof connection === 'string' || connection instanceof String
|
||||
? pgConnector.parseConnectionString(connection as string, client)
|
||||
? parsePgConnectionString(connection as string)
|
||||
: connection;
|
||||
}
|
||||
|
||||
@@ -267,7 +256,7 @@ function createSchemaOverride(
|
||||
name: string,
|
||||
): Partial<Knex.Config | undefined> {
|
||||
try {
|
||||
return pgConnector.createSchemaOverride?.(name);
|
||||
return defaultSchemaOverride(name);
|
||||
} catch (e) {
|
||||
throw new InputError(
|
||||
`Unable to create database schema override for '${client}' connector`,
|
||||
@@ -281,7 +270,7 @@ function createNameOverride(
|
||||
name: string,
|
||||
): Partial<Knex.Config> {
|
||||
try {
|
||||
return pgConnector.createNameOverride(name);
|
||||
return defaultNameOverride(name);
|
||||
} catch (e) {
|
||||
throw new InputError(
|
||||
`Unable to create database name override for '${client}' connector`,
|
||||
@@ -298,7 +287,7 @@ export class PgConnector implements Connector {
|
||||
|
||||
async getClient(
|
||||
pluginId: string,
|
||||
deps?: {
|
||||
_deps?: {
|
||||
lifecycle: LifecycleService;
|
||||
pluginMetadata: PluginMetadataService;
|
||||
},
|
||||
@@ -310,7 +299,7 @@ export class PgConnector implements Connector {
|
||||
const databaseName = this.getDatabaseName(pluginId);
|
||||
if (databaseName && this.getEnsureExistsConfig(pluginId)) {
|
||||
try {
|
||||
await pgConnector.ensureDatabaseExists!(pluginConfig, databaseName);
|
||||
await ensurePgDatabaseExists(pluginConfig, databaseName);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
|
||||
@@ -326,7 +315,7 @@ export class PgConnector implements Connector {
|
||||
this.getEnsureExistsConfig(pluginId)
|
||||
) {
|
||||
try {
|
||||
await pgConnector.ensureSchemaExists!(pluginConfig, pluginId);
|
||||
await ensurePgSchemaExists(pluginConfig, pluginId);
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Failed to connect to the database to make sure that schema for plugin '${pluginId}' exists, ${error}`,
|
||||
@@ -341,10 +330,9 @@ export class PgConnector implements Connector {
|
||||
schemaOverrides,
|
||||
);
|
||||
|
||||
const client = pgConnector.createClient(
|
||||
const client = createPgDatabaseClient(
|
||||
pluginConfig,
|
||||
databaseClientOverrides,
|
||||
deps,
|
||||
);
|
||||
|
||||
return client;
|
||||
@@ -465,12 +453,9 @@ export class PgConnector implements Connector {
|
||||
* unless `pluginDivisionMode` is set to `schema`.
|
||||
*/
|
||||
private getConnectionConfig(pluginId: string): Knex.StaticConnectionConfig {
|
||||
const { client, overridden } = this.getClientType(pluginId);
|
||||
const { overridden } = this.getClientType(pluginId);
|
||||
|
||||
let baseConnection = normalizeConnection(
|
||||
this.config.get('connection'),
|
||||
this.config.getString('client'),
|
||||
);
|
||||
let baseConnection = normalizeConnection(this.config.get('connection'));
|
||||
|
||||
// Databases cannot be shared unless the `pluginDivisionMode` is set to `schema`. The
|
||||
// `database` property from the base connection is omitted unless `pluginDivisionMode`
|
||||
@@ -482,7 +467,6 @@ export class PgConnector implements Connector {
|
||||
// get and normalize optional plugin specific database connection
|
||||
const connection = normalizeConnection(
|
||||
this.config.getOptional(`${pluginPath(pluginId)}.connection`),
|
||||
client,
|
||||
);
|
||||
|
||||
(
|
||||
|
||||
@@ -26,7 +26,7 @@ import { ensureDirSync } from 'fs-extra';
|
||||
import knexFactory, { Knex } from 'knex';
|
||||
import { merge, omit } from 'lodash';
|
||||
import path from 'path';
|
||||
import { Connector, DatabaseConnector } from '../types';
|
||||
import { Connector } from '../types';
|
||||
import { mergeDatabaseConfig } from './mergeDatabaseConfig';
|
||||
|
||||
/**
|
||||
@@ -157,17 +157,6 @@ export function parseSqliteConnectionString(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* SQLite3 database connector.
|
||||
*
|
||||
* Exposes database connector functionality via an immutable object.
|
||||
*/
|
||||
export const sqliteConnector: DatabaseConnector = Object.freeze({
|
||||
createClient: createSqliteDatabaseClient,
|
||||
createNameOverride: createSqliteNameOverride,
|
||||
parseConnectionString: parseSqliteConnectionString,
|
||||
});
|
||||
|
||||
/**
|
||||
* Provides a config lookup path for a plugin's config block.
|
||||
*/
|
||||
@@ -177,14 +166,13 @@ function pluginPath(pluginId: string): string {
|
||||
|
||||
function normalizeConnection(
|
||||
connection: Knex.StaticConnectionConfig | JsonObject | string | undefined,
|
||||
client: string,
|
||||
): Partial<Knex.StaticConnectionConfig> {
|
||||
if (typeof connection === 'undefined' || connection === null) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return typeof connection === 'string' || connection instanceof String
|
||||
? sqliteConnector.parseConnectionString(connection as string, client)
|
||||
? parseSqliteConnectionString(connection as string)
|
||||
: connection;
|
||||
}
|
||||
|
||||
@@ -193,7 +181,7 @@ function createNameOverride(
|
||||
name: string,
|
||||
): Partial<Knex.Config> {
|
||||
try {
|
||||
return sqliteConnector.createNameOverride(name);
|
||||
return createSqliteNameOverride(name);
|
||||
} catch (e) {
|
||||
throw new InputError(
|
||||
`Unable to create database name override for '${client}' connector`,
|
||||
@@ -228,7 +216,7 @@ export class Sqlite3Connector implements Connector {
|
||||
this.getDatabaseOverrides(pluginId),
|
||||
);
|
||||
|
||||
const client = sqliteConnector.createClient(
|
||||
const client = createSqliteDatabaseClient(
|
||||
pluginConfig,
|
||||
databaseClientOverrides,
|
||||
deps,
|
||||
@@ -340,10 +328,7 @@ export class Sqlite3Connector implements Connector {
|
||||
private getConnectionConfig(pluginId: string): Knex.StaticConnectionConfig {
|
||||
const { client, overridden } = this.getClientType(pluginId);
|
||||
|
||||
let baseConnection = normalizeConnection(
|
||||
this.config.get('connection'),
|
||||
this.config.getString('client'),
|
||||
);
|
||||
let baseConnection = normalizeConnection(this.config.get('connection'));
|
||||
|
||||
if (
|
||||
client.includes('sqlite3') &&
|
||||
@@ -366,7 +351,6 @@ export class Sqlite3Connector implements Connector {
|
||||
// get and normalize optional plugin specific database connection
|
||||
const connection = normalizeConnection(
|
||||
this.config.getOptional(`${pluginPath(pluginId)}.connection`),
|
||||
client,
|
||||
);
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user