Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core-database): clearly separate manager and factory behaviour #2346

Merged
merged 1 commit into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
},
};