refactored connection and connection manager classes

This commit is contained in:
Umed Khudoiberdiev 2016-08-05 20:24:02 +05:00
parent 713037f8b4
commit 2caa6b480f
50 changed files with 285 additions and 216 deletions

View File

@ -9,9 +9,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: [Post]
};
/*const options: CreateConnectionOptions = {

View File

@ -13,9 +13,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"]
};

View File

@ -10,11 +10,11 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true
}
},
autoSchemaCreate: true,
entities: [EverythingEntity]
};

View File

@ -10,9 +10,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
usedNamingStrategy: "custom_strategy",
entities: [Post],
namingStrategies: [CustomNamingStrategy]

View File

@ -13,12 +13,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"]
};

View File

@ -10,9 +10,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: [Post, PostAuthor]
};

View File

@ -9,9 +9,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: [Post]
};

View File

@ -9,9 +9,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: [Post]
};

View File

@ -10,12 +10,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post]
};

View File

@ -12,12 +12,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Author, Category]
};

View File

@ -13,12 +13,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Author, Category, PostMetadata]
};

View File

@ -16,11 +16,11 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logQueries: true
}
},
autoSchemaCreate: true,
entities: [Post, PostDetails, PostCategory, PostMetadata, PostImage, PostInformation, PostAuthor]
};

View File

@ -12,12 +12,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Author, Category]
};

View File

@ -12,12 +12,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Author, Category]
};

View File

@ -10,12 +10,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Category]
};

View File

@ -12,12 +12,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Author, Category]
};

View File

@ -11,9 +11,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
// entitySchemaDirectories: [__dirname + "/schemas"],
entitySchemas: [
require(__dirname + "/../../../../sample/sample24-schemas/schemas/post.json"),

View File

@ -11,12 +11,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Author]
};

View File

@ -12,12 +12,12 @@ const options: CreateConnectionOptions = {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Question, Counters]
};

View File

@ -15,9 +15,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: [Post, PostDetails, PostCategory, PostMetadata, PostImage, PostInformation, PostAuthor]
};

View File

@ -10,9 +10,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"]
};

View File

@ -13,9 +13,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: [Post, PostAuthor, PostCategory],
subscribers: [EverythingSubscriber]
};

View File

@ -12,9 +12,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"]
};

View File

@ -11,9 +11,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"]
};

View File

@ -9,9 +9,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"]
};

View File

@ -11,9 +11,9 @@ const options: CreateConnectionOptions = {
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entityDirectories: [__dirname + "/entity/*"],
subscriberDirectories: [__dirname + "/subscriber/*"]
};

View File

@ -1,6 +1,6 @@
/**
* Used to get a type of the creating Function.
*/
export interface ConstructorFunction<T> {
export interface ObjectType<T> {
new (): T;
}

View File

@ -2,7 +2,7 @@ import {Connection} from "../connection/Connection";
import {ConnectionNotFoundError} from "./error/ConnectionNotFoundError";
import {MysqlDriver} from "../driver/MysqlDriver";
import {CreateConnectionOptions} from "./CreateConnectionOptions";
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "../driver/ConnectionOptions";
import {Driver} from "../driver/Driver";
import {MissingDriverError} from "./error/MissingDriverError";
import {PostgresDriver} from "../driver/PostgresDriver";
@ -63,6 +63,26 @@ export class ConnectionManager {
return connection;
}
/**
* Creates a new connection based on the given connection options and registers a new connection in the manager.
*/
async createAndConnect(options: CreateConnectionOptions): Promise<Connection> {
const connection = this.create(options);
// connect to the database
await connection.connect();
// if option is set - drop schema once connection is done
if (options.dropSchemaOnConnection)
await connection.dropDatabase();
// if option is set - automatically synchronize a schema
if (options.autoSchemaCreate)
await connection.syncSchema();
return connection;
}
/**
* Creates a new connection and pushes a connection to the array.
*/

View File

@ -1,4 +1,4 @@
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "../driver/ConnectionOptions";
import {EntitySchema} from "../metadata/entity-schema/EntitySchema";
/**
@ -26,6 +26,18 @@ export interface CreateConnectionOptions {
*/
usedNamingStrategy?: string|Function;
/**
* Drops the schema each time connection is being established.
* Be careful with this option and don't use this in production - otherwise you'll loose all your production data.
* This option is useful during debug and development.
*/
dropSchemaOnConnection?: boolean;
/**
* Indicates if database schema should be auto created every time application launch.
*/
autoSchemaCreate?: boolean;
/**
* Entities to be loaded for the new connection.
*/

View File

@ -1,9 +1,8 @@
import {Driver} from "../driver/Driver";
import {ConnectionOptions} from "./ConnectionOptions";
import {Repository} from "../repository/Repository";
import {EntitySubscriberInterface} from "../subscriber/EntitySubscriberInterface";
import {RepositoryNotFoundError} from "./error/RepositoryNotFoundError";
import {ConstructorFunction} from "../common/ConstructorFunction";
import {ObjectType} from "../common/ObjectType";
import {EntityListenerMetadata} from "../metadata/EntityListenerMetadata";
import {EntityManager} from "../entity-manager/EntityManager";
import {importClassesFromDirectories, importJsonsFromDirectories} from "../util/DirectoryExportedClassesLoader";
@ -80,6 +79,11 @@ export class Connection {
*/
private readonly _reactiveEntityManager: ReactiveEntityManager;
/**
* Stores all registered metadatas with their repositories.
*/
private readonly repositoryForMetadatas: RepositoryForMetadata[] = [];
/**
* Entity listeners that are registered for this connection.
*/
@ -120,11 +124,6 @@ export class Connection {
*/
private _isConnected = false;
/**
* Stores all registered metadatas with their repositories.
*/
private readonly repositoryForMetadatas: RepositoryForMetadata[] = [];
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
@ -149,7 +148,7 @@ export class Connection {
}
/**
* Entity manager allows to work with any entity of this connection.
* Gets entity manager that allows to perform repository operations with any entity of this connection.
*/
get entityManager() {
if (!this.isConnected)
@ -159,7 +158,7 @@ export class Connection {
}
/**
* Entity manager allows to work with any entity of your connection.
* Gets entity manager that allows to perform repository operations with any entity of this connection.
* This version of entity manager is reactive - works with Observables instead of Promises.
*/
get reactiveEntityManager() {
@ -188,20 +187,14 @@ export class Connection {
// set connected status for the current connection
this._isConnected = true;
// drop the schema if special option is set
if (this.driver.connectionOptions.dropSchemaOnConnection)
await this.driver.clearDatabase();
// second build schema
if (this.driver.connectionOptions.autoSchemaCreate === true)
await this.syncSchema();
return this;
}
/**
* Closes this connection.
* Closes connection with the database.
* Once connection is closed, you cannot use repositories and perform any operations except
* opening connection again.
*/
async close(): Promise<void> {
if (!this.isConnected)
@ -211,15 +204,22 @@ export class Connection {
this._isConnected = false;
}
/**
* Drops the database and all its data.
*/
async dropDatabase(): Promise<void> {
return this.driver.clearDatabase();
}
/**
* Creates database schema for all entities registered in this connection.
*/
async syncSchema(dropBeforeSync: boolean = false): Promise<void> {
if (!this.isConnected)
throw new CannotSyncNotConnectedError(this.name);
if (dropBeforeSync)
await this.driver.clearDatabase();
await this.dropDatabase();
const schemaBuilder = this.driver.createSchemaBuilder();
const schemaCreatorFactory = getFromContainer(SchemaCreatorFactory);
@ -228,7 +228,7 @@ export class Connection {
}
/**
* Imports entities from the given paths (directories) for the current connection.
* Imports entities from the given paths (directories) and registers them in the current connection.
*/
async importEntitiesFromDirectories(paths: string[]): Promise<this> {
this.importEntities(importClassesFromDirectories(paths));
@ -236,7 +236,7 @@ export class Connection {
}
/**
* Imports entity schemas from the given paths (directories) for the current connection.
* Imports entity schemas from the given paths (directories) and registers them in the current connection.
*/
async importEntitySchemaFromDirectories(paths: string[]): Promise<this> {
this.importEntitySchemas(importJsonsFromDirectories(paths));
@ -244,7 +244,7 @@ export class Connection {
}
/**
* Imports subscribers from the given paths (directories) for the current connection.
* Imports subscribers from the given paths (directories) and registers them in the current connection.
*/
async importSubscribersFromDirectories(paths: string[]): Promise<this> {
this.importSubscribers(importClassesFromDirectories(paths));
@ -252,7 +252,7 @@ export class Connection {
}
/**
* Imports naming strategies from the given paths (directories) for the current connection.
* Imports naming strategies from the given paths (directories) and registers them in the current connection.
*/
async importNamingStrategiesFromDirectories(paths: string[]): Promise<this> {
this.importEntities(importClassesFromDirectories(paths));
@ -260,7 +260,7 @@ export class Connection {
}
/**
* Imports entities for the current connection.
* Imports entities and registers them in the current connection.
*/
async importEntities(entities: Function[]): Promise<this> {
if (this.isConnected)
@ -271,7 +271,7 @@ export class Connection {
}
/**
* Imports schemas for the current connection.
* Imports schemas and registers them in the current connection.
*/
async importEntitySchemas(schemas: EntitySchema[]): Promise<this> {
if (this.isConnected)
@ -282,7 +282,7 @@ export class Connection {
}
/**
* Imports entities for the given connection. If connection name is not given then default connection is used.
* Imports subscribers and registers them in the current connection.
*/
async importSubscribers(subscriberClasses: Function[]): Promise<this> {
if (this.isConnected)
@ -293,7 +293,7 @@ export class Connection {
}
/**
* Imports entities for the current connection.
* Imports naming strategies and registers them in the current connection.
*/
async importNamingStrategies(strategies: Function[]): Promise<this> {
if (this.isConnected)
@ -304,17 +304,20 @@ export class Connection {
}
/**
* Sets given naming strategy to be used. Naming strategy must be set to be used before connection is established.
* Sets given naming strategy to be used.
* Naming strategy must be set to be used before connection is established.
*/
useNamingStrategy(name: string): this;
/**
* Sets given naming strategy to be used. Naming strategy must be set to be used before connection is established.
* Sets given naming strategy to be used.
* Naming strategy must be set to be used before connection is established.
*/
useNamingStrategy(strategy: Function): this;
/**
* Sets given naming strategy to be used. Naming strategy must be set to be used before connection is established.
* Sets given naming strategy to be used.
* Naming strategy must be set to be used before connection is established.
*/
useNamingStrategy(strategyClassOrName: string|Function): this {
if (this.isConnected)
@ -325,7 +328,17 @@ export class Connection {
}
/**
* Gets the entity metadata of the given entity target.
* Gets the entity metadata of the given entity class.
*/
getMetadata(entity: Function): EntityMetadata;
/**
* Gets the entity metadata of the given entity name.
*/
getMetadata(entity: string): EntityMetadata;
/**
Gets entity metadata for the given entity class or schema name.
*/
getMetadata(entity: Function|string): EntityMetadata {
return this.entityMetadatas.findByTarget(entity);
@ -334,7 +347,7 @@ export class Connection {
/**
* Gets repository for the given entity class.
*/
getRepository<Entity>(entityClass: ConstructorFunction<Entity>): Repository<Entity>;
getRepository<Entity>(entityClass: ObjectType<Entity>): Repository<Entity>;
/**
* Gets repository for the given entity name.
@ -344,7 +357,7 @@ export class Connection {
/**
* Gets repository for the given entity class or name.
*/
getRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): Repository<Entity> {
getRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): Repository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
@ -361,18 +374,24 @@ export class Connection {
/**
* Gets tree repository for the given entity class.
* Only tree-type entities can have a TreeRepository,
* like ones decorated with @ClosureTable decorator.
*/
getTreeRepository<Entity>(entityClass: ConstructorFunction<Entity>): TreeRepository<Entity>;
getTreeRepository<Entity>(entityClass: ObjectType<Entity>): TreeRepository<Entity>;
/**
* Gets tree repository for the given entity name.
* Gets tree repository for the given entity class.
* Only tree-type entities can have a TreeRepository,
* like ones decorated with @ClosureTable decorator.
*/
getTreeRepository<Entity>(entityName: string): TreeRepository<Entity>;
/**
* Gets tree repository for the given entity class or name.
* Only tree-type entities can have a TreeRepository,
* like ones decorated with @ClosureTable decorator.
*/
getTreeRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): TreeRepository<Entity> {
getTreeRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): TreeRepository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
@ -392,18 +411,21 @@ export class Connection {
/**
* Gets specific repository for the given entity class.
* SpecificRepository is a special repository that contains specific and non standard repository methods.
*/
getSpecificRepository<Entity>(entityClass: ConstructorFunction<Entity>): SpecificRepository<Entity>;
getSpecificRepository<Entity>(entityClass: ObjectType<Entity>): SpecificRepository<Entity>;
/**
* Gets specific repository for the given entity name.
* SpecificRepository is a special repository that contains specific and non standard repository methods.
*/
getSpecificRepository<Entity>(entityName: string): SpecificRepository<Entity>;
/**
* Gets specific repository for the given entity class or name.
* SpecificRepository is a special repository that contains specific and non standard repository methods.
*/
getSpecificRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): SpecificRepository<Entity> {
getSpecificRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): SpecificRepository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
@ -418,48 +440,26 @@ export class Connection {
return repositoryAggregate.specificRepository;
}
/**
* Gets specific repository for the given entity class.
*/
getSpecificReactiveRepository<Entity>(entityClass: ConstructorFunction<Entity>): SpecificReactiveRepository<Entity>;
/**
* Gets specific repository for the given entity name.
*/
getSpecificReactiveRepository<Entity>(entityName: string): SpecificReactiveRepository<Entity>;
/**
* Gets specific repository for the given entity class or name.
*/
getSpecificReactiveRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): SpecificReactiveRepository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
if (!this.entityMetadatas.hasTarget(entityClassOrName))
throw new RepositoryNotFoundError(this.name, entityClassOrName);
const metadata = this.entityMetadatas.findByTarget(entityClassOrName);
const repositoryAggregate = this.repositoryForMetadatas.find(metadataRepository => metadataRepository.metadata === metadata);
if (!repositoryAggregate)
throw new RepositoryNotFoundError(this.name, entityClassOrName);
return repositoryAggregate.specificReactiveRepository;
}
/**
* Gets reactive repository for the given entity class.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getReactiveRepository<Entity>(entityClass: ConstructorFunction<Entity>): ReactiveRepository<Entity>;
getReactiveRepository<Entity>(entityClass: ObjectType<Entity>): ReactiveRepository<Entity>;
/**
* Gets reactive repository for the given entity name.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getReactiveRepository<Entity>(entityName: string): ReactiveRepository<Entity>;
/**
* Gets reactive repository for the given entity class.
* Gets reactive repository for the given entity class or name.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getReactiveRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): ReactiveRepository<Entity> {
getReactiveRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): ReactiveRepository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
@ -476,18 +476,30 @@ export class Connection {
/**
* Gets reactive tree repository for the given entity class.
* Only tree-type entities can have a TreeRepository,
* like ones decorated with @ClosureTable decorator.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getReactiveTreeRepository<Entity>(entityClass: ConstructorFunction<Entity>): TreeReactiveRepository<Entity>;
getReactiveTreeRepository<Entity>(entityClass: ObjectType<Entity>): TreeReactiveRepository<Entity>;
/**
* Gets reactive tree repository for the given entity name.
* Only tree-type entities can have a TreeRepository,
* like ones decorated with @ClosureTable decorator.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getReactiveTreeRepository<Entity>(entityName: string): TreeReactiveRepository<Entity>;
/**
* Gets reactive tree repository for the given entity class.
* Gets reactive tree repository for the given entity class or name.
* Only tree-type entities can have a TreeRepository,
* like ones decorated with @ClosureTable decorator.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getReactiveTreeRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): TreeReactiveRepository<Entity> {
getReactiveTreeRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): TreeReactiveRepository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
@ -505,6 +517,43 @@ export class Connection {
return repositoryAggregate.reactiveRepository as TreeReactiveRepository<Entity>;
}
/**
* Gets specific repository for the given entity class.
* SpecificReactiveRepository is a special repository that contains specific and non standard repository methods.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getSpecificReactiveRepository<Entity>(entityClass: ObjectType<Entity>): SpecificReactiveRepository<Entity>;
/**
* Gets specific repository for the given entity name.
* SpecificReactiveRepository is a special repository that contains specific and non standard repository methods.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getSpecificReactiveRepository<Entity>(entityName: string): SpecificReactiveRepository<Entity>;
/**
* Gets specific repository for the given entity class or name.
* SpecificReactiveRepository is a special repository that contains specific and non standard repository methods.
* Reactive repositories has same functionality as regular repositories,
* the only difference is that reactive repository methods return Observable instead of Promise.
*/
getSpecificReactiveRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): SpecificReactiveRepository<Entity> {
if (!this.isConnected)
throw new NoConnectionForRepositoryError(this.name);
if (!this.entityMetadatas.hasTarget(entityClassOrName))
throw new RepositoryNotFoundError(this.name, entityClassOrName);
const metadata = this.entityMetadatas.findByTarget(entityClassOrName);
const repositoryAggregate = this.repositoryForMetadatas.find(metadataRepository => metadataRepository.metadata === metadata);
if (!repositoryAggregate)
throw new RepositoryNotFoundError(this.name, entityClassOrName);
return repositoryAggregate.specificReactiveRepository;
}
// -------------------------------------------------------------------------
// Private Methods
// -------------------------------------------------------------------------

View File

@ -1,4 +1,4 @@
import {ConstructorFunction} from "../common/ConstructorFunction";
import {ObjectType} from "../common/ObjectType";
import {getMetadataArgsStorage} from "../index";
import {EmbeddedMetadataArgs} from "../metadata-args/EmbeddedMetadataArgs";
@ -7,7 +7,7 @@ import {EmbeddedMetadataArgs} from "../metadata-args/EmbeddedMetadataArgs";
* single table of the entity where Embedded is used. And on hydration all columns which supposed to be in the
* embedded will be mapped to it from the single table.
*/
export function Embedded<T>(typeFunction: (type?: any) => ConstructorFunction<T>) {
export function Embedded<T>(typeFunction: (type?: any) => ObjectType<T>) {
return function (object: Object, propertyName: string) {
// const reflectedType = (Reflect as any).getMetadata("design:type", object, propertyName);

View File

@ -1,7 +1,7 @@
import {RelationOptions} from "../options/RelationOptions";
import {RelationTypes} from "../../metadata/types/RelationTypes";
import {getMetadataArgsStorage} from "../../index";
import {ConstructorFunction} from "../../common/ConstructorFunction";
import {ObjectType} from "../../common/ObjectType";
import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
/**
@ -9,14 +9,14 @@ import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is owner side of the relationship.
*/
export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
export function ManyToMany<T>(typeFunction: (type?: any) => ObjectType<T>, options?: RelationOptions): Function;
/**
* Many-to-many is a type of relationship when Entity1 can have multiple instances of Entity2, and Entity2 can have
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is owner side of the relationship.
*/
export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function ManyToMany<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
@ -25,7 +25,7 @@ export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<
* multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage
* entity1 and entity2 ids. This is owner side of the relationship.
*/
export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function ManyToMany<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: string|((object: T) => any);

View File

@ -1,7 +1,7 @@
import {RelationOptions} from "../options/RelationOptions";
import {RelationTypes} from "../../metadata/types/RelationTypes";
import {getMetadataArgsStorage} from "../../index";
import {ConstructorFunction} from "../../common/ConstructorFunction";
import {ObjectType} from "../../common/ObjectType";
import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
/**
@ -9,14 +9,14 @@ import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
* Entity2 can have a multiple instances of Entity1. Entity1 is an owner of the relationship, and storages Entity2 id
* on its own side.
*/
export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
export function ManyToOne<T>(typeFunction: (type?: any) => ObjectType<T>, options?: RelationOptions): Function;
/**
* Many-to-one relation allows to create type of relation when Entity1 can have single instance of Entity2, but
* Entity2 can have a multiple instances of Entity1. Entity1 is an owner of the relationship, and storages Entity2 id
* on its own side.
*/
export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function ManyToOne<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
@ -25,7 +25,7 @@ export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T
* Entity2 can have a multiple instances of Entity1. Entity1 is an owner of the relationship, and storages Entity2 id
* on its own side.
*/
export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function ManyToOne<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: string|((object: T) => any);

View File

@ -1,7 +1,7 @@
import {RelationOptions} from "../options/RelationOptions";
import {RelationTypes} from "../../metadata/types/RelationTypes";
import {getMetadataArgsStorage} from "../../index";
import {ConstructorFunction} from "../../common/ConstructorFunction";
import {ObjectType} from "../../common/ObjectType";
import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
// todo: make decorators which use inverse side string separate
@ -16,7 +16,7 @@ import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
* One-to-many relation allows to create type of relation when Entity2 can have multiple instances of Entity1.
* Entity1 have only one Entity2. Entity1 is an owner of the relationship, and storages Entity2 id on its own side.
*/
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function OneToMany<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSide: string|((object: T) => any),
options?: RelationOptions): Function;
@ -24,7 +24,7 @@ export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T
* One-to-many relation allows to create type of relation when Entity2 can have multiple instances of Entity1.
* Entity1 have only one Entity2. Entity1 is an owner of the relationship, and storages Entity2 id on its own side.
*/
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function OneToMany<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSideOrOptions: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: string|((object: T) => any);

View File

@ -1,20 +1,20 @@
import {RelationOptions} from "../options/RelationOptions";
import {RelationTypes} from "../../metadata/types/RelationTypes";
import {getMetadataArgsStorage} from "../../index";
import {ConstructorFunction} from "../../common/ConstructorFunction";
import {ObjectType} from "../../common/ObjectType";
import {RelationMetadataArgs} from "../../metadata-args/RelationMetadataArgs";
/**
* One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2.
* Entity1 is an owner of the relationship, and storages Entity1 id on its own side.
*/
export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>, options?: RelationOptions): Function;
export function OneToOne<T>(typeFunction: (type?: any) => ObjectType<T>, options?: RelationOptions): Function;
/**
* One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2.
* Entity1 is an owner of the relationship, and storages Entity1 id on its own side.
*/
export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function OneToOne<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSide?: string|((object: T) => any),
options?: RelationOptions): Function;
@ -22,7 +22,7 @@ export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>
* One-to-one relation allows to create direct relation between two entities. Entity1 have only one Entity2.
* Entity1 is an owner of the relationship, and storages Entity1 id on its own side.
*/
export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
export function OneToOne<T>(typeFunction: (type?: any) => ObjectType<T>,
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
options?: RelationOptions): Function {
let inverseSideProperty: string|((object: T) => any);

View File

@ -1,4 +1,4 @@
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "./ConnectionOptions";
import {ColumnMetadata} from "../metadata/ColumnMetadata";
import {ColumnTypes} from "../metadata/types/ColumnTypes";
import * as moment from "moment";

View File

@ -1,5 +1,5 @@
/**
* Connection options passed to the connection.
* Connection options passed to the driver.
*/
export interface ConnectionOptions {
@ -33,18 +33,6 @@ export interface ConnectionOptions {
*/
database?: string;
/**
* Drops the schema each time connection is being established.
* Be careful with this option and don't use this in production - otherwise you'll loose all your production data.
* This option is useful during debug and development.
*/
dropSchemaOnConnection?: boolean;
/**
* Indicates if database schema should be auto created every time application launch.
*/
autoSchemaCreate?: boolean;
/**
* Logging options.
*/

View File

@ -1,6 +1,6 @@
import {SchemaBuilder} from "../schema-builder/SchemaBuilder";
import {ColumnMetadata} from "../metadata/ColumnMetadata";
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "./ConnectionOptions";
import {ObjectLiteral} from "../common/ObjectLiteral";
/**

View File

@ -6,7 +6,7 @@ import {BaseDriver} from "./BaseDriver";
import {ColumnMetadata} from "../metadata/ColumnMetadata";
import {ColumnTypes} from "../metadata/types/ColumnTypes";
import * as moment from "moment";
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "./ConnectionOptions";
import {ObjectLiteral} from "../common/ObjectLiteral";
/**

View File

@ -2,7 +2,7 @@ import {Driver} from "./Driver";
import {SchemaBuilder} from "../schema-builder/SchemaBuilder";
import {ConnectionIsNotSetError} from "./error/ConnectionIsNotSetError";
import {BaseDriver} from "./BaseDriver";
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "./ConnectionOptions";
import {PostgresSchemaBuilder} from "../schema-builder/PostgresSchemaBuilder";
import {ObjectLiteral} from "../common/ObjectLiteral";

View File

@ -1,7 +1,7 @@
import {Connection} from "../connection/Connection";
import {QueryBuilder} from "../query-builder/QueryBuilder";
import {Repository} from "../repository/Repository";
import {ConstructorFunction} from "../common/ConstructorFunction";
import {ObjectType} from "../common/ObjectType";
import {ReactiveRepository} from "../repository/ReactiveRepository";
import {TreeRepository} from "../repository/TreeRepository";
import {ObjectLiteral} from "../common/ObjectLiteral";
@ -26,7 +26,7 @@ export abstract class BaseEntityManager {
/**
* Gets repository for the given entity class.
*/
getRepository<Entity>(entityClass: ConstructorFunction<Entity>): Repository<Entity>;
getRepository<Entity>(entityClass: ObjectType<Entity>): Repository<Entity>;
/**
* Gets repository for the given entity name.
@ -36,14 +36,14 @@ export abstract class BaseEntityManager {
/**
* Gets repository for the given entity class or name.
*/
getRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): Repository<Entity> {
getRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): Repository<Entity> {
return this.obtainRepository(entityClassOrName);
}
/**
* Gets tree repository for the given entity class.
*/
getTreeRepository<Entity>(entityClass: ConstructorFunction<Entity>): TreeRepository<Entity>;
getTreeRepository<Entity>(entityClass: ObjectType<Entity>): TreeRepository<Entity>;
/**
* Gets tree repository for the given entity name.
@ -53,14 +53,14 @@ export abstract class BaseEntityManager {
/**
* Gets tree repository for the given entity class or name.
*/
getTreeRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): TreeRepository<Entity> {
getTreeRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): TreeRepository<Entity> {
return this.obtainTreeRepository(entityClassOrName);
}
/**
* Gets reactive repository for the given entity class.
*/
getReactiveRepository<Entity>(entityClass: ConstructorFunction<Entity>): ReactiveRepository<Entity>;
getReactiveRepository<Entity>(entityClass: ObjectType<Entity>): ReactiveRepository<Entity>;
/**
* Gets reactive repository for the given entity name.
@ -70,14 +70,14 @@ export abstract class BaseEntityManager {
/**
* Gets reactive repository of the given entity.
*/
getReactiveRepository<Entity>(entityClass: ConstructorFunction<Entity>|string): ReactiveRepository<Entity> {
getReactiveRepository<Entity>(entityClass: ObjectType<Entity>|string): ReactiveRepository<Entity> {
return this.obtainReactiveRepository(entityClass);
}
/**
* Gets reactive tree repository for the given entity class.
*/
getReactiveTreeRepository<Entity>(entityClass: ConstructorFunction<Entity>): TreeReactiveRepository<Entity>;
getReactiveTreeRepository<Entity>(entityClass: ObjectType<Entity>): TreeReactiveRepository<Entity>;
/**
* Gets reactive tree repository for the given entity name.
@ -87,7 +87,7 @@ export abstract class BaseEntityManager {
/**
* Gets reactive tree repository of the given entity.
*/
getReactiveTreeRepository<Entity>(entityClass: ConstructorFunction<Entity>|string): TreeReactiveRepository<Entity> {
getReactiveTreeRepository<Entity>(entityClass: ObjectType<Entity>|string): TreeReactiveRepository<Entity> {
return this.obtainReactiveTreeRepository(entityClass);
}
@ -115,32 +115,32 @@ export abstract class BaseEntityManager {
/**
* Creates a new query builder that can be used to build an sql query.
*/
createQueryBuilder<Entity>(entityClass: ConstructorFunction<Entity>, alias: string): QueryBuilder<Entity> {
createQueryBuilder<Entity>(entityClass: ObjectType<Entity>, alias: string): QueryBuilder<Entity> {
return this.obtainRepository(entityClass).createQueryBuilder(alias);
}
/**
* Creates a new entity instance.
*/
create<Entity>(entityClass: ConstructorFunction<Entity>): Entity;
create<Entity>(entityClass: ObjectType<Entity>): Entity;
/**
* Creates a new entity instance and copies all entity properties from this object into a new entity.
* Note that it copies only properties that present in entity schema.
*/
create<Entity>(entityClass: ConstructorFunction<Entity>, plainObject: Object): Entity;
create<Entity>(entityClass: ObjectType<Entity>, plainObject: Object): Entity;
/**
* Creates a new entities and copies all entity properties from given objects into their new entities.
* Note that it copies only properties that present in entity schema.
*/
create<Entity>(entityClass: ConstructorFunction<Entity>, plainObjects: Object[]): Entity[];
create<Entity>(entityClass: ObjectType<Entity>, plainObjects: Object[]): Entity[];
/**
* Creates a new entity instance or instances.
* Can copy properties from the given object into new entities.
*/
create<Entity>(entityClass: ConstructorFunction<Entity>, plainObjectOrObjects?: Object|Object[]): Entity|Entity[] {
create<Entity>(entityClass: ObjectType<Entity>, plainObjectOrObjects?: Object|Object[]): Entity|Entity[] {
if (plainObjectOrObjects instanceof Array) {
return this.obtainRepository(entityClass).create(plainObjectOrObjects);
} else if (plainObjectOrObjects) {
@ -156,14 +156,14 @@ export abstract class BaseEntityManager {
* and returns this new entity. This new entity is actually a loaded from the db entity with all properties
* replaced from the new object.
*/
preload<Entity>(entityClass: ConstructorFunction<Entity>, object: Object): Promise<Entity> {
preload<Entity>(entityClass: ObjectType<Entity>, object: Object): Promise<Entity> {
return this.obtainRepository(entityClass).preload(object);
}
/**
* Merges two entities into one new entity.
*/
merge<Entity>(entityClass: ConstructorFunction<Entity>, ...objects: ObjectLiteral[]): Entity {
merge<Entity>(entityClass: ObjectType<Entity>, ...objects: ObjectLiteral[]): Entity {
return <Entity> this.obtainRepository(entityClass).merge(...objects);
}
@ -176,7 +176,7 @@ export abstract class BaseEntityManager {
* code, and obtainRepository does not accept a Function, and only ConstructorFunction it can accept - it brings
* us type problems. To avoid this we are using private function here.
*/
protected obtainRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): Repository<Entity> {
protected obtainRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): Repository<Entity> {
if (typeof entityClassOrName === "string") {
return this.connection.getRepository<Entity>(entityClassOrName);
} else {
@ -189,7 +189,7 @@ export abstract class BaseEntityManager {
* code, and obtainTreeRepository does not accept a Function, and only ConstructorFunction it can accept - it brings
* us type problems. To avoid this we are using private function here.
*/
protected obtainTreeRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): TreeRepository<Entity> {
protected obtainTreeRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): TreeRepository<Entity> {
if (typeof entityClassOrName === "string") {
return this.connection.getTreeRepository<Entity>(entityClassOrName);
} else {
@ -202,7 +202,7 @@ export abstract class BaseEntityManager {
* code, and obtainReactiveRepository does not accept a Function, and only ConstructorFunction it can accept - it brings
* us type problems. To avoid this we are using private function here.
*/
protected obtainReactiveRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): ReactiveRepository<Entity> {
protected obtainReactiveRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): ReactiveRepository<Entity> {
if (typeof entityClassOrName === "string") {
return this.connection.getReactiveRepository<Entity>(entityClassOrName);
} else {
@ -215,7 +215,7 @@ export abstract class BaseEntityManager {
* code, and obtainReactiveTreeRepository does not accept a Function, and only ConstructorFunction it can accept - it brings
* us type problems. To avoid this we are using private function here.
*/
protected obtainReactiveTreeRepository<Entity>(entityClassOrName: ConstructorFunction<Entity>|string): TreeReactiveRepository<Entity> {
protected obtainReactiveTreeRepository<Entity>(entityClassOrName: ObjectType<Entity>|string): TreeReactiveRepository<Entity> {
if (typeof entityClassOrName === "string") {
return this.connection.getReactiveTreeRepository<Entity>(entityClassOrName);
} else {

View File

@ -1,6 +1,6 @@
import {Connection} from "../connection/Connection";
import {FindOptions} from "../repository/FindOptions";
import {ConstructorFunction} from "../common/ConstructorFunction";
import {ObjectType} from "../common/ObjectType";
import {BaseEntityManager} from "./BaseEntityManager";
/**
@ -47,27 +47,27 @@ export class EntityManager extends BaseEntityManager {
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>): Promise<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object): Promise<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, options: FindOptions): Promise<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, options: FindOptions): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object, options: FindOptions): Promise<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Promise<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity[]> {
find<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity[]> {
if (conditionsOrFindOptions && options) {
return this.obtainRepository(entityClass).find(conditionsOrFindOptions, options);
@ -82,27 +82,27 @@ export class EntityManager extends BaseEntityManager {
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>): Promise<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>): Promise<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object): Promise<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Promise<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, options: FindOptions): Promise<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, options: FindOptions): Promise<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<[Entity[], number]> {
findAndCount<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<[Entity[], number]> {
if (conditionsOrFindOptions && options) {
return this.obtainRepository(entityClass).findAndCount(conditionsOrFindOptions, options);
@ -117,27 +117,27 @@ export class EntityManager extends BaseEntityManager {
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>): Promise<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object): Promise<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, options: FindOptions): Promise<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, options: FindOptions): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object, options: FindOptions): Promise<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Promise<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity> {
findOne<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<Entity> {
if (conditionsOrFindOptions && options) {
return this.obtainRepository(entityClass).findOne(conditionsOrFindOptions, options);
@ -152,7 +152,7 @@ export class EntityManager extends BaseEntityManager {
/**
* Finds entity with given id.
*/
findOneById<Entity>(entityClass: ConstructorFunction<Entity>, id: any, options?: FindOptions): Promise<Entity> {
findOneById<Entity>(entityClass: ObjectType<Entity>, id: any, options?: FindOptions): Promise<Entity> {
return this.obtainRepository(entityClass).findOneById(id, options);
}

View File

@ -1,7 +1,7 @@
import * as Rx from "rxjs/Rx";
import {Connection} from "../connection/Connection";
import {FindOptions} from "../repository/FindOptions";
import {ConstructorFunction} from "../common/ConstructorFunction";
import {ObjectType} from "../common/ObjectType";
import {BaseEntityManager} from "./BaseEntityManager";
/**
@ -49,27 +49,27 @@ export class ReactiveEntityManager extends BaseEntityManager {
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>): Rx.Observable<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>): Rx.Observable<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object): Rx.Observable<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Rx.Observable<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, options: FindOptions): Rx.Observable<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, options: FindOptions): Rx.Observable<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object, options: FindOptions): Rx.Observable<Entity[]>;
find<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Rx.Observable<Entity[]>;
/**
* Finds entities that match given conditions.
*/
find<Entity>(entityClass: ConstructorFunction<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<Entity[]> {
find<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<Entity[]> {
if (conditionsOrFindOptions && options) {
return this.getReactiveRepository(entityClass).find(conditionsOrFindOptions, options);
@ -84,27 +84,27 @@ export class ReactiveEntityManager extends BaseEntityManager {
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>): Rx.Observable<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>): Rx.Observable<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object): Rx.Observable<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Rx.Observable<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, options: FindOptions): Rx.Observable<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, options: FindOptions): Rx.Observable<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object, options: FindOptions): Rx.Observable<[ Entity[], number ]>;
findAndCount<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Rx.Observable<[ Entity[], number ]>;
/**
* Finds entities that match given conditions.
*/
findAndCount<Entity>(entityClass: ConstructorFunction<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<[Entity[], number]> {
findAndCount<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<[Entity[], number]> {
if (conditionsOrFindOptions && options) {
return this.getReactiveRepository(entityClass).findAndCount(conditionsOrFindOptions, options);
@ -119,27 +119,27 @@ export class ReactiveEntityManager extends BaseEntityManager {
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>): Rx.Observable<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>): Rx.Observable<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object): Rx.Observable<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: Object): Rx.Observable<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, options: FindOptions): Rx.Observable<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, options: FindOptions): Rx.Observable<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, conditions: Object, options: FindOptions): Rx.Observable<Entity>;
findOne<Entity>(entityClass: ObjectType<Entity>, conditions: Object, options: FindOptions): Rx.Observable<Entity>;
/**
* Finds first entity that matches given conditions.
*/
findOne<Entity>(entityClass: ConstructorFunction<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<Entity> {
findOne<Entity>(entityClass: ObjectType<Entity>, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<Entity> {
if (conditionsOrFindOptions && options) {
return this.getReactiveRepository(entityClass).findOne(conditionsOrFindOptions, options);
@ -154,7 +154,7 @@ export class ReactiveEntityManager extends BaseEntityManager {
/**
* Finds entity with given id.
*/
findOneById<Entity>(entityClass: ConstructorFunction<Entity>, id: any, options?: FindOptions): Rx.Observable<Entity> {
findOneById<Entity>(entityClass: ObjectType<Entity>, id: any, options?: FindOptions): Rx.Observable<Entity> {
return this.getReactiveRepository(entityClass).findOneById(id, options);
}

View File

@ -2,7 +2,7 @@
*/
import {ConnectionOptions} from "./connection/ConnectionOptions";
import {ConnectionOptions} from "./driver/ConnectionOptions";
import {ConnectionManager} from "./connection-manager/ConnectionManager";
import {Connection} from "./connection/Connection";
import {MysqlDriver} from "./driver/MysqlDriver";
@ -93,7 +93,7 @@ export function getConnectionManager() {
* Allows to quickly create a connection based on the given options. Uses ConnectionManager.
*/
export function createConnection(options: CreateConnectionOptions) {
return getConnectionManager().create(options).connect();
return getConnectionManager().createAndConnect(options);
}
// -------------------------------------------------------------------------
@ -101,7 +101,7 @@ export function createConnection(options: CreateConnectionOptions) {
// -------------------------------------------------------------------------
export {Connection} from "./connection/Connection";
export {ConnectionOptions} from "./connection/ConnectionOptions";
export {ConnectionOptions} from "./driver/ConnectionOptions";
export {ConnectionManager} from "./connection-manager/ConnectionManager";
export {CreateConnectionOptions} from "./connection-manager/CreateConnectionOptions";
export {Driver} from "./driver/Driver";

View File

@ -1,5 +1,5 @@
import {QueryBuilder} from "../query-builder/QueryBuilder";
import {ConnectionOptions} from "../connection/ConnectionOptions";
import {ConnectionOptions} from "../driver/ConnectionOptions";
/**
* Options to be passed to find methods.

View File

@ -305,7 +305,7 @@ export class SpecificRepository<Entity extends ObjectLiteral> {
.filter(entityWithId => entityWithId.id !== null && entityWithId.id !== undefined)
.filter(entityWithId => !dbEntities.find(dbEntity => dbEntity.entityTarget === entityWithId.entityTarget && dbEntity.id === entityWithId.id))
.map(entityWithId => {
const metadata = this.connection.getMetadata(entityWithId.entityTarget);
const metadata = this.connection.getMetadata(entityWithId.entityTarget as any); // todo: fix type
const repository = this.connection.getRepository(entityWithId.entityTarget as any); // todo: fix type
return repository.findOneById(entityWithId.id).then(loadedEntity => {
if (!loadedEntity) return undefined;

View File

@ -22,11 +22,11 @@ describe("persistence > many-to-many", function() {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Category, User]
};
// connect to db

View File

@ -21,11 +21,11 @@ describe("repository > removeById and removeByIds methods", function() {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post]
};
// connect to db

View File

@ -22,12 +22,12 @@ describe("repository > set/add/remove relation methods", function() {
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true,
logging: {
// logQueries: true, // uncomment for debugging
logFailedQueryError: true
}
},
autoSchemaCreate: true,
entities: [Post, Category]
};
// connect to db

View File

@ -1,7 +1,7 @@
import {CreateConnectionOptions} from "../../src/connection-manager/CreateConnectionOptions";
import {createConnection} from "../../src/index";
import {Connection} from "../../src/connection/Connection";
import {ConnectionOptions} from "../../src/connection/ConnectionOptions";
import {ConnectionOptions} from "../../src/driver/ConnectionOptions";
import {EntitySchema} from "../../src/metadata/entity-schema/EntitySchema";
export interface TestingConnectionOptions {
@ -25,7 +25,6 @@ export function createTestingConnectionOptions(type: "mysql"|"mysqlSecondary"|"p
username: parameters.connections[type].username,
password: parameters.connections[type].password,
database: parameters.connections[type].database,
autoSchemaCreate: true,
logging: {
// logQueries: true, // uncomment for debugging
logOnlyFailedQueries: true,
@ -40,6 +39,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions
driver: "mysql",
connectionName: "mysqlPrimaryConnection",
connection: createTestingConnectionOptions("mysql"),
autoSchemaCreate: true,
entities: options && options.entities ? options.entities : [],
entitySchemas: options && options.entitySchemas ? options.entitySchemas : [],
entityDirectories: options && options.entityDirectories ? options.entityDirectories : [],
@ -100,9 +100,9 @@ export function setupConnection(callback: (connection: Connection) => any, entit
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaCreate: true
database: "test"
},
autoSchemaCreate: true,
entities: entities
};