Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create EntityTarget and use instead of EntitySchema / ObjectType / etc #6701

Merged
merged 1 commit into from Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/common/EntityTarget.ts
@@ -0,0 +1,11 @@
import {ObjectType} from "./ObjectType";
import {EntitySchema} from "..";

/**
* Entity target.
*/
export type EntityTarget<Entity> =
| ObjectType<Entity>
| EntitySchema<Entity>
| string
| { type: Entity, name: string };
21 changes: 11 additions & 10 deletions src/connection/Connection.ts
@@ -1,6 +1,7 @@
import {Driver} from "../driver/Driver";
import {Repository} from "../repository/Repository";
import {EntitySubscriberInterface} from "../subscriber/EntitySubscriberInterface";
import {EntityTarget} from "../common/EntityTarget";
import {ObjectType} from "../common/ObjectType";
import {EntityManager} from "../entity-manager/EntityManager";
import {DefaultNamingStrategy} from "../naming-strategy/DefaultNamingStrategy";
Expand Down Expand Up @@ -320,14 +321,14 @@ export class Connection {
/**
* Checks if entity metadata exist for the given entity class, target name or table name.
*/
hasMetadata(target: Function|EntitySchema<any>|string): boolean {
hasMetadata(target: EntityTarget<any>): boolean {
return !!this.findMetadata(target);
}

/**
* Gets entity metadata for the given entity class or schema name.
*/
getMetadata(target: Function|EntitySchema<any>|string): EntityMetadata {
getMetadata(target: EntityTarget<any>): EntityMetadata {
const metadata = this.findMetadata(target);
if (!metadata)
throw new EntityMetadataNotFoundError(target);
Expand All @@ -338,23 +339,23 @@ export class Connection {
/**
* Gets repository for the given entity.
*/
getRepository<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string): Repository<Entity> {
getRepository<Entity>(target: EntityTarget<Entity>): Repository<Entity> {
return this.manager.getRepository(target);
}

/**
* Gets tree repository for the given entity class or name.
* Only tree-type entities can have a TreeRepository, like ones decorated with @Tree decorator.
*/
getTreeRepository<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string): TreeRepository<Entity> {
getTreeRepository<Entity>(target: EntityTarget<Entity>): TreeRepository<Entity> {
return this.manager.getTreeRepository(target);
}

/**
* Gets mongodb-specific repository for the given entity class or name.
* Works only if connection is mongodb-specific.
*/
getMongoRepository<Entity>(target: ObjectType<Entity>|EntitySchema<Entity>|string): MongoRepository<Entity> {
getMongoRepository<Entity>(target: EntityTarget<Entity>): MongoRepository<Entity> {
if (!(this.driver instanceof MongoDriver))
throw new Error(`You can use getMongoRepository only for MongoDB connections.`);

Expand Down Expand Up @@ -408,7 +409,7 @@ export class Connection {
/**
* Creates a new query builder that can be used to build a sql query.
*/
createQueryBuilder<Entity>(entityClass: ObjectType<Entity>|EntitySchema<Entity>|Function|string, alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;
createQueryBuilder<Entity>(entityClass: EntityTarget<Entity>, alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;

/**
* Creates a new query builder that can be used to build a sql query.
Expand All @@ -418,12 +419,12 @@ export class Connection {
/**
* Creates a new query builder that can be used to build a sql query.
*/
createQueryBuilder<Entity>(entityOrRunner?: ObjectType<Entity>|EntitySchema<Entity>|Function|string|QueryRunner, alias?: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity> {
createQueryBuilder<Entity>(entityOrRunner?: EntityTarget<Entity>|QueryRunner, alias?: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity> {
if (this instanceof MongoEntityManager)
throw new Error(`Query Builder is not supported by MongoDB.`);

if (alias) {
const metadata = this.getMetadata(entityOrRunner as Function|EntitySchema<Entity>|string);
const metadata = this.getMetadata(entityOrRunner as EntityTarget<Entity>);
return new SelectQueryBuilder(this, queryRunner)
.select(alias)
.from(metadata.target, alias);
Expand Down Expand Up @@ -453,7 +454,7 @@ export class Connection {
/**
* Gets entity metadata of the junction table (many-to-many table).
*/
getManyToManyMetadata(entityTarget: Function|string, relationPropertyPath: string) {
getManyToManyMetadata(entityTarget: EntityTarget<any>, relationPropertyPath: string) {
const relationMetadata = this.getMetadata(entityTarget).findRelationWithPropertyPath(relationPropertyPath);
if (!relationMetadata)
throw new Error(`Relation "${relationPropertyPath}" was not found in ${entityTarget} entity.`);
Expand All @@ -478,7 +479,7 @@ export class Connection {
/**
* Finds exist entity metadata by the given entity class, target name or table name.
*/
protected findMetadata(target: Function|EntitySchema<any>|string): EntityMetadata|undefined {
protected findMetadata(target: EntityTarget<any>): EntityMetadata|undefined {
return this.entityMetadatas.find(metadata => {
if (metadata.target === target)
return true;
Expand Down