-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core-database): clearly separate manager and factory behavio…
…ur (#2346)
- Loading branch information
1 parent
a2cec00
commit ee0398b
Showing
4 changed files
with
23 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}; |