diff --git a/sample/sample1-simple-entity/app.ts b/sample/sample1-simple-entity/app.ts index f900efd96..357933c2c 100644 --- a/sample/sample1-simple-entity/app.ts +++ b/sample/sample1-simple-entity/app.ts @@ -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 = { diff --git a/sample/sample10-mixed/app.ts b/sample/sample10-mixed/app.ts index d05d6e769..4bfca47a1 100644 --- a/sample/sample10-mixed/app.ts +++ b/sample/sample10-mixed/app.ts @@ -13,9 +13,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entityDirectories: [__dirname + "/entity/*"] }; diff --git a/sample/sample11-all-types-entity/app.ts b/sample/sample11-all-types-entity/app.ts index 1d219a99b..d8304032b 100644 --- a/sample/sample11-all-types-entity/app.ts +++ b/sample/sample11-all-types-entity/app.ts @@ -10,11 +10,11 @@ const options: CreateConnectionOptions = { username: "root", password: "admin", database: "test", - autoSchemaCreate: true, logging: { logOnlyFailedQueries: true } }, + autoSchemaCreate: true, entities: [EverythingEntity] }; diff --git a/sample/sample12-custom-naming-strategy/app.ts b/sample/sample12-custom-naming-strategy/app.ts index e8dd41ee5..b972aab5c 100644 --- a/sample/sample12-custom-naming-strategy/app.ts +++ b/sample/sample12-custom-naming-strategy/app.ts @@ -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] diff --git a/sample/sample13-everywhere-abstraction/app.ts b/sample/sample13-everywhere-abstraction/app.ts index 0604c732e..3a53d1288 100644 --- a/sample/sample13-everywhere-abstraction/app.ts +++ b/sample/sample13-everywhere-abstraction/app.ts @@ -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/*"] }; diff --git a/sample/sample14-errors-in-wrong-metdata/app.ts b/sample/sample14-errors-in-wrong-metdata/app.ts index 76d3304da..342e87807 100644 --- a/sample/sample14-errors-in-wrong-metdata/app.ts +++ b/sample/sample14-errors-in-wrong-metdata/app.ts @@ -10,9 +10,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entities: [Post, PostAuthor] }; diff --git a/sample/sample15-we-are-reactive/app.ts b/sample/sample15-we-are-reactive/app.ts index ca4c515a2..c135e5661 100644 --- a/sample/sample15-we-are-reactive/app.ts +++ b/sample/sample15-we-are-reactive/app.ts @@ -9,9 +9,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entities: [Post] }; diff --git a/sample/sample16-indexes/app.ts b/sample/sample16-indexes/app.ts index 917089223..5df1bbcef 100644 --- a/sample/sample16-indexes/app.ts +++ b/sample/sample16-indexes/app.ts @@ -9,9 +9,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entities: [Post] }; diff --git a/sample/sample17-versioning/app.ts b/sample/sample17-versioning/app.ts index 042ded1cf..c83bce69f 100644 --- a/sample/sample17-versioning/app.ts +++ b/sample/sample17-versioning/app.ts @@ -10,12 +10,12 @@ const options: CreateConnectionOptions = { username: "root", password: "admin", database: "test", - autoSchemaCreate: true, logging: { logOnlyFailedQueries: true, logFailedQueryError: true } }, + autoSchemaCreate: true, entities: [Post] }; diff --git a/sample/sample18-lazy-relations/app.ts b/sample/sample18-lazy-relations/app.ts index ff508c187..6be6075e0 100644 --- a/sample/sample18-lazy-relations/app.ts +++ b/sample/sample18-lazy-relations/app.ts @@ -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] }; diff --git a/sample/sample19-one-side-relations/app.ts b/sample/sample19-one-side-relations/app.ts index 9ca810832..e64044ef7 100644 --- a/sample/sample19-one-side-relations/app.ts +++ b/sample/sample19-one-side-relations/app.ts @@ -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] }; diff --git a/sample/sample2-one-to-one/app.ts b/sample/sample2-one-to-one/app.ts index 7ddeb5b66..65a1c68d6 100644 --- a/sample/sample2-one-to-one/app.ts +++ b/sample/sample2-one-to-one/app.ts @@ -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] }; diff --git a/sample/sample20-join-without-relation/app.ts b/sample/sample20-join-without-relation/app.ts index fee926236..6afdc20fc 100644 --- a/sample/sample20-join-without-relation/app.ts +++ b/sample/sample20-join-without-relation/app.ts @@ -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] }; diff --git a/sample/sample21-custom-join-table-column/app.ts b/sample/sample21-custom-join-table-column/app.ts index a461a0d29..738085b71 100644 --- a/sample/sample21-custom-join-table-column/app.ts +++ b/sample/sample21-custom-join-table-column/app.ts @@ -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] }; diff --git a/sample/sample22-closure-table/app.ts b/sample/sample22-closure-table/app.ts index 4272f5a72..26a3f59ee 100644 --- a/sample/sample22-closure-table/app.ts +++ b/sample/sample22-closure-table/app.ts @@ -10,12 +10,12 @@ const options: CreateConnectionOptions = { username: "root", password: "admin", database: "test", - autoSchemaCreate: true, logging: { logOnlyFailedQueries: true, logFailedQueryError: true } }, + autoSchemaCreate: true, entities: [Category] }; diff --git a/sample/sample23-nested-joins/app.ts b/sample/sample23-nested-joins/app.ts index cc078220c..ca63d0153 100644 --- a/sample/sample23-nested-joins/app.ts +++ b/sample/sample23-nested-joins/app.ts @@ -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] }; diff --git a/sample/sample24-schemas/app.ts b/sample/sample24-schemas/app.ts index 22652488a..8d67d26ab 100644 --- a/sample/sample24-schemas/app.ts +++ b/sample/sample24-schemas/app.ts @@ -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"), diff --git a/sample/sample25-insert-from-inverse-side/app.ts b/sample/sample25-insert-from-inverse-side/app.ts index a6afcd397..02123c821 100644 --- a/sample/sample25-insert-from-inverse-side/app.ts +++ b/sample/sample25-insert-from-inverse-side/app.ts @@ -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] }; diff --git a/sample/sample26-embedded-tables/app.ts b/sample/sample26-embedded-tables/app.ts index 3db20ef37..35cc4e616 100644 --- a/sample/sample26-embedded-tables/app.ts +++ b/sample/sample26-embedded-tables/app.ts @@ -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] }; diff --git a/sample/sample3-many-to-one/app.ts b/sample/sample3-many-to-one/app.ts index 216f535f2..c760fc8ae 100644 --- a/sample/sample3-many-to-one/app.ts +++ b/sample/sample3-many-to-one/app.ts @@ -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] }; diff --git a/sample/sample4-many-to-many/app.ts b/sample/sample4-many-to-many/app.ts index 79c5a7d36..92249b31c 100644 --- a/sample/sample4-many-to-many/app.ts +++ b/sample/sample4-many-to-many/app.ts @@ -10,9 +10,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entityDirectories: [__dirname + "/entity/*"] }; diff --git a/sample/sample5-subscribers/app.ts b/sample/sample5-subscribers/app.ts index 4efe5121e..c59f4da3d 100644 --- a/sample/sample5-subscribers/app.ts +++ b/sample/sample5-subscribers/app.ts @@ -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] }; diff --git a/sample/sample6-abstract-table/app.ts b/sample/sample6-abstract-table/app.ts index be0695b84..d9c3822d0 100644 --- a/sample/sample6-abstract-table/app.ts +++ b/sample/sample6-abstract-table/app.ts @@ -12,9 +12,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entityDirectories: [__dirname + "/entity/*"] }; diff --git a/sample/sample7-pagination/app.ts b/sample/sample7-pagination/app.ts index 0aa70f070..73db30463 100644 --- a/sample/sample7-pagination/app.ts +++ b/sample/sample7-pagination/app.ts @@ -11,9 +11,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entityDirectories: [__dirname + "/entity/*"] }; diff --git a/sample/sample8-self-referencing/app.ts b/sample/sample8-self-referencing/app.ts index 64f832afe..ae002ee4b 100644 --- a/sample/sample8-self-referencing/app.ts +++ b/sample/sample8-self-referencing/app.ts @@ -9,9 +9,9 @@ const options: CreateConnectionOptions = { port: 3306, username: "root", password: "admin", - database: "test", - autoSchemaCreate: true + database: "test" }, + autoSchemaCreate: true, entityDirectories: [__dirname + "/entity/*"] }; diff --git a/sample/sample9-entity-listeners/app.ts b/sample/sample9-entity-listeners/app.ts index 8c09bfc21..62419b078 100644 --- a/sample/sample9-entity-listeners/app.ts +++ b/sample/sample9-entity-listeners/app.ts @@ -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/*"] }; diff --git a/src/common/ConstructorFunction.ts b/src/common/ObjectType.ts similarity index 63% rename from src/common/ConstructorFunction.ts rename to src/common/ObjectType.ts index b7155fdc7..7bfa6e771 100644 --- a/src/common/ConstructorFunction.ts +++ b/src/common/ObjectType.ts @@ -1,6 +1,6 @@ /** * Used to get a type of the creating Function. */ -export interface ConstructorFunction { +export interface ObjectType { new (): T; } diff --git a/src/connection-manager/ConnectionManager.ts b/src/connection-manager/ConnectionManager.ts index dab518b65..18fa29304 100644 --- a/src/connection-manager/ConnectionManager.ts +++ b/src/connection-manager/ConnectionManager.ts @@ -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 { + 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. */ diff --git a/src/connection-manager/CreateConnectionOptions.ts b/src/connection-manager/CreateConnectionOptions.ts index b5a0242a1..8460a3d94 100644 --- a/src/connection-manager/CreateConnectionOptions.ts +++ b/src/connection-manager/CreateConnectionOptions.ts @@ -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. */ diff --git a/src/connection/Connection.ts b/src/connection/Connection.ts index b73d20738..c6e4e8aa7 100644 --- a/src/connection/Connection.ts +++ b/src/connection/Connection.ts @@ -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 { if (!this.isConnected) @@ -211,15 +204,22 @@ export class Connection { this._isConnected = false; } + /** + * Drops the database and all its data. + */ + async dropDatabase(): Promise { + return this.driver.clearDatabase(); + } + /** * Creates database schema for all entities registered in this connection. */ async syncSchema(dropBeforeSync: boolean = false): Promise { 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.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.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.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.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 { 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 { 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 { 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 { 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(entityClass: ConstructorFunction): Repository; + getRepository(entityClass: ObjectType): Repository; /** * Gets repository for the given entity name. @@ -344,7 +357,7 @@ export class Connection { /** * Gets repository for the given entity class or name. */ - getRepository(entityClassOrName: ConstructorFunction|string): Repository { + getRepository(entityClassOrName: ObjectType|string): Repository { 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(entityClass: ConstructorFunction): TreeRepository; + getTreeRepository(entityClass: ObjectType): TreeRepository; /** - * 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(entityName: string): TreeRepository; /** * 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(entityClassOrName: ConstructorFunction|string): TreeRepository { + getTreeRepository(entityClassOrName: ObjectType|string): TreeRepository { 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(entityClass: ConstructorFunction): SpecificRepository; + getSpecificRepository(entityClass: ObjectType): SpecificRepository; /** * Gets specific repository for the given entity name. + * SpecificRepository is a special repository that contains specific and non standard repository methods. */ getSpecificRepository(entityName: string): SpecificRepository; /** * Gets specific repository for the given entity class or name. + * SpecificRepository is a special repository that contains specific and non standard repository methods. */ - getSpecificRepository(entityClassOrName: ConstructorFunction|string): SpecificRepository { + getSpecificRepository(entityClassOrName: ObjectType|string): SpecificRepository { 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(entityClass: ConstructorFunction): SpecificReactiveRepository; - - /** - * Gets specific repository for the given entity name. - */ - getSpecificReactiveRepository(entityName: string): SpecificReactiveRepository; - - /** - * Gets specific repository for the given entity class or name. - */ - getSpecificReactiveRepository(entityClassOrName: ConstructorFunction|string): SpecificReactiveRepository { - 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(entityClass: ConstructorFunction): ReactiveRepository; + getReactiveRepository(entityClass: ObjectType): ReactiveRepository; /** * 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(entityName: string): ReactiveRepository; /** - * 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(entityClassOrName: ConstructorFunction|string): ReactiveRepository { + getReactiveRepository(entityClassOrName: ObjectType|string): ReactiveRepository { 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(entityClass: ConstructorFunction): TreeReactiveRepository; + getReactiveTreeRepository(entityClass: ObjectType): TreeReactiveRepository; /** * 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(entityName: string): TreeReactiveRepository; /** - * 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(entityClassOrName: ConstructorFunction|string): TreeReactiveRepository { + getReactiveTreeRepository(entityClassOrName: ObjectType|string): TreeReactiveRepository { if (!this.isConnected) throw new NoConnectionForRepositoryError(this.name); @@ -505,6 +517,43 @@ export class Connection { return repositoryAggregate.reactiveRepository as TreeReactiveRepository; } + /** + * 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(entityClass: ObjectType): SpecificReactiveRepository; + + /** + * 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(entityName: string): SpecificReactiveRepository; + + /** + * 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(entityClassOrName: ObjectType|string): SpecificReactiveRepository { + 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 // ------------------------------------------------------------------------- diff --git a/src/decorator/Embedded.ts b/src/decorator/Embedded.ts index 3f6fcc8e3..013529ad6 100644 --- a/src/decorator/Embedded.ts +++ b/src/decorator/Embedded.ts @@ -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(typeFunction: (type?: any) => ConstructorFunction) { +export function Embedded(typeFunction: (type?: any) => ObjectType) { return function (object: Object, propertyName: string) { // const reflectedType = (Reflect as any).getMetadata("design:type", object, propertyName); diff --git a/src/decorator/relations/ManyToMany.ts b/src/decorator/relations/ManyToMany.ts index 2637805b2..0a8c72f4d 100644 --- a/src/decorator/relations/ManyToMany.ts +++ b/src/decorator/relations/ManyToMany.ts @@ -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(typeFunction: (type?: any) => ConstructorFunction, options?: RelationOptions): Function; +export function ManyToMany(typeFunction: (type?: any) => ObjectType, 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(typeFunction: (type?: any) => ConstructorFunction, +export function ManyToMany(typeFunction: (type?: any) => ObjectType, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -25,7 +25,7 @@ export function ManyToMany(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(typeFunction: (type?: any) => ConstructorFunction, +export function ManyToMany(typeFunction: (type?: any) => ObjectType, inverseSideOrOptions?: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/decorator/relations/ManyToOne.ts b/src/decorator/relations/ManyToOne.ts index 7af4428d2..a5a3036de 100644 --- a/src/decorator/relations/ManyToOne.ts +++ b/src/decorator/relations/ManyToOne.ts @@ -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(typeFunction: (type?: any) => ConstructorFunction, options?: RelationOptions): Function; +export function ManyToOne(typeFunction: (type?: any) => ObjectType, 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(typeFunction: (type?: any) => ConstructorFunction, +export function ManyToOne(typeFunction: (type?: any) => ObjectType, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -25,7 +25,7 @@ export function ManyToOne(typeFunction: (type?: any) => ConstructorFunction(typeFunction: (type?: any) => ConstructorFunction, +export function ManyToOne(typeFunction: (type?: any) => ObjectType, inverseSideOrOptions?: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/decorator/relations/OneToMany.ts b/src/decorator/relations/OneToMany.ts index f67e8fecb..ecfadb055 100644 --- a/src/decorator/relations/OneToMany.ts +++ b/src/decorator/relations/OneToMany.ts @@ -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(typeFunction: (type?: any) => ConstructorFunction, +export function OneToMany(typeFunction: (type?: any) => ObjectType, inverseSide: string|((object: T) => any), options?: RelationOptions): Function; @@ -24,7 +24,7 @@ export function OneToMany(typeFunction: (type?: any) => ConstructorFunction(typeFunction: (type?: any) => ConstructorFunction, +export function OneToMany(typeFunction: (type?: any) => ObjectType, inverseSideOrOptions: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/decorator/relations/OneToOne.ts b/src/decorator/relations/OneToOne.ts index 6912a0bfa..bca3ce7de 100644 --- a/src/decorator/relations/OneToOne.ts +++ b/src/decorator/relations/OneToOne.ts @@ -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(typeFunction: (type?: any) => ConstructorFunction, options?: RelationOptions): Function; +export function OneToOne(typeFunction: (type?: any) => ObjectType, 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(typeFunction: (type?: any) => ConstructorFunction, +export function OneToOne(typeFunction: (type?: any) => ObjectType, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -22,7 +22,7 @@ export function OneToOne(typeFunction: (type?: any) => ConstructorFunction * 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(typeFunction: (type?: any) => ConstructorFunction, +export function OneToOne(typeFunction: (type?: any) => ObjectType, inverseSideOrOptions?: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/driver/BaseDriver.ts b/src/driver/BaseDriver.ts index 6d3ac8b81..863cacedd 100644 --- a/src/driver/BaseDriver.ts +++ b/src/driver/BaseDriver.ts @@ -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"; diff --git a/src/connection/ConnectionOptions.ts b/src/driver/ConnectionOptions.ts similarity index 68% rename from src/connection/ConnectionOptions.ts rename to src/driver/ConnectionOptions.ts index e27441f25..ffbd2cfd7 100644 --- a/src/connection/ConnectionOptions.ts +++ b/src/driver/ConnectionOptions.ts @@ -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. */ diff --git a/src/driver/Driver.ts b/src/driver/Driver.ts index fe8dfec68..d705206ac 100644 --- a/src/driver/Driver.ts +++ b/src/driver/Driver.ts @@ -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"; /** diff --git a/src/driver/MysqlDriver.ts b/src/driver/MysqlDriver.ts index c06b5977a..f255ee38f 100644 --- a/src/driver/MysqlDriver.ts +++ b/src/driver/MysqlDriver.ts @@ -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"; /** diff --git a/src/driver/PostgresDriver.ts b/src/driver/PostgresDriver.ts index a11fafcca..bc94ca5ba 100644 --- a/src/driver/PostgresDriver.ts +++ b/src/driver/PostgresDriver.ts @@ -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"; diff --git a/src/entity-manager/BaseEntityManager.ts b/src/entity-manager/BaseEntityManager.ts index 0b8d0fa2a..9a14e2054 100644 --- a/src/entity-manager/BaseEntityManager.ts +++ b/src/entity-manager/BaseEntityManager.ts @@ -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(entityClass: ConstructorFunction): Repository; + getRepository(entityClass: ObjectType): Repository; /** * 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(entityClassOrName: ConstructorFunction|string): Repository { + getRepository(entityClassOrName: ObjectType|string): Repository { return this.obtainRepository(entityClassOrName); } /** * Gets tree repository for the given entity class. */ - getTreeRepository(entityClass: ConstructorFunction): TreeRepository; + getTreeRepository(entityClass: ObjectType): TreeRepository; /** * 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(entityClassOrName: ConstructorFunction|string): TreeRepository { + getTreeRepository(entityClassOrName: ObjectType|string): TreeRepository { return this.obtainTreeRepository(entityClassOrName); } /** * Gets reactive repository for the given entity class. */ - getReactiveRepository(entityClass: ConstructorFunction): ReactiveRepository; + getReactiveRepository(entityClass: ObjectType): ReactiveRepository; /** * Gets reactive repository for the given entity name. @@ -70,14 +70,14 @@ export abstract class BaseEntityManager { /** * Gets reactive repository of the given entity. */ - getReactiveRepository(entityClass: ConstructorFunction|string): ReactiveRepository { + getReactiveRepository(entityClass: ObjectType|string): ReactiveRepository { return this.obtainReactiveRepository(entityClass); } /** * Gets reactive tree repository for the given entity class. */ - getReactiveTreeRepository(entityClass: ConstructorFunction): TreeReactiveRepository; + getReactiveTreeRepository(entityClass: ObjectType): TreeReactiveRepository; /** * 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(entityClass: ConstructorFunction|string): TreeReactiveRepository { + getReactiveTreeRepository(entityClass: ObjectType|string): TreeReactiveRepository { 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(entityClass: ConstructorFunction, alias: string): QueryBuilder { + createQueryBuilder(entityClass: ObjectType, alias: string): QueryBuilder { return this.obtainRepository(entityClass).createQueryBuilder(alias); } /** * Creates a new entity instance. */ - create(entityClass: ConstructorFunction): Entity; + create(entityClass: ObjectType): 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(entityClass: ConstructorFunction, plainObject: Object): Entity; + create(entityClass: ObjectType, 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(entityClass: ConstructorFunction, plainObjects: Object[]): Entity[]; + create(entityClass: ObjectType, plainObjects: Object[]): Entity[]; /** * Creates a new entity instance or instances. * Can copy properties from the given object into new entities. */ - create(entityClass: ConstructorFunction, plainObjectOrObjects?: Object|Object[]): Entity|Entity[] { + create(entityClass: ObjectType, 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(entityClass: ConstructorFunction, object: Object): Promise { + preload(entityClass: ObjectType, object: Object): Promise { return this.obtainRepository(entityClass).preload(object); } /** * Merges two entities into one new entity. */ - merge(entityClass: ConstructorFunction, ...objects: ObjectLiteral[]): Entity { + merge(entityClass: ObjectType, ...objects: ObjectLiteral[]): Entity { return 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(entityClassOrName: ConstructorFunction|string): Repository { + protected obtainRepository(entityClassOrName: ObjectType|string): Repository { if (typeof entityClassOrName === "string") { return this.connection.getRepository(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(entityClassOrName: ConstructorFunction|string): TreeRepository { + protected obtainTreeRepository(entityClassOrName: ObjectType|string): TreeRepository { if (typeof entityClassOrName === "string") { return this.connection.getTreeRepository(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(entityClassOrName: ConstructorFunction|string): ReactiveRepository { + protected obtainReactiveRepository(entityClassOrName: ObjectType|string): ReactiveRepository { if (typeof entityClassOrName === "string") { return this.connection.getReactiveRepository(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(entityClassOrName: ConstructorFunction|string): TreeReactiveRepository { + protected obtainReactiveTreeRepository(entityClassOrName: ObjectType|string): TreeReactiveRepository { if (typeof entityClassOrName === "string") { return this.connection.getReactiveTreeRepository(entityClassOrName); } else { diff --git a/src/entity-manager/EntityManager.ts b/src/entity-manager/EntityManager.ts index f71ad6a59..8689568ca 100644 --- a/src/entity-manager/EntityManager.ts +++ b/src/entity-manager/EntityManager.ts @@ -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(entityClass: ConstructorFunction): Promise; + find(entityClass: ObjectType): Promise; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, conditions: Object): Promise; + find(entityClass: ObjectType, conditions: Object): Promise; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, options: FindOptions): Promise; + find(entityClass: ObjectType, options: FindOptions): Promise; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, conditions: Object, options: FindOptions): Promise; + find(entityClass: ObjectType, conditions: Object, options: FindOptions): Promise; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { + find(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { 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(entityClass: ConstructorFunction): Promise<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, conditions: Object): Promise<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, conditions: Object): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, options: FindOptions): Promise<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, options: FindOptions): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, conditions: Object, options: FindOptions): Promise<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise<[Entity[], number]> { + findAndCount(entityClass: ObjectType, 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(entityClass: ConstructorFunction): Promise; + findOne(entityClass: ObjectType): Promise; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, conditions: Object): Promise; + findOne(entityClass: ObjectType, conditions: Object): Promise; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, options: FindOptions): Promise; + findOne(entityClass: ObjectType, options: FindOptions): Promise; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, conditions: Object, options: FindOptions): Promise; + findOne(entityClass: ObjectType, conditions: Object, options: FindOptions): Promise; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { + findOne(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Promise { 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(entityClass: ConstructorFunction, id: any, options?: FindOptions): Promise { + findOneById(entityClass: ObjectType, id: any, options?: FindOptions): Promise { return this.obtainRepository(entityClass).findOneById(id, options); } diff --git a/src/entity-manager/ReactiveEntityManager.ts b/src/entity-manager/ReactiveEntityManager.ts index e2b82ab04..d7a2af5fb 100644 --- a/src/entity-manager/ReactiveEntityManager.ts +++ b/src/entity-manager/ReactiveEntityManager.ts @@ -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(entityClass: ConstructorFunction): Rx.Observable; + find(entityClass: ObjectType): Rx.Observable; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, conditions: Object): Rx.Observable; + find(entityClass: ObjectType, conditions: Object): Rx.Observable; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, options: FindOptions): Rx.Observable; + find(entityClass: ObjectType, options: FindOptions): Rx.Observable; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, conditions: Object, options: FindOptions): Rx.Observable; + find(entityClass: ObjectType, conditions: Object, options: FindOptions): Rx.Observable; /** * Finds entities that match given conditions. */ - find(entityClass: ConstructorFunction, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable { + find(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable { 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(entityClass: ConstructorFunction): Rx.Observable<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType): Rx.Observable<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, conditions: Object): Rx.Observable<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, conditions: Object): Rx.Observable<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, options: FindOptions): Rx.Observable<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, options: FindOptions): Rx.Observable<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, conditions: Object, options: FindOptions): Rx.Observable<[ Entity[], number ]>; + findAndCount(entityClass: ObjectType, conditions: Object, options: FindOptions): Rx.Observable<[ Entity[], number ]>; /** * Finds entities that match given conditions. */ - findAndCount(entityClass: ConstructorFunction, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable<[Entity[], number]> { + findAndCount(entityClass: ObjectType, 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(entityClass: ConstructorFunction): Rx.Observable; + findOne(entityClass: ObjectType): Rx.Observable; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, conditions: Object): Rx.Observable; + findOne(entityClass: ObjectType, conditions: Object): Rx.Observable; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, options: FindOptions): Rx.Observable; + findOne(entityClass: ObjectType, options: FindOptions): Rx.Observable; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, conditions: Object, options: FindOptions): Rx.Observable; + findOne(entityClass: ObjectType, conditions: Object, options: FindOptions): Rx.Observable; /** * Finds first entity that matches given conditions. */ - findOne(entityClass: ConstructorFunction, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable { + findOne(entityClass: ObjectType, conditionsOrFindOptions?: Object|FindOptions, options?: FindOptions): Rx.Observable { 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(entityClass: ConstructorFunction, id: any, options?: FindOptions): Rx.Observable { + findOneById(entityClass: ObjectType, id: any, options?: FindOptions): Rx.Observable { return this.getReactiveRepository(entityClass).findOneById(id, options); } diff --git a/src/index.ts b/src/index.ts index 0fcc3f143..f34b54395 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"; diff --git a/src/repository/FindOptions.ts b/src/repository/FindOptions.ts index f297fd7c6..e0245b987 100644 --- a/src/repository/FindOptions.ts +++ b/src/repository/FindOptions.ts @@ -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. diff --git a/src/repository/SpecificRepository.ts b/src/repository/SpecificRepository.ts index 9addcddf7..84a322db5 100644 --- a/src/repository/SpecificRepository.ts +++ b/src/repository/SpecificRepository.ts @@ -305,7 +305,7 @@ export class SpecificRepository { .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; diff --git a/test/functional/persistence/many-to-many/persistence-many-to-many.ts b/test/functional/persistence/many-to-many/persistence-many-to-many.ts index 6bd303fe6..c6817b6fe 100644 --- a/test/functional/persistence/many-to-many/persistence-many-to-many.ts +++ b/test/functional/persistence/many-to-many/persistence-many-to-many.ts @@ -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 diff --git a/test/functional/repository/delete-by-id-and-ids/repository-remove-by-id-and-ids.ts b/test/functional/repository/delete-by-id-and-ids/repository-remove-by-id-and-ids.ts index 2fba06acf..d1f16d056 100644 --- a/test/functional/repository/delete-by-id-and-ids/repository-remove-by-id-and-ids.ts +++ b/test/functional/repository/delete-by-id-and-ids/repository-remove-by-id-and-ids.ts @@ -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 diff --git a/test/functional/repository/set-add-remove-relations/repository-set-add-remove-relations.ts b/test/functional/repository/set-add-remove-relations/repository-set-add-remove-relations.ts index 8f755fe4b..1271aeb3f 100644 --- a/test/functional/repository/set-add-remove-relations/repository-set-add-remove-relations.ts +++ b/test/functional/repository/set-add-remove-relations/repository-set-add-remove-relations.ts @@ -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 diff --git a/test/utils/test-utils.ts b/test/utils/test-utils.ts index 3772f3399..ca75e546f 100644 --- a/test/utils/test-utils.ts +++ b/test/utils/test-utils.ts @@ -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 };