refactoring: added ConnectionArray

This commit is contained in:
Umed Khudoiberdiev 2016-04-15 16:09:43 +05:00
parent 8dd12834fb
commit 40ef579d17
3 changed files with 70 additions and 28 deletions

View File

@ -10,10 +10,7 @@ import {ConstructorFunction} from "../common/ConstructorFunction";
import {EntityListenerMetadata} from "../metadata-builder/metadata/EntityListenerMetadata";
import {EntityManager} from "../repository/EntityManager";
interface RepositoryAndMetadata {
repository: Repository<any>;
metadata: EntityMetadata;
}
type RepositoryAndMetadata = { repository: Repository<any>, metadata: EntityMetadata };
/**
* A single connection instance to the database. Each connection has its own repositories, subscribers and metadatas.
@ -153,23 +150,26 @@ export class Connection {
/**
* Registers entity metadatas for the current connection.
*/
addEntityMetadatas(metadatas: EntityMetadata[]) {
addEntityMetadatas(metadatas: EntityMetadata[]): Connection {
this._entityMetadatas = this._entityMetadatas.concat(metadatas);
this.repositoryAndMetadatas = this.repositoryAndMetadatas.concat(metadatas.map(metadata => this.createRepoMeta(metadata)));
return this;
}
/**
* Registers entity listener metadatas for the current connection.
*/
addEntityListenerMetadatas(metadatas: EntityListenerMetadata[]) {
addEntityListenerMetadatas(metadatas: EntityListenerMetadata[]): Connection {
this._entityListenerMetadatas = this._entityListenerMetadatas.concat(metadatas);
return this;
}
/**
* Registers subscribers for the current connection.
*/
addSubscribers(subscribers: OrmSubscriber<any>[]) {
addSubscribers(subscribers: OrmSubscriber<any>[]): Connection {
this._subscribers = this._subscribers.concat(subscribers);
return this;
}
// -------------------------------------------------------------------------

View File

@ -0,0 +1,48 @@
import {Connection} from "./Connection";
import {Driver} from "../driver/Driver";
import {ConnectionOptions} from "./ConnectionOptions";
/**
* Array for the connections.
*/
export class ConnectionArray extends Array<Connection> {
// -------------------------------------------------------------------------
// Public Methods
// -------------------------------------------------------------------------
/**
* Creates a new connection and pushes a connection to the array.
*/
createAndPush(name: string, driver: Driver, options: ConnectionOptions) {
this.removeByName(name);
const connection = new Connection(name, <Driver> driver, options);
this.push(connection);
return connection;
}
/**
* Gets connection with a given name.
*/
getWithName(name: string) {
return this.find(connection => connection.name === name);
}
/**
* Checks if connection with a given name exist.
*/
hasWithName(name: string) {
return !!this.getWithName(name);
}
// -------------------------------------------------------------------------
// Private Methods
// -------------------------------------------------------------------------
private removeByName(name: string) {
const existConnection = this.find(connection => connection.name === name);
if (existConnection)
this.splice(this.indexOf(existConnection), 1);
}
}

View File

@ -8,6 +8,7 @@ import {importClassesFromDirectories} from "../util/DirectoryExportedClassesLoad
import {ConnectionOptions} from "tls";
import {NamingStrategy} from "../naming-strategy/NamingStrategy";
import {CannotSetNamingStrategyError} from "./error/CannotSetNamingStrategyError";
import {ConnectionArray} from "./ConnectionArray";
/**
* Connection manager holds all connections made to the databases and providers helper management functions
@ -19,7 +20,7 @@ export class ConnectionManager {
// Properties
// -------------------------------------------------------------------------
private connections: Connection[] = [];
private connections = new ConnectionArray();
private _entityMetadataBuilder: EntityMetadataBuilder;
private _namingStrategy: NamingStrategy;
private _container: { get(someClass: any): any };
@ -57,21 +58,14 @@ export class ConnectionManager {
*/
createConnection(driver: Driver, options: ConnectionOptions): Connection;
createConnection(name: string, driver: Driver, options: ConnectionOptions): Connection;
createConnection(name: string|Driver, driver?: Driver|ConnectionOptions, options?: ConnectionOptions): Connection {
if (typeof name === "object") {
driver = <Driver> name;
createConnection(nameOrDriver: string|Driver, driver?: Driver|ConnectionOptions, options?: ConnectionOptions): Connection {
if (typeof nameOrDriver === "object") {
driver = <Driver> nameOrDriver;
options = <ConnectionOptions> driver;
}
if (!name) {
name = "default";
}
const existConnection = this.connections.find(connection => connection.name === name);
if (existConnection)
this.connections.splice(this.connections.indexOf(existConnection), 1);
const connection = new Connection(<string> name, <Driver> driver, options);
this.connections.push(connection);
return connection;
const name = typeof nameOrDriver === "string" ? <string> nameOrDriver : "default";
return this.connections.createAndPush(name, <Driver> driver, options);
}
/**
@ -79,13 +73,12 @@ export class ConnectionManager {
* connection.
*/
getConnection(name: string = "default"): Connection {
if (!name)
name = "default";
const foundConnection = this.connections.find(connection => connection.name === name);
if (!foundConnection)
if (!name) name = "default";
if (!this.connections.hasWithName(name))
throw new ConnectionNotFoundError(name);
return foundConnection;
return this.connections.getWithName(name);
}
/**
@ -137,8 +130,9 @@ export class ConnectionManager {
const entityMetadatas = this.entityMetadataBuilder.build(entities);
const entityListenerMetadatas = defaultMetadataStorage.findEntityListenersForClasses(entities);
this.getConnection(connectionName).addEntityMetadatas(entityMetadatas);
this.getConnection(connectionName).addEntityListenerMetadatas(entityListenerMetadatas);
this.getConnection(connectionName)
.addEntityMetadatas(entityMetadatas)
.addEntityListenerMetadatas(entityListenerMetadatas);
}
/**