mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added some magic to repositories from connection using generics
This commit is contained in:
parent
60eadca654
commit
bf9bfd69a6
6
src/common/ConstructorFunction.ts
Normal file
6
src/common/ConstructorFunction.ts
Normal file
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Used to get a type of the creating Function.
|
||||
*/
|
||||
export interface ConstructorFunction<T> {
|
||||
new (): T;
|
||||
}
|
||||
@ -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<any>;
|
||||
@ -138,7 +139,7 @@ export class Connection {
|
||||
/**
|
||||
* Gets repository for the given entity class.
|
||||
*/
|
||||
getRepository<Entity>(entityClass: Function): Repository<Entity> {
|
||||
getRepository<Entity>(entityClass: ConstructorFunction<Entity>): Repository<Entity> {
|
||||
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<Entity>(entityClass: Function): OrmBroadcaster<Entity> {
|
||||
getBroadcaster<Entity>(entityClass: ConstructorFunction<Entity>): OrmBroadcaster<Entity> {
|
||||
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<any>(this, metadata, this.getBroadcaster(metadata.target))
|
||||
repository: new Repository<any>(this, metadata, this.getBroadcaster(<any> metadata.target))
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -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(<any> cls);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -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<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
|
||||
export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<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) => ObjectConstructor<T>,
|
||||
export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
@ -24,7 +25,7 @@ export function ManyToMany<T>(typeFunction: (type?: any) => ObjectConstructor<T>
|
||||
* 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) => ObjectConstructor<T>,
|
||||
export function ManyToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
|
||||
@ -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<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
|
||||
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFunction<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 inverse side of the relationship.
|
||||
*/
|
||||
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
|
||||
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
@ -24,7 +25,7 @@ export function ManyToManyInverse<T>(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<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
|
||||
export function ManyToManyInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
|
||||
@ -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<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
|
||||
export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<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) => ObjectConstructor<T>,
|
||||
export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
@ -24,7 +25,7 @@ export function ManyToOne<T>(typeFunction: (type?: any) => ObjectConstructor<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) => ObjectConstructor<T>,
|
||||
export function ManyToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
|
||||
@ -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<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
|
||||
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>, 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<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
|
||||
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
@ -21,7 +22,7 @@ export function OneToMany<T>(typeFunction: (type?: any) => ObjectConstructor<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) => ObjectConstructor<T>,
|
||||
export function OneToMany<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
|
||||
@ -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<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
|
||||
export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<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) => ObjectConstructor<T>,
|
||||
export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
@ -21,7 +22,7 @@ export function OneToOne<T>(typeFunction: (type?: any) => ObjectConstructor<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) => ObjectConstructor<T>,
|
||||
export function OneToOne<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
|
||||
@ -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<T>(typeFunction: (type?: any) => ObjectConstructor<T>, options?: RelationOptions): Function;
|
||||
export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>, 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<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
|
||||
export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSide?: string|((object: T) => any),
|
||||
options?: RelationOptions): Function;
|
||||
|
||||
@ -24,7 +25,7 @@ export function OneToOneInverse<T>(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<T>(typeFunction: (type?: any) => ObjectConstructor<T>,
|
||||
export function OneToOneInverse<T>(typeFunction: (type?: any) => ConstructorFunction<T>,
|
||||
inverseSideOrOptions?: string|((object: T) => any)|RelationOptions,
|
||||
options?: RelationOptions): Function {
|
||||
let inverseSideProperty: string|((object: T) => any);
|
||||
|
||||
@ -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<T> {
|
||||
new (): T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a constants for each relation type.
|
||||
*/
|
||||
|
||||
@ -44,7 +44,7 @@ describe("insertion", function() {
|
||||
|
||||
let postRepository: Repository<Post>;
|
||||
beforeEach(function() {
|
||||
postRepository = connection.getRepository<Post>(Post);
|
||||
postRepository = connection.getRepository(Post);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -54,11 +54,11 @@ describe("one-to-one", function() {
|
||||
postImageRepository: Repository<PostImage>,
|
||||
postMetadataRepository: Repository<PostMetadata>;
|
||||
before(function() {
|
||||
postRepository = connection.getRepository<Post>(Post);
|
||||
postDetailsRepository = connection.getRepository<PostDetails>(PostDetails);
|
||||
postCategoryRepository = connection.getRepository<PostCategory>(PostCategory);
|
||||
postImageRepository = connection.getRepository<PostImage>(PostImage);
|
||||
postMetadataRepository = connection.getRepository<PostMetadata>(PostMetadata);
|
||||
postRepository = connection.getRepository(Post);
|
||||
postDetailsRepository = connection.getRepository(PostDetails);
|
||||
postCategoryRepository = connection.getRepository(PostCategory);
|
||||
postImageRepository = connection.getRepository(PostImage);
|
||||
postMetadataRepository = connection.getRepository(PostMetadata);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -54,11 +54,11 @@ describe("many-to-one", function() {
|
||||
postImageRepository: Repository<PostImage>,
|
||||
postMetadataRepository: Repository<PostMetadata>;
|
||||
before(function() {
|
||||
postRepository = connection.getRepository<Post>(Post);
|
||||
postDetailsRepository = connection.getRepository<PostDetails>(PostDetails);
|
||||
postCategoryRepository = connection.getRepository<PostCategory>(PostCategory);
|
||||
postImageRepository = connection.getRepository<PostImage>(PostImage);
|
||||
postMetadataRepository = connection.getRepository<PostMetadata>(PostMetadata);
|
||||
postRepository = connection.getRepository(Post);
|
||||
postDetailsRepository = connection.getRepository(PostDetails);
|
||||
postCategoryRepository = connection.getRepository(PostCategory);
|
||||
postImageRepository = connection.getRepository(PostImage);
|
||||
postMetadataRepository = connection.getRepository(PostMetadata);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -56,11 +56,11 @@ describe("many-to-many", function() {
|
||||
postImageRepository: Repository<PostImage>,
|
||||
postMetadataRepository: Repository<PostMetadata>;
|
||||
before(function() {
|
||||
postRepository = connection.getRepository<Post>(Post);
|
||||
postDetailsRepository = connection.getRepository<PostDetails>(PostDetails);
|
||||
postCategoryRepository = connection.getRepository<PostCategory>(PostCategory);
|
||||
postImageRepository = connection.getRepository<PostImage>(PostImage);
|
||||
postMetadataRepository = connection.getRepository<PostMetadata>(PostMetadata);
|
||||
postRepository = connection.getRepository(Post);
|
||||
postDetailsRepository = connection.getRepository(PostDetails);
|
||||
postCategoryRepository = connection.getRepository(PostCategory);
|
||||
postImageRepository = connection.getRepository(PostImage);
|
||||
postMetadataRepository = connection.getRepository(PostMetadata);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user