Skip to content

Commit

Permalink
refactor(core-database): clearly separate manager and factory behavio…
Browse files Browse the repository at this point in the history
…ur (#2346)
  • Loading branch information
faustbrian authored Mar 31, 2019
1 parent a2cec00 commit ee0398b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 30 deletions.
7 changes: 3 additions & 4 deletions packages/core-database-postgres/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -13,9 +13,8 @@ export const plugin: Container.PluginDescriptor = {

const walletManager = new WalletManager();

const databaseManager = container.resolvePlugin<DatabaseManager>("database-manager");

const connection = await databaseManager.makeConnection(new PostgresConnection(options, walletManager));
const connectionManager = container.resolvePlugin<ConnectionManager>("database-manager");
const connection = await connectionManager.createConnection(new PostgresConnection(options, walletManager));

return databaseServiceFactory(options, walletManager, connection);
},
Expand Down
7 changes: 7 additions & 0 deletions packages/core-database/src/factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Database } from "@arkecosystem/core-interfaces";

export class ConnectionFactory {
public async make(connection: Database.IDatabaseConnection): Promise<Database.IDatabaseConnection> {
return connection.make();
}
}
35 changes: 11 additions & 24 deletions packages/core-database/src/manager.ts
Original file line number Diff line number Diff line change
@@ -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<string, Database.IDatabaseConnection> = 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<Database.IDatabaseConnection> {
this.connections[name] = await connection.make();
this.connections.set(name, await this.factory.make(connection));

return this.connection(name);
}
}
4 changes: 2 additions & 2 deletions packages/core-database/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Container, Logger } from "@arkecosystem/core-interfaces";
import { DatabaseManager } from "./manager";
import { ConnectionManager } from "./manager";

export const plugin: Container.PluginDescriptor = {
pkg: require("../package.json"),
alias: "database-manager",
async register(container: Container.IContainer, options) {
container.resolvePlugin<Logger.ILogger>("logger").info("Starting Database Manager");

return new DatabaseManager();
return new ConnectionManager();
},
};

0 comments on commit ee0398b

Please sign in to comment.