From ee0398bcf6d365381b8e8d8314b3ac0a5233cbef Mon Sep 17 00:00:00 2001 From: Brian Faust Date: Sun, 31 Mar 2019 17:41:15 +0300 Subject: [PATCH] refactor(core-database): clearly separate manager and factory behaviour (#2346) --- packages/core-database-postgres/src/plugin.ts | 7 ++-- packages/core-database/src/factory.ts | 7 ++++ packages/core-database/src/manager.ts | 35 ++++++------------- packages/core-database/src/plugin.ts | 4 +-- 4 files changed, 23 insertions(+), 30 deletions(-) create mode 100644 packages/core-database/src/factory.ts diff --git a/packages/core-database-postgres/src/plugin.ts b/packages/core-database-postgres/src/plugin.ts index 7c52c4dca4..f455005c30 100644 --- a/packages/core-database-postgres/src/plugin.ts +++ b/packages/core-database-postgres/src/plugin.ts @@ -1,4 +1,4 @@ -import { DatabaseManager, databaseServiceFactory, WalletManager } from "@arkecosystem/core-database"; +import { ConnectionManager, databaseServiceFactory, WalletManager } from "@arkecosystem/core-database"; import { Container, Database, Logger } from "@arkecosystem/core-interfaces"; import { defaults } from "./defaults"; import { PostgresConnection } from "./postgres-connection"; @@ -13,9 +13,8 @@ export const plugin: Container.PluginDescriptor = { const walletManager = new WalletManager(); - const databaseManager = container.resolvePlugin("database-manager"); - - const connection = await databaseManager.makeConnection(new PostgresConnection(options, walletManager)); + const connectionManager = container.resolvePlugin("database-manager"); + const connection = await connectionManager.createConnection(new PostgresConnection(options, walletManager)); return databaseServiceFactory(options, walletManager, connection); }, diff --git a/packages/core-database/src/factory.ts b/packages/core-database/src/factory.ts new file mode 100644 index 0000000000..d1702b55e7 --- /dev/null +++ b/packages/core-database/src/factory.ts @@ -0,0 +1,7 @@ +import { Database } from "@arkecosystem/core-interfaces"; + +export class ConnectionFactory { + public async make(connection: Database.IDatabaseConnection): Promise { + return connection.make(); + } +} diff --git a/packages/core-database/src/manager.ts b/packages/core-database/src/manager.ts index 5975b58e71..b418dfd077 100644 --- a/packages/core-database/src/manager.ts +++ b/packages/core-database/src/manager.ts @@ -1,36 +1,23 @@ import { Database } from "@arkecosystem/core-interfaces"; +import { ConnectionFactory } from "./factory"; -export class DatabaseManager { - public connections: { [key: string]: Database.IDatabaseConnection }; +export class ConnectionManager { + private readonly factory: ConnectionFactory = new ConnectionFactory(); + private readonly connections: Map = new Map< + string, + Database.IDatabaseConnection + >(); - /** - * Create a new database manager instance. - * @constructor - */ - constructor() { - this.connections = {}; - } - - /** - * Get a database connection instance. - * @param {String} name - * @return {DatabaseConnection} - */ public connection(name = "default"): Database.IDatabaseConnection { - return this.connections[name]; + return this.connections.get(name); } - /** - * Make the database connection instance. - * @param {DatabaseConnection} connection - * @param {String} name - * @return {void} - */ - public async makeConnection( + public async createConnection( connection: Database.IDatabaseConnection, name = "default", ): Promise { - this.connections[name] = await connection.make(); + this.connections.set(name, await this.factory.make(connection)); + return this.connection(name); } } diff --git a/packages/core-database/src/plugin.ts b/packages/core-database/src/plugin.ts index 2a5ab78f83..92404eb258 100644 --- a/packages/core-database/src/plugin.ts +++ b/packages/core-database/src/plugin.ts @@ -1,5 +1,5 @@ import { Container, Logger } from "@arkecosystem/core-interfaces"; -import { DatabaseManager } from "./manager"; +import { ConnectionManager } from "./manager"; export const plugin: Container.PluginDescriptor = { pkg: require("../package.json"), @@ -7,6 +7,6 @@ export const plugin: Container.PluginDescriptor = { async register(container: Container.IContainer, options) { container.resolvePlugin("logger").info("Starting Database Manager"); - return new DatabaseManager(); + return new ConnectionManager(); }, };