diff --git a/src/common/ConstructorFunction.ts b/src/common/ConstructorFunction.ts new file mode 100644 index 000000000..b7155fdc7 --- /dev/null +++ b/src/common/ConstructorFunction.ts @@ -0,0 +1,6 @@ +/** + * Used to get a type of the creating Function. + */ +export interface ConstructorFunction { + new (): T; +} diff --git a/src/connection/Connection.ts b/src/connection/Connection.ts index 0a4439715..a752746e4 100644 --- a/src/connection/Connection.ts +++ b/src/connection/Connection.ts @@ -8,6 +8,7 @@ import {BroadcasterNotFoundError} from "./error/BroadcasterNotFoundError"; import {EntityMetadata} from "../metadata-builder/metadata/EntityMetadata"; import {SchemaCreator} from "../schema-creator/SchemaCreator"; import {MetadataNotFoundError} from "./error/MetadataNotFoundError"; +import {ConstructorFunction} from "../common/ConstructorFunction"; interface RepositoryAndMetadata { repository: Repository; @@ -138,7 +139,7 @@ export class Connection { /** * Gets repository for the given entity class. */ - getRepository(entityClass: Function): Repository { + getRepository(entityClass: ConstructorFunction): Repository { const metadata = this.getMetadata(entityClass); const repoMeta = this.repositoryAndMetadatas.find(repoMeta => repoMeta.metadata === metadata); if (!repoMeta) @@ -161,7 +162,7 @@ export class Connection { /** * Gets the broadcaster for the given entity class. */ - getBroadcaster(entityClass: Function): OrmBroadcaster { + getBroadcaster(entityClass: ConstructorFunction): OrmBroadcaster { const metadata = this.broadcasters.find(broadcaster => broadcaster.entityClass === entityClass); if (!metadata) throw new BroadcasterNotFoundError(entityClass); @@ -180,7 +181,7 @@ export class Connection { private createRepoMeta(metadata: EntityMetadata): RepositoryAndMetadata { return { metadata: metadata, - repository: new Repository(this, metadata, this.getBroadcaster(metadata.target)) + repository: new Repository(this, metadata, this.getBroadcaster( metadata.target)) }; } diff --git a/src/decorator/OrmRepository.ts b/src/decorator/OrmRepository.ts index 5b30ef928..3dca310f8 100644 --- a/src/decorator/OrmRepository.ts +++ b/src/decorator/OrmRepository.ts @@ -1,4 +1,5 @@ import {ConnectionManager} from "../connection/ConnectionManager"; +import {ConstructorFunction} from "../common/ConstructorFunction"; // todo: should this decorator be outside of this module? // todo: also create "inject" version of this to allow to inject to properties @@ -22,7 +23,7 @@ export function OrmRepository(cls: Function, connectionName?: string): Function getValue: () => { const connectionManager: ConnectionManager = container.get(ConnectionManager); const connection = connectionManager.getConnection(connectionName); - return connection.getRepository(cls); + return connection.getRepository( cls); } }); }; diff --git a/src/decorator/relations/ManyToMany.ts b/src/decorator/relations/ManyToMany.ts index d76accac5..d953a1082 100644 --- a/src/decorator/relations/ManyToMany.ts +++ b/src/decorator/relations/ManyToMany.ts @@ -1,21 +1,22 @@ import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; -import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes"; +import {RelationTypes} from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; +import {ConstructorFunction} from "../../common/ConstructorFunction"; /** * 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) => ObjectConstructor, options?: RelationOptions): Function; +export function ManyToMany(typeFunction: (type?: any) => ConstructorFunction, 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) => ObjectConstructor, +export function ManyToMany(typeFunction: (type?: any) => ConstructorFunction, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -24,7 +25,7 @@ export function ManyToMany(typeFunction: (type?: any) => ObjectConstructor * 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) => ObjectConstructor, +export function ManyToMany(typeFunction: (type?: any) => ConstructorFunction, inverseSideOrOptions?: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/decorator/relations/ManyToManyInverse.ts b/src/decorator/relations/ManyToManyInverse.ts index 48e55d85b..2295ffb1d 100644 --- a/src/decorator/relations/ManyToManyInverse.ts +++ b/src/decorator/relations/ManyToManyInverse.ts @@ -1,21 +1,22 @@ import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; -import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes"; +import {RelationTypes} from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; +import {ConstructorFunction} from "../../common/ConstructorFunction"; /** * 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 inverse side of the relationship. */ -export function ManyToManyInverse(typeFunction: (type?: any) => ObjectConstructor, options?: RelationOptions): Function; +export function ManyToManyInverse(typeFunction: (type?: any) => ConstructorFunction, 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 inverse side of the relationship. */ -export function ManyToManyInverse(typeFunction: (type?: any) => ObjectConstructor, +export function ManyToManyInverse(typeFunction: (type?: any) => ConstructorFunction, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -24,7 +25,7 @@ export function ManyToManyInverse(typeFunction: (type?: any) => ObjectConstru * multiple instances of Entity1. To achieve it, this type of relation creates a junction table, where it storage * entity1 and entity2 ids. This is inverse side of the relationship. */ -export function ManyToManyInverse(typeFunction: (type?: any) => ObjectConstructor, +export function ManyToManyInverse(typeFunction: (type?: any) => ConstructorFunction, 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 c472df593..8c7ea0957 100644 --- a/src/decorator/relations/ManyToOne.ts +++ b/src/decorator/relations/ManyToOne.ts @@ -1,21 +1,22 @@ import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; -import {RelationTypes, ObjectConstructor} from "../../metadata-builder/types/RelationTypes"; +import {RelationTypes} from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; +import {ConstructorFunction} from "../../common/ConstructorFunction"; /** * 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) => ObjectConstructor, options?: RelationOptions): Function; +export function ManyToOne(typeFunction: (type?: any) => ConstructorFunction, 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) => ObjectConstructor, +export function ManyToOne(typeFunction: (type?: any) => ConstructorFunction, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -24,7 +25,7 @@ export function ManyToOne(typeFunction: (type?: any) => ObjectConstructor, * 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) => ObjectConstructor, +export function ManyToOne(typeFunction: (type?: any) => ConstructorFunction, 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 b1b97d311..dc9d68dc9 100644 --- a/src/decorator/relations/OneToMany.ts +++ b/src/decorator/relations/OneToMany.ts @@ -1,19 +1,20 @@ import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; -import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes"; +import {RelationTypes} from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; +import {ConstructorFunction} from "../../common/ConstructorFunction"; /** * 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) => ObjectConstructor, options?: RelationOptions): Function; +export function OneToMany(typeFunction: (type?: any) => ConstructorFunction, options?: RelationOptions): Function; /** * 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) => ObjectConstructor, +export function OneToMany(typeFunction: (type?: any) => ConstructorFunction, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -21,7 +22,7 @@ export function OneToMany(typeFunction: (type?: any) => ObjectConstructor, * 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) => ObjectConstructor, +export function OneToMany(typeFunction: (type?: any) => ConstructorFunction, 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 a159952b6..9ad72b6be 100644 --- a/src/decorator/relations/OneToOne.ts +++ b/src/decorator/relations/OneToOne.ts @@ -1,19 +1,20 @@ import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; -import {ObjectConstructor, RelationTypes} from "../../metadata-builder/types/RelationTypes"; +import {RelationTypes} from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; +import {ConstructorFunction} from "../../common/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) => ObjectConstructor, options?: RelationOptions): Function; +export function OneToOne(typeFunction: (type?: any) => ConstructorFunction, 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) => ObjectConstructor, +export function OneToOne(typeFunction: (type?: any) => ConstructorFunction, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -21,7 +22,7 @@ export function OneToOne(typeFunction: (type?: any) => ObjectConstructor, * 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) => ObjectConstructor, +export function OneToOne(typeFunction: (type?: any) => ConstructorFunction, inverseSideOrOptions?: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/decorator/relations/OneToOneInverse.ts b/src/decorator/relations/OneToOneInverse.ts index 1d428a899..e712dae63 100644 --- a/src/decorator/relations/OneToOneInverse.ts +++ b/src/decorator/relations/OneToOneInverse.ts @@ -1,21 +1,22 @@ import {RelationMetadata} from "../../metadata-builder/metadata/RelationMetadata"; import {RelationOptions} from "../../metadata-builder/options/RelationOptions"; -import {RelationTypes, ObjectConstructor} from "../../metadata-builder/types/RelationTypes"; +import {RelationTypes} from "../../metadata-builder/types/RelationTypes"; import {defaultMetadataStorage} from "../../metadata-builder/MetadataStorage"; +import {ConstructorFunction} from "../../common/ConstructorFunction"; /** * Inverse side of the one-to-one relation. One-to-one relation allows to create direct relation between two entities. * Entity2 have only one Entity1. Entity2 is inverse side of the relation on Entity1. Does not storage id of the * Entity1. Entity1's id is storage on the one-to-one owner side. */ -export function OneToOneInverse(typeFunction: (type?: any) => ObjectConstructor, options?: RelationOptions): Function; +export function OneToOneInverse(typeFunction: (type?: any) => ConstructorFunction, options?: RelationOptions): Function; /** * Inverse side of the one-to-one relation. One-to-one relation allows to create direct relation between two entities. * Entity2 have only one Entity1. Entity2 is inverse side of the relation on Entity1. Does not storage id of the * Entity1. Entity1's id is storage on the one-to-one owner side. */ -export function OneToOneInverse(typeFunction: (type?: any) => ObjectConstructor, +export function OneToOneInverse(typeFunction: (type?: any) => ConstructorFunction, inverseSide?: string|((object: T) => any), options?: RelationOptions): Function; @@ -24,7 +25,7 @@ export function OneToOneInverse(typeFunction: (type?: any) => ObjectConstruct * Entity2 have only one Entity1. Entity2 is inverse side of the relation on Entity1. Does not storage id of the * Entity1. Entity1's id is storage on the one-to-one owner side. */ -export function OneToOneInverse(typeFunction: (type?: any) => ObjectConstructor, +export function OneToOneInverse(typeFunction: (type?: any) => ConstructorFunction, inverseSideOrOptions?: string|((object: T) => any)|RelationOptions, options?: RelationOptions): Function { let inverseSideProperty: string|((object: T) => any); diff --git a/src/metadata-builder/types/RelationTypes.ts b/src/metadata-builder/types/RelationTypes.ts index 6e27f661e..1ffad2721 100644 --- a/src/metadata-builder/types/RelationTypes.ts +++ b/src/metadata-builder/types/RelationTypes.ts @@ -3,13 +3,6 @@ */ export type RelationType = "one-to-one"|"one-to-many"|"many-to-one"|"many-to-many"; -/** - * Used to get a type of the creating Function. - */ -export interface ObjectConstructor { - new (): T; -} - /** * Provides a constants for each relation type. */ diff --git a/test/integration/sample1-simple-entity.ts b/test/integration/sample1-simple-entity.ts index 358b07ba0..0099dec8d 100644 --- a/test/integration/sample1-simple-entity.ts +++ b/test/integration/sample1-simple-entity.ts @@ -44,7 +44,7 @@ describe("insertion", function() { let postRepository: Repository; beforeEach(function() { - postRepository = connection.getRepository(Post); + postRepository = connection.getRepository(Post); }); // ------------------------------------------------------------------------- diff --git a/test/integration/sample2-one-to-one.ts b/test/integration/sample2-one-to-one.ts index 9e49f7d15..200df8457 100644 --- a/test/integration/sample2-one-to-one.ts +++ b/test/integration/sample2-one-to-one.ts @@ -54,11 +54,11 @@ describe("one-to-one", function() { postImageRepository: Repository, postMetadataRepository: Repository; before(function() { - postRepository = connection.getRepository(Post); - postDetailsRepository = connection.getRepository(PostDetails); - postCategoryRepository = connection.getRepository(PostCategory); - postImageRepository = connection.getRepository(PostImage); - postMetadataRepository = connection.getRepository(PostMetadata); + postRepository = connection.getRepository(Post); + postDetailsRepository = connection.getRepository(PostDetails); + postCategoryRepository = connection.getRepository(PostCategory); + postImageRepository = connection.getRepository(PostImage); + postMetadataRepository = connection.getRepository(PostMetadata); }); // ------------------------------------------------------------------------- diff --git a/test/integration/sample3-many-to-one.ts b/test/integration/sample3-many-to-one.ts index 0ea9ac2f4..ad2ff5056 100644 --- a/test/integration/sample3-many-to-one.ts +++ b/test/integration/sample3-many-to-one.ts @@ -54,11 +54,11 @@ describe("many-to-one", function() { postImageRepository: Repository, postMetadataRepository: Repository; before(function() { - postRepository = connection.getRepository(Post); - postDetailsRepository = connection.getRepository(PostDetails); - postCategoryRepository = connection.getRepository(PostCategory); - postImageRepository = connection.getRepository(PostImage); - postMetadataRepository = connection.getRepository(PostMetadata); + postRepository = connection.getRepository(Post); + postDetailsRepository = connection.getRepository(PostDetails); + postCategoryRepository = connection.getRepository(PostCategory); + postImageRepository = connection.getRepository(PostImage); + postMetadataRepository = connection.getRepository(PostMetadata); }); // ------------------------------------------------------------------------- diff --git a/test/integration/sample4-many-to-many.ts b/test/integration/sample4-many-to-many.ts index 213184260..801d31eeb 100644 --- a/test/integration/sample4-many-to-many.ts +++ b/test/integration/sample4-many-to-many.ts @@ -56,11 +56,11 @@ describe("many-to-many", function() { postImageRepository: Repository, postMetadataRepository: Repository; before(function() { - postRepository = connection.getRepository(Post); - postDetailsRepository = connection.getRepository(PostDetails); - postCategoryRepository = connection.getRepository(PostCategory); - postImageRepository = connection.getRepository(PostImage); - postMetadataRepository = connection.getRepository(PostMetadata); + postRepository = connection.getRepository(Post); + postDetailsRepository = connection.getRepository(PostDetails); + postCategoryRepository = connection.getRepository(PostCategory); + postImageRepository = connection.getRepository(PostImage); + postMetadataRepository = connection.getRepository(PostMetadata); }); // -------------------------------------------------------------------------