refactor: move database connector interface

Signed-off-by: Minn Soe <contributions@minn.io>
This commit is contained in:
Minn Soe
2021-06-06 16:56:38 +01:00
parent dc75c78cd7
commit 214efffe3a
6 changed files with 39 additions and 34 deletions
@@ -18,7 +18,7 @@ import { Config, JsonObject } from '@backstage/config';
import { InputError } from '@backstage/errors';
import knexFactory, { Knex } from 'knex';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './connector';
import { DatabaseConnector } from './types';
import { mysqlConnector } from './mysql';
import { pgConnector } from './postgres';
@@ -1,30 +0,0 @@
/*
* 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 { Config } from '@backstage/config';
import { Knex } from 'knex';
export interface DatabaseConnector {
createClient(dbConfig: Config, overrides?: Partial<Knex.Config>): Knex;
createNameOverride(name: string): Partial<Knex.Config>;
parseConnectionString(
connectionString: string,
client?: string,
): Knex.StaticConnectionConfig;
ensureDatabaseExists?(
dbConfig: Config,
...databases: Array<string>
): Promise<void>;
}
@@ -18,7 +18,7 @@ import { Config } from '@backstage/config';
import { InputError } from '@backstage/errors';
import knexFactory, { Knex } from 'knex';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './connector';
import { DatabaseConnector } from './types';
import yn from 'yn';
/**
@@ -17,7 +17,7 @@
import knexFactory, { Knex } from 'knex';
import { Config } from '@backstage/config';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './connector';
import { DatabaseConnector } from './types';
/**
* Creates a knex postgres database connection
@@ -19,7 +19,7 @@ import { ensureDirSync } from 'fs-extra';
import knexFactory, { Knex } from 'knex';
import path from 'path';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './connector';
import { DatabaseConnector } from './types';
/**
* Creates a knex sqlite3 database connection
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { Config } from '@backstage/config';
import { Knex } from 'knex';
/**
@@ -28,3 +29,37 @@ export interface PluginDatabaseManager {
*/
getClient(): Promise<Knex>;
}
/**
* DatabaseConnector manages an underlying Knex database driver.
*/
export interface DatabaseConnector {
/**
* createClient provides an instance of a knex database connector.
*/
createClient(dbConfig: Config, overrides?: Partial<Knex.Config>): Knex;
/**
* createNameOverride provides a partial knex config sufficient to override a
* database name.
*/
createNameOverride(name: string): Partial<Knex.Config>;
/**
* parseConnectionString produces a knex connection config object representing
* a database connection string.
*/
parseConnectionString(
connectionString: string,
client?: string,
): Knex.StaticConnectionConfig;
/**
* ensureDatabaseExists performs a side-effect to ensure database names passed in are
* present.
*
* Calling this function on databases which already exist should do nothing.
* Missing databases should be created if needed.
*/
ensureDatabaseExists?(
dbConfig: Config,
...databases: Array<string>
): Promise<void>;
}