Skip to content

Commit

Permalink
fix: allow EntitySchema to be passed to EntityRepository (#4884)
Browse files Browse the repository at this point in the history
* fix: allow EntitySchema to be passed to EntityRepository

* fix: listenTo's result must not be a string, but it is compared to entity name when using schemas
  • Loading branch information
da-mkay authored and pleerock committed Oct 12, 2019
1 parent 58ac18a commit 652a20e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/decorator/EntityRepository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {getMetadataArgsStorage} from "../";
import {EntityRepositoryMetadataArgs} from "../metadata-args/EntityRepositoryMetadataArgs";
import { getMetadataArgsStorage } from "../";
import { EntityRepositoryMetadataArgs } from "../metadata-args/EntityRepositoryMetadataArgs";
import { EntitySchema } from "../entity-schema/EntitySchema";

/**
* Used to declare a class as a custom repository.
* Custom repository can manage some specific entity or just be generic.
* Custom repository optionally can extend AbstractRepository, Repository or TreeRepository.
*/
export function EntityRepository(entity?: Function): Function {
export function EntityRepository(entity?: Function | EntitySchema<any>): Function {
return function (target: Function) {

getMetadataArgsStorage().entityRepositories.push({
Expand Down
4 changes: 3 additions & 1 deletion src/metadata-args/EntityRepositoryMetadataArgs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EntitySchema } from "../entity-schema/EntitySchema";

/**
* Arguments for EntityRepositoryMetadata class, helps to construct an EntityRepositoryMetadata object.
*/
Expand All @@ -11,6 +13,6 @@ export interface EntityRepositoryMetadataArgs {
/**
* Entity managed by this custom repository.
*/
readonly entity?: Function|string;
readonly entity?: Function|string|EntitySchema<any>;

}
3 changes: 2 additions & 1 deletion src/repository/AbstractRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {CustomRepositoryDoesNotHaveEntityError} from "../error/CustomRepositoryD
import {getMetadataArgsStorage} from "../index";
import {CustomRepositoryNotFoundError} from "../error/CustomRepositoryNotFoundError";
import {SelectQueryBuilder} from "../query-builder/SelectQueryBuilder";
import { EntitySchema } from "../entity-schema/EntitySchema";

/**
* Provides abstract class for custom repositories that do not inherit from original orm Repository.
Expand Down Expand Up @@ -99,7 +100,7 @@ export class AbstractRepository<Entity extends ObjectLiteral> {
* Gets custom repository's managed entity.
* If given custom repository does not manage any entity then undefined will be returned.
*/
private getCustomRepositoryTarget(customRepository: any): Function|string|undefined {
private getCustomRepositoryTarget(customRepository: any): Function|string|EntitySchema<any>|undefined {
const entityRepositoryMetadataArgs = getMetadataArgsStorage().entityRepositories.find(repository => {
return repository.target === (customRepository instanceof Function ? customRepository : (customRepository as any).constructor);
});
Expand Down
2 changes: 1 addition & 1 deletion src/subscriber/EntitySubscriberInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface EntitySubscriberInterface<Entity = any> {
* Returns the class of the entity to which events will listen.
* If this method is omitted, then subscriber will listen to events of all entities.
*/
listenTo?(): Function;
listenTo?(): Function | string;

/**
* Called after entity is loaded from the database.
Expand Down

0 comments on commit 652a20e

Please sign in to comment.