UNPKG

4.75 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var CustomRepositoryDoesNotHaveEntityError_1 = require("../error/CustomRepositoryDoesNotHaveEntityError");
4var index_1 = require("../index");
5var CustomRepositoryNotFoundError_1 = require("../error/CustomRepositoryNotFoundError");
6/**
7 * Provides abstract class for custom repositories that do not inherit from original orm Repository.
8 * Contains all most-necessary methods to simplify code in the custom repository.
9 * All methods are protected thus not exposed and it allows to create encapsulated custom repository.
10 *
11 * @experimental
12 */
13var AbstractRepository = /** @class */ (function () {
14 function AbstractRepository() {
15 }
16 Object.defineProperty(AbstractRepository.prototype, "repository", {
17 // -------------------------------------------------------------------------
18 // Protected Accessors
19 // -------------------------------------------------------------------------
20 /**
21 * Gets the original ORM repository for the entity that is managed by this repository.
22 * If current repository does not manage any entity, then exception will be thrown.
23 */
24 get: function () {
25 var target = this.getCustomRepositoryTarget(this);
26 if (!target)
27 throw new CustomRepositoryDoesNotHaveEntityError_1.CustomRepositoryDoesNotHaveEntityError(this.constructor);
28 return this.manager.getRepository(target);
29 },
30 enumerable: true,
31 configurable: true
32 });
33 Object.defineProperty(AbstractRepository.prototype, "treeRepository", {
34 /**
35 * Gets the original ORM tree 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 get: function () {
39 var target = this.getCustomRepositoryTarget(this);
40 if (!target)
41 throw new CustomRepositoryDoesNotHaveEntityError_1.CustomRepositoryDoesNotHaveEntityError(this.constructor);
42 return this.manager.getTreeRepository(target);
43 },
44 enumerable: true,
45 configurable: true
46 });
47 // -------------------------------------------------------------------------
48 // Protected Methods
49 // -------------------------------------------------------------------------
50 /**
51 * Creates a new query builder for the repository's entity that can be used to build a sql query.
52 * If current repository does not manage any entity, then exception will be thrown.
53 */
54 AbstractRepository.prototype.createQueryBuilder = function (alias) {
55 var target = this.getCustomRepositoryTarget(this.constructor);
56 if (!target)
57 throw new CustomRepositoryDoesNotHaveEntityError_1.CustomRepositoryDoesNotHaveEntityError(this.constructor);
58 return this.manager.getRepository(target).createQueryBuilder(alias);
59 };
60 /**
61 * Creates a new query builder for the given entity that can be used to build a sql query.
62 */
63 AbstractRepository.prototype.createQueryBuilderFor = function (entity, alias) {
64 return this.getRepositoryFor(entity).createQueryBuilder(alias);
65 };
66 /**
67 * Gets the original ORM repository for the given entity class.
68 */
69 AbstractRepository.prototype.getRepositoryFor = function (entity) {
70 return this.manager.getRepository(entity);
71 };
72 /**
73 * Gets the original ORM tree repository for the given entity class.
74 */
75 AbstractRepository.prototype.getTreeRepositoryFor = function (entity) {
76 return this.manager.getTreeRepository(entity);
77 };
78 // -------------------------------------------------------------------------
79 // Private Methods
80 // -------------------------------------------------------------------------
81 /**
82 * Gets custom repository's managed entity.
83 * If given custom repository does not manage any entity then undefined will be returned.
84 */
85 AbstractRepository.prototype.getCustomRepositoryTarget = function (customRepository) {
86 var entityRepositoryMetadataArgs = index_1.getMetadataArgsStorage().entityRepositories.find(function (repository) {
87 return repository.target === (customRepository instanceof Function ? customRepository : customRepository.constructor);
88 });
89 if (!entityRepositoryMetadataArgs)
90 throw new CustomRepositoryNotFoundError_1.CustomRepositoryNotFoundError(customRepository);
91 return entityRepositoryMetadataArgs.entity;
92 };
93 return AbstractRepository;
94}());
95exports.AbstractRepository = AbstractRepository;
96
97//# sourceMappingURL=AbstractRepository.js.map