refactor: add defaultNameOverride and reorganize

Signed-off-by: Minn Soe <contributions@minn.io>
This commit is contained in:
Minn Soe
2021-06-07 13:07:24 +01:00
parent 214efffe3a
commit f1a9108c53
11 changed files with 114 additions and 35 deletions
+8
View File
@@ -104,6 +104,14 @@ export function createServiceBuilder(_module: NodeModule): ServiceBuilderImpl;
// @public (undocumented)
export function createStatusCheckRouter(options: StatusCheckRouterOptions): Promise<express.Router>;
// @public
export interface DatabaseConnector {
createClient(dbConfig: Config, overrides?: Partial<Knex.Config>): Knex;
createNameOverride(name: string): Partial<Knex.Config>;
ensureDatabaseExists?(dbConfig: Config, ...databases: Array<string>): Promise<void>;
parseConnectionString(connectionString: string, client?: string): Knex.StaticConnectionConfig;
}
// @public (undocumented)
export class DatabaseManager {
forPlugin(pluginId: string): PluginDatabaseManager;
@@ -20,9 +20,7 @@ import knexFactory, { Knex } from 'knex';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './types';
import { mysqlConnector } from './mysql';
import { pgConnector } from './postgres';
import { sqlite3Connector } from './sqlite3';
import { mysqlConnector, pgConnector, sqlite3Connector } from './connectors';
type DatabaseClient = 'pg' | 'sqlite3' | 'mysql' | 'mysql2' | string;
@@ -0,0 +1,26 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import defaultNameOverride from './defaultNameOverride';
describe('defaultNameOverride()', () => {
it('returns a partial knex static connection config with database name', () => {
const testDatabaseName = 'testdatabase';
expect(defaultNameOverride(testDatabaseName)).toHaveProperty(
'connection.database',
testDatabaseName,
);
});
});
@@ -0,0 +1,34 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Knex } from 'knex';
/**
* Provides a partial knex config with database name override.
*
* Default override for knex database drivers which accept ConnectionConfig
* with `connection.database` as the database name field.
*
* @param name database name to get config override for
*/
export default function defaultNameOverride(
name: string,
): Partial<Knex.Config> {
return {
connection: {
database: name,
},
};
}
@@ -0,0 +1,18 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './mysql';
export * from './postgres';
export * from './sqlite3';
@@ -14,12 +14,14 @@
* limitations under the License.
*/
import knexFactory, { Knex } from 'knex';
import yn from 'yn';
import { Config } from '@backstage/config';
import { InputError } from '@backstage/errors';
import knexFactory, { Knex } from 'knex';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './types';
import yn from 'yn';
import { mergeDatabaseConfig } from '../config';
import { DatabaseConnector } from '../types';
import defaultNameOverride from './defaultNameOverride';
/**
* Creates a knex mysql database connection
@@ -161,22 +163,14 @@ export async function ensureMysqlDatabaseExists(
}
}
export function createMysqlNameOverride(name: string): Partial<Knex.Config> {
return {
connection: {
database: name,
},
};
}
/**
* MySql database connector.
* MySQL database connector.
*
* Exposes database connector functionality via an immutable object.
*/
export const mysqlConnector: DatabaseConnector = Object.freeze({
createClient: createMysqlDatabaseClient,
ensureDatabaseExists: ensureMysqlDatabaseExists,
createNameOverride: createMysqlNameOverride,
createNameOverride: defaultNameOverride,
parseConnectionString: parseMysqlConnectionString,
});
@@ -15,9 +15,11 @@
*/
import knexFactory, { Knex } from 'knex';
import { Config } from '@backstage/config';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './types';
import { mergeDatabaseConfig } from '../config';
import { DatabaseConnector } from '../types';
import defaultNameOverride from './defaultNameOverride';
/**
* Creates a knex postgres database connection
@@ -133,14 +135,6 @@ export async function ensurePgDatabaseExists(
}
}
export function createPgNameOverride(name: string): Partial<Knex.Config> {
return {
connection: {
database: name,
},
};
}
/**
* PostgreSQL database connector.
*
@@ -149,6 +143,6 @@ export function createPgNameOverride(name: string): Partial<Knex.Config> {
export const pgConnector: DatabaseConnector = Object.freeze({
createClient: createPgDatabaseClient,
ensureDatabaseExists: ensurePgDatabaseExists,
createNameOverride: createPgNameOverride,
createNameOverride: defaultNameOverride,
parseConnectionString: parsePgConnectionString,
});
@@ -13,16 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import path from 'path';
import { Config } from '@backstage/config';
import { ensureDirSync } from 'fs-extra';
import knexFactory, { Knex } from 'knex';
import path from 'path';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './types';
import { Config } from '@backstage/config';
import { mergeDatabaseConfig } from '../config';
import { DatabaseConnector } from '../types';
/**
* Creates a knex sqlite3 database connection
* Creates a knex SQLite3 database connection
*
* @param dbConfig The database config
* @param overrides Additional options to merge with the config
@@ -55,7 +56,7 @@ export function createSqliteDatabaseClient(
}
/**
* Builds a knex sqlite3 connection config
* Builds a knex SQLite3 connection config
*
* @param dbConfig The database config
* @param overrides Additional options to merge with the config
@@ -101,12 +102,18 @@ export function buildSqliteDatabaseConfig(
return config;
}
/**
* Provides a partial knex SQLite3 config to override database name.
*/
export function createSqliteNameOverride(name: string): Partial<Knex.Config> {
return {
connection: parseSqliteConnectionString(name),
};
}
/**
* Produces a partial knex SQLite3 connection config with database name.
*/
export function parseSqliteConnectionString(
name: string,
): Knex.Sqlite3ConnectionConfig {
@@ -116,7 +123,7 @@ export function parseSqliteConnectionString(
}
/**
* Sqlite3 database connector.
* SQLite3 database connector.
*
* Exposes database connector functionality via an immutable object.
*/