1
import {ObjectLiteral} from "../common/ObjectLiteral";
2
import {EntityManager} from "../entity-manager/EntityManager";
3
import {Repository} from "./Repository";
4
import {TreeRepository} from "./TreeRepository";
5
import {EntityTarget} from "../common/EntityTarget";
6
import {ObjectType} from "../common/ObjectType";
7 10
import {CustomRepositoryDoesNotHaveEntityError} from "../error/CustomRepositoryDoesNotHaveEntityError";
8 10
import {getMetadataArgsStorage} from "../index";
9 10
import {CustomRepositoryNotFoundError} from "../error/CustomRepositoryNotFoundError";
10
import {SelectQueryBuilder} from "../query-builder/SelectQueryBuilder";
11

12
/**
13
 * Provides abstract class for custom repositories that do not inherit from original orm Repository.
14
 * Contains all most-necessary methods to simplify code in the custom repository.
15
 * All methods are protected thus not exposed and it allows to create encapsulated custom repository.
16
 *
17
 * @experimental
18
 */
19 10
export class AbstractRepository<Entity extends ObjectLiteral> {
20

21
    // -------------------------------------------------------------------------
22
    // Protected Methods Set Dynamically
23
    // -------------------------------------------------------------------------
24

25
    /**
26
     * Gets entity manager that allows to perform repository operations with any entity.
27
     */
28
    protected manager: EntityManager;
29

30
    // -------------------------------------------------------------------------
31
    // Protected Accessors
32
    // -------------------------------------------------------------------------
33

34
    /**
35
     * Gets the original ORM repository for the entity that is managed by this repository.
36
     * If current repository does not manage any entity, then exception will be thrown.
37
     */
38 10
    protected get repository(): Repository<Entity> {
39 0
        const target = this.getCustomRepositoryTarget(this as any);
40 0
        if (!target)
41 0
            throw new CustomRepositoryDoesNotHaveEntityError(this.constructor);
42

43 0
        return this.manager.getRepository<Entity>(target);
44
    }
45

46
    /**
47
     * Gets the original ORM tree repository for the entity that is managed by this repository.
48
     * If current repository does not manage any entity, then exception will be thrown.
49
     */
50 10
    protected get treeRepository(): TreeRepository<Entity> {
51 0
        const target = this.getCustomRepositoryTarget(this as any);
52 0
        if (!target)
53 0
            throw new CustomRepositoryDoesNotHaveEntityError(this.constructor);
54

55 0
        return this.manager.getTreeRepository<Entity>(target);
56
    }
57

58
    // -------------------------------------------------------------------------
59
    // Protected Methods
60
    // -------------------------------------------------------------------------
61

62
    /**
63
     * Creates a new query builder for the repository's entity that can be used to build a sql query.
64
     * If current repository does not manage any entity, then exception will be thrown.
65
     */
66 10
    protected createQueryBuilder(alias: string): SelectQueryBuilder<Entity> {
67 0
        const target = this.getCustomRepositoryTarget(this.constructor);
68 0
        if (!target)
69 0
            throw new CustomRepositoryDoesNotHaveEntityError(this.constructor);
70

71 0
        return this.manager.getRepository<Entity>(target).createQueryBuilder(alias);
72
    }
73

74
    /**
75
     * Creates a new query builder for the given entity that can be used to build a sql query.
76
     */
77 10
    protected createQueryBuilderFor<T>(entity: ObjectType<T>, alias: string): SelectQueryBuilder<T> {
78 0
        return this.getRepositoryFor(entity).createQueryBuilder(alias);
79
    }
80

81
    /**
82
     * Gets the original ORM repository for the given entity class.
83
     */
84 10
    protected getRepositoryFor<T>(entity: ObjectType<T>): Repository<T> {
85 0
        return this.manager.getRepository(entity);
86
    }
87

88
    /**
89
     * Gets the original ORM tree repository for the given entity class.
90
     */
91 10
    protected getTreeRepositoryFor<T>(entity: ObjectType<T>): TreeRepository<T> {
92 0
        return this.manager.getTreeRepository(entity);
93
    }
94

95
    // -------------------------------------------------------------------------
96
    // Private Methods
97
    // -------------------------------------------------------------------------
98

99
    /**
100
     * Gets custom repository's managed entity.
101
     * If given custom repository does not manage any entity then undefined will be returned.
102
     */
103 10
    private getCustomRepositoryTarget(customRepository: any): EntityTarget<any>|undefined {
104 0
        const entityRepositoryMetadataArgs = getMetadataArgsStorage().entityRepositories.find(repository => {
105 0
            return repository.target === (customRepository instanceof Function ? customRepository : (customRepository as any).constructor);
106
        });
107 0
        if (!entityRepositoryMetadataArgs)
108 0
            throw new CustomRepositoryNotFoundError(customRepository);
109

110 0
        return entityRepositoryMetadataArgs.entity;
111
    }
112

113 10
}

Read our documentation on viewing source code .

Loading