refactored connection related code

This commit is contained in:
Umed Khudoiberdiev 2016-03-13 11:05:43 +05:00
parent b9a6732d43
commit 3eda744d6c
10 changed files with 66 additions and 31 deletions

View File

@ -7,7 +7,7 @@
"readmeFilename": "README.md",
"author": {
"name": "Umed Khudoiberdiev",
"email": "zarrhost@gmail.com"
"email": "pleerock.me@gmail.com"
},
"repository": {
"type": "git",

View File

@ -7,6 +7,7 @@ import {RepositoryNotFoundError} from "./error/RepositoryNotFoundError";
import {BroadcasterNotFoundError} from "./error/BroadcasterNotFoundError";
import {EntityMetadata} from "../metadata-builder/metadata/EntityMetadata";
import {SchemaCreator} from "../schema-creator/SchemaCreator";
import {MetadataNotFoundError} from "./error/MetadataNotFoundError";
/**
* A single connection instance to the database. Each connection has its own repositories, subscribers and metadatas.
@ -88,7 +89,8 @@ export class Connection {
*/
connect(options: ConnectionOptions): Promise<void> {
const schemaCreator = new SchemaCreator(this);
return this._driver.connect(options)
return this._driver
.connect(options)
.then(() => {
if (options.autoSchemaCreate === true)
return schemaCreator.create();
@ -103,7 +105,7 @@ export class Connection {
}
/**
* Adds a new entity metadatas.
* Registers entity metadatas for the current connection.
*/
addMetadatas(metadatas: EntityMetadata[]) {
this._metadatas = this._metadatas.concat(metadatas);
@ -112,7 +114,7 @@ export class Connection {
}
/**
* Adds subscribers to this connection.
* Registers subscribers for the current connection.
*/
addSubscribers(subscribers: OrmSubscriber<any>[]) {
this._subscribers = this._subscribers.concat(subscribers);
@ -131,13 +133,12 @@ export class Connection {
}
/**
* Gets the metadata for the given entity class.
* Gets the entity metadata for the given entity class.
*/
getMetadata(entityClass: Function): EntityMetadata {
const metadata = this.metadatas.find(metadata => metadata.target === entityClass);
// todo:
// if (!metadata)
// throw new MetadataNotFoundError(entityClass);
if (!metadata)
throw new MetadataNotFoundError(entityClass);
return metadata;
}
@ -146,7 +147,7 @@ export class Connection {
* Gets the broadcaster for the given entity class.
*/
getBroadcaster<Entity>(entityClass: Function): OrmBroadcaster<Entity> {
let metadata = this.broadcasters.find(broadcaster => broadcaster.entityClass === entityClass);
const metadata = this.broadcasters.find(broadcaster => broadcaster.entityClass === entityClass);
if (!metadata)
throw new BroadcasterNotFoundError(entityClass);

View File

@ -25,11 +25,8 @@ export class ConnectionManager {
// Constructor
// -------------------------------------------------------------------------
constructor(entityMetadataBuilder?: EntityMetadataBuilder) {
if (!entityMetadataBuilder)
entityMetadataBuilder = new EntityMetadataBuilder(defaultMetadataStorage, new DefaultNamingStrategy());
this.entityMetadataBuilder = entityMetadataBuilder;
constructor() {
this.entityMetadataBuilder = new EntityMetadataBuilder(defaultMetadataStorage, new DefaultNamingStrategy());
}
// -------------------------------------------------------------------------
@ -37,8 +34,7 @@ export class ConnectionManager {
// -------------------------------------------------------------------------
/**
* Sets a container that can be used in your custom subscribers. This allows you to inject services in your
* classes.
* Sets a container that can be used in custom user subscribers. This allows to inject services in user classes.
*/
set container(container: { get(someClass: any): any }) {
this._container = container;
@ -62,7 +58,8 @@ export class ConnectionManager {
}
/**
* Gets the specific connection.
* Gets registered connection with the given name. If connection name is not given then it will get a default
* connection.
*/
getConnection(name: string = "default"): Connection {
const foundConnection = this.connections.find(connection => connection.name === name);
@ -73,7 +70,7 @@ export class ConnectionManager {
}
/**
* Imports entities to the given connection.
* Imports entities for the given connection. If connection name is not given then default connection is used.
*/
importEntities(entities: Function[]): void;
importEntities(connectionName: string, entities: Function[]): void;
@ -85,13 +82,14 @@ export class ConnectionManager {
entities = <Function[]> connectionNameOrEntities;
}
let metadatas = this.entityMetadataBuilder.build(entities);
const metadatas = this.entityMetadataBuilder.build(entities);
if (metadatas.length > 0)
this.getConnection(connectionName).addMetadatas(metadatas);
}
/**
* Imports entities from the given paths.
* Imports entities from the given paths (directories) for the given connection. If connection name is not given
* then default connection is used.
*/
importEntitiesFromDirectories(paths: string[]): void;
importEntitiesFromDirectories(connectionName: string, paths: string[]): void;
@ -103,8 +101,8 @@ export class ConnectionManager {
paths = <string[]> connectionNameOrPaths;
}
let entitiesInFiles = OrmUtils.requireAll(paths);
let allEntities = entitiesInFiles.reduce((allEntities, entities) => {
const entitiesInFiles = OrmUtils.requireAll(paths);
const allEntities = entitiesInFiles.reduce((allEntities, entities) => {
return allEntities.concat(Object.keys(entities).map(key => entities[key]));
}, []);
@ -112,7 +110,8 @@ export class ConnectionManager {
}
/**
* Imports subscribers from the given paths.
* Imports subscribers from the given paths (directories) for the given connection. If connection name is not given
* then default connection is used.
*/
importSubscribersFromDirectories(paths: string[]): void;
importSubscribersFromDirectories(connectionName: string, paths: string[]): void;
@ -131,9 +130,10 @@ export class ConnectionManager {
.ormEventSubscriberMetadatas
.filter(metadata => allSubscriberClasses.indexOf(metadata.constructor) !== -1)
.map(metadata => {
let constructor: any = metadata.constructor;
const constructor: any = metadata.constructor;
return this._container ? this._container.get(constructor) : new constructor();
});
if (subscribers.length > 0)
this.getConnection(connectionName).addSubscribers(subscribers);
}

View File

@ -7,11 +7,35 @@ export interface ConnectionOptions {
* Url to where perform connection.
*/
url?: string;
/**
* Database host.
*/
host?: string;
/**
* Database host port.
*/
port?: number;
/**
* Database username.
*/
username?: string;
/**
* Database password.
*/
password?: string;
/**
* Database name to connect to.
*/
database?: string;
/**
* Indicates if database schema should be auto created every time application launch.
*/
autoSchemaCreate?: boolean;
}

View File

@ -1,9 +1,10 @@
export class BroadcasterNotFoundError extends Error {
name = "BroadcasterNotFoundError";
constructor(documentClassOrName: string|Function) {
constructor(entityClassOrName: string|Function) {
super();
this.message = `No broadcaster for ${documentClassOrName} has been found!`;
const name = entityClassOrName instanceof Function ? (<any> entityClassOrName).name : entityClassOrName;
this.message = `No broadcaster for "${name}" was found. Looks like this entity is not registered in your connection?`;
}
}

View File

@ -3,7 +3,7 @@ export class ConnectionNotFoundError extends Error {
constructor(name: string) {
super();
this.message = `No connection ${name} found.`;
this.message = `Connection "${name}" was not found.`;
}
}

View File

@ -0,0 +1,9 @@
export class MetadataNotFoundError extends Error {
name = "MetadataNotFoundError";
constructor(entityClass: Function) {
super();
this.message = `No metadata for ${entityClass} has been found!`;
}
}

View File

@ -3,7 +3,7 @@ export class RepositoryNotFoundError extends Error {
constructor(entityClass: Function) {
super();
this.message = `No repository for ${entityClass} has been found!`;
this.message = `No repository for "${entityClass}" was found. Looks like this entity is not registered in your connection?`;
}
}

View File

@ -1,9 +1,10 @@
export class SchemaNotFoundError extends Error {
name = "SchemaNotFoundError";
constructor(documentClassOrName: string|Function) {
constructor(entityClassOrName: string|Function) {
super();
this.message = `No schema for ${documentClassOrName} has been found!`;
const name = entityClassOrName instanceof Function ? (<any> entityClassOrName).name : entityClassOrName;
this.message = `No schema for "${name}" was found. Looks like this entity is not registered in your connection?`;
}
}

View File

@ -12,7 +12,6 @@
"no-duplicate-variable": true,
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-var-keyword": true,
"one-line": [
true,