/
AbstractRepository.ts
112 lines (94 loc) · 4.71 KB
/
AbstractRepository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {ObjectLiteral} from "../common/ObjectLiteral";
import {EntityManager} from "../entity-manager/EntityManager";
import {Repository} from "./Repository";
import {TreeRepository} from "./TreeRepository";
import {ObjectType} from "../common/ObjectType";
import {CustomRepositoryDoesNotHaveEntityError} from "../error/CustomRepositoryDoesNotHaveEntityError";
import {getMetadataArgsStorage} from "../index";
import {CustomRepositoryNotFoundError} from "../error/CustomRepositoryNotFoundError";
import {SelectQueryBuilder} from "../query-builder/SelectQueryBuilder";
/**
* Provides abstract class for custom repositories that do not inherit from original orm Repository.
* Contains all most-necessary methods to simplify code in the custom repository.
* All methods are protected thus not exposed and it allows to create encapsulated custom repository.
*
* @experimental
*/
export class AbstractRepository<Entity extends ObjectLiteral> {
// -------------------------------------------------------------------------
// Protected Methods Set Dynamically
// -------------------------------------------------------------------------
/**
* Gets entity manager that allows to perform repository operations with any entity.
*/
protected manager: EntityManager;
// -------------------------------------------------------------------------
// Protected Accessors
// -------------------------------------------------------------------------
/**
* Gets the original ORM repository for the entity that is managed by this repository.
* If current repository does not manage any entity, then exception will be thrown.
*/
protected get repository(): Repository<Entity> {
const target = this.getCustomRepositoryTarget(this as any);
if (!target)
throw new CustomRepositoryDoesNotHaveEntityError(this.constructor);
return this.manager.getRepository<Entity>(target);
}
/**
* Gets the original ORM tree repository for the entity that is managed by this repository.
* If current repository does not manage any entity, then exception will be thrown.
*/
protected get treeRepository(): TreeRepository<Entity> {
const target = this.getCustomRepositoryTarget(this as any);
if (!target)
throw new CustomRepositoryDoesNotHaveEntityError(this.constructor);
return this.manager.getTreeRepository<Entity>(target);
}
// -------------------------------------------------------------------------
// Protected Methods
// -------------------------------------------------------------------------
/**
* Creates a new query builder for the repository's entity that can be used to build a sql query.
* If current repository does not manage any entity, then exception will be thrown.
*/
protected createQueryBuilder(alias: string): SelectQueryBuilder<Entity> {
const target = this.getCustomRepositoryTarget(this.constructor);
if (!target)
throw new CustomRepositoryDoesNotHaveEntityError(this.constructor);
return this.manager.getRepository<Entity>(target).createQueryBuilder(alias);
}
/**
* Creates a new query builder for the given entity that can be used to build a sql query.
*/
protected createQueryBuilderFor<T>(entity: ObjectType<T>, alias: string): SelectQueryBuilder<T> {
return this.getRepositoryFor(entity).createQueryBuilder(alias);
}
/**
* Gets the original ORM repository for the given entity class.
*/
protected getRepositoryFor<T>(entity: ObjectType<T>): Repository<T> {
return this.manager.getRepository(entity);
}
/**
* Gets the original ORM tree repository for the given entity class.
*/
protected getTreeRepositoryFor<T>(entity: ObjectType<T>): TreeRepository<T> {
return this.manager.getTreeRepository(entity);
}
// -------------------------------------------------------------------------
// Private Methods
// -------------------------------------------------------------------------
/**
* 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 {
const entityRepositoryMetadataArgs = getMetadataArgsStorage().entityRepositories.find(repository => {
return repository.target === (customRepository instanceof Function ? customRepository : (customRepository as any).constructor);
});
if (!entityRepositoryMetadataArgs)
throw new CustomRepositoryNotFoundError(customRepository);
return entityRepositoryMetadataArgs.entity;
}
}